QPdfWriter - Use QPageSize and QPageLayout

Add support to QPdfWriter for QPageSize, QPageLayout, and resolution.

[ChangeLog][QtGui][QPdfWriter] The QPdfWriter now supports setting
the PDF orientation, layout and resolution by using QPageSize and
QPageLayout.

Change-Id: I9c269f997ec540dac1989f355c6a2e7488947966
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
This commit is contained in:
John Layt 2014-01-05 18:51:32 +01:00 committed by The Qt Project
parent 0c04b31d27
commit 9e29fab38d
6 changed files with 499 additions and 20 deletions

View File

@ -90,6 +90,9 @@ QPdfWriter::QPdfWriter(const QString &filename)
Q_D(QPdfWriter);
d->engine->setOutputFilename(filename);
// Set QPagedPaintDevice layout to match the current paint engine layout
devicePageLayout() = d->engine->pageLayout();
}
/*!
@ -101,6 +104,9 @@ QPdfWriter::QPdfWriter(QIODevice *device)
Q_D(QPdfWriter);
d->engine->d_func()->outDevice = device;
// Set QPagedPaintDevice layout to match the current paint engine layout
devicePageLayout() = d->engine->pageLayout();
}
/*!
@ -147,7 +153,6 @@ void QPdfWriter::setCreator(const QString &creator)
d->engine->d_func()->creator = creator;
}
/*!
\reimp
*/
@ -159,25 +164,213 @@ QPaintEngine *QPdfWriter::paintEngine() const
}
/*!
\reimp
*/
void QPdfWriter::setPageSize(PageSize size)
\since 5.3
Sets the PDF \a resolution in DPI.
This setting affects the coordinate system as returned by, for
example QPainter::viewport().
\sa resolution()
*/
void QPdfWriter::setResolution(int resolution)
{
Q_D(const QPdfWriter);
QPagedPaintDevice::setPageSize(size);
d->engine->setPageSize(QPageSize(QPageSize::PageSizeId(size)));
if (resolution > 0)
d->engine->setResolution(resolution);
}
/*!
\reimp
*/
void QPdfWriter::setPageSizeMM(const QSizeF &size)
\since 5.3
Returns the resolution of the PDF in DPI.
\sa setResolution()
*/
int QPdfWriter::resolution() const
{
Q_D(const QPdfWriter);
return d->engine->resolution();
}
QPagedPaintDevice::setPageSizeMM(size);
d->engine->setPageSize(QPageSize(size, QPageSize::Millimeter));
/*!
\since 5.3
Sets the PDF page layout to \a newPageLayout.
You should call this before calling QPainter::begin(), or immediately
before calling newPage() to apply the new page layout to a new page.
You should not call any painting methods between a call to setPageLayout()
and newPage() as the wrong paint metrics may be used.
Returns true if the page layout was successfully set to \a newPageLayout.
\sa pageLayout()
*/
bool QPdfWriter::setPageLayout(const QPageLayout &newPageLayout)
{
Q_D(const QPdfWriter);
// Try to set the paint engine page layout
d->engine->setPageLayout(newPageLayout);
// Set QPagedPaintDevice layout to match the current paint engine layout
devicePageLayout() = d->engine->pageLayout();
return pageLayout().isEquivalentTo(newPageLayout);
}
/*!
\since 5.3
Sets the PDF page size to \a pageSize.
To get the current QPageSize use pageLayout().pageSize().
You should call this before calling QPainter::begin(), or immediately
before calling newPage() to apply the new page size to a new page.
You should not call any painting methods between a call to setPageSize()
and newPage() as the wrong paint metrics may be used.
Returns true if the page size was successfully set to \a pageSize.
\sa pageLayout()
*/
bool QPdfWriter::setPageSize(const QPageSize &pageSize)
{
Q_D(const QPdfWriter);
// Try to set the paint engine page size
d->engine->setPageSize(pageSize);
// Set QPagedPaintDevice layout to match the current paint engine layout
devicePageLayout() = d->engine->pageLayout();
return pageLayout().pageSize().isEquivalentTo(pageSize);
}
/*!
\since 5.3
Sets the PDF page \a orientation.
The page orientation is used to define the orientation of the
page size when obtaining the page rect.
You should call this before calling QPainter::begin(), or immediately
before calling newPage() to apply the new orientation to a new page.
You should not call any painting methods between a call to setPageOrientation()
and newPage() as the wrong paint metrics may be used.
To get the current QPageLayout::Orientation use pageLayout().pageOrientation().
Returns true if the page orientation was successfully set to \a orientation.
\sa pageLayout()
*/
bool QPdfWriter::setPageOrientation(QPageLayout::Orientation orientation)
{
Q_D(const QPdfWriter);
// Set the print engine value
d->engine->setPageOrientation(orientation);
// Set QPagedPaintDevice layout to match the current paint engine layout
devicePageLayout() = d->engine->pageLayout();
return pageLayout().orientation() == orientation;
}
/*!
\since 5.3
Set the PDF page \a margins in the current page layout units.
You should call this before calling QPainter::begin(), or immediately
before calling newPage() to apply the new margins to a new page.
You should not call any painting methods between a call to setPageMargins()
and newPage() as the wrong paint metrics may be used.
To get the current page margins use pageLayout().pageMargins().
Returns true if the page margins were successfully set to \a margins.
\sa pageLayout()
*/
bool QPdfWriter::setPageMargins(const QMarginsF &margins)
{
Q_D(const QPdfWriter);
// Try to set engine margins
d->engine->setPageMargins(margins, pageLayout().units());
// Set QPagedPaintDevice layout to match the current paint engine layout
devicePageLayout() = d->engine->pageLayout();
return pageLayout().margins() == margins;
}
/*!
\since 5.3
Set the PDF page \a margins defined in the given \a units.
You should call this before calling QPainter::begin(), or immediately
before calling newPage() to apply the new margins to a new page.
You should not call any painting methods between a call to setPageMargins()
and newPage() as the wrong paint metrics may be used.
To get the current page margins use pageLayout().pageMargins().
Returns true if the page margins were successfully set to \a margins.
\sa pageLayout()
*/
bool QPdfWriter::setPageMargins(const QMarginsF &margins, QPageLayout::Unit units)
{
Q_D(const QPdfWriter);
// Try to set engine margins
d->engine->setPageMargins(margins, units);
// Set QPagedPaintDevice layout to match the current paint engine layout
devicePageLayout() = d->engine->pageLayout();
return pageLayout().margins() == margins && pageLayout().units() == units;
}
/*!
Returns the current page layout. Use this method to access the current
QPageSize, QPageLayout::Orientation, QMarginsF, fullRect() and paintRect().
Note that you cannot use the setters on the returned object, you must either
call the individual QPdfWriter methods or use setPageLayout().
\sa setPageLayout(), setPageSize(), setPageOrientation(), setPageMargins()
*/
QPageLayout QPdfWriter::pageLayout() const
{
Q_D(const QPdfWriter);
return d->engine->pageLayout();
}
/*!
\reimp
\obsolete Use setPageSize(QPageSize(id)) instead
\sa setPageSize()
*/
void QPdfWriter::setPageSize(PageSize size)
{
setPageSize(QPageSize(QPageSize::PageSizeId(size)));
}
/*!
\reimp
\obsolete Use setPageSize(QPageSize(size, QPageSize::Millimeter)) instead
\sa setPageSize()
*/
void QPdfWriter::setPageSizeMM(const QSizeF &size)
{
setPageSize(QPageSize(size, QPageSize::Millimeter));
}
/*!
@ -203,17 +396,15 @@ bool QPdfWriter::newPage()
/*!
\reimp
\reimp
\obsolete Use setPageMargins(QMarginsF(l, t, r, b), QPageLayout::Millimeter) instead
\sa setPageMargins()
*/
void QPdfWriter::setMargins(const Margins &m)
{
Q_D(QPdfWriter);
QPagedPaintDevice::setMargins(m);
const qreal multiplier = 72./25.4;
d->engine->setPageMargins(QMarginsF(m.left * multiplier, m.top * multiplier,
m.right * multiplier, m.bottom * multiplier), QPageLayout::Point);
setPageMargins(QMarginsF(m.left, m.top, m.right, m.bottom), QPageLayout::Millimeter);
}
QT_END_NAMESPACE

