def key_press_event(self, ev, which=0): code = ev.key() if self.capture == 0 or code in (0, Qt.Key_unknown, Qt.Key_Shift, Qt.Key_Control, Qt.Key_Alt, Qt.Key_Meta, Qt.Key_AltGr, Qt.Key_CapsLock, Qt.Key_NumLock, Qt.Key_ScrollLock): return QWidget.keyPressEvent(self, ev) button = getattr(self, 'button%d' % which) button.setStyleSheet('QPushButton { font-weight: normal}') mods = int(ev.modifiers()) & ~Qt.KeypadModifier txt = unicode(ev.text()) if txt and txt.lower() == txt.upper(): # We have a symbol like ! or > etc. In this case the value of code # already includes Shift, so remove it mods &= ~Qt.ShiftModifier sequence = QKeySequence(code | mods) button.setText(sequence.toString(QKeySequence.NativeText)) self.capture = 0 dup_desc = self.dup_check(sequence) if dup_desc is not None: error_dialog(self, _('Already assigned'), unicode(sequence.toString(QKeySequence.NativeText)) + ' ' + _('already assigned to') + ' ' + dup_desc, show=True) self.clear_clicked(which=which)
def key_press_event(self, ev, which=0): code = ev.key() if self.capture == 0 or code in ( 0, Qt.Key_unknown, Qt.Key_Shift, Qt.Key_Control, Qt.Key_Alt, Qt.Key_Meta, Qt.Key_AltGr, Qt.Key_CapsLock, Qt.Key_NumLock, Qt.Key_ScrollLock, ): return QWidget.keyPressEvent(self, ev) button = getattr(self, "button%d" % which) font = QFont() button.setFont(font) sequence = QKeySequence(code | (int(ev.modifiers()) & ~Qt.KeypadModifier)) button.setText(sequence.toString(QKeySequence.NativeText)) self.capture = 0 setattr(self, "shortcut%d" % which, sequence) dup_desc = self.dup_check(sequence, self.key) if dup_desc is not None: error_dialog( self, _("Already assigned"), unicode(sequence.toString(QKeySequence.NativeText)) + " " + _("already assigned to") + " " + dup_desc, show=True, ) self.clear_clicked(which=which)
def key_press_event(self, ev, which=0): code = ev.key() if self.capture == 0 or code in (0, Qt.Key_unknown, Qt.Key_Shift, Qt.Key_Control, Qt.Key_Alt, Qt.Key_Meta, Qt.Key_AltGr, Qt.Key_CapsLock, Qt.Key_NumLock, Qt.Key_ScrollLock): return QWidget.keyPressEvent(self, ev) button = getattr(self, 'button%d'%which) button.setStyleSheet('QPushButton { font-weight: normal}') mods = int(ev.modifiers()) & ~Qt.KeypadModifier # for some reason qt sometimes produces ascii control codes in text, # for example ctrl+shift+u will give text == '\x15' on linux txt = clean_ascii_chars(ev.text()) if txt and txt.lower() == txt.upper(): # We have a symbol like ! or > etc. In this case the value of code # already includes Shift, so remove it mods &= ~Qt.ShiftModifier sequence = QKeySequence(code|mods) button.setText(sequence.toString(QKeySequence.NativeText)) self.capture = 0 dup_desc = self.dup_check(sequence) if dup_desc is not None: error_dialog(self, _('Already assigned'), unicode(sequence.toString(QKeySequence.NativeText)) + ' ' + _('already assigned to') + ' ' + dup_desc, show=True) self.clear_clicked(which=which)
def key_press_event(self, ev, which=0): code = ev.key() if self.capture == 0 or code in (0, Qt.Key.Key_unknown, Qt.Key.Key_Shift, Qt.Key.Key_Control, Qt.Key.Key_Alt, Qt.Key.Key_Meta, Qt.Key.Key_AltGr, Qt.Key.Key_CapsLock, Qt.Key.Key_NumLock, Qt.Key.Key_ScrollLock): return QWidget.keyPressEvent(self, ev) sequence = QKeySequence(code | (int(ev.modifiers()) & (~Qt.KeyboardModifier.KeypadModifier))) setattr(self, 'shortcut%d' % which, sequence) self.clear_button(which) self.capture = 0 dup_desc = self.dup_check(sequence, self.key) if dup_desc is not None: error_dialog(self, _('Already assigned'), unicode_type( sequence.toString( QKeySequence.SequenceFormat.NativeText)) + ' ' + _('already assigned to') + ' ' + dup_desc, show=True) self.clear_clicked(which=which)
def setEditorData(self, editor, index): defs = index.data(DEFAULTS) defs = _(" or ").join([unicode(x.toString(x.NativeText)) for x in defs]) editor.key = unicode(index.data(KEY)) editor.default_shortcuts.setText(_("&Default") + ": %s" % defs) editor.default_shortcuts.setChecked(True) editor.header.setText("<b>%s: %s</b>" % (_("Customize shortcuts for"), unicode(index.data(DESCRIPTION)))) custom = index.data(CUSTOM) if custom: editor.custom.setChecked(True) for x in (0, 1): button = getattr(editor, "button%d" % (x + 1)) if len(custom) > x: seq = QKeySequence(custom[x]) button.setText(seq.toString(seq.NativeText)) setattr(editor, "shortcut%d" % (x + 1), seq)
def key_press_event(self, ev, which=0): code = ev.key() if self.capture == 0 or code in (0, Qt.Key_unknown, Qt.Key_Shift, Qt.Key_Control, Qt.Key_Alt, Qt.Key_Meta, Qt.Key_AltGr, Qt.Key_CapsLock, Qt.Key_NumLock, Qt.Key_ScrollLock): return QWidget.keyPressEvent(self, ev) sequence = QKeySequence(code|(int(ev.modifiers())&~Qt.KeypadModifier)) setattr(self, 'shortcut%d'%which, sequence) self.clear_button(which) self.capture = 0 dup_desc = self.dup_check(sequence, self.key) if dup_desc is not None: error_dialog(self, _('Already assigned'), unicode_type(sequence.toString(QKeySequence.NativeText)) + ' ' + _('already assigned to') + ' ' + dup_desc, show=True) self.clear_clicked(which=which)
def setEditorData(self, editor, index): defs = index.data(DEFAULTS) defs = _(' or ').join([unicode_type(x.toString(x.NativeText)) for x in defs]) editor.key = unicode_type(index.data(KEY)) editor.default_shortcuts.setText(_('&Default') + ': %s' % defs) editor.default_shortcuts.setChecked(True) editor.header.setText('<b>%s: %s</b>'%(_('Customize shortcuts for'), unicode_type(index.data(DESCRIPTION)))) custom = index.data(CUSTOM) if custom: editor.custom.setChecked(True) for x in (0, 1): button = getattr(editor, 'button%d'%(x+1)) if len(custom) > x: seq = QKeySequence(custom[x]) button.setText(seq.toString(seq.NativeText)) setattr(editor, 'shortcut%d'%(x+1), seq)
def finalize(shortcuts, custom_keys_map={}): # {{{ ''' Resolve conflicts and assign keys to every action in shortcuts, which must be a OrderedDict. User specified mappings of unique names to keys (as a list of strings) should be passed in in custom_keys_map. Return a mapping of unique names to resolved keys. Also sets the set_to_default member correctly for each shortcut. ''' seen, keys_map = {}, {} for unique_name, shortcut in iteritems(shortcuts): custom_keys = custom_keys_map.get(unique_name, None) if custom_keys is None: candidates = shortcut['default_keys'] shortcut['set_to_default'] = True else: candidates = custom_keys shortcut['set_to_default'] = False keys = [] for x in candidates: ks = QKeySequence(x, QKeySequence.SequenceFormat.PortableText) x = unicode_type( ks.toString(QKeySequence.SequenceFormat.PortableText)) if x in seen: if DEBUG: prints('Key %r for shortcut %s is already used by' ' %s, ignoring' % (x, shortcut['name'], seen[x]['name'])) keys_map[unique_name] = () continue seen[x] = shortcut keys.append(ks) keys = tuple(keys) keys_map[unique_name] = keys ac = shortcut['action'] if ac is None or sip.isdeleted(ac): if ac is not None and DEBUG: prints('Shortcut %r has a deleted action' % unique_name) continue ac.setShortcuts(list(keys)) return keys_map
def finalize(shortcuts, custom_keys_map={}): # {{{ ''' Resolve conflicts and assign keys to every action in shortcuts, which must be a OrderedDict. User specified mappings of unique names to keys (as a list of strings) should be passed in in custom_keys_map. Return a mapping of unique names to resolved keys. Also sets the set_to_default member correctly for each shortcut. ''' seen, keys_map = {}, {} for unique_name, shortcut in shortcuts.iteritems(): ac = shortcut['action'] if ac is None or sip.isdeleted(ac): if ac is not None and DEBUG: prints('Shortcut %r has a deleted action' % unique_name) continue custom_keys = custom_keys_map.get(unique_name, None) if custom_keys is None: candidates = shortcut['default_keys'] shortcut['set_to_default'] = True else: candidates = custom_keys shortcut['set_to_default'] = False keys = [] for x in candidates: ks = QKeySequence(x, QKeySequence.PortableText) x = unicode(ks.toString(QKeySequence.PortableText)) if x in seen: if DEBUG: prints('Key %r for shortcut %s is already used by' ' %s, ignoring'%(x, shortcut['name'], seen[x]['name'])) keys_map[unique_name] = () continue seen[x] = shortcut keys.append(ks) keys = tuple(keys) keys_map[unique_name] = keys ac.setShortcuts(list(keys)) return keys_map
def shortcut(self, keySequence: str): kSeq = QKeySequence(keySequence) self.widget.setShortcut(kSeq) self.widget.setToolTip(kSeq.toString()) return self