def do_test(self): selections = [] self.match_locs = [] if self.regex_valid(): text = unicode_type(self.preview.toPlainText()) regex = unicode_type(self.regex.text()) cursor = QTextCursor(self.preview.document()) extsel = QTextEdit.ExtraSelection() extsel.cursor = cursor extsel.format.setBackground(QBrush(Qt.yellow)) try: for match in compile_regular_expression(regex).finditer(text): es = QTextEdit.ExtraSelection(extsel) es.cursor.setPosition(match.start(), QTextCursor.MoveAnchor) es.cursor.setPosition(match.end(), QTextCursor.KeepAnchor) selections.append(es) self.match_locs.append((match.start(), match.end())) except: pass self.preview.setExtraSelections(selections) if self.match_locs: self.next.setEnabled(True) self.previous.setEnabled(True) self.occurrences.setText(unicode_type(len(self.match_locs)))
def search(self, text="", show_msg=True): # impl. of interface IEditor find_text = self._view.search_replace.controls["search-edit"].text() if not show_msg and text.strip(): find_text = text # pragma: no cover flags = QTextDocument.FindFlags() if self._view.search_replace.controls["cs-box"].isChecked(): flags |= QTextDocument.FindCaseSensitively old_cursor = self._view.text.textCursor() self._view.text.moveCursor(QTextCursor.Start) color = QColor(Qt.yellow).lighter(130) extra_sels = [] while self._view.text.find(find_text, options=flags): extra = QTextEdit.ExtraSelection() extra.format.setBackground(color) extra.cursor = self._view.text.textCursor() extra_sels.append(extra) self._view.text.setExtraSelections(extra_sels) if len(extra_sels) != 0: txt = self.tr("Results found") txt = f'{txt}: {len(extra_sels)}' cursor = self._view.text.textCursor() cursor.clearSelection() self._view.text.setTextCursor(cursor) else: txt = self.tr("Nothing found :(") self._view.text.setTextCursor(old_cursor) if show_msg: self._view.show_info(txt)
def highlight(self): hi_selection = QTextEdit.ExtraSelection() hi_selection.format.setBackground(self.palette().alternateBase()) hi_selection.format.setProperty(QTextFormat.FullWidthSelection, True) hi_selection.cursor = self.textCursor() hi_selection.cursor.clearSelection() self.setExtraSelections([hi_selection])
def mark_selected_text(self): sel = QTextEdit.ExtraSelection() sel.format.setBackground(self.highlight_color) sel.cursor = self.textCursor() if sel.cursor.hasSelection(): self.current_search_mark = sel c = self.textCursor() c.clearSelection() self.setTextCursor(c) else: self.current_search_mark = None self.update_extra_selections()
def add_tag(tag): a = QTextEdit.ExtraSelection() a.cursor, a.format = editor.textCursor(), editor.match_paren_format a.cursor.setPosition(tag.start_block.position()), a.cursor.movePosition(a.cursor.EndOfBlock, a.cursor.KeepAnchor) text = unicode(a.cursor.selectedText()) start_pos = utf16_length(text[:tag.start_offset]) a.cursor.setPosition(tag.end_block.position()), a.cursor.movePosition(a.cursor.EndOfBlock, a.cursor.KeepAnchor) text = unicode(a.cursor.selectedText()) end_pos = utf16_length(text[:tag.end_offset + 1]) a.cursor.setPosition(tag.start_block.position() + start_pos) a.cursor.setPosition(tag.end_block.position() + end_pos, a.cursor.KeepAnchor) ans.append(a)
def highlightCurrentLine(self): extra_selections = [] if self.hasFocus() and not self.isReadOnly(): selection = QTextEdit.ExtraSelection() line_color = QColor(Qt.yellow).lighter(180) selection.format.setBackground(line_color) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() extra_selections.append(selection) self.setExtraSelections(extra_selections)
def highlight_cursor_line(self): sel = QTextEdit.ExtraSelection() sel.format.setBackground(self.palette().alternateBase()) sel.format.setProperty(QTextFormat.FullWidthSelection, True) sel.cursor = self.textCursor() sel.cursor.clearSelection() self.current_cursor_line = sel self.update_extra_selections(instant=False) # Update the cursor line's line number in the line number area try: self.line_number_area.update(0, self.last_current_lnum[0], self.line_number_area.width(), self.last_current_lnum[1]) except AttributeError: pass block = self.textCursor().block() top = int(self.blockBoundingGeometry(block).translated(self.contentOffset()).top()) height = int(self.blockBoundingRect(block).height()) self.line_number_area.update(0, top, self.line_number_area.width(), height)