def __init__(self, document_widget, document_model, formatter): super(PugdebugDocumentContents, self).__init__() self.document_widget = document_widget self.document_model = document_model self.setReadOnly(True) self.cursorPositionChanged.connect(self.highlight) self.setPlainText(document_model.contents) self.remove_line_highlights() self.syntaxer = PugdebugSyntaxer(self.document(), formatter)
class PugdebugDocumentContents(QPlainTextEdit): document_widget = None document_model = None syntaxer = None def __init__(self, document_widget, document_model, formatter): super(PugdebugDocumentContents, self).__init__() self.document_widget = document_widget self.document_model = document_model self.setReadOnly(True) self.cursorPositionChanged.connect(self.highlight) self.setPlainText(document_model.contents) self.remove_line_highlights() self.syntaxer = PugdebugSyntaxer(self.document(), formatter) def update_contents(self, document_model): """Update the contents of the document Set the new contents of the document. Refresh the syntaxer. """ self.setPlainText(document_model.contents) self.syntaxer.setDocument(self.document()) self.syntaxer.highlight() def mousePressEvent(self, event): pass def mouseDoubleClickEvent(self, event): path = self.document_model.path cursor = self.cursorForPosition(event.pos()) block = cursor.block() # Do not try to set breakpoints on empty lines if len(block.text()) == 0: return # Set/unset breakpoint flag on the double clicked line if self.block_has_breakpoint(block): self.block_remove_breakpoint(block) else: self.block_set_breakpoint(block) line_number = block.blockNumber() + 1 self.document_widget.document_double_clicked_signal.emit( path, line_number ) def contextMenuEvent(self, event): pass def move_to_line(self, line, is_current=True): """Move cursor to line Move the cursor of the QPlainTextEdit that holds the document contents to the line. Move the cursor block by block until the block number matches the line number. """ line = line - 1 if line < 0: line = 0 cursor = self.textCursor() cursor.movePosition(QTextCursor.Start, QTextCursor.MoveAnchor, 0) block_number = cursor.blockNumber() while block_number < line: cursor_moved = cursor.movePosition( QTextCursor.NextBlock, QTextCursor.MoveAnchor, 1 ) # Unmark block as current block = cursor.block() self.block_set_is_current(block, False) if cursor_moved is False: break block_number = cursor.blockNumber() # Mark block on which the cursor is as the current one block = cursor.block() self.block_set_is_current(block, is_current) self.setTextCursor(cursor) def highlight(self): selection = QTextEdit.ExtraSelection() color = QColor(209, 220, 236) selection.format.setBackground(color) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() self.setExtraSelections([selection]) def remove_line_highlights(self): """Remove line highlights Move the cursor to first (zero) line. Clear the extra selections in the file. """ self.setExtraSelections([]) def block_has_breakpoint(self, block): user_data = self.__get_block_user_data(block) return user_data.breakpoint def block_set_breakpoint(self, block): user_data = self.__get_block_user_data(block) user_data.breakpoint = True block.setUserData(user_data) def block_remove_breakpoint(self, block): user_data = self.__get_block_user_data(block) user_data.breakpoint = False block.setUserData(user_data) def block_is_current(self, block): user_data = self.__get_block_user_data(block) return user_data.is_current def block_set_is_current(self, block, is_current): user_data = self.__get_block_user_data(block) user_data.is_current = is_current block.setUserData(user_data) def __get_block_user_data(self, block): user_data = block.userData() if user_data is None: user_data = PugdebugBlockData() return user_data
class PugdebugDocumentContents(QPlainTextEdit): document_widget = None document_model = None syntaxer = None def __init__(self, document_widget, document_model, formatter): super(PugdebugDocumentContents, self).__init__() self.document_widget = document_widget self.document_model = document_model self.setReadOnly(True) self.cursorPositionChanged.connect(self.highlight) self.setPlainText(document_model.contents) self.remove_line_highlights() self.syntaxer = PugdebugSyntaxer(self.document(), formatter) self.viewport().setCursor(Qt.ArrowCursor) self.set_editor_features() def update_contents(self, document_model): """Update the contents of the document Set the new contents of the document. Refresh the syntaxer. """ self.setPlainText(document_model.contents) self.syntaxer.setDocument(self.document()) self.syntaxer.highlight() def set_editor_features(self): self.setTabStopWidth(int(get_setting('editor/tab_width'))) font = QFont('mono') font.setPixelSize(int(get_setting('editor/font_size'))) self.setFont(font) def mousePressEvent(self, event): pass def mouseDoubleClickEvent(self, event): path = self.document_model.path cursor = self.cursorForPosition(event.pos()) block = cursor.block() # Do not try to set breakpoints on empty lines if len(block.text()) == 0: return # Set/unset breakpoint flag on the double clicked line if self.block_has_breakpoint(block): self.block_remove_breakpoint(block) else: self.block_set_breakpoint(block) line_number = block.blockNumber() + 1 self.document_widget.document_double_clicked_signal.emit( path, line_number) def contextMenuEvent(self, event): pass def move_to_line(self, line, is_current=True): """Move cursor to line Move the cursor of the QPlainTextEdit that holds the document contents to the line. Move the cursor block by block until the block number matches the line number. """ line = line - 1 if line < 0: line = 0 cursor = self.textCursor() cursor.movePosition(QTextCursor.Start, QTextCursor.MoveAnchor, 0) block_number = cursor.blockNumber() while block_number < line: cursor_moved = cursor.movePosition(QTextCursor.NextBlock, QTextCursor.MoveAnchor, 1) # Unmark block as current block = cursor.block() self.block_set_is_current(block, False) if cursor_moved is False: break block_number = cursor.blockNumber() # Mark block on which the cursor is as the current one block = cursor.block() self.block_set_is_current(block, is_current) self.setTextCursor(cursor) def highlight(self): selection = QTextEdit.ExtraSelection() color = QColor(209, 220, 236) selection.format.setBackground(color) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() self.setExtraSelections([selection]) def remove_line_highlights(self): """Remove line highlights Move the cursor to first (zero) line. Clear the extra selections in the file. """ self.setExtraSelections([]) def block_has_breakpoint(self, block): user_data = self.__get_block_user_data(block) return user_data.breakpoint def block_set_breakpoint(self, block): user_data = self.__get_block_user_data(block) user_data.breakpoint = True block.setUserData(user_data) def block_remove_breakpoint(self, block): user_data = self.__get_block_user_data(block) user_data.breakpoint = False block.setUserData(user_data) def block_is_current(self, block): user_data = self.__get_block_user_data(block) return user_data.is_current def block_set_is_current(self, block, is_current): user_data = self.__get_block_user_data(block) user_data.is_current = is_current block.setUserData(user_data) def __get_block_user_data(self, block): user_data = block.userData() if user_data is None: user_data = PugdebugBlockData() return user_data