Example #1
0
    def _create_chartable(self):    
        self._chartable_frame = gtk.Frame()
        self._chartable = CharTable()
        self._chartable_frame.add(self._chartable)

        self._chartable.connect("character-selected", 
                                self._on_character_selected)
Example #2
0
    def _create_ui(self):
        self.add_events(gdk.BUTTON_PRESS_MASK)

        self.set_title("Candidates")

        frame = gtk.Frame()
        self._chartable = CharTable()
        self._chartable.add_events(gdk.BUTTON_PRESS_MASK)
        self._chartable.set_characters(self._candidates)
        self._chartable.set_layout(CharTable.LAYOUT_HORIZONTAL)
        max_width, max_height = self._chartable.get_max_char_size()
        self._chartable.set_size_request(int(max_width*3.5),
                                         int(max_height*3.5))
        frame.add(self._chartable)

        self.connect("button-press-event", self._on_button_press)
        self._chartable.connect("character-selected",
                                self._on_character_selected)

        vbox = gtk.VBox(spacing=2)
        vbox.pack_start(frame)

        self._edit_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_EDIT,
                                         gtk.ICON_SIZE_BUTTON)
        self._edit_button.set_image(image)
        self._edit_button.set_relief(gtk.RELIEF_NONE)
        self._edit_button.connect("clicked", self._on_edit)

        self._delete_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_DELETE,
                                         gtk.ICON_SIZE_BUTTON)
        self._delete_button.set_image(image)
        self._delete_button.set_relief(gtk.RELIEF_NONE)
        self._delete_button.connect("clicked", self._on_delete)

        self._close_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_CLOSE,
                                         gtk.ICON_SIZE_BUTTON)
        self._close_button.set_image(image)
        self._close_button.set_relief(gtk.RELIEF_NONE)
        self._close_button.connect("clicked", self._on_close)

        frame = gtk.Frame()
        buttonbox = gtk.HBox()
        buttonbox.pack_start(self._edit_button, expand=False)
        buttonbox.pack_start(self._delete_button, expand=False)
        buttonbox.pack_start(self._close_button, expand=False)
        frame.add(buttonbox)
        vbox.pack_start(frame)

        self.add(vbox)
Example #3
0
class CandidatePopup(gtk.Window):

    __gsignals__ = {
        "character_selected": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [gobject.TYPE_PYOBJECT]),
        "edit-character": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []),
        "delete-character": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []),
    }

    def __init__(self, candidates):
        gtk.Window.__init__(self, gtk.WINDOW_POPUP)
        self._candidates = candidates
        self._create_ui()

    def get_selected(self):
        return self._chartable.get_selected()

    def _create_ui(self):
        self.add_events(gdk.BUTTON_PRESS_MASK)

        self.set_title("Candidates")

        frame = gtk.Frame()
        self._chartable = CharTable()
        self._chartable.add_events(gdk.BUTTON_PRESS_MASK)
        self._chartable.set_characters(self._candidates)
        self._chartable.set_layout(CharTable.LAYOUT_HORIZONTAL)
        max_width, max_height = self._chartable.get_max_char_size()
        self._chartable.set_size_request(max_width * 3, max_height * 3)
        frame.add(self._chartable)

        self.connect("button-press-event", self._on_button_press)
        self._chartable.connect("character-selected", self._on_character_selected)

        vbox = gtk.VBox(spacing=2)
        vbox.pack_start(frame)

        self._edit_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_EDIT, gtk.ICON_SIZE_BUTTON)
        self._edit_button.set_image(image)
        self._edit_button.set_relief(gtk.RELIEF_NONE)
        self._edit_button.connect("clicked", self._on_edit)

        self._delete_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_DELETE, gtk.ICON_SIZE_BUTTON)
        self._delete_button.set_image(image)
        self._delete_button.set_relief(gtk.RELIEF_NONE)
        self._delete_button.connect("clicked", self._on_delete)

        self._close_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_BUTTON)
        self._close_button.set_image(image)
        self._close_button.set_relief(gtk.RELIEF_NONE)
        self._close_button.connect("clicked", self._on_close)

        frame = gtk.Frame()
        buttonbox = gtk.HBox()
        buttonbox.pack_start(self._edit_button, expand=False)
        buttonbox.pack_start(self._delete_button, expand=False)
        buttonbox.pack_start(self._close_button, expand=False)
        frame.add(buttonbox)
        vbox.pack_start(frame)

        self.add(vbox)

    def _on_close(self, button):
        self.popdown()

    def _on_edit(self, button):
        self.emit("edit-character")
        self.popdown()

    def _on_delete(self, button):
        self.emit("delete-character")
        self.popdown()

    def _on_character_selected(self, chartable, event):
        self.emit("character-selected", event)

    def _on_button_press(self, window, event):
        # If we're clicking outside of the window or in the chartable
        # close the popup
        if event.window != self.window or (
            tuple(self.allocation.intersect(gdk.Rectangle(x=int(event.x), y=int(event.y), width=1, height=1)))
        ) == (0, 0, 0, 0):
            self.popdown()

    def popup(self):
        self.show_all()

        # grab pointer
        self.grab_add()
        gdk.pointer_grab(
            self.window,
            True,
            gdk.BUTTON_PRESS_MASK | gdk.BUTTON_RELEASE_MASK | gdk.POINTER_MOTION_MASK,
            None,
            None,
            gtk.get_current_event_time(),
        )

    def popdown(self):
        gdk.pointer_ungrab(gtk.get_current_event_time())
        self.grab_remove()
        self.destroy()
