def build_invoice(self, data): document = QTextDocument() self.setDocument(document) document.setPageSize(QSizeF(self.doc_width, self.doc_height)) document.setDefaultFont(font) cursor = QTextCursor(document) cursor.insertText(f"Customer Name: {data['c_name']}\n") cursor.insertText(f"Customer Address: {data['c_addr']}\n") cursor.insertText(f"Date: {data['i_date']}\n") cursor.insertText(f"Total Due: {data['total_due']}\n") # + return document # +++
def sizeHint(self, option, index): transaction_type = index.data(Qt.DisplayRole) fontMetrics = option.fontMetrics document = QTextDocument("W") option.font.setWeight(QFont.Bold) document.setDefaultFont(option.font) w = document.idealWidth() h = fontMetrics.height() if (transaction_type == TransactionType.Dividend) or (transaction_type == TransactionType.Trade): h = h * 2 return QSize(w, h)
def draw(cls, x, y, self: PreparedText, painter: QPainter): font = QFont(self.fontName) if self.fontSize: font.setPointSizeF(self.fontSize) if self.fontBold: font.setBold(True) if self.fontItalic: font.setItalic(True) brushStyle = self.brushStyle or Qt.BrushStyle.SolidPattern w = self.width h = self.height tx = self.left + x ty = self.top + y if self.border: w -= self.border.width h -= self.border.width * 2 tx += self.border.width ty += self.border.width rect = QRectF(tx, ty, w, h) if self.backColor: painter.setBrush(brush_style_map[brushStyle]) painter.fillRect(rect, QColor('#' + hex(self.backColor)[2:])) if self.allowTags: doc = QTextDocument() doc.setDefaultFont(font) doc.setHtml(self.text) doc.setDocumentMargin(0) painter.save() painter.translate(tx + 2, ty + 1) doc.drawContents(painter, QRectF(0, 0, self.width, self.height)) painter.restore() else: painter.save() painter.setFont(font) flags = cls.textFlags(self) rect.setX(rect.x() + 2) rect.setY(rect.y() + 1) painter.drawText(rect, flags, self.text) painter.restore() if self.border and self.border.color is not None: old_pen = painter.pen() pen = QPen( QColor(self.border.color), self.border.width, pen_style_map.get(self.border.style, Qt.PenStyle.SolidLine)) painter.setPen(pen) painter.drawLines( cls.getLines(self, self.left + x, self.top + y, self.left + self.width + x, self.top + y + self.height)) painter.setPen(old_pen)
def print(self, printer: QPrinter) -> None: print_options = PrintOptions( print_no=self.ui.check_box_no_column.isChecked(), print_code=self.ui.check_box_code_column.isChecked(), print_description=self.ui.check_box_details_column.isChecked(), print_list_price=self.ui.check_box_list_price_column.isChecked(), print_discount=self.ui.check_box_discount_column.isChecked(), print_price=self.ui.check_box_price_column.isChecked(), print_quantity=self.ui.check_box_quantity_column.isChecked(), print_total=self.ui.check_box_total_column.isChecked() ) margin = 5 printer.setPageSize(QPrinter.A4) printer.setPageMargins(margin, margin, margin, margin, QPrinter.Millimeter) printer.setResolution(96) doc = QTextDocument() doc.setUseDesignMetrics(True) doc.setDefaultFont(self.offer_font) doc.setHtml(self.offer.printout(print_options)) doc.setPageSize(printer.pageRect().size()) doc.print_(printer)
class QBlockCode(QCachedGraphicsItem): """ Top-level code widget for a selection of text. Will construct an AST using QBlockCodeObj, mirroring the structure associated with the target object. This text is then rendered using a QTextDocument, with appropriate styles applied to it. Interaction events will be propagated to corresponding objects. """ GRAPH_ADDR_SPACING = 20 addr: int _addr_str: str obj: QBlockCodeObj _config: ConfigurationManager disasm_view: 'QDisassemblyBaseControl' workspace: 'Workspace' infodock: InfoDock parent: Any def __init__(self, addr: int, obj: QBlockCodeObj, config: ConfigurationManager, disasm_view: 'QDisassemblyBaseControl', workspace: 'Workspace', infodock: InfoDock, parent: Any = None): super().__init__(parent=parent) self.addr = addr self._addr_str = "%08x" % self.addr self._addr_item: QGraphicsSimpleTextItem = None self.obj = obj self._width = 0 self._height = 0 self._config = config self.parent = parent self.workspace = workspace self.infodock = infodock self._disasm_view = disasm_view self._qtextdoc = QTextDocument() self._qtextdoc.setDefaultFont(self._config.disasm_font) self._qtextdoc.setDocumentMargin(0) self._addr_item = QGraphicsSimpleTextItem(self._addr_str, self) self._addr_item.setBrush(Conf.disasm_view_node_address_color) self._addr_item.setFont(Conf.disasm_font) self.update_document() self.setToolTip("Address: " + self._addr_str) self.refresh() def refresh(self): self._addr_item.setVisible(self._disasm_view.show_address) self._layout_items_and_update_size() def update_document(self): self._qtextdoc.clear() cur = QTextCursor(self._qtextdoc) self.obj.render_to_doc(cur) def paint(self, painter, option, widget): #pylint: disable=unused-argument self.update_document() painter.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform | QPainter.HighQualityAntialiasing) painter.setFont(self._config.disasm_font) if self.infodock.is_instruction_selected( self.addr) or self.obj.should_highlight_line: highlight_color = Conf.disasm_view_node_instruction_selected_background_color painter.setBrush(highlight_color) painter.setPen(highlight_color) painter.drawRect(0, 0, self.width, self.height) x = 0 if self._disasm_view.show_address: x += self._addr_item.boundingRect().width( ) + self.GRAPH_ADDR_SPACING painter.translate(QPointF(x, 0)) self._qtextdoc.drawContents(painter) # # Event handlers # def get_obj_for_mouse_event(self, event: QMouseEvent) -> QBlockCodeObj: p = event.pos() if self._disasm_view.show_address: offset = self._addr_item.boundingRect().width( ) + self.GRAPH_ADDR_SPACING p.setX(p.x() - offset) if p.x() >= 0: hitpos = self._qtextdoc.documentLayout().hitTest( p, Qt.HitTestAccuracy.ExactHit) if hitpos >= 0: return self.obj.get_hit_obj(hitpos) return None def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.infodock.select_instruction(self.addr) obj = self.get_obj_for_mouse_event(event) if obj is not None: obj.mousePressEvent(event) def mouseDoubleClickEvent(self, event): obj = self.get_obj_for_mouse_event(event) if obj is not None: obj.mouseDoubleClickEvent(event) # # Private methods # def _layout_items_and_update_size(self): self.update_document() x, y = 0, 0 if self._disasm_view.show_address: self._addr_item.setPos(x, y) x += self._addr_item.boundingRect().width( ) + self.GRAPH_ADDR_SPACING x += self._qtextdoc.size().width() y += self._qtextdoc.size().height() self._width = x self._height = y self.recalculate_size() def _boundingRect(self): return QRectF(0, 0, self._width, self._height)
def __init__(self, fileName=None): super(MainWindow, self).__init__() self.curFile = '' self.curPage = ( 0, 0 ) # Tuple containing current page and total pages of the current file self.setWindowTitle('Pablo Editor') self.setWindowIcon(QIcon( GenUtils.resource_path('src/images/icon.ico'))) self.setWindowState(Qt.WindowFullScreen) self.setWindowState(Qt.WindowMaximized) # available_geometry = app.desktop().availableGeometry(self) # self.resize(available_geometry.width(), available_geometry.height()) self.readSettings() self.dropView = dropview.DropView() self.paged_text_edit = pagedtextedit.PagedTextEdit(self.dropView) # The textedit must be transparent; the white pages are painted in paintEvent() function self.paged_text_edit.setStyleSheet( "QTextEdit { background-color: transparent }") self.paged_text_edit.setFrameStyle( QFrame.NoFrame) # Removes a border-like line around the TextEdit doc = QTextDocument() font = QFont() font.setPointSize(12) font.setFamily('Calibri') doc.setDefaultFont(font) # doc.setDefaultStyleSheet("body { font-color: black; font-family: Calibri; font-size: 12;} ") self.paged_text_edit.setDocument(doc) self.paged_text_edit.setPageFormat(QPageSize.A5Extra) self.paged_text_edit.setPageMargins(QMarginsF(15, 15, 15, 15)) self.paged_text_edit.setUsePageMode(True) self.paged_text_edit.setPageNumbersAlignment(Qt.AlignBottom | Qt.AlignCenter) self.text_edit_layout = QHBoxLayout() self.text_edit_layout.setAlignment(Qt.AlignTop | Qt.AlignLeft) self.text_edit_layout.setMargin(0) self.paged_text_edit.setLayout(self.text_edit_layout) # This below block of code prevents undoing the setDocumentMargin() and setFrameformat() # methods in the aboutUpdateDocumentGeometry function self.paged_text_edit.aboutUpdateDocumentGeometry() # self.paged_text_edit.document().clearUndoRedoStack() # this does not work # These commands work self.paged_text_edit.document().setUndoRedoEnabled(False) self.paged_text_edit.document().setUndoRedoEnabled(True) self.setCentralWidget(self.dropView) self.dropView.addWidget(self.paged_text_edit) self.dropView.setCurrentWidget(self.paged_text_edit) self._setup_components() self.setCurrentFile('') self.paged_text_edit.document().contentsChanged.connect( self.documentWasModified) self.paged_text_edit.currentCharFormatChanged.connect( self.updateFontWidgets) self.paged_text_edit.cursorPositionChanged.connect( self.updatePositions) self.paged_text_edit.pageInfo.connect(self.readPageInfo) self.statusBar.writeMessageOnStatus("Ready", 10000)
def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex): """ Displays dirty files in red with corresponding rebuild buttons if in developer mode (is_dev_mode). Otherwise, renders normally Args: painter (QPainter): Current painter option (QStyleOptionViewItem): Current option index (QModelIndex): Current index of related model """ if self.is_dev_mode: source_model = self.get_source_model(index.model(), self.source_model_type) model = index.model() # get data of filename filename = str( model.data( model.sibling(index.row(), source_model.FILENAME, index))) if source_model.is_file_dirty(filename): if index.column() == source_model.FILENAME: text = filename palette = option.palette document = QTextDocument() document.setDefaultFont(option.font) document.setHtml(f"<font color={'red'}>{text}</font>") background_color = palette.base().color() painter.save() painter.fillRect(option.rect, background_color) painter.translate(option.rect.x(), option.rect.y()) document.drawContents(painter) painter.restore() elif index.column() == source_model.REBUILD: if '.py' in filename: text = "rebuild" palette = option.palette document = QTextDocument() # needed to add Right Alignment: qt bug : # https://bugreports.qt.io/browse/QTBUG-22851 document.setTextWidth(option.rect.width()) text_options = document.defaultTextOption() text_options.setTextDirection(Qt.RightToLeft) document.setDefaultTextOption( text_options) # get right alignment document.setDefaultFont(option.font) document.setHtml( f'<font color={"red"}> <b> {text}</b> </font>') background_color = palette.base().color() painter.save() painter.fillRect(option.rect, background_color) painter.translate(option.rect.x(), option.rect.y()) document.drawContents(painter) painter.restore() else: QItemDelegate.paint(self, painter, option, index) else: QItemDelegate.paint(self, painter, option, index)