From 29e0c1c272862919ccea7a70464e8de0756bed97 Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Thu, 17 Jul 2014 11:25:49 +0000 Subject: SvgCard use image_paths properties instead of pixbufs Now we set a image_path, and the card create the pixbuf as needed, setting the right size, and storing the image file name, solving errors saving the files when the game is edited. --- diff --git a/activity.py b/activity.py index 040b888..5362015 100644 --- a/activity.py +++ b/activity.py @@ -270,7 +270,6 @@ class MemorizeActivity(Activity): temp_snd_folder = self.game.model.data['pathsnd'] self.game.model.create_temp_directories() game_zip = zipfile.ZipFile(file_path, 'w') - equal_pairs = self.game.model.data['equal_pairs'] == '1' save_image_and_sound = True if 'origin' in self.game.model.data: if self.game.model.data['origin'] == 'art4apps': @@ -282,23 +281,18 @@ class MemorizeActivity(Activity): for pair in self.game.model.pairs: # aimg aimg = self.game.model.pairs[pair].get_property('aimg') + logging.error('saving image a %s', aimg) if aimg is not None: - if equal_pairs: - aimgfile = 'img' + str(pair) + '.jpg' - else: - aimgfile = 'aimg' + str(pair) + '.jpg' - game_zip.write(os.path.join(temp_img_folder, aimgfile), - os.path.join('images', aimgfile)) + game_zip.write(os.path.join(temp_img_folder, aimg), + os.path.join('images', aimg)) # bimg bimg = self.game.model.pairs[pair].get_property('bimg') + logging.error('saving image b %s', bimg) if bimg is not None: - if equal_pairs: - bimgfile = 'img' + str(pair) + '.jpg' - else: - bimgfile = 'bimg' + str(pair) + '.jpg' - game_zip.write(os.path.join(temp_img_folder, bimgfile), - os.path.join('images', bimgfile)) + game_zip.write(os.path.join(temp_img_folder, bimg), + os.path.join('images', bimg)) + # asnd asnd = self.game.model.pairs[pair].get_property('asnd') if asnd is not None: diff --git a/cardlist.py b/cardlist.py index 6d50722..96ba5c7 100644 --- a/cardlist.py +++ b/cardlist.py @@ -15,11 +15,11 @@ # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # import os +import shutil from gi.repository import Gtk from gi.repository import Gdk from gi.repository import GObject -from gi.repository import GdkPixbuf import svgcard import logging @@ -82,18 +82,16 @@ class CardList(Gtk.EventBox): self.clean_list(load=True) for key in game_pairs: if game_pairs[key].props.aimg is not None: - aimg = GdkPixbuf.Pixbuf.new_from_file( - join(self.model.data['pathimg'], - game_pairs[key].props.aimg)) + aimg_path = join(self.model.data['pathimg'], + game_pairs[key].props.aimg) else: - aimg = None + aimg_path = None if game_pairs[key].props.bimg is not None: - bimg = GdkPixbuf.Pixbuf.new_from_file( - join(self.model.data['pathimg'], - game_pairs[key].props.bimg)) + bimg_path = join(self.model.data['pathimg'], + game_pairs[key].props.bimg) else: - bimg = None + bimg_path = None if game_pairs[key].props.asnd is not None: asnd = join(self.model.data['pathsnd'], @@ -109,11 +107,9 @@ class CardList(Gtk.EventBox): self.add_pair( None, game_pairs[key].props.achar, - game_pairs[key].props.bchar, aimg, bimg, asnd, bsnd, + game_pairs[key].props.bchar, aimg_path, bimg_path, asnd, bsnd, game_pairs[key].props.aspeak, game_pairs[key].props.bspeak, - font_name1, font_name2, - game_pairs[key].props.aimg, game_pairs[key].props.bimg, - False, load=True) + font_name1, font_name2, False, load=True) self.get_window().thaw_updates() self.emit('update-create-toolbar', self.model.data['name'], self.model.data['equal_pairs'], @@ -122,7 +118,6 @@ class CardList(Gtk.EventBox): 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 = game_model.data['pathimg'] @@ -147,27 +142,29 @@ class CardList(Gtk.EventBox): pair_card.set_property('bspeak', bspeak) # aimg - aimg = self.pairs[pair].get_pixbuf(1) + aimg = self.pairs[pair].get_image_path(1) if aimg is not None: - aimgfile = self.pairs[pair].get_image_name(1) - if not os.path.exists(join(temp_img_folder, aimgfile)): - if equal_pairs: - aimgfile = 'img' + str(pair) + '.jpg' - else: - aimgfile = 'aimg' + str(pair) + '.jpg' - aimg.savev(join(temp_img_folder, aimgfile), 'jpeg', [], []) - pair_card.set_property('aimg', aimgfile) + aimgfile = self.pairs[pair].get_image_path(1) + pair_card.set_property('aimg', basename(aimgfile)) + if not os.path.exists(aimgfile): + destination_path = join(temp_img_folder, + basename(aimgfile)) + if not os.path.exists(destination_path): + GObject.idle_add(shutil.copyfile, aimgfile, + destination_path) + logging.error('copy img to %s', destination_path) # bimg - bimg = self.pairs[pair].get_pixbuf(2) + bimg = self.pairs[pair].get_image_path(2) if bimg is not None: - bimgfile = self.pairs[pair].get_image_name(2) - if not os.path.exists(join(temp_img_folder, bimgfile)): - if equal_pairs: - bimgfile = 'img' + str(pair) + '.jpg' - else: - bimgfile = 'bimg' + str(pair) + '.jpg' - bimg.savev(join(temp_img_folder, bimgfile), 'jpeg', [], []) - pair_card.set_property('bimg', bimgfile) + bimgfile = self.pairs[pair].get_image_path(2) + pair_card.set_property('bimg', basename(bimgfile)) + if not os.path.exists(bimgfile): + destination_path = join(temp_img_folder, + basename(bimgfile)) + if not os.path.exists(destination_path): + GObject.idle_add(shutil.copyfile, bimgfile, + destination_path) + logging.error('copy img to %s', destination_path) # asnd asnd = self.pairs[pair].get_sound(1) @@ -193,11 +190,11 @@ class CardList(Gtk.EventBox): self.pair_list_modified = True self.model.mark_modified() - def add_pair(self, widget, achar, bchar, aimg, bimg, asnd, bsnd, + def add_pair(self, widget, achar, bchar, aimg_path, bimg_path, asnd, bsnd, aspeak, bspeak, font_name1, font_name2, - aimg_name=None, bimage_name=None, show=True, load=False): - pair = CardPair(achar, bchar, aimg, bimg, asnd, bsnd, aspeak, bspeak, - font_name1, font_name2, aimg_name, bimage_name) + show=True, load=False): + pair = CardPair(achar, bchar, aimg_path, bimg_path, asnd, bsnd, + aspeak, bspeak, font_name1, font_name2) self.hbox.pack_end(pair, False, True, 0) self.pairs.append(pair) pair.connect('pair-selected', self.set_selected) @@ -235,17 +232,17 @@ class CardList(Gtk.EventBox): 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_image_path(1), + self.current_pair.get_image_path(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, + def update_selected(self, widget, newtext1, newtext2, aimg_path, bimg_path, asnd, bsnd, aspeak, bspeak): self.current_pair.change_text(newtext1, newtext2) - self.current_pair.change_pixbuf(aimg, bimg) + self.current_pair.change_image_path(aimg_path, bimg_path) self.current_pair.change_sound(asnd, bsnd) self.current_pair.change_speak(aspeak, bspeak) self.model.mark_modified() @@ -259,10 +256,9 @@ class CardPair(Gtk.EventBox): None, [GObject.TYPE_PYOBJECT]), } - def __init__(self, text1, text2=None, aimg=None, bimg=None, + def __init__(self, text1, text2=None, aimg_path=None, bimg_path=None, asnd=None, bsnd=None, aspeak=None, bspeak=None, - font_name1=None, font_name2=None, - aimg_name=None, bimg_name=None): + font_name1=None, font_name2=None): Gtk.EventBox.__init__(self) self.bg_color = '#d7d7d7' self._stroke_color = '#ffffff' @@ -271,9 +267,6 @@ class CardPair(Gtk.EventBox): self.asnd = asnd self.bsnd = bsnd - self.aimg_name = aimg_name - self.bimg_name = bimg_name - self.current_game_key = None row = Gtk.VBox() @@ -286,9 +279,8 @@ class CardPair(Gtk.EventBox): 'text_color': style.Color('#ffffff')}, 'front': {'fill_color': style.Color(self._fill_color), 'stroke_color': style.Color(self._stroke_color)}}, - None, PAIR_SIZE, self.bg_color, font_name1) + aimg_path, PAIR_SIZE, self.bg_color, font_name1) self.bcard1.flip() - self.bcard1.set_pixbuf(aimg) self.bcard1.set_valign(Gtk.Align.CENTER) row.pack_start(self.bcard1, True, True, 0) @@ -298,9 +290,8 @@ class CardPair(Gtk.EventBox): 'text_color': style.Color('#ffffff')}, 'front': {'fill_color': style.Color(self._fill_color), 'stroke_color': style.Color(self._stroke_color)}}, - None, PAIR_SIZE, self.bg_color, font_name2) + bimg_path, PAIR_SIZE, self.bg_color, font_name2) self.bcard2.flip() - self.bcard2.set_pixbuf(bimg) self.bcard2.set_valign(Gtk.Align.CENTER) row.pack_start(self.bcard2, True, True, 0) self.connect('button-press-event', self.emit_selected) @@ -322,9 +313,9 @@ class CardPair(Gtk.EventBox): self.bcard1.set_border(stroke_color, fill_color) self.bcard2.set_border(stroke_color, fill_color) - def change_pixbuf(self, aimg, bimg): - self.bcard1.set_pixbuf(aimg) - self.bcard2.set_pixbuf(bimg) + def change_image_path(self, aimage_path, bimage_path): + self.bcard1.set_image_path(aimage_path) + self.bcard2.set_image_path(bimage_path) def change_text(self, text1, text2): self.bcard1.change_text(text1) @@ -356,20 +347,11 @@ class CardPair(Gtk.EventBox): else: return self.bcard2.get_speak() - def get_pixbuf(self, card): - if card == 1: - return self.bcard1.get_pixbuf() - else: - return self.bcard2.get_pixbuf() - - def get_image_name(self, card): + def get_image_path(self, card): if card == 1: - name = self.aimg_name + return self.bcard1.get_image_path() else: - name = self.bimg_name - if name is None: - name = 'img%d' % card - return name + return self.bcard2.get_image_path() def get_sound(self, card): if card == 1: diff --git a/cardtable.py b/cardtable.py index ccd6baf..d5a4b85 100644 --- a/cardtable.py +++ b/cardtable.py @@ -135,9 +135,9 @@ class CardTable(Gtk.EventBox): for card in self.cards_data: if card.get('img', None): - jpg = os.path.join(self.data['pathimg'], card['img']) + image_path = os.path.join(self.data['pathimg'], card['img']) else: - jpg = None + image_path = None props = {} props['front_text'] = {'card_text': card.get('char', ''), 'speak': card.get('speak')} @@ -154,7 +154,7 @@ class CardTable(Gtk.EventBox): 'stroke_color': background_color2} card = svgcard.SvgCard( - identifier, props, jpg, + identifier, props, image_path, self.card_size, self._background_color, font_name) card.connect('enter-notify-event', self.mouse_event, [x, y]) card.set_events(Gdk.EventMask.TOUCH_MASK | diff --git a/createcardpanel.py b/createcardpanel.py index fd21e6b..35a0ead 100644 --- a/createcardpanel.py +++ b/createcardpanel.py @@ -20,7 +20,6 @@ from gi.repository import Gtk from gi.repository import Gdk from gi.repository import GObject -from gi.repository import GdkPixbuf from os.path import join, basename @@ -29,6 +28,7 @@ from gettext import gettext as _ import svgcard import logging +from sugar3.activity import activity from sugar3.graphics import style from sugar3.graphics.toolbutton import ToolButton from sugar3.graphics.icon import Icon @@ -154,8 +154,8 @@ class CreateCardPanel(Gtk.EventBox): if self.equal_pairs: self.emit('add-pair', self.cardeditor1.get_text(), self.cardeditor1.get_text(), - self.cardeditor1.get_pixbuf(), - self.cardeditor1.get_pixbuf(), + self.cardeditor1.get_image_path(), + self.cardeditor1.get_image_path(), self.cardeditor1.get_snd(), self.cardeditor1.get_snd(), self.cardeditor1.get_speak(), @@ -165,8 +165,8 @@ class CreateCardPanel(Gtk.EventBox): else: self.emit('add-pair', self.cardeditor1.get_text(), self.cardeditor2.get_text(), - self.cardeditor1.get_pixbuf(), - self.cardeditor2.get_pixbuf(), + self.cardeditor1.get_image_path(), + self.cardeditor2.get_image_path(), self.cardeditor1.get_snd(), self.cardeditor2.get_snd(), self.cardeditor1.get_speak(), @@ -180,8 +180,8 @@ class CreateCardPanel(Gtk.EventBox): if self.equal_pairs: self.emit('update-pair', self.cardeditor1.get_text(), self.cardeditor1.get_text(), - self.cardeditor1.get_pixbuf(), - self.cardeditor1.get_pixbuf(), + self.cardeditor1.get_image_path(), + self.cardeditor1.get_image_path(), self.cardeditor1.get_snd(), self.cardeditor1.get_snd(), self.cardeditor1.get_speak(), @@ -189,21 +189,21 @@ class CreateCardPanel(Gtk.EventBox): else: self.emit('update-pair', self.cardeditor1.get_text(), self.cardeditor2.get_text(), - self.cardeditor1.get_pixbuf(), - self.cardeditor2.get_pixbuf(), + self.cardeditor1.get_image_path(), + self.cardeditor2.get_image_path(), self.cardeditor1.get_snd(), self.cardeditor2.get_snd(), self.cardeditor1.get_speak(), self.cardeditor2.get_speak()) self.clean(None) - def pair_selected(self, widget, selected, newtext1, newtext2, aimg, bimg, - asnd, bsnd, aspeak, bspeak): + def pair_selected(self, widget, selected, newtext1, newtext2, + aimage_path, bimage_path, asnd, bsnd, aspeak, bspeak): if selected: self.cardeditor1.set_text(newtext1) self.cardeditor2.set_text(newtext2) - self.cardeditor1.set_pixbuf(aimg) - self.cardeditor2.set_pixbuf(bimg) + self.cardeditor1.set_image_path(aimage_path) + self.cardeditor2.set_image_path(bimage_path) self.cardeditor1.set_snd(asnd) self.cardeditor2.set_snd(bsnd) self.cardeditor1.set_speak(aspeak) @@ -404,33 +404,22 @@ class CardEditor(Gtk.EventBox): self.usespeak.handler_unblock_by_func(self._usespeak_cb) self.usespeak.palette.voices.resume(value) - def get_pixbuf(self): - return self.card.get_pixbuf() + def get_image_path(self): + return self.card.get_image_path() - def set_pixbuf(self, pixbuf): - self.card.set_pixbuf(pixbuf) + def set_image_path(self, image_path): + self.card.set_image_path(image_path) self.emit('has-picture', True) def _load_image(self, widget): def load(jobject): index = jobject.file_path - + dst = join(self.temp_folder, 'images', basename(index)) + shutil.copy(index, dst) self.set_speak(None) - - pixbuf_t = GdkPixbuf.Pixbuf.new_from_file_at_size( - index, PAIR_SIZE - style.DEFAULT_SPACING * 2, - PAIR_SIZE - style.DEFAULT_SPACING * 2) - size = max(pixbuf_t.get_width(), pixbuf_t.get_height()) - pixbuf_z = GdkPixbuf.Pixbuf.new_from_file_at_size( - 'images/white.png', size, size) - pixbuf_t.composite(pixbuf_z, 0, 0, pixbuf_t.get_width(), - pixbuf_t.get_height(), 0, 0, 1, 1, - GdkPixbuf.InterpType.BILINEAR, 255) - self.card.set_pixbuf(pixbuf_z) - logging.debug('Picture Loaded: ' + index) + self.card.set_image_path(dst) + logging.debug('Picture Loaded: ' + dst) self.emit('has-picture', True) - del pixbuf_t - del pixbuf_z chooser.pick(parent=self.get_toplevel(), what=chooser.IMAGE, @@ -445,10 +434,9 @@ class CardEditor(Gtk.EventBox): dst = join(self.temp_folder, 'sounds', basename(index)) shutil.copy(index, dst) self.set_snd(dst) - icon_theme = Gtk.IconTheme.get_default() - pixbuf_t = icon_theme.load_icon("audio-x-generic", - style.XLARGE_ICON_SIZE, 0) - self.card.set_pixbuf(pixbuf_t) + sound_icon_path = join(activity.get_bundle_path(), + 'icons/sound.svg') + self.card.set_image_path(sound_icon_path) self.emit('has-sound', True) logging.debug('Audio Loaded: ' + dst) @@ -464,7 +452,6 @@ class CardEditor(Gtk.EventBox): return self.snd = None - self.card.set_pixbuf(None) self.emit('has-sound', False) self.emit('has-picture', False) @@ -481,7 +468,7 @@ class CardEditor(Gtk.EventBox): def clean(self): self.textentry.set_text('') - self.card.set_pixbuf(None) + self.card.set_image_path(None) self.snd = None self.emit('has-text', False) self.emit('has-picture', False) diff --git a/svgcard.py b/svgcard.py index fde06ed..ccc185d 100644 --- a/svgcard.py +++ b/svgcard.py @@ -54,15 +54,15 @@ class SvgCard(Gtk.EventBox): 'stroke_color': style.Color('#111111')} default_props['front_text'] = {'text_color': '#ffffff'} - def __init__(self, identifier, pprops, jpeg, size, + def __init__(self, identifier, pprops, image_path, size, bg_color='#000000', font_name=model.DEFAULT_FONT): Gtk.EventBox.__init__(self) - + logging.error('SvgCard image_path %s', image_path) self.bg_color = bg_color self.flipped = False - self.flipped_once = False self.id = identifier - self.jpeg = jpeg + self._image_path = image_path + self.jpeg = None self.size = size # animation data self._steps_scales = [0.66, 0.33, 0.1, 0.33, 0.66] @@ -154,6 +154,12 @@ class SvgCard(Gtk.EventBox): cache_context.restore() text_props = self.props[flipped and 'front_text' or 'back_text'] + if self._image_path is not None: + if self.jpeg is None: + image_size = self.size - style.DEFAULT_SPACING * 2 + self.jpeg = GdkPixbuf.Pixbuf.new_from_file_at_size( + self._image_path, image_size, image_size) + if self.jpeg is not None and flipped: Gdk.cairo_set_source_pixbuf( cache_context, self.jpeg, @@ -197,13 +203,20 @@ class SvgCard(Gtk.EventBox): else: self.queue_draw() - def set_pixbuf(self, pixbuf): - self.jpeg = pixbuf + def set_image_path(self, image_path): + self._image_path = image_path + if self._image_path is not None: + if self.jpeg is None: + image_size = self.size - style.DEFAULT_SPACING * 2 + self.jpeg = GdkPixbuf.Pixbuf.new_from_file_at_size( + self._image_path, image_size, image_size) + else: + self.jpeg = None self._cached_surface[True] = None self.queue_draw() - def get_pixbuf(self): - return self.jpeg + def get_image_path(self): + return self._image_path def set_highlight(self, status, mouse=False): if self.flipped and mouse: @@ -215,12 +228,11 @@ class SvgCard(Gtk.EventBox): if self.flipped: return - if not self.flipped_once: + if self.jpeg is None: if self.jpeg is not None: image_size = self.size - style.DEFAULT_SPACING * 2 self.jpeg = GdkPixbuf.Pixbuf.new_from_file_at_size( - self.jpeg, image_size, image_size) - self.flipped_once = True + self._image_path, image_size, image_size) if full_animation: if self.id != -1 and self.get_speak(): -- cgit v0.9.1