From 6ce44c53c7aa802b817b72d49de88e4da0181488 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 15 Jun 2021 17:49:04 +0200 Subject: [PATCH] Windows: Read page size and orientation from setup dialog PAGESETUPDLG's hDevMode reports the page size and orientation selection of the user, so read that data to get accurate results. Otherwise, the page size of a landscape page wouldn't match any known page format, and we'd end up with a custom size that would not be valid for the preview, breaking the preview UI's orientation state. Reuse the helper from QPageSize to map Windows page size ID to our own enum. Fixes: QTBUG-93764 Pick-to: 6.2 6.1 5.15 Change-Id: Ib9a848619e3ba8780264ad76ed43c4fffae6b07f Reviewed-by: Friedemann Kleint Reviewed-by: Andy Shaw --- .../dialogs/qpagesetupdialog_win.cpp | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/printsupport/dialogs/qpagesetupdialog_win.cpp b/src/printsupport/dialogs/qpagesetupdialog_win.cpp index 2e2b41b372..6d51473d52 100644 --- a/src/printsupport/dialogs/qpagesetupdialog_win.cpp +++ b/src/printsupport/dialogs/qpagesetupdialog_win.cpp @@ -134,10 +134,35 @@ int QPageSetupDialog::exec() QDialog::setVisible(false); if (result) { engine->setGlobalDevMode(psd.hDevNames, psd.hDevMode); - d->printer->setPageSize(QPageSize(QSizeF(psd.ptPaperSize.x / multiplier, psd.ptPaperSize.y / multiplier), - layout.units() == QPageLayout::Inch ? QPageSize::Inch : QPageSize::Millimeter)); + QPageSize pageSize; + // try to read orientation and paper size ID from the dialog's devmode struct + if (psd.hDevMode) { + DEVMODE *rDevmode = reinterpret_cast(GlobalLock(psd.hDevMode)); + if (rDevmode->dmFields & DM_ORIENTATION) { + layout.setOrientation(rDevmode->dmOrientation == DMORIENT_PORTRAIT + ? QPageLayout::Portrait : QPageLayout::Landscape); + } + if (rDevmode->dmFields & DM_PAPERSIZE) + pageSize = QPageSize::id(rDevmode->dmPaperSize); + GlobalUnlock(rDevmode); + } + // fall back to use our own matching, and assume that paper that's wider than long means landscape + if (!pageSize.isValid() || pageSize.id() == QPageSize::Custom) { + QSizeF unitSize(psd.ptPaperSize.x / multiplier, psd.ptPaperSize.y / multiplier); + if (unitSize.width() > unitSize.height()) { + layout.setOrientation(QPageLayout::Landscape); + unitSize.transpose(); + } else { + layout.setOrientation(QPageLayout::Portrait); + } + pageSize = QPageSize(unitSize, layout.units() == QPageLayout::Inch + ? QPageSize::Inch : QPageSize::Millimeter); + } + layout.setPageSize(pageSize); + const QMarginsF margins(psd.rtMargin.left, psd.rtMargin.top, psd.rtMargin.right, psd.rtMargin.bottom); - d->printer->setPageMargins(margins / multiplier, layout.units()); + layout.setMargins(margins / multiplier); + d->printer->setPageLayout(layout); // copy from our temp DEVMODE struct if (!engine->globalDevMode() && hDevMode) {