Example #4
0
class RecognizerWidgetBase(gtk.HBox):

    DEFAULT_CANVAS_WIDTH = 250

    __gsignals__ = {"commit-string": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [gobject.TYPE_STRING])}

    def __init__(self):
        gtk.HBox.__init__(self)

        self._recognizer = None
        self._search_on_stroke = True

        self._create_ui()
        self.clear_canvas()
        self.clear_characters()

        self._load_preferences()

    def _load_preferences(self):
        pm = PreferenceManager()
        pm.load()
        self.set_drawing_stopped_time(pm["GENERAL"]["DRAWING_STOPPED_TIME"])
        self.set_search_on_stroke(pm["GENERAL"]["SEARCH_ON_STROKE"])
        self.set_selected_model(pm["GENERAL"]["SELECTED_MODEL"])
        self.set_draw_annotations(pm["GENERAL"]["DRAW_ANNOTATIONS"])

    def _save_preferences(self):
        pm = PreferenceManager()
        pm["GENERAL"]["DRAWING_STOPPED_TIME"] = self.get_drawing_stopped_time()
        pm["GENERAL"]["SEARCH_ON_STROKE"] = self.get_search_on_stroke()
        pm["GENERAL"]["SELECTED_MODEL"] = self.get_selected_model()
        pm["GENERAL"]["DRAW_ANNOTATIONS"] = self.get_draw_annotations()
        pm.save()

    def _create_toolbar_separator(self):
        self._toolbar.pack_start(gtk.HSeparator(), expand=False)

    def _create_clear_button(self):
        self._clear_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_CLEAR, gtk.ICON_SIZE_BUTTON)
        self._clear_button.set_image(image)
        self._clear_button.connect("clicked", self._on_clear)
        self._toolbar.pack_start(self._clear_button, expand=False)

    def _create_find_button(self):
        self._find_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON)
        self._find_button.set_image(image)
        self._find_button.connect("clicked", self._on_find)
        self._toolbar.pack_start(self._find_button, expand=False)

    def _create_undo_button(self):
        self._undo_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_UNDO, gtk.ICON_SIZE_BUTTON)
        self._undo_button.set_image(image)
        self._undo_button.connect("clicked", self._on_undo)
        self._toolbar.pack_start(self._undo_button, expand=False)

    def _create_prefs_button(self):
        self._prefs_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_PREFERENCES, gtk.ICON_SIZE_BUTTON)
        self._prefs_button.set_image(image)
        self._prefs_button.connect("clicked", self._on_prefs)
        self._toolbar.pack_start(self._prefs_button, expand=False)

    def _create_models_button(self):
        self._models_button = gtk.Button("Models")
        self._models_button.connect("button-press-event", self._on_models)
        self._toolbar.pack_start(self._models_button, expand=False)

    def _create_model_menu(self):
        menu = gtk.Menu()

        all_models = Recognizer.get_all_available_models()

        if len(all_models) == 0:
            return None

        i = 0
        for r_name, model_name, meta in all_models:
            item = gtk.MenuItem("%d. %s (%s)" % (i + 1, model_name, r_name))
            item.connect("activate", self._on_activate_model, i)
            menu.append(item)
            i += 1

        return menu

    def _create_canvas(self, canvas_name):
        canvas = Canvas()
        canvas.set_size_request(self.DEFAULT_CANVAS_WIDTH, self.DEFAULT_CANVAS_WIDTH)

        canvas.connect("button-press-event", self._on_canvas_button_press, canvas_name)

        canvas.connect("drawing-stopped", self._on_canvas_drawing_stopped, canvas_name)

        canvas.connect("stroke-added", self._on_canvas_stroke_added, canvas_name)

        setattr(self, canvas_name, canvas)

        frame = gtk.Frame()
        frame.add(canvas)

        setattr(self, canvas_name + "_frame", frame)

    def _create_chartable(self):
        self._chartable_frame = gtk.Frame()
        self._chartable = CharTable()
        self._chartable_frame.add(self._chartable)

        self._chartable.connect("character-selected", self._on_character_selected)

    def _on_models(self, button, event):
        menu = self._create_model_menu()
        if menu:
            menu.show_all()
            menu.popup(None, None, None, event.button, event.time)
        else:
            parent = self.get_toplevel()
            msg = "No recognizers and/or no models installed!"
            dialog = ErrorDialog(parent, msg).run()

    def _on_activate_model(self, item, i):
        self.set_selected_model(i)
        self._save_preferences()

    def _on_find(self, button):
        self.find()

    def _on_undo(self, button):
        self.revert_stroke()

    def _on_prefs(self, button):
        parent = self.get_toplevel()
        if not parent.flags() & gtk.TOPLEVEL:
            parent = None
        pref_dialog = PreferenceDialog(parent)

        pref_dialog.connect("response", self._on_pref_validate)

        pref_dialog.set_search_on_stroke(self.get_search_on_stroke())
        pref_dialog.set_drawing_stopped_time(self.get_drawing_stopped_time())
        pref_dialog.set_draw_annotations(self.get_draw_annotations())

        pref_dialog.run()

    def _on_pref_validate(self, dialog, response):
        if response == gtk.RESPONSE_OK:
            if dialog.get_search_on_stroke():
                self.set_search_on_stroke(True)
            else:
                self.set_drawing_stopped_time(dialog.get_drawing_stopped_time())
            self.set_draw_annotations(dialog.get_draw_annotations())
            self._save_preferences()

        dialog.destroy()

    def _on_clear(self, button):
        self.clear_canvas()

    def clear_all(self):
        self.clear_characters()
        self.clear_canvas()

    def get_search_on_stroke(self):
        return self._search_on_stroke

    def set_search_on_stroke(self, enabled):
        self._search_on_stroke = enabled

    def get_characters(self):
        return self._chartable.get_characters()

    def get_selected_model(self):
        return self._models_button.selected_model

    def set_selected_model(self, i):
        try:
            r_name, model_name, meta = Recognizer.get_all_available_models()[i]

            klass = Recognizer.get_available_recognizers()[r_name]
            self._recognizer = klass()
            self._recognizer.set_model(meta["name"])
            self._models_button.set_label(meta["shortname"])
            # a hack to retain the model id the button
            self._models_button.selected_model = i

            self._ready = True
        except IndexError:
            self._ready = False

    def get_toolbar_vbox(self):
        return self._toolbar
