def __init__(self): """__init__""" self.widget = Gtk.ScrolledWindow() self.table = Gtk.Table(1, 2) self.table.set_row_spacings(5) self.widget.add_with_viewport(self.table) self.widget.set_policy(NEVER, AUTOMATIC)
def _create_cqk_hbox(self, cqk_view): """_create_cqk_hbox :param cqk_view: """ cqk_hbox = Gtk.HBox() cqk_label = Gtk.Label(label=_("chainQuitKey:")) cqk_label.set_padding(5, 5) cqk_frame = Gtk.Frame() cqk_frame.add(cqk_view) cqk_hbox.pack_start(cqk_label, False, False, False) cqk_hbox.pack_start(cqk_frame, True, True, 0) return cqk_hbox
def _create_models(self): """_create_models""" model = Gtk.TreeStore( TYPE_UINT, # accel key TYPE_INT, # accel mods TYPE_STRING, # accel string (openbox) TYPE_BOOLEAN, # chroot TYPE_BOOLEAN, # show chroot TYPE_PYOBJECT, # OBKeyBind TYPE_STRING # keybind descriptor ) cqk_model = Gtk.ListStore( TYPE_UINT, # accel key TYPE_INT, # accel mods TYPE_STRING) # accel string (openbox) return (model, cqk_model)
def key_gtk2openbox(key, mods): """key_gtk2openbox :param key: :param mods: """ result = "" if mods: modtable = Gtk.accelerator_name(0, mods) modtable = [ REPLACE_TABLE_GTK2OPENBOX[i] for i in modtable[1:-1].split('><') ] result = '-'.join(modtable) if key: keychar = Gtk.accelerator_name(key, 0) if result != "": result += '-' result += keychar return result
def _create_scroll(self, view): """_create_scroll :param view: """ scroll = Gtk.ScrolledWindow() scroll.add(view) scroll.set_policy(AUTOMATIC, AUTOMATIC) scroll.set_shadow_type(Gtk.ShadowType.IN) return scroll
def add_row(self, label_text, table): """add_row :param label_text: :param table: """ label = Gtk.Label(label=_(label_text)) label.set_alignment(0, 0) row = self.table.props.n_rows self.table.attach(label, 0, 1, row, row + 1, EXPAND | FILL, 0, 5, 0) self.table.attach(table, 1, 2, row, row + 1, FILL, 0, 5, 0)
def key_gtk2openbox(key, mods): """key_gtk2openbox :param key: :param mods: """ result = "" if mods: modtable = Gtk.accelerator_name(0, mods) modtable = [ REPLACE_TABLE_GTK2OPENBOX[i] for i in modtable[1:-1].split('><') ] result = '-'.join(modtable) if key: keychar = Gtk.accelerator_name(key, 0) if result != "": result += '-' result += keychar return result
def key_openbox2gtk(obstr): """key_openbox2gtk :param obstr: """ if obstr is not None: toks = obstr.split("-") try: toksgdk = [REPLACE_TABLE_OPENBOX2GTK[mod.lower()] for mod in toks[:-1]] except KeyError: return (0, 0) toksgdk.append(toks[-1]) return Gtk.accelerator_parse("".join(toksgdk))
def key_openbox2gtk(obstr): """key_openbox2gtk :param obstr: """ if obstr is not None: toks = obstr.split("-") try: toksgdk = [REPLACE_TABLE_OPENBOX2GTK[mod.lower()] for mod in toks[:-1]] except BufferError: return (0, 0) toksgdk.append(toks[-1]) return Gtk.accelerator_parse("".join(toksgdk))
def _create_context_menu(self): """_create_context_menu""" context_menu = Gtk.Menu() self.context_items = {} item = Gtk.ImageMenuItem(Gtk.STOCK_CUT) item.connect('activate', lambda menu: self.cut_selected()) item.get_child().set_label(_("Cut")) context_menu.append(item) self.sw_selection_available.append(item) item = Gtk.ImageMenuItem(Gtk.STOCK_COPY) item.connect('activate', lambda menu: self.copy_selected()) item.get_child().set_label(_("Copy")) context_menu.append(item) self.sw_selection_available.append(item) item = Gtk.ImageMenuItem(Gtk.STOCK_PASTE) item.connect('activate', lambda menu: self.insert_sibling( copy.deepcopy(self.copied))) item.get_child().set_label(_("Paste")) context_menu.append(item) self.sw_paste_buffer.append(item) item = Gtk.ImageMenuItem(Gtk.STOCK_PASTE) item.get_child().set_label(_("Paste as child")) item.connect('activate', lambda menu: self.insert_child( copy.deepcopy(self.copied))) context_menu.append(item) self.sw_insert_child_and_paste.append(item) item = Gtk.ImageMenuItem(Gtk.STOCK_REMOVE) item.connect('activate', lambda menu: self.del_selected()) item.get_child().set_label(_("Remove")) context_menu.append(item) self.sw_selection_available.append(item) context_menu.show_all() return context_menu
def quit(b): Gtk.main_quit()
def _create_toolbar(self): """_create_toolbar""" agr = Gtk.AccelGroup() self.window.add_accel_group(agr) toolbar = Gtk.Toolbar() toolbar.set_style(Gtk.ToolbarStyle.ICONS) toolbar.set_show_arrow(False) def duplicate(b): self.duplicate_selected() but = Gtk.ToolButton(Gtk.STOCK_DND_MULTIPLE) but.set_tooltip_text(_("Duplicate sibling keybind")+" (Ctrl + d)") but.connect('clicked', duplicate) key, mod = Gtk.accelerator_parse("<Control>d") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) def copy(b): self.copy_selected() but = Gtk.ToolButton(Gtk.STOCK_COPY) but.set_tooltip_text(_("Copy")+" (Ctrl + c)") but.connect('clicked', copy) key, mod = Gtk.accelerator_parse("<Control>c") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) toolbar.insert(Gtk.SeparatorToolItem(), -1) def paste(b): self.insert_sibling( copy.deepcopy(self.copied)) but = Gtk.ToolButton(Gtk.STOCK_PASTE) but.set_tooltip_text(_("Paste")+" (Ctrl + v)") but.connect('clicked',paste) key, mod = Gtk.accelerator_parse("<Control>v") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) def add_sibling(b): self.insert_sibling(OBKeyBind()) but = Gtk.ToolButton() but.set_icon_widget(self.icons['add_sibling']) but.set_tooltip_text(_("Insert sibling keybind")+" (Ctrl + a)") but.connect('clicked',add_sibling) key, mod = Gtk.accelerator_parse("<Control>a") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) toolbar.insert(Gtk.SeparatorToolItem(), -1) def add_child(b): self.insert_child(OBKeyBind()) but = Gtk.ToolButton() but.set_icon_widget(self.icons['add_child']) but.set_tooltip_text(_("Insert child keybind")+" (Ctrl + i)") but.connect('clicked',add_child) key, mod = Gtk.accelerator_parse("<Control>i") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) def paste_as_child(b): self.insert_child(copy.deepcopy(self.copied)) but = Gtk.ToolButton(Gtk.STOCK_PASTE) but.set_tooltip_text(_("Paste as child")+" (Ctrl + V)") but.connect('clicked', paste_as_child) key, mod = Gtk.accelerator_parse("<Control><Shift>v") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) toolbar.insert(Gtk.SeparatorToolItem(), -1) def remove(b): self.del_selected() but = Gtk.ToolButton(Gtk.STOCK_REMOVE) but.set_tooltip_text(_("Remove keybind")+" (Delete)") but.connect('clicked',remove) toolbar.insert(but, -1) key, mod = Gtk.accelerator_parse("Delete") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) self.sw_selection_available.append(but) sep = Gtk.SeparatorToolItem() sep.set_draw(False) sep.set_expand(True) toolbar.insert(sep, -1) toolbar.insert(Gtk.SeparatorToolItem(), -1) def reset(b): for p in self.deleted_items_paths + self.changed_items_paths: self._refresh_row(p) self.deleted_items_paths = list() self.changed_items_paths = list() self.ob.load() if self.ob.keyboard_node: self.keyboard = OBKeyboard(self.ob.keyboard_node) for kb in self.keyboard.keybinds: self.apply_keybind(kb) but = Gtk.ToolButton(Gtk.STOCK_UNDO) but.set_tooltip_text(_("Undo everything")+" (Ctrl + z)") but.connect('clicked',reset) key, mod = Gtk.accelerator_parse("<Control>z") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) def save(b): self.apply_delete() self.ob.save(self.keyboard.deparse()) self.changed_items_paths=list([]) but = Gtk.ToolButton(Gtk.STOCK_SAVE) but.set_tooltip_text(_("Save ") + self.ob.path + _(" file") + " (Ctrl + s)") but.connect('clicked', save) key, mod = Gtk.accelerator_parse("<Control>s") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) def quit(b): Gtk.main_quit() but = Gtk.ToolButton(Gtk.STOCK_QUIT) but.set_tooltip_text(_("Quit application") + " (Ctrl + q)") but.connect('clicked', quit) key, mod = Gtk.accelerator_parse("<Control>q") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) return toolbar
def __init__(self, actionlist, obconfig, window): """__init__ :param actionlist: :param ob: """ self.window = window self.widget = Gtk.VBox() self.ob = obconfig self.changed_items_paths = list() self.deleted_items_paths = list() self.deleted_items_paths_lvl = dict() self.colors = { 'deleting':[ "#FF5633", "#FD4727", "#EC361B", "#DC220D", "#CC0000", "#BC0000", "#AC0000", "#9D0000", "#8D0000" ], 'changed':"#aaaaff" } if obconfig.keyboard_node: self.keyboard = OBKeyboard(obconfig.keyboard_node) self.actionlist = actionlist actionlist.set_callback(self.actions_cb) self.icons = self.load_icons() self.model, self.cqk_model = self._create_models() self.view, self.cqk_view = self._create_views( self.model, self.cqk_model) # copy & paste self.copied = None # sensitivity switchers & conditions self.cond_insert_child = SensCondition(False) self.cond_paste_buffer = SensCondition(False) self.cond_selection_available = SensCondition(False) self.sw_insert_child_and_paste = SensSwitcher( [self.cond_insert_child, self.cond_paste_buffer]) self.sw_insert_child = SensSwitcher( [self.cond_insert_child]) self.sw_paste_buffer = SensSwitcher( [self.cond_paste_buffer]) self.sw_selection_available = SensSwitcher( [self.cond_selection_available]) self.context_menu = self._create_context_menu() for kb in self.keyboard.keybinds: self.apply_keybind(kb) self.apply_cqk_initial_value() # self.add_child_button self.widget.pack_start(self._create_toolbar(), False, True, 0) self.widget.pack_start(self._create_scroll(self.view), True, True, 0) self.widget.pack_start(self._create_cqk_hbox(self.cqk_view), False, True, 0) if len(self.model): self.view.get_selection().select_iter(self.model.get_iter_first()) self.sw_insert_child_and_paste.notify() self.sw_insert_child.notify() self.sw_paste_buffer.notify() self.sw_selection_available.notify()
def _create_views(self, model, cqk_model): """_create_views :param model: :param cqk_model: """ # added accel_mode=1 (CELL_RENDERER_ACCEL_MODE_OTHER) for key "Tab" r0 = Gtk.CellRendererAccel(accel_mode=1) r0.props.editable = True r0.connect('accel-edited', self.accel_edited) r1 = Gtk.CellRendererText() r1.props.editable = True r1.connect('edited', self.key_edited) r3 = Gtk.CellRendererText() r3.props.editable = False r2 = Gtk.CellRendererToggle() r2.connect('toggled', self.chroot_toggled) c0 = Gtk.TreeViewColumn(_("Key"), r0, accel_key=0, accel_mods=1) c1 = Gtk.TreeViewColumn(_("Key (text)"), r1, text=2) c2 = Gtk.TreeViewColumn(_("Chroot"), r2, active=3, visible=4) c3 = Gtk.TreeViewColumn(_("Action"), r3, text=6) c0.set_cell_data_func(r0, self._cell_func) c1.set_cell_data_func(r1, self._cell_func) c2.set_cell_data_func(r2, self._cell_func) c3.set_cell_data_func(r3, self._cell_func) c0.set_resizable(True) c1.set_resizable(True) c2.set_resizable(True) c3.set_resizable(True) c0.set_fixed_width(200) c1.set_fixed_width(200) c2.set_fixed_width(100) c3.set_fixed_width(200) # the action column is the most important one, # so make it get the extra available space c3.set_expand(True) # SORT def compare(model, row1, row2, user_data): sort_column, _ = model.get_sort_column_id() value1 = model.get_value(row1, sort_column) value2 = model.get_value(row2, sort_column) if value1 < value2: return -1 elif value1 == value2: return 0 else: return 1 c3.set_sort_column_id(6) model.set_sort_func(6, compare, None) c0.set_sort_column_id(2) c1.set_sort_column_id(2) model.set_sort_func(2, compare, None) c2.set_sort_column_id(4) # FILTER def match_func(model, column, query, iterr, data=None): value = model.get_value(iterr, 6) if query == "": return False else: query = query.lower() if query.startswith("+"): query = "\+" if query.startswith("*"): query = "\*" pattern = re.compile(".*"+query+".*") return not pattern.match(value.lower()) # ADD TO VIEW view = Gtk.TreeView(model) view.set_search_column(6) view.set_search_equal_func(match_func) view.append_column(c3) view.append_column(c0) view.append_column(c1) view.append_column(c2) view.get_selection().connect('changed', self.view_cursor_changed) view.get_selection().set_mode(2) # BROWSE view.connect('button-press-event', self.view_button_clicked) # view.expand_all() # chainQuitKey table (wtf hack) r0 = Gtk.CellRendererAccel() r0.props.editable = True r0.connect('accel-edited', self.cqk_accel_edited) r1 = Gtk.CellRendererText() r1.props.editable = True r1.connect('edited', self.cqk_key_edited) c0 = Gtk.TreeViewColumn("Key", r0, accel_key=0, accel_mods=1) c1 = Gtk.TreeViewColumn("Key (text)", r1, text=2) def cqk_view_focus_lost(view, event): view.get_selection().unselect_all() cqk_view = Gtk.TreeView(cqk_model) cqk_view.set_headers_visible(False) cqk_view.append_column(c0) cqk_view.append_column(c1) cqk_view.connect('focus-out-event', cqk_view_focus_lost) return (view, cqk_view)
def quit(b): Gtk.main_quit()
def _create_toolbar(self): """_create_toolbar""" agr = Gtk.AccelGroup() self.window.add_accel_group(agr) toolbar = Gtk.Toolbar() toolbar.set_style(Gtk.ToolbarStyle.ICONS) toolbar.set_show_arrow(False) def duplicate(b): self.duplicate_selected() but = Gtk.ToolButton(Gtk.STOCK_DND_MULTIPLE) but.set_tooltip_text(_("Duplicate sibling keybind")+" (Ctrl + d)") but.connect('clicked', duplicate) key, mod = Gtk.accelerator_parse("<Control>d") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) def copy(b): self.copy_selected() but = Gtk.ToolButton(Gtk.STOCK_COPY) but.set_tooltip_text(_("Copy")+" (Ctrl + c)") but.connect('clicked', copy) key, mod = Gtk.accelerator_parse("<Control>c") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) toolbar.insert(Gtk.SeparatorToolItem(), -1) def paste(b): self.insert_sibling( copy.deepcopy(self.copied)) but = Gtk.ToolButton(Gtk.STOCK_PASTE) but.set_tooltip_text(_("Paste")+" (Ctrl + v)") but.connect('clicked',paste) key, mod = Gtk.accelerator_parse("<Control>v") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) def add_sibling(b): self.insert_sibling(OBKeyBind()) but = Gtk.ToolButton() but.set_icon_widget(self.icons['add_sibling']) but.set_tooltip_text(_("Insert sibling keybind")+" (Ctrl + a)") but.connect('clicked',add_sibling) key, mod = Gtk.accelerator_parse("<Control>a") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) toolbar.insert(Gtk.SeparatorToolItem(), -1) def add_child(b): self.insert_child(OBKeyBind()) but = Gtk.ToolButton() but.set_icon_widget(self.icons['add_child']) but.set_tooltip_text(_("Insert child keybind")+" (Ctrl + i)") but.connect('clicked',add_child) key, mod = Gtk.accelerator_parse("<Control>i") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) def paste_as_child(b): self.insert_child(copy.deepcopy(self.copied)) but = Gtk.ToolButton(Gtk.STOCK_PASTE) but.set_tooltip_text(_("Paste as child")+" (Ctrl + V)") but.connect('clicked', paste_as_child) key, mod = Gtk.accelerator_parse("<Control><Shift>v") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) toolbar.insert(Gtk.SeparatorToolItem(), -1) def remove(b): self.del_selected() but = Gtk.ToolButton(Gtk.STOCK_REMOVE) but.set_tooltip_text(_("Remove keybind")+" (Delete)") but.connect('clicked',remove) toolbar.insert(but, -1) key, mod = Gtk.accelerator_parse("Delete") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) self.sw_selection_available.append(but) sep = Gtk.SeparatorToolItem() sep.set_draw(False) sep.set_expand(True) toolbar.insert(sep, -1) toolbar.insert(Gtk.SeparatorToolItem(), -1) def reset(b): for p in self.deleted_items_paths + self.changed_items_paths: self._refresh_row(p) self.deleted_items_paths = list() self.changed_items_paths = list() self.ob.load() if self.ob.keyboard_node: self.keyboard = OBKeyboard(self.ob.keyboard_node) for kb in self.keyboard.keybinds: self.apply_keybind(kb) but = Gtk.ToolButton(Gtk.STOCK_UNDO) but.set_tooltip_text(_("Undo everything")+" (Ctrl + z)") but.connect('clicked',reset) key, mod = Gtk.accelerator_parse("<Control>z") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) def save(b): self.apply_delete() self.ob.save(self.keyboard.deparse()) self.changed_items_paths=list([]) but = Gtk.ToolButton(Gtk.STOCK_SAVE) but.set_tooltip_text(_("Save ") + self.ob.path + _(" file") + " (Ctrl + s)") but.connect('clicked', save) key, mod = Gtk.accelerator_parse("<Control>s") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) def quit(b): Gtk.main_quit() but = Gtk.ToolButton(Gtk.STOCK_QUIT) but.set_tooltip_text(_("Quit application") + " (Ctrl + q)") but.connect('clicked', quit) key, mod = Gtk.accelerator_parse("<Control>q") but.add_accelerator("clicked", agr, key, mod, Gtk.AccelFlags.VISIBLE) toolbar.insert(but, -1) return toolbar