Beispiel #1
0
    def drawContents(self, painter):
        """
        Reimplementation of drawContents to limit the drawing
        inside `textRext`.

        """
        painter.setPen(self.__color)
        painter.setFont(self.font())

        if self.__textRect:
            rect = self.__textRect
        else:
            rect = self.rect().adjusted(5, 5, -5, -5)
        if Qt.mightBeRichText(self.__message):
            doc = QTextDocument()
            doc.setHtml(self.__message)
            doc.setTextWidth(rect.width())
            cursor = QTextCursor(doc)
            cursor.select(QTextCursor.Document)
            fmt = QTextBlockFormat()
            fmt.setAlignment(self.__alignment)
            cursor.mergeBlockFormat(fmt)
            painter.save()
            painter.translate(rect.topLeft())
            doc.drawContents(painter)
            painter.restore()
        else:
            painter.drawText(rect, self.__alignment, self.__message)
Beispiel #2
0
    def make_plain_text(self):
        cursor = self.textCursor()

        char_format = QTextCharFormat()
        char_format.setFont(self.font)

        cursor.setCharFormat(char_format)

        block_format = QTextBlockFormat()
        block_format.setNonBreakableLines(False)
        cursor.setBlockFormat(block_format)
Beispiel #3
0
 def setFormat(self):
     format = QTextBlockFormat()
     format.setBackground(QtCore.Qt.yellow)
     return format
Beispiel #4
0
    def printDocument2(self):
        dialog = QPrintDialog()
        if not dialog.exec_():
            return
        self.printer = dialog.printer()
        headFormat = QTextBlockFormat()
        headFormat.setAlignment(Qt.AlignLeft)
        headFormat.setTextIndent(self.printer.pageRect().width() - 216)
        bodyFormat = QTextBlockFormat()
        bodyFormat.setAlignment(Qt.AlignJustify)
        lastParaBodyFormat = QTextBlockFormat(bodyFormat)
        lastParaBodyFormat.setPageBreakPolicy(
            QTextFormat.PageBreak_AlwaysAfter)
        rightBodyFormat = QTextBlockFormat()
        rightBodyFormat.setAlignment(Qt.AlignRight)
        headCharFormat = QTextCharFormat()
        headCharFormat.setFont(QFont("Helvetica", 10))
        bodyCharFormat = QTextCharFormat()
        bodyCharFormat.setFont(QFont("Times", 11))
        redBodyCharFormat = QTextCharFormat(bodyCharFormat)
        redBodyCharFormat.setForeground(Qt.red)
        tableFormat = QTextTableFormat()
        tableFormat.setBorder(1)
        tableFormat.setCellPadding(2)

        document = QTextDocument()
        cursor = QTextCursor(document)
        mainFrame = cursor.currentFrame()
        page = 1

        cursor.insertBlock(headFormat, headCharFormat)

        for text in ("Greasy Hands Ltd.", "New Lombard Street", "London",
                     "WC13", QDate.currentDate().toString(self.DATE_FORMAT)):
            cursor.insertBlock(headFormat, headCharFormat)
            cursor.insertText(text)

        cursor.insertBlock(bodyFormat, bodyCharFormat)
        cursor.insertText("Barrio Francisco Meza")

        cursor.insertBlock(bodyFormat)
        cursor.insertBlock(bodyFormat, bodyCharFormat)
        cursor.insertText("Dear Lyuis")
        cursor.insertBlock(bodyFormat)
        cursor.insertBlock(bodyFormat, bodyCharFormat)
        cursor.insertText(
            QString("The balance of your account is $ %L1.").arg(
                float(500.987), 0, "f", 2))

        cursor.insertBlock(bodyFormat, redBodyCharFormat)
        cursor.insertText("Please remit the amount")

        cursor.insertBlock(bodyFormat, bodyCharFormat)
        cursor.insertText("Transaction")

        transactions = [(QDate.currentDate(), 500), (QDate.currentDate(), 500),
                        (QDate.currentDate(), -500),
                        (QDate.currentDate(), 500)]

        table = cursor.insertTable(len(transactions), 3, tableFormat)

        row = 0
        for date, amount in transactions:
            cellCursor = table.cellAt(row, 0).firstCursorPosition()
            cellCursor.setBlockFormat(rightBodyFormat)
            cellCursor.insertText(date.toString(self.DATE_FORMAT),
                                  bodyCharFormat)

            cellCursor = table.cellAt(row, 1).firstCursorPosition()
            cellCursor.insertText("Credit", bodyCharFormat)

            cellCursor = table.cellAt(row, 2).firstCursorPosition()
            cellCursor.setBlockFormat(rightBodyFormat)

            cellCursor.insertText(
                QString("The balance of your account is $ %L1.").arg(
                    float(amount), 0, "f", 2), redBodyCharFormat)

            row += 1

        cursor.setPosition(mainFrame.lastPosition())
        cursor.insertBlock(bodyFormat, bodyCharFormat)
        cursor.insertText("We hope")
        document.print_(self.printer)