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
Change-Id: Ib9a848619e3ba8780264ad76ed43c4fffae6b07f
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
(cherry picked from commit 6ce44c53c7aa802b817b72d49de88e4da0181488)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2021-06-15 17:49:04 +02:00 committed by Qt Cherry-pick Bot
parent 51838c964f
commit d842468685

View File

@ -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<DEVMODE*>(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) {