def getFirstVisibleBlockId(self): """ Detect the first block for which bounding rect - once translated in absolute coordinated - is contained by the editor's text area :return: the first visible block """ if self.verticalScrollBar().sliderPosition() == 0: return 0 cursor = QTextCursor(self.document()) cursor.movePosition(QTextCursor.Start) for i in range(self.document().blockCount()): block = cursor.block() r1 = self.viewport().geometry() r2 = self.document().documentLayout().blockBoundingRect(block).translated( self.viewport().geometry().x(), self.viewport().geometry().y() - self.verticalScrollBar().sliderPosition() ).toRect() if r1.contains(r2, True): return i cursor.movePosition(QTextCursor.NextBlock) return 0
def goto_line(self, line_nb): """ Sets the cursor position at the end of the specified line :param line_nb: line number to go to """ cursor = QTextCursor(self.document().findBlockByLineNumber(line_nb - 1)) # Line numbers starts at 0 cursor.movePosition(QTextCursor.EndOfLine) self.setTextCursor(cursor)
def add_message_to_document(document, message): """Adds a message to a document and return the cursor. Args: document (QTextDocument) message (str) Returns: QTextCursor """ cursor = QTextCursor(document) cursor.movePosition(QTextCursor.End) cursor.insertBlock() cursor.insertHtml(message) return cursor
def init_ui(self): layout = QVBoxLayout(self.central_widget) layout.addWidget(self.text_editor) help_button = QPushButton('Помощь по синтаксису') help_button.clicked.connect(self.help_syntax) layout.addWidget(help_button) save_button = QPushButton('Сохранить изменения') save_button.setStyleSheet('font-weight: bold;') save_button.clicked.connect(self.save_module) layout.addWidget(save_button) save_as_button = QPushButton('Сохранить как ...') save_as_button.setStyleSheet('font-style: italic;') save_as_button.clicked.connect(self.save_as_module) layout.addWidget(save_as_button) cursor = QTextCursor(self.text_editor.document()) cursor.movePosition(QTextCursor.End) self.text_editor.setTextCursor(cursor) self.setCentralWidget(self.central_widget)
def on_log_received(self, data): time_info = datetime.fromtimestamp((data['time'] / 1000)).isoformat() log_message = '%s: %s : %s' % (time_info, data['level'], data['message']) message_document = self.document() cursor_to_add = QTextCursor(message_document) cursor_to_add.movePosition(cursor_to_add.End) cursor_to_add.insertText(log_message + '\n') if data['level'] in COLORS: fmt = QTextCharFormat() fmt.setForeground(COLORS[data['level']]) cursor_to_add.movePosition(cursor_to_add.PreviousBlock) log_lvl_data = LogLevelData(log_levels[data['level'].upper()]) cursor_to_add.block().setUserData(log_lvl_data) cursor_to_add_fmt = message_document.find(data['level'], cursor_to_add.position()) cursor_to_add_fmt.mergeCharFormat(fmt) if log_levels[data['level']] > self.log_lvl: cursor_to_add.block().setVisible(False) self.ensureCursorVisible()
def init_ui(self): layout = QVBoxLayout(self.central_widget) layout.addWidget(self.text_editor) help_button = QPushButton('Помощь по синтаксису') help_button.clicked.connect(self.help_syntax) layout.addWidget(help_button) crypto_label = QLabel('Алгоритм шифрования') crypto_label.setStyleSheet('font-style: italic;') crypto_label.setAlignment(Qt.AlignCenter) layout.addWidget(crypto_label) self.type_of_crypto.addItem('XOR') self.type_of_crypto.addItem('AES-256') layout.addWidget(self.type_of_crypto) create_button = QPushButton('Сохранить модуль') create_button.setStyleSheet('font-weight: bold;') create_button.clicked.connect(self.save_module) layout.addWidget(create_button) self.text_editor.setText(create_basic_text()) cursor = QTextCursor(self.text_editor.document()) cursor.movePosition(QTextCursor.End) self.text_editor.setTextCursor(cursor) self.setCentralWidget(self.central_widget)
def set_cursor(self, line_number): doc = self.textbox.document() cursor = QTextCursor(doc) cursor.movePosition(QTextCursor.Start) for _ in range(line_number - 1): cursor.movePosition(QTextCursor.Down) cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.KeepAnchor) self.textbox.setTextCursor(cursor) self.textbox.centerCursor()
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.assertTrue(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.assertTrue(b.parentFrame(), items[i].parentFrame()) b.__isub__(1) i -= 1
def reset_cursor(self): doc = self.textbox.document() cursor = QTextCursor(doc) cursor.movePosition(QTextCursor.Start) self.textbox.setTextCursor(cursor)
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 unhighlight(self): cursor = QTextCursor(self.document()) cursor.movePosition(QTextCursor.Start) cursor.movePosition(QTextCursor.End, QTextCursor.KeepAnchor) cursor.setCharFormat(QTextCharFormat())