diff options
author | Peng Wu <alexepico@gmail.com> | 2014-11-27 17:43:40 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2014-11-27 17:44:45 +0800 |
commit | 81a84ccbf568b74ed61aeaa8e4977ac89d1aeb8e (patch) | |
tree | c930cf024084bf27c65cdb2778c0c1906170f822 | |
parent | 028425606c60348f25a4d9c9efca5ade89869b27 (diff) | |
download | ibus-libpinyin-81a84ccbf568b74ed61aeaa8e4977ac89d1aeb8e.tar.gz ibus-libpinyin-81a84ccbf568b74ed61aeaa8e4977ac89d1aeb8e.tar.xz ibus-libpinyin-81a84ccbf568b74ed61aeaa8e4977ac89d1aeb8e.zip |
import shortcuteditor.py
-rw-r--r-- | setup/keyboardshortcut.py | 6 | ||||
-rw-r--r-- | setup/shortcuteditor.py | 209 |
2 files changed, 212 insertions, 3 deletions
diff --git a/setup/keyboardshortcut.py b/setup/keyboardshortcut.py index 32b7dc3..755888d 100644 --- a/setup/keyboardshortcut.py +++ b/setup/keyboardshortcut.py @@ -120,9 +120,9 @@ class KeyboardShortcutSelection(Gtk.Box): def set_shortcut(self, shortcut = None): if shortcut == None: - self.__accel_label.set_label("") - else: - self.__accel_label.set_label(shortcut) + shortcut = "" + self.__accel_label.set_label(shortcut) + self.__set_shortcut_to_buttons(shortcut) def get_shortcut(self): return self.__accel_label.get_label() diff --git a/setup/shortcuteditor.py b/setup/shortcuteditor.py new file mode 100644 index 0000000..0fe41d7 --- /dev/null +++ b/setup/shortcuteditor.py @@ -0,0 +1,209 @@ +# vim:set et ts=4 sts=4: +# -*- coding: utf-8 -*- +# +# ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus +# +# Copyright (c) 2014 Peng Wu <alexepico@gmail.com> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + +__all__ = ( + "ShortcutEditor", + "ShortcutEditorDialog" +) + +import gettext +from gi.repository import GObject +from gi.repository import Gdk +from gi.repository import Gtk + +from keyboardshortcut import KeyboardShortcutSelectionDialog + +gettext.install('ibus-libpinyin') + +( +COLUMN_DESCRIPTION, +COLUMN_CONFIG_KEYNAME, +COLUMN_ACCELERATOR, +) = range(3) + +# The default shortcut value is stored here. +accelerators = \ + ( + (_("Switch Chinese/English"), "MainSwitch" , "<Shift>"), + (_("Full/Half Width Letter"), "LetterSwitch", ""), + (_("Full/Half Width Punct"), "PunctSwitch", "<Control>period"), + (_("Switch Traditional/Simplfied Chinese"), "TradSwitch", "<Control><Shift>F") + ) + +class ShortcutTreeView(Gtk.TreeView): + __gtype_name__ = 'ShortcutTreeView' + + def __init__(self, config=None): + super(ShortcutTreeView, self).__init__() + + self.set_headers_visible(True) + + self.__model = self.__create_model() + self.set_model(self.__model) + + self.__add_columns() + + def __create_model(self): + model = Gtk.ListStore(str, str, str) + + for accel in accelerators: + iter = model.append() + # (accel_key, accel_mods) = Gtk.accelerator_parse(accel_str) + model.set(iter, + COLUMN_DESCRIPTION, accel[COLUMN_DESCRIPTION], + COLUMN_CONFIG_KEYNAME, accel[COLUMN_CONFIG_KEYNAME], + COLUMN_ACCELERATOR, accel[COLUMN_ACCELERATOR], + ) + + return model + + def __add_columns(self): + # column for description + renderer = Gtk.CellRendererText() + column = Gtk.TreeViewColumn(_('Description'), renderer, text=COLUMN_DESCRIPTION) + self.append_column(column) + + # column for accelerator + renderer = Gtk.CellRendererText() + column = Gtk.TreeViewColumn(_('Accelerator'), renderer, text=COLUMN_ACCELERATOR) + self.append_column(column) + + def __set_shortcut_value(self, key, value): + # check duplicate shortcut + for row in self.__model: + if row[COLUMN_CONFIG_KEYNAME] == key: + continue + if row[COLUMN_ACCELERATOR] == value: + dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.WARNING, + Gtk.ButtonsType.OK, + _("This shortcut key is already used.")) + dialog.run() + dialog.destroy() + return + + # store the shortcut + for row in self.__model: + if row[COLUMN_CONFIG_KEYNAME] == key: + row[COLUMN_ACCELERATOR] = value + return + + def set_default_shortcut(self): + selection = self.get_selection() + (model, iterator) = selection.get_selected() + + if not iterator: + return + + key = model[iterator][COLUMN_CONFIG_KEYNAME] + for accel in accelerators: + (label, keyname, value) = accel + if key == keyname: + self.__set_shortcut_value(key, value) + + def set_shortcut(self, shortcut=""): + selection = self.get_selection() + (model, iterator) = selection.get_selected() + + if not iterator: + return + + self.__set_shortcut_value( + model[iterator][COLUMN_CONFIG_KEYNAME], + shortcut + ) + + +class ShortcutEditor(Gtk.Box): + def __init__(self, config=None): + super(ShortcutEditor, self).__init__( + orientation=Gtk.Orientation.VERTICAL) + self.__init_ui() + + def __init_ui(self): + # shortcut tree view + self.__shortcut_treeview = ShortcutTreeView() + self.__shortcut_treeview.connect("cursor-changed", self.__shortcut_treeview_cursor_changed_cb) + self.pack_start(self.__shortcut_treeview, False, True, 4) + + # buttons + hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) + # set default button + self.__set_default_button = Gtk.Button(label = _("_Default"), + use_underline = True) + self.__set_default_button.set_sensitive(False) + self.__set_default_button.connect("clicked", self.__set_default_button_clicked_cb) + hbox.pack_start(self.__set_default_button, False, True, 0) + # edit button + self.__edit_button = Gtk.Button(label= _("_Edit"), + use_underline = True) + self.__edit_button.set_sensitive(False) + self.__edit_button.connect("clicked", self.__edit_button_clicked_cb) + hbox.pack_start(self.__edit_button, False, True, 0) + self.pack_start(hbox, False, True, 4) + + def __shortcut_treeview_cursor_changed_cb(self, treeview): + selection = treeview.get_selection() + (model, iterator) = selection.get_selected() + + if iterator: + self.__set_default_button.set_sensitive(True) + self.__edit_button.set_sensitive(True) + else: + self.__set_default_button.set_sensitive(False) + self.__edit_button.set_sensitive(False) + + def __set_default_button_clicked_cb(self, button): + self.__shortcut_treeview.set_default_shortcut() + + def __edit_button_clicked_cb(self, button): + dlg = KeyboardShortcutSelectionDialog(title = _("Select Switching Key")) + buttons = (_("_Cancel"), Gtk.ResponseType.CANCEL, + _("_OK"), Gtk.ResponseType.OK) + dlg.add_buttons(*buttons) + + selection = self.__shortcut_treeview.get_selection() + (model, iterator) = selection.get_selected() + if iterator: + dlg.set_shortcut(model[iterator][COLUMN_ACCELERATOR]) + + response = dlg.run() + dlg.destroy() + if response == Gtk.ResponseType.CANCEL: + return + + self.__shortcut_treeview.set_shortcut(dlg.get_shortcut()) + +class ShortcutEditorDialog(Gtk.Dialog): + def __init__(self, title = None, transient_for = None, flags = 0): + super(ShortcutEditorDialog, self).__init__( + title = title, transient_for = transient_for, flags = flags) + self.__shortcut_editor = ShortcutEditor() + self.vbox.pack_start(self.__shortcut_editor, False, True, 0) + self.vbox.show_all() + + +if __name__ == "__main__": + dlg = ShortcutEditorDialog(title = "Shortcut Editor test") + buttons = (_("_Cancel"), Gtk.ResponseType.CANCEL, + _("_OK"), Gtk.ResponseType.OK) + dlg.add_buttons(*buttons) + print((dlg.run())) |