def __init__(self, parent, *args): QPlainTextEdit.__init__(self, *args) self.parent = parent self.config_main = self.parent.config_main self.config_theme = self.parent.config_theme self.tools = self.parent.tools self.lvars = self.parent.lvars self._loaded = False self.setFrameStyle(QFrame.NoFrame) self.setLineWrapMode(QPlainTextEdit.NoWrap) self._setup_ui() self._casts_marked = False self._casts_selections = None self.cursorPositionChanged.connect( self._on_cursor_position_changed) self._bracket_info = hrdev_plugin.include.helper.AttributeDict() self._bracket_info.saved_bracket = None self._bracket_info.depth = 0 self._bracket_info.seeking_nl = False self._bracket_info.open_brackets = ['[', '{', '('] self._bracket_info.closed_brackets = [']', '}', ')'] self._bracket_info.pairs_closed = {']': '[', '}': '{', ')': '('} self._bracket_info.pairs_open = {'[': ']', '{': '}', '(': ')'} self._bracket_info.ignore_stack_left = [] self._bracket_info.ignore_stack_right = [] self._left_selected_bracket = QTextEdit.ExtraSelection() self._right_selected_bracket = QTextEdit.ExtraSelection() toolTipWidget = QtGui.QLabel() toolTipWidget.setStyleSheet( "QLabel { background-color : #ffffcc; color : #222; padding: 5px; }" ) toolTipWidget.setFrameShape(QtGui.QFrame.StyledPanel) toolTipWidget.setWindowFlags(QtCore.Qt.ToolTip) toolTipWidget.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents) toolTipWidget.hide() self._toolTipWidget = toolTipWidget self._timer = QtCore.QBasicTimer() self._timer.start(2000, self) self._min_marker_len = self.config_main.getint( 'editor', 'min_marker_len') return
def _toogle_breakpoint(self, cursor, line_number): '''Toggle breakpoint line.''' if line_number in self.breakpoints: self.breakpoints[line_number].cursor.clearSelection() self.breakpoints.pop(line_number) selections = [] for prev_selection in self.breakpoints: selections.append(self.breakpoints[prev_selection]) self.edit.setExtraSelections(selections) return color = self.config_theme.get('editor', 'breakpoint_line_color') selection = QTextEdit.ExtraSelection() selection.format.setBackground(QtGui.QColor(color)) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = cursor self.breakpoints[line_number] = selection selections = [] for prev_selection in self.breakpoints: selections.append(self.breakpoints[prev_selection]) selections.append(selection) self.edit.setExtraSelections(selections) return
def testPropertyValues(self): app = QApplication(sys.argv) textEdit = QPlainTextEdit() textEdit.insertPlainText("PySide INdT") selection = QTextEdit.ExtraSelection() selection.cursor = textEdit.textCursor() selection.cursor.setPosition(2) self.assertEqual(selection.cursor.position(), 2)
def _watch_line(self): '''Handler for current line change.''' selection = QTextEdit.ExtraSelection() color = self.config_theme.get('editor', 'current_line_color') selection.format.setBackground(QtGui.QColor(color)) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() return selection
def _toggle_casts(self): '''TODO: feature to toggle casting.''' print 'toggle 0' if self._casts_marked: for selection in self._casts_selections: selection.cursor.clearSelection() self._casts_marked = False self._casts_selections = None return print 'toggle 1' search_flag = QtGui.QTextDocument.FindFlags(0) search_flag |= QtGui.QTextDocument.FindWholeWords search_flag |= QtGui.QTextDocument.FindCaseSensitively marker_color = self.config_theme.get('editor', 'hidden_color') self._casts_selections = [] selection = QTextEdit.ExtraSelection() cursor = self.document().find(QtCore.QRegExp(r'\(\w+\s\*\)')) cursor.select(QtGui.QTextCursor.WordUnderCursor) print cursor.block().text() cursor.movePosition(QtGui.QTextCursor.Start) selection.format.setBackground(QtGui.QColor(marker_color)) selection.cursor = cursor self._casts_selections.append(selection) while cursor: cursor = self.document().find(QtCore.QRegExp(r'\(\w+\s\*\)'), cursor, search_flag) if not cursor: break selection = QTextEdit.ExtraSelection() selection.format.setBackground(QtGui.QColor(marker_color)) selection.cursor = cursor print cursor.block().text() self._casts_selections.append(selection) self.setExtraSelections(self._casts_selections) self._casts_marked = True return
def highlightCurrentLine(self): #Create a selection region that shows the current line #Taken from the codeeditor.cpp example selection = QTextEdit.ExtraSelection() lineColor = QColor(Qt.green).lighter(160) selection.format.setBackground(lineColor) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() self.setExtraSelections([selection])
def highlightCurrentLine(self): extraSelections = [] if (not self.isReadOnly()): lineColor = QColor("#E0EEEE") selection = QTextEdit.ExtraSelection() selection.format.setBackground(lineColor) selection.format.setProperty(QTextCharFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() extraSelections.append(selection) self.setExtraSelections(extraSelections)
def __init__(self, parent, *args): QPlainTextEdit.__init__(self, *args) self.parent = parent self.config_main = self.parent.config_main self.config_theme = self.parent.config_theme self.tools = self.parent.tools self._loaded = False self.setFrameStyle(QFrame.NoFrame) self.setLineWrapMode(QPlainTextEdit.NoWrap) self._setup_ui() self._casts_marked = False self._casts_selections = None self.cursorPositionChanged.connect( self._on_cursor_position_changed) self._bracket_info = include.helper.AttributeDict() self._bracket_info.saved_bracket = None self._bracket_info.depth = 0 self._bracket_info.seeking_nl = False self._bracket_info.open_brackets = ['[', '{', '('] self._bracket_info.closed_brackets = [']', '}', ')'] self._bracket_info.pairs_closed = {']': '[', '}': '{', ')': '('} self._bracket_info.pairs_open = {'[': ']', '{': '}', '(': ')'} self._bracket_info.ignore_stack_left = [] self._bracket_info.ignore_stack_right = [] self._left_selected_bracket = QTextEdit.ExtraSelection() self._right_selected_bracket = QTextEdit.ExtraSelection() self._min_marker_len = self.config_main.getint('editor', 'min_marker_len') return
def _watch_marker(self): '''Handler to create text markers.''' if not self.textCursor(): return cursor = self.textCursor() cursor.select(QtGui.QTextCursor.WordUnderCursor) word = cursor.selectedText() if not word: return if len(word) < self._min_marker_len: return cursor.movePosition(QtGui.QTextCursor.Start) search_flag = QtGui.QTextDocument.FindFlags(0) search_flag |= QtGui.QTextDocument.FindWholeWords search_flag |= QtGui.QTextDocument.FindCaseSensitively selections = [] marker_color = self.config_theme.get('editor', 'marker_color') selection = QTextEdit.ExtraSelection() selection.format.setBackground(QtGui.QColor(marker_color)) selection.cursor = cursor selections.append(selection) while cursor: cursor = self.document().find(word, cursor, search_flag) if not cursor: break selection = QTextEdit.ExtraSelection() selection.format.setBackground(QtGui.QColor(marker_color)) selection.cursor = cursor selections.append(selection) return selections
def highlight(self, ctrl, lineno): selection = QTextEdit.ExtraSelection() selection.cursor = ctrl.textCursor() selection.format.setBackground(QColor(100, 200, 100)) selection.format.setProperty(QTextFormat.FullWidthSelection, True) doc = ctrl.document() block = doc.findBlockByLineNumber(lineno) selection.cursor.setPosition(block.position()) ss = ctrl.extraSelections() ss.append(selection) ctrl.setExtraSelections(ss) selection.cursor.clearSelection()
def selections(self): """Build list of extra selections for rectangular selection""" selections = [] cursors = self.cursors() if cursors: background = self._qpart.palette().color(QPalette.Highlight) foreground = self._qpart.palette().color(QPalette.HighlightedText) for cursor in cursors: selection = QTextEdit.ExtraSelection() selection.format.setBackground(background) selection.format.setForeground(foreground) selection.cursor = cursor selections.append(selection) return selections
def _makeMatchSelection(self, block, columnIndex, matched): """Make matched or unmatched QTextEdit.ExtraSelection """ selection = QTextEdit.ExtraSelection() if matched: bgColor = QColor('#9ED1FF') else: bgColor = QColor('#FFDFDF') selection.format.setBackground(bgColor) selection.cursor = QTextCursor(block) selection.cursor.setPositionInBlock(columnIndex) selection.cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor) return selection
def highlight_line(self, lineno=None): if lineno is None: color = self.edit_color else: color = self.code.line_highlight_color selection = QTextEdit.ExtraSelection() selection.format.setBackground(color) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.code.textCursor() if lineno is not None: doc = self.code.document() block = doc.findBlockByLineNumber(lineno - 1) pos = block.position() selection.cursor.setPosition(pos) selection.cursor.clearSelection() self.code.setExtraSelections([selection])
def _highlight(self, lineno, kind): if kind == 'addition': color = QColor(225, 254, 229) #light green elif kind == 'deletion': color = QColor(255, 228, 228) #light red selection = QTextEdit.ExtraSelection() selection.format.setBackground(color) selection.format.setProperty(QTextFormat.FullWidthSelection, True) ctrl = self.control doc = ctrl.document() block = doc.findBlockByLineNumber(lineno) pos = block.position() cursor = ctrl.textCursor() cursor.setPosition(pos) selection.cursor = cursor return selection