Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qabstractprintdialog.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
// Qt-Security score:significant reason:default
4
5
#
include
"qabstractprintdialog_p.h"
6
#
include
"qcoreapplication.h"
7
#
include
"qprintdialog.h"
8
#
include
"qprinter.h"
9
#
include
"private/qprinter_p.h"
10
11
QT_BEGIN_NAMESPACE
12
13
/*!
14
\class QAbstractPrintDialog
15
\brief The QAbstractPrintDialog class provides a base implementation for
16
print dialogs used to configure printers.
17
18
\ingroup printing
19
\inmodule QtPrintSupport
20
21
This class implements getter and setter functions that are used to
22
customize settings shown in print dialogs, but it is not used directly.
23
Use QPrintDialog to display a print dialog in your application.
24
25
\sa QPrintDialog, QPrinter
26
*/
27
28
/*!
29
\enum QAbstractPrintDialog::PrintRange
30
31
Used to specify the print range selection option.
32
33
\value AllPages All pages should be printed.
34
\value Selection Only the selection should be printed.
35
\value PageRange The specified page range should be printed.
36
\value CurrentPage Only the currently visible page should be printed.
37
38
\sa QPrinter::PrintRange
39
*/
40
41
/*!
42
\enum QAbstractPrintDialog::PrintDialogOption
43
44
Used to specify which parts of the print dialog should be visible.
45
46
\value PrintToFile The print to file option is enabled.
47
\value PrintSelection The print selection option is enabled.
48
\value PrintPageRange The page range selection option is enabled.
49
\value PrintShowPageSize Show the page size + margins page only if this is enabled.
50
\value PrintCollateCopies The collate copies option is enabled
51
\value PrintCurrentPage The print current page option is enabled
52
*/
53
54
/*!
55
Constructs an abstract print dialog for \a printer with \a parent
56
as parent widget.
57
*/
58
QAbstractPrintDialog::QAbstractPrintDialog(QPrinter *printer, QWidget *parent)
59
: QDialog(*(
new
QAbstractPrintDialogPrivate), parent)
60
{
61
Q_D(QAbstractPrintDialog);
62
setWindowTitle(QCoreApplication::translate(
"QPrintDialog"
,
"Print"
));
63
d->setPrinter(printer);
64
d->minPage = printer->fromPage();
65
int
to = printer->toPage();
66
d->maxPage = to > 0 ? to : INT_MAX;
67
}
68
69
/*!
70
\internal
71
*/
72
QAbstractPrintDialog::QAbstractPrintDialog(QAbstractPrintDialogPrivate &ptr,
73
QPrinter *printer,
74
QWidget *parent)
75
: QDialog(ptr, parent)
76
{
77
Q_D(QAbstractPrintDialog);
78
setWindowTitle(QCoreApplication::translate(
"QPrintDialog"
,
"Print"
));
79
d->setPrinter(printer);
80
}
81
82
/*!
83
\internal
84
*/
85
QAbstractPrintDialog::~QAbstractPrintDialog()
86
{
87
Q_D(QAbstractPrintDialog);
88
if
(d->ownsPrinter)
89
delete
d->printer;
90
}
91
92
/*!
93
Sets the given \a option to be enabled if \a on is true;
94
otherwise, clears the given \a option.
95
96
\sa options, testOption()
97
*/
98
void
QPrintDialog::setOption(PrintDialogOption option,
bool
on)
99
{
100
auto
*d =
static_cast
<QAbstractPrintDialogPrivate *>(d_ptr.data());
101
if
(!(d->options & option) != !on)
102
setOptions(d->options ^ option);
103
}
104
105
/*!
106
Returns \c true if the given \a option is enabled; otherwise, returns
107
false.
108
109
\sa options, setOption()
110
*/
111
bool
QPrintDialog::testOption(PrintDialogOption option)
const
112
{
113
auto
*d =
static_cast
<
const
QAbstractPrintDialogPrivate *>(d_ptr.data());
114
return
(d->options & option) != 0;
115
}
116
117
/*!
118
\property QPrintDialog::options
119
\brief the various options that affect the look and feel of the dialog
120
\since 4.5
121
122
By default, all options are disabled.
123
124
Options should be set before showing the dialog. Setting them while the
125
dialog is visible is not guaranteed to have an immediate effect on the
126
dialog (depending on the option and on the platform).
127
128
\sa setOption(), testOption()
129
*/
130
void
QPrintDialog::setOptions(PrintDialogOptions options)
131
{
132
auto
*d =
static_cast
<QAbstractPrintDialogPrivate *>(d_ptr.data());
133
134
PrintDialogOptions changed = (options ^ d->options);
135
if
(!changed)
136
return
;
137
138
d->options = options;
139
}
140
141
QPrintDialog::PrintDialogOptions QPrintDialog::options()
const
142
{
143
auto
*d =
static_cast
<
const
QAbstractPrintDialogPrivate *>(d_ptr.data());
144
return
d->options;
145
}
146
147
/*!
148
Sets the print range option in to be \a range.
149
*/
150
void
QAbstractPrintDialog::setPrintRange(PrintRange range)
151
{
152
Q_D(QAbstractPrintDialog);
153
d->printer->setPrintRange(QPrinter::PrintRange(range));
154
}
155
156
/*!
157
Returns the print range.
158
*/
159
QAbstractPrintDialog::PrintRange QAbstractPrintDialog::printRange()
const
160
{
161
Q_D(
const
QAbstractPrintDialog);
162
return
QAbstractPrintDialog::PrintRange(d->pd->printRange);
163
}
164
165
/*!
166
Sets the page range in this dialog to be from \a min to \a max. This also
167
enables the PrintPageRange option.
168
*/
169
void
QAbstractPrintDialog::setMinMax(
int
min,
int
max)
170
{
171
Q_D(QAbstractPrintDialog);
172
Q_ASSERT_X(min <= max,
"QAbstractPrintDialog::setMinMax"
,
173
"'min' must be less than or equal to 'max'"
);
174
d->minPage = min;
175
d->maxPage = max;
176
d->options |= PrintPageRange;
177
}
178
179
/*!
180
Returns the minimum page in the page range.
181
By default, this value is set to 1.
182
*/
183
int
QAbstractPrintDialog::minPage()
const
184
{
185
Q_D(
const
QAbstractPrintDialog);
186
return
d->minPage;
187
}
188
189
/*!
190
Returns the maximum page in the page range. As of Qt 4.4, this
191
function returns INT_MAX by default. Previous versions returned 1
192
by default.
193
*/
194
int
QAbstractPrintDialog::maxPage()
const
195
{
196
Q_D(
const
QAbstractPrintDialog);
197
return
d->maxPage;
198
}
199
200
/*!
201
Sets the range in the print dialog to be from \a from to \a to.
202
*/
203
void
QAbstractPrintDialog::setFromTo(
int
from,
int
to)
204
{
205
Q_D(QAbstractPrintDialog);
206
Q_ASSERT_X(from <= to,
"QAbstractPrintDialog::setFromTo"
,
207
"'from' must be less than or equal to 'to'"
);
208
d->printer->setFromTo(from, to);
209
210
if
(d->minPage == 0 && d->maxPage == 0)
211
setMinMax(1, to);
212
}
213
214
/*!
215
Returns the first page to be printed
216
By default, this value is set to 0.
217
*/
218
int
QAbstractPrintDialog::fromPage()
const
219
{
220
Q_D(
const
QAbstractPrintDialog);
221
return
d->printer->fromPage();
222
}
223
224
/*!
225
Returns the last page to be printed.
226
By default, this value is set to 0.
227
*/
228
int
QAbstractPrintDialog::toPage()
const
229
{
230
Q_D(
const
QAbstractPrintDialog);
231
return
d->printer->toPage();
232
}
233
234
235
/*!
236
Returns the printer that this printer dialog operates
237
on.
238
*/
239
QPrinter *QAbstractPrintDialog::printer()
const
240
{
241
Q_D(
const
QAbstractPrintDialog);
242
return
d->printer;
243
}
244
245
void
QAbstractPrintDialogPrivate
::
setPrinter
(QPrinter *newPrinter)
246
{
247
if
(newPrinter) {
248
printer = newPrinter;
249
ownsPrinter
=
false
;
250
if
(printer->fromPage() || printer->toPage())
251
options |= QAbstractPrintDialog::PrintPageRange;
252
}
else
{
253
printer =
new
QPrinter;
254
ownsPrinter
=
true
;
255
}
256
pd
= printer->d_func();
257
}
258
259
/*!
260
\class QPrintDialog
261
262
\brief The QPrintDialog class provides a dialog for specifying
263
the printer's configuration.
264
265
\ingroup standard-dialogs
266
\ingroup printing
267
\inmodule QtPrintSupport
268
269
The dialog allows users to change document-related settings, such
270
as the paper size and orientation, type of print (color or
271
grayscale), range of pages, and number of copies to print.
272
273
Controls are also provided to enable users to choose from the
274
printers available, including any configured network printers.
275
276
Typically, QPrintDialog objects are constructed with a QPrinter
277
object, and executed using the exec() function.
278
279
\snippet code/src_gui_dialogs_qabstractprintdialog.cpp 0
280
281
If the dialog is accepted by the user, the QPrinter object is
282
correctly configured for printing.
283
284
\table
285
\row
286
\li \inlineimage plastique-printdialog.png {Screenshot of menu to print a
287
document}
288
\li \inlineimage plastique-printdialog-properties.png {Screenshot of settings
289
in which the document will be printed}
290
\endtable
291
292
The printer dialog (shown above in Plastique style) enables access to common
293
printing properties. On X11 platforms that use the CUPS printing system, the
294
settings for each available printer can be modified via the dialog's
295
\uicontrol{Properties} push button.
296
297
On Windows and \macos, the native print dialog is used, which means that
298
some QWidget and QDialog properties set on the dialog won't be respected.
299
The native print dialog on \macos does not support setting printer options,
300
i.e. setOptions() and setOption() have no effect.
301
302
In Qt 4.4, it was possible to use the static functions to show a sheet on
303
\macos. This is no longer supported in Qt 4.5. If you want this
304
functionality, use QPrintDialog::open().
305
306
\sa QPageSetupDialog, QPrinter
307
*/
308
309
/*!
310
\fn QPrintDialog::QPrintDialog(QPrinter *printer, QWidget *parent)
311
312
Constructs a new modal printer dialog for the given \a printer
313
with the given \a parent.
314
*/
315
316
/*!
317
\fn QPrintDialog::~QPrintDialog()
318
319
Destroys the print dialog.
320
*/
321
322
/*!
323
\fn int QPrintDialog::exec()
324
\reimp
325
*/
326
327
/*!
328
\since 4.4
329
330
Set a list of widgets as \a tabs to be shown on the print dialog, if supported.
331
332
Currently this option is only supported on X11.
333
334
Setting the option tabs will transfer their ownership to the print dialog.
335
*/
336
void
QAbstractPrintDialog::setOptionTabs(
const
QList<QWidget*> &tabs)
337
{
338
Q_D(QAbstractPrintDialog);
339
d->setTabs(tabs);
340
}
341
342
/*!
343
344
\fn void QPrintDialog::accepted(QPrinter *printer)
345
346
This signal is emitted when the user accepts the values set in the print dialog.
347
The \a printer parameter includes the printer that the settings were applied to.
348
*/
349
350
/*!
351
\fn QPrinter *QPrintDialog::printer()
352
353
Returns the printer that this printer dialog operates
354
on. This can be useful when using the QPrintDialog::open() method.
355
*/
356
357
/*!
358
Closes the dialog and sets its result code to \a result. If this dialog
359
is shown with exec(), done() causes the local event loop to finish,
360
and exec() to return \a result.
361
362
\note This function does not apply to the Native Print Dialog on the Mac
363
\macos and Windows platforms, because the dialog is required to be modal
364
and only the user can close it.
365
366
\sa QDialog::done()
367
*/
368
void
QPrintDialog::done(
int
result)
369
{
370
auto
*d =
static_cast
<QAbstractPrintDialogPrivate *>(d_ptr.data());
371
if
(result == Accepted) {
372
// Emit accepted(QPrinter*) at the same time as the dialog
373
// is accepted. Doing it here is too late, as done() will
374
// also emit finished().
375
QObject::connect(
this
, &QDialog::accepted,
this
, [
this
]{
376
emit accepted(printer());
377
}, Qt::SingleShotConnection);
378
}
379
QDialog::done(result);
380
if
(d->receiverToDisconnectOnClose) {
381
disconnect(
this
, SIGNAL(accepted(QPrinter*)),
382
d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
383
d->receiverToDisconnectOnClose =
nullptr
;
384
}
385
d->memberToDisconnectOnClose.clear();
386
}
387
388
/*!
389
\since 4.5
390
\overload
391
392
Opens the dialog and connects its accepted() signal to the slot specified
393
by \a receiver and \a member.
394
395
The signal will be disconnected from the slot when the dialog is closed.
396
*/
397
void
QPrintDialog::open(QObject *receiver,
const
char
*member)
398
{
399
auto
*d =
static_cast
<QAbstractPrintDialogPrivate *>(d_ptr.data());
400
connect(
this
, SIGNAL(accepted(QPrinter*)), receiver, member);
401
d->receiverToDisconnectOnClose = receiver;
402
d->memberToDisconnectOnClose = member;
403
QDialog::open();
404
}
405
406
QT_END_NAMESPACE
407
408
#
include
"moc_qabstractprintdialog.cpp"
QAbstractPrintDialogPrivate
Definition
qabstractprintdialog_p.h:34
QAbstractPrintDialogPrivate::ownsPrinter
bool ownsPrinter
Definition
qabstractprintdialog_p.h:58
QAbstractPrintDialogPrivate::setPrinter
void setPrinter(QPrinter *newPrinter)
Definition
qabstractprintdialog.cpp:245
QAbstractPrintDialogPrivate::pd
QPrinterPrivate * pd
Definition
qabstractprintdialog_p.h:47
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtbase
src
printsupport
dialogs
qabstractprintdialog.cpp
Generated on
for Qt by
1.18.0