From bc46b591b2cb81a5cb0c513aa4139b7e88fa8b06 Mon Sep 17 00:00:00 2001 From: John Layt Date: Fri, 28 Mar 2014 10:58:25 +0100 Subject: QPagedPaintDevice - Move QPageLayout methods The new QPageLayout methods weren't originally added to QPagePaintDevice as no new virtuals can be added, instead static polymorphism was used to add the methods directly in the derived classes QPdfWriter and QPrinter. This however means that classes like QTextDocument with print() methods that take a QPagedPaintDevice are unable to access the QPageLayout methods. To fix this, instead make the QPagedPaintDevicePrivate a virtual class and have QPdfWriter and QPrinter implement derived private classes that are called by the non-virtual QPagedPaintDevice base methods. Change-Id: Ieb6e513b1fa05f5ae76ea1f9156b0b1a053089eb Reviewed-by: Lars Knoll --- src/gui/painting/qpdfwriter.cpp | 129 ++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 57 deletions(-) (limited to 'src/gui/painting/qpdfwriter.cpp') diff --git a/src/gui/painting/qpdfwriter.cpp b/src/gui/painting/qpdfwriter.cpp index 08c8f42fd9..b856d6625c 100644 --- a/src/gui/painting/qpdfwriter.cpp +++ b/src/gui/painting/qpdfwriter.cpp @@ -43,6 +43,7 @@ #ifndef QT_NO_PDF +#include "qpagedpaintdevice_p.h" #include #include "private/qpdf_p.h" #include @@ -68,6 +69,64 @@ public: QFile *output; }; +class QPdfPagedPaintDevicePrivate : public QPagedPaintDevicePrivate +{ +public: + QPdfPagedPaintDevicePrivate(QPdfWriterPrivate *d) + : QPagedPaintDevicePrivate(), pd(d) + {} + + virtual ~QPdfPagedPaintDevicePrivate() + {} + + bool setPageLayout(const QPageLayout &newPageLayout) Q_DECL_OVERRIDE + { + // Try to set the paint engine page layout + pd->engine->setPageLayout(newPageLayout); + // Set QPagedPaintDevice layout to match the current paint engine layout + m_pageLayout = pd->engine->pageLayout(); + return m_pageLayout.isEquivalentTo(newPageLayout); + } + + bool setPageSize(const QPageSize &pageSize) Q_DECL_OVERRIDE + { + // Try to set the paint engine page size + pd->engine->setPageSize(pageSize); + // Set QPagedPaintDevice layout to match the current paint engine layout + m_pageLayout = pd->engine->pageLayout(); + return m_pageLayout.pageSize().isEquivalentTo(pageSize); + } + + bool setPageOrientation(QPageLayout::Orientation orientation) Q_DECL_OVERRIDE + { + // Set the print engine value + pd->engine->setPageOrientation(orientation); + // Set QPagedPaintDevice layout to match the current paint engine layout + m_pageLayout = pd->engine->pageLayout(); + return m_pageLayout.orientation() == orientation; + } + + bool setPageMargins(const QMarginsF &margins) Q_DECL_OVERRIDE + { + return setPageMargins(margins, pageLayout().units()); + } + + bool setPageMargins(const QMarginsF &margins, QPageLayout::Unit units) Q_DECL_OVERRIDE + { + // Try to set engine margins + pd->engine->setPageMargins(margins, units); + // Set QPagedPaintDevice layout to match the current paint engine layout + m_pageLayout = pd->engine->pageLayout(); + return m_pageLayout.margins() == margins && m_pageLayout.units() == units; + } + + QPageLayout pageLayout() const Q_DECL_OVERRIDE + { + return pd->engine->pageLayout(); + } + + QPdfWriterPrivate *pd; +}; /*! \class QPdfWriter \inmodule QtGui @@ -85,7 +144,8 @@ public: Constructs a PDF writer that will write the pdf to \a filename. */ QPdfWriter::QPdfWriter(const QString &filename) - : QObject(*new QPdfWriterPrivate) + : QObject(*new QPdfWriterPrivate), + QPagedPaintDevice(new QPdfPagedPaintDevicePrivate(d_func())) { Q_D(QPdfWriter); @@ -195,7 +255,10 @@ int QPdfWriter::resolution() const return d->engine->resolution(); } +// Defined in QPagedPaintDevice but non-virtual, add QPdfWriter specific doc here +#ifdef Q_QDOC /*! + \fn bool QPdfWriter::setPageLayout(const QPageLayout &newPageLayout) \since 5.3 Sets the PDF page layout to \a newPageLayout. @@ -210,17 +273,8 @@ int QPdfWriter::resolution() const \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); -} - /*! + \fn bool QPdfWriter::setPageSize(const QPageSize &pageSize) \since 5.3 Sets the PDF page size to \a pageSize. @@ -237,17 +291,8 @@ bool QPdfWriter::setPageLayout(const QPageLayout &newPageLayout) \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); -} - /*! + \fn bool QPdfWriter::setPageOrientation(QPageLayout::Orientation orientation) \since 5.3 Sets the PDF page \a orientation. @@ -267,17 +312,8 @@ bool QPdfWriter::setPageSize(const QPageSize &pageSize) \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; -} - /*! + \fn bool QPdfWriter::setPageMargins(const QMarginsF &margins) \since 5.3 Set the PDF page \a margins in the current page layout units. @@ -294,17 +330,8 @@ bool QPdfWriter::setPageOrientation(QPageLayout::Orientation orientation) \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; -} - /*! + \fn bool QPdfWriter::setPageMargins(const QMarginsF &margins, QPageLayout::Unit units) \since 5.3 Set the PDF page \a margins defined in the given \a units. @@ -321,17 +348,10 @@ bool QPdfWriter::setPageMargins(const QMarginsF &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; -} - /*! + \fn QPageLayout QPdfWriter::pageLayout() const + \since 5.3 + Returns the current page layout. Use this method to access the current QPageSize, QPageLayout::Orientation, QMarginsF, fullRect() and paintRect(). @@ -340,12 +360,7 @@ bool QPdfWriter::setPageMargins(const QMarginsF &margins, QPageLayout::Unit unit \sa setPageLayout(), setPageSize(), setPageOrientation(), setPageMargins() */ - -QPageLayout QPdfWriter::pageLayout() const -{ - Q_D(const QPdfWriter); - return d->engine->pageLayout(); -} +#endif /*! \reimp -- cgit v1.2.3