Debian Codegraph @

setup/keyboardshortcut.py

raw
1 # vim:set et sts=4 sw=4:
2 #
3 # ibus - The Input Bus
4 #
5 # Copyright (c) 2007-2015 Peng Huang <[email protected]>
6 # Copyright (c) 2007-2015 Red Hat, Inc.
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21 # USA
22
23 __all__ = (
24 "KeyboardShortcutSelection",
25 "KeyboardShortcutSelectionDialog",
26 );
27
28 from gi.repository import Gdk
29 from gi.repository import GObject
30 from gi.repository import Gtk
31 from gi.repository import IBus
32 from gi.repository import Pango
33
34 from i18n import _, N_
35
36 MAX_HOTKEY = 6
37
38 class KeyboardShortcutSelection(Gtk.Box):
39 def __init__(self, shortcuts = None):
40 super(KeyboardShortcutSelection, self).__init__(
41 orientation=Gtk.Orientation.VERTICAL)
42 self.__init_ui()
43 self.set_shortcuts(shortcuts)
44
45 def __init_ui(self):
46 # label = Gtk.Label(_("Keyboard shortcuts:"))
47 # label.set_justify(Gtk.Justification.LEFT)
48 # label.set_alignment(0.0, 0.5)
49 # self.pack_start(label, False, True, 4)
50
51 # shortcuts view
52 self.__shortcut_view = Gtk.TreeView(
53 model = Gtk.ListStore(GObject.TYPE_STRING))
54 renderer = Gtk.CellRendererText()
55 column = Gtk.TreeViewColumn(_("Keyboard shortcuts"), renderer, text = 0)
56 self.__shortcut_view.append_column(column)
57 self.__shortcut_view.connect("cursor-changed", self.__shortcut_view_cursor_changed_cb)
58 scrolledwindow = Gtk.ScrolledWindow()
59 scrolledwindow.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
60 scrolledwindow.set_min_content_height(100)
61 scrolledwindow.add(self.__shortcut_view)
62 scrolledwindow.set_shadow_type(Gtk.ShadowType.IN)
63 self.pack_start(scrolledwindow, True, True, 4)
64
65 # key code
66 hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
67 label = Gtk.Label(label = _("Key code:"))
68 label.set_justify(Gtk.Justification.LEFT)
69 label.set_alignment(0.0, 0.5)
70 hbox.pack_start(label, False, True, 4)
71
72 self.__keycode_entry = Gtk.Entry()
73 self.__keycode_entry.connect("notify::text", self.__keycode_entry_notify_cb)
74 hbox.pack_start(self.__keycode_entry, True, True, 4)
75 self.__keycode_button = Gtk.Button(label = "...")
76 self.__keycode_button.connect("clicked", self.__keycode_button_clicked_cb)
77 hbox.pack_start(self.__keycode_button, False, True, 4)
78 self.pack_start(hbox, False, True, 4)
79
80 # modifiers
81 hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
82 label = Gtk.Label(label = _("Modifiers:"))
83 label.set_justify(Gtk.Justification.LEFT)
84 label.set_alignment(0.0, 0.5)
85 hbox.pack_start(label, False, True, 4)
86
87 table = Gtk.Table(n_rows = 4, n_columns = 2)
88 self.__modifier_buttons = []
89 self.__modifier_buttons.append(("Control",
90 Gtk.CheckButton.new_with_mnemonic("_Control"),
91 Gdk.ModifierType.CONTROL_MASK))
92 self.__modifier_buttons.append(("Alt",
93 Gtk.CheckButton.new_with_mnemonic("A_lt"),
94 Gdk.ModifierType.MOD1_MASK))
95 self.__modifier_buttons.append(("Shift",
96 Gtk.CheckButton.new_with_mnemonic("_Shift"),
97 Gdk.ModifierType.SHIFT_MASK))
98 self.__modifier_buttons.append(("Meta",
99 Gtk.CheckButton.new_with_mnemonic("_Meta"),
100 Gdk.ModifierType.META_MASK))
101 self.__modifier_buttons.append(("Super",
102 Gtk.CheckButton.new_with_mnemonic("S_uper"),
103 Gdk.ModifierType.SUPER_MASK))
104 self.__modifier_buttons.append(("Hyper",
105 Gtk.CheckButton.new_with_mnemonic("_Hyper"),
106 Gdk.ModifierType.HYPER_MASK))
107 # <CapsLock> is not parsed by gtk_accelerator_parse()
108 # <Release> is not supported by XIGrabKeycode()
109 for name, button, mask in self.__modifier_buttons:
110 button.connect("toggled", self.__modifier_button_toggled_cb, name)
111
112 table.attach(self.__modifier_buttons[0][1], 0, 1, 0, 1)
113 table.attach(self.__modifier_buttons[1][1], 1, 2, 0, 1)
114 table.attach(self.__modifier_buttons[2][1], 2, 3, 0, 1)
115 table.attach(self.__modifier_buttons[3][1], 0, 1, 1, 2)
116 table.attach(self.__modifier_buttons[4][1], 1, 2, 1, 2)
117 table.attach(self.__modifier_buttons[5][1], 2, 3, 1, 2)
118 hbox.pack_start(table, True, True, 4)
119 self.pack_start(hbox, False, True, 4)
120
121 # buttons
122 hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
123 # add button
124 self.__add_button = Gtk.Button(label = _("_Add"),
125 use_underline = True)
126 self.__add_button.set_sensitive(False)
127 self.__add_button.connect("clicked", self.__add_button_clicked_cb)
128 hbox.pack_start(self.__add_button, False, True, 0)
129 # apply button
130 self.__apply_button = Gtk.Button(label = _("_Apply"),
131 use_underline = True)
132 self.__apply_button.set_sensitive(False)
133 self.__apply_button.connect("clicked", self.__apply_button_clicked_cb)
134 hbox.pack_start(self.__apply_button, False, True, 0)
135 # delete button
136 self.__delete_button = Gtk.Button(label = _("_Delete"),
137 use_underline = True)
138 self.__delete_button.set_sensitive(False)
139 self.__delete_button.connect("clicked", self.__delete_button_clicked_cb)
140 hbox.pack_start(self.__delete_button, False, True, 0)
141 self.pack_start(hbox, False, True, 4)
142
143 def set_shortcuts(self, shortcuts = None):
144 if shortcuts == None:
145 shortcuts = []
146 model = self.__shortcut_view.get_model()
147 model.clear()
148
149 added = []
150 for shortcut in shortcuts:
151 if shortcut not in added:
152 it = model.insert(0)
153 model[it][0] = shortcut
154 added.append(shortcut)
155
156 def get_shortcuts(self):
157 model = self.__shortcut_view.get_model()
158 try:
159 return [i[0] for i in model]
160 except:
161 return []
162
163 def add_shortcut(self, shortcut):
164 model = self.__shortcut_view.get_model()
165 if len(model) > MAX_HOTKEY:
166 return
167 if shortcut in self.get_shortcuts():
168 return
169 it = model.insert(0)
170 model[it][0] = shortcut
171 self.__add_button.set_sensitive(False)
172 path = model.get_path(it)
173 self.__shortcut_view.set_cursor(path, None, False)
174
175 def __get_shortcut_from_buttons(self):
176 modifiers = []
177 keycode = self.__keycode_entry.get_text()
178 if Gdk.keyval_from_name(keycode) == 0:
179 return None
180
181 for name, button, mask in self.__modifier_buttons:
182 if button.get_active():
183 modifiers.append(name)
184 if keycode.startswith("_"):
185 keycode = keycode[1:]
186 shortcut = "".join(['<' + m + '>' for m in modifiers])
187 shortcut += keycode
188 return shortcut
189
190 def __set_shortcut_to_buttons(self, shortcut):
191 (keyval, state) = Gtk.accelerator_parse(shortcut)
192 if keyval == 0 and state == 0:
193 return
194 for name, button, mask in self.__modifier_buttons:
195 if state & mask:
196 button.set_active(True)
197 else:
198 button.set_active(False)
199 self.__keycode_entry.set_text(shortcut.rsplit('>', 1)[-1])
200
201 def __get_selected_shortcut(self):
202 model = self.__shortcut_view.get_model()
203 path, column = self.__shortcut_view.get_cursor()
204 if path == None:
205 return None
206 return model[path.get_indices()[0]][0]
207
208 def __set_selected_shortcut(self, shortcut):
209 model = self.__shortcut_view.get_model()
210 path, column = self.__shortcut_view.get_cursor()
211 model[path[0]][0] = shortcut
212 self.__update_add_and_apply_buttons()
213
214 def __del_selected_shortcut(self):
215 model = self.__shortcut_view.get_model()
216 path, column = self.__shortcut_view.get_cursor()
217 model.remove(model.get_iter(path))
218 self.__update_add_and_apply_buttons()
219
220 def __shortcut_view_cursor_changed_cb(self, treeview):
221 shortcut = self.__get_selected_shortcut()
222 if shortcut != None:
223 self.__set_shortcut_to_buttons(shortcut)
224 self.__delete_button.set_sensitive(True)
225 else:
226 self.__delete_button.set_sensitive(False)
227
228 def __update_add_and_apply_buttons(self):
229 shortcut = self.__get_shortcut_from_buttons()
230 selected_shortcut = self.__get_selected_shortcut()
231 shortcuts = self.get_shortcuts()
232 can_add = shortcut != None and \
233 shortcut not in shortcuts \
234 and len(shortcuts) < MAX_HOTKEY
235 self.__add_button.set_sensitive(can_add)
236 can_apply = shortcut != selected_shortcut and \
237 shortcut != None and \
238 selected_shortcut != None and \
239 shortcut not in shortcuts
240 self.__apply_button.set_sensitive(can_apply)
241
242 def __modifier_button_toggled_cb(self, button, name):
243 self.__update_add_and_apply_buttons()
244
245 def __keycode_entry_notify_cb(self, entry, arg):
246 self.__update_add_and_apply_buttons()
247
248 def __keycode_button_clicked_cb(self, button):
249 out = []
250 dlg = Gtk.MessageDialog(transient_for = self.get_toplevel(),
251 buttons = Gtk.ButtonsType.CLOSE)
252 message = _("Please press a key (or a key combination).\n" \
253 "The dialog will be closed when the key is released.")
254 dlg.set_markup(message)
255 dlg.set_title(_("Please press a key (or a key combination)"))
256 sw = Gtk.ScrolledWindow()
257
258 def __accel_edited_cb(c, path, keyval, state, keycode):
259 out.append(keyval)
260 out.append(state)
261 out.append(keycode)
262 dlg.response(Gtk.ResponseType.OK)
263
264 model = Gtk.ListStore(GObject.TYPE_INT,
265 GObject.TYPE_UINT,
266 GObject.TYPE_UINT)
267 accel_view = Gtk.TreeView(model = model)
268 accel_view.set_headers_visible(False)
269 sw.add(accel_view)
270 sw.set_min_content_height(30)
271 column = Gtk.TreeViewColumn()
272 renderer = Gtk.CellRendererAccel(accel_mode=Gtk.CellRendererAccelMode.OTHER,
273 editable=True)
274 renderer.connect('accel-edited', __accel_edited_cb)
275 column.pack_start(renderer, True)
276 column.add_attribute(renderer, 'accel-mods', 0)
277 column.add_attribute(renderer, 'accel-key', 1)
278 column.add_attribute(renderer, 'keycode', 2)
279 accel_view.append_column(column)
280 it = model.append(None)
281 area = dlg.get_message_area()
282 area.pack_end(sw, True, True, 0)
283 sw.show_all()
284 id = dlg.run()
285 dlg.destroy()
286 if id != Gtk.ResponseType.OK or len(out) < 3:
287 return
288 keyval = out[0]
289 state = out[1]
290 keycode = out[2]
291
292 for name, button, mask in self.__modifier_buttons:
293 if state & mask:
294 button.set_active(True)
295 else:
296 button.set_active(False)
297
298 shortcut = Gtk.accelerator_name_with_keycode(None,
299 keyval,
300 keycode,
301 state)
302 shortcut = shortcut.replace('<Primary>', '<Control>')
303 self.__keycode_entry.set_text(shortcut.rsplit('>', 1)[-1])
304
305 def __add_button_clicked_cb(self, button):
306 shortcut = self.__get_shortcut_from_buttons()
307 self.add_shortcut(shortcut)
308
309 def __apply_button_clicked_cb(self, button):
310 shortcut = self.__get_shortcut_from_buttons()
311 self.__set_selected_shortcut(shortcut)
312
313 def __delete_button_clicked_cb(self, button):
314 self.__del_selected_shortcut()
315 self.__delete_button.set_sensitive(False)
316 self.__apply_button.set_sensitive(False)
317
318 class KeyboardShortcutSelectionDialog(Gtk.Dialog):
319 def __init__(self, title = None, transient_for = None, flags = 0):
320 super(KeyboardShortcutSelectionDialog, self).__init__(
321 title = title, transient_for = transient_for, flags = flags)
322 self.__selection_view = KeyboardShortcutSelection()
323 self.vbox.pack_start(self.__selection_view, False, True, 0)
324 self.vbox.show_all()
325
326 def set_shortcuts(self, shotrcuts = None):
327 self.__selection_view.set_shortcuts(shotrcuts)
328
329 def add_shortcut(self, shotrcut):
330 self.__selection_view.add_shortcut(shotrcut)
331
332 def get_shortcuts(self):
333 return self.__selection_view.get_shortcuts()
334
335
336
337 if __name__ == "__main__":
338 dlg = KeyboardShortcutSelectionDialog(title = "Select test")
339 buttons = (_("_Cancel"), Gtk.ResponseType.CANCEL,
340 _("_OK"), Gtk.ResponseType.OK)
341 dlg.add_buttons(buttons)
342 dlg.add_shortcut("Control+Shift+space")
343 dlg.set_shortcuts(None)
344 print((dlg.run()))
345 print((dlg.get_shortcuts()))
346