def setRowMod(self, row, name, enabled, conditions): for i, text in enumerate([name, enabled, conditions]): label = QLabel() label.setText(text) label.setAlignment(Qt.AlignCenter) self.ui.injectionsTable.setCellWidget(row, i, label)
def setRowMod(self, row, name, frontEnabled, frontText, backEnabled, backText): for i, text in enumerate([name, frontEnabled, frontText, backEnabled, backText]): label = QLabel() label.setText(text) label.setAlignment(Qt.AlignCenter) self.ui.iterationsTable.setCellWidget(row, i, label)
def setRowMod(self, row, *args): for i, text in enumerate(args): label = QLabel() label.setText(text) label.setAlignment(Qt.AlignCenter) self.ui.scriptsTable.setCellWidget(row, i, label)
class QKeyGrab(QDialog): """ Simple key combination grabber for hotkey assignments Based in part on ImageResizer by searene (https://github.com/searene/Anki-Addons) """ modkey_names = PLATFORM_MODKEY_NAMES[PLATFORM] def __init__(self, parent): """ Initialize dialog Arguments: parent {QWidget} -- Parent Qt widget """ QDialog.__init__(self, parent=parent) self.parent = parent # self.active is used to trace whether there's any key held now: self.active = 0 self._resetDialog() self._setupUI() def _setupUI(self): """Basic UI setup""" mainLayout = QVBoxLayout() self.label = QLabel("Please press the key combination\n" "you would like to assign") self.label.setAlignment(Qt.AlignCenter) mainLayout.addWidget(self.label) self.setLayout(mainLayout) self.setWindowTitle("Grab key combination") def _resetDialog(self): self.extra = self.key_string = None self.meta = self.ctrl = self.alt = self.shift = False def keyPressEvent(self, evt): """ Intercept key presses and save current key plus active modifiers. Arguments: evt {QKeyEvent} -- Intercepted key press event """ self.active += 1 key = evt.key() if key > 0 and key < 127: self.extra = chr(key) elif key == Qt.Key_Control: self.ctrl = True elif key == Qt.Key_Alt: self.alt = True elif key == Qt.Key_Shift: self.shift = True elif key == Qt.Key_Meta: self.meta = True else: self.extra = QKeySequence(key).toString() self.other = True def keyReleaseEvent(self, evt): """ Intercept key release event, checking and then saving key combo and exiting dialog. Arguments: evt {QKeyEvent} -- Intercepted key release event """ self.active -= 1 if self.active != 0: # at least 1 key still held return # TODO: platform-specific messages msg = None if not (self.shift or self.ctrl or self.alt or self.meta or self.other): msg = ("Please use at least one keyboard modifier\n" "({meta}, {ctrl}, {alt}, {shift})".format( **self.modkey_names)) if (self.shift and not (self.ctrl or self.alt or self.meta or self.other)): msg = ("Shift needs to be combined with at least one\n" "other modifier ({meta}, {ctrl}, {alt})".format( **self.modkey_names)) if not self.extra: msg = ("Please press at least one key that is \n" "not a modifier (not {meta}, {ctrl}, " "{alt}, or {shift})".format(**self.modkey_names)) if msg: self.label.setText(msg) self._resetDialog() return combo = [] if self.meta: combo.append("Meta") if self.ctrl: combo.append("Ctrl") if self.shift: combo.append("Shift") if self.alt: combo.append("Alt") combo.append(self.extra) self.key_string = "+".join(combo) self.accept()