Exemplo n.º 1
0
class CardPair(Gtk.EventBox):

    __gsignals__ = {
        'pair-selected': (GObject.SignalFlags.RUN_FIRST,
                          None, [GObject.TYPE_PYOBJECT]),
    }

    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):
        Gtk.EventBox.__init__(self)
        self.bg_color = '#d7d7d7'
        self._stroke_color = '#ffffff'
        self._fill_color = '#4c4d4f'

        self.asnd = asnd
        self.bsnd = bsnd

        self.current_game_key = None

        row = Gtk.VBox()
        row.props.margin_left = style.DEFAULT_SPACING
        row.props.margin_right = style.DEFAULT_SPACING

        self.bcard1 = Card(
            -1, {'front_text': {'card_text': text1,
                                'speak': aspeak,
                                'text_color': style.Color('#ffffff')},
                 'front': {'fill_color': style.Color(self._fill_color),
                           'stroke_color': style.Color(self._stroke_color)}},
            aimg_path, PAIR_SIZE, self.bg_color, font_name1)
        self.bcard1.flip()
        self.bcard1.set_valign(Gtk.Align.CENTER)
        row.pack_start(self.bcard1, True, True, 0)

        self.bcard2 = Card(
            -1, {'front_text': {'card_text': text2,
                                'speak': bspeak,
                                'text_color': style.Color('#ffffff')},
                 'front': {'fill_color': style.Color(self._fill_color),
                           'stroke_color': style.Color(self._stroke_color)}},
            bimg_path, PAIR_SIZE, self.bg_color, font_name2)
        self.bcard2.flip()
        self.bcard2.set_valign(Gtk.Align.CENTER)
        row.pack_start(self.bcard2, True, True, 0)
        self.connect('button-press-event', self.emit_selected)
        self.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse(self.bg_color))
        self.add(row)
        self.show_all()

    def emit_selected(self, widget, event):
        self.emit('pair-selected', self)

    def set_selected(self, status):
        if not status:
            stroke_color = self._stroke_color
            fill_color = self._fill_color
        else:
            stroke_color = user_color.get_stroke_color()
            fill_color = user_color.get_fill_color()

        self.bcard1.set_border(stroke_color, fill_color)
        self.bcard2.set_border(stroke_color, fill_color)

    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)
        self.bcard2.change_text(text2)

    def change_sound(self, asnd, bsnd):
        self.asnd = asnd
        self.bsnd = bsnd

    def change_font(self, card, font_name):
        if card == 1:
            self.bcard1.change_font(font_name)
        else:
            self.bcard2.change_font(font_name)

    def get_text(self, card):
        if card == 1:
            return self.bcard1.get_text()
        else:
            return self.bcard2.get_text()

    def change_speak(self, aspeak, bspeak):
        self.bcard1.change_speak(aspeak)
        self.bcard2.change_speak(bspeak)

    def get_speak(self, card):
        if card == 1:
            return self.bcard1.get_speak()
        else:
            return self.bcard2.get_speak()

    def get_image_path(self, card):
        if card == 1:
            return self.bcard1.get_image_path()
        else:
            return self.bcard2.get_image_path()

    def get_sound(self, card):
        if card == 1:
            return self.asnd
        else:
            return self.bsnd
