Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/cardlist.py
diff options
context:
space:
mode:
Diffstat (limited to 'cardlist.py')
-rw-r--r--cardlist.py195
1 files changed, 81 insertions, 114 deletions
diff --git a/cardlist.py b/cardlist.py
index 35fcea4..ba126b9 100644
--- a/cardlist.py
+++ b/cardlist.py
@@ -18,14 +18,11 @@
import gtk
import svgcard
import logging
+from os.path import join, basename
+import shutil
-import os
-from os import environ
-from os.path import join
+from model import Pair
-import model
-import zipfile
-import tempfile
from gobject import SIGNAL_RUN_FIRST, TYPE_PYOBJECT
from sugar.graphics import style
@@ -35,20 +32,20 @@ import theme
_logger = logging.getLogger('memorize-activity')
+
class CardList(gtk.EventBox):
-
+
__gsignals__ = {
'pair-selected': (SIGNAL_RUN_FIRST, None, 9 * [TYPE_PYOBJECT]),
'update-create-toolbar': (SIGNAL_RUN_FIRST, None, 3 * [TYPE_PYOBJECT]),
- 'update-create-buttons': (SIGNAL_RUN_FIRST, None, 2 * [TYPE_PYOBJECT]),
}
def __init__(self):
gtk.EventBox.__init__(self)
- self.model = model.Model()
self.pairs = []
self.current_pair = None
self.current_game_key = None
+ self.model = None
self.vbox = gtk.VBox(False)
@@ -56,25 +53,22 @@ class CardList(gtk.EventBox):
fill_box.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#000000'))
fill_box.show()
self.vbox.pack_end(fill_box, True, True)
-
+
scroll = gtk.ScrolledWindow()
scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
scroll.add_with_viewport(self.vbox)
scroll.set_border_width(0)
- scroll.get_child().modify_bg(gtk.STATE_NORMAL,
+ scroll.get_child().modify_bg(gtk.STATE_NORMAL,
gtk.gdk.color_parse('#000000'))
self.add(scroll)
self.show_all()
-
- def load_game(self, widget, game_name):
- self.model.read(game_name)
+
+ def load_game(self, game):
+ self.model = game.model
self.current_game_key = self.model.data['game_file']
- self.emit('update-create-toolbar', self.model.data['name'],
- self.model.data.get('equal_pairs', 'False'),
- self.model.data.get('divided', '0'))
game_pairs = self.model.pairs
game_data = self.model.data
- self.clean_list()
+ self.clean_list(load=True)
for key in game_pairs:
if game_pairs[key].props.aimg != None:
aimg = gtk.gdk.pixbuf_new_from_file( \
@@ -82,49 +76,43 @@ class CardList(gtk.EventBox):
game_pairs[key].props.aimg))
else:
aimg = None
-
+
if game_pairs[key].props.bimg != None:
bimg = gtk.gdk.pixbuf_new_from_file( \
join(self.model.data['pathimg'],
game_pairs[key].props.bimg))
else:
bimg = None
-
+
if game_pairs[key].props.asnd != None:
asnd = join(self.model.data['pathsnd'],
game_pairs[key].props.asnd)
else:
asnd = None
-
- if game_pairs[key].props.bsnd != None:
+
+ if game_pairs[key].props.bsnd != None:
bsnd = join(self.model.data['pathsnd'],
game_pairs[key].props.bsnd)
else:
bsnd = None
-
+
self.add_pair(None, game_pairs[key].props.achar,
game_pairs[key].props.bchar, aimg, bimg, asnd, bsnd,
game_pairs[key].props.aspeak, game_pairs[key].props.bspeak,
- False)
-
- def save_game(self, widget, game_name, equal_pairs, grouped):
-
- tmp_root = join(environ['SUGAR_ACTIVITY_ROOT'], 'instance')
- temp_folder = tempfile.mkdtemp(dir=tmp_root)
- os.chmod(temp_folder, 0777)
- temp_img_folder = join(temp_folder, 'images')
- temp_snd_folder = join(temp_folder, 'sounds')
-
- os.makedirs(temp_img_folder)
- os.makedirs(temp_snd_folder)
-
- game_zip = zipfile.ZipFile(join(temp_folder, 'game.zip'), 'w')
-
- game_model = model.Model(temp_folder)
- game_model.data['name'] = game_name
+ False, load=True)
+ self.emit('update-create-toolbar', self.model.data['name'],
+ self.model.data['equal_pairs'],
+ self.model.data['divided'])
+
+ def update_model(self, game_model):
+ game_model.pairs = {}
+ equal_pairs = game_model.data['equal_pairs'] == '1'
+ game_model.create_temp_directories()
+ temp_img_folder = join(game_model.temp_folder, 'images')
+ temp_snd_folder = join(game_model.temp_folder, 'sounds')
+
for pair in range(len(self.pairs)):
- pair_card = model.Pair()
-
+ pair_card = Pair()
# achar
achar = self.pairs[pair].get_text(1)
if achar != '':
@@ -146,17 +134,13 @@ class CardList(gtk.EventBox):
# aimg
aimg = self.pairs[pair].get_pixbuf(1)
if aimg != None:
-
if equal_pairs:
aimgfile = 'img' + str(pair) + '.jpg'
else:
aimgfile = 'aimg' + str(pair) + '.jpg'
- aimg.save(join(temp_img_folder, aimgfile), 'jpeg',
- {'quality':'85'})
- game_zip.write(join(temp_img_folder, aimgfile),
- join('images', aimgfile))
pair_card.set_property('aimg', aimgfile)
-
+ aimg.save(join(temp_img_folder, aimgfile), 'jpeg',
+ {'quality': '85'})
# bimg
bimg = self.pairs[pair].get_pixbuf(2)
if bimg != None:
@@ -164,70 +148,49 @@ class CardList(gtk.EventBox):
bimgfile = 'img' + str(pair) + '.jpg'
else:
bimgfile = 'bimg' + str(pair) + '.jpg'
- bimg.save(join(temp_img_folder, bimgfile), 'jpeg',
- {'quality':'85'})
- game_zip.write(join(temp_img_folder, bimgfile),
- join('images', bimgfile))
pair_card.set_property('bimg', bimgfile)
+ bimg.save(join(temp_img_folder, bimgfile), 'jpeg',
+ {'quality': '85'})
+
# asnd
asnd = self.pairs[pair].get_sound(1)
+ logging.debug('update_model asnd %s', asnd)
if asnd != None:
- if equal_pairs:
- asndfile = 'snd' + str(pair) + '.ogg'
- else:
- asndfile = 'asnd' + str(pair) + '.ogg'
- _logger.error(asndfile + ': ' + asnd)
- game_zip.write(asnd, join('sounds', asndfile))
- pair_card.set_property('asnd', asndfile)
-
+ pair_card.set_property('asnd', basename(asnd))
# bsnd
bsnd = self.pairs[pair].get_sound(2)
+ logging.debug('update_model bsnd %s', bsnd)
if bsnd != None:
- if equal_pairs:
- bsndfile = 'snd'+str(pair)+'.ogg'
- else:
- bsndfile = 'bsnd' + str(pair) + '.ogg'
- game_zip.write(bsnd, join('sounds', bsndfile))
- _logger.error(bsndfile + ': ' + bsnd)
- pair_card.set_property('bsnd', bsndfile)
-
+ pair_card.set_property('bsnd', basename(bsnd))
+
game_model.pairs[pair] = pair_card
- game_model.write(equal_pairs, grouped)
- game_zip.write(join(temp_folder, 'game.xml'), 'game.xml')
- game_zip.close()
- game_model.save_byte_array(join(temp_folder, 'game.zip'), game_name)
- def clean_list(self, button = None):
+ def clean_list(self, button=None, load=False):
if button != None:
self.current_game_key = None
map(lambda x: self.vbox.remove(x), self.pairs)
del self.pairs
self.pairs = []
-
- def clean_tmp_folder(self, path):
- for root, dirs, files in os.walk(path, topdown=False):
- for name in files:
- os.remove(join(root, name))
- for name in dirs:
- os.rmdir(join(root, name))
- os.rmdir(path)
-
+ if not load:
+ self.model.mark_modified()
+
def add_pair(self, widget, achar, bchar, aimg, bimg, asnd, bsnd,
- aspeak, bspeak, show = True):
- pair = Pair(achar, bchar, aimg, bimg, asnd, bsnd, aspeak, bspeak)
+ aspeak, bspeak, show=True, load=False):
+ pair = CardPair(achar, bchar, aimg, bimg, asnd, bsnd, aspeak, bspeak)
self.vbox.pack_end(pair, False, True)
self.pairs.append(pair)
pair.connect('pair-selected', self.set_selected)
pair.connect('pair-closed', self.rem_pair)
- self.emit('update-create-buttons', True, True)
+ if not load:
+ self.model.mark_modified()
if show:
self.show_all()
-
+
def rem_pair(self, widget, event):
- self.vbox.remove(widget)
+ self.vbox.remove(widget)
self.pairs.remove(widget)
del widget
- self.emit('update-create-buttons', True, True)
+ self.model.mark_modified()
self.emit('pair-selected', False, None, None, None, None, None, None,
False, False)
@@ -235,13 +198,17 @@ class CardList(gtk.EventBox):
if self.current_pair is not None:
current_pair = self.current_pair
current_pair.set_selected(False)
- self.current_pair = widget
+ self.current_pair = widget
widget.set_selected(True)
self.emit('pair-selected', True,
- self.current_pair.get_text(1), self.current_pair.get_text(2),
- self.current_pair.get_pixbuf(1), self.current_pair.get_pixbuf(2),
- self.current_pair.get_sound(1), self.current_pair.get_sound(2),
- self.current_pair.get_speak(1), self.current_pair.get_speak(2))
+ self.current_pair.get_text(1),
+ self.current_pair.get_text(2),
+ self.current_pair.get_pixbuf(1),
+ self.current_pair.get_pixbuf(2),
+ self.current_pair.get_sound(1),
+ self.current_pair.get_sound(2),
+ self.current_pair.get_speak(1),
+ self.current_pair.get_speak(2))
def update_selected(self, widget, newtext1, newtext2, aimg, bimg,
asnd, bsnd, aspeak, bspeak):
@@ -249,18 +216,18 @@ class CardList(gtk.EventBox):
self.current_pair.change_pixbuf(aimg, bimg)
self.current_pair.change_sound(asnd, bsnd)
self.current_pair.change_speak(aspeak, bspeak)
-
- self.emit('update-create-buttons', True, True)
-
-class Pair(gtk.EventBox):
+ self.model.mark_modified()
+
+
+class CardPair(gtk.EventBox):
__gsignals__ = {
'pair-selected': (SIGNAL_RUN_FIRST, None, [TYPE_PYOBJECT]),
'pair-closed': (SIGNAL_RUN_FIRST, None, [TYPE_PYOBJECT]),
}
- def __init__(self, text1, text2 = None, aimg = None, bimg = None,
- asnd = None, bsnd = None, aspeak=None, bspeak=None):
+ def __init__(self, text1, text2=None, aimg=None, bimg=None,
+ asnd=None, bsnd=None, aspeak=None, bspeak=None):
gtk.EventBox.__init__(self)
self.bg_color = '#000000'
@@ -274,12 +241,12 @@ class Pair(gtk.EventBox):
row.props.spacing = 10
self.bcard1 = svgcard.SvgCard(-1,
- { 'front_text' : { 'card_text' : text1,
- 'speak' : aspeak,
- 'text_color' : '#ffffff' },
- 'front' : { 'fill_color' : '#4c4d4f',
- 'stroke_color' : '#ffffff',
- 'opacity' : '1' } },
+ {'front_text': {'card_text': text1,
+ 'speak': aspeak,
+ 'text_color': '#ffffff'},
+ 'front': {'fill_color': '#4c4d4f',
+ 'stroke_color': '#ffffff',
+ 'opacity': '1'}},
None, theme.PAIR_SIZE, 1, self.bg_color)
self.bcard1.flip()
self.bcard1.set_pixbuf(aimg)
@@ -288,12 +255,12 @@ class Pair(gtk.EventBox):
row.pack_start(align)
self.bcard2 = svgcard.SvgCard(-1,
- { 'front_text' : { 'card_text' : text2,
- 'speak' : bspeak,
- 'text_color' : '#ffffff' },
- 'front' : { 'fill_color' : '#4c4d4f',
- 'stroke_color' : '#ffffff',
- 'opacity' : '1' } },
+ {'front_text': {'card_text': text2,
+ 'speak': bspeak,
+ 'text_color': '#ffffff'},
+ 'front': {'fill_color': '#4c4d4f',
+ 'stroke_color': '#ffffff',
+ 'opacity': '1'}},
None, theme.PAIR_SIZE, 1, self.bg_color)
self.bcard2.flip()
self.bcard2.set_pixbuf(bimg)
@@ -331,15 +298,15 @@ class Pair(gtk.EventBox):
self.bg_color = '#000000'
else:
self.bg_color = '#b2b3b7'
-
+
self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(self.bg_color))
self.bcard1.set_background(self.bg_color)
self.bcard2.set_background(self.bg_color)
-
+
def change_pixbuf(self, aimg, bimg):
self.bcard1.set_pixbuf(aimg)
self.bcard2.set_pixbuf(bimg)
-
+
def change_text(self, text1, text2):
self.bcard1.change_text(text1)
self.bcard2.change_text(text2)