From 6bf19eae5239760be2c11fc39b56e11cd40c3efc Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Mon, 02 Feb 2009 16:01:31 +0000 Subject: Add a bit of OOP to ground/sound --- diff --git a/document.py b/document.py index e7c3ba8..72e571e 100644 --- a/document.py +++ b/document.py @@ -17,17 +17,14 @@ import gtk from xml.etree.ElementTree import Element, SubElement, tostring, fromstring import theme +from sound import Sound +from ground import Ground from utils import * class Document: tape = [] - - ground_name = None - ground_orig = None - ground_filename = None - - sound_name = None - sound_filename = None + ground = None + sound = None class Tape: def __init__(self): @@ -44,26 +41,19 @@ def save(filepath): zip = Zip(filepath, 'w') manifest = Element('memorize') - ground = SubElement(manifest, 'ground') - if Document.ground_filename: - ground.attrib['preinstalled'] = '1' - ground.attrib['filename'] = Document.ground_filename - else: - ground.attrib['preinstalled'] = '0' - ground.attrib['filename'] = 'ground.png' - zip.write_pixbuf('ground.png', Document.ground_orig) - ground.text = Document.ground_name - - sound = SubElement(manifest, 'sound') - if not os.path.isabs(Document.sound_filename): - sound.attrib['preinstalled'] = '1' - sound.attrib['filename'] = Document.sound_filename - else: - sound.attrib['preinstalled'] = '0' - sound.attrib['filename'] = 'sound' - zip.write(Document.sound_filename, 'sound') - sound.text = Document.sound_name - + def _save(node, arcname, value): + if value.custom(): + node.attrib['custom'] = '1' + node.attrib['filename'] = arcname + zip.writestr(arcname, value.read()) + else: + node.attrib['custom'] = '0' + node.attrib['filename'] = value.filename() + node.text = value.name + + _save(SubElement(manifest, 'ground'), 'ground.png', Document.ground) + _save(SubElement(manifest, 'sound'), 'sound', Document.sound) + saved = {} tape = SubElement(manifest, 'tape') for i in range(theme.TAPE_COUNT): @@ -82,27 +72,23 @@ def save(filepath): zip.writestr('MANIFEST.xml', tostring(manifest, encoding='utf-8')) zip.close() + import shutil + shutil.copy(filepath, '/tmp/foo.zip') + def load(filepath): zip = Zip(filepath, 'r') manifest = fromstring(zip.read('MANIFEST.xml')) - ground = manifest.find('ground') - if int(ground.attrib['preinstalled']): - Document.ground_orig = theme.pixbuf(ground.attrib['filename']) - Document.ground_filename = ground.attrib['filename'] - else: - Document.ground_orig = zip.read_pixbuf(ground.attrib['filename']) - Document.ground_name = ground.text - - sound = manifest.find('sound') - if int(sound.attrib['preinstalled']): - Document.sound_filename = sound.attrib['filename'] - else: - arcfile = sound.attrib['filename'] - sndfile = os.path.join(theme.SESSION_PATH, 'sound.001') - file(sndfile, 'w').write(zip.read(arcfile)) - Document.sound_filename = sndfile - Document.sound_name = sound.text + def _load(node, klass): + if int(node.attrib['custom']): + out = klass(node.text, zip.read(node.attrib['filename']), + theme.RESTORED) + else: + out = klass(node.text, node.attrib['filename'], theme.PREINSTALLED) + return out + + Document.ground = _load(manifest.find('ground'), Ground) + Document.sound = _load(manifest.find('sound'), Sound) loaded = {} for i, frame in enumerate(manifest.findall('tape/frame')): diff --git a/ground.py b/ground.py index 95e1b4b..99611d2 100644 --- a/ground.py +++ b/ground.py @@ -12,68 +12,82 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +import os import gtk +import cStringIO from gettext import gettext as _ from sugar.graphics.objectchooser import ObjectChooser import theme -from document import Document - -PREINSTALLED = 0 -CUSTOM = 1 -TEMPORARY = 2 def load(): - if not Document.ground_filename: - custom = Ground(Document.ground_name, None, TEMPORARY) - custom._pixbuf = Document.ground_orig - THEMES.insert(-1, custom) + from document import Document + + if Document.ground and Document.ground.custom(): + THEMES.insert(-1, Document.ground) class Ground: - def __init__(self, name, file, type): + def __init__(self, name, image, type): self.name = name - self._file = file - if file: self._pixbuf = theme.pixbuf(file) self._type = type self._thumb = None - def filename(self): - if self._type == PREINSTALLED: - return self._file + if type == theme.RESTORED: + tmpfile = os.path.join(theme.SESSION_PATH, '.tmp.png') + file(tmpfile, 'w').write(image) + self._orig = gtk.gdk.pixbuf_new_from_file(tmpfile) + os.unlink(tmpfile) + self._filename = 'ground.png' else: - return None + self._filename = image + self._orig = theme.pixbuf(image) + + def custom(self): + return self._type != theme.PREINSTALLED + + def read(self): + def push(data, buffer): + buffer.write(data) + + buffer = cStringIO.StringIO() + self._orig.save_to_callback(push, 'png', user_data=buffer) + return buffer.getvalue() + + def filename(self): + return self._filename def thumb(self): if not self._thumb: - self._thumb = theme.scale(self._pixbuf) + self._thumb = theme.scale(self._orig) return self._thumb def orig(self): - return self._pixbuf + return self._orig def change(self): - if self._type != CUSTOM: + if self._type != theme.CUSTOM: return self - return theme.choose(lambda title, file: Ground(title, file, TEMPORARY)) + return theme.choose( + lambda title, file: Ground(title, file, theme.JOURNAL)) THEMES = [ - Ground(_('Saturn'), 'images/backpics/bigbg01.gif', PREINSTALLED), - Ground(_('Snowflakes'), 'images/backpics/bigbg02.gif', PREINSTALLED), - Ground(_('Eye'), 'images/backpics/bigbg03.gif', PREINSTALLED), - Ground(_('Blobs'), 'images/backpics/bigbg04.gif', PREINSTALLED), - Ground(_('Star Night'), 'images/backpics/bigbg05.gif', PREINSTALLED), - Ground(_('Forest'), 'images/backpics/bigbg06.gif', PREINSTALLED), - Ground(_('Spiral'), 'images/backpics/bigbg07.gif', PREINSTALLED), - Ground(_('Beam'), 'images/backpics/bigbg08.gif', PREINSTALLED), - Ground(_('Cloth'), 'images/backpics/bigbg09.gif', PREINSTALLED), - Ground(_('Faces'), 'images/backpics/bigbg10.gif', PREINSTALLED), - Ground(_('Leaves'), 'images/backpics/bigbg11.gif', PREINSTALLED), - Ground(_('Vegetables'), 'images/backpics/bigbg12.gif', PREINSTALLED), - Ground(_('Spotlight'), 'images/backpics/bigbg13.gif', PREINSTALLED), - Ground(_('Strips'), 'images/backpics/bigbg14.gif', PREINSTALLED), - Ground(_('Scene'), 'images/backpics/bigbg15.gif', PREINSTALLED), - Ground(_('Rhombs'), 'images/backpics/bigbg16.gif', PREINSTALLED), - Ground(_('Milky Way'), 'images/backpics/bigbg17.gif', PREINSTALLED), + 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), None, - Ground(_('Custom'), 'images/backpics/custom.png', CUSTOM)] + Ground(_('Custom'), 'images/backpics/custom.png', theme.CUSTOM)] diff --git a/montage.py b/montage.py index 8c41ab5..e97b313 100644 --- a/montage.py +++ b/montage.py @@ -269,7 +269,7 @@ class View(gtk.EventBox): self.show_all() def restore(self): - def new_combo(themes, cb, selname = None, closure = None): + def new_combo(themes, cb, object = None, closure = None): combo = ComboBox() sel = 0 @@ -278,7 +278,7 @@ class View(gtk.EventBox): combo.append_item(o, text = o.name, size = (theme.THUMB_SIZE, theme.THUMB_SIZE), pixbuf = o.thumb()) - if o.name == selname: + if object and o.name == object.name: sel = i else: combo.append_separator() @@ -292,9 +292,9 @@ class View(gtk.EventBox): self.controlbox.pack_start(new_combo(char.THEMES, self._char_cb), False, False) self.controlbox.pack_start(new_combo(ground.THEMES, self._combo_cb, - Document.ground_name, self._ground_cb), False, False) + Document.ground, self._ground_cb), False, False) self.controlbox.pack_start(new_combo(sound.THEMES, self._combo_cb, - Document.sound_name, self._sound_cb), False, False) + Document.sound, self._sound_cb), False, False) for i in range(theme.TAPE_COUNT): View.tape[i].child.set_from_pixbuf(theme.scale(Document.tape[i].orig)) @@ -364,13 +364,10 @@ class View(gtk.EventBox): def _ground_cb(self, choice): self.screen.bgpixbuf = choice.orig() self.screen.draw() - Document.ground_name = choice.name - Document.ground_orig = choice.orig() - Document.ground_filename = choice.filename() + Document.ground = choice def _sound_cb(self, choice): - Document.sound_name = choice.name - Document.sound_filename = choice.filename() + Document.sound = choice def _screen_size_cb(self, widget, aloc): size = min(aloc.width, aloc.height) diff --git a/sound.py b/sound.py index 22be221..25b254e 100644 --- a/sound.py +++ b/sound.py @@ -19,38 +19,51 @@ from glob import glob from gettext import gettext as _ import theme -from document import Document from utils import * from sugar.activity.activity import get_bundle_path -PREISTALLED = 0 -CUSTOM = 1 -TEMPORARY = 2 -JOURNAL = 3 - def load(): - if os.path.isabs(Document.sound_filename): - custom = Sound(Document.sound_name, 'images/sounds/speaker.png', - Document.sound_filename, TEMPORARY) - THEMES.insert(-1, custom) + from document import Document + + if Document.sound and Document.sound.custom(): + THEMES.insert(-1, Document.sound) class Sound: playing = False current = None player = None - def __init__(self, name, imgfile, soundfile, type): + def __init__(self, name, sound, type): self.name = name - self._thumb = theme.pixbuf(imgfile, THUMB_SIZE) self._type = type - if type == JOURNAL: + def _tmpname(): l = sorted(glob(os.path.join(theme.SESSION_PATH, 'sound*'))) - self._soundfile = os.path.join(theme.SESSION_PATH, - 'sound.%03d' % (len(l)+1)) - os.rename(soundfile, self._soundfile) + 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 + + def custom(self): + return self._type != theme.PREINSTALLED + + def read(self): + if not self._soundfile: + return '' else: - self._soundfile = soundfile + return file(self._soundfile, 'r').read() def filename(self): return self._soundfile @@ -61,10 +74,9 @@ class Sound: def change(self): out = self - if self._type == CUSTOM: + if self._type == theme.CUSTOM: out = theme.choose( - lambda title, file: Sound(title, - 'images/sounds/speaker.png', file, JOURNAL)) + lambda title, file: Sound(title, file, theme.JOURNAL)) if not out: return None @@ -80,19 +92,13 @@ class Sound: return out THEMES = [ - Sound(_('Gobble'), 'images/sounds/speaker.png', 'sounds/gobble.wav', - PREISTALLED), - Sound(_('Funk'), 'images/sounds/speaker.png', 'sounds/funk.wav', - PREISTALLED), - Sound(_('Giggle'), 'images/sounds/speaker.png', 'sounds/giggle.wav', - PREISTALLED), - Sound(_('Jungle'), 'images/sounds/speaker.png', 'sounds/jungle.wav', - PREISTALLED), - Sound(_('Mute'), 'images/sounds/mute.png', '', - PREISTALLED), + 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), None, - Sound(_('Custom'), 'images/sounds/custom.png', None, - CUSTOM)] + Sound(_('Custom'), None, theme.CUSTOM) ] Sound.current = THEMES[0] diff --git a/theme.py b/theme.py index 05ebdc4..74bd3ef 100644 --- a/theme.py +++ b/theme.py @@ -19,10 +19,17 @@ import shutil from sugar.activity.activity import get_bundle_path, get_activity_root from sugar.graphics import style -TRANSIMG = '50x50blank-trans.png' +PREINSTALLED = 0 +CUSTOM = 1 +JOURNAL = 2 +RESTORED = 3 + +SOUND_SPEAKER = 'images/sounds/speaker.png' +SOUND_MUTE = 'images/sounds/custom.png' +SOUND_CUSTOM = 'images/sounds/custom.png' LOGO_WIDTH = 275 -TAPE_COUNT = 13 +TAPE_COUNT = 11 FRAME_COUNT = 14 DESKTOP_WIDTH = gtk.gdk.screen_width() -- cgit v0.9.1