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 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 on_text_changed(self): if self.tageditor.is_undoing: self.tageditor.is_undoing = False return blocked = self.tageditor.blockSignals(True) self.tageditor.textCursor().beginEditBlock() doc = self.tageditor.document() # pattern = QtCore.QRegExp(r'\{\d{1,2}\}|\{\d{1,2}>|<\d{1,2}\}') pattern = QtCore.QRegExp(r'\{\d{1,2}\}|\{\d{1,2}>|<\d{1,2}\}|\{j\}|\{[biu_^]+>|<[biu_^]+\}') pattern.setMinimal(True) cursor = QTextCursor(doc) # print('textChanged') while True: cursor = doc.find(pattern, cursor) if cursor.isNull(): break matched_str = cursor.selectedText() if matched_str.endswith('>'): tag_name = cursor.selectedText().strip('{>') self.insert_tag(cursor, tag_name, tag_name, TagKind.START) elif matched_str.startswith('<'): tag_name = cursor.selectedText().strip('<}') self.insert_tag(cursor, tag_name, tag_name, TagKind.END) elif matched_str.startswith('{'): tag_name = cursor.selectedText().strip('{}') self.insert_tag(cursor, tag_name, tag_name, TagKind.EMPTY) self.tageditor.textCursor().endEditBlock() self.tageditor.blockSignals(blocked)
def search_and_replace(self, word, newWord = None, replace = False): self.textEdit.textCursor().beginEditBlock() doc = self.textEdit.document() cursor = QTextCursor(doc) while True: cursor = doc.find(word, cursor) if cursor.isNull(): break if replace and newWord is not None: cursor.insertText(newWord) self.textEdit.textCursor().endEditBlock()
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 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 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 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 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)