Example #5
0
class CandidatePopup(gtk.Window):

    __gsignals__ = {

        "character_selected" : (gobject.SIGNAL_RUN_LAST,
                                gobject.TYPE_NONE,
                                [gobject.TYPE_PYOBJECT]),

        "edit-character"     : (gobject.SIGNAL_RUN_LAST, 
                                gobject.TYPE_NONE,
                                []),

        "delete-character"   : (gobject.SIGNAL_RUN_LAST, 
                                gobject.TYPE_NONE,
                                [])
    }

    def __init__(self, candidates):
        gtk.Window.__init__(self, gtk.WINDOW_POPUP)
        self._candidates = candidates
        self._create_ui()

    def get_selected(self):
        return self._chartable.get_selected()

    def _create_ui(self):
        self.add_events(gdk.BUTTON_PRESS_MASK)

        self.set_title("Candidates")

        frame = gtk.Frame()
        self._chartable = CharTable()
        self._chartable.add_events(gdk.BUTTON_PRESS_MASK)
        self._chartable.set_characters(self._candidates)
        self._chartable.set_layout(CharTable.LAYOUT_HORIZONTAL)
        max_width, max_height = self._chartable.get_max_char_size()
        self._chartable.set_size_request(int(max_width*3.5),
                                         int(max_height*3.5))
        frame.add(self._chartable)

        self.connect("button-press-event", self._on_button_press)
        self._chartable.connect("character-selected",
                                self._on_character_selected)

        vbox = gtk.VBox(spacing=2)
        vbox.pack_start(frame)

        self._edit_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_EDIT,
                                         gtk.ICON_SIZE_BUTTON)
        self._edit_button.set_image(image)
        self._edit_button.set_relief(gtk.RELIEF_NONE)
        self._edit_button.connect("clicked", self._on_edit)

        self._delete_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_DELETE,
                                         gtk.ICON_SIZE_BUTTON)
        self._delete_button.set_image(image)
        self._delete_button.set_relief(gtk.RELIEF_NONE)
        self._delete_button.connect("clicked", self._on_delete)

        self._close_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_CLOSE,
                                         gtk.ICON_SIZE_BUTTON)
        self._close_button.set_image(image)
        self._close_button.set_relief(gtk.RELIEF_NONE)
        self._close_button.connect("clicked", self._on_close)

        frame = gtk.Frame()
        buttonbox = gtk.HBox()
        buttonbox.pack_start(self._edit_button, expand=False)
        buttonbox.pack_start(self._delete_button, expand=False)
        buttonbox.pack_start(self._close_button, expand=False)
        frame.add(buttonbox)
        vbox.pack_start(frame)

        self.add(vbox)

    def _on_close(self, button):
        self.popdown()

    def _on_edit(self, button):
        self.emit("edit-character")
        self.popdown()

    def _on_delete(self, button):
        self.emit("delete-character")
        self.popdown()

    def _on_character_selected(self, chartable, event):
        self.emit("character-selected", event)

    def _on_button_press(self, window, event):
        # If we're clicking outside of the window or in the chartable
        # close the popup
        if (event.window != self.window or
            (tuple(self.allocation.intersect(
                   gdk.Rectangle(x=int(event.x), y=int(event.y),
                                 width=1, height=1)))) == (0, 0, 0, 0)):
            self.popdown()

    def popup(self):
        self.show_all()

        # grab pointer
        self.grab_add()
        gdk.pointer_grab(self.window,
                         True,
                         gdk.BUTTON_PRESS_MASK|
                         gdk.BUTTON_RELEASE_MASK|
                         gdk.POINTER_MOTION_MASK,
                         None, None, 
                         gtk.get_current_event_time())

    def popdown(self):
        gdk.pointer_ungrab(gtk.get_current_event_time())
        self.grab_remove()
        self.destroy()
