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) 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()) 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()) + ' ' + _('already assigned to') + ' ' + dup_desc, show=True) self.clear_clicked(which=which)
def setEditorData(self, editor, index): defs = index.data(DEFAULTS).toPyObject() defs = _(' or ').join( [unicode(x.toString(x.NativeText)) for x in defs]) editor.key = unicode(index.data(KEY).toString()) 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).toString()))) custom = index.data(CUSTOM).toPyObject() 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 shorcuts, 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(): 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) # print (111111, unique_name, candidates, keys) keys_map[unique_name] = keys ac = shortcut["action"] if ac is not None: ac.setShortcuts(list(keys)) return keys_map
def finalize(shortcuts, custom_keys_map={}): # {{{ ''' Resolve conflicts and assign keys to every action in shorcuts, 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(): 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) #print (111111, unique_name, candidates, keys) keys_map[unique_name] = keys ac = shortcut['action'] if ac is not None: ac.setShortcuts(list(keys)) return keys_map