def create_toolbutton(parent, settings): """Create a QToolButton""" button = QtGui.QToolButton(parent) text = settings["text"] if "text" in settings else "No name" button.setObjectName(text2objectname(text, prefix = "toolButton")) button.setText(text) # attrs if "icon" in settings: button.setIcon(settings["icon"]) if "shortcut" in settings: button.setShortcut(settings["shortcut"]) if "tip" in settings: button.setToolTip(settings["tip"]) if settings.get("text_beside_icon", False): button.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon) button.setAutoRaise(settings.get("autoraise", True)) if "triggered" in settings and isinstance(settings["triggered"], collections.Callable): parent.connect(button, QtCore.SIGNAL("clicked()"), settings["triggered"]) if "clicked" in settings and isinstance(settings["clicked"], collections.Callable): parent.connect(button, QtCore.SIGNAL("clicked()"), settings["clicked"]) if "toggled" in settings and isinstance(settings["toggled"], collections.Callable): parent.connect(button, QtCore.SIGNAL("toggled(bool)"), settings["toggled"]) button.setCheckable(True) return button
def create_toolbutton(parent, settings): """Create a QToolButton""" button = QtGui.QToolButton(parent) text = settings["text"] if settings.has_key("text") else "No name" button.setObjectName(text2objectname(text, prefix = "toolButton")) button.setText(text) # attrs if settings.has_key("icon"): icon = settings["icon"] if isinstance(icon, basestring): icon = resources.getIcon(icon) button.setIcon(icon) if settings.has_key("shortcut"): button.setShortcut(settings["shortcut"]) if settings.has_key("tip"): button.setToolTip(settings["tip"]) if settings.get("text_beside_icon", False): button.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon) button.setAutoRaise(settings.get("autoraise", True)) if settings.has_key("triggered") and callable(settings["triggered"]): parent.connect(button, QtCore.SIGNAL("clicked()"), settings["triggered"]) if settings.has_key("clicked") and callable(settings["clicked"]): parent.connect(button, QtCore.SIGNAL("clicked()"), settings["clicked"]) if settings.has_key("toggled") and callable(settings["toggled"]): parent.connect(button, QtCore.SIGNAL("toggled(bool)"), settings["toggled"]) button.setCheckable(True) return button
def create_menu(parent, settings, useSeparatorName=False, connectActions=False): text = settings.get("text", "Menu") menu = QtGui.QMenu(text, parent) menu.setObjectName(text2objectname(text, prefix="menu")) # attrs if settings.has_key("icon"): icon = settings["icon"] if isinstance(icon, basestring): icon = resources.getIcon(icon) menu.setIcon(icon) # actions actions = extend_menu(menu, settings.get("items", []), useSeparatorName) if connectActions: for action in actions: if hasattr(action, 'callback'): if action.isCheckable(): parent.connect(action, QtCore.SIGNAL('triggered(bool)'), action.callback) else: parent.connect(action, QtCore.SIGNAL('triggered()'), action.callback) return menu, actions
def create_action(parent, settings): """Create a QAction""" text = settings.get("text", "Action") action = QtGui.QAction(text, parent) action.setObjectName(text2objectname(text, prefix="action")) # attrs if settings.has_key("icon"): icon = settings["icon"] if isinstance(icon, basestring): icon = resources.getIcon(icon) action.setIcon(icon) if settings.has_key("shortcut"): action.setShortcut(settings["shortcut"]) if settings.has_key("tip"): action.setToolTip(settings["tip"]) action.setStatusTip(settings["tip"]) if settings.has_key("data"): action.setData(settings["data"]) if settings.has_key("menurole"): action.setMenuRole(settings["menurole"]) if settings.has_key("checkable"): action.setCheckable(settings["checkable"]) # callables if settings.has_key("callback"): action.callback = settings["callback"] if settings.has_key("testChecked"): action.testChecked = settings["testChecked"] if settings.has_key("triggered") and callable(settings["triggered"]): parent.connect(action, QtCore.SIGNAL("triggered()"), settings["triggered"]) if settings.has_key("toggled") and callable(settings["toggled"]): parent.connect(action, QtCore.SIGNAL("toggled(bool)"), settings["toggled"]) action.setCheckable(True) #TODO: Hard-code all shortcuts and choose context=QtCore.Qt.WidgetShortcut # (this will avoid calling shortcuts from another dockwidget # since the context thing doesn't work quite well with these widgets) action.setShortcutContext(settings.get("context", QtCore.Qt.WindowShortcut)) return action
def extend_menu(menu, items, useSeparatorName=False): actions = [] for item in items: if item == "-": action = menu.addSeparator() actions.append(action) elif isinstance(item, basestring) and item.startswith("--"): name = item[item.rfind("-") + 1:] action = menu.addSeparator() action.setObjectName(text2objectname(name, prefix="section")) if useSeparatorName: action.setText(name) actions.append(action) elif isinstance(item, dict) and 'items' in item: submenu, subactions = create_menu(menu, item) subaction = menu.addMenu(submenu) actions.append(subaction) actions.extend(subactions) elif isinstance(item, dict): action = create_action(menu, item) actions.append(action) menu.addAction(action) elif isinstance(item, QtGui.QAction): menu.addAction(item) elif isinstance(item, QtGui.QMenu): menu.addMenu(item) elif isinstance(item, list): actionGroup = QtGui.QActionGroup(menu) actions.append(actionGroup) actionGroup.setExclusive(False) map(menu.addAction, item) map(lambda action: action.setActionGroup(actionGroup), item) elif isinstance(item, tuple): actionGroup = QtGui.QActionGroup(menu) actions.append(actionGroup) actionGroup.setExclusive(True) map(menu.addAction, item) map(lambda action: action.setActionGroup(actionGroup), item) else: raise Exception("%s" % item) return actions
def extend_menu(menu, items, useSeparatorName = False): actions = [] for item in items: if item == "-": action = menu.addSeparator() actions.append(action) elif isinstance(item, basestring) and item.startswith("--"): name = item[item.rfind("-") + 1:] action = menu.addSeparator() action.setObjectName(text2objectname(name, prefix = "section")) if useSeparatorName: action.setText(name) actions.append(action) elif isinstance(item, dict) and 'items' in item: submenu, subactions = create_menu(menu, item) subaction = menu.addMenu(submenu) actions.append(subaction) actions.extend(subactions) elif isinstance(item, dict): action = create_action(menu, item) actions.append(action) menu.addAction(action) elif isinstance(item, QtGui.QAction): menu.addAction(item) elif isinstance(item, QtGui.QMenu): menu.addMenu(item) elif isinstance(item, list): actionGroup = QtGui.QActionGroup(menu) actions.append(actionGroup) actionGroup.setExclusive(False) map(menu.addAction, item) map(lambda action: action.setActionGroup(actionGroup), item) elif isinstance(item, tuple): actionGroup = QtGui.QActionGroup(menu) actions.append(actionGroup) actionGroup.setExclusive(True) map(menu.addAction, item) map(lambda action: action.setActionGroup(actionGroup), item) else: raise Exception("%s" % item) return actions
def create_action(parent, settings): """Create a QAction""" text = settings.get("text", "Action") action = QtGui.QAction(text, parent) action.setObjectName(text2objectname(text, prefix = "action")) # attrs if settings.has_key("icon"): icon = settings["icon"] if isinstance(icon, basestring): icon = resources.getIcon(icon) action.setIcon(icon) if settings.has_key("shortcut"): action.setShortcut(settings["shortcut"]) if settings.has_key("tip"): action.setToolTip(settings["tip"]) action.setStatusTip(settings["tip"]) if settings.has_key("data"): action.setData(settings["data"]) if settings.has_key("menurole"): action.setMenuRole(settings["menurole"]) if settings.has_key("checkable"): action.setCheckable(settings["checkable"]) # callables if settings.has_key("callback"): action.callback = settings["callback"] if settings.has_key("testChecked"): action.testChecked = settings["testChecked"] if settings.has_key("triggered") and callable(settings["triggered"]): parent.connect(action, QtCore.SIGNAL("triggered()"), settings["triggered"]) if settings.has_key("toggled") and callable(settings["toggled"]): parent.connect(action, QtCore.SIGNAL("toggled(bool)"), settings["toggled"]) action.setCheckable(True) #TODO: Hard-code all shortcuts and choose context=QtCore.Qt.WidgetShortcut # (this will avoid calling shortcuts from another dockwidget # since the context thing doesn't work quite well with these widgets) action.setShortcutContext(settings.get("context", QtCore.Qt.WindowShortcut)) return action
def create_menu(parent, settings, useSeparatorName = False, connectActions = False): text = settings.get("text", "Menu") menu = QtGui.QMenu(text, parent) menu.setObjectName(text2objectname(text, prefix = "menu")) # attrs if settings.has_key("icon"): icon = settings["icon"] if isinstance(icon, basestring): icon = resources.getIcon(icon) menu.setIcon(icon) # actions actions = extend_menu(menu, settings.get("items", []), useSeparatorName) if connectActions: for action in actions: if hasattr(action, 'callback'): if action.isCheckable(): parent.connect(action, QtCore.SIGNAL('triggered(bool)'), action.callback) else: parent.connect(action, QtCore.SIGNAL('triggered()'), action.callback) return menu, actions
def create_shortcut(parent, settings, dispatcher = None, sequence_handler=None): """Create a QAction""" shortcut = QtGui.QShortcut(parent) name = settings.get("name", "None") shortcut.setObjectName(text2objectname(name, prefix = "shortcut")) # attrs if "sequence" in settings: sequence = settings["sequence"] if sequence_handler is not None: sequence_handler(shortcut, sequence) elif isinstance(sequence, QtGui.QKeySequence): shortcut.setKey(sequence) if "context" in settings: shortcut.setContext(settings["context"]) # Action functions shortcut.functionActivated = None if "activated" in settings and isinstance(settings["activated"], collections.Callable): shortcut.functionActivated = settings["activated"] # The signal dispatcher def dispatch_signal(dispatcher, handler): def _dispatch(*largs): dispatcher(handler, *largs) return _dispatch if shortcut.functionActivated is not None: parent.connect(shortcut, QtCore.SIGNAL("activated()"), isinstance(dispatcher, collections.Callable) and \ dispatch_signal(dispatcher, shortcut.functionActivated) or \ shortcut.functionActivated) # Test functions if "testEnabled" in settings and isinstance(settings["testEnabled"], collections.Callable): shortcut.testEnabled = settings["testEnabled"] return shortcut