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 <qt_sanity_bot@ovi.com>
Reviewed-by: Gunnar Sletta <gunnar.sletta@nokia.com>
This commit is contained in:
Lars Knoll 2011-08-16 14:04:48 +02:00
parent 9ebdc333a6
commit 699e8fe3a6
8 changed files with 53 additions and 81 deletions

View File

@ -43,6 +43,7 @@
#define PRINTVIEW_H
#include <QTableView>
class QPrinter;
class PrintView : public QTableView
{

View File

@ -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
{

View File

@ -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 += \

View File

@ -60,6 +60,7 @@
#include "qprinter.h"
#include "qtextdocumentwriter.h"
#include "private/qtextcursor_p.h"
#include "qpagedpaintdevice.h"
#include <qtextformat.h>
#include <qdatetime.h>
@ -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
{

View File

@ -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;

View File

@ -65,6 +65,7 @@
#include "qtextdocument_p.h"
#include <private/qprinter_p.h>
#include <private/qabstracttextdocumentlayout_p.h>
#include "qpagedpaintdevice.h"
#include <limits.h>
@ -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;
}
// 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) {
printPage(page, &p, doc, body, pageNumberPos);
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();
}
if (page == toPage)
break;
if (page == toPage)
break;
if (ascending)
++page;
else
--page;
if (ascending)
++page;
else
--page;
printer->newPage();
}
if ( i < docCopies - 1)
printer->newPage();
if (!printer->newPage())
return;
}
}
#endif

View File

@ -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,

View File

@ -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