def _handle_key_dict(self, d: dict) -> str: """ :param d: dictionary :return: Takes an configuration for a single key. Generates a keycode to to be inserted into KEYMAP macro in keymap.c file. As a side effect, prepare macro code and field variables used for insertion. """ p = KeyConfDictParser(d) p.assign_id_if_no_id_bound() p = self._prepare_macro_code_to_be_inserted(p) return p.to_keymap_element()
def _handle_macro_id_conflict(self, d: dict): """ Should be called only when loading keymap or layer from external source. :param d: :return: """ # data should be shared but be on the safe side # make sure changes are shared. p = KeyConfDictParser(d) if not p.is_macro(): return try: d = d[key_macro] except KeyError as e: return # no macro if type(d) is str: return # generated by old script code = p.macro_code ids = p.macro_ids if type(ids) is not list: raise TypeError("supposed to be a list of ids but got:%s" % str(ids)) if len(ids) > 2: raise ValueError("Too many ids are bound to a single key.") # remove all old ids and give them anew new_ids = [] new_code = code for id in ids: if not MacroIDPool.is_id_in_use(id): MacroIDPool.mark_as_in_use([id]) new_ids.append(ids) continue new_id = MacroIDPool.get_id() new_ids.append(new_id) new_code = re.sub("M\(\s*" + str(id) + "\s*\)", "M(%s)" % (new_id), code) new_code = re.sub("case \s*" + str(id) + "\s*:", "case %s:" % (new_id), new_code) code = new_code g = KeyConfDictGenerator() g.set_macro_type(p.macro_type) g.set_macro_ids(new_ids) g.set_code(new_code) g.set_macro_name(p.macro_name) self.keymap.update_key_data(d, g.to_macro_dict())
def __init_gui(self,d): p = KeyConfDictParser(d) hbl = QHBoxLayout() l = QLabel("Modifier mask") cb = self.modifier_selection_combobox = QComboBox() for o in self.options.keys(): cb.addItem(QIcon(),o) pv = p.modifier_mask pv = pv if pv != '' else 'None' cb.setCurrentText(pv) hbl2 = QHBoxLayout() l2 = QLabel("Modifier mask 2") cb2 = self.modifier_selection_combobox2 = QComboBox() for o in self.options.keys(): cb2.addItem(QIcon(),o) pv = p.modifier_mask2 pv = pv if pv != '' else 'None' cb2.setCurrentText(pv) hbl.addWidget(l) hbl.addWidget(cb) hbl2.addWidget(l2) hbl2.addWidget(cb2) self.addLayout(hbl) self.addLayout(hbl2)
def _set_button_label(self, d: dict, b: QPushButton): p = KeyConfDictParser(d) try: macro = d[key_macro] except KeyError as e: macro = None # No macro if not p.is_macro(): # I should get rid of this asap. if p.special_action != '' and p.keyname == '': if p.special_action_param == '': p.keyname = p.special_action else: p.keyname = p.special_action + '(' + p.special_action_param + '(' b.setText(p.keyname) return if type(macro) is str: # likely generated by an old script, not dict, # no ids bound to this key yet. t = 'Macro:\n' + p.macro_name b.setText(t) return # generated by a new script p = KeyConfDictParser(d) # if p.macro_type == "": # raise ValueError("") if len(d[key_macro][key_macro_ids]) < 1: raise ValueError("") t = 'Macro:\n' + p.macro_name b.setText(t)
def get_all_macro_ids_in_use(self): ids = [] for d in self.get_all_keys_data: if d is None: continue p = KeyConfDictParser(d) for id in p.ids: ids.append(id) return ids
def get_keyname(key_conf_dict: dict): d = key_conf_dict p = KeyConfDictParser(d) if p.is_macro(): return '' # Macro should use macro_name t = p.to_keymap_element() # check if keymap_element contains hid_usage_id that # needs to be translated. # if p.special_action != "": return KeyConfDictGenerator._line_break_label(p.keyname) # Note: when user chooses the keycode to assign to a key from # drop down menu, p.hid_usage_id is empty;bad design. # todo fix this. if p.hid_usage_id != "" and t.index(str(p.hid_usage_id)) > -1: t = t.replace(str(p.hid_usage_id), p.keyname) else: # when user selects input from drop down menu t = p.keyname return KeyConfDictGenerator._line_break_label(t)
def __init__(self, macro_dict=''): super(MacroEditor, self).__init__() # here for backward compatibility # it's defaultdict and not dict if type(macro_dict) is str: macro_dict = {key_macro_code: macro_dict} self.macro_data = macro_dict p = KeyConfDictParser(self.macro_data) self.code = p.macro_code self.macro_type = p.macro_type self.macro_ids = p.macro_ids self.macro_name = p.macro_name self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.__init_gui() self.__init_listeners()
def _prepare_macro_code_to_be_inserted(self, p: KeyConfDictParser): if not p.is_macro(): return p # CTL receptor use two macro ids; the first should be inserted into KEYMAP. # somescript still does not assign macro id; to be removed code = p.macro_code m = MacroComposer() # KeyConfDictParser already have assigned an unique id to macro if m.is_contain_macro_gui_timer(code): m, d = m.make_macro_timer_name_unique( code, str(time.time()).replace('.', '_')) self.field_members_to_add.append(d) self.macro_code.append(m) else: self.macro_code.append(code) return p
def __init_gui(self, d): p = KeyConfDictParser(d) # self.font = QFont() # self.font.setPointSize(13) bl = self # bl = self.vertical_box_layout = QVBoxLayout() hl1 = QHBoxLayout() hl1.setContentsMargins(0, 5, 5, 5) hl1.setSpacing(5) # literal row l_literal = QLabel("input literal") self.literal_input_te = input_literal = QLineEdit() # input_literal.setMaximumHeight(font_height) # input_literal.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) def filter(): length = len(input_literal.text()) self.literalUpdated = True if length == 2: c = input_literal.cursor() # c = input_literal.textCursor() # c.movePosition(QTextCursor.Left,QTextCursor.MoveAnchor) # c.deletePreviousChar() # c.movePosition(QTextCursor.Right,QTextCursor.MoveAnchor) ct = input_literal.text() input_literal.clear() input_literal.setText(ct[1]) input_literal.textChanged.connect( filter # stackoverflow # lambda: te.setText(te.toPlainText()[-1]) ) hl1.addWidget(l_literal) hl1.addWidget(input_literal) # hid usage id row hl2 = QHBoxLayout() hl2.setContentsMargins(0, 5, 5, 5) hl2.setSpacing(5) l_hid_usage_id = QLabel("HID usage id:") hl2.addWidget(l_hid_usage_id) self.hid_usage_id_display = QLabel( ) # contents will be set upon user input self.hid_usage_id_display.setText(p.hid_usage_id) hl2.addWidget(self.hid_usage_id_display) # key name row hl3 = QHBoxLayout() hl3.setContentsMargins(0, 5, 5, 5) hl3.setSpacing(5) l_key_name = QLabel("Key's name") hl3.addWidget(l_key_name) self.key_name = QLineEdit() # contents will be set upon user input self.key_name.setText(p.keyname) # self.key_name.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) # self.key_name.setMaximumHeight(font_height) hl3.addWidget(self.key_name) bl.addLayout(hl1) bl.addLayout(hl2) bl.addLayout(hl3) # self.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding) # self.setLayout(bl) bl.setAlignment(QtCore.Qt.AlignTop)
def _dict_to_name_string(self, d): k = KeyConfDictParser(d) return k.to_descriptive_string()