Repository : http://git.fedorahosted.org/git/?p=ibus-typing-booster.git
On branch : master
commit ab477117e1a5cc0a2838ddfd086f2a9268bc6492 Author: Mike FABIAN mfabian@redhat.com Date: Fri Nov 18 14:29:55 2016 +0100
Display existing shortcuts and make it possible to delete them
engine/itb_util.py | 5 +++ engine/tabsqlitedb.py | 23 ++++++++++++ po/Makevars | 2 +- setup/main.py | 90 +++++++++++++++++++++++++++++++++++++++++++++---- setup/setup.glade | 57 +++++++++++++++++++++++++++---- 5 files changed, 162 insertions(+), 15 deletions(-)
diff --git a/engine/itb_util.py b/engine/itb_util.py index 027f7ca..5f5726c 100755 --- a/engine/itb_util.py +++ b/engine/itb_util.py @@ -28,6 +28,11 @@ import unicodedata
from gi.repository import GLib
+# maximum possible value for the INTEGER datatype in SQLite3 +SQLITE_MAXINT = 2**63-1 +# user frequency used for a user defined shortcut +SHORTCUT_USER_FREQ = 1000000 + # If a character ending a sentence is committed (possibly # followed by whitespace) remove trailing white space # before the committed string. For example if diff --git a/engine/tabsqlitedb.py b/engine/tabsqlitedb.py index 4f56a5d..e8149f0 100755 --- a/engine/tabsqlitedb.py +++ b/engine/tabsqlitedb.py @@ -717,6 +717,29 @@ CREATE TABLE phrases (id INTEGER PRIMARY KEY, input_phrase TEXT, phrase TEXT, p_ except: return 0
+ def list_user_shortcuts(self): + '''Returns a list of user defined shortcuts from the user database. + + :rtype: List of tuples of strings: [(str, str), ...] + + ''' + sqlstr = ''' + SELECT input_phrase, phrase FROM user_db.phrases WHERE user_freq >= :freq + ;''' + sqlargs = {'freq': itb_util.SHORTCUT_USER_FREQ} + if DEBUG_LEVEL > 1: + sys.stderr.write( + "tabsqlitedb.list_user_shortcuts() sqlstr=%s\n" + %sqlstr) + sys.stderr.write( + "tabsqlitedb.list_user_shortcuts() sqlargs=%s\n" + %sqlargs) + result = self.db.execute(sqlstr, sqlargs).fetchall() + if DEBUG_LEVEL > 1: + sys.stderr.write( + "tabsqlite.list_user_shortcuts() result=%s\n" %result) + return result + def check_phrase_and_update_frequency( self, input_phrase = '', phrase = '', p_phrase = '', pp_phrase = '', user_freq_increment = 1, commit=True): diff --git a/po/Makevars b/po/Makevars index 0dc6124..57aa77e 100644 --- a/po/Makevars +++ b/po/Makevars @@ -8,7 +8,7 @@ subdir = po top_builddir = ..
# These options get passed to xgettext. -XGETTEXT_OPTIONS = --keyword=_ --keyword=N_ +XGETTEXT_OPTIONS = -c --keyword=_ --keyword=N_
# This is the copyright holder that gets inserted into the header of the # $(DOMAIN).pot file. Set this to the copyright holder of the surrounding diff --git a/setup/main.py b/setup/main.py index 045b061..6dd7852 100644 --- a/setup/main.py +++ b/setup/main.py @@ -162,10 +162,35 @@ class SetupUI: "shortcut_clear_button") shortcut_clear_button.connect( 'clicked', event_handler.on_shortcut_clear_clicked) + shortcut_delete_button = self.builder.get_object( + "shortcut_delete_button") + shortcut_delete_button.connect( + 'clicked', event_handler.on_shortcut_delete_clicked) shortcut_add_button = self.builder.get_object( "shortcut_add_button") shortcut_add_button.connect( 'clicked', event_handler.on_shortcut_add_clicked) + self.shortcut_treeview = self.builder.get_object( + "shortcut_treeview") + self.shortcut_treeview_model = Gtk.ListStore(str, str) + self.shortcut_treeview.set_model(self.shortcut_treeview_model) + current_shortcuts = self.tabsqlitedb.list_user_shortcuts() + for i, shortcut in enumerate(current_shortcuts): + self.shortcut_treeview_model.append(shortcut) + self.shortcut_treeview.append_column( + Gtk.TreeViewColumn( + # Translators: Column heading of the table listing the existing shortcuts + _('Shortcut'), + Gtk.CellRendererText(), + text=0)) + self.shortcut_treeview.append_column( + Gtk.TreeViewColumn( + # Translators: Column heading of the table listing the existing shortcuts + _('Shortcut expansion'), + Gtk.CellRendererText(), + text=1)) + self.shortcut_treeview.get_selection().connect( + 'changed', event_handler.on_shortcut_selected)
self.install_dictionary_button = self.builder.get_object( "install_dictionary_button") @@ -468,20 +493,71 @@ class EventHandler: ''' SETUP_UI.shortcut_entry.set_text('') SETUP_UI.shortcut_expansion_entry.set_text('') + SETUP_UI.shortcut_treeview.get_selection().unselect_all() + + def on_shortcut_delete_clicked(self, dummy_widget): + ''' + The button to delete a custom shortcut has been clicked. + ''' + shortcut = SETUP_UI.shortcut_entry.get_text().strip() + shortcut_expansion = ( + SETUP_UI.shortcut_expansion_entry.get_text().strip()) + SETUP_UI.shortcut_entry.set_text('') + SETUP_UI.shortcut_expansion_entry.set_text('') + SETUP_UI.shortcut_treeview.get_selection().unselect_all() + if shortcut and shortcut_expansion: + model = SETUP_UI.shortcut_treeview_model + iterator = model.get_iter_first() + while iterator: + if (model.get_value(iterator, 0) == shortcut + and + model.get_value(iterator, 1) == shortcut_expansion): + SETUP_UI.tabsqlitedb.remove_phrase( + input_phrase=shortcut, + phrase=shortcut_expansion) + if not model.remove(iterator): + iterator = None + else: + iterator = model.iter_next(iterator)
def on_shortcut_add_clicked(self, dummy_widget): ''' The button to add a custom shortcut has been clicked. ''' + SETUP_UI.shortcut_treeview.get_selection().unselect_all() shortcut = SETUP_UI.shortcut_entry.get_text().strip() - shortcut_expansion = SETUP_UI.shortcut_expansion_entry.get_text().strip() + shortcut_expansion = ( + SETUP_UI.shortcut_expansion_entry.get_text().strip()) if shortcut and shortcut_expansion: - sys.stderr.write( - 'defining shortcut: ���%s��� -> ���%s���\n' %(shortcut, shortcut_expansion)) - SETUP_UI.tabsqlitedb.check_phrase_and_update_frequency( - input_phrase=shortcut, - phrase=shortcut_expansion, - user_freq_increment=1000000) + model = SETUP_UI.shortcut_treeview_model + iterator = model.get_iter_first() + shortcut_existing = False + while iterator: + if (model.get_value(iterator, 0) == shortcut + and + model.get_value(iterator, 1) == shortcut_expansion): + shortcut_existing = True + iterator = model.iter_next(iterator) + if not shortcut_existing: + sys.stderr.write( + 'defining shortcut: ���%s��� -> ���%s���\n' + %(shortcut, shortcut_expansion)) + SETUP_UI.tabsqlitedb.check_phrase_and_update_frequency( + input_phrase=shortcut, + phrase=shortcut_expansion, + user_freq_increment=itb_util.SHORTCUT_USER_FREQ) + model.append((shortcut, shortcut_expansion)) + + def on_shortcut_selected(self, selection): + ''' + A row in the list of shortcuts has been selected. + ''' + (model, iterator) = selection.get_selected() + if iterator: + shortcut = model[iterator][0] + shortcut_expansion = model[iterator][1] + SETUP_UI.shortcut_entry.set_text(shortcut) + SETUP_UI.shortcut_expansion_entry.set_text(shortcut_expansion)
def on_install_dictionary_clicked(self, dummy_widget): ''' diff --git a/setup/setup.glade b/setup/setup.glade index 7830878..b0e71c6 100644 --- a/setup/setup.glade +++ b/setup/setup.glade @@ -344,6 +344,8 @@ <packing> <property name="top_attach">7</property> <property name="bottom_attach">8</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> </packing> </child> <child> @@ -356,6 +358,8 @@ <property name="right_attach">2</property> <property name="top_attach">7</property> <property name="bottom_attach">8</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> </packing> </child> </object> @@ -399,6 +403,8 @@ <property name="margin_right">10</property> <property name="margin_top">10</property> <property name="margin_bottom">10</property> + <property name="hexpand">True</property> + <property name="vexpand">True</property> <property name="row_spacing">10</property> <property name="column_spacing">10</property> <child> @@ -425,7 +431,7 @@ <property name="vexpand">False</property> </object> <packing> - <property name="left_attach">1</property> + <property name="left_attach">2</property> <property name="top_attach">4</property> </packing> </child> @@ -440,7 +446,7 @@ <packing> <property name="left_attach">0</property> <property name="top_attach">0</property> - <property name="width">2</property> + <property name="width">3</property> </packing> </child> <child> @@ -454,7 +460,7 @@ <packing> <property name="left_attach">0</property> <property name="top_attach">2</property> - <property name="width">2</property> + <property name="width">3</property> </packing> </child> <child> @@ -465,7 +471,7 @@ <packing> <property name="left_attach">0</property> <property name="top_attach">1</property> - <property name="width">2</property> + <property name="width">3</property> </packing> </child> <child> @@ -476,7 +482,44 @@ <packing> <property name="left_attach">0</property> <property name="top_attach">3</property> - <property name="width">2</property> + <property name="width">3</property> + </packing> + </child> + <child> + <object class="GtkScrolledWindow"> + <property name="visible">True</property> + <property name="can_focus">True</property> + <property name="hexpand">True</property> + <property name="vexpand">True</property> + <property name="shadow_type">in</property> + <child> + <object class="GtkTreeView" id="shortcut_treeview"> + <property name="visible">True</property> + <property name="can_focus">True</property> + <child internal-child="selection"> + <object class="GtkTreeSelection"/> + </child> + </object> + </child> + </object> + <packing> + <property name="left_attach">0</property> + <property name="top_attach">5</property> + <property name="width">3</property> + <property name="height">3</property> + </packing> + </child> + <child> + <object class="GtkButton" id="shortcut_delete_button"> + <property name="label" translatable="yes">Delete shortcut</property> + <property name="visible">True</property> + <property name="can_focus">True</property> + <property name="receives_default">True</property> + <property name="hexpand">True</property> + </object> + <packing> + <property name="left_attach">1</property> + <property name="top_attach">4</property> </packing> </child> </object> @@ -756,8 +799,8 @@ </child> </object> <packing> - <property name="expand">False</property> - <property name="fill">False</property> + <property name="expand">True</property> + <property name="fill">True</property> <property name="position">1</property> </packing> </child>
ibus-typing-booster@lists.fedorahosted.org