Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
7#include "qprintdialog.h"
8#include "qprinter.h"
9#include "private/qprinter_p.h"
10
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*/
58QAbstractPrintDialog::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*/
72QAbstractPrintDialog::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*/
85QAbstractPrintDialog::~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*/
98void 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*/
111bool 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*/
130void 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
141QPrintDialog::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 */
150void 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*/
159QAbstractPrintDialog::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*/
169void 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*/
183int 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*/
194int 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*/
203void 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*/
218int 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*/
228int 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*/
239QPrinter *QAbstractPrintDialog::printer() const
240{
241 Q_D(const QAbstractPrintDialog);
242 return d->printer;
243}
244
245void 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
287 \li \inlineimage plastique-printdialog-properties.png
288 \endtable
289
290 The printer dialog (shown above in Plastique style) enables access to common
291 printing properties. On X11 platforms that use the CUPS printing system, the
292 settings for each available printer can be modified via the dialog's
293 \uicontrol{Properties} push button.
294
295 On Windows and \macos, the native print dialog is used, which means that
296 some QWidget and QDialog properties set on the dialog won't be respected.
297 The native print dialog on \macos does not support setting printer options,
298 i.e. setOptions() and setOption() have no effect.
299
300 In Qt 4.4, it was possible to use the static functions to show a sheet on
301 \macos. This is no longer supported in Qt 4.5. If you want this
302 functionality, use QPrintDialog::open().
303
304 \sa QPageSetupDialog, QPrinter
305*/
306
307/*!
308 \fn QPrintDialog::QPrintDialog(QPrinter *printer, QWidget *parent)
309
310 Constructs a new modal printer dialog for the given \a printer
311 with the given \a parent.
312*/
313
314/*!
315 \fn QPrintDialog::~QPrintDialog()
316
317 Destroys the print dialog.
318*/
319
320/*!
321 \fn int QPrintDialog::exec()
322 \reimp
323*/
324
325/*!
326 \since 4.4
327
328 Set a list of widgets as \a tabs to be shown on the print dialog, if supported.
329
330 Currently this option is only supported on X11.
331
332 Setting the option tabs will transfer their ownership to the print dialog.
333*/
334void QAbstractPrintDialog::setOptionTabs(const QList<QWidget*> &tabs)
335{
336 Q_D(QAbstractPrintDialog);
337 d->setTabs(tabs);
338}
339
340/*!
341
342 \fn void QPrintDialog::accepted(QPrinter *printer)
343
344 This signal is emitted when the user accepts the values set in the print dialog.
345 The \a printer parameter includes the printer that the settings were applied to.
346*/
347
348/*!
349 \fn QPrinter *QPrintDialog::printer()
350
351 Returns the printer that this printer dialog operates
352 on. This can be useful when using the QPrintDialog::open() method.
353*/
354
355/*!
356 Closes the dialog and sets its result code to \a result. If this dialog
357 is shown with exec(), done() causes the local event loop to finish,
358 and exec() to return \a result.
359
360 \note This function does not apply to the Native Print Dialog on the Mac
361 \macos and Windows platforms, because the dialog is required to be modal
362 and only the user can close it.
363
364 \sa QDialog::done()
365*/
366void QPrintDialog::done(int result)
367{
368 auto *d = static_cast<QAbstractPrintDialogPrivate *>(d_ptr.data());
369 if (result == Accepted) {
370 // Emit accepted(QPrinter*) at the same time as the dialog
371 // is accepted. Doing it here is too late, as done() will
372 // also emit finished().
373 QObject::connect(this, &QDialog::accepted, this, [this]{
374 emit accepted(printer());
375 }, Qt::SingleShotConnection);
376 }
377 QDialog::done(result);
378 if (d->receiverToDisconnectOnClose) {
379 disconnect(this, SIGNAL(accepted(QPrinter*)),
380 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
381 d->receiverToDisconnectOnClose = nullptr;
382 }
383 d->memberToDisconnectOnClose.clear();
384}
385
386/*!
387 \since 4.5
388 \overload
389
390 Opens the dialog and connects its accepted() signal to the slot specified
391 by \a receiver and \a member.
392
393 The signal will be disconnected from the slot when the dialog is closed.
394*/
395void QPrintDialog::open(QObject *receiver, const char *member)
396{
397 auto *d = static_cast<QAbstractPrintDialogPrivate *>(d_ptr.data());
398 connect(this, SIGNAL(accepted(QPrinter*)), receiver, member);
399 d->receiverToDisconnectOnClose = receiver;
400 d->memberToDisconnectOnClose = member;
401 QDialog::open();
402}
403
404QT_END_NAMESPACE
405
406#include "moc_qabstractprintdialog.cpp"
void setPrinter(QPrinter *newPrinter)