def onMatchNumberChange(self, matchNumber): # Set default format on the whole text before highlighting the selected # match. document = self.matchText.document() cursor = QTextCursor(document) cursor.movePosition(QTextCursor.End, QTextCursor.KeepAnchor) cursor.setCharFormat(QTextCharFormat()) search = self.getSearchText() for i, match in enumerate(self.regex.finditer(search)): if i + 1 == matchNumber: break else: assert False, ("We didn't find a match?! (RE=%r, text=%r" % (self.regex.pattern, search)) self.formatMatchedText(document, match) model = self.groupsView.model() model.clear() # Create a reversed self.regex.groupindex dictionnary groupsIndexes = dict( (v, k) for (k, v) in self.regex.groupindex.iteritems()) for i in range(1, self.regex.groups + 1): groupName = groupsIndexes.get(i, "") groupValue = match.group(i) model.append((groupName, groupValue))
def onMatchNumberChange(self, matchNumber): # Set default format on the whole text before highlighting the selected # match. document = self.matchText.document() cursor = QTextCursor(document) cursor.movePosition(QTextCursor.End, QTextCursor.KeepAnchor) cursor.setCharFormat(QTextCharFormat()) search = self.getSearchText() for i, match in enumerate(self.regex.finditer(search)): if i + 1 == matchNumber: break else: assert False, ("We didn't find a match?! (RE=%r, text=%r" % (self.regex.pattern, search)) self.formatMatchedText(document, match) model = self.groupsView.model() model.clear() # Create a reversed self.regex.groupindex dictionnary groupsIndexes = dict((v, k) for (k, v) in self.regex.groupindex.iteritems()) for i in range(1, self.regex.groups + 1): groupName = groupsIndexes.get(i, "") groupValue = match.group(i) model.append((groupName, groupValue))
def formatMatchedText(self, document, match): """Format the matched text in a document""" cursor = QTextCursor(document) cursor.setPosition(match.start()) cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor, match.end() - match.start()) cursor.setCharFormat(self.matchFormat)
def formatMatchedText(self, document, match): """Format the matched text in a document""" cursor = QTextCursor(document) cursor.setPosition(match.start()) cursor.movePosition( QTextCursor.NextCharacter, QTextCursor.KeepAnchor, match.end() - match.start()) cursor.setCharFormat(self.matchFormat)
def __highlight(self, positions, color=None, cancel=False): cursor = QTextCursor(self.document()) for position in positions: if position > self.get_position('eof'): return cursor.setPosition(position) cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor) charformat = cursor.charFormat() pen = QPen(Qt.NoPen) if cancel else QPen(color) charformat.setTextOutline(pen) cursor.setCharFormat(charformat)
def html_copy(cursor, scheme='editor', number_lines=False): """Return a new QTextDocument with highlighting set as HTML textcharformats. The cursor is a cursor of a document.Document instance. If the cursor has a selection, only the selection is put in the new document. If number_lines is True, line numbers are added. """ data = textformats.formatData(scheme) doc = QTextDocument() doc.setDefaultFont(data.font) doc.setPlainText(cursor.document().toPlainText()) if metainfo.info(cursor.document()).highlighting: highlight(doc, mapping(data), ly.lex.state(documentinfo.mode(cursor.document()))) if cursor.hasSelection(): # cut out not selected text start, end = cursor.selectionStart(), cursor.selectionEnd() cur1 = QTextCursor(doc) cur1.setPosition(start, QTextCursor.KeepAnchor) cur2 = QTextCursor(doc) cur2.setPosition(end) cur2.movePosition(QTextCursor.End, QTextCursor.KeepAnchor) cur2.removeSelectedText() cur1.removeSelectedText() if number_lines: c = QTextCursor(doc) f = QTextCharFormat() f.setBackground(QColor('#eeeeee')) if cursor.hasSelection(): num = cursor.document().findBlock( cursor.selectionStart()).blockNumber() + 1 last = cursor.document().findBlock(cursor.selectionEnd()) else: num = 1 last = cursor.document().lastBlock() lastnum = last.blockNumber() + 1 padding = len(format(lastnum)) block = doc.firstBlock() while block.isValid(): c.setPosition(block.position()) c.setCharFormat(f) c.insertText('{0:>{1}d} '.format(num, padding)) block = block.next() num += 1 return doc
def html_copy(cursor, scheme="editor", number_lines=False): """Return a new QTextDocument with highlighting set as HTML textcharformats. The cursor is a cursor of a document.Document instance. If the cursor has a selection, only the selection is put in the new document. If number_lines is True, line numbers are added. """ data = textformats.formatData(scheme) doc = QTextDocument() doc.setDefaultFont(data.font) doc.setPlainText(cursor.document().toPlainText()) if metainfo.info(cursor.document()).highlighting: highlight(doc, mapping(data), ly.lex.state(documentinfo.mode(cursor.document()))) if cursor.hasSelection(): # cut out not selected text start, end = cursor.selectionStart(), cursor.selectionEnd() cur1 = QTextCursor(doc) cur1.setPosition(start, QTextCursor.KeepAnchor) cur2 = QTextCursor(doc) cur2.setPosition(end) cur2.movePosition(QTextCursor.End, QTextCursor.KeepAnchor) cur2.removeSelectedText() cur1.removeSelectedText() if number_lines: c = QTextCursor(doc) f = QTextCharFormat() f.setBackground(QColor("#eeeeee")) if cursor.hasSelection(): num = cursor.document().findBlock(cursor.selectionStart()).blockNumber() + 1 last = cursor.document().findBlock(cursor.selectionEnd()) else: num = 1 last = cursor.document().lastBlock() lastnum = last.blockNumber() + 1 padding = len(format(lastnum)) block = doc.firstBlock() while block.isValid(): c.setPosition(block.position()) c.setCharFormat(f) c.insertText("{0:>{1}d} ".format(num, padding)) block = block.next() num += 1 return doc
def notify(self, text, textHighlight=None): # highlightRange is range in passed text. length = len(self.notifyBox.toPlainText()) self.notifyBox.append(text) self.notifyBox.verticalScrollBar().setValue( self.notifyBox.verticalScrollBar().maximum()) if textHighlight != None: begin = length + text.find(textHighlight) + 1 end = begin + len(textHighlight) fmt = QTextCharFormat() col = QColor(Qt.red) col.setAlpha(130) fmt.setBackground(col) cursor = QTextCursor(self.notifyBox.document()) cursor.setPosition(begin) cursor.setPosition(end, QTextCursor.KeepAnchor) cursor.setCharFormat(fmt)
def makeTableDocument(self): printer = QPrinter() document = QTextDocument() document.setDefaultStyleSheet("table {border:1px; border-color:teal}") document.setDefaultStyleSheet("h1, h2, h3 {color:teal}") document.setDocumentMargin(0.0) document.setPageSize(QSizeF(printer.pageRect().size())) header = ''' <html> <body> <div style="line-height:2.5"> <h1>Desmond International College</h1> <h2>Km4, Happiness Street, Kafanchan</h2> <h2>Kaduna, Nigeria</h2> </div> <div> <h2 style='display:block; text-align:center; word-spacing:10vw; text-transform: uppercase; margin-top:25px; margin-bottom:15px'><u>STUDENT DATA TABLE</u></h2> </div> </body> </html> ''' #print(dir(document)) cursor = QTextCursor(document) rows = self.table.rowCount() columns = self.table.columnCount() cursor.insertHtml(header) table = cursor.insertTable(rows + 1, columns) formats = table.format() formats.setHeaderRowCount(1) table.setFormat(formats) formats = cursor.blockCharFormat() formats.setFontWeight(QFont.Bold) for column in range(columns): cursor.setCharFormat(formats) cursor.insertText(self.table.horizontalHeaderItem(column).text()) cursor.movePosition(QTextCursor.NextCell) for row in range(rows): for column in range(columns): cursor.insertText(self.table.item(row, column).text()) cursor.movePosition(QTextCursor.NextCell) return document
def highlight(document, mapping=None, state=None): """Highlight a generic QTextDocument once. mapping is an optional Mapping instance, defaulting to the current configured editor highlighting formats (returned by highlight_mapping()). state is an optional ly.lex.State instance. By default the text type is guessed. """ if mapping is None: mapping = highlight_mapping() if state is None: state = ly.lex.guessState(document.toPlainText()) cursor = QTextCursor(document) block = document.firstBlock() while block.isValid(): for token in state.tokens(block.text()): f = mapping[token] if f: cursor.setPosition(block.position() + token.pos) cursor.setPosition(block.position() + token.end, QTextCursor.KeepAnchor) cursor.setCharFormat(f) block = block.next()