def _update_preedit(self, cand=''): preedit_text = self._preedit_text if self.should_draw_preedit() else '' text = IBus.Text.new_from_string(preedit_text + cand + self.roman_text) preedit_len = len(preedit_text) cand_len = len(cand) roman_len = len(self.roman_text) text_len = preedit_len + cand_len + roman_len attrs = IBus.AttrList() if 0 < text_len else None pos = 0 if 0 < preedit_len: attrs.append(IBus.Attribute.new(IBus.AttrType.UNDERLINE, IBus.AttrUnderline.SINGLE, pos, pos + preedit_len)) pos += preedit_len if 0 < cand_len: attrs.append(IBus.Attribute.new(IBus.AttrType.FOREGROUND, CANDIDATE_FOREGROUND_COLOR, pos, pos + cand_len)) attrs.append(IBus.Attribute.new(IBus.AttrType.BACKGROUND, CANDIDATE_BACKGROUND_COLOR, pos, pos + cand_len)) pos += cand_len if 0 < roman_len: attrs.append(IBus.Attribute.new(IBus.AttrType.UNDERLINE, IBus.AttrUnderline.SINGLE, pos, pos + roman_len)) pos += preedit_len if attrs: text.set_attributes(attrs) # A delay is necessary for textareas of Firefox 102.0.1. if text: time.sleep(EVENT_DELAY) # Note self.hide_preedit_text() does not seem to work as expected with Kate. # cf. "Qt5 IBus input context does not implement hide_preedit_text()", # https://bugreports.qt.io/browse/QTBUG-48412 self.update_preedit_text(text, text_len, 0 < text_len) self._update_lookup_table()
def update_candidates(self): preedit_len = len(self.preedit_string) # IBusAttrList — AttrList of IBusText. # An IBusText is the main text object in IBus. # The text is decorated according to associated IBusAttribute, # e.g. the foreground/background color, underline, and applied scope. attrs = IBus.AttrList() self.lookup_table.clear() # reset candidate self.candidates = [] if preedit_len > 0: iwubi_results, len_wubi_list = self.iwubi.find_characters( self.preedit_string) self._last_wubi_list_len = len_wubi_list for char_sequence, display_str in iwubi_results: candidate = IBus.Text.new_from_string(display_str) self.candidates.append(char_sequence) self.lookup_table.append_candidate(candidate) # Do not show auxiliary bar. Keep UI clean. attrs.append( IBus.Attribute.new(IBus.AttrType.UNDERLINE, IBus.AttrUnderline.SINGLE, 0, preedit_len)) text = IBus.Text.new_from_string(self.preedit_string) text.set_attributes(attrs) # update_preedit_text: Update the pre-edit buffer. # text : Update content. # cursor_pos : Current position of cursor # visible : Whether the pre-edit buffer is visible. self.update_preedit_text(text, preedit_len, preedit_len > 0) self._update_lookup_table() self.is_invalidate = False
def update_candidates(self): preedit_len = len(self.preedit_string) attrs = IBus.AttrList() self.lookup_table.clear() self.candidates = [] if preedit_len > 0: uniemoji_results = self.uniemoji.find_characters( self.preedit_string) for char_sequence, display_str in uniemoji_results: candidate = IBus.Text.new_from_string(display_str) self.candidates.append(char_sequence) self.lookup_table.append_candidate(candidate) text = IBus.Text.new_from_string(self.preedit_string) text.set_attributes(attrs) self.update_auxiliary_text(text, preedit_len > 0) attrs.append( IBus.Attribute.new(IBus.AttrType.UNDERLINE, IBus.AttrUnderline.SINGLE, 0, preedit_len)) text = IBus.Text.new_from_string(self.preedit_string) text.set_attributes(attrs) self.update_preedit_text(text, preedit_len, preedit_len > 0) self._update_lookup_table() self.is_invalidate = False
def __update(self): transliterated_text = self.__transliterator.transliterate( self.__preedit_string) preedit_len = len(self.__preedit_string) attrs = IBus.AttrList() self.__lookup_table.clear() if preedit_len > 0: if not self.__dict.check(self.__preedit_string): attrs.append( IBus.Attribute.new(IBus.AttrType.FOREGROUND, 0xff0000, 0, preedit_len)) for text in self.__transliterator.check_and_suggest( transliterated_text): self.__lookup_table.append_candidate( IBus.Text.new_from_string(text)) self.__lookup_table.append_candidate( IBus.Text.new_from_string(self.__preedit_string)) text = IBus.Text.new_from_string(transliterated_text) text.set_attributes(attrs) self.update_auxiliary_text(text, preedit_len > 0) attrs.append( IBus.Attribute.new(IBus.AttrType.UNDERLINE, IBus.AttrUnderline.SINGLE, 0, preedit_len)) text = IBus.Text.new_from_string(self.__preedit_string) text.set_attributes(attrs) self.update_preedit_text(text, preedit_len, preedit_len > 0) self.__update_lookup_table() self.__is_invalidate = False
def update_candidates(self): preedit_len = len(self.preedit_string) attrs = IBus.AttrList() self.lookup_table.clear() self.candidates = [] if preedit_len > 0: check = self.preedit_string.lower() for name in self.table: if check in name: candidate = IBus.Text.new_from_string(u'{}: {}'.format( self.table[name], name)) self.candidates.append(self.table[name]) self.lookup_table.append_candidate(candidate) text = IBus.Text.new_from_string(self.preedit_string) text.set_attributes(attrs) self.update_auxiliary_text(text, preedit_len > 0) attrs.append( IBus.Attribute.new(IBus.AttrType.UNDERLINE, IBus.AttrUnderline.SINGLE, 0, preedit_len)) text = IBus.Text.new_from_string(self.preedit_string) text.set_attributes(attrs) self.update_preedit_text(text, preedit_len, preedit_len > 0) self._update_lookup_table() self.is_invalidate = False
def __update(self): preedit_len = len(self.__preedit_string) text = IBus.Text.new_from_string(self.__preedit_string) if 0 < preedit_len: attrs = IBus.AttrList() attrs.append(IBus.Attribute.new(IBus.AttrType.UNDERLINE, IBus.AttrUnderline.SINGLE, 0, preedit_len)) text.set_attributes(attrs) # Note self.hide_preedit_text() does not seem to work as expected with Kate. # cf. "Qt5 IBus input context does not implement hide_preedit_text()", # https://bugreports.qt.io/browse/QTBUG-48412 self.update_preedit_text(text, preedit_len, 0 < preedit_len) self.__update_lookup_table()
def update_candidates(self): preedit_len = len(self.preedit_string) attrs = IBus.AttrList() self.lookup_table.clear() self.candidates = [] # candidate_strings = set() # if preedit_len > 0: if self.preedit_string == "a": unicode_name = '1' ascii_match = 'first' display_str = u'{}: {} [{}]'.format(ascii_match, unicode_name, self.preedit_string) candidate = IBus.Text.new_from_string(display_str) self.candidates.append(ascii_match) self.lookup_table.append_candidate(candidate) unicode_name = '2' ascii_match = 'number one' display_str = u'{}: {} [{}]'.format(ascii_match, unicode_name, self.preedit_string) candidate = IBus.Text.new_from_string(display_str) self.candidates.append(ascii_match) self.lookup_table.append_candidate(candidate) if self.preedit_string == "b": unicode_name = '3' ascii_match = 'second' display_str = u'{}: {} [{}]'.format(ascii_match, unicode_name, self.preedit_string) candidate = IBus.Text.new_from_string(display_str) self.candidates.append(ascii_match) self.lookup_table.append_candidate(candidate) text = IBus.Text.new_from_string(self.preedit_string) text.set_attributes(attrs) self.update_auxiliary_text(text, preedit_len > 0) attrs.append( IBus.Attribute.new(IBus.AttrType.UNDERLINE, IBus.AttrUnderline.SINGLE, 0, preedit_len)) text = IBus.Text.new_from_string(self.preedit_string) text.set_attributes(attrs) self.update_preedit_text(text, preedit_len, preedit_len > 0) self._update_lookup_table() self.is_invalidate = False
def _update_aux (self): '''Update Aux String in UI''' _aux = self._aux_str if _aux: attrs = IBus.AttrList() attrs.append(IBus.attr_foreground_new(0x9515b5, 0, len(_aux))) text = IBus.Text.new_from_string(_aux) i = 0 while attrs.get(i) != None: attr = attrs.get(i) text.append_attribute(attr.get_attr_type(), attr.get_value(), attr.get_start_index(), attr.get_end_index()) i += 1 super(tabengine, self).update_auxiliary_text(text, True) else: self.hide_auxiliary_text()
def _update_preedit (self): '''Update Preedit String in UI''' _str = self._preedit_str if _str == '': super(tabengine, self).update_preedit_text(IBus.Text.new_from_string(''), 0, False) else: # because ibus now can only insert preedit into txt, so... attrs = IBus.AttrList() attrs.append(IBus.attr_underline_new(IBus.AttrUnderline.SINGLE, 0, len(_str))) text = IBus.Text.new_from_string(_str) i = 0 while attrs.get(i) != None: attr = attrs.get(i) text.append_attribute(attr.get_attr_type(), attr.get_value(), attr.get_start_index(), attr.get_end_index()) i += 1 super(tabengine, self).update_preedit_text(text, len(_str), True)
def __update(self): preedit_len = len(self.__preedit_string) attrs = IBus.AttrList() self.__lookup_table.clear() self.__pos = 0 self.__candidates = [] if preedit_len > 2 and self.__state == 2: words = string.split( self.__preedit_string[2:].strip().upper().replace( '\'', '\'\''), ' ') (cands, fuz_cands) = self.__data.find_candidates(words) sort_func = lambda x: int(x, 16) candidate_codes = sorted(list(cands), key=sort_func) + \ sorted(list(fuz_cands), key=sort_func) for code in candidate_codes: self.__candidates += [ unichr(int(code, 16)) + " - \\u" + code + \ " - " + self.__data.find_description(code) ] for candidate in self.__candidates[0:10]: self.__lookup_table.append_candidate( IBus.Text.new_from_string(candidate)) elif preedit_len > 1 and (self.__state == 1 or self.__state == 3): pass text = IBus.Text.new_from_string(self.__preedit_string) text.set_attributes(attrs) self.update_auxiliary_text(text, self.__state == 2 and preedit_len > 0) attrs.append( IBus.Attribute.new(IBus.AttrType.UNDERLINE, IBus.AttrUnderline.SINGLE, 0, preedit_len)) text = IBus.Text.new_from_string(self.__preedit_string) text.set_attributes(attrs) self.update_preedit_text(text, preedit_len, preedit_len > 0) self.__update_lookup_table() self.__is_invalidate = False
def update_candidates(self): preedit_len = len(self.preedit_string) attrs = IBus.AttrList() self.lookup_table.clear() self.candidates = [] candidate_strings = set() if preedit_len > 0: # Look for an ASCII alias that matches exactly ascii_match = self.ascii_table.get(self.preedit_string) if ascii_match: unicode_name = self.reverse_ascii_table[ascii_match] display_str = '{}: {} [{}]'.format(ascii_match, unicode_name, self.preedit_string) candidate = IBus.Text.new_from_string(display_str) self.candidates.append(ascii_match) self.lookup_table.append_candidate(candidate) # Look for a fuzzy match against a description for level, score, name, candidate_type in self.filter( self.preedit_string.lower()): uniemoji_char = self.table[name] # Since we have several source (UnicodeData.txt, EmojiOne), # make sure we don't output multiple identical candidates if candidate_type == CANDIDATE_UNICODE: if uniemoji_char.unicode_str in candidate_strings: continue candidate_strings.add(uniemoji_char.unicode_str) display_str = None if uniemoji_char.is_emojione: unicode_name = self.unicode_chars_to_names.get( uniemoji_char.unicode_str) if unicode_name and unicode_name != name: display_str = '{}: :{}: {}'.format( uniemoji_char.unicode_str, name.replace(' ', '_'), unicode_name) if display_str is None: shortname = self.unicode_chars_to_shortnames.get( uniemoji_char.unicode_str, '') if shortname: shortname = ':' + shortname + ': ' display_str = '{}: {}{}'.format( uniemoji_char.unicode_str, shortname, name) candidate = IBus.Text.new_from_string(display_str) self.candidates.append(uniemoji_char.unicode_str) self.lookup_table.append_candidate(candidate) # Aliases expand into several candidates for unicode_str in uniemoji_char.aliasing: if unicode_str in candidate_strings: continue candidate_strings.add(unicode_str) unicode_name = self.unicode_chars_to_names.get(unicode_str) shortname = self.unicode_chars_to_shortnames.get( unicode_str, '') if shortname: shortname = ':' + shortname + ': ' display_str = '{}: {}{} [{}]'.format( unicode_str, shortname, unicode_name, name) candidate = IBus.Text.new_from_string(display_str) self.candidates.append(unicode_str) self.lookup_table.append_candidate(candidate) text = IBus.Text.new_from_string(self.preedit_string) text.set_attributes(attrs) self.update_auxiliary_text(text, preedit_len > 0) attrs.append( IBus.Attribute.new(IBus.AttrType.UNDERLINE, IBus.AttrUnderline.SINGLE, 0, preedit_len)) text = IBus.Text.new_from_string(self.preedit_string) text.set_attributes(attrs) self.update_preedit_text(text, preedit_len, preedit_len > 0) self._update_lookup_table() self.is_invalidate = False