Exemplo n.º 2
0
class CardPair(Gtk.EventBox):

    __gsignals__ = {
        'pair-selected': (GObject.SignalFlags.RUN_FIRST,
                          None, [GObject.TYPE_PYOBJECT]),
    }

    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):
        Gtk.EventBox.__init__(self)
        self.bg_color = '#d7d7d7'
        self._stroke_color = '#ffffff'
        self._fill_color = '#4c4d4f'

        self.asnd = asnd
        self.bsnd = bsnd

        self.current_game_key = None

        row = Gtk.VBox()
        row.props.margin_left = style.DEFAULT_SPACING
        row.props.margin_right = style.DEFAULT_SPACING

        self.bcard1 = Card(
            -1, {'front_text': {'card_text': text1,
                                'speak': aspeak,
                                'text_color': style.Color('#ffffff')},
                 'front': {'fill_color': style.Color(self._fill_color),
                           'stroke_color': style.Color(self._stroke_color)}},
            aimg_path, PAIR_SIZE, self.bg_color, font_name1)
        self.bcard1.flip()
        self.bcard1.set_valign(Gtk.Align.CENTER)
        row.pack_start(self.bcard1, True, True, 0)

        self.bcard2 = Card(
            -1, {'front_text': {'card_text': text2,
                                'speak': bspeak,
                                'text_color': style.Color('#ffffff')},
                 'front': {'fill_color': style.Color(self._fill_color),
                           'stroke_color': style.Color(self._stroke_color)}},
            bimg_path, PAIR_SIZE, self.bg_color, font_name2)
        self.bcard2.flip()
        self.bcard2.set_valign(Gtk.Align.CENTER)
        row.pack_start(self.bcard2, True, True, 0)
        self.connect('button-press-event', self.emit_selected)
        self.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse(self.bg_color))
        self.add(row)
        self.show_all()

    def emit_selected(self, widget, event):
        self.emit('pair-selected', self)

    def set_selected(self, status):
        if not status:
            stroke_color = self._stroke_color
            fill_color = self._fill_color
        else:
            stroke_color = user_color.get_stroke_color()
            fill_color = user_color.get_fill_color()

        self.bcard1.set_border(stroke_color, fill_color)
        self.bcard2.set_border(stroke_color, fill_color)

    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)
        self.bcard2.change_text(text2)

    def change_sound(self, asnd, bsnd):
        self.asnd = asnd
        self.bsnd = bsnd

    def change_font(self, card, font_name):
        if card == 1:
            self.bcard1.change_font(font_name)
        else:
            self.bcard2.change_font(font_name)

    def get_text(self, card):
        if card == 1:
            return self.bcard1.get_text()
        else:
            return self.bcard2.get_text()

    def change_speak(self, aspeak, bspeak):
        self.bcard1.change_speak(aspeak)
        self.bcard2.change_speak(bspeak)

    def get_speak(self, card):
        if card == 1:
            return self.bcard1.get_speak()
        else:
            return self.bcard2.get_speak()

    def get_image_path(self, card):
        if card == 1:
            return self.bcard1.get_image_path()
        else:
            return self.bcard2.get_image_path()

    def get_sound(self, card):
        if card == 1:
            return self.asnd
        else:
            return self.bsnd
