From dbc50e06df8465e6de02ac0b4458e1a3f3f8568c Mon Sep 17 00:00:00 2001 From: John Layt Date: Mon, 30 Dec 2013 18:04:40 +0100 Subject: QPrinter - Use QPageSize and QPageLayout Use QPageSize and QPageMargins to get/set values in the print engines, add api to directly set the values, and rewrite the docs to make the paper-based api obsolete instead of the page-based api. Add new PPK keys to pass QPageSize, QPageMargins and QPageLayout to the print engines to ensure no level of detail is lost, e.g. for custom sizes passed to QPrinter. [ChangeLog][QtPrintSupport][QPrinter] QPrinter can now use QPageSize and QPageLayout in the public api to control the page layout for a print job. Change-Id: Iee39a4042bcd6141d29b0a82b49066d7a7a78120 Reviewed-by: Lars Knoll --- .../printsupport/kernel/qprinter/tst_qprinter.cpp | 158 +++++++-------------- 1 file changed, 54 insertions(+), 104 deletions(-) (limited to 'tests/auto/printsupport/kernel/qprinter') 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 #include #include +#include #include @@ -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(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 > sizes = info.supportedSizesWithNames(); + QList sizes = info.supportedPageSizes(); if (sizes.size() == 0) QSKIP("No printers installed on this machine"); for (int i=0; i > sizes = info.supportedSizesWithNames(); + QList sizes = info.supportedPageSizes(); if (sizes.size() == 0) QSKIP("No printers installed on this machine"); for (int i=0; i