def toolbar(name, group=None, callable=None): """ Returns the action with the given name for toolbar use. Toolbar use means that there is an icon without text. Needed configuration keys: icon, action Ignored keys: text Parameters: name -- The name of the action. group -- The QActionGroup to add this action to. callable -- The callable to call when the action is triggered. Can be used to override the action in the definition or provide one. """ global toolbar_actions action = toolbar_actions.get(name, None) if action is None: # Create the toolbar action and insert it into the toolbar_actions dict global parent global action_definitions config = action_definitions.get(name, None) if config is None: return None #Without the config no action can be created if group is None: action = KAction(parent) else: action = KAction(group) # Will result in a KeyError when the icon is not set in the config. # This is slightly wanted behaviour, as the icon is needed for a # toolbar action. action.setIcon(KIcon(config["icon"])) if "checkable" in config: action.setCheckable(True) # Same exception is wanted here. As an alternative one can specify a callable as a parameter. if callable is not None: action.triggered.connect(callable) else: action.triggered.connect(config["action"]) if "text" in config: action.setText(config["text"]) toolbar_actions["name"] = action return action
def kaction(name, actionCollection=None, callable=None): # Create the toolbar action and insert it into the toolbar_actions dict global parent global action_definitions global action_groups config = action_definitions.get(name, None) if config is None: print "ERR: No config for action:", name return None #Without the config no action can be created action = KAction(parent) if "group" in config: group_name = config["group"] if group_name in action_groups: group = action_groups[group_name] else: group = QtGui.QActionGroup(parent) action_groups[group_name] = group action.setActionGroup(group) if "icon" in config: action.setIcon(KIcon(config["icon"])) if "checkable" in config: action.setCheckable(True) # Same exception is wanted here. As an alternative one can specify a callable as a parameter. if callable is not None: action.triggered.connect(callable) else: action.triggered.connect(config["action"]) if "text" in config: action.setText(config["text"]) if "tooltip" in config: action.setToolTip(config["tooltip"]) if "whatsthis" in config: action.setWhatsThis(config["whatsthis"]) if actionCollection is not None: actionCollection.addAction(name, action) return action
def addHotKey(self, name, key): if not KGlobalAccel.isGlobalShortcutAvailable(key): actions = KGlobalAccel.getGlobalShortcutsByKey(key) if KGlobalAccel.promptStealShortcutSystemwide(None, actions, key): KGlobalAccel.stealShortcutSystemwide(key) action = KAction(None) action.setObjectName(name) action.setText(name) action.setGlobalShortcut(KShortcut(key), \ KAction.ShortcutType(KAction.ActiveShortcut | KAction.DefaultShortcut), KAction.NoAutoloading) action.triggered.connect(self.catchGlobalKey) self.actions[self.nextId] = action self.nextId += 1 return self.nextId - 1
def menu(name): """ Returns the action with the given name for menu use. Menu use that the action has a text and might have an icon. Needed configuration keys: text, action Optional keys: icon Parameters: name -- The name of the action, """ global menu_actions action = menu_actions.get(name, None) if action is None: # Create the toolbar action and insert it into the toolbar_actions dict global parent global action_definitions config = action_definitions.get(name, None) if config is None: return None #Without the config no action can be created action = KAction(parent) if "icon" in config: action.setIcon(KIcon(config["icon"])) if "checkable" in config: action.setCheckable(True) # Will result in a KeyError when the text is not set in the config. # This is slightly wanted behaviour, as the text is needed for a # menu action. action.setText(config["text"]) # Same exception is wanted here. action.triggered.connect(config["action"]) menu_actions["name"] = action return action
def setupActions(self): """Sets up all actions (signal/slot combinations).""" # standard action KStandardAction.quit(g_app.quit, self.actionCollection()) # dictionary actions self.dictionaryPage.registerGlobalActions(self.actionCollection()) # update dictionaries if self.updateDialog: self.updateAction = self.updateDialog.updateAction( self.actionCollection()) # optimise database self.updateDialog.optimiseAction(self.actionCollection()) else: self.updateAction = None # search bar self.characterCombo = KHistoryComboBox() self.characterCombo.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) font = QFont() font.setPointSize(13) self.characterCombo.setFont(font) self.characterCombo.setObjectName("characterCombo") self.connect(self.characterCombo, SIGNAL("activated(const QString &)"), self.slotCharacterComboActivated) comboAction = KAction(self) comboAction.setText(i18n("Search bar")) comboAction.setShortcut(Qt.Key_F6) self.connect(comboAction, SIGNAL("triggered()"), self.slotSearchComboActivated) comboAction.setDefaultWidget(self.characterCombo) comboAction.setWhatsThis( i18n("<html>Search bar<br/><br/>Enter character of search string</html>")) self.actionCollection().addAction("searchbar", comboAction) goUrl = self.actionCollection().addAction("go_search") goUrl.setIcon(KIcon("go-jump-locationbar")) goUrl.setText(i18n("Go")) self.connect(goUrl, SIGNAL("triggered()"), lambda: self.slotCharacterComboActivated( self.characterCombo.currentText())) goUrl.setWhatsThis( i18n("<html>Go<br /><br />Searches for the string given in the search bar.</html>")) # clear search bar action clearLocationAction = KAction(KIcon("edit-clear-locationbar-ltr"), i18n("Clear &Location Bar"), self) clearLocationAction.setShortcut(Qt.CTRL + Qt.Key_L) clearLocationAction.setWhatsThis( i18n("Clears the location bar and places the cursor inside")) self.actionCollection().addAction("clearlocationbar", clearLocationAction) self.connect(clearLocationAction, SIGNAL("triggered(bool)"), self.characterCombo.clearEditText) self.connect(clearLocationAction, SIGNAL("triggered(bool)"), self.characterCombo.setFocus) # show/hide character page self.toggleToolboxAction = KToggleAction(KIcon("view-sidetree"), i18n("Show Character Toolbox"), self) self.toggleToolboxAction.setShortcut(Qt.Key_F9) self.toggleToolboxAction.setWhatsThis( i18n("Shows and Hides the character choosing toolbox")) self.actionCollection().addAction("showtoolbox", self.toggleToolboxAction) self.connect(self.toggleToolboxAction, SIGNAL("triggered(bool)"), self.slotToggleToolbox) # auto-lookup clipboard self.autoLookupAction = KToggleAction(i18n("&Auto-Lookup"), self) self.autoLookupAction.setToolTip( i18n("Automatically look up text selected by the mouse cursor.")) self.autoLookupAction.setWhatsThis( i18n("Automatically look up text selected by the mouse cursor.")) self.actionCollection().addAction("autolookup", self.autoLookupAction) self.connect(self.autoLookupAction, SIGNAL("triggered(bool)"), self.setAutoLookup) self.autoLookupAction.setIcon( QIcon(util.getIcon('auto-lookup-selection.png'))) self.connect(QApplication.clipboard(), SIGNAL("selectionChanged()"), self.slotSelectionChanged)