View File

@ -48,6 +48,7 @@
#include <QtCore/qobject.h>
#include <QtGui/qpagedpaintdevice.h>
#include <QtGui/qpagelayout.h>
QT_BEGIN_NAMESPACE
@ -70,6 +71,17 @@ public:
bool newPage();
void setResolution(int resolution);
int resolution() const;
bool setPageLayout(const QPageLayout &pageLayout);
bool setPageSize(const QPageSize &pageSize);
bool setPageOrientation(QPageLayout::Orientation orientation);
bool setPageMargins(const QMarginsF &margins);
bool setPageMargins(const QMarginsF &margins, QPageLayout::Unit units);
QPageLayout pageLayout() const;
void setPageSize(PageSize size);
void setPageSizeMM(const QSizeF &size);

View File

@ -9,6 +9,7 @@ SUBDIRS=\
qpagesize \
qpainter \
qpathclipper \
qpdfwriter \
qpen \
qpaintengine \
qtransform \

View File

@ -0,0 +1 @@
tst_qpdfwriter

View File

@ -0,0 +1,9 @@
CONFIG += testcase
CONFIG += parallel_test
TARGET = tst_qpdfwriter
SOURCES += tst_qpdfwriter.cpp
QT += gui-private testlib
DEFINES += QT_USE_USING_NAMESPACE
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0