Example #6
0
class RecognizerWidgetBase(gtk.HBox):

    DEFAULT_CANVAS_WIDTH = 250

    __gsignals__ = {

        "commit-string" :         (gobject.SIGNAL_RUN_LAST, 
                                   gobject.TYPE_NONE,
                                   [gobject.TYPE_STRING])
    }

    def __init__(self):
        gtk.HBox.__init__(self)

        self._recognizer = None
        self._search_on_stroke = True

        self._create_ui()
        self.clear_canvas()
        self.clear_characters()

        self._load_preferences()

    def _load_preferences(self):
        pm = PreferenceManager()
        pm.load()
        self.set_drawing_stopped_time(pm["GENERAL"]["DRAWING_STOPPED_TIME"])
        self.set_search_on_stroke(pm["GENERAL"]["SEARCH_ON_STROKE"])
        self.set_selected_model(pm["GENERAL"]["SELECTED_MODEL"])
        self.set_draw_annotations(pm["GENERAL"]["DRAW_ANNOTATIONS"])

    def _save_preferences(self):
        pm = PreferenceManager()
        pm["GENERAL"]["DRAWING_STOPPED_TIME"] = self.get_drawing_stopped_time()
        pm["GENERAL"]["SEARCH_ON_STROKE"] = self.get_search_on_stroke()
        pm["GENERAL"]["SELECTED_MODEL"] = self.get_selected_model()
        pm["GENERAL"]["DRAW_ANNOTATIONS"] = self.get_draw_annotations()
        pm.save()

    def _create_toolbar_separator(self):
        self._toolbar.pack_start(gtk.HSeparator(), expand=False)

    def _create_clear_button(self):
        self._clear_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_CLEAR, gtk.ICON_SIZE_BUTTON)
        self._clear_button.set_image(image)
        self._clear_button.connect("clicked", self._on_clear)
        self._toolbar.pack_start(self._clear_button, expand=False)

    def _create_find_button(self):
        self._find_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON)
        self._find_button.set_image(image)
        self._find_button.connect("clicked", self._on_find)
        self._toolbar.pack_start(self._find_button, expand=False)

    def _create_undo_button(self):
        self._undo_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_UNDO, gtk.ICON_SIZE_BUTTON)
        self._undo_button.set_image(image)
        self._undo_button.connect("clicked", self._on_undo)
        self._toolbar.pack_start(self._undo_button, expand=False)

    def _create_prefs_button(self):
        self._prefs_button = gtk.Button()
        image = gtk.image_new_from_stock(gtk.STOCK_PREFERENCES,
                                         gtk.ICON_SIZE_BUTTON)
        self._prefs_button.set_image(image)
        self._prefs_button.connect("clicked", self._on_prefs)
        self._toolbar.pack_start(self._prefs_button, expand=False)

    def _create_models_button(self):
        self._models_button = gtk.Button("Models")
        self._models_button.connect("button-press-event", self._on_models)
        self._toolbar.pack_start(self._models_button, expand=False)

    def _create_model_menu(self):
        menu = gtk.Menu()

        all_models = Recognizer.get_all_available_models()

        if len(all_models) == 0:
            return None

        i = 0
        for r_name, model_name, meta in all_models:
            item = gtk.MenuItem("%d. %s (%s)" % (i+1, model_name, r_name))
            item.connect("activate", self._on_activate_model, i)
            menu.append(item)
            i += 1

        return menu

    def _create_canvas(self, canvas_name):
        canvas = Canvas()
        canvas.set_size_request(self.DEFAULT_CANVAS_WIDTH,
                                self.DEFAULT_CANVAS_WIDTH)

        canvas.connect("button-press-event",
                       self._on_canvas_button_press,
                       canvas_name)

        canvas.connect("drawing-stopped",
                       self._on_canvas_drawing_stopped,
                       canvas_name)

        canvas.connect("stroke-added",
                       self._on_canvas_stroke_added,
                       canvas_name)

        setattr(self, canvas_name, canvas)

        frame = gtk.Frame()
        frame.add(canvas)

        setattr(self, canvas_name + "_frame", frame)

    def _create_chartable(self):    
        self._chartable_frame = gtk.Frame()
        self._chartable = CharTable()
        self._chartable_frame.add(self._chartable)

        self._chartable.connect("character-selected", 
                                self._on_character_selected)

    def _on_models(self, button, event):
        menu = self._create_model_menu()
        if menu:
            menu.show_all()
            menu.popup(None, None, None, event.button, event.time)
        else:
            parent = self.get_toplevel()
            msg = "No recognizers and/or no models installed!"
            dialog = ErrorDialog(parent, msg).run()

    def _on_activate_model(self, item, i):
        self.set_selected_model(i)
        self._save_preferences()

    def _on_find(self, button):
        self.find()

    def _on_undo(self, button):
        self.revert_stroke()

    def _on_prefs(self, button):
        parent = self.get_toplevel()
        if not parent.flags() & gtk.TOPLEVEL:
            parent = None
        pref_dialog = PreferenceDialog(parent)

        pref_dialog.connect("response", self._on_pref_validate)

        pref_dialog.set_search_on_stroke(self.get_search_on_stroke())
        pref_dialog.set_drawing_stopped_time(self.get_drawing_stopped_time())
        pref_dialog.set_draw_annotations(self.get_draw_annotations())

        pref_dialog.run()

    def _on_pref_validate(self, dialog, response):
        if response == gtk.RESPONSE_OK:
            if dialog.get_search_on_stroke():
                self.set_search_on_stroke(True)
            else:
                self.set_drawing_stopped_time(dialog.get_drawing_stopped_time())
            self.set_draw_annotations(dialog.get_draw_annotations())
            self._save_preferences()

        dialog.destroy()

    def _on_clear(self, button):
        self.clear_canvas()

    def clear_all(self):
        self.clear_characters()
        self.clear_canvas()

    def get_search_on_stroke(self):
        return self._search_on_stroke

    def set_search_on_stroke(self, enabled):
        self._search_on_stroke = enabled

    def get_characters(self):
        return self._chartable.get_characters()

    def get_selected_model(self):
       return self._models_button.selected_model

    def set_selected_model(self, i):
        try:
            r_name, model_name, meta = Recognizer.get_all_available_models()[i]

            klass = Recognizer.get_available_recognizers()[r_name]
            self._recognizer = klass()
            self._recognizer.set_model(meta["name"])
            self._models_button.set_label(meta["shortname"])
            # a hack to retain the model id the button
            self._models_button.selected_model = i

            self._ready = True
        except IndexError:
            self._ready = False

    def get_toolbar_vbox(self):
        return self._toolbar