示例#1
0
文件: command.py 项目: nerdocs/MedUX
    def __init__(self, actionId):
        super().__init__(actionId)

        self._action = ProxyAction(self)
        self._active = False
        self._contextInitialized = False
        self._action.setShortcutVisibleInToolTip(True)
        self._action.changed.connect(self._updateActiveState)

        self._contextActionMap = {}      # QtC: QMap<Id, QPointer<QAction> >, here: Id -> QAction
        self._scriptableMap = {}         # QtC: QMap<QAction*, bool>
示例#2
0
    def testShortcut(self):
        a = QAction("Test Action", None)
        a.setToolTip("Test Tooltip")

        p = ProxyAction()
        p.setAttribute(ProxyActionAttribute.UpdateText)
        p.action = a
        p.setShortcut(QKeySequence("Ctrl+T"))
        self.assertEqual(p.text(), "Test Action")
        p.setShortcutVisibleInToolTip(True)
        self.assertEqual(p.toolTip(), "Test Tooltip <span style=\"color: gray; font-size: small\">Ctrl+T</span>")
        p.setShortcutVisibleInToolTip(False)
        self.assertEqual(p.toolTip(), "Test Tooltip")
示例#3
0
文件: command.py 项目: nerdocs/MedUX
class Action(AbstractCommand):
    def __init__(self, actionId):
        super().__init__(actionId)

        self._action = ProxyAction(self)
        self._active = False
        self._contextInitialized = False
        self._action.setShortcutVisibleInToolTip(True)
        self._action.changed.connect(self._updateActiveState)

        self._contextActionMap = {}      # QtC: QMap<Id, QPointer<QAction> >, here: Id -> QAction
        self._scriptableMap = {}         # QtC: QMap<QAction*, bool>


    def action(self):
        return self._action

    def setKeySequence(self, key: QKeySequence):
        super().setKeySequence(key)
        self._action.setShortcut(key)
        self.keySequenceChanged.emit()

    def keySequence(self):
        return self._action.shortcut()

    def setCurrentContext(self, context: Context):
        self._context = copy(context)

        currentAction = None
        for i in range(len(self.context)):
            a = self._contextActionMap.get(self._context[i], None)
            if a:
                currentAction = a
                break

        self._action.setAction(currentAction)
        self._updateActiveState()

    def shortcut(self):
        return None

    @pyqtSlot()
    def _updateActiveState(self):
        """Updates the state of the Action"""
        self._setActive(self._action.isEnabled() and self._action.isVisible() and not self._action.isSeparator())

    def __msgActionWarning(self, newAction: QAction, actionId: ItemId, oldAction: QAction or None):
        """
        :returns: a Warning text with the given Action's and ItemId's properties
        :param newAction: Action to take strings from
        :param  actionId: ItemId to take strings from
        """
        oldActionMsg = " {}/{}".format(oldAction.objectName(), oldAction.text()) if oldAction else ""
        msg = "addOverrideAction {}/{}: Action{} is already registered for context {} {}.".format(
            newAction.objectName(), newAction.text(), oldActionMsg, actionId.uniqueIdentifier, actionId.toSetting())
        return msg

    def addOverrideAction(self, action: QAction, context: Context, scriptable: bool):
        if HostOsInfo.isMacHost():
            action.setIconVisibleInMenu(False)
            if self.isEmpty():
                self._action.initialize(action)
            if context.isEmpty():
                self._contextActionMap[None] = action
            else:
                for i in range(len(context)):
                    cId = context[i]
                    if cId in self._contextActionMap:
                        warnings.warn(self.__msgActionWarning(action, cId, self._contextActionMap.get(cId, None)))
                    self._contextActionMap[cId] = action

            self._scriptableMap[action] = scriptable
            self.setCurrentContext(self._context)

    def removeOverrideAction(self, action: QAction):
        """Silently deletes all occurrences of :param: action or empty actions from internal list."""
        try:
            del self._contextActionMap[action]
            #FIXME: QtC here cleans all empty "dangling" QPointers (0) as well.
            # No way to do this in Python as there is no QPointer that keeps track of the object's life cycle
        except KeyError:
            pass

        self.setCurrentContext(self._context)

    def isActive(self) -> bool:
        return self._active

    def _setActive(self, state: bool):
        if bool(state) != self._active:
            self._active = bool(state)
            self.activeStateChanged.emit()

    def isEmpty(self):
        return len(self._contextActionMap) == 0

    def isScriptable(self, context: Context=None):
        if context is None:
            return True in self._scriptableMap.values()

        if context == self._context and self._action.action in self._scriptableMap:
            return self._scriptableMap[self._action.action]

        for i in range(len(context)):
            a = self._contextActionMap.get(i, 0)
            if a is not None:
                if a in self._scriptableMap and self._scriptableMap[a] is not None:
                    return True
        return False

    def setAttribute(self, attr: CommandAttribute):
        super().setAttribute(attr)
        if attr == CommandAttribute.CA_Hide:
            self._action.setAttribute(ProxyActionAttribute.Hide)
        elif attr == CommandAttribute.CA_UpdateText:
            self._action.setAttribute(ProxyActionAttribute.UpdateText)
        elif attr == CommandAttribute.CA_UpdateIcon:
            self._action.setAttribute(ProxyActionAttribute.UpdateIcon)
        elif attr == CommandAttribute.CA_NonConfigurable:
            pass

    def removeAttribute(self, attr: CommandAttribute):
        super().setAttribute(attr)
        if attr == CommandAttribute.CA_Hide:
            self._action.removeAttribute(ProxyActionAttribute.Hide)
        elif attr == CommandAttribute.CA_UpdateText:
            self._action.removeAttribute(ProxyActionAttribute.UpdateText)
        elif attr == CommandAttribute.CA_UpdateIcon:
            self._action.removeAttribute(ProxyActionAttribute.UpdateIcon)
        elif attr == CommandAttribute.CA_NonConfigurable:
            pass
示例#4
0
文件: command.py 项目: nerdocs/MedUX
 def stringWithAppendedShortcut(self, string: str) -> str:
     """Returns the **string** with an appended representation of the keyboard shortcut
     that is currently assigned to this Command."""
     return ProxyAction.stringWithAppendedShortcut(str, self.keySequence())