diff options
author | Andy Shaw <[email protected]> | 2017-11-28 15:41:30 +0100 |
---|---|---|
committer | Andy Shaw <[email protected]> | 2018-05-02 13:32:09 +0000 |
commit | 3e001feb4d03d350ecea76f136c503675a2431ab (patch) | |
tree | d9d45d1623cf28c232ded118b4add60e40a10490 | |
parent | 3e4b7223f102c40372bd11c50ecc684597f4bf32 (diff) |
If the page size is not valid on the new printer, set a custom size
When the page size was not valid on the new printer, it would end up
having the wrong page size name on the new printer. What should happen
in this case is that it should set the originally set page size as a
custom page size on the printer instead.
Task-number: QTBUG-62221
Change-Id: Iaca34ae262f5d0685ca60e4ca4b38229a2283289
Reviewed-by: Albert Astals Cid <[email protected]>
4 files changed, 66 insertions, 3 deletions
diff --git a/src/plugins/platforms/cocoa/qprintengine_mac.mm b/src/plugins/platforms/cocoa/qprintengine_mac.mm index b3d48c1ec39..eade4075001 100644 --- a/src/plugins/platforms/cocoa/qprintengine_mac.mm +++ b/src/plugins/platforms/cocoa/qprintengine_mac.mm @@ -574,6 +574,11 @@ void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va d->setPageSize(QPageSize(QPageSize::id(value.toInt()))); break; case PPK_PrinterName: { + QVariant pageSize = QVariant::fromValue(d->m_pageLayout.pageSize()); + const bool isFullPage = d->m_pageLayout.mode() == QPageLayout::FullPageMode; + QVariant orientation = QVariant::fromValue(d->m_pageLayout.orientation()); + QVariant margins = QVariant::fromValue(QPair<QMarginsF, QPageLayout::Unit>(d->m_pageLayout.margins(), + d->m_pageLayout.units())); QString id = value.toString(); if (id.isEmpty()) id = QCocoaPrinterSupport().defaultPrintDeviceId(); @@ -583,7 +588,14 @@ void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va PMPrinter printer = d->m_printDevice->macPrinter(); PMRetain(printer); PMSessionSetCurrentPMPrinter(d->session(), printer); - // TODO Do we need to check if the page size, etc, are valid on new printer? + // Ensure the settings are up to date and valid + if (d->m_printDevice->supportedPageSize(pageSize.value<QPageSize>()).isValid()) + setProperty(PPK_QPageSize, pageSize); + else + setProperty(PPK_CustomPaperSize, pageSize.value<QPageSize>().size(QPageSize::Point)); + setProperty(PPK_FullPage, QVariant(isFullPage)); + setProperty(PPK_Orientation, orientation); + setProperty(PPK_QPageMargins, margins); break; } case PPK_CustomPaperSize: diff --git a/src/plugins/printsupport/cups/qcupsprintengine.cpp b/src/plugins/printsupport/cups/qcupsprintengine.cpp index bd0d641e79b..6c29a99705c 100644 --- a/src/plugins/printsupport/cups/qcupsprintengine.cpp +++ b/src/plugins/printsupport/cups/qcupsprintengine.cpp @@ -276,7 +276,10 @@ void QCupsPrintEnginePrivate::changePrinter(const QString &newPrinter) grayscale = m_printDevice.defaultColorMode() == QPrint::GrayScale; // Get the equivalent page size for this printer as supported names may be different - setPageSize(m_pageLayout.pageSize()); + if (m_printDevice.supportedPageSize(m_pageLayout.pageSize()).isValid()) + setPageSize(m_pageLayout.pageSize()); + else + setPageSize(QPageSize(m_pageLayout.pageSize().size(QPageSize::Point), QPageSize::Point)); } void QCupsPrintEnginePrivate::setPageSize(const QPageSize &pageSize) diff --git a/src/printsupport/kernel/qprintengine_win.cpp b/src/printsupport/kernel/qprintengine_win.cpp index a943d24cb1f..ca4d1d0bd6a 100644 --- a/src/printsupport/kernel/qprintengine_win.cpp +++ b/src/printsupport/kernel/qprintengine_win.cpp @@ -1234,7 +1234,10 @@ void QWin32PrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant & if (printDevice.isValid()) { d->m_printDevice = printDevice; d->initialize(); - setProperty(PPK_QPageSize, pageSize); + if (d->m_printDevice.supportedPageSize(pageSize.value<QPageSize>()).isValid()) + setProperty(PPK_QPageSize, pageSize); + else + setProperty(PPK_CustomPaperSize, pageSize.value<QPageSize>().size(QPageSize::Point)); setProperty(PPK_FullPage, QVariant(isFullPage)); setProperty(PPK_Orientation, orientation); setProperty(PPK_QPageMargins, margins); diff --git a/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp b/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp index f68cf5c59c7..4f86e741178 100644 --- a/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp +++ b/tests/auto/printsupport/kernel/qprinter/tst_qprinter.cpp @@ -122,6 +122,7 @@ private slots: void testPageMetrics_data(); void testPageMetrics(); + void reusePageMetrics(); #endif private: QString testFileName(const QString &prefix, const QString &suffix); @@ -1955,6 +1956,50 @@ QString tst_QPrinter::testFileName(const QString &prefix, const QString &suffix) return result; } +void tst_QPrinter::reusePageMetrics() +{ + QList<QPrinterInfo> availablePrinters = QPrinterInfo::availablePrinters(); + if (availablePrinters.size() < 2) + QSKIP("Not enough printers to do this test with, need at least 2 setup"); + QPrinter defaultP; + QPrinterInfo info(defaultP); + QString otherPrinterName; + for (QPrinterInfo i : qAsConst(availablePrinters)) { + if (i.printerName() != defaultP.printerName()) { + otherPrinterName = i.printerName(); + break; + } + } + QPrinter otherP(QPrinterInfo::printerInfo(otherPrinterName)); + QList<QPageSize> defaultPageSizes = info.supportedPageSizes(); + QList<QPageSize> otherPageSizes = QPrinterInfo(otherP).supportedPageSizes(); + QPageSize unavailableSizeToSet; + for (QPageSize s : qAsConst(defaultPageSizes)) { + bool found = false; + for (QPageSize os : qAsConst(otherPageSizes)) { + if (os.isEquivalentTo(s)) { + found = true; + break; + } + } + const QPageSize tmpSize(s.size(QPageSize::Point), QPageSize::Point); + if (!tmpSize.name().startsWith("Custom")) + found = true; + if (!found) { + unavailableSizeToSet = s; + break; + } + } + if (!unavailableSizeToSet.isValid()) + QSKIP("Could not find a size that was not available on the non default printer. The test " + "requires this"); + defaultP.setPageSize(unavailableSizeToSet); + defaultP.setPrinterName(otherP.printerName()); + QVERIFY(defaultP.pageLayout().pageSize().isEquivalentTo(unavailableSizeToSet)); + QVERIFY(defaultP.pageLayout().pageSize().name() != unavailableSizeToSet.name()); + QCOMPARE(defaultP.pageLayout().pageSize().sizePoints(), unavailableSizeToSet.sizePoints()); +} + #endif // QT_CONFIG(printer) QTEST_MAIN(tst_QPrinter) |