def append(self, text): """Append line to the end """ cursor = QTextCursor(self._doc) cursor.movePosition(QTextCursor.End) cursor.insertBlock() cursor.insertText(text)
def gotoRow(self, row): rows = self.document().rootFrame().childFrames() if row >= len(rows): traceLogger.critical("invalid row %d > %d" % (row, len(rows) - 1)) row = len(rows) cursor = QTextCursor(rows[row].lastCursorPosition()) cursor.movePosition(QTextCursor.NextBlock) self.setTextCursor(cursor) self.ensureCursorVisible() cursor = QTextCursor(rows[row].childFrames()[0]) self.setTextCursor(cursor) self.ensureCursorVisible()
def gotoRow(self,row): rows = self.document().rootFrame().childFrames() if row >= len(rows): traceLogger.critical("invalid row %d > %d" % (row,len(rows)-1)) row = len(rows) cursor = QTextCursor(rows[row].lastCursorPosition()) cursor.movePosition(QTextCursor.NextBlock) self.setTextCursor(cursor) self.ensureCursorVisible() cursor = QTextCursor(rows[row].childFrames()[0]) self.setTextCursor(cursor) self.ensureCursorVisible()
def _removeBlock(blockIndex): block = self._doc.findBlockByNumber(blockIndex) if block.next().isValid(): # not the last cursor = QTextCursor(block) cursor.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor) elif block.previous().isValid(): # the last, not the first cursor = QTextCursor(block.previous()) cursor.movePosition(QTextCursor.EndOfBlock) cursor.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor) cursor.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor) else: # only one block cursor = QTextCursor(block) cursor.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor) cursor.removeSelectedText()
def insert(self, index, text): """Insert line to the document """ if index < 0 or index > self._doc.blockCount(): raise IndexError('Invalid block index', index) if index == 0: # first cursor = QTextCursor(self._doc.firstBlock()) cursor.insertText(text) cursor.insertBlock() elif index != self._doc.blockCount(): # not the last cursor = QTextCursor(self._doc.findBlockByNumber(index).previous()) cursor.movePosition(QTextCursor.EndOfBlock) cursor.insertBlock() cursor.insertText(text) else: # last append to the end self.append(text)
def insertRow(self, row): self.insertingFrames = True document = self.document() rootFrame = document.rootFrame() rows = rootFrame.childFrames() if row > len(rows): traceLogger.critical("invalid row %d > %d" % (row, len(rows))) row = len(rows) if row == len(rows): cursor = QTextCursor(document) cursor.movePosition(QTextCursor.End) elif rows: cursor = QTextCursor(rows[row].firstCursorPosition()) cursor.movePosition(QTextCursor.PreviousBlock) cursor.insertFrame(QtReduceRowFormat()) cursor.insertFrame(QtReduceInput().frameFormat) position = cursor.position() cursor.clearSelection() cursor.setBlockFormat(QtReduceInput().blockFormat) cursor.setBlockCharFormat(QtReduceInput().charFormat) cursor.movePosition(QTextCursor.NextBlock) cursor.insertFrame(QtReduceNoResult().frameFormat) cursor.setBlockFormat(QtReduceNoResult().blockFormat) cursor.setBlockCharFormat(QtReduceNoResult().charFormat) cursor.insertText(QtReduceFrameView.NotEvaluated) cursor.setPosition(position) self.insertingFrames = False
def insertRow(self,row): self.insertingFrames = True document = self.document() rootFrame = document.rootFrame() rows = rootFrame.childFrames() if row > len(rows): traceLogger.critical("invalid row %d > %d" % (row,len(rows))) row = len(rows) if row == len(rows): cursor = QTextCursor(document) cursor.movePosition(QTextCursor.End) elif rows: cursor = QTextCursor(rows[row].firstCursorPosition()) cursor.movePosition(QTextCursor.PreviousBlock) cursor.insertFrame(QtReduceRowFormat()) cursor.insertFrame(QtReduceInput().frameFormat) position = cursor.position() cursor.clearSelection() cursor.setBlockFormat(QtReduceInput().blockFormat) cursor.setBlockCharFormat(QtReduceInput().charFormat) cursor.movePosition(QTextCursor.NextBlock) cursor.insertFrame(QtReduceNoResult().frameFormat) cursor.setBlockFormat(QtReduceNoResult().blockFormat) cursor.setBlockCharFormat(QtReduceNoResult().charFormat) cursor.insertText(QtReduceFrameView.NotEvaluated) cursor.setPosition(position) self.insertingFrames = False
def cursorForPosition(codeEdit, line, column, selectEndOfLine=False, selection=None, selectWordUnderCursor=False): """ Return a QTextCursor set to line and column with the specified selection :param line: :param column: """ tc = QTextCursor(codeEdit.document()) tc.movePosition(QTextCursor.Start, QTextCursor.MoveAnchor) tc.movePosition(QTextCursor.Down, QTextCursor.MoveAnchor, line - 1) tc.setPosition(tc.position() + column - 1) if selectEndOfLine is True: tc.movePosition(QTextCursor.EndOfLine, QTextCursor.KeepAnchor) elif isinstance(selection, int): tc.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor, selection) elif selectWordUnderCursor is True: tc.select(QTextCursor.WordUnderCursor) codeEdit.setTextCursor(tc) return tc
def _setBlockText(blockIndex, text): cursor = QTextCursor(self._doc.findBlockByNumber(blockIndex)) cursor.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor) cursor.insertText(text)
def testCase(self): editor = QTextEdit() cursor = QTextCursor(editor.textCursor()) cursor.movePosition(QTextCursor.Start) mainFrame = cursor.currentFrame() plainCharFormat = QTextCharFormat() boldCharFormat = QTextCharFormat() boldCharFormat.setFontWeight(QFont.Bold) cursor.insertText( """ Text documents are represented by the QTextDocument class, rather than by QString objects. Each QTextDocument object contains information about the document's internal representation, its structure, and keeps track of modifications to provide undo/redo facilities. This approach allows features such as the layout management to be delegated to specialized classes, but also provides a focus for the framework.""", plainCharFormat) frameFormat = QTextFrameFormat() frameFormat.setMargin(32) frameFormat.setPadding(8) frameFormat.setBorder(4) cursor.insertFrame(frameFormat) cursor.insertText( """ Documents are either converted from external sources or created from scratch using Qt. The creation process can done by an editor widget, such as QTextEdit, or by explicit calls to the Scribe API.""", boldCharFormat) cursor = mainFrame.lastCursorPosition() cursor.insertText( """ There are two complementary ways to visualize the contents of a document: as a linear buffer that is used by editors to modify the contents, and as an object hierarchy containing structural information that is useful to layout engines. In the hierarchical model, the objects generally correspond to visual elements such as frames, tables, and lists. At a lower level, these elements describe properties such as the style of text used and its alignment. The linear representation of the document is used for editing and manipulation of the document's contents.""", plainCharFormat) frame = cursor.currentFrame() items = [] #test iterator for i in frame: items.append(i) #test __iadd__ b = frame.begin() i = 0 while not b.atEnd(): self.assertEqual(b, items[i]) self.assert_(b.parentFrame(), items[i].parentFrame()) b.__iadd__(1) i += 1 #test __isub__ b = frame.end() i = 0 while i > 0: self.assertEqual(b, items[i]) self.assert_(b.parentFrame(), items[i].parentFrame()) b.__isub__(1) i -= 1
def testCase(self): editor = QTextEdit() cursor = QTextCursor(editor.textCursor()) cursor.movePosition(QTextCursor.Start) mainFrame = cursor.currentFrame() plainCharFormat = QTextCharFormat() boldCharFormat = QTextCharFormat() boldCharFormat.setFontWeight(QFont.Bold); cursor.insertText(""" Text documents are represented by the QTextDocument class, rather than by QString objects. Each QTextDocument object contains information about the document's internal representation, its structure, and keeps track of modifications to provide undo/redo facilities. This approach allows features such as the layout management to be delegated to specialized classes, but also provides a focus for the framework.""", plainCharFormat) frameFormat = QTextFrameFormat() frameFormat.setMargin(32) frameFormat.setPadding(8) frameFormat.setBorder(4) cursor.insertFrame(frameFormat) cursor.insertText(""" Documents are either converted from external sources or created from scratch using Qt. The creation process can done by an editor widget, such as QTextEdit, or by explicit calls to the Scribe API.""", boldCharFormat) cursor = mainFrame.lastCursorPosition() cursor.insertText(""" There are two complementary ways to visualize the contents of a document: as a linear buffer that is used by editors to modify the contents, and as an object hierarchy containing structural information that is useful to layout engines. In the hierarchical model, the objects generally correspond to visual elements such as frames, tables, and lists. At a lower level, these elements describe properties such as the style of text used and its alignment. The linear representation of the document is used for editing and manipulation of the document's contents.""", plainCharFormat) frame = cursor.currentFrame() items = [] #test iterator for i in frame: items.append(i) #test __iadd__ b = frame.begin() i = 0 while not b.atEnd(): self.assertEqual(b, items[i]) self.assert_(b.parentFrame(), items[i].parentFrame()) b.__iadd__(1) i += 1 #test __isub__ b = frame.end() i = 0 while i > 0: self.assertEqual(b, items[i]) self.assert_(b.parentFrame(), items[i].parentFrame()) b.__isub__(1) i -= 1