def update_minimap_doc(self, position, charsRemoved, charsAdded): def select_blocks(first_block, last_block): first_block_pos = first_block.position() doc_cursor.setPosition(first_block_pos) last_block_pos = last_block.position() doc_cursor.setPosition(last_block_pos, QTextCursor.KeepAnchor) doc_cursor.movePosition( QTextCursor.EndOfBlock, QTextCursor.KeepAnchor) print("selected text :", doc_cursor.selectedText()) return doc_cursor.selectedText() doc_cursor = QTextCursor(self._doc) minimap_cursor = QTextCursor(self._minimap_doc) # IF one same block is modified if self._minimap_doc.blockCount() == self._doc.blockCount(): doc_cursor.setPosition(position) doc_cursor.select(QTextCursor.BlockUnderCursor) minimap_cursor.setPosition(position) minimap_cursor.select(QTextCursor.BlockUnderCursor) minimap_cursor.insertFragment(doc_cursor.selection()) # TODO: if the doc is modified on more than one block but resulting in # the same count of blocks (right now only the first block would be # updated) else: # ELSE doc_cursor.select(QTextCursor.Document) minimap_cursor.select(QTextCursor.Document) minimap_cursor.insertFragment(doc_cursor.selection())
def update_minimap_doc(self, position, charsRemoved, charsAdded): def select_blocks(first_block, last_block): first_block_pos = first_block.position() doc_cursor.setPosition(first_block_pos) last_block_pos = last_block.position() doc_cursor.setPosition(last_block_pos, QTextCursor.KeepAnchor) doc_cursor.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor) print("selected text :", doc_cursor.selectedText()) return doc_cursor.selectedText() doc_cursor = QTextCursor(self._doc) minimap_cursor = QTextCursor(self._minimap_doc) # IF one same block is modified if self._minimap_doc.blockCount() == self._doc.blockCount(): doc_cursor.setPosition(position) doc_cursor.select(QTextCursor.BlockUnderCursor) minimap_cursor.setPosition(position) minimap_cursor.select(QTextCursor.BlockUnderCursor) minimap_cursor.insertFragment(doc_cursor.selection()) # TODO: if the doc is modified on more than one block but resulting in # the same count of blocks (right now only the first block would be # updated) else: # ELSE doc_cursor.select(QTextCursor.Document) minimap_cursor.select(QTextCursor.Document) minimap_cursor.insertFragment(doc_cursor.selection())
def SaveAsPDF(self): """ Save Table as pdf """ fileName, _ = QFileDialog.getSaveFileName(self, "Save File", "", "PDF Files (*.pdf)") if fileName == "": return try: videoName = self.player.fileName timestamp = self.player.player.position() QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) rows = self.VManager.rowCount() columns = self.VManager.columnCount() printer = QPrinter(QPrinter.HighResolution) innerRect = printer.pageRect() sizeF = QSizeF(innerRect.size().width(), innerRect.size().height()) header = QTextDocument() header.setPageSize(sizeF) cursor_header = QTextCursor(header) format1 = QTextCharFormat() format1.setFontPointSize(16) cursor_header.insertHtml( "<p style='text-align: left;'><strong>Video</strong>: %s <strong> TimeStamp</strong>: %s </p>" % (videoName, timestamp)) cursor_header.insertHtml("<br><br><br> ") cursor_header.select(QTextCursor.Document) fragment_header = cursor_header.selection() document = QTextDocument() cursor = QTextCursor(document) tableFormat = QTextTableFormat() tableFormat.setHeaderRowCount(1) tableFormat.setBorderBrush(QBrush(Qt.black)) tableFormat.setAlignment(Qt.AlignHCenter) tableFormat.setCellPadding(5) tableFormat.setCellSpacing(5) cursor.movePosition(QTextCursor.Start, QTextCursor.MoveAnchor) cursor.insertFragment(fragment_header) textTable = cursor.insertTable(rows + 1, columns, tableFormat) tableHeaderFormat = QTextCharFormat() tableHeaderFormat.setBackground(QColor("#DADADA")) for column in range(columns): cell = textTable.cellAt(0, column) cell.setFormat(tableHeaderFormat) cellCursor = cell.firstCursorPosition() cellCursor.insertText( self.VManager.horizontalHeaderItem(column).data( Qt.DisplayRole)) for row in range(rows): for column in range(columns): item = self.VManager.item(row, column) if item is not None: cell = textTable.cellAt(row + 1, column) cellCursor = cell.firstCursorPosition() cellCursor.insertText( self.VManager.item(row, column).text()) cursor.movePosition(QTextCursor.End) printer.setOrientation(QPrinter.Portrait) printer.setPageMargins(30, 100, 10, 40, QPrinter.DevicePixel) printer.setFullPage(True) printer.setPageSize(QPrinter.A4) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(fileName) document.print_(printer) QApplication.restoreOverrideCursor() qgsu.showUserAndLogMessage( QCoreApplication.translate("QgsFmvMetadata", "Succesfully creating PDF")) except Exception as e: QApplication.restoreOverrideCursor() qgsu.showUserAndLogMessage(QCoreApplication.translate( "QgsFmvMetadata", "Failed creating PDF"), level=QGis.Warning) return return
class TextSectionEditor(QTextDocument): def __init__(self, sectionId: str, content="", pos=0, selectionStart=0, selectionEnd=0): super().__init__() self.dtb = DTB() self.sectionId = sectionId self.setDefaultStyleSheet(CSS) self.setHtml(content) self.s_start = selectionStart self.s_end = selectionEnd self.cur = QTextCursor(self) self.cur.setPosition(pos) self.result = { "text": "", "cursorPosition": self.pos, "eventAccepted": False } self.pending = False @property def len(self): return self.characterCount() @property def s_len(self): return abs(self.s_end - self.s_start) @property def pos(self): return self.cur.position() def onChange(self): self._update_ddb() self.setResponse(True) return self.result def onLoad(self): item = self.dtb.getDB("Section", self.sectionId) self.setHtml(item["text"]) self.setResponse(True, cur=self.len) return self.result def onMenu(self, style={}, **kwargs): backup = [self.pos, self.s_start, self.s_end] for k, v in style.items(): # un peut répétition mais uniquement sur des if alors ... if k == "fgColor": self._set_fg_color(v) elif k == "underline": # pragma: no branch self._set_underline(style["underline"]) self.cur.setPosition(backup[0]) self.s_start = backup[1] self.s_end = backup[2] if self.pending: # else: self._update_ddb() else: self.setResponse(False) return self.result def onKey(self, event): # on met en premier ceux à qui il faut passer l'event if event["key"] == Qt.Key_Return: self.do_key_return(event) elif event["modifiers"] == Qt.ControlModifier: self.do_control_modifier(event) else: self.setResponse(False) if self.pending: self._update_ddb() return self.result def do_control_modifier(self, event): if event == KeyW.KEY_1: self.do_key_1() elif event == KeyW.KEY_2: self.do_key_2() elif event == KeyW.KEY_3: self.do_key_3() elif event == KeyW.KEY_4: self.do_key_4() elif event["key"] == Qt.Key_U: # pragma: no branch self.do_key_u() def do_key_1(self): self._set_fg_color(BLACK) def do_key_2(self): self._set_fg_color(BLUE) def do_key_3(self): self._set_fg_color(GREEN) def do_key_4(self): self._set_fg_color(RED) def do_key_return(self, event): block = self.findBlock(self.pos) if event["modifiers"] == Qt.ControlModifier: self._appendEmptyBlock() elif event["modifiers"] == Qt.ShiftModifier: self._insertEmptyBlock() elif block.blockFormat().headingLevel(): self._appendEmptyBlock() else: if self._headerAutoFormat(): return else: self.setResponse(False) def do_key_u(self): self._set_underline("toggle") def setResponse(self, accepted, text=None, cur=None): self.result["text"] = text or self.toHtml() self.result["cursorPosition"] = cur or self.cur.position() self.result["eventAccepted"] = accepted def _appendEmptyBlock(self, section="p", pre_move=QTextCursor.EndOfBlock, set_response=True): self.cur.movePosition(pre_move) self.cur.insertBlock(blockFormat[section], blockCharFormat[section]) self.cur.insertFragment(QTextDocumentFragment.fromPlainText("")) self.pending = True if set_response: self.setResponse(True) def _insertEmptyBlock(self, section="p", pre_move=QTextCursor.StartOfBlock, set_response=True): old = self.cur.blockFormat().headingLevel() self._appendEmptyBlock(pre_move=pre_move, set_response=False) self._set_block_style(old) self.cur.movePosition(QTextCursor.PreviousBlock) self._set_block_style(section) self.pending = True if set_response: # pragma: no branch self.setResponse(True) def _headerAutoFormat(self): # on check les expressions régulières suivantes: # #, ##, ###, ####, ##### line = self.cur.block().text() matched = RE_AUTOPARAGRAPH_DEBUT.search(line) matched_at_start = False if not matched: matched = RE_AUTOPARAGRAPH_FIN.search(line) if not matched: return False else: matched_at_start = True # strip les # et applique les styles par défault level = len(matched.groups()[0]) if matched_at_start: text = self.cur.block().text()[level + 1:] else: text = self.cur.block().text()[:-(level + 1)] self.cur.beginEditBlock() self.cur.select(QTextCursor.LineUnderCursor) self.cur.setCharFormat(blockCharFormat[level]) self.cur.insertText(text) self.cur.setBlockFormat(blockFormat[level]) self.cur.endEditBlock() self._appendEmptyBlock() self.pending = True self.setResponse(True) return True @contextmanager def _merge_char_format(self): self._select_word_or_selection() f: QTextCharFormat = QTextCharFormat() yield f self.cur.mergeCharFormat(f) self.pending = True self.setResponse(True, cur=max(self.pos, self.s_start, self.s_end)) def _select_word_or_selection(self): if self.s_start < self.pos: self.cur.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, self.s_len) elif self.s_end > self.pos: self.cur.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor, self.s_len) else: self.cur.select(QTextCursor.WordUnderCursor) def _set_block_style(self, level): self.cur.setBlockFormat(blockFormat[level]) self.cur.setBlockCharFormat(blockCharFormat[level]) def _set_fg_color(self, color): with self._merge_char_format() as f: f.setForeground(QBrush(QColor(color))) def _set_underline(self, value): with self._merge_char_format() as f: if value == "toggle": value = not self.cur.charFormat().fontUnderline() f.setFontUnderline(value) def _update_ddb(self): new_body = TextSectionFormatter(self.toHtml()).build_body() self.dtb.setDB("Section", self.sectionId, {"text": new_body}) return new_body