From 814fbfcfa45f3ad3fd4778760eca38e394365d2a Mon Sep 17 00:00:00 2001 From: David Vierra Date: Tue, 13 Jan 2015 15:38:41 -1000 Subject: [PATCH] More work on worldview keybinding editor - accepts keypresses including lone modifier keys --- src/mcedit2/worldview/viewcontrols.py | 151 ++++++++++++++++++++------ 1 file changed, 115 insertions(+), 36 deletions(-) diff --git a/src/mcedit2/worldview/viewcontrols.py b/src/mcedit2/worldview/viewcontrols.py index db1de02..6a1a4a7 100644 --- a/src/mcedit2/worldview/viewcontrols.py +++ b/src/mcedit2/worldview/viewcontrols.py @@ -21,58 +21,137 @@ def buttonName(buttons): parts = [name for mask, name in _buttonNames if buttons & mask] return "+".join(parts) -def modifierText(mouseAction): - return QtGui.QKeySequence(mouseAction.modifiers).toString() + buttonName(mouseAction.button) + + +def describeKeys(viewAction): + modifierKeyNames = { + Qt.Key_Shift: QtGui.qApp.tr("Shift"), + Qt.Key_Control: QtGui.qApp.tr("Control"), + Qt.Key_Alt: QtGui.qApp.tr("Alt"), + Qt.Key_Meta: QtGui.qApp.tr("Meta"), + } + + s = modifierKeyNames.get(viewAction.key) # QKeySequence returns weird strings when only a modifier is pressed + if s is None: + s = QtGui.QKeySequence(viewAction.key | viewAction.modifiers).toString() + if viewAction.key == 0: + s = s[:-2] + + if hasattr(viewAction, 'button'): + if len(s): + s += "+" + s += buttonName(viewAction.button) + return s class ButtonModifierInput(QtGui.QPushButton): - def __init__(self, action, *a, **kw): - super(ButtonModifierInput, self).__init__(flat=True, text=modifierText(action), *a, **kw) + def __init__(self, viewAction, *a, **kw): + """ + + :type viewAction: mcedit2.worldview.worldview.ViewMouseAction + """ + super(ButtonModifierInput, self).__init__(text=describeKeys(viewAction), *a, **kw) self.clicked.connect(self.buttonClicked) - self.inputWidget = ModifierInputWidget(action) + self.viewAction = viewAction + self.inputWidget = ModifierInputWidget(self) def buttonClicked(self): - self.inputWidget.show() + inputWidget = self.inputWidget + inputWidget.show() + + rect = inputWidget.geometry() + topRight = self.parent().mapToGlobal(self.geometry().topLeft()) + rect.moveTopRight(topRight) + inputWidget.setGeometry(rect) + + def updateText(self): + self.setText(describeKeys(self.viewAction)) + + @property + def key(self): + return self.viewAction.key + + @key.setter + def key(self, value): + self.viewAction.key = value + self.updateText() + + @property + def modifiers(self): + return self.viewAction.modifiers + + @modifiers.setter + def modifiers(self, value): + self.viewAction.modifiers = value + self.updateText() + + @property + def button(self): + return self.viewAction.button + + @button.setter + def button(self, value): + if hasattr(self.viewAction, 'button'): + self.viewAction.button = value + self.updateText() + class ModifierInputWidget(QtGui.QWidget): - def __init__(self, mouseAction, *a, **kw): + def __init__(self, inputButton, *a, **kw): """ - :type mouseAction: ViewMouseAction + :type inputButton: ButtonModifierInput """ super(ModifierInputWidget, self).__init__(f=Qt.Popup, *a, **kw) - self.inputLabel = QtGui.QLabel(modifierText(mouseAction)) - self.helpLabel = QtGui.QLabel("Click mouse button xxor press a keyxx, while holding any number of modifier keys (Shift, Ctrl, Alt)") + self.inputLabel = QtGui.QLabel(describeKeys(inputButton)) + if hasattr(inputButton.viewAction, "button"): + s = "Click a mouse button or press a key, while holding any number of modifier keys (Shift, Ctrl, Alt)" + else: + s = "Press a key while holding any number of modifier keys (Shift, Ctrl, Alt)" + + self.helpLabel = QtGui.QLabel(s) self.setLayout(Column(self.inputLabel, self.helpLabel)) self.helpLabel.mousePressEvent = self.mousePressEvent - self.mouseAction = mouseAction + self.inputButton = inputButton def mousePressEvent(self, mouseEvent): """ - :type mouseEvent: QMouseEvent + :type mouseEvent: QtGui.QMouseEvent """ - ks = QtGui.QKeySequence(mouseEvent.modifiers()) - s = ks.toString() + buttonName(mouseEvent.button()) - self.inputLabel.setText(s) - self.mouseAction.modifiers = mouseEvent.modifiers() - self.mouseAction.button = mouseEvent.button() + if not self.rect().contains(mouseEvent.pos()): + super(ModifierInputWidget, self).mousePressEvent(mouseEvent) + return + + if not hasattr(self.inputButton.viewAction, "button"): + return + + self.inputButton.modifiers = mouseEvent.modifiers() + self.inputButton.button = mouseEvent.button() + self.inputLabel.setText(describeKeys(self.inputButton.viewAction)) + + def keyPressEvent(self, keyEvent): + """ + + :param keyEvent: + :type keyEvent: QtGui.QKeyEvent + :return: + :rtype: + """ + key = keyEvent.key() + if key == Qt.Key_Escape: + self.close() + return + + modifiers = keyEvent.modifiers() + + log.info("Control change: key %s modifiers %s", hex(key), hex(int(modifiers))) + + self.inputButton.modifiers = modifiers + self.inputButton.key = key + self.inputButton.button = Qt.NoButton + self.inputLabel.setText(describeKeys(self.inputButton.viewAction)) - # - #def keyPressEvent(self, *args, **kwargs): - # pass - # - #def keyReleaseEvent(self, *args, **kwargs): - # pass - # - #def mouseReleaseEvent(self, mouseEvent): - # pass - # - #def __init__(self, mouseAction, *args, **kwargs): - # super(ButtonModifierInput, self).__init__(*args, **kwargs) - # self.setMaximumWidth(150) - # self.mouseAction = mouseAction - # self.setText() class ViewControls(QtGui.QFrame): def __init__(self, worldView, *args, **kwargs): @@ -89,7 +168,7 @@ class ViewControls(QtGui.QFrame): layout = QtGui.QFormLayout() self.controlInputs = [] - for mouseAction in self.worldView.mouseActions: + for mouseAction in self.worldView.mouseActions + self.worldView.keyActions: if not mouseAction.hidden: modInput = ButtonModifierInput(mouseAction, enabled=False) self.controlInputs.append(modInput) @@ -103,13 +182,13 @@ class ViewControls(QtGui.QFrame): self.controlsButton = QtGui.QToolButton() self.controlsButton.setDefaultAction(action) - self.unlockButton = QtGui.QPushButton("Edit", clicked=self.unlockControls) - layout.addRow(QtGui.QLabel("Edit Controls:"), self.unlockButton) - self.setLayout(layout) + self.unlockButton = QtGui.QPushButton("Edit Controls", clicked=self.unlockControls) + self.setLayout(Column(layout, self.unlockButton)) def unlockControls(self): for ci in self.controlInputs: ci.setEnabled(True) + self.unlockButton.setEnabled(False) def lockControls(self):