diff options
-rw-r--r-- | src/plugins/platforms/cocoa/qprintengine_mac.mm | 32 | ||||
-rw-r--r-- | src/plugins/printsupport/cups/qcupsprintengine.cpp | 12 | ||||
-rw-r--r-- | src/printsupport/dialogs/qpagesetupdialog_unix.cpp | 13 | ||||
-rw-r--r-- | src/printsupport/dialogs/qprintdialog_mac.mm | 41 | ||||
-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 | ||||
-rw-r--r-- | tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp | 158 |
11 files changed, 512 insertions, 325 deletions
diff --git a/src/plugins/platforms/cocoa/qprintengine_mac.mm b/src/plugins/platforms/cocoa/qprintengine_mac.mm index bfe44c7ab3..95713eba59 100644 --- a/src/plugins/platforms/cocoa/qprintengine_mac.mm +++ b/src/plugins/platforms/cocoa/qprintengine_mac.mm @@ -42,7 +42,7 @@ #include "qprintengine_mac_p.h" #include "qcocoaprintersupport.h" #include <quuid.h> -#include <QtGui/qpagesize.h> +#include <QtGui/qpagelayout.h> #include <QtCore/qcoreapplication.h> #include <QtCore/qdebug.h> @@ -559,6 +559,26 @@ void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va margins.at(2).toReal(), margins.at(3).toReal())); break; } + case PPK_QPageSize: + d->setPageSize(value.value<QPageSize>()); + 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_printDevice->isValidPageLayout(pageLayout, d->resolution.hRes)) { + 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()); + } + break; + } // No default so that compiler will complain if new keys added and not handled in this engine } } @@ -688,6 +708,16 @@ QVariant QMacPrintEngine::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); // No default so that compiler will complain if new keys added and not handled in this engine } return ret; diff --git a/src/plugins/printsupport/cups/qcupsprintengine.cpp b/src/plugins/printsupport/cups/qcupsprintengine.cpp index 2c05a76084..90de1a2a8b 100644 --- a/src/plugins/printsupport/cups/qcupsprintengine.cpp +++ b/src/plugins/printsupport/cups/qcupsprintengine.cpp @@ -96,6 +96,18 @@ void QCupsPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &v case PPK_CupsOptions: d->cupsOptions = value.toStringList(); break; + case PPK_QPageSize: + d->setPageSize(value.value<QPageSize>()); + break; + case PPK_QPageLayout: { + QPageLayout pageLayout = value.value<QPageLayout>(); + if (pageLayout.isValid() && d->m_printDevice.isValidPageLayout(pageLayout, d->resolution)) { + d->m_pageLayout = pageLayout; + // Replace the page size with the CUPS page size + d->setPageSize(d->m_printDevice.supportedPageSize(pageLayout.pageSize())); + } + break; + } default: QPdfPrintEngine::setProperty(key, value); break; diff --git a/src/printsupport/dialogs/qpagesetupdialog_unix.cpp b/src/printsupport/dialogs/qpagesetupdialog_unix.cpp index d2975ad9da..7b84d8b86f 100644 --- a/src/printsupport/dialogs/qpagesetupdialog_unix.cpp +++ b/src/printsupport/dialogs/qpagesetupdialog_unix.cpp @@ -434,18 +434,7 @@ void QPageSetupWidget::selectPrinter() } if (!preferredSizeMatched) widget.paperSize->setCurrentIndex(cupsDefaultSize); - if (m_printer->d_func()->hasCustomPageMargins) { - m_printer->getPageMargins(&m_leftMargin, &m_topMargin, &m_rightMargin, &m_bottomMargin, QPrinter::Point); - } else { - QByteArray cupsPaperSizeChoice = widget.paperSize->itemData(widget.paperSize->currentIndex()).toByteArray(); - QRect paper = cups.paperRect(cupsPaperSizeChoice); - QRect content = cups.pageRect(cupsPaperSizeChoice); - - m_leftMargin = content.x() - paper.x(); - m_topMargin = content.y() - paper.y(); - m_rightMargin = paper.right() - content.right(); - m_bottomMargin = paper.bottom() - content.bottom(); - } + m_printer->getPageMargins(&m_leftMargin, &m_topMargin, &m_rightMargin, &m_bottomMargin, QPrinter::Point); } else m_cups = false; #endif diff --git a/src/printsupport/dialogs/qprintdialog_mac.mm b/src/printsupport/dialogs/qprintdialog_mac.mm index 67216f7f2a..7194aee22f 100644 --- a/src/printsupport/dialogs/qprintdialog_mac.mm +++ b/src/printsupport/dialogs/qprintdialog_mac.mm @@ -151,6 +151,47 @@ QT_USE_NAMESPACE } } + // Note this code should be in QCocoaPrintDevice, but that implementation is in the plugin, + // we need to move the dialog implementation into the plugin first to be able to access it. + // Need to tell QPrinter/QPageLayout if the page size or orientation has been changed + PMPageFormat pageFormat = static_cast<PMPageFormat>([printInfo PMPageFormat]); + PMPaper paper; + PMGetPageFormatPaper(pageFormat, &paper); + PMOrientation orientation; + PMGetOrientation(pageFormat, &orientation); + QPageSize pageSize; + QCFString key; + double width = 0; + double height = 0; + // If the PPD name is empty then is custom, for some reason PMPaperIsCustom doesn't work here + PMPaperGetPPDPaperName(paper, &key); + if (PMPaperGetWidth(paper, &width) == noErr && PMPaperGetHeight(paper, &height) == noErr) { + QString ppdKey = key; + if (ppdKey.isEmpty()) { + // Is using a custom page size as defined in the Print Dialog custom settings using mm or inches. + // We can't ask PMPaper what those units actually are, we can only get the point size which may return + // slightly wrong results due to rounding. + // Testing shows if using metric/mm then is rounded mm, if imperial/inch is rounded to 2 decimal places + // Even if we pass in our own custom size in mm with decimal places, the dialog will still round it! + // Suspect internal storage is in rounded mm? + if (QLocale().measurementSystem() == QLocale::MetricSystem) { + QSizeF sizef = QSizeF(width, height) / qt_pointMultiplier(QPageLayout::Millimeter); + // Round to 0 decimal places + pageSize = QPageSize(sizef.toSize(), QPageSize::Millimeter); + } else { + qreal multiplier = qt_pointMultiplier(QPageLayout::Inch); + const int w = qRound(width * 100 / multiplier); + const int h = qRound(height * 100 / multiplier); + pageSize = QPageSize(QSizeF(w / 100.0, h / 100.0), QPageSize::Inch); + } + } else { + pageSize = QPlatformPrintDevice::createPageSize(key, QSize(width, height), QString()); + } + } + if (pageSize.isValid() && !pageSize.isEquivalentTo(printer->pageLayout().pageSize())) + printer->setPageSize(pageSize); + printer->setOrientation(orientation == kPMLandscape ? QPrinter::Landscape : QPrinter::Portrait); + dialog->done((returnCode == NSOKButton) ? QDialog::Accepted : QDialog::Rejected); } @end 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; diff --git a/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp b/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp index 1a3edd1540..1211c75a3d 100644 --- a/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp +++ b/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp @@ -51,6 +51,7 @@ #include <qvariant.h> #include <qpainter.h> #include <qprintengine.h> +#include <qpagelayout.h> #include <math.h> @@ -461,61 +462,54 @@ void tst_QPrinter::testPageMargins() { QPrinter obj1; - qreal toMillimeters[6]; - toMillimeters[QPrinter::Millimeter] = 1; - toMillimeters[QPrinter::Point] = 0.352777778; - toMillimeters[QPrinter::Inch] = 25.4; - toMillimeters[QPrinter::Pica] = 4.23333333; - toMillimeters[QPrinter::Didot] = 0.376; - toMillimeters[QPrinter::Cicero] = 4.51166667; - QFETCH(qreal, left); QFETCH(qreal, top); QFETCH(qreal, right); QFETCH(qreal, bottom); QFETCH(int, unit); - qreal nLeft, nTop, nRight, nBottom; + QPageLayout layout = QPageLayout(QPageSize(QPageSize::A0), QPageLayout::Portrait, + QMarginsF(left, top, right, bottom), QPageLayout::Unit(unit)); - obj1.setPageMargins(left, top, right, bottom, static_cast<QPrinter::Unit>(unit)); + qreal nLeft, nTop, nRight, nBottom; - qreal tolerance = 0.05; + obj1.setPageMargins(left, top, right, bottom, QPrinter::Unit(unit)); obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Millimeter); - QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Millimeter]) < tolerance); - QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Millimeter]) < tolerance); - QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Millimeter]) < tolerance); - QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Millimeter]) < tolerance); + QCOMPARE(nLeft, layout.margins(QPageLayout::Millimeter).left()); + QCOMPARE(nRight, layout.margins(QPageLayout::Millimeter).right()); + QCOMPARE(nTop, layout.margins(QPageLayout::Millimeter).top()); + QCOMPARE(nBottom, layout.margins(QPageLayout::Millimeter).bottom()); obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Point); - QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Point]) < tolerance); - QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Point]) < tolerance); - QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Point]) < tolerance); - QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Point]) < tolerance); + QCOMPARE(nLeft, layout.margins(QPageLayout::Point).left()); + QCOMPARE(nRight, layout.margins(QPageLayout::Point).right()); + QCOMPARE(nTop, layout.margins(QPageLayout::Point).top()); + QCOMPARE(nBottom, layout.margins(QPageLayout::Point).bottom()); obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Inch); - QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Inch]) < tolerance); - QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Inch]) < tolerance); - QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Inch]) < tolerance); - QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Inch]) < tolerance); + QCOMPARE(nLeft, layout.margins(QPageLayout::Inch).left()); + QCOMPARE(nRight, layout.margins(QPageLayout::Inch).right()); + QCOMPARE(nTop, layout.margins(QPageLayout::Inch).top()); + QCOMPARE(nBottom, layout.margins(QPageLayout::Inch).bottom()); obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Pica); - QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Pica]) < tolerance); - QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Pica]) < tolerance); - QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Pica]) < tolerance); - QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Pica]) < tolerance); + QCOMPARE(nLeft, layout.margins(QPageLayout::Pica).left()); + QCOMPARE(nRight, layout.margins(QPageLayout::Pica).right()); + QCOMPARE(nTop, layout.margins(QPageLayout::Pica).top()); + QCOMPARE(nBottom, layout.margins(QPageLayout::Pica).bottom()); obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Didot); - QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Didot]) < tolerance); - QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Didot]) < tolerance); - QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Didot]) < tolerance); - QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Didot]) < tolerance); + QCOMPARE(nLeft, layout.margins(QPageLayout::Didot).left()); + QCOMPARE(nRight, layout.margins(QPageLayout::Didot).right()); + QCOMPARE(nTop, layout.margins(QPageLayout::Didot).top()); + QCOMPARE(nBottom, layout.margins(QPageLayout::Didot).bottom()); obj1.getPageMargins(&nLeft, &nTop, &nRight, &nBottom, QPrinter::Cicero); - QVERIFY(fabs(left*toMillimeters[unit] - nLeft*toMillimeters[QPrinter::Cicero]) < tolerance); - QVERIFY(fabs(top*toMillimeters[unit] - nTop*toMillimeters[QPrinter::Cicero]) < tolerance); - QVERIFY(fabs(right*toMillimeters[unit] - nRight*toMillimeters[QPrinter::Cicero]) < tolerance); - QVERIFY(fabs(bottom*toMillimeters[unit] - nBottom*toMillimeters[QPrinter::Cicero]) < tolerance); + QCOMPARE(nLeft, layout.margins(QPageLayout::Cicero).left()); + QCOMPARE(nRight, layout.margins(QPageLayout::Cicero).right()); + QCOMPARE(nTop, layout.margins(QPageLayout::Cicero).top()); + QCOMPARE(nBottom, layout.margins(QPageLayout::Cicero).bottom()); } void tst_QPrinter::errorReporting() @@ -536,13 +530,6 @@ void tst_QPrinter::errorReporting() painter.end(); } -static QByteArray msgSizeMismatch(const QSizeF &actual, const QSizeF &expected) -{ - QString result; - QDebug(&result) << "Paper size mismatch" << actual << "!=" << expected; - return result.toLocal8Bit(); -} - void tst_QPrinter::testCustomPageSizes() { QPrinter p; @@ -551,16 +538,14 @@ void tst_QPrinter::testCustomPageSizes() p.setPaperSize(customSize, QPrinter::Inch); QSizeF paperSize = p.paperSize(QPrinter::Inch); - // Due to the different calculations, the sizes may be off by a fraction so we have to check it manually - // instead of relying on QSizeF comparison - QVERIFY2(sqrt(pow(paperSize.width() - customSize.width(), 2.0) + pow(paperSize.height() - customSize.height(), 2.0)) < 0.01, - msgSizeMismatch(paperSize, customSize)); + QCOMPARE(paperSize.width(), customSize.width()); + QCOMPARE(paperSize.height(), customSize.height()); QPrinter p2(QPrinter::HighResolution); p2.setPaperSize(customSize, QPrinter::Inch); paperSize = p.paperSize(QPrinter::Inch); - QVERIFY2(sqrt(pow(paperSize.width() - customSize.width(), 2.0) + pow(paperSize.height() - customSize.height(), 2.0)) < 0.01, - msgSizeMismatch(paperSize, customSize)); + QCOMPARE(paperSize.width(), customSize.width()); + QCOMPARE(paperSize.height(), customSize.height()); } void tst_QPrinter::customPaperSizeAndMargins_data() @@ -732,71 +717,57 @@ void tst_QPrinter::testPdfTitle() void tst_QPrinter::customPaperNameSettingBySize() { -#ifndef Q_OS_WIN - QSKIP("Currently this triggers a problem on non Windows platforms, this will be fixed separately - QTBUG-34521"); -#endif QPrinter printer(QPrinter::HighResolution); QPrinterInfo info(printer); - QList<QPair<QString, QSizeF> > sizes = info.supportedSizesWithNames(); + QList<QPageSize> sizes = info.supportedPageSizes(); if (sizes.size() == 0) QSKIP("No printers installed on this machine"); for (int i=0; i<sizes.size(); i++) { - printer.setPaperSize(sizes.at(i).second, QPrinter::Millimeter); - // TODO Bypass direct compare until QPageSize used in QPrinter for consistency - //QCOMPARE(sizes.at(i).second, printer.paperSize(QPrinter::Millimeter)); - QCOMPARE(qRound(sizes.at(i).second.width()), qRound(printer.paperSize(QPrinter::Millimeter).width())); - QCOMPARE(qRound(sizes.at(i).second.height()), qRound(printer.paperSize(QPrinter::Millimeter).height())); + printer.setPaperSize(sizes.at(i).size(QPageSize::Millimeter), QPrinter::Millimeter); + QCOMPARE(sizes.at(i).size(QPageSize::Millimeter), printer.paperSize(QPrinter::Millimeter)); // Some printers have the same size under different names which can cause a problem for the test - // So we iterate up to the current position to check - QSizeF paperSize = sizes.at(i).second; + // So we look at all the other sizes to see if one also matches as we don't know which order they are in + QSizeF paperSize = sizes.at(i).size(QPageSize::Millimeter); QString paperName = printer.paperName(); - bool paperNameFound = (sizes.at(i).first == paperName); + bool paperNameFound = (sizes.at(i).name() == paperName); if (!paperNameFound) { - for (int j=0; j<i; j++) { - if (sizes.at(j).second == paperSize && sizes.at(j).first == paperName) { + for (int j = 0; j < sizes.size(); ++j) { + if (j != i + && sizes.at(j).size(QPageSize::Millimeter) == paperSize + && sizes.at(j).name() == paperName) { paperNameFound = true; break; } } } // Fail with the original values - // Disable until QPrinter uses QPageSize internally to preserve custom values - //if (!paperNameFound) - // QCOMPARE(sizes.at(i).first, printer.paperName()); + if (!paperNameFound) + QCOMPARE(sizes.at(i).name(), printer.paperName()); } // Check setting a custom size after setting a standard one works - QSizeF customSize(200, 200); + QSizeF customSize(200, 300); printer.setPaperSize(customSize, QPrinter::Millimeter); - // TODO Bypass direct compare until QPageSize used in QPrinter for consistency - //QCOMPARE(printer.paperSize(QPrinter::Millimeter), customSize); - QCOMPARE(qRound(customSize.width()), qRound(printer.paperSize(QPrinter::Millimeter).width())); - QCOMPARE(qRound(customSize.height()), qRound(printer.paperSize(QPrinter::Millimeter).height())); + QCOMPARE(printer.paperSize(QPrinter::Millimeter), customSize); QCOMPARE(printer.paperSize(), QPrinter::Custom); // Finally check setting a standard size after a custom one works - printer.setPaperSize(sizes.at(0).second, QPrinter::Millimeter); - QCOMPARE(printer.paperName(), sizes.at(0).first); - QCOMPARE(printer.paperSize(QPrinter::Millimeter), sizes.at(0).second); + printer.setPaperSize(sizes.at(0).size(QPageSize::Millimeter), QPrinter::Millimeter); + QCOMPARE(printer.paperName(), sizes.at(0).name()); + QCOMPARE(printer.paperSize(QPrinter::Millimeter), sizes.at(0).size(QPageSize::Millimeter)); } void tst_QPrinter::customPaperNameSettingByName() { -#ifndef Q_OS_WIN - QSKIP("Currently this triggers a problem on non Windows platforms, this will be fixed separately - QTBUG-34521"); -#endif QPrinter printer(QPrinter::HighResolution); QPrinterInfo info(printer); - QList<QPair<QString, QSizeF> > sizes = info.supportedSizesWithNames(); + QList<QPageSize> sizes = info.supportedPageSizes(); if (sizes.size() == 0) QSKIP("No printers installed on this machine"); for (int i=0; i<sizes.size(); i++) { - printer.setPaperName(sizes.at(i).first); - QCOMPARE(sizes.at(i).first, printer.paperName()); - QSizeF paperSize = printer.paperSize(QPrinter::Millimeter); - // TODO Change tolerance back to 0.01 once QPrinter uses QPageSize internally - QVERIFY2(sqrt(pow(sizes.at(i).second.width() - paperSize.width(), 2.0) + pow(sizes.at(i).second.height() - paperSize.height(), 2.0)) < 1.0, - msgSizeMismatch(sizes.at(i).second, paperSize)); + printer.setPaperName(sizes.at(i).name()); + QCOMPARE(sizes.at(i).name(), printer.paperName()); + QCOMPARE(sizes.at(i).size(QPageSize::Millimeter), printer.paperSize(QPrinter::Millimeter)); } } @@ -1744,7 +1715,6 @@ void tst_QPrinter::windowsPageSize() // Test value preservation native.setOutputFormat(QPrinter::PdfFormat); QCOMPARE(native.pageSize(), QPrinter::Letter); - QEXPECT_FAIL("", "Win paper size doesn't persist over format change", Continue); QCOMPARE(native.winPageSize(), DMPAPER_LETTER); native.setOutputFormat(QPrinter::NativeFormat); QCOMPARE(native.pageSize(), QPrinter::Letter); @@ -1812,8 +1782,6 @@ void tst_QPrinter::testPageMetrics_data() void tst_QPrinter::testPageMetrics() { - QSKIP("Skip tests until new backends pass"); - QFETCH(int, outputFormat); QFETCH(int, pageSize); QFETCH(qreal, widthMMf); @@ -1883,16 +1851,10 @@ void tst_QPrinter::testPageMetrics() QCOMPARE(printer.heightMM(), qRound(heightMMf - topMMf - bottomMMf)); // QPrinter::paperRect() always returns set orientation - QEXPECT_FAIL("", "Rect calculation lacks required precision", Continue); QCOMPARE(printer.paperRect(QPrinter::Millimeter), QRectF(0, 0, widthMMf, heightMMf)); - QCOMPARE(qRound(printer.paperRect(QPrinter::Millimeter).width()), qRound(widthMMf)); - QCOMPARE(qRound(printer.paperRect(QPrinter::Millimeter).height()), qRound(heightMMf)); // QPrinter::pageRect() always returns set orientation - QEXPECT_FAIL("", "Rect calculation lacks required precision", Continue); QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, widthMMf - leftMMf - rightMMf, heightMMf - topMMf - bottomMMf)); - QCOMPARE(qRound(printer.pageRect(QPrinter::Millimeter).width()), qRound(widthMMf - leftMMf - rightMMf)); - QCOMPARE(qRound(printer.pageRect(QPrinter::Millimeter).height()), qRound(heightMMf - topMMf - bottomMMf)); // Now switch to Landscape mode, size should be unchanged, but rect and metrics should change @@ -1929,16 +1891,10 @@ void tst_QPrinter::testPageMetrics() QCOMPARE(printer.heightMM(), qRound(widthMMf - topMMf - bottomMMf)); // QPrinter::paperRect() always returns set orientation - QEXPECT_FAIL("", "Rect calculation lacks required precision", Continue); QCOMPARE(printer.paperRect(QPrinter::Millimeter), QRectF(0, 0, heightMMf, widthMMf)); - QCOMPARE(qRound(printer.paperRect(QPrinter::Millimeter).width()), qRound(heightMMf)); - QCOMPARE(qRound(printer.paperRect(QPrinter::Millimeter).height()), qRound(widthMMf)); // QPrinter::pageRect() always returns set orientation - QEXPECT_FAIL("", "Rect calculation lacks required precision", Continue); QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf)); - QCOMPARE(qRound(printer.pageRect(QPrinter::Millimeter).width()), qRound(heightMMf - leftMMf - rightMMf)); - QCOMPARE(qRound(printer.pageRect(QPrinter::Millimeter).height()), qRound(widthMMf - topMMf - bottomMMf)); // Now while in Landscape mode, set the size again, results should be the same @@ -1976,16 +1932,10 @@ void tst_QPrinter::testPageMetrics() QCOMPARE(printer.heightMM(), qRound(widthMMf - topMMf - bottomMMf)); // QPrinter::paperRect() always returns set orientation - QEXPECT_FAIL("", "Rect calculation lacks required precision", Continue); QCOMPARE(printer.paperRect(QPrinter::Millimeter), QRectF(0, 0, heightMMf, widthMMf)); - QCOMPARE(qRound(printer.paperRect(QPrinter::Millimeter).width()), qRound(heightMMf)); - QCOMPARE(qRound(printer.paperRect(QPrinter::Millimeter).height()), qRound(widthMMf)); // QPrinter::pageRect() always returns set orientation - QEXPECT_FAIL("", "Rect calculation lacks required precision", Continue); QCOMPARE(printer.pageRect(QPrinter::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf)); - QCOMPARE(qRound(printer.pageRect(QPrinter::Millimeter).width()), qRound(heightMMf - leftMMf - rightMMf)); - QCOMPARE(qRound(printer.pageRect(QPrinter::Millimeter).height()), qRound(widthMMf - topMMf - bottomMMf)); } #endif // QT_NO_PRINTER |