diff options
Diffstat (limited to 'src/printsupport/kernel')
-rw-r--r-- | src/printsupport/kernel/qprintengine.h | 5 | ||||
-rw-r--r-- | src/printsupport/kernel/qprintengine_pdf.cpp | 30 | ||||
-rw-r--r-- | src/printsupport/kernel/qprintengine_win.cpp | 60 | ||||
-rw-r--r-- | src/printsupport/kernel/qprinter.cpp | 472 | ||||
-rw-r--r-- | src/printsupport/kernel/qprinter.h | 9 | ||||
-rw-r--r-- | src/printsupport/kernel/qprinter_p.h | 5 |
6 files changed, 373 insertions, 208 deletions
diff --git a/src/printsupport/kernel/qprintengine.h b/src/printsupport/kernel/qprintengine.h index 3993a22bef..a69fe01fd4 100644 --- a/src/printsupport/kernel/qprintengine.h +++ b/src/printsupport/kernel/qprintengine.h @@ -45,6 +45,8 @@ #include <QtCore/qvariant.h> #include <QtPrintSupport/qprinter.h> +Q_DECLARE_METATYPE(QMarginsF) + QT_BEGIN_NAMESPACE @@ -85,6 +87,9 @@ public: PPK_CopyCount, PPK_SupportsMultipleCopies, PPK_PaperName, + PPK_QPageSize, + PPK_QPageMargins, + PPK_QPageLayout, PPK_PaperSize = PPK_PageSize, PPK_CustomBase = 0xff00 diff --git a/src/printsupport/kernel/qprintengine_pdf.cpp b/src/printsupport/kernel/qprintengine_pdf.cpp index c24905009d..d62da0e1d6 100644 --- a/src/printsupport/kernel/qprintengine_pdf.cpp +++ b/src/printsupport/kernel/qprintengine_pdf.cpp @@ -48,6 +48,7 @@ #include <qdebug.h> #include <qbuffer.h> #include "qprinterinfo.h" +#include <QtGui/qpagelayout.h> #include <limits.h> #include <math.h> @@ -219,6 +220,24 @@ void QPdfPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va margins.at(2).toReal(), margins.at(3).toReal())); break; } + case PPK_QPageSize: { + QPageSize pageSize = value.value<QPageSize>(); + if (pageSize.isValid()) + d->m_pageLayout.setPageSize(pageSize); + break; + } + case PPK_QPageMargins: { + QPair<QMarginsF, QPageLayout::Unit> pair = value.value<QPair<QMarginsF, QPageLayout::Unit> >(); + d->m_pageLayout.setUnits(pair.second); + d->m_pageLayout.setMargins(pair.first); + break; + } + case PPK_QPageLayout: { + QPageLayout pageLayout = value.value<QPageLayout>(); + if (pageLayout.isValid()) + d->m_pageLayout = pageLayout; + break; + } // No default so that compiler will complain if new keys added and not handled in this engine } } @@ -319,6 +338,17 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const ret = list; break; } + case PPK_QPageSize: + ret.setValue(d->m_pageLayout.pageSize()); + break; + case PPK_QPageMargins: { + QPair<QMarginsF, QPageLayout::Unit> pair = qMakePair(d->m_pageLayout.margins(), d->m_pageLayout.units()); + ret.setValue(pair); + break; + } + case PPK_QPageLayout: + ret.setValue(d->m_pageLayout); + break; // No default so that compiler will complain if new keys added and not handled in this engine } return ret; diff --git a/src/printsupport/kernel/qprintengine_win.cpp b/src/printsupport/kernel/qprintengine_win.cpp index 78532f50dd..52b67d162b 100644 --- a/src/printsupport/kernel/qprintengine_win.cpp +++ b/src/printsupport/kernel/qprintengine_win.cpp @@ -62,6 +62,7 @@ #include <private/qpixmap_raster_p.h> #include <QtCore/QMetaType> #include <QtCore/qt_windows.h> +#include <QtGui/qpagelayout.h> Q_DECLARE_METATYPE(HFONT) Q_DECLARE_METATYPE(LOGFONT) @@ -1195,8 +1196,50 @@ void QWin32PrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant & break; } - // No default so that compiler will complain if new keys added and not handled in this engine + case PPK_QPageSize: { + // Get the page size from the printer if supported + const QPageSize pageSize = value.value<QPageSize>(); + if (pageSize.isValid()) { + d->setPageSize(pageSize); + d->doReinit(); +#ifdef QT_DEBUG_METRICS + qDebug() << "QWin32PrintEngine::setProperty(PPK_QPageSize," << pageSize << ")"; + d->debugMetrics(); +#endif // QT_DEBUG_METRICS + } + break; + } + case PPK_QPageMargins: { + QPair<QMarginsF, QPageLayout::Unit> pair = value.value<QPair<QMarginsF, QPageLayout::Unit> >(); + d->m_pageLayout.setUnits(pair.second); + d->m_pageLayout.setMargins(pair.first); + d->updateMetrics(); +#ifdef QT_DEBUG_METRICS + qDebug() << "QWin32PrintEngine::setProperty(PPK_QPageMargins," << pair.first << pair.second << ")"; + d->debugMetrics(); +#endif // QT_DEBUG_METRICS + break; + } + + case PPK_QPageLayout: { + QPageLayout pageLayout = value.value<QPageLayout>(); + if (pageLayout.isValid() && d->m_printDevice.isValidPageLayout(pageLayout, d->resolution)) { + setProperty(PPK_QPageSize, QVariant::fromValue(pageLayout.pageSize())); + setProperty(PPK_FullPage, pageLayout.mode() == QPageLayout::FullPageMode); + setProperty(PPK_Orientation, QVariant::fromValue(pageLayout.orientation())); + d->m_pageLayout.setUnits(pageLayout.units()); + d->m_pageLayout.setMargins(pageLayout.margins()); + d->updateMetrics(); +#ifdef QT_DEBUG_METRICS + qDebug() << "QWin32PrintEngine::setProperty(PPK_QPageLayout," << pageLayout << ")"; + d->debugMetrics(); +#endif // QT_DEBUG_METRICS + } + break; + } + + // No default so that compiler will complain if new keys added and not handled in this engine } } @@ -1345,8 +1388,21 @@ QVariant QWin32PrintEngine::property(PrintEnginePropertyKey key) const break; } - // No default so that compiler will complain if new keys added and not handled in this engine + case PPK_QPageSize: + value.setValue(d->m_pageLayout.pageSize()); + break; + + case PPK_QPageMargins: { + QPair<QMarginsF, QPageLayout::Unit> pair = qMakePair(d->m_pageLayout.margins(), d->m_pageLayout.units()); + value.setValue(pair); + break; + } + + case PPK_QPageLayout: + value.setValue(d->m_pageLayout); + break; + // No default so that compiler will complain if new keys added and not handled in this engine } return value; } diff --git a/src/printsupport/kernel/qprinter.cpp b/src/printsupport/kernel/qprinter.cpp index 9e10f5c636..f72a0ae0fb 100644 --- a/src/printsupport/kernel/qprinter.cpp +++ b/src/printsupport/kernel/qprinter.cpp @@ -67,39 +67,14 @@ QT_BEGIN_NAMESPACE return; \ } -// NB! This table needs to be in sync with QPrinter::PaperSize -static const float qt_paperSizes[][2] = { - {210, 297}, // A4 - {176, 250}, // B5 - {215.9f, 279.4f}, // Letter - {215.9f, 355.6f}, // Legal - {190.5f, 254}, // Executive - {841, 1189}, // A0 - {594, 841}, // A1 - {420, 594}, // A2 - {297, 420}, // A3 - {148, 210}, // A5 - {105, 148}, // A6 - {74, 105}, // A7 - {52, 74}, // A8 - {37, 52}, // A8 - {1000, 1414}, // B0 - {707, 1000}, // B1 - {31, 44}, // B10 - {500, 707}, // B2 - {353, 500}, // B3 - {250, 353}, // B4 - {125, 176}, // B6 - {88, 125}, // B7 - {62, 88}, // B8 - {44, 62}, // B9 - {163, 229}, // C5E - {105, 241}, // US Common - {110, 220}, // DLE - {210, 330}, // Folio - {431.8f, 279.4f}, // Ledger - {279.4f, 431.8f} // Tabloid -}; +#define ABORT_IF_ACTIVE_RETURN(location, retValue) \ + if (d->printEngine->printerState() == QPrinter::Active) { \ + qWarning("%s: Cannot be changed while printer is active", location); \ + return retValue; \ + } + +extern qreal qt_pixelMultiplier(int resolution); +extern QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits); /// return the multiplier of converting from the unit value to postscript-points. Q_PRINTSUPPORT_EXPORT double qt_multiplierForUnit(QPrinter::Unit unit, int resolution) @@ -123,28 +98,6 @@ Q_PRINTSUPPORT_EXPORT double qt_multiplierForUnit(QPrinter::Unit unit, int resol return 1.0; } -/// return the QSize from the specified in unit as millimeters -Q_PRINTSUPPORT_EXPORT QSizeF qt_SizeFromUnitToMillimeter(const QSizeF &size, QPrinter::Unit unit, double resolution) -{ - switch (unit) { - case QPrinter::Millimeter: - return size; - case QPrinter::Point: - return size * 0.352777778; - case QPrinter::Inch: - return size * 25.4; - case QPrinter::Pica: - return size * 4.23333333334; - case QPrinter::Didot: - return size * 0.377; - case QPrinter::Cicero: - return size * 4.511666667; - case QPrinter::DevicePixel: - return size * (0.352777778 * 72.0 / resolution); - } - return size; -} - // not static: it's needed in qpagesetupdialog_unix.cpp Q_PRINTSUPPORT_EXPORT QSizeF qt_printerPaperSize(QPrinter::Orientation orientation, QPrinter::PaperSize paperSize, @@ -152,10 +105,12 @@ Q_PRINTSUPPORT_EXPORT QSizeF qt_printerPaperSize(QPrinter::Orientation orientati int resolution) { QPageSize pageSize = QPageSize(QPageSize::PageSizeId(paperSize)); - if (orientation == QPrinter::Landscape) - return pageSize.size(QPage::Unit(unit), resolution).transposed(); + QSizeF sizef; + if (unit == QPrinter::DevicePixel) + sizef = pageSize.size(QPageSize::Point) * qt_multiplierForUnit(unit, resolution); else - return pageSize.size(QPage::Unit(unit), resolution); + sizef = pageSize.size(QPageSize::Unit(unit)); + return orientation == QPrinter::Landscape ? sizef.transposed() : sizef; } QPrinterInfo QPrinterPrivate::findValidPrinter(const QPrinterInfo &printer) @@ -1007,21 +962,165 @@ void QPrinter::setCreator(const QString &creator) d->setProperty(QPrintEngine::PPK_Creator, creator); } +/*! + \since 5.3 + + Sets the page layout to \a newLayout. + + If the \a newLayout is not valid for the current printer then the page + layout will not be changed. For example, if the page size is not supported + by the printer, or if the margins fall outside the printable area. + + Returns true if the page layout was successfully set to \a newLayout. + + \sa pageLayout(), setPageSize(), setPageOrientation(), setPageMargins() +*/ + +bool QPrinter::setPageLayout(const QPageLayout &newLayout) +{ + Q_D(QPrinter); + + if (d->paintEngine->type() != QPaintEngine::Pdf) + ABORT_IF_ACTIVE_RETURN("QPrinter::setPageLayout", false); + + // Try to set the print engine page layout + d->setProperty(QPrintEngine::PPK_QPageLayout, QVariant::fromValue(newLayout)); + + // Set QPagedPaintDevice layout to match the current print engine value + devicePageLayout() = pageLayout(); + + return pageLayout().isEquivalentTo(newLayout); +} + +/*! + \since 5.3 + + Sets the page size to \a pageSize. + + If the \a pageSize is not valid for the current printer then the page + size will not be changed. + + Changing the page size may affect the current page margins if they fall + outside the printable margins for the new page size on the current printer. + + To obtain the current QPageSize use pageLayout().pageSize(). + + Returns true if the page size was successfully set to \a pageSize. + + \sa pageLayout(), setPageLayout() +*/ + +bool QPrinter::setPageSize(const QPageSize &pageSize) +{ + Q_D(QPrinter); + + if (d->paintEngine->type() != QPaintEngine::Pdf) + ABORT_IF_ACTIVE_RETURN("QPrinter::setPageSize", false); + + // Try to set the print engine page size + d->setProperty(QPrintEngine::PPK_QPageSize, QVariant::fromValue(pageSize)); + + // Set QPagedPaintDevice layout to match the current print engine value + devicePageLayout() = pageLayout(); + + return pageLayout().pageSize().isEquivalentTo(pageSize); +} + +/*! + \since 5.3 + + Sets the page \a orientation to QPageLayout::Portrait or QPageLayout::Landscape. + + The printer driver reads this setting and prints the page using the + specified orientation. + + On Windows and Mac, this option can be changed while printing and will + take effect from the next call to newPage(). + + To obtain the current QPageLayout::Orientation use pageLayout().pageOrientation(). + + Returns true if the page orientation was successfully set to \a orientation. + + \sa pageLayout(), setPageLayout() +*/ + +bool QPrinter::setPageOrientation(QPageLayout::Orientation orientation) +{ + Q_D(QPrinter); + + // Set the print engine value + d->setProperty(QPrintEngine::PPK_Orientation, orientation); + + // Set QPagedPaintDevice layout to match the current print engine value + devicePageLayout() = pageLayout(); + + return pageLayout().orientation() == orientation; +} + +/*! + \since 5.3 + + Set the page margins to \a margins in the given \a units. If \a units are + not provided then the current units are used. + + If in Full Page mode then no check is performed on the \a margins set, + otherwise the \a margins must fall within the printable area for the page + size on the current printer. + + To obtain the current page margins use pageLayout().pageMargins(). + + Returns true if the page margins was successfully set to \a margins. + + \sa pageLayout(), setPageLayout() +*/ + +bool QPrinter::setPageMargins(const QMarginsF &margins, QPageLayout::Unit units) +{ + Q_D(QPrinter); + + // Try to set print engine margins + QPair<QMarginsF, QPageLayout::Unit> pair = qMakePair(margins, units); + d->setProperty(QPrintEngine::PPK_QPageMargins, QVariant::fromValue(pair)); + + // Set QPagedPaintDevice layout to match the current print engine value + devicePageLayout() = pageLayout(); + + return pageLayout().margins() == margins && pageLayout().units() == units; +} + +/*! + Returns the current page layout. Use this method to access the current + QPageSize, QPageLayout::Orientation, QMarginsF, fullPageRect() and paintRect(). + + Note that you cannot use the setters on the returned object, you must either + call the QPrinter methods or setPageLayout(). + + \sa setPageLayout(), setPageSize(), setPageOrientation(), setPageMargins() +*/ + +QPageLayout QPrinter::pageLayout() const +{ + Q_D(const QPrinter); + return d->printEngine->property(QPrintEngine::PPK_QPageLayout).value<QPageLayout>(); +} /*! + \obsolete Use pageLayout().pageOrientation() instead. + Returns the orientation setting. This is driver-dependent, but is usually QPrinter::Portrait. - \sa setOrientation() + \sa pageLayout() */ QPrinter::Orientation QPrinter::orientation() const { - Q_D(const QPrinter); - return QPrinter::Orientation(d->printEngine->property(QPrintEngine::PPK_Orientation).toInt()); + return QPrinter::Orientation(pageLayout().orientation()); } /*! + \obsolete Use setPageOrientation() instead. + Sets the print orientation to \a orientation. The orientation can be either QPrinter::Portrait or @@ -1033,32 +1132,34 @@ QPrinter::Orientation QPrinter::orientation() const On Windows and Mac, this option can be changed while printing and will take effect from the next call to newPage(). - \sa orientation() + \sa setPageOrientation() */ void QPrinter::setOrientation(Orientation orientation) { - Q_D(QPrinter); - d->setProperty(QPrintEngine::PPK_Orientation, orientation); + setPageOrientation(QPageLayout::Orientation(orientation)); } - /*! \since 4.4 + + \obsolete Use pageLayout().pageSize().id() instead. + Returns the printer paper size. The default value is driver-dependent. - \sa setPaperSize(), pageRect(), paperRect() + \sa pageLayout() */ QPrinter::PaperSize QPrinter::paperSize() const { - Q_D(const QPrinter); - return QPrinter::PaperSize(d->printEngine->property(QPrintEngine::PPK_PaperSize).toInt()); + return pageSize(); } /*! \since 4.4 + \obsolete Use setPageSize(QPageSize) instead. + Sets the printer paper size to \a newPaperSize if that size is supported. The result is undefined if \a newPaperSize is not supported. @@ -1068,116 +1169,103 @@ QPrinter::PaperSize QPrinter::paperSize() const This function is useful mostly for setting a default value that the user can override in the print dialog. - \sa paperSize(), PaperSize, setFullPage(), setResolution(), pageRect(), paperRect() + \sa setPageSize() */ void QPrinter::setPaperSize(PaperSize newPaperSize) { - setPageSize(newPaperSize); + setPageSize(QPageSize(QPageSize::PageSizeId(newPaperSize))); } /*! - \obsolete + \obsolete Use pageLayout().pageSize().id() instead. Returns the printer page size. The default value is driver-dependent. - Use paperSize() instead. + \sa pageLayout() */ QPrinter::PageSize QPrinter::pageSize() const { - return paperSize(); + return QPrinter::PaperSize(pageLayout().pageSize().id()); } /*! - \obsolete + \obsolete Use setPageSize(QPageSize) instead. Sets the printer page size based on \a newPageSize. - Use setPaperSize() instead. + \sa setPageSize() */ void QPrinter::setPageSize(PageSize newPageSize) { - QPagedPaintDevice::setPageSize(newPageSize); - - Q_D(QPrinter); - if (d->paintEngine->type() != QPaintEngine::Pdf) - ABORT_IF_ACTIVE("QPrinter::setPaperSize"); - if (newPageSize < 0 || newPageSize >= NPageSize) { - qWarning("QPrinter::setPaperSize: Illegal paper size %d", newPageSize); - return; - } - d->setProperty(QPrintEngine::PPK_PaperSize, newPageSize); - d->hasUserSetPageSize = true; + setPageSize(QPageSize(QPageSize::PageSizeId(newPageSize))); } /*! \since 4.4 + \obsolete Use setPageSize(QPageSize) instead. + Sets the paper size based on \a paperSize in \a unit. Note that the paper size is defined in a portrait layout, regardless of what the current printer orientation is set to. - \sa paperSize() + \sa setPageSize() */ void QPrinter::setPaperSize(const QSizeF &paperSize, QPrinter::Unit unit) { - Q_D(QPrinter); - if (d->paintEngine->type() != QPaintEngine::Pdf) - ABORT_IF_ACTIVE("QPrinter::setPaperSize"); - setPageSizeMM(qt_SizeFromUnitToMillimeter(paperSize, unit, resolution())); + setPageSize(QPageSize(paperSize, QPageSize::Unit(unit))); } /*! \reimp + \obsolete Use setPageSize(QPageSize) instead. + + Use setPageSize(QPageSize) instead. + Note that the page size is defined in a portrait layout, regardless of what the current printer orientation is set to. + + \sa setPageSize() */ void QPrinter::setPageSizeMM(const QSizeF &size) { - Q_D(QPrinter); - - QPagedPaintDevice::setPageSizeMM(size); - - QSizeF s = size * 72./25.4; - d->setProperty(QPrintEngine::PPK_CustomPaperSize, s); - d->hasUserSetPageSize = true; + setPageSize(QPageSize(size, QPageSize::Millimeter)); } /*! \since 4.4 + \obsolete Use pageLayout().pageSize().size() or + pageLayout().fullPageSize() instead. + Returns the paper size in \a unit. Note that the returned size reflects the current paper orientation. - \sa setPaperSize() + \sa pageLayout() */ QSizeF QPrinter::paperSize(Unit unit) const { - Q_D(const QPrinter); - int res = resolution(); - const qreal multiplier = qt_multiplierForUnit(unit, res); - PaperSize paperType = paperSize(); - if (paperType == Custom) { - QSizeF size = d->printEngine->property(QPrintEngine::PPK_CustomPaperSize).toSizeF(); - return QSizeF(size.width() / multiplier, size.height() / multiplier); - } - else { - return qt_printerPaperSize(orientation(), paperType, unit, res); - } + if (unit == QPrinter::DevicePixel) + return pageLayout().fullRectPixels(resolution()).size(); + else + return pageLayout().fullRect(QPageLayout::Unit(unit)).size(); } /*! \since 5.1 + \obsolete Use setPageSize(QPageSize) instead. + Sets the paper used by the printer to \a paperName. - \sa paperName() + \sa setPageSize() */ void QPrinter::setPaperName(const QString &paperName) @@ -1191,9 +1279,13 @@ void QPrinter::setPaperName(const QString &paperName) /*! \since 5.1 + \obsolete Use pageLayout().pageSize().name() instead. + Returns the paper name of the paper set on the printer. The default value for this is driver-dependent. + + \sa pageLayout() */ QString QPrinter::paperName() const @@ -1438,13 +1530,16 @@ void QPrinter::setCollateCopies(bool collate) printer's margins, so the application must account for the margins itself. - \sa fullPage(), setPaperSize(), width(), height() + \sa fullPage(), pageLayout(), setPageSize(), width(), height() */ void QPrinter::setFullPage(bool fp) { Q_D(QPrinter); + // Set the print engine d->setProperty(QPrintEngine::PPK_FullPage, fp); + // Set QPagedPaintDevice layout to match the current print engine value + devicePageLayout() = pageLayout(); } @@ -1455,7 +1550,7 @@ void QPrinter::setFullPage(bool fp) See setFullPage() for details and caveats. - \sa setFullPage(), PaperSize + \sa setFullPage(), pageLayout() */ bool QPrinter::fullPage() const @@ -1635,19 +1730,10 @@ QPrinter::DuplexMode QPrinter::duplex() const */ QRectF QPrinter::pageRect(Unit unit) const { - Q_D(const QPrinter); - int res = resolution(); - const qreal multiplier = qt_multiplierForUnit(unit, res); - // the page rect is in device pixels - QRect devRect(d->printEngine->property(QPrintEngine::PPK_PageRect).toRect()); - if (unit == DevicePixel) - return devRect; - QRectF diRect(devRect.x()*72.0/res, - devRect.y()*72.0/res, - devRect.width()*72.0/res, - devRect.height()*72.0/res); - return QRectF(diRect.x()/multiplier, diRect.y()/multiplier, - diRect.width()/multiplier, diRect.height()/multiplier); + if (unit == QPrinter::DevicePixel) + return pageLayout().paintRectPixels(resolution()); + else + return pageLayout().paintRect(QPageLayout::Unit(unit)); } @@ -1661,29 +1747,22 @@ QRectF QPrinter::pageRect(Unit unit) const */ QRectF QPrinter::paperRect(Unit unit) const { - Q_D(const QPrinter); - int res = resolution(); - const qreal multiplier = qt_multiplierForUnit(unit, resolution()); - // the page rect is in device pixels - QRect devRect(d->printEngine->property(QPrintEngine::PPK_PaperRect).toRect()); - if (unit == DevicePixel) - return devRect; - QRectF diRect(devRect.x()*72.0/res, - devRect.y()*72.0/res, - devRect.width()*72.0/res, - devRect.height()*72.0/res); - return QRectF(diRect.x()/multiplier, diRect.y()/multiplier, - diRect.width()/multiplier, diRect.height()/multiplier); + if (unit == QPrinter::DevicePixel) + return pageLayout().fullRectPixels(resolution()); + else + return pageLayout().fullRect(QPageLayout::Unit(unit)); } /*! + \obsolete Use pageLayout().paintRect() instead. + Returns the page's rectangle; this is usually smaller than the paperRect() since the page normally has margins between its borders and the paper. The unit of the returned rectangle is DevicePixel. - \sa paperSize() + \sa pageLayout() */ QRect QPrinter::pageRect() const { @@ -1692,12 +1771,14 @@ QRect QPrinter::pageRect() const } /*! + \obsolete Use pageLayout().fullPageRect() instead. + Returns the paper's rectangle; this is usually larger than the pageRect(). The unit of the returned rectangle is DevicePixel. - \sa pageRect() + \sa pageLayout() */ QRect QPrinter::paperRect() const { @@ -1705,61 +1786,69 @@ QRect QPrinter::paperRect() const return d->printEngine->property(QPrintEngine::PPK_PaperRect).toRect(); } - /*! \since 4.4 + \obsolete Use setPageMargins(QMarginsF, QPageLayout::Unit) instead. + This function sets the \a left, \a top, \a right and \a bottom page margins for this printer. The unit of the margins are specified with the \a unit parameter. - \sa getPageMargins() + \sa setPageMargins() */ void QPrinter::setPageMargins(qreal left, qreal top, qreal right, qreal bottom, QPrinter::Unit unit) { - const qreal multiplier = qt_multiplierForUnit(unit, resolution()) * 25.4/72.; - Margins m = { left*multiplier, right*multiplier, top*multiplier, bottom*multiplier }; - setMargins(m); + if (unit == QPrinter::DevicePixel) { + QMarginsF margins = QMarginsF(left, top, right, bottom); + margins *= qt_pixelMultiplier(resolution()); + margins = qt_convertMargins(margins, QPageLayout::Point, pageLayout().units()); + setPageMargins(margins, pageLayout().units()); + } else { + setPageMargins(QMarginsF(left, top, right, bottom), QPageLayout::Unit(unit)); + } } /*! - \reimp + \reimp + + \obsolete Use setPageMargins(QMarginsF, QPageLayout::Unit) instead. + + \sa setPageMargins() */ void QPrinter::setMargins(const Margins &m) { - Q_D(QPrinter); - - // set margins also to super class - QPagedPaintDevice::setMargins(m); - - const qreal multiplier = 72./25.4; - QList<QVariant> margins; - margins << (m.left * multiplier) << (m.top * multiplier) - << (m.right * multiplier) << (m.bottom * multiplier); - d->setProperty(QPrintEngine::PPK_PageMargins, margins); - d->hasCustomPageMargins = true; + setPageMargins(QMarginsF(m.left, m.top, m.right, m.bottom), QPageLayout::Millimeter); } - /*! \since 4.4 + \obsolete Use pageLayout().pageMargins() instead. + Returns the page margins for this printer in \a left, \a top, \a right, \a bottom. The unit of the returned margins are specified with the \a unit parameter. - \sa setPageMargins() + \sa pageLayout(), setPageMargins() */ void QPrinter::getPageMargins(qreal *left, qreal *top, qreal *right, qreal *bottom, QPrinter::Unit unit) const { - Q_D(const QPrinter); - Q_ASSERT(left && top && right && bottom); - const qreal multiplier = qt_multiplierForUnit(unit, resolution()); - QList<QVariant> margins(d->printEngine->property(QPrintEngine::PPK_PageMargins).toList()); - *left = margins.at(0).toReal() / multiplier; - *top = margins.at(1).toReal() / multiplier; - *right = margins.at(2).toReal() / multiplier; - *bottom = margins.at(3).toReal() / multiplier; + QMarginsF margins; + if (unit == QPrinter::DevicePixel) { + QMargins tmp = pageLayout().marginsPixels(resolution()); + margins = QMarginsF(tmp.left(), tmp.top(), tmp.right(), tmp.bottom()); + } else { + margins = pageLayout().margins(QPageLayout::Unit(unit)); + } + if (left) + *left = margins.left(); + if (right) + *right = margins.right(); + if (top) + *top = margins.top(); + if (bottom) + *bottom = margins.bottom(); } /*! @@ -1795,13 +1884,15 @@ QPrintEngine *QPrinter::printEngine() const #if defined (Q_OS_WIN) /*! + \obsolete Use QPageSize::id(windowsId) and setPageLayout(QPageSize) instead. + Sets the page size to be used by the printer under Windows to \a pageSize. \warning This function is not portable so you may prefer to use setPaperSize() instead. - \sa winPageSize() + \sa pageLayout() */ void QPrinter::setWinPageSize(int pageSize) { @@ -1811,12 +1902,14 @@ void QPrinter::setWinPageSize(int pageSize) } /*! + \obsolete Use pageLayout.pageSize().windowsId() instead. + Returns the page size used by the printer under Windows. \warning This function is not portable so you may prefer to use paperSize() instead. - \sa setWinPageSize() + \sa pageLayout() */ int QPrinter::winPageSize() const { @@ -2148,13 +2241,19 @@ QPrinter::PrintRange QPrinter::printRange() const in the QPrinter::Point unit. \value PPK_PageMargins A QList<QVariant> containing the left, top, - right and bottom margin values. + right and bottom margin values in the QPrinter::Point unit. \value PPK_CopyCount An integer specifying the number of copies to print. \value PPK_SupportsMultipleCopies A boolean value indicating whether or not the printer supports printing multiple copies in one job. + \value PPK_QPageSize Set the page size using a QPageSize object. + + \value PPK_QPageMargins Set the page margins using a QPair of QMarginsF and QPageLayout::Unit. + + \value PPK_QPageLayout Set the page layout using a QPageLayout object. + \value PPK_CustomBase Basis for extension. */ @@ -2206,37 +2305,6 @@ QPrinter::PrintRange QPrinter::printRange() const Returns the current state of the printer being used by the print engine. */ -/* - Returns the dimensions for the given paper size, \a size, in millimeters. -*/ -QSizeF qt_paperSizeToQSizeF(QPrinter::PaperSize size) -{ - if (size == QPrinter::Custom) return QSizeF(0, 0); - return QSizeF(qt_paperSizes[size][0], qt_paperSizes[size][1]); -} - -/* - Returns the PaperSize type that matches \a size, where \a size - is in millimeters. - - Because dimensions may not always be completely accurate (for - example when converting between units), a particular PaperSize - will be returned if it matches within -1/+1 millimeters. -*/ -QPrinter::PaperSize qSizeFTopaperSize(const QSizeF& size) -{ - for (int i = 0; i < static_cast<int>(QPrinter::NPageSize); ++i) { - if (qt_paperSizes[i][0] >= size.width() - 1 && - qt_paperSizes[i][0] <= size.width() + 1 && - qt_paperSizes[i][1] >= size.height() - 1 && - qt_paperSizes[i][1] <= size.height() + 1) { - return QPrinter::PaperSize(i); - } - } - - return QPrinter::Custom; -} - QT_END_NAMESPACE #endif // QT_NO_PRINTER diff --git a/src/printsupport/kernel/qprinter.h b/src/printsupport/kernel/qprinter.h index f842c222f6..709d77d4e3 100644 --- a/src/printsupport/kernel/qprinter.h +++ b/src/printsupport/kernel/qprinter.h @@ -45,6 +45,7 @@ #include <QtCore/qstring.h> #include <QtCore/qscopedpointer.h> #include <QtGui/qpagedpaintdevice.h> +#include <QtGui/qpagelayout.h> #include <QtPrintSupport/qtprintsupportglobal.h> QT_BEGIN_NAMESPACE @@ -60,6 +61,8 @@ class QPrinterPrivate; class QPaintEngine; class QPrintEngine; class QPrinterInfo; +class QPageSize; +class QPageMargins; class Q_PRINTSUPPORT_EXPORT QPrinter : public QPagedPaintDevice { @@ -304,6 +307,12 @@ public: void setCreator(const QString &); QString creator() const; + bool setPageLayout(const QPageLayout &pageLayout); + bool setPageSize(const QPageSize &pageSize); + bool setPageOrientation(QPageLayout::Orientation orientation); + bool setPageMargins(const QMarginsF &margins, QPageLayout::Unit units); + QPageLayout pageLayout() const; + void setOrientation(Orientation); Orientation orientation() const; diff --git a/src/printsupport/kernel/qprinter_p.h b/src/printsupport/kernel/qprinter_p.h index 2357b9e944..ddc2dad413 100644 --- a/src/printsupport/kernel/qprinter_p.h +++ b/src/printsupport/kernel/qprinter_p.h @@ -87,9 +87,7 @@ public: q_ptr(printer), printRange(QPrinter::AllPages), use_default_engine(true), - validPrinter(false), - hasCustomPageMargins(false), - hasUserSetPageSize(false) + validPrinter(false) { } @@ -129,7 +127,6 @@ public: uint validPrinter : 1; uint hasCustomPageMargins : 1; - uint hasUserSetPageSize : 1; // Used to remember which properties have been manually set by the user. QSet<QPrintEngine::PrintEnginePropertyKey> m_properties; |