From 7671f904c967dfe561ebb8f3ce08389963784ea9 Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Sat, 07 Feb 2009 09:05:07 +0000 Subject: Mix in Char/Ground with more OOP --- diff --git a/document.py b/document.py index fe84abb..2af457a 100644 --- a/document.py +++ b/document.py @@ -18,8 +18,8 @@ import cjson from zipfile import ZipFile import theme -from sound import Sound -from ground import Ground +from sound import * +from ground import * from utils import * from char import Frame @@ -51,8 +51,8 @@ def save(filepath): zip.writestr(arcname, value.read()) else: node['custom'] = False - node['filename'] = value.filename() node['name'] = value.name + node['id'] = value.id _save(cfg['ground'], 'ground.png', Document.ground) _save(cfg['sound'], 'sound', Document.sound) @@ -86,16 +86,15 @@ def load(filepath): zip = ZipFile(filepath, 'r') cfg = cjson.decode(zip.read('MANIFEST')) - def _load(node, klass): + def _load(node, restored_class, preinstalled_class): if node['custom']: - out = klass(node['name'], zip.read(node['filename']), - theme.RESTORED) + return restored_class(node['name'], node['id'], + zip.read(node['filename'])) else: - out = klass(node['name'], node['filename'], theme.PREINSTALLED) - return out + return preinstalled_class(node['name'], node['id']) - Document.ground = _load(cfg['ground'], Ground) - Document.sound = _load(cfg['sound'], Sound) + Document.ground = _load(cfg['ground'], RestoredGround, PreinstalledGround) + Document.sound = _load(cfg['sound'], RestoredSound, PreinstalledSound) loaded = {} for node in cfg['tape']: diff --git a/ground.py b/ground.py index ab5d07c..fdf3bf8 100644 --- a/ground.py +++ b/ground.py @@ -16,10 +16,7 @@ import os import gtk from gettext import gettext as _ -from sugar.graphics.objectchooser import ObjectChooser - import theme -from utils import pixbuf, pixbuf2str def load(): from document import Document @@ -28,30 +25,17 @@ def load(): THEMES.insert(-1, Document.ground) class Ground: - def __init__(self, name, image, type): + def __init__(self, name, id): self.name = name - self._type = type + self.id = id self._thumb = None - if type == theme.RESTORED: - tmpfile = os.path.join(theme.SESSION_PATH, '.tmp.png') - file(tmpfile, 'w').write(image) - self._orig = theme.pixbuf(tmpfile) - os.unlink(tmpfile) - self._filename = 'ground.png' - else: - self._filename = image - self._orig = theme.pixbuf(image) - def custom(self): - return self._type != theme.PREINSTALLED + return True def read(self): pixbuf2str(self._orig) - def filename(self): - return self._filename - def thumb(self): if not self._thumb: self._thumb = theme.scale(self._orig) @@ -61,28 +45,52 @@ class Ground: return self._orig def select(self): - if self._type != theme.CUSTOM: - return self - return theme.choose( - lambda title, file: Ground(title, file, theme.JOURNAL)) + return self + +class PreinstalledGround(Ground): + def __init__(self, name, filename): + Ground.__init__(self, name, filename) + self._orig = theme.pixbuf(filename) + + def custom(self): + return False + +class CustomGround(Ground): + def __init__(self, name, filename): + Ground.__init__(self, name, None) + self._orig = theme.pixbuf(filename) + + def select(self): + return theme.choose(lambda jobject: JournalGround(jobject)) + +class RestoredGround(Ground): + def __init__(self, name, id, data): + Ground.__init__(self, name, id) + self._orig = str2pixbuf(data) + +class JournalGround(Ground): + def __init__(self, jobject): + Ground.__init__(self, jobject.props.metadata['title'], + jobject.object_id) + self._orig = theme.pixbuf(jobject.file_path) THEMES = [ - Ground(_('Saturn'), 'images/backpics/bigbg01.gif', theme.PREINSTALLED), - Ground(_('Snowflakes'), 'images/backpics/bigbg02.gif', theme.PREINSTALLED), - Ground(_('Eye'), 'images/backpics/bigbg03.gif', theme.PREINSTALLED), - Ground(_('Blobs'), 'images/backpics/bigbg04.gif', theme.PREINSTALLED), - Ground(_('Star Night'), 'images/backpics/bigbg05.gif', theme.PREINSTALLED), - Ground(_('Forest'), 'images/backpics/bigbg06.gif', theme.PREINSTALLED), - Ground(_('Spiral'), 'images/backpics/bigbg07.gif', theme.PREINSTALLED), - Ground(_('Beam'), 'images/backpics/bigbg08.gif', theme.PREINSTALLED), - Ground(_('Cloth'), 'images/backpics/bigbg09.gif', theme.PREINSTALLED), - Ground(_('Faces'), 'images/backpics/bigbg10.gif', theme.PREINSTALLED), - Ground(_('Leaves'), 'images/backpics/bigbg11.gif', theme.PREINSTALLED), - Ground(_('Vegetables'), 'images/backpics/bigbg12.gif', theme.PREINSTALLED), - Ground(_('Spotlight'), 'images/backpics/bigbg13.gif', theme.PREINSTALLED), - Ground(_('Strips'), 'images/backpics/bigbg14.gif', theme.PREINSTALLED), - Ground(_('Scene'), 'images/backpics/bigbg15.gif', theme.PREINSTALLED), - Ground(_('Rhombs'), 'images/backpics/bigbg16.gif', theme.PREINSTALLED), - Ground(_('Milky Way'), 'images/backpics/bigbg17.gif', theme.PREINSTALLED), + PreinstalledGround(_('Saturn'), 'images/backpics/bigbg01.gif'), + PreinstalledGround(_('Snowflakes'), 'images/backpics/bigbg02.gif'), + PreinstalledGround(_('Eye'), 'images/backpics/bigbg03.gif'), + PreinstalledGround(_('Blobs'), 'images/backpics/bigbg04.gif'), + PreinstalledGround(_('Star Night'), 'images/backpics/bigbg05.gif'), + PreinstalledGround(_('Forest'), 'images/backpics/bigbg06.gif'), + PreinstalledGround(_('Spiral'), 'images/backpics/bigbg07.gif'), + PreinstalledGround(_('Beam'), 'images/backpics/bigbg08.gif'), + PreinstalledGround(_('Cloth'), 'images/backpics/bigbg09.gif'), + PreinstalledGround(_('Faces'), 'images/backpics/bigbg10.gif'), + PreinstalledGround(_('Leaves'), 'images/backpics/bigbg11.gif'), + PreinstalledGround(_('Vegetables'), 'images/backpics/bigbg12.gif'), + PreinstalledGround(_('Spotlight'), 'images/backpics/bigbg13.gif'), + PreinstalledGround(_('Strips'), 'images/backpics/bigbg14.gif'), + PreinstalledGround(_('Scene'), 'images/backpics/bigbg15.gif'), + PreinstalledGround(_('Rhombs'), 'images/backpics/bigbg16.gif'), + PreinstalledGround(_('Milky Way'), 'images/backpics/bigbg17.gif'), None, - Ground(_('Custom'), 'images/backpics/custom.png', theme.CUSTOM)] + CustomGround(_('Custom'), 'images/backpics/custom.png')] diff --git a/sound.py b/sound.py index 7c526b9..863595d 100644 --- a/sound.py +++ b/sound.py @@ -33,72 +33,82 @@ class Sound: current = None player = None - def __init__(self, name, sound, type): + def __init__(self, name, id, soundfile, thumb): self.name = name - self._type = type - - def _tmpname(): - l = sorted(glob(os.path.join(theme.SESSION_PATH, 'sound*'))) - return os.path.join(theme.SESSION_PATH, 'sound.%03d' % (len(l)+1)) - - if type == theme.RESTORED: - self._thumb = theme.pixbuf(theme.SOUND_CUSTOM, theme.THUMB_SIZE) - self._soundfile = _tmpname() - file(self._soundfile, 'w').write(sound) - elif type == theme.CUSTOM: - self._thumb = theme.pixbuf(theme.SOUND_MUTE, theme.THUMB_SIZE) - self._soundfile = '' - elif type == theme.JOURNAL: - self._thumb = theme.pixbuf(theme.SOUND_CUSTOM, theme.THUMB_SIZE) - self._soundfile = _tmpname() - os.rename(sound, self._soundfile) - else: - self._thumb = theme.pixbuf(theme.SOUND_SPEAKER, theme.THUMB_SIZE) - self._soundfile = sound + self.id = id + self._soundfile = soundfile + self._thumb = theme.pixbuf(thumb, theme.THUMB_SIZE) def custom(self): - return self._type != theme.PREINSTALLED + return True def read(self): - if not self._soundfile: - return '' - else: - return file(self._soundfile, 'r').read() - - def filename(self): - return self._soundfile + return file(self._soundfile, 'r').read() def thumb(self): return self._thumb def select(self): - out = self + Sound.current = self + if Sound.playing: + Sound.player.set_state(gst.STATE_NULL) + Sound.player.set_property('uri', + 'file://' + theme.path(self._soundfile)) + Sound.player.set_state(gst.STATE_PLAYING) + return self - if self._type == theme.CUSTOM: - out = theme.choose( - lambda title, file: Sound(title, file, theme.JOURNAL)) - if not out: - return None +class PreinstalledSound(Sound): + def __init__(self, name, filename): + Sound.__init__(self, name, filename, filename, theme.SOUND_SPEAKER) - Sound.current = self - if not Sound.playing: return out - Sound.player.set_state(gst.STATE_NULL) - if len(out._soundfile) == 0: return out + def custom(self): + return False + +class MuteSound(Sound): + def __init__(self, name): + Sound.__init__(self, name, None, None, theme.SOUND_MUTE) + + def custom(self): + return False + + def read(self): + return '' - Sound.player.set_property('uri', 'file://' + theme.path(out._soundfile)) + def select(self): Sound.player.set_state(gst.STATE_NULL) - Sound.player.set_state(gst.STATE_PLAYING) + return self + +class CustomSound(Sound): + def __init__(self, name): + Sound.__init__(self, name, None, None, theme.SOUND_CUSTOM) - return out + def select(self): + sound = theme.choose(lambda jobject: JournalSound(jobject)) + if sound: + sound.select() + return sound + +class RestoredSound(Sound): + def __init__(self, name, id, data): + soundfile = os.path.join(theme.SESSION_PATH, id) + Sound.__init__(self, name, id, soundfile, theme.SOUND_CUSTOM) + file(soundfile, 'w').write(data) + +class JournalSound(Sound): + def __init__(self, jobject): + soundfile = os.path.join(theme.SESSION_PATH, jobject.object_id) + Sound.__init__(self, jobject.props.metadata['title'], + jobject.object_id, soundfile, theme.SOUND_CUSTOM) + os.rename(jobject.file_path, soundfile) THEMES = [ - Sound(_('Gobble'), 'sounds/gobble.wav', theme.PREINSTALLED), - Sound(_('Funk'), 'sounds/funk.wav', theme.PREINSTALLED), - Sound(_('Giggle'), 'sounds/giggle.wav', theme.PREINSTALLED), - Sound(_('Jungle'), 'sounds/jungle.wav', theme.PREINSTALLED), - Sound(_('Mute'), '', theme.PREINSTALLED), + PreinstalledSound(_('Gobble'), 'sounds/gobble.wav'), + PreinstalledSound(_('Funk'), 'sounds/funk.wav'), + PreinstalledSound(_('Giggle'), 'sounds/giggle.wav'), + PreinstalledSound(_('Jungle'), 'sounds/jungle.wav'), + MuteSound(_('Mute')), None, - Sound(_('Custom'), None, theme.CUSTOM) ] + CustomSound(_('Custom')) ] Sound.current = THEMES[0] diff --git a/theme.py b/theme.py index fe440ff..a43e58b 100644 --- a/theme.py +++ b/theme.py @@ -132,6 +132,22 @@ def choose(out_fun): return None +def pixbuf2str(pixbuf): + def push(data, buffer): + buffer.write(data) + + import cStringIO + buffer = cStringIO.StringIO() + pixbuf.save_to_callback(push, 'png', user_data=buffer) + return buffer.getvalue() + +def str2pixbuf(data): + tmpfile = os.path.join(SESSION_PATH, '.tmp.png') + file(tmpfile, 'w').write(data) + out = theme.pixbuf(tmpfile) + os.unlink(tmpfile) + return out + # customize theme gtkrc = os.path.join(get_bundle_path(), 'gtkrc') gtk.rc_add_default_file(gtkrc) diff --git a/utils.py b/utils.py index 586bc2f..bd219f0 100644 --- a/utils.py +++ b/utils.py @@ -15,7 +15,6 @@ import os import gtk import pango -import cStringIO import sugar from sugar.graphics import style @@ -23,25 +22,6 @@ from sugar.graphics.icon import Icon from theme import * -def pixbuf2str(pixbuf): - def push(data, buffer): - buffer.write(data) - - buffer = cStringIO.StringIO() - pixbuf.save_to_callback(push, 'png', user_data=buffer) - return buffer.getvalue() - - - - - def read_pixbuf(self, arcfile): - tmpfile = os.path.join(SESSION_PATH, 'tmp.png') - file(tmpfile, 'w').write(self.read(arcfile)) - out = gtk.gdk.pixbuf_new_from_file(tmpfile) - os.unlink(tmpfile) - return out - - class ComboBox(sugar.graphics.combobox.ComboBox): def __init__(self): sugar.graphics.combobox.ComboBox.__init__(self) -- cgit v0.9.1