Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

void QTextDocument::print ( QPrinter printer  )  const

Prints the document to the given printer. The QPrinter must be set up before being used with this function.

This is only a convenience method to print the whole document to the printer.

If the document is already paginated through a specified height in the pageSize() property it is printed as-is.

If the document is not paginated, like for example a document used in a QTextEdit, then a temporary copy of the document is created and the copy is broken into multiple pages according to the size of the QPrinter's paperRect(). By default a 2 cm margin is set around the document contents. In addition the current page number is printed at the bottom of each page.

Note that QPrinter::Selection is not supported as print range with this function since the selection is a property of QTextCursor. If you have a QTextEdit associated with your QTextDocument then you can use QTextEdit's print() function because QTextEdit has access to the user's selection.

See also:
QTextEdit::print()

Definition at line 1504 of file qtextdocument.cpp.

References clone(), QPrinter::collateCopies(), defaultFont, QPainter::device(), documentLayout(), firstBlock(), QTextFrame::frameFormat(), QPrinter::fromPage(), QPrinter::fullPage(), QSizeF::height(), QPainter::isActive(), QTextBlock::isValid(), QPrinter::newPage(), QPrinter::numCopies(), pageCount(), QPrinter::pageOrder(), QPrinter::pageRect(), QAbstractTextDocumentLayout::paintDevice(), QPrinter::printerState(), QSizeF::rheight(), rootFrame(), QSizeF::rwidth(), QPainter::scale(), QPrinter::setDocName(), QTextFrame::setFrameFormat(), QTextFrameFormat::setMargin(), QPrinter::setPageMargins(), setPageSize(), QAbstractTextDocumentLayout::setPaintDevice(), QRect::size(), QPrinter::toPage(), and QSizeF::width().

Referenced by TextEdit::filePrintPdf(), MainWindow::print(), and MainWindow::printFile().

{
    Q_D(const QTextDocument);

    if (!d->title.isEmpty())
        printer->setDocName(d->title);

    bool documentPaginated = d->pageSize.isValid() && !d->pageSize.isNull()
                             && d->pageSize.height() != INT_MAX;

    if (!documentPaginated && !printer->fullPage() && !printer->d_func()->hasCustomPageMargins)
        printer->setPageMargins(23.53, 23.53, 23.53, 23.53, QPrinter::Millimeter);

    QPainter p(printer);

    // Check that there is a valid device to print to.
    if (!p.isActive())
        return;

    const QTextDocument *doc = this;
    QTextDocument *clonedDoc = 0;
    (void)doc->documentLayout(); // make sure that there is a layout

    QRectF body = QRectF(QPointF(0, 0), d->pageSize);
    QPointF pageNumberPos;

    if (documentPaginated) {
        qreal sourceDpiX = qt_defaultDpi();
        qreal sourceDpiY = sourceDpiX;

        QPaintDevice *dev = doc->documentLayout()->paintDevice();
        if (dev) {
            sourceDpiX = dev->logicalDpiX();
            sourceDpiY = dev->logicalDpiY();
        }

        const qreal dpiScaleX = qreal(printer->logicalDpiX()) / sourceDpiX;
        const qreal dpiScaleY = qreal(printer->logicalDpiY()) / sourceDpiY;

        // scale to dpi
        p.scale(dpiScaleX, dpiScaleY);

        QSizeF scaledPageSize = d->pageSize;
        scaledPageSize.rwidth() *= dpiScaleX;
        scaledPageSize.rheight() *= dpiScaleY;

        const QSizeF printerPageSize(printer->pageRect().size());

        // scale to page
        p.scale(printerPageSize.width() / scaledPageSize.width(),
                printerPageSize.height() / scaledPageSize.height());
    } else {
        doc = clone(const_cast<QTextDocument *>(this));
        clonedDoc = const_cast<QTextDocument *>(doc);

        for (QTextBlock srcBlock = firstBlock(), dstBlock = clonedDoc->firstBlock();
             srcBlock.isValid() && dstBlock.isValid();
             srcBlock = srcBlock.next(), dstBlock = dstBlock.next()) {
            dstBlock.layout()->setAdditionalFormats(srcBlock.layout()->additionalFormats());
        }

        QAbstractTextDocumentLayout *layout = doc->documentLayout();
        layout->setPaintDevice(p.device());

        int dpiy = p.device()->logicalDpiY();
        int margin = 0;
        if (printer->fullPage() && !printer->d_func()->hasCustomPageMargins) {
            // for compatibility
            margin = (int) ((2/2.54)*dpiy); // 2 cm margins
            QTextFrameFormat fmt = doc->rootFrame()->frameFormat();
            fmt.setMargin(margin);
            doc->rootFrame()->setFrameFormat(fmt);
        }

        QRectF pageRect(printer->pageRect());
        body = QRectF(0, 0, pageRect.width(), pageRect.height());
        pageNumberPos = QPointF(body.width() - margin,
                                body.height() - margin
                                + QFontMetrics(doc->defaultFont(), p.device()).ascent()
                                + 5 * dpiy / 72.0);
        clonedDoc->setPageSize(body.size());
    }

    int docCopies;
    int pageCopies;
    if (printer->collateCopies() == true){
        docCopies = 1;
        pageCopies = printer->numCopies();
    } else {
        docCopies = printer->numCopies();
        pageCopies = 1;
    }

    int fromPage = printer->fromPage();
    int toPage = printer->toPage();
    bool ascending = true;

    if (fromPage == 0 && toPage == 0) {
        fromPage = 1;
        toPage = doc->pageCount();
    }
    // paranoia check
    fromPage = qMax(1, fromPage);
    toPage = qMin(doc->pageCount(), toPage);

    if (printer->pageOrder() == QPrinter::LastPageFirst) {
        int tmp = fromPage;
        fromPage = toPage;
        toPage = tmp;
        ascending = false;
    }

    for (int i = 0; i < docCopies; ++i) {

        int page = fromPage;
        while (true) {
            for (int j = 0; j < pageCopies; ++j) {
                if (printer->printerState() == QPrinter::Aborted
                    || printer->printerState() == QPrinter::Error)
                    goto UserCanceled;
                printPage(page, &p, doc, body, pageNumberPos);
                if (j < pageCopies - 1)
                    printer->newPage();
            }

            if (page == toPage)
                break;

            if (ascending)
                ++page;
            else
                --page;

            printer->newPage();
        }

        if ( i < docCopies - 1)
            printer->newPage();
    }

UserCanceled:
    delete clonedDoc;
}


Generated by  Doxygen 1.6.0   Back to index