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:
parent
9ebdc333a6
commit
699e8fe3a6
@ -43,6 +43,7 @@
|
|||||||
#define PRINTVIEW_H
|
#define PRINTVIEW_H
|
||||||
|
|
||||||
#include <QTableView>
|
#include <QTableView>
|
||||||
|
class QPrinter;
|
||||||
|
|
||||||
class PrintView : public QTableView
|
class PrintView : public QTableView
|
||||||
{
|
{
|
||||||
|
@ -52,6 +52,7 @@ QT_FORWARD_DECLARE_CLASS(QFontComboBox)
|
|||||||
QT_FORWARD_DECLARE_CLASS(QTextEdit)
|
QT_FORWARD_DECLARE_CLASS(QTextEdit)
|
||||||
QT_FORWARD_DECLARE_CLASS(QTextCharFormat)
|
QT_FORWARD_DECLARE_CLASS(QTextCharFormat)
|
||||||
QT_FORWARD_DECLARE_CLASS(QMenu)
|
QT_FORWARD_DECLARE_CLASS(QMenu)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QPrinter)
|
||||||
|
|
||||||
class TextEdit : public QMainWindow
|
class TextEdit : public QMainWindow
|
||||||
{
|
{
|
||||||
|
@ -5,7 +5,8 @@ HEADERS += \
|
|||||||
$$PWD/qprinter.h \
|
$$PWD/qprinter.h \
|
||||||
$$PWD/qprinter_p.h \
|
$$PWD/qprinter_p.h \
|
||||||
$$PWD/qprinterinfo.h \
|
$$PWD/qprinterinfo.h \
|
||||||
$$PWD/qprinterinfo_p.h
|
$$PWD/qprinterinfo_p.h \
|
||||||
|
$$PWD/qprintabletextdocument.h
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
$$PWD/qpaintengine_alpha.cpp \
|
$$PWD/qpaintengine_alpha.cpp \
|
||||||
@ -13,6 +14,7 @@ SOURCES += \
|
|||||||
$$PWD/qprintengine_pdf.cpp \
|
$$PWD/qprintengine_pdf.cpp \
|
||||||
$$PWD/qprinter.cpp \
|
$$PWD/qprinter.cpp \
|
||||||
$$PWD/qprinterinfo.cpp \
|
$$PWD/qprinterinfo.cpp \
|
||||||
|
$$PWD/qprintabletextdocument.cpp
|
||||||
|
|
||||||
unix:!symbian {
|
unix:!symbian {
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
|
@ -60,6 +60,7 @@
|
|||||||
#include "qprinter.h"
|
#include "qprinter.h"
|
||||||
#include "qtextdocumentwriter.h"
|
#include "qtextdocumentwriter.h"
|
||||||
#include "private/qtextcursor_p.h"
|
#include "private/qtextcursor_p.h"
|
||||||
|
#include "qpagedpaintdevice.h"
|
||||||
|
|
||||||
#include <qtextformat.h>
|
#include <qtextformat.h>
|
||||||
#include <qdatetime.h>
|
#include <qdatetime.h>
|
||||||
@ -2230,15 +2231,15 @@ bool QTextControl::isWordSelectionEnabled() const
|
|||||||
return d->wordSelectionEnabled;
|
return d->wordSelectionEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef QT_NO_PRINTER
|
void QTextControl::print(QPagedPaintDevice *printer) const
|
||||||
void QTextControl::print(QPrinter *printer) const
|
|
||||||
{
|
{
|
||||||
#ifndef QT_NO_PRINTER
|
|
||||||
Q_D(const QTextControl);
|
Q_D(const QTextControl);
|
||||||
if (!printer || !printer->isValid())
|
if (!printer)
|
||||||
return;
|
return;
|
||||||
QTextDocument *tempDoc = 0;
|
QTextDocument *tempDoc = 0;
|
||||||
const QTextDocument *doc = d->doc;
|
const QTextDocument *doc = d->doc;
|
||||||
|
// ####
|
||||||
|
#if 0
|
||||||
if (printer->printRange() == QPrinter::Selection) {
|
if (printer->printRange() == QPrinter::Selection) {
|
||||||
if (!d->cursor.hasSelection())
|
if (!d->cursor.hasSelection())
|
||||||
return;
|
return;
|
||||||
@ -2253,11 +2254,10 @@ void QTextControl::print(QPrinter *printer) const
|
|||||||
// copy the custom object handlers
|
// copy the custom object handlers
|
||||||
doc->documentLayout()->d_func()->handlers = d->doc->documentLayout()->d_func()->handlers;
|
doc->documentLayout()->d_func()->handlers = d->doc->documentLayout()->d_func()->handlers;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
doc->print(printer);
|
doc->print(printer);
|
||||||
delete tempDoc;
|
delete tempDoc;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
#endif // QT_NO_PRINTER
|
|
||||||
|
|
||||||
QMimeData *QTextControl::createMimeDataFromSelection() const
|
QMimeData *QTextControl::createMimeDataFromSelection() const
|
||||||
{
|
{
|
||||||
|
@ -75,6 +75,7 @@ class QMimeData;
|
|||||||
class QAbstractScrollArea;
|
class QAbstractScrollArea;
|
||||||
class QEvent;
|
class QEvent;
|
||||||
class QTimerEvent;
|
class QTimerEvent;
|
||||||
|
class QPagedPaintDevice;
|
||||||
|
|
||||||
class Q_GUI_EXPORT QTextControl : public QObject
|
class Q_GUI_EXPORT QTextControl : public QObject
|
||||||
{
|
{
|
||||||
@ -171,9 +172,7 @@ public:
|
|||||||
bool isWordSelectionEnabled() const;
|
bool isWordSelectionEnabled() const;
|
||||||
void setWordSelectionEnabled(bool enabled);
|
void setWordSelectionEnabled(bool enabled);
|
||||||
|
|
||||||
#ifndef QT_NO_PRINTER
|
void print(QPagedPaintDevice *printer) const;
|
||||||
void print(QPrinter *printer) const;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
virtual int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const;
|
virtual int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const;
|
||||||
virtual QRectF blockBoundingRect(const QTextBlock &block) const;
|
virtual QRectF blockBoundingRect(const QTextBlock &block) const;
|
||||||
|
@ -65,6 +65,7 @@
|
|||||||
#include "qtextdocument_p.h"
|
#include "qtextdocument_p.h"
|
||||||
#include <private/qprinter_p.h>
|
#include <private/qprinter_p.h>
|
||||||
#include <private/qabstracttextdocumentlayout_p.h>
|
#include <private/qabstracttextdocumentlayout_p.h>
|
||||||
|
#include "qpagedpaintdevice.h"
|
||||||
|
|
||||||
#include <limits.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.
|
set up before being used with this function.
|
||||||
|
|
||||||
This is only a convenience method to print the whole document to the printer.
|
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,
|
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
|
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
|
a 2 cm margin is set around the document contents. In addition the current page
|
||||||
number is printed at the bottom of each 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()
|
\sa QTextEdit::print()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void QTextDocument::print(QPrinter *printer) const
|
void QTextDocument::print(QPagedPaintDevice *printer) const
|
||||||
{
|
{
|
||||||
Q_D(const QTextDocument);
|
Q_D(const QTextDocument);
|
||||||
|
|
||||||
if (!printer || !printer->isValid())
|
if (!printer)
|
||||||
return;
|
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()
|
bool documentPaginated = d->pageSize.isValid() && !d->pageSize.isNull()
|
||||||
&& d->pageSize.height() != INT_MAX;
|
&& d->pageSize.height() != INT_MAX;
|
||||||
|
|
||||||
if (!documentPaginated && !printer->fullPage() && !printer->d_func()->hasCustomPageMargins)
|
// ### set page size to paginated size?
|
||||||
printer->setPageMargins(23.53, 23.53, 23.53, 23.53, QPrinter::Millimeter);
|
// if (!documentPaginated && !printer->fullPage() && !printer->d_func()->hasCustomPageMargins)
|
||||||
|
// printer->setPageMargins(23.53, 23.53, 23.53, 23.53, QPrinter::Millimeter);
|
||||||
|
|
||||||
QPainter p(printer);
|
QPainter p(printer);
|
||||||
|
|
||||||
@ -1765,7 +1763,7 @@ void QTextDocument::print(QPrinter *printer) const
|
|||||||
scaledPageSize.rwidth() *= dpiScaleX;
|
scaledPageSize.rwidth() *= dpiScaleX;
|
||||||
scaledPageSize.rheight() *= dpiScaleY;
|
scaledPageSize.rheight() *= dpiScaleY;
|
||||||
|
|
||||||
const QSizeF printerPageSize(printer->pageRect().size());
|
const QSizeF printerPageSize(printer->width(), printer->height());
|
||||||
|
|
||||||
// scale to page
|
// scale to page
|
||||||
p.scale(printerPageSize.width() / scaledPageSize.width(),
|
p.scale(printerPageSize.width() / scaledPageSize.width(),
|
||||||
@ -1787,17 +1785,12 @@ void QTextDocument::print(QPrinter *printer) const
|
|||||||
layout->d_func()->handlers = documentLayout()->d_func()->handlers;
|
layout->d_func()->handlers = documentLayout()->d_func()->handlers;
|
||||||
|
|
||||||
int dpiy = p.device()->logicalDpiY();
|
int dpiy = p.device()->logicalDpiY();
|
||||||
int margin = 0;
|
int margin = (int) ((2/2.54)*dpiy); // 2 cm margins
|
||||||
if (printer->fullPage() && !printer->d_func()->hasCustomPageMargins) {
|
QTextFrameFormat fmt = doc->rootFrame()->frameFormat();
|
||||||
// for compatibility
|
fmt.setMargin(margin);
|
||||||
margin = (int) ((2/2.54)*dpiy); // 2 cm margins
|
doc->rootFrame()->setFrameFormat(fmt);
|
||||||
QTextFrameFormat fmt = doc->rootFrame()->frameFormat();
|
|
||||||
fmt.setMargin(margin);
|
|
||||||
doc->rootFrame()->setFrameFormat(fmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
QRectF pageRect(printer->pageRect());
|
body = QRectF(0, 0, printer->width(), printer->height());
|
||||||
body = QRectF(0, 0, pageRect.width(), pageRect.height());
|
|
||||||
pageNumberPos = QPointF(body.width() - margin,
|
pageNumberPos = QPointF(body.width() - margin,
|
||||||
body.height() - margin
|
body.height() - margin
|
||||||
+ QFontMetrics(doc->defaultFont(), p.device()).ascent()
|
+ QFontMetrics(doc->defaultFont(), p.device()).ascent()
|
||||||
@ -1805,18 +1798,10 @@ void QTextDocument::print(QPrinter *printer) const
|
|||||||
clonedDoc->setPageSize(body.size());
|
clonedDoc->setPageSize(body.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
int docCopies;
|
int fromPage = 0;
|
||||||
int pageCopies;
|
int toPage = 0;
|
||||||
if (printer->collateCopies() == true){
|
// int fromPage = printer->fromPage();
|
||||||
docCopies = 1;
|
// int toPage = printer->toPage();
|
||||||
pageCopies = printer->supportsMultipleCopies() ? 1 : printer->copyCount();
|
|
||||||
} else {
|
|
||||||
docCopies = printer->supportsMultipleCopies() ? 1 : printer->copyCount();
|
|
||||||
pageCopies = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int fromPage = printer->fromPage();
|
|
||||||
int toPage = printer->toPage();
|
|
||||||
bool ascending = true;
|
bool ascending = true;
|
||||||
|
|
||||||
if (fromPage == 0 && toPage == 0) {
|
if (fromPage == 0 && toPage == 0) {
|
||||||
@ -1833,39 +1818,27 @@ void QTextDocument::print(QPrinter *printer) const
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (printer->pageOrder() == QPrinter::LastPageFirst) {
|
// if (printer->pageOrder() == QPrinter::LastPageFirst) {
|
||||||
int tmp = fromPage;
|
// int tmp = fromPage;
|
||||||
fromPage = toPage;
|
// fromPage = toPage;
|
||||||
toPage = tmp;
|
// toPage = tmp;
|
||||||
ascending = false;
|
// ascending = false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
for (int i = 0; i < docCopies; ++i) {
|
int page = fromPage;
|
||||||
|
while (true) {
|
||||||
|
printPage(page, &p, doc, body, pageNumberPos);
|
||||||
|
|
||||||
int page = fromPage;
|
if (page == toPage)
|
||||||
while (true) {
|
break;
|
||||||
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)
|
if (ascending)
|
||||||
break;
|
++page;
|
||||||
|
else
|
||||||
|
--page;
|
||||||
|
|
||||||
if (ascending)
|
if (!printer->newPage())
|
||||||
++page;
|
return;
|
||||||
else
|
|
||||||
--page;
|
|
||||||
|
|
||||||
printer->newPage();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( i < docCopies - 1)
|
|
||||||
printer->newPage();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -57,7 +57,7 @@ class QTextFormatCollection;
|
|||||||
class QTextListFormat;
|
class QTextListFormat;
|
||||||
class QRect;
|
class QRect;
|
||||||
class QPainter;
|
class QPainter;
|
||||||
class QPrinter;
|
class QPagedPaintDevice;
|
||||||
class QAbstractTextDocumentLayout;
|
class QAbstractTextDocumentLayout;
|
||||||
class QPoint;
|
class QPoint;
|
||||||
class QTextObject;
|
class QTextObject;
|
||||||
@ -206,9 +206,7 @@ public:
|
|||||||
|
|
||||||
bool isModified() const;
|
bool isModified() const;
|
||||||
|
|
||||||
#ifndef QT_NO_PRINTER
|
void print(QPagedPaintDevice *printer) const;
|
||||||
void print(QPrinter *printer) const;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
enum ResourceType {
|
enum ResourceType {
|
||||||
HtmlResource = 1,
|
HtmlResource = 1,
|
||||||
|
@ -2426,7 +2426,6 @@ bool QWidgetTextControl::isWordSelectionEnabled() const
|
|||||||
#ifndef QT_NO_PRINTER
|
#ifndef QT_NO_PRINTER
|
||||||
void QWidgetTextControl::print(QPrinter *printer) const
|
void QWidgetTextControl::print(QPrinter *printer) const
|
||||||
{
|
{
|
||||||
#ifndef QT_NO_PRINTER
|
|
||||||
Q_D(const QWidgetTextControl);
|
Q_D(const QWidgetTextControl);
|
||||||
if (!printer || !printer->isValid())
|
if (!printer || !printer->isValid())
|
||||||
return;
|
return;
|
||||||
@ -2448,7 +2447,6 @@ void QWidgetTextControl::print(QPrinter *printer) const
|
|||||||
}
|
}
|
||||||
doc->print(printer);
|
doc->print(printer);
|
||||||
delete tempDoc;
|
delete tempDoc;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
#endif // QT_NO_PRINTER
|
#endif // QT_NO_PRINTER
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user