Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qpagesetupdialog_win.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qpagesetupdialog.h"
5
6#include <qapplication.h>
7
8#include <private/qprintengine_win_p.h>
10#include "qprinter.h"
11#include <qpa/qplatformnativeinterface.h>
12
14
16 : QDialog(*(new QPageSetupDialogPrivate(printer)), parent)
17{
18 setWindowTitle(QCoreApplication::translate("QPrintPreviewDialog", "Page Setup"));
20}
21
24{
25 setWindowTitle(QCoreApplication::translate("QPrintPreviewDialog", "Page Setup"));
27}
28
30{
32
33 if (d->printer->outputFormat() != QPrinter::NativeFormat)
34 return Rejected;
35
36 QWin32PrintEngine *engine = static_cast<QWin32PrintEngine*>(d->printer->paintEngine());
38
39 PAGESETUPDLG psd;
40 memset(&psd, 0, sizeof(PAGESETUPDLG));
41 psd.lStructSize = sizeof(PAGESETUPDLG);
42
43 // we need a temp DEVMODE struct if we don't have a global DEVMODE
44 HGLOBAL hDevMode = 0;
45 int devModeSize = 0;
46 if (!engine->globalDevMode()) {
47 devModeSize = sizeof(DEVMODE) + ep->devMode->dmDriverExtra;
48 hDevMode = GlobalAlloc(GHND, devModeSize);
49 if (hDevMode) {
50 void *dest = GlobalLock(hDevMode);
51 memcpy(dest, ep->devMode, devModeSize);
52 GlobalUnlock(hDevMode);
53 }
54 psd.hDevMode = hDevMode;
55 } else {
56 psd.hDevMode = engine->globalDevMode();
57 }
58
59 HGLOBAL *tempDevNames = engine->createGlobalDevNames();
60 psd.hDevNames = tempDevNames;
61
64 Q_ASSERT(!parent ||parent->testAttribute(Qt::WA_WState_Created));
65
66 QWindow *parentWindow = parent ? parent->windowHandle() : 0;
67 psd.hwndOwner = parentWindow ? (HWND)QGuiApplication::platformNativeInterface()->nativeResourceForWindow("handle", parentWindow) : 0;
68
69 psd.Flags = PSD_MARGINS;
70 QPageLayout layout = d->printer->pageLayout();
71 switch (layout.units()) {
74 break;
81 break;
82 }
83 qreal multiplier = 1.0;
84 if (layout.units() == QPageLayout::Millimeter) {
85 psd.Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
86 multiplier = 100.0;
87 } else { // QPageLayout::Inch)
88 psd.Flags |= PSD_INTHOUSANDTHSOFINCHES;
89 multiplier = 1000.0;
90 }
91 psd.rtMargin.left = layout.margins().left() * multiplier;
92 psd.rtMargin.top = layout.margins().top() * multiplier;
93 psd.rtMargin.right = layout.margins().right() * multiplier;
94 psd.rtMargin.bottom = layout.margins().bottom() * multiplier;
95
97 bool result = PageSetupDlg(&psd);
99 if (result) {
100 engine->setGlobalDevMode(psd.hDevNames, psd.hDevMode);
101 QPageSize pageSize;
102 // try to read orientation and paper size ID from the dialog's devmode struct
103 if (psd.hDevMode) {
104 DEVMODE *rDevmode = reinterpret_cast<DEVMODE*>(GlobalLock(psd.hDevMode));
105 if (rDevmode->dmFields & DM_ORIENTATION) {
106 layout.setOrientation(rDevmode->dmOrientation == DMORIENT_PORTRAIT
107 ? QPageLayout::Portrait : QPageLayout::Landscape);
108 }
109 if (rDevmode->dmFields & DM_PAPERSIZE)
110 pageSize = QPageSize::id(rDevmode->dmPaperSize);
111 GlobalUnlock(rDevmode);
112 }
113 // fall back to use our own matching, and assume that paper that's wider than long means landscape
114 if (!pageSize.isValid() || pageSize.id() == QPageSize::Custom) {
115 QSizeF unitSize(psd.ptPaperSize.x / multiplier, psd.ptPaperSize.y / multiplier);
116 if (unitSize.width() > unitSize.height()) {
117 layout.setOrientation(QPageLayout::Landscape);
118 unitSize.transpose();
119 } else {
120 layout.setOrientation(QPageLayout::Portrait);
121 }
122 pageSize = QPageSize(unitSize, layout.units() == QPageLayout::Inch
123 ? QPageSize::Inch : QPageSize::Millimeter);
124 }
125 layout.setPageSize(pageSize);
126
127 const QMarginsF margins(psd.rtMargin.left, psd.rtMargin.top, psd.rtMargin.right, psd.rtMargin.bottom);
128 layout.setMargins(margins / multiplier);
129 d->printer->setPageLayout(layout);
130
131 // copy from our temp DEVMODE struct
132 if (!engine->globalDevMode() && hDevMode) {
133 // Make sure memory is allocated
134 if (ep->ownsDevMode && ep->devMode)
135 free(ep->devMode);
136 ep->devMode = (DEVMODE *) malloc(devModeSize);
137 ep->ownsDevMode = true;
138
139 // Copy
140 void *src = GlobalLock(hDevMode);
141 memcpy(ep->devMode, src, devModeSize);
142 GlobalUnlock(hDevMode);
143 }
144 }
145
146 if (!engine->globalDevMode() && hDevMode)
147 GlobalFree(hDevMode);
148 GlobalFree(tempDevNames);
149 done(result);
150 return result;
151}
152
153void QPageSetupDialog::setVisible(bool visible)
154{
155 if (!visible)
156 return;
157 exec();
158}
159
static QWidget * activeWindow()
Returns the application top-level window that has the keyboard input focus, or \nullptr if no applica...
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
The QDialog class is the base class of dialog windows.
Definition qdialog.h:19
int result() const
In general returns the modal dialog's result code, Accepted or Rejected.
Definition qdialog.cpp:471
@ Rejected
Definition qdialog.h:30
virtual void done(int)
Closes the dialog and sets its result code to r.
Definition qdialog.cpp:598
static QPlatformNativeInterface * platformNativeInterface()
@ MetricSystem
Definition qlocale.h:858
MeasurementSystem measurementSystem() const
Definition qlocale.cpp:3186
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition qlocale.cpp:2742
\inmodule QtCore
Definition qmargins.h:274
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
QScopedPointer< QObjectData > d_ptr
Definition qobject.h:338
\inmodule QtGui
Definition qpagelayout.h:20
The QPageSetupDialog class provides a configuration dialog for the page-related options on a printer.
QPageSetupDialog(QPrinter *printer, QWidget *parent=nullptr)
Constructs a page setup dialog that configures printer with parent as the parent widget.
int exec() override
This virtual function is called to pop up the dialog.
\inmodule QtGui
Definition qpagesize.h:22
bool isValid() const
Returns true if this page size is valid.
PageSizeId id() const
Returns the standard QPageSize::PageSizeId of the page, or QPageSize::Custom.
virtual void * nativeResourceForWindow(const QByteArray &resource, QWindow *window)
\reentrant
Definition qprinter.h:28
@ NativeFormat
Definition qprinter.h:69
T * data() const noexcept
Returns the value of the pointer referenced by this object.
\inmodule QtCore
Definition qsize.h:207
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QLayout * layout() const
Returns the layout manager that is installed on this widget, or \nullptr if no layout manager is inst...
virtual void setVisible(bool visible)
Definition qwidget.cpp:8329
QWidget * parentWidget() const
Returns the parent of this widget, or \nullptr if it does not have any parent widget.
Definition qwidget.h:904
bool visible
whether the widget is visible
Definition qwidget.h:144
\inmodule QtGui
Definition qwindow.h:63
Combined button and popup list for selecting options.
@ WA_DontShowOnScreen
Definition qnamespace.h:382
@ WA_WState_Created
Definition qnamespace.h:326
GLenum src
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
double qreal
Definition qtypes.h:92
app setAttribute(Qt::AA_DontShowIconsInMenus)
QJSEngine engine
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent