Beispiel #1
0
def create_toolbutton(parent, settings):
    """Create a QToolButton"""
    button = QtWidgets.QToolButton(parent)
    text = settings["text"] if "text" in settings else "No name"
    button.setObjectName(text_to_objectname(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
Beispiel #2
0
def add_actions(target, actions, before=None):
    """Add actions to a menu"""
    # Convert before to action
    before_action = None
    if before:
        objectName = text_to_objectname(before, prefix = "actionMenu")
        before_action = next((ta for ta in target.actions() if ta.objectName() == objectName), None)
    for action in actions:
        if isinstance(action, QtGui.QMenu):
            if before_action is None:
                target.addMenu(action)
            else:
                target.insertMenu(before_action, action)
        elif isinstance(action, QtGui.QAction):
            if before_action is None:
                target.addAction(action)
            else:
                target.insertAction(before_action, action)
Beispiel #3
0
def extend_menu(rootMenu, settings, dispatcher = None, separatorName = False, 
    sequence_handler = None, icon_resolver = None):
    collectedObjects = [ rootMenu ]
    for item in settings:
        objects = None
        if item == "-":
            objects = rootMenu.addSeparator()
            objects.setObjectName(text_to_objectname("None", prefix = "separator"))
        elif isinstance(item, six.string_types) and item.startswith("--"):
            name = item[item.rfind("-") + 1:]
            objects = rootMenu.addSeparator()
            objects.setObjectName(text_to_objectname(name, prefix = "separator"))
            if separatorName:
                objects.setText(name)
        elif isinstance(item, dict) and 'items' in item:
            objects = create_menu(rootMenu.parent(), item,
                dispatcher = dispatcher,
                separatorName = separatorName,
                allObjects = True,
                sequence_handler = sequence_handler, 
                icon_resolver = icon_resolver)
            add_actions(rootMenu, [ objects[0] ], item.get("before", None))
        elif isinstance(item, dict):
            objects = create_action(rootMenu.parent(), item,
                dispatcher = dispatcher, 
                sequence_handler = sequence_handler, 
                icon_resolver = icon_resolver)
            add_actions(rootMenu, [ objects ], item.get("before", None))
        elif isinstance(item, QtGui.QAction):
            rootMenu.addAction(item)
            objects = item
        elif isinstance(item, QtGui.QMenu):
            objects = rootMenu.addMenu(item)
        elif isinstance(item, (tuple, list)):
            actionGroup = QtGui.QActionGroup(rootMenu.parent())
            actionGroup.setExclusive(isinstance(item, tuple))
            objects = []
            for action in item:
                if isinstance(action, dict):
                    action = create_action(rootMenu.parent(), action,
                        dispatcher = dispatcher, 
                        sequence_handler = sequence_handler,
                        icon_resolver = icon_resolver)
                if action == "-":
                    action = rootMenu.addSeparator()
                    action.setObjectName(text_to_objectname("None", prefix = "separator"))
                elif isinstance(action, str) and action.startswith("--"):
                    name = action[action.rfind("-") + 1:]
                    action = rootMenu.addSeparator()
                    action.setObjectName(text_to_objectname(name, prefix = "separator"))
                    if separatorName:
                        action.setText(name)
                else:
                    rootMenu.addAction(action)
                    action.setActionGroup(actionGroup)
                objects.append(action)
        else:
            raise Exception("%s" % item)
        if objects is not None:
            getattr(collectedObjects, isinstance(objects, (tuple, list)) and "extend" or "append")(objects)
    return collectedObjects
Beispiel #4
0
def create_menu(parent, settings, dispatcher = None, separatorName = False, 
    allObjects = False, sequence_handler = None, icon_resolver = None):
    text = settings["text"]
    menu = QtGui.QMenu(text, parent)
    
    menu.setObjectName(text_to_objectname(text, prefix = "menu"))
    menu.menuAction().setObjectName(text_to_objectname(text, prefix = "actionMenu"))

    icon = settings.get("icon")
    if icon is None and icon_resolver is not None:
        icon = icon_resolver(text_to_iconname(text, prefix="icon"))
    if icon is not None:
        menu.setIcon(icon)
    
    # Action functions
    menu.functionTriggered = menu.functionAboutToHide = menu.functionAboutToShow = None
    if "triggered" in settings and isinstance(settings["triggered"], collections.Callable):
        menu.functionTriggered = settings["triggered"]
    if "aboutToHide" in settings and isinstance(settings["aboutToHide"], collections.Callable):
        menu.functionAboutToHide = settings["aboutToHide"]
    if "aboutToShow" in settings and isinstance(settings["aboutToShow"], collections.Callable):
        menu.functionAboutToShow = settings["aboutToShow"]

    # The signal dispatcher
    def dispatch_signal(dispatcher, handler):
        def _dispatch(*largs):
            dispatcher(handler, *largs)
        return _dispatch

    if menu.functionTriggered is not None:
        parent.connect(menu, QtCore.SIGNAL("triggered(QAction)"),
            isinstance(dispatcher, collections.Callable) and \
            dispatch_signal(dispatcher, menu.functionTriggered) or \
            menu.functionTriggered)

    if menu.functionAboutToHide is not None:
        parent.connect(menu, QtCore.SIGNAL("aboutToHide()"),
            isinstance(dispatcher, collections.Callable) and \
            dispatch_signal(dispatcher, menu.functionAboutToHide) or \
            menu.functionAboutToHide)

    if menu.functionAboutToShow is not None:
        parent.connect(menu, QtCore.SIGNAL("aboutToShow()"),
            isinstance(dispatcher, collections.Callable) and \
            dispatch_signal(dispatcher, menu.functionAboutToShow) or \
            menu.functionAboutToShow)

    # The signal dispatcher
    if "testEnabled" in settings:
        menu.testEnabled = settings["testEnabled"]
    if "testVisible" in settings:
        menu.testVisible = settings["testVisible"]

    objects = extend_menu(menu,
        settings.get("items", []),
        dispatcher = dispatcher,
        separatorName = separatorName,
        sequence_handler = sequence_handler, 
        icon_resolver = icon_resolver)

    return allObjects and objects or menu
Beispiel #5
0
def extend_menu(rootMenu, settings, dispatcher = None, separatorName = False, 
    sequence_handler = None, icon_handler = None):
    collectedObjects = []
    for item in settings:
        if item == "-":
            separator = rootMenu.addSeparator()
            separator.setObjectName(text_to_objectname("None", prefix = "separator"))
            collectedObjects.append(separator)
        elif isinstance(item, six.string_types) and item.startswith("--"):
            name = item[item.rfind("-") + 1:]
            separator = rootMenu.addSeparator()
            separator.setObjectName(text_to_objectname(name, prefix = "separator"))
            if separatorName:
                separator.setText(name)
            collectedObjects.append(separator)
        elif isinstance(item, dict) and 'items' in item:
            menu, objects = create_menu(rootMenu.parent(), item,
                dispatcher = dispatcher,
                separatorName = separatorName,
                sequence_handler = sequence_handler, 
                icon_handler = icon_handler)
            add_actions(rootMenu, [ menu ], item.get("before", None), prefix="actionMenu")
            collectedObjects.append(menu)
            collectedObjects.extend(objects)
        elif isinstance(item, dict):
            action = create_action(rootMenu.parent(), item,
                dispatcher = dispatcher, 
                sequence_handler = sequence_handler, 
                icon_handler = icon_handler)
            add_actions(rootMenu, [ action ], item.get("before", None), prefix="action")
            collectedObjects.append(action)
        elif isinstance(item, QtWidgets.QAction):
            rootMenu.addAction(item)
            collectedObjects.append(item)
        elif isinstance(item, QtWidgets.QMenu):
            rootMenu.addMenu(item)
            collectedObjects.append(item)
        elif isinstance(item, (tuple, list)):
            actionGroup = QtWidgets.QActionGroup(rootMenu.parent())
            actionGroup.setExclusive(isinstance(item, tuple))
            for action in item:
                if isinstance(action, dict):
                    action = create_action(rootMenu.parent(), action,
                        dispatcher = dispatcher, 
                        sequence_handler = sequence_handler,
                        icon_handler = icon_handler)
                if action == "-":
                    action = rootMenu.addSeparator()
                    action.setObjectName(text_to_objectname("None", prefix = "separator"))
                elif isinstance(action, str) and action.startswith("--"):
                    name = action[action.rfind("-") + 1:]
                    action = rootMenu.addSeparator()
                    action.setObjectName(text_to_objectname(name, prefix = "separator"))
                    if separatorName:
                        action.setText(name)
                else:
                    rootMenu.addAction(action)
                    action.setActionGroup(actionGroup)
                collectedObjects.append(action)
        else:
            raise Exception("%s" % item)
    return collectedObjects
Beispiel #6
0
def create_menu(parent, settings, dispatcher = None, separatorName = False,
    sequence_handler = None, icon_handler=None):
    text = settings["text"]
    menu = QtWidgets.QMenu(text, parent)
    
    menu.setObjectName(text_to_objectname(text, prefix = "menu"))
    menu.menuAction().setObjectName(text_to_objectname(text, prefix = "actionMenu"))

    icon = settings.get("icon", text_to_iconname(text))
    if icon and icon_handler is not None:
        icon_handler(menu, icon)
    elif isinstance(icon, QtGui.QIcon) and not icon.isNull():
        menu.setIcon(icon)
    
    # Action functions
    menu.functionTriggered = menu.functionAboutToHide = menu.functionAboutToShow = None
    if "triggered" in settings and isinstance(settings["triggered"], collections.Callable):
        menu.functionTriggered = settings["triggered"]
    if "aboutToHide" in settings and isinstance(settings["aboutToHide"], collections.Callable):
        menu.functionAboutToHide = settings["aboutToHide"]
    if "aboutToShow" in settings and isinstance(settings["aboutToShow"], collections.Callable):
        menu.functionAboutToShow = settings["aboutToShow"]

    # The signal dispatcher
    def dispatch_signal(dispatcher, handler):
        def _dispatch(*largs):
            dispatcher(handler, *largs)
        return _dispatch

    if menu.functionTriggered is not None:
        menu.triggered[QtWidgets.QAction].connect(
            isinstance(dispatcher, collections.Callable) and \
            dispatch_signal(dispatcher, menu.functionTriggered) or \
            menu.functionTriggered)

    if menu.functionAboutToHide is not None:
        menu.aboutToHide.connect(
            isinstance(dispatcher, collections.Callable) and \
            dispatch_signal(dispatcher, menu.functionAboutToHide) or \
            menu.functionAboutToHide)

    if menu.functionAboutToShow is not None:
        menu.aboutToShow.connect(
            isinstance(dispatcher, collections.Callable) and \
            dispatch_signal(dispatcher, menu.functionAboutToShow) or \
            menu.functionAboutToShow)

    # The signal dispatcher
    if "testEnabled" in settings:
        menu.testEnabled = settings["testEnabled"]
    if "testVisible" in settings:
        menu.testVisible = settings["testVisible"]

    objects = extend_menu(menu,
        settings.get("items", []),
        dispatcher = dispatcher,
        separatorName = separatorName,
        sequence_handler = sequence_handler, 
        icon_handler = icon_handler)

    return menu, objects