Exemplo n.º 3
0
class CardEditor(Gtk.EventBox):

    __gsignals__ = {
        'has-text': (GObject.SignalFlags.RUN_FIRST, None,
                     [GObject.TYPE_PYOBJECT]),
        'has-picture': (GObject.SignalFlags.RUN_FIRST, None,
                        [GObject.TYPE_PYOBJECT]),
        'has-sound': (GObject.SignalFlags.RUN_FIRST, None,
                      [GObject.TYPE_PYOBJECT]),
        'change-font': (GObject.SignalFlags.RUN_FIRST, None,
                        [GObject.TYPE_PYOBJECT]),
    }

    def __init__(self, game, editor_index):
        Gtk.EventBox.__init__(self)
        self._game = game
        self.snd = None
        self.editor_index = editor_index
        self.temp_folder = None

        box = Gtk.Grid()
        box.set_column_spacing(style.DEFAULT_SPACING)
        box.set_row_spacing(style.DEFAULT_SPACING)
        box.props.margin = style.DEFAULT_SPACING

        self.card = Card(
            -1, {'front_text': {'card_text': '',
                                'text_color': style.Color('#ffffff')}},
            None, PAIR_SIZE, '#c0c0c0')
        self.card.flip()
        card_align = Gtk.Alignment.new(.5, .5, 0, 0)
        card_align.add(self.card)
        box.attach(card_align, 0, 0, 1, 1)

        self.textentry = Gtk.Entry()
        self.textentry.connect('changed', self.update_text)
        self.textentry.set_valign(Gtk.Align.START)
        box.attach(self.textentry, 0, 1, 1, 1)

        toolbar = Gtk.VBox()
        toolbar.set_valign(Gtk.Align.CENTER)

        browsepicture = ToolButton(icon_name='import_picture',
                                   tooltip=_('Insert picture'))
        toolbar.add(browsepicture)

        browsesound = ToolButton(icon_name='import_sound',
                                 tooltip=_('Insert sound'))
        toolbar.add(browsesound)

        browsepicture.connect('clicked', self._load_image)
        browsesound.connect('clicked', self._load_audio)

        self.usespeak = ToggleToolButton(icon_name='speak')
        self.usespeak.set_palette(SpeakPalette(self))
        toolbar.add(self.usespeak)
        self.usespeak.connect('toggled', self._usespeak_cb)

        self.fontbutton = FontButton()
        toolbar.add(self.fontbutton)
        self.id_font_changed = self.fontbutton.connect(
            'changed', self.__font_changed_cb)
        box.attach(toolbar, 1, 0, 1, 2)

        self.add(box)

    def __font_changed_cb(self, widget):
        font = widget.get_font_name()
        if font:
            self.card.change_font(font)
            self.emit('change-font', font)

    def set_font_name(self, font_name):
        self.fontbutton.handler_block(self.id_font_changed)
        self.fontbutton.set_font_name(font_name)
        self.fontbutton.handler_unblock(self.id_font_changed)

    def update_text(self, entry):
        self.card.change_text(entry.get_text())
        if len(entry.get_text()) == 0:
            self.emit('has-text', False)
        else:
            self.emit('has-text', True)

    def get_text(self):
        return self.textentry.get_text()

    def set_text(self, newtext):
        if newtext is None:
            newtext = ''
        self.textentry.set_text(newtext)

    def get_speak(self):
        if self.usespeak is None:
            return None
        if self.usespeak.props.active:
            return self.usespeak.palette.face.status.voice.language

    def set_speak(self, value):
        if self.usespeak is None:
            return
        if value is None:
            self.usespeak.props.active = False
        else:
            try:
                self.usespeak.handler_block_by_func(self._usespeak_cb)
                self.usespeak.props.active = True
            finally:
                self.usespeak.handler_unblock_by_func(self._usespeak_cb)
            self.usespeak.palette.voices.resume(value)

    def get_image_path(self):
        return self.card.get_image_path()

    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):
            origin_path = jobject.file_path
            self._game.model.mark_modified()
            self._game.model.create_temp_directories()
            destination_path = join(self._game.model.data['pathimg'],
                                    basename(origin_path))
            shutil.copy(origin_path, destination_path)
            self.set_speak(None)
            self.card.set_image_path(destination_path)
            logging.debug('Picture Loaded: %s', destination_path)
            self.emit('has-picture', True)

        chooser.pick(parent=self.get_toplevel(),
                     what=chooser.IMAGE,
                     cb=load)

    def _load_audio(self, widget):
        def load(jobject):
            origin_path = jobject.file_path
            self.set_speak(None)
            self._game.model.mark_modified()
            self._game.model.create_temp_directories()
            destination_path = join(self._game.model.data['pathsnd'],
                                    basename(origin_path))
            shutil.copy(origin_path, destination_path)
            self.set_snd(destination_path)
            logging.debug('Audio Loaded: %s', destination_path)

            # add a icon too
            sound_icon_path = join(activity.get_bundle_path(),
                                   'icons/sound.svg')
            destination_path = join(self._game.model.data['pathimg'],
                                    'sound.svg')
            shutil.copy(sound_icon_path, destination_path)

            self.card.set_image_path(destination_path)
            self.emit('has-sound', True)

        chooser.pick(parent=self.get_toplevel(),
                     what=chooser.AUDIO,
                     cb=load)

    def _usespeak_cb(self, button):
        self.card.change_speak(button.props.active)

        if not button.props.active:
            self.usespeak.palette.face.shut_up()
            return

        self.snd = None
        self.emit('has-sound', False)
        self.emit('has-picture', False)

        button.palette.face.say(self.get_text())

    def set_snd(self, snd):
        self.snd = snd

    def get_snd(self):
        return self.snd

    def get_font_name(self):
        return self.fontbutton.get_font_name()

    def clean(self):
        self.textentry.set_text('')
        self.card.set_image_path(None)
        self.snd = None
        self.emit('has-text', False)
        self.emit('has-picture', False)
        self.emit('has-sound', False)
        if self.usespeak is not None and self.usespeak.palette is not None:
            self.usespeak.props.active = False
            self.usespeak.palette.face.shut_up()