def __check_brackets(self): left, right = QTextEdit.ExtraSelection(), QTextEdit.ExtraSelection() cursor = self.textCursor() block = cursor.block() data = block.userData() previous, _next = None, None if data is not None: position = cursor.position() block_pos = cursor.block().position() paren = data.paren n = len(paren) for k in range(0, n): if paren[k].position == position - block_pos or \ paren[k].position == position - block_pos - 1: previous = paren[k].position + block_pos if paren[k].character == '(': _next = self.__match_left(block, paren[k].character, k + 1, 0) elif paren[k].character == ')': _next = self.__match_right(block, paren[k].character, k, 0) if _next is not None and _next > 0: if previous is not None and previous > 0: _format = QTextCharFormat() cursor.setPosition(previous) cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor) _format.setForeground(Qt.blue) _format.setBackground(Qt.white) left.format = _format left.cursor = cursor cursor.setPosition(_next) cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor) _format.setForeground(Qt.white) _format.setBackground(Qt.blue) right.format = _format right.cursor = cursor return left, right elif previous is not None: _format = QTextCharFormat() cursor.setPosition(previous) cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor) _format.setForeground(Qt.white) _format.setBackground(Qt.red) left.format = _format left.cursor = cursor return (left, right)
def highlightCurrentLine(self): if globalSettings.relativeLineNumbers: self.lineNumberArea.update() if globalSettings.highlightCurrentLine == 'disabled': return self.setExtraSelections([]) selection = QTextEdit.ExtraSelection() selection.format.setBackground(colorValues['currentLineHighlight']) selection.format.setProperty(QTextFormat.Property.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() selections = [selection] if globalSettings.highlightCurrentLine == 'wrapped-line': selections.append(QTextEdit.ExtraSelection()) selections[0].cursor.movePosition( QTextCursor.MoveOperation.StartOfBlock) selections[0].cursor.movePosition( QTextCursor.MoveOperation.EndOfBlock, QTextCursor.MoveMode.KeepAnchor) selections[1].format.setBackground( colorValues['currentLineHighlight']) selections[1].format.setProperty( QTextFormat.Property.FullWidthSelection, True) selections[1].cursor = self.textCursor() selections[1].cursor.movePosition( QTextCursor.MoveOperation.EndOfBlock) elif selection.cursor.block().textDirection( ) == Qt.LayoutDirection.RightToLeft: # FullWidthSelection does not work correctly for RTL direction selection.cursor.movePosition( QTextCursor.MoveOperation.StartOfLine) selection.cursor.movePosition(QTextCursor.MoveOperation.EndOfLine, QTextCursor.MoveMode.KeepAnchor) self.setExtraSelections(selections)
def check(self): cursor = self.textCursor() b = cursor.block() extraSelections = [] if len(b.text()) > 120: selection = QTextEdit.ExtraSelection() # TODO: implement something using flake8 selection.format.setFontUnderline(True) selection.format.setUnderlineColor(QColor("#FF0000")) selection.format.setUnderlineStyle( QTextCharFormat.SpellCheckUnderline) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() self.extraSelections_.append(selection) self.setExtraSelections(self.extraSelections_) font = QFont() font.setFamily("Iosevka") font.setPointSize(10) QToolTip.setFont(font) cursor = self.textCursor() current_line = cursor.positionInBlock() QToolTip.showText( QCursor.pos(), "Line too long (" + str(current_line) + "> 120) | violation on line: " + str(b.blockNumber() + 1))
def highlightError(self, index): """Hihglights the error message at the given index and jumps to its location.""" self._currentErrorIndex = index # set text format pos, anchor, url = self._errors[index] es = QTextEdit.ExtraSelection() es.cursor = QTextCursor(self.document()) es.cursor.setPosition(pos) es.cursor.setPosition(anchor, QTextCursor.KeepAnchor) bg = qutil.mixcolor(self.palette().highlight().color(), self.palette().base().color(), 0.4) es.format.setBackground(bg) es.format.setProperty(QTextFormat.FullWidthSelection, True) self.setExtraSelections([es]) # scroll log to the message cursor = QTextCursor(self.document()) cursor.setPosition(anchor) self.setTextCursor(cursor) cursor.setPosition(pos) self.setTextCursor(cursor) # jump to the error location cursor = errors.errors(self._document()).cursor(url, True) if cursor: self.parentWidget().mainwindow().setTextCursor(cursor, findOpenView=True)
def _highlight_checkers(self, neditable): """Add checker selections to the Editor""" # Remove selections if they exists self.clear_extra_selections('checker') # Get checkers from neditable checkers = neditable.sorted_checkers self.highlight_checker_updated.emit(checkers) selections = [] # FIXME: generalize it with extra_selection.ExtraSelection for items in checkers: checker, color, _ = items lines = checker.checks.keys() for line in lines: msg, col = checker.checks[line] selection = QTextEdit.ExtraSelection() selection.cursor = self.textCursor() selection.cursor.movePosition(QTextCursor.Start, QTextCursor.MoveAnchor) selection.cursor.movePosition(QTextCursor.Down, QTextCursor.MoveAnchor, line) selection.cursor.movePosition(QTextCursor.Right, QTextCursor.MoveAnchor, col - 1) selection.cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.KeepAnchor) selection.format.setUnderlineStyle( QTextCharFormat.SingleUnderline) selection.format.setUnderlineColor(QColor(color)) selections.append(selection) self.add_extra_selections('checker', selections)
def findTextDialog(self): """ Search for text in QTextEdit widget """ # Display input dialog to ask user for text to search for find_text, ok = QInputDialog.getText(self, "Search Text", "Find:") extra_selections = [] # Check to make sure the text can be modified if ok and not self.text_field.isReadOnly(): # set the cursor in the textedit field to the beginning self.text_field.moveCursor(QTextCursor.Start) color = QColor(Qt.yellow) # Look for next occurrence of text while (self.text_field.find(find_text)): # Use ExtraSelections to mark the text you are # searching for as yellow selection = QTextEdit.ExtraSelection() selection.format.setBackground(color) # Set the cursor of the selection selection.cursor = self.text_field.textCursor() # Add selection to list extra_selections.append(selection) # Highlight selections in text edit widget for i in extra_selections: self.text_field.setExtraSelections(extra_selections)
def highlightSearch(self, wordList=None, regExpList=None): """Highlight any found search terms. Arguments: wordList -- list of words to highlight regExpList -- a list of regular expression objects to highlight """ backColor = self.palette().brush(QPalette.Active, QPalette.Highlight) foreColor = self.palette().brush(QPalette.Active, QPalette.HighlightedText) if wordList is None: wordList = [] if regExpList is None: regExpList = [] for regExp in regExpList: for match in regExp.finditer(self.toPlainText()): matchText = match.group() if matchText not in wordList: wordList.append(matchText) selections = [] for word in wordList: while self.find(word): extraSel = QTextEdit.ExtraSelection() extraSel.cursor = self.textCursor() extraSel.format.setBackground(backColor) extraSel.format.setForeground(foreColor) selections.append(extraSel) cursor = QTextCursor(self.document()) self.setTextCursor(cursor) # reset main cursor/selection self.setExtraSelections(selections)
def drawCursor(self, cursor): """Draws the cursor position as the selection of the specified cursor.""" es = QTextEdit.ExtraSelection() es.format.setBackground(self.textEdit().palette().color(QPalette.Text)) es.format.setForeground(self.textEdit().palette().color(QPalette.Base)) es.cursor = cursor self.textEdit().setExtraSelections([es])
def highlightMatches(self, pattern): cur = self.__editor.textCursor() re = QRegExp(pattern) cur = self.__editor.document().find(re) a = cur.position() searchSelections = [] while not cur.isNull(): if cur.hasSelection(): selection = QTextEdit.ExtraSelection() selection.format.setBackground(Qt.GlobalColor.yellow) selection.format.setForeground(Qt.GlobalColor.black) selection.cursor = cur searchSelections.append(selection) else: cur.movePosition(QTextCursor.MoveOperation.NextCharacter) cur = self.__editor.document().find(re, cur) b = cur.position() if a == b: cur.movePosition(QTextCursor.MoveOperation.NextCharacter) cur = self.__editor.document().find(re, cur) b = cur.position() if (a == b): break a = b self.__updateSelections(searchSelections)
def highligtCurrentLine(self): ''' 用来突出显示包含光标的行 ''' newCurrentLineNumber = self.textCursor().blockNumber() # 返回光标所在块的编号,如果光标无效,则返回0 if newCurrentLineNumber != self.currentLineNumber: lineColor = QColor(Qt.yellow).lighter(160) self.currentLineNumber = newCurrentLineNumber hi_selection = QTextEdit.ExtraSelection() # QTextEdit.ExtraSelection结构提供了一种为文档中已选择指定字符格式的方法 hi_selection.format.setBackground(lineColor) # 设定背景色 hi_selection.format.setProperty(QTextFormat.FullWidthSelection, True) # 设定文本的整个宽度将显示为选中状态 hi_selection.cursor = self.textCursor() hi_selection.cursor.clearSelection() # 通过将锚点设置为光标位置来清除当前选择 self.setExtraSelections([hi_selection])
def highlight(self, text_format, cursors, priority=0, msec=0): """Highlights the selection of an arbitrary list of QTextCursors. ``text_format`` is a QTextCharFormat; ``priority`` determines the order of drawing, highlighting with higher priority is drawn over highlighting with lower priority. ``msec``, if > 0, removes the highlighting after that many milliseconds. """ key = id(text_format) self._formats[key] = text_format selections = [] for cursor in cursors: es = QTextEdit.ExtraSelection() es.cursor = cursor es.format = text_format selections.append(es) if msec: def clear(selfref=weakref.ref(self)): self = selfref() if self: self.clear(text_format) timer = QTimer(timeout=clear, singleShot=True) timer.start(msec) self._selections[key] = (priority, selections, timer) else: self._selections[key] = (priority, selections) self.update()
def highlight(self, regex: QRegularExpression, color: QColor = QColor(Qt.yellow)): if regex.pattern() == "": return self.moveCursor(QTextCursor.Start) matches = [] it: QRegularExpressionMatchIterator = regex.globalMatch(self.toPlainText()) while it.hasNext(): match: QRegularExpressionMatch = it.next() if not match.hasMatch(): continue begin = match.capturedStart() end = match.capturedEnd() matchSelection: QTextEdit.ExtraSelection = QTextEdit.ExtraSelection() fmt: QTextCharFormat = matchSelection.format fmt.setBackground(color) matchSelection.cursor = self.textCursor() matchSelection.cursor.setPosition(begin, QTextCursor.MoveAnchor) matchSelection.cursor.setPosition(end, QTextCursor.KeepAnchor) matches.append(matchSelection) self.setExtraSelections(matches) self.moveCursor(QTextCursor.Start)
def _create_extra_selection( self, cursor: QTextCursor) -> QTextEdit.ExtraSelection: palette = self.palette() selection = QTextEdit.ExtraSelection() selection.format.setBackground(palette.color(palette.Highlight)) selection.format.setForeground(palette.color(palette.HighlightedText)) selection.cursor = cursor return selection
def highligtCurrentLine(self, hidden=False): if (hidden): hi_selection = QTextEdit.ExtraSelection() hi_selection.cursor = self.textCursor() hi_selection.cursor.clearSelection() self.setExtraSelections([hi_selection]) else: newCurrentLineNumber = self.textCursor().blockNumber() if newCurrentLineNumber != self.currentLineNumber: self.currentLineNumber = newCurrentLineNumber hi_selection = QTextEdit.ExtraSelection() hi_selection.format.setBackground(self.currentLineColor) hi_selection.format.setProperty(QTextFormat.FullWidthSelection, True) hi_selection.cursor = self.textCursor() hi_selection.cursor.clearSelection() self.setExtraSelections([hi_selection])
def highlightCurrentLine(self): highlightColor = self.palette().alternateBase() selection = QTextEdit.ExtraSelection() selection.format.setBackground(highlightColor) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() self.setExtraSelections([selection])
def highlightIncomplete(self, pos): selection = QTextEdit.ExtraSelection() selection.format.setForeground(Qt.magenta) cursor = self.textCursor() cursor.setPosition(pos) cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor) selection.cursor = cursor self.setExtraSelections(self.extraSelections() + [selection])
def highlightCurrentLine(self): """Draws a borderless rectangle around the current line.""" selection = QTextEdit.ExtraSelection() selection.format.setBackground(APythonTextEditor.HIGHLIGHT) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() self.setExtraSelections([selection])
def createParSelection(self, pos): selection = QTextEdit.ExtraSelection() selection.format.setBackground(Qt.green) selection.format.setForeground(Qt.black) cursor = self.textCursor() cursor.setPosition(pos) cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor) selection.cursor = cursor self.setExtraSelections(self.extraSelections() + [selection])
def highlight_current_line(self): extra_selections = list() if not self.isReadOnly(): selection = QTextEdit.ExtraSelection() line_color = QColor(Qt.yellow).lighter(160) selection.format.setBackground(line_color) selection.format.setProperty(QTextFormat.FullWidthSelection, True) extra_selections.append(selection) self.setExtraSelections(extra_selections)
def highlightCurrentLine(self): if not globalSettings.highlightCurrentLine: return self.setExtraSelections([]) selection = QTextEdit.ExtraSelection() selection.format.setBackground(colorValues['currentLineHighlight']) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() self.setExtraSelections([selection])
def highlightCurrentLine(self): extraSelections = [] if not self.isReadOnly(): selection = QTextEdit.ExtraSelection() selection.format.setBackground(self.theme.backgroundCurrentLine) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() extraSelections.append(selection) self.setExtraSelections(extraSelections)
def highlightCurrentLine(self): extraSelections = [] if not self.isReadOnly(): selection = QTextEdit.ExtraSelection() lineColor = QColor(48, 48, 75) #color of the line highlighter QColor(136, 138, 133) selection.format.setBackground(lineColor) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() extraSelections.append(selection) self.setExtraSelections(extraSelections)
def highlightCurrentLine(self): extraSelections = [] if not self.isReadOnly(): selection = QTextEdit.ExtraSelection() lineColor = QColor(Qt.yellow).lighter(160) selection.format.setBackground(lineColor) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() extraSelections.append(selection) self.setExtraSelections(extraSelections)
def paintEvent(self, event): highlighted_line = QTextEdit.ExtraSelection() highlighted_line.format.setBackground(lineHighlightColor) highlighted_line.format.setProperty(QTextFormat.FullWidthSelection, QVariant(True)) highlighted_line.cursor = self.editor.textCursor() highlighted_line.cursor.clearSelection() self.editor.setExtraSelections([ highlighted_line, self.left_selected_bracket, self.right_selected_bracket ])
def highlight_current_line(self): new_current_line_number = self.textCursor().blockNumber() if new_current_line_number != self.current_line_number: self.current_line_number = new_current_line_number hi_selection = QTextEdit.ExtraSelection() hi_selection.format.setBackground(self.current_line_color) hi_selection.format.setProperty(QTextFormat.FullWidthSelection, True) hi_selection.cursor = self.textCursor() hi_selection.cursor.clearSelection() self.setExtraSelections([hi_selection])
def UpdateHighlightedLine(self): selection = QTextEdit.ExtraSelection() selection.format.setBackground(self.palette().alternateBase()) selection.format.setProperty(QTextFormat.FullWidthSelection, QVariant(True)) selection.cursor = self.textCursor() selection.cursor.clearSelection() self.setExtraSelections([ selection, ]) return
def highlightCurrentLine(self): """Highlight current line under cursor""" currentSelection = QTextEdit.ExtraSelection() lineColor = QColor(Qt.gray).darker(250) currentSelection.format.setBackground(lineColor) currentSelection.format.setProperty(QTextFormat.FullWidthSelection, True) currentSelection.cursor = self.textCursor() currentSelection.cursor.clearSelection() self.setExtraSelections([currentSelection])
def highlightAllToggled(self, toggled: bool) -> None: if self.find_cursors: extraSelections = deque() if self.highlightAll.isChecked(): for cursor in self.find_cursors: extra = QTextEdit.ExtraSelection() extra.format.setBackground(self.highlightColor) extra.format.setForeground(self.textHighlightColor) extra.cursor = cursor extraSelections.append(extra) self.log.setExtraSelections(extraSelections)
def highligtCurrentLine(self): newCurrentLineNumber = self.editor.textCursor().blockNumber() if newCurrentLineNumber != self.currentLineNumber: lineColor = QColor(Qt.yellow).lighter(160) self.currentLineNumber = newCurrentLineNumber hi_selection = QTextEdit.ExtraSelection() hi_selection.format.setBackground(lineColor) hi_selection.format.setProperty(QTextFormat.FullWidthSelection, True) hi_selection.cursor = self.editor.textCursor() hi_selection.cursor.clearSelection() self.editor.setExtraSelections([hi_selection])
def highlight(self, cursors): """Highlights the selections of the specified QTextCursor instances.""" selections = [] for cursor in cursors: es = QTextEdit.ExtraSelection() es.cursor = cursor es.format = self.format selections.append(es) self.edit().setExtraSelections(selections) if self.time and selections: self._timer.start(self.time)