View File

@ -0,0 +1,265 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QtGlobal>
#include <QtAlgorithms>
#include <QtGui/QPageLayout>
#include <QtGui/QPdfWriter>
class tst_QPdfWriter : public QObject
{
Q_OBJECT
private slots:
void basics();
void testPageMetrics_data();
void testPageMetrics();
};
void tst_QPdfWriter::basics()
{
QTemporaryFile file;
if (!file.open())
QSKIP("Couldn't open temp file!");
QPdfWriter writer(file.fileName());
QCOMPARE(writer.title(), QString());
writer.setTitle(QString("Test Title"));
QCOMPARE(writer.title(), QString("Test Title"));
QCOMPARE(writer.creator(), QString());
writer.setCreator(QString("Test Creator"));
QCOMPARE(writer.creator(), QString("Test Creator"));
QCOMPARE(writer.resolution(), 1200);
writer.setResolution(600);
QCOMPARE(writer.resolution(), 600);
QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A4);
QCOMPARE(writer.pageSize(), QPdfWriter::A4);
QCOMPARE(writer.pageSizeMM(), QSizeF(210, 297));
writer.setPageSize(QPageSize(QPageSize::A5));
QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A5);
QCOMPARE(writer.pageSize(), QPdfWriter::A5);
QCOMPARE(writer.pageSizeMM(), QSizeF(148, 210));
writer.setPageSize(QPdfWriter::A3);
QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A3);
QCOMPARE(writer.pageSize(), QPdfWriter::A3);
QCOMPARE(writer.pageSizeMM(), QSizeF(297, 420));
writer.setPageSizeMM(QSize(210, 297));
QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A4);
QCOMPARE(writer.pageSize(), QPdfWriter::A4);
QCOMPARE(writer.pageSizeMM(), QSizeF(210, 297));
QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Portrait);
writer.setPageOrientation(QPageLayout::Landscape);
QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Landscape);
QCOMPARE(writer.pageSizeMM(), QSizeF(210, 297));
QCOMPARE(writer.pageLayout().margins(), QMarginsF(10, 10, 10, 10));
QCOMPARE(writer.pageLayout().units(), QPageLayout::Point);
QCOMPARE(writer.margins().left, 3.53); // mm
QCOMPARE(writer.margins().right, 3.53);
QCOMPARE(writer.margins().top, 3.53);
QCOMPARE(writer.margins().bottom, 3.53);
writer.setPageMargins(QMarginsF(20, 20, 20, 20), QPageLayout::Millimeter);
QCOMPARE(writer.pageLayout().margins(), QMarginsF(20, 20, 20, 20));
QCOMPARE(writer.pageLayout().units(), QPageLayout::Millimeter);
QCOMPARE(writer.margins().left, 20.0);
QCOMPARE(writer.margins().right, 20.0);
QCOMPARE(writer.margins().top, 20.0);
QCOMPARE(writer.margins().bottom, 20.0);
QPdfWriter::Margins margins = {50, 50, 50, 50};
writer.setMargins(margins);
QCOMPARE(writer.pageLayout().margins(), QMarginsF(50, 50, 50, 50));
QCOMPARE(writer.pageLayout().units(), QPageLayout::Millimeter);
QCOMPARE(writer.margins().left, 50.0);
QCOMPARE(writer.margins().right, 50.0);
QCOMPARE(writer.margins().top, 50.0);
QCOMPARE(writer.margins().bottom, 50.0);
QCOMPARE(writer.pageLayout().fullRect(QPageLayout::Millimeter), QRectF(0, 0, 297, 210));
QCOMPARE(writer.pageLayout().paintRect(QPageLayout::Millimeter), QRectF(50, 50, 197, 110));
}
// Test the old page metrics methods, see also QPrinter tests for the same.
void tst_QPdfWriter::testPageMetrics_data()
{
QTest::addColumn<int>("pageSize");
QTest::addColumn<qreal>("widthMMf");
QTest::addColumn<qreal>("heightMMf");
QTest::addColumn<bool>("setMargins");
QTest::addColumn<qreal>("leftMMf");
QTest::addColumn<qreal>("rightMMf");
QTest::addColumn<qreal>("topMMf");
QTest::addColumn<qreal>("bottomMMf");
QTest::newRow("A4") << int(QPdfWriter::A4) << 210.0 << 297.0 << false << 3.53 << 3.53 << 3.53 << 3.53;
QTest::newRow("A4 Margins") << int(QPdfWriter::A4) << 210.0 << 297.0 << true << 20.0 << 30.0 << 40.0 << 50.0;
QTest::newRow("Portrait") << -1 << 345.0 << 678.0 << false << 3.53 << 3.53 << 3.53 << 3.53;
QTest::newRow("Portrait Margins") << -1 << 345.0 << 678.0 << true << 20.0 << 30.0 << 40.0 << 50.0;
QTest::newRow("Landscape") << -1 << 678.0 << 345.0 << false << 3.53 << 3.53 << 3.53 << 3.53;
QTest::newRow("Landscape Margins") << -1 << 678.0 << 345.0 << true << 20.0 << 30.0 << 40.0 << 50.0;
}
void tst_QPdfWriter::testPageMetrics()
{
QFETCH(int, pageSize);
QFETCH(qreal, widthMMf);
QFETCH(qreal, heightMMf);
QFETCH(bool, setMargins);
QFETCH(qreal, leftMMf);
QFETCH(qreal, rightMMf);
QFETCH(qreal, topMMf);
QFETCH(qreal, bottomMMf);
QSizeF sizeMMf = QSizeF(widthMMf, heightMMf);
QTemporaryFile file;
if (!file.open())
QSKIP("Couldn't open temp file!");
QPdfWriter writer(file.fileName());
QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Portrait);
if (setMargins) {
// Setup the given margins
QPdfWriter::Margins margins;
margins.left = leftMMf;
margins.right = rightMMf;
margins.top = topMMf;
margins.bottom = bottomMMf;
writer.setMargins(margins);
QCOMPARE(writer.margins().left, leftMMf);
QCOMPARE(writer.margins().right, rightMMf);
QCOMPARE(writer.margins().top, topMMf);
QCOMPARE(writer.margins().bottom, bottomMMf);
}
// Set the given size, in Portrait mode
if (pageSize < 0) {
writer.setPageSizeMM(sizeMMf);
QCOMPARE(writer.pageSize(), QPdfWriter::Custom);
QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::Custom);
} else {
writer.setPageSize(QPdfWriter::PageSize(pageSize));
QCOMPARE(writer.pageSize(), QPdfWriter::PageSize(pageSize));
QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::PageSizeId(pageSize));
}
QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Portrait);
QCOMPARE(writer.margins().left, leftMMf);
QCOMPARE(writer.margins().right, rightMMf);
QCOMPARE(writer.margins().top, topMMf);
QCOMPARE(writer.margins().bottom, bottomMMf);
// QPagedPaintDevice::pageSizeMM() always returns Portrait
QCOMPARE(writer.pageSizeMM(), sizeMMf);
// QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation
QCOMPARE(writer.widthMM(), qRound(widthMMf - leftMMf - rightMMf));
QCOMPARE(writer.heightMM(), qRound(heightMMf - topMMf - bottomMMf));
// Now switch to Landscape mode, size should be unchanged, but rect and metrics should change
writer.setPageOrientation(QPageLayout::Landscape);
if (pageSize < 0) {
QCOMPARE(writer.pageSize(), QPdfWriter::Custom);
QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::Custom);
} else {
QCOMPARE(writer.pageSize(), QPdfWriter::PageSize(pageSize));
QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::PageSizeId(pageSize));
}
QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Landscape);
QCOMPARE(writer.margins().left, leftMMf);
QCOMPARE(writer.margins().right, rightMMf);
QCOMPARE(writer.margins().top, topMMf);
QCOMPARE(writer.margins().bottom, bottomMMf);
// QPagedPaintDevice::pageSizeMM() always returns Portrait
QCOMPARE(writer.pageSizeMM(), sizeMMf);
// QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation
QCOMPARE(writer.widthMM(), qRound(heightMMf - leftMMf - rightMMf));
QCOMPARE(writer.heightMM(), qRound(widthMMf - topMMf - bottomMMf));
// QPdfWriter::fullRect() always returns set orientation
QCOMPARE(writer.pageLayout().fullRect(QPageLayout::Millimeter), QRectF(0, 0, heightMMf, widthMMf));
// QPdfWriter::paintRect() always returns set orientation
QCOMPARE(writer.pageLayout().paintRect(QPageLayout::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf));
// Now while in Landscape mode, set the size again, results should be the same
if (pageSize < 0) {
writer.setPageSizeMM(sizeMMf);
QCOMPARE(writer.pageSize(), QPdfWriter::Custom);
QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::Custom);
} else {
writer.setPageSize(QPdfWriter::PageSize(pageSize));
QCOMPARE(writer.pageSize(), QPdfWriter::PageSize(pageSize));
QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::PageSizeId(pageSize));
}
QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Landscape);
QCOMPARE(writer.margins().left, leftMMf);
QCOMPARE(writer.margins().right, rightMMf);
QCOMPARE(writer.margins().top, topMMf);
QCOMPARE(writer.margins().bottom, bottomMMf);
// QPagedPaintDevice::pageSizeMM() always returns Portrait
QCOMPARE(writer.pageSizeMM(), sizeMMf);
// QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation
QCOMPARE(writer.widthMM(), qRound(heightMMf - leftMMf - rightMMf));
QCOMPARE(writer.heightMM(), qRound(widthMMf - topMMf - bottomMMf));
// QPdfWriter::fullRect() always returns set orientation
QCOMPARE(writer.pageLayout().fullRect(QPageLayout::Millimeter), QRectF(0, 0, heightMMf, widthMMf));
// QPdfWriter::paintRect() always returns set orientation
QCOMPARE(writer.pageLayout().paintRect(QPageLayout::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf));
}
QTEST_MAIN(tst_QPdfWriter)
#include "tst_qpdfwriter.moc"