summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJohn Layt <jlayt@kde.org>2014-01-05 18:51:32 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-03-17 13:45:52 +0100
commit9e29fab38df831f33385ee25bff235a620aa20d5 (patch)
tree0fc8f608aa5124a370561462ff0515d41c4e712a /src
parent0c04b31d2715f68aa883107b4aa593fd95aefdfe (diff)
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>
Diffstat (limited to 'src')
-rw-r--r--src/gui/painting/qpdfwriter.cpp229
-rw-r--r--src/gui/painting/qpdfwriter.h12
2 files changed, 222 insertions, 19 deletions
diff --git a/src/gui/painting/qpdfwriter.cpp b/src/gui/painting/qpdfwriter.cpp
index a6f752b129..08c8f42fd9 100644
--- a/src/gui/painting/qpdfwriter.cpp
+++ b/src/gui/painting/qpdfwriter.cpp
@@ -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);
+ if (resolution > 0)
+ d->engine->setResolution(resolution);
+}
+
+/*!
+ \since 5.3
- QPagedPaintDevice::setPageSize(size);
- d->engine->setPageSize(QPageSize(QPageSize::PageSizeId(size)));
+ Returns the resolution of the PDF in DPI.
+
+ \sa setResolution()
+*/
+
+int QPdfWriter::resolution() const
+{
+ Q_D(const QPdfWriter);
+ return d->engine->resolution();
}
/*!
- \reimp
- */
-void QPdfWriter::setPageSizeMM(const QSizeF &size)
+ \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.
- QPagedPaintDevice::setPageSizeMM(size);
- d->engine->setPageSize(QPageSize(size, QPageSize::Millimeter));
+ \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
diff --git a/src/gui/painting/qpdfwriter.h b/src/gui/painting/qpdfwriter.h
index f5c25de5e9..fce0b88ea8 100644
--- a/src/gui/painting/qpdfwriter.h
+++ b/src/gui/painting/qpdfwriter.h
@@ -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);