From: Lars Knoll Date: Tue, 16 Aug 2011 12:04:48 +0000 (+0200) Subject: Use the paged paintdevice in the print() method of QTextDocument. X-Git-Tag: qt-v5.0.0-alpha1~3626^2~181 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=699e8fe3a6724dd8288dd6a55b25e070eb198c9d;p=profile%2Fivi%2Fqtbase.git Use the paged paintdevice in the print() method of QTextDocument. This is required to be able to move the printing system into it's own library. Change-Id: If70a55be4c4413f9cd917a30d1b368f32c1145e4 Reviewed-on: http://codereview.qt.nokia.com/3207 Reviewed-by: Qt Sanity Bot Reviewed-by: Gunnar Sletta --- diff --git a/examples/itemviews/spreadsheet/printview.h b/examples/itemviews/spreadsheet/printview.h index 7c74844..43a7bb3 100644 --- a/examples/itemviews/spreadsheet/printview.h +++ b/examples/itemviews/spreadsheet/printview.h @@ -43,6 +43,7 @@ #define PRINTVIEW_H #include +class QPrinter; class PrintView : public QTableView { diff --git a/examples/richtext/textedit/textedit.h b/examples/richtext/textedit/textedit.h index 6436178..ed20572 100644 --- a/examples/richtext/textedit/textedit.h +++ b/examples/richtext/textedit/textedit.h @@ -52,6 +52,7 @@ QT_FORWARD_DECLARE_CLASS(QFontComboBox) QT_FORWARD_DECLARE_CLASS(QTextEdit) QT_FORWARD_DECLARE_CLASS(QTextCharFormat) QT_FORWARD_DECLARE_CLASS(QMenu) +QT_FORWARD_DECLARE_CLASS(QPrinter) class TextEdit : public QMainWindow { diff --git a/src/gui/printsupport/printsupport.pri b/src/gui/printsupport/printsupport.pri index 2faf36f..b477321 100644 --- a/src/gui/printsupport/printsupport.pri +++ b/src/gui/printsupport/printsupport.pri @@ -5,7 +5,8 @@ HEADERS += \ $$PWD/qprinter.h \ $$PWD/qprinter_p.h \ $$PWD/qprinterinfo.h \ - $$PWD/qprinterinfo_p.h + $$PWD/qprinterinfo_p.h \ + $$PWD/qprintabletextdocument.h SOURCES += \ $$PWD/qpaintengine_alpha.cpp \ @@ -13,6 +14,7 @@ SOURCES += \ $$PWD/qprintengine_pdf.cpp \ $$PWD/qprinter.cpp \ $$PWD/qprinterinfo.cpp \ + $$PWD/qprintabletextdocument.cpp unix:!symbian { HEADERS += \ diff --git a/src/gui/text/qtextcontrol.cpp b/src/gui/text/qtextcontrol.cpp index 689fd43..0a4b61f 100644 --- a/src/gui/text/qtextcontrol.cpp +++ b/src/gui/text/qtextcontrol.cpp @@ -60,6 +60,7 @@ #include "qprinter.h" #include "qtextdocumentwriter.h" #include "private/qtextcursor_p.h" +#include "qpagedpaintdevice.h" #include #include @@ -2230,15 +2231,15 @@ bool QTextControl::isWordSelectionEnabled() const return d->wordSelectionEnabled; } -#ifndef QT_NO_PRINTER -void QTextControl::print(QPrinter *printer) const +void QTextControl::print(QPagedPaintDevice *printer) const { -#ifndef QT_NO_PRINTER Q_D(const QTextControl); - if (!printer || !printer->isValid()) + if (!printer) return; QTextDocument *tempDoc = 0; const QTextDocument *doc = d->doc; + // #### +#if 0 if (printer->printRange() == QPrinter::Selection) { if (!d->cursor.hasSelection()) return; @@ -2253,11 +2254,10 @@ void QTextControl::print(QPrinter *printer) const // copy the custom object handlers doc->documentLayout()->d_func()->handlers = d->doc->documentLayout()->d_func()->handlers; } +#endif doc->print(printer); delete tempDoc; -#endif } -#endif // QT_NO_PRINTER QMimeData *QTextControl::createMimeDataFromSelection() const { diff --git a/src/gui/text/qtextcontrol_p.h b/src/gui/text/qtextcontrol_p.h index 87eea3c..95c5f4d 100644 --- a/src/gui/text/qtextcontrol_p.h +++ b/src/gui/text/qtextcontrol_p.h @@ -75,6 +75,7 @@ class QMimeData; class QAbstractScrollArea; class QEvent; class QTimerEvent; +class QPagedPaintDevice; class Q_GUI_EXPORT QTextControl : public QObject { @@ -171,9 +172,7 @@ public: bool isWordSelectionEnabled() const; void setWordSelectionEnabled(bool enabled); -#ifndef QT_NO_PRINTER - void print(QPrinter *printer) const; -#endif + void print(QPagedPaintDevice *printer) const; virtual int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const; virtual QRectF blockBoundingRect(const QTextBlock &block) const; diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index 08b6e0b..c3dd72a 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -65,6 +65,7 @@ #include "qtextdocument_p.h" #include #include +#include "qpagedpaintdevice.h" #include @@ -1694,7 +1695,7 @@ static void printPage(int index, QPainter *painter, const QTextDocument *doc, co } /*! - Prints the document to the given \a printer. The QPrinter must be + Prints the document to the given \a device. The QPageablePaintDevice must be set up before being used with this function. This is only a convenience method to print the whole document to the printer. @@ -1704,33 +1705,30 @@ static void printPage(int index, QPainter *painter, const QTextDocument *doc, co 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 + multiple pages according to the size of the paint device'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. - \sa QTextEdit::print() */ -void QTextDocument::print(QPrinter *printer) const +void QTextDocument::print(QPagedPaintDevice *printer) const { Q_D(const QTextDocument); - if (!printer || !printer->isValid()) + if (!printer) return; - if (!d->title.isEmpty()) - printer->setDocName(d->title); + // ### +// 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); + // ### set page size to paginated size? +// if (!documentPaginated && !printer->fullPage() && !printer->d_func()->hasCustomPageMargins) +// printer->setPageMargins(23.53, 23.53, 23.53, 23.53, QPrinter::Millimeter); QPainter p(printer); @@ -1765,7 +1763,7 @@ void QTextDocument::print(QPrinter *printer) const scaledPageSize.rwidth() *= dpiScaleX; scaledPageSize.rheight() *= dpiScaleY; - const QSizeF printerPageSize(printer->pageRect().size()); + const QSizeF printerPageSize(printer->width(), printer->height()); // scale to page p.scale(printerPageSize.width() / scaledPageSize.width(), @@ -1787,17 +1785,12 @@ void QTextDocument::print(QPrinter *printer) const layout->d_func()->handlers = documentLayout()->d_func()->handlers; 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); - } + int 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()); + body = QRectF(0, 0, printer->width(), printer->height()); pageNumberPos = QPointF(body.width() - margin, body.height() - margin + QFontMetrics(doc->defaultFont(), p.device()).ascent() @@ -1805,18 +1798,10 @@ void QTextDocument::print(QPrinter *printer) const clonedDoc->setPageSize(body.size()); } - int docCopies; - int pageCopies; - if (printer->collateCopies() == true){ - docCopies = 1; - pageCopies = printer->supportsMultipleCopies() ? 1 : printer->copyCount(); - } else { - docCopies = printer->supportsMultipleCopies() ? 1 : printer->copyCount(); - pageCopies = 1; - } - - int fromPage = printer->fromPage(); - int toPage = printer->toPage(); + int fromPage = 0; + int toPage = 0; +// int fromPage = printer->fromPage(); +// int toPage = printer->toPage(); bool ascending = true; if (fromPage == 0 && toPage == 0) { @@ -1833,39 +1818,27 @@ void QTextDocument::print(QPrinter *printer) const return; } - if (printer->pageOrder() == QPrinter::LastPageFirst) { - int tmp = fromPage; - fromPage = toPage; - toPage = tmp; - ascending = false; - } - - for (int i = 0; i < docCopies; ++i) { +// if (printer->pageOrder() == QPrinter::LastPageFirst) { +// int tmp = fromPage; +// fromPage = toPage; +// toPage = tmp; +// ascending = false; +// } - int page = fromPage; - while (true) { - for (int j = 0; j < pageCopies; ++j) { - if (printer->printerState() == QPrinter::Aborted - || printer->printerState() == QPrinter::Error) - return; - printPage(page, &p, doc, body, pageNumberPos); - if (j < pageCopies - 1) - printer->newPage(); - } + int page = fromPage; + while (true) { + printPage(page, &p, doc, body, pageNumberPos); - if (page == toPage) - break; - - if (ascending) - ++page; - else - --page; + if (page == toPage) + break; - printer->newPage(); - } + if (ascending) + ++page; + else + --page; - if ( i < docCopies - 1) - printer->newPage(); + if (!printer->newPage()) + return; } } #endif diff --git a/src/gui/text/qtextdocument.h b/src/gui/text/qtextdocument.h index 268b72e..363d7ab 100644 --- a/src/gui/text/qtextdocument.h +++ b/src/gui/text/qtextdocument.h @@ -57,7 +57,7 @@ class QTextFormatCollection; class QTextListFormat; class QRect; class QPainter; -class QPrinter; +class QPagedPaintDevice; class QAbstractTextDocumentLayout; class QPoint; class QTextObject; @@ -206,9 +206,7 @@ public: bool isModified() const; -#ifndef QT_NO_PRINTER - void print(QPrinter *printer) const; -#endif + void print(QPagedPaintDevice *printer) const; enum ResourceType { HtmlResource = 1, diff --git a/src/widgets/widgets/qwidgettextcontrol.cpp b/src/widgets/widgets/qwidgettextcontrol.cpp index 462fe09..0f24113 100644 --- a/src/widgets/widgets/qwidgettextcontrol.cpp +++ b/src/widgets/widgets/qwidgettextcontrol.cpp @@ -2426,7 +2426,6 @@ bool QWidgetTextControl::isWordSelectionEnabled() const #ifndef QT_NO_PRINTER void QWidgetTextControl::print(QPrinter *printer) const { -#ifndef QT_NO_PRINTER Q_D(const QWidgetTextControl); if (!printer || !printer->isValid()) return; @@ -2448,7 +2447,6 @@ void QWidgetTextControl::print(QPrinter *printer) const } doc->print(printer); delete tempDoc; -#endif } #endif // QT_NO_PRINTER