def highlight(self, document: QTextDocument, rules: list): """Highlights document""" char_format = QTextCharFormat() cursor = QTextCursor(document) while not cursor.isNull() and not cursor.atEnd(): cursor.movePosition(QTextCursor.EndOfWord, QTextCursor.KeepAnchor) text = cursor.selectedText() color, bgcolor = self.get_register_hl_color( text, self.highlighted_regs) if not color: color, bgcolor = self.get_color(text, rules) if color: char_format.setForeground(QColor(color)) if bgcolor: char_format.setBackground(QColor(bgcolor)) if color or bgcolor: cursor.mergeCharFormat(char_format) char_format.clearBackground() self.move_to_next_word(document, cursor)
def paintEvent(self, event): if not globalSettings.lineNumbersEnabled: return QWidget.paintEvent(self, event) painter = QPainter(self) painter.fillRect(event.rect(), colorValues['lineNumberArea']) cursor = QTextCursor(self.editor.document()) cursor.movePosition(QTextCursor.Start) atEnd = False if globalSettings.relativeLineNumbers: relativeTo = self.editor.textCursor().blockNumber() else: relativeTo = -1 while not atEnd: rect = self.editor.cursorRect(cursor) block = cursor.block() if block.isVisible(): number = str(cursor.blockNumber() - relativeTo).replace( '-', '−') painter.setPen(colorValues['lineNumberAreaText']) painter.drawText(0, rect.top(), self.width() - 2, self.fontMetrics().height(), Qt.AlignRight, number) cursor.movePosition(QTextCursor.EndOfBlock) atEnd = cursor.atEnd() if not atEnd: cursor.movePosition(QTextCursor.NextBlock)
def highlight(self): char_format = QTextCharFormat() cursor = QTextCursor(self.doc) while not cursor.isNull() and not cursor.atEnd(): cursor.movePosition(QTextCursor.EndOfWord, QTextCursor.KeepAnchor) color = self.get_color(cursor.selectedText()) if color is not None: if self.use_darker_text_color: color = color.darker() char_format.setForeground(color) cursor.mergeCharFormat(char_format) self.move_to_next_word(self.doc, cursor)
def keywordHighlight(self, doc): cursor = QTextCursor(doc) cursor.beginEditBlock() fmt = QTextCharFormat() fmt.setBackground(Qt.darkYellow) highlightCursor = QTextCursor(doc) while not highlightCursor.isNull() and not highlightCursor.atEnd(): highlightCursor = doc.find(self.keyword, highlightCursor) if not highlightCursor.isNull(): highlightCursor.mergeCharFormat(fmt) cursor.endEditBlock()
def lineNumberAreaPaintEvent(self, event): painter = QPainter(self.lineNumberArea) painter.fillRect(event.rect(), colorValues['lineNumberArea']) cursor = QTextCursor(self.document()) cursor.movePosition(QTextCursor.Start) atEnd = False while not atEnd: rect = self.cursorRect(cursor) block = cursor.block() if block.isVisible(): number = str(cursor.blockNumber() + 1) painter.setPen(colorValues['lineNumberAreaText']) painter.drawText(0, rect.top(), self.lineNumberArea.width()-2, self.fontMetrics().height(), Qt.AlignRight, number) cursor.movePosition(QTextCursor.EndOfBlock) atEnd = cursor.atEnd() if not atEnd: cursor.movePosition(QTextCursor.NextBlock)
def highlight(self): char_format = QTextCharFormat() cursor = QTextCursor(self.doc) while not cursor.isNull() and not cursor.atEnd(): cursor.movePosition(QTextCursor.EndOfWord, QTextCursor.KeepAnchor) color = self.get_register_hl_color(cursor.selectedText()) if color is not None: char_format.setBackground(self.reg_hl_bg_color) else: color = self.get_color(cursor.selectedText()) if color is not None: char_format.setForeground(color) cursor.mergeCharFormat(char_format) char_format.clearBackground() self.move_to_next_word(self.doc, cursor)
def lineNumberAreaPaintEvent(self, event): painter = QPainter(self.lineNumberArea) painter.fillRect(event.rect(), Qt.cyan) cursor = QTextCursor(self.document()) cursor.movePosition(QTextCursor.Start) atEnd = False while not atEnd: rect = self.cursorRect(cursor) block = cursor.block() if block.isVisible(): number = str(cursor.blockNumber() + 1) painter.setPen(Qt.darkCyan) painter.drawText(0, rect.top(), self.lineNumberArea.width() - 2, self.fontMetrics().height(), Qt.AlignRight, number) cursor.movePosition(QTextCursor.EndOfBlock) atEnd = cursor.atEnd() if not atEnd: cursor.movePosition(QTextCursor.NextBlock)
def on_findButton_clicked(self): search_string = self.ui_line_edit.text() document = self.ui_text_edit.document() self.found = False document.undo() if not search_string: QMessageBox.information(self, "Empty Search Field", 'Please enter a word') else: highlight_cursor = QTextCursor(document) cursor = QTextCursor(document) cursor.beginEditBlock() plain_format = QTextCharFormat(highlight_cursor.charFormat()) color_format = plain_format color_format.setForeground(Qt.red) while not highlight_cursor.isNull() and not highlight_cursor.atEnd(): highlight_cursor = document.find( search_string, highlight_cursor, QTextDocument.FindWholeWords ) if not highlight_cursor.isNull(): self.found = True highlight_cursor.movePosition(QTextCursor.WordRight, QTextCursor.KeepAnchor) highlight_cursor.mergeCharFormat(color_format) cursor.endEditBlock() if not self.found: QMessageBox.information(self, "Word not found", "Sorry, the word coannot be found")
def paintEvent(self, event): if not globalSettings.lineNumbersEnabled: return QWidget.paintEvent(self, event) painter = QPainter(self) painter.fillRect(event.rect(), colorValues['lineNumberArea']) cursor = QTextCursor(self.editor.document()) cursor.movePosition(QTextCursor.Start) atEnd = False if globalSettings.relativeLineNumbers: relativeTo = self.editor.textCursor().blockNumber() else: relativeTo = -1 while not atEnd: rect = self.editor.cursorRect(cursor) block = cursor.block() if block.isVisible(): number = str(cursor.blockNumber() - relativeTo).replace('-', '−') painter.setPen(colorValues['lineNumberAreaText']) painter.drawText(0, rect.top(), self.width() - 2, self.fontMetrics().height(), Qt.AlignRight, number) cursor.movePosition(QTextCursor.EndOfBlock) atEnd = cursor.atEnd() if not atEnd: cursor.movePosition(QTextCursor.NextBlock)
def move_to_next_word(self, doc: QTextDocument, cursor: QTextCursor): """Moves cursor to next word""" while not cursor.isNull() and not cursor.atEnd(): if doc.characterAt(cursor.position()) not in self.ignored_chars: return cursor.movePosition(QTextCursor.NextCharacter)