def _show_completions(self, txt, force=False): force = force or self._complete_empty if self._match == self.SimpleMatch: pattern = "^" + QRegExp.escape(txt) else: pattern = ".*".join(QRegExp.escape(t) for t in txt.split()) self._proxy_model.setFilterRegExp(QRegExp(pattern, Qt.CaseInsensitive)) if self._proxy_model.rowCount() == 0: self._popup.hide() elif not txt and not force: self._popup.hide() else: self._popup.popup()
def slotCursorPositionChanged(self): """Called whenever the cursor position changes. Highlights matching characters if the cursor is at one of them. """ cursor = self.edit().textCursor() block = cursor.block() text = block.text() # try both characters at the cursor col = cursor.position() - block.position() end = col + 1 col = max(0, col - 1) for c in text[col:end]: if c in self.matchPairs: break col += 1 else: self.clear() return # the cursor is at a character from matchPairs i = self.matchPairs.index(c) cursor.setPosition(block.position() + col) # find the matching character new = QTextCursor(cursor) if i & 1: # look backward match = self.matchPairs[i - 1] flags = QTextDocument.FindBackward else: # look forward match = self.matchPairs[i + 1] flags = QTextDocument.FindFlags() new.movePosition(QTextCursor.Right) # search, also nesting rx = QRegExp(QRegExp.escape(c) + '|' + QRegExp.escape(match)) nest = 0 while nest >= 0: new = cursor.document().find(rx, new, flags) if new.isNull(): self.clear() return nest += 1 if new.selectedText() == c else -1 cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor) self.highlight([cursor, new])
def slotCursorPositionChanged(self): """Called whenever the cursor position changes. Highlights matching characters if the cursor is at one of them. """ cursor = self.edit().textCursor() block = cursor.block() text = block.text() # try both characters at the cursor col = cursor.position() - block.position() end = col + 1 col = max(0, col - 1) for c in text[col:end]: if c in self.matchPairs: break col += 1 else: self.clear() return # the cursor is at a character from matchPairs i = self.matchPairs.index(c) cursor.setPosition(block.position() + col) # find the matching character new = QTextCursor(cursor) if i & 1: # look backward match = self.matchPairs[i - 1] flags = QTextDocument.FindBackward else: # look forward match = self.matchPairs[i + 1] flags = QTextDocument.FindFlags() new.movePosition(QTextCursor.Right) # search, also nesting rx = QRegExp(QRegExp.escape(c) + "|" + QRegExp.escape(match)) nest = 0 while nest >= 0: new = cursor.document().find(rx, new, flags) if new.isNull(): self.clear() return nest += 1 if new.selectedText() == c else -1 cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor) self.highlight([cursor, new])
def __score(self, name, summary): """ Private method to calculate some score for a search result. @param name name of the returned package @type str @param summary summary text for the package @type str @return score value @rtype int """ score = 0 for queryTerm in self.__query: if queryTerm.lower() in name.lower(): score += 4 if queryTerm.lower() == name.lower(): score += 4 if queryTerm.lower() in summary.lower(): if QRegExp(r'\b{0}\b'.format(QRegExp.escape(queryTerm)), Qt.CaseInsensitive).indexIn(summary) != -1: # word match gets even higher score score += 2 else: score += 1 return score
def setSearchString(self, string): """ Public method to set the current search string. @param string new search string (string) """ if string == self.__searchString: return self.__searchString = string self.__searchMatcher.setPattern(self.__searchString) self.__wordMatcher.setPattern("\\b" + QRegExp.escape(self.__searchString)) self.invalidateFilter()
def setSearchString(self, string): """ Public method to set the current search string. @param string new search string (string) """ if string == self.__searchString: return self.__searchString = string self.__searchMatcher.setPattern(self.__searchString) self.__wordMatcher.setPattern( "\\b" + QRegExp.escape(self.__searchString)) self.invalidateFilter()
def highlight_text(text, format): cursor = self._plain_text.textCursor() regex = QRegExp(QRegExp.escape(text)) # Process the displayed document pos = 0 index = regex.indexIn(self._plain_text.toPlainText(), pos) while index != -1: # Select the matched text and apply the desired format cursor.setPosition(index) cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor, len(text)) cursor.mergeCharFormat(format) # Move to the next match pos = index + regex.matchedLength() index = regex.indexIn(self.toPlainText(), pos)
def filterOnFocus(window, focused): loclist = getattr(window, 'build_loclist', None) if not loclist: return model = loclist.model() if not getattr(model, 'isFilterOnFocus', False): orig = model model = QSortFilterProxyModel() model.isFilterOnFocus = True loclist.setModel(model) model.setSourceModel(orig) model.setFilterRole(AbsolutePathRole) model.setFilterRegExp(QRegExp.escape(focused.path or ''))
def update_field_name(self): """Update the window title. Update the window title with the field name corresponding to the field_id which we are using to filter our racer table view. If this is the general racer table view (not a field- specific one), then there's really not much to be done...the window title will always be the same. """ if self.field_id: field_name = self.modeldb.field_table_model.name_from_id( self.field_id) self.setWindowTitle('Racers (%s)' % field_name) regexp = '^' + QRegExp.escape(field_name) + '$' self.proxy_model_filter.setFilterRegExp( QRegExp(regexp, Qt.CaseSensitive)) else: self.setWindowTitle('Racers')
def cursor_position_changed(self): cursor = self.text_edit.textCursor() line, column = cursor.blockNumber(), cursor.columnNumber() w = self.text_edit.window() if isinstance(w, QMainWindow): if line in list(self.line_to_object_map.keys()): o = self.line_to_object_map[line] w.set_object_under_caret(o) else: w.set_object_under_caret(None) extra_selections = [] # Highlight current line. selection = QTextEdit.ExtraSelection() selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.format.setBackground(QBrush(QColor("#E6E6FC"))) selection.cursor = self.text_edit.textCursor() selection.cursor.clearSelection() extra_selections.append(selection) # Highlight all defs and uses. if self.show_def_use: if line in list(self.line_to_object_map.keys()): o = self.line_to_object_map[line] uses = [] defs = [] if hasattr(o, "uses") and o.uses() is not None: uses = o.uses() if hasattr(o, "definitions") and o.definitions() is not None: defs = o.definitions() for o2 in uses + defs: use_line = self.object_to_line_map[o2] selection = QTextEdit.ExtraSelection() selection.format.setProperty( QTextFormat.FullWidthSelection, True) if o2 in uses and o2 in defs: color = "#F0F0F0" elif o2 in uses: color = "#F0FFF0" elif o2 in defs: color = "#FFF0F0" else: assert False selection.format.setBackground(QBrush(QColor(color))) text_block = self.text_edit.document( ).findBlockByLineNumber(use_line) cursor = self.text_edit.textCursor() cursor.setPosition(text_block.position()) selection.cursor = cursor selection.cursor.clearSelection() extra_selections.append(selection) # Highlight all occurences of word under cursor. cursor = self.text_edit.textCursor() cursor.select(QTextCursor.WordUnderCursor) text = cursor.selectedText() rtext = r"\b%s\b" % QRegExp.escape(text) if self.text_edit.textCursor().hasSelection(): text = self.text_edit.textCursor().selectedText() rtext = QRegExp.escape(text) regexp = QRegExp(rtext, Qt.CaseSensitive) cursor.movePosition(QTextCursor.Start) flags = QTextDocument.FindCaseSensitively | QTextDocument.FindWholeWords cursor = self.text_edit.document().find(regexp, cursor, flags) first = cursor while cursor: selection = QTextEdit.ExtraSelection() selection.format.setBackground(QBrush(Qt.yellow)) selection.cursor = cursor selection.format.setFontUnderline(True) extra_selections.append(selection) cursor = self.text_edit.document().find(regexp, cursor, flags) if cursor == first: break self.text_edit.setExtraSelections(extra_selections)