1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qabstractprintdialog_p.h"
41#include "qcoreapplication.h"
42#include "qprintdialog.h"
43#include "qprinter.h"
44#include "private/qprinter_p.h"
45
46QT_BEGIN_NAMESPACE
47
48/*!
49 \class QAbstractPrintDialog
50 \brief The QAbstractPrintDialog class provides a base implementation for
51 print dialogs used to configure printers.
52
53 \ingroup printing
54 \inmodule QtPrintSupport
55
56 This class implements getter and setter functions that are used to
57 customize settings shown in print dialogs, but it is not used directly.
58 Use QPrintDialog to display a print dialog in your application.
59
60 \sa QPrintDialog, QPrinter
61*/
62
63/*!
64 \enum QAbstractPrintDialog::PrintRange
65
66 Used to specify the print range selection option.
67
68 \value AllPages All pages should be printed.
69 \value Selection Only the selection should be printed.
70 \value PageRange The specified page range should be printed.
71 \value CurrentPage Only the currently visible page should be printed.
72
73 \sa QPrinter::PrintRange
74*/
75
76/*!
77 \enum QAbstractPrintDialog::PrintDialogOption
78
79 Used to specify which parts of the print dialog should be visible.
80
81 \value None None of the options are enabled.
82 \value PrintToFile The print to file option is enabled.
83 \value PrintSelection The print selection option is enabled.
84 \value PrintPageRange The page range selection option is enabled.
85 \value PrintShowPageSize Show the page size + margins page only if this is enabled.
86 \value PrintCollateCopies The collate copies option is enabled
87 \value PrintCurrentPage The print current page option is enabled
88
89 This value is obsolete and does nothing since Qt 4.5:
90
91 \value DontUseSheet In previous versions of Qt, exec() the print dialog
92 would create a sheet by default the dialog was given a parent.
93 This is no longer supported in Qt 4.5. If you want to use sheets, use
94 QPrintDialog::open() instead.
95*/
96
97/*!
98 Constructs an abstract print dialog for \a printer with \a parent
99 as parent widget.
100*/
101QAbstractPrintDialog::QAbstractPrintDialog(QPrinter *printer, QWidget *parent)
102 : QDialog(*(new QAbstractPrintDialogPrivate), parent)
103{
104 Q_D(QAbstractPrintDialog);
105 setWindowTitle(QCoreApplication::translate(context: "QPrintDialog", key: "Print"));
106 d->setPrinter(printer);
107 d->minPage = printer->fromPage();
108 int to = printer->toPage();
109 d->maxPage = to > 0 ? to : INT_MAX;
110}
111
112/*!
113 \internal
114*/
115QAbstractPrintDialog::QAbstractPrintDialog(QAbstractPrintDialogPrivate &ptr,
116 QPrinter *printer,
117 QWidget *parent)
118 : QDialog(ptr, parent)
119{
120 Q_D(QAbstractPrintDialog);
121 setWindowTitle(QCoreApplication::translate(context: "QPrintDialog", key: "Print"));
122 d->setPrinter(printer);
123}
124
125/*!
126 \internal
127*/
128QAbstractPrintDialog::~QAbstractPrintDialog()
129{
130 Q_D(QAbstractPrintDialog);
131 if (d->ownsPrinter)
132 delete d->printer;
133}
134
135/*!
136 Sets the given \a option to be enabled if \a on is true;
137 otherwise, clears the given \a option.
138
139 \sa options, testOption()
140*/
141void QPrintDialog::setOption(PrintDialogOption option, bool on)
142{
143 auto *d = static_cast<QAbstractPrintDialogPrivate *>(d_ptr.data());
144 if (!(d->options & option) != !on)
145 setOptions(d->options ^ option);
146}
147
148/*!
149 Returns \c true if the given \a option is enabled; otherwise, returns
150 false.
151
152 \sa options, setOption()
153*/
154bool QPrintDialog::testOption(PrintDialogOption option) const
155{
156 auto *d = static_cast<const QAbstractPrintDialogPrivate *>(d_ptr.data());
157 return (d->options & option) != 0;
158}
159
160/*!
161 \property QPrintDialog::options
162 \brief the various options that affect the look and feel of the dialog
163 \since 4.5
164
165 By default, all options are disabled.
166
167 Options should be set before showing the dialog. Setting them while the
168 dialog is visible is not guaranteed to have an immediate effect on the
169 dialog (depending on the option and on the platform).
170
171 \sa setOption(), testOption()
172*/
173void QPrintDialog::setOptions(PrintDialogOptions options)
174{
175 auto *d = static_cast<QAbstractPrintDialogPrivate *>(d_ptr.data());
176
177 PrintDialogOptions changed = (options ^ d->options);
178 if (!changed)
179 return;
180
181 d->options = options;
182}
183
184QPrintDialog::PrintDialogOptions QPrintDialog::options() const
185{
186 auto *d = static_cast<const QAbstractPrintDialogPrivate *>(d_ptr.data());
187 return d->options;
188}
189
190/*!
191 \obsolete
192
193 Use QPrintDialog::setOptions() instead.
194*/
195void QAbstractPrintDialog::setEnabledOptions(PrintDialogOptions options)
196{
197 Q_D(QAbstractPrintDialog);
198 d->options = options;
199}
200
201/*!
202 \obsolete
203
204 Use QPrintDialog::setOption(\a option, true) instead.
205*/
206void QAbstractPrintDialog::addEnabledOption(PrintDialogOption option)
207{
208 Q_D(QAbstractPrintDialog);
209 d->options |= option;
210}
211
212/*!
213 \obsolete
214
215 Use QPrintDialog::options() instead.
216*/
217QAbstractPrintDialog::PrintDialogOptions QAbstractPrintDialog::enabledOptions() const
218{
219 Q_D(const QAbstractPrintDialog);
220 return d->options;
221}
222
223/*!
224 \obsolete
225
226 Use QPrintDialog::testOption(\a option) instead.
227*/
228bool QAbstractPrintDialog::isOptionEnabled(PrintDialogOption option) const
229{
230 Q_D(const QAbstractPrintDialog);
231 return d->options & option;
232}
233
234/*!
235 Sets the print range option in to be \a range.
236 */
237void QAbstractPrintDialog::setPrintRange(PrintRange range)
238{
239 Q_D(QAbstractPrintDialog);
240 d->printer->setPrintRange(QPrinter::PrintRange(range));
241}
242
243/*!
244 Returns the print range.
245*/
246QAbstractPrintDialog::PrintRange QAbstractPrintDialog::printRange() const
247{
248 Q_D(const QAbstractPrintDialog);
249 return QAbstractPrintDialog::PrintRange(d->pd->printRange);
250}
251
252/*!
253 Sets the page range in this dialog to be from \a min to \a max. This also
254 enables the PrintPageRange option.
255*/
256void QAbstractPrintDialog::setMinMax(int min, int max)
257{
258 Q_D(QAbstractPrintDialog);
259 Q_ASSERT_X(min <= max, "QAbstractPrintDialog::setMinMax",
260 "'min' must be less than or equal to 'max'");
261 d->minPage = min;
262 d->maxPage = max;
263 d->options |= PrintPageRange;
264}
265
266/*!
267 Returns the minimum page in the page range.
268 By default, this value is set to 1.
269*/
270int QAbstractPrintDialog::minPage() const
271{
272 Q_D(const QAbstractPrintDialog);
273 return d->minPage;
274}
275
276/*!
277 Returns the maximum page in the page range. As of Qt 4.4, this
278 function returns INT_MAX by default. Previous versions returned 1
279 by default.
280*/
281int QAbstractPrintDialog::maxPage() const
282{
283 Q_D(const QAbstractPrintDialog);
284 return d->maxPage;
285}
286
287/*!
288 Sets the range in the print dialog to be from \a from to \a to.
289*/
290void QAbstractPrintDialog::setFromTo(int from, int to)
291{
292 Q_D(QAbstractPrintDialog);
293 Q_ASSERT_X(from <= to, "QAbstractPrintDialog::setFromTo",
294 "'from' must be less than or equal to 'to'");
295 d->printer->setFromTo(fromPage: from, toPage: to);
296
297 if (d->minPage == 0 && d->maxPage == 0)
298 setMinMax(min: 1, max: to);
299}
300
301/*!
302 Returns the first page to be printed
303 By default, this value is set to 0.
304*/
305int QAbstractPrintDialog::fromPage() const
306{
307 Q_D(const QAbstractPrintDialog);
308 return d->printer->fromPage();
309}
310
311/*!
312 Returns the last page to be printed.
313 By default, this value is set to 0.
314*/
315int QAbstractPrintDialog::toPage() const
316{
317 Q_D(const QAbstractPrintDialog);
318 return d->printer->toPage();
319}
320
321
322/*!
323 Returns the printer that this printer dialog operates
324 on.
325*/
326QPrinter *QAbstractPrintDialog::printer() const
327{
328 Q_D(const QAbstractPrintDialog);
329 return d->printer;
330}
331
332void QAbstractPrintDialogPrivate::setPrinter(QPrinter *newPrinter)
333{
334 if (newPrinter) {
335 printer = newPrinter;
336 ownsPrinter = false;
337 if (printer->fromPage() || printer->toPage())
338 options |= QAbstractPrintDialog::PrintPageRange;
339 } else {
340 printer = new QPrinter;
341 ownsPrinter = true;
342 }
343 pd = printer->d_func();
344}
345
346/*!
347 \class QPrintDialog
348
349 \brief The QPrintDialog class provides a dialog for specifying
350 the printer's configuration.
351
352 \ingroup standard-dialogs
353 \ingroup printing
354 \inmodule QtPrintSupport
355
356 The dialog allows users to change document-related settings, such
357 as the paper size and orientation, type of print (color or
358 grayscale), range of pages, and number of copies to print.
359
360 Controls are also provided to enable users to choose from the
361 printers available, including any configured network printers.
362
363 Typically, QPrintDialog objects are constructed with a QPrinter
364 object, and executed using the exec() function.
365
366 \snippet code/src_gui_dialogs_qabstractprintdialog.cpp 0
367
368 If the dialog is accepted by the user, the QPrinter object is
369 correctly configured for printing.
370
371 \table
372 \row
373 \li \inlineimage plastique-printdialog.png
374 \li \inlineimage plastique-printdialog-properties.png
375 \endtable
376
377 The printer dialog (shown above in Plastique style) enables access to common
378 printing properties. On X11 platforms that use the CUPS printing system, the
379 settings for each available printer can be modified via the dialog's
380 \uicontrol{Properties} push button.
381
382 On Windows and \macos, the native print dialog is used, which means that
383 some QWidget and QDialog properties set on the dialog won't be respected.
384 The native print dialog on \macos does not support setting printer options,
385 i.e. setOptions() and setOption() have no effect.
386
387 In Qt 4.4, it was possible to use the static functions to show a sheet on
388 \macos. This is no longer supported in Qt 4.5. If you want this
389 functionality, use QPrintDialog::open().
390
391 \sa QPageSetupDialog, QPrinter
392*/
393
394/*!
395 \fn QPrintDialog::QPrintDialog(QPrinter *printer, QWidget *parent)
396
397 Constructs a new modal printer dialog for the given \a printer
398 with the given \a parent.
399*/
400
401/*!
402 \fn QPrintDialog::~QPrintDialog()
403
404 Destroys the print dialog.
405*/
406
407/*!
408 \fn int QPrintDialog::exec()
409 \reimp
410*/
411
412/*!
413 \since 4.4
414
415 Set a list of widgets as \a tabs to be shown on the print dialog, if supported.
416
417 Currently this option is only supported on X11.
418
419 Setting the option tabs will transfer their ownership to the print dialog.
420*/
421void QAbstractPrintDialog::setOptionTabs(const QList<QWidget*> &tabs)
422{
423 Q_D(QAbstractPrintDialog);
424 d->setTabs(tabs);
425}
426
427/*!
428
429 \fn void QPrintDialog::accepted(QPrinter *printer)
430
431 This signal is emitted when the user accepts the values set in the print dialog.
432 The \a printer parameter includes the printer that the settings were applied to.
433*/
434
435/*!
436 \fn QPrinter *QPrintDialog::printer()
437
438 Returns the printer that this printer dialog operates
439 on. This can be useful when using the QPrintDialog::open() method.
440*/
441
442/*!
443 Closes the dialog and sets its result code to \a result. If this dialog
444 is shown with exec(), done() causes the local event loop to finish,
445 and exec() to return \a result.
446
447 \note This function does not apply to the Native Print Dialog on the Mac
448 \macos and Windows platforms, because the dialog is required to be modal
449 and only the user can close it.
450
451 \sa QDialog::done()
452*/
453void QPrintDialog::done(int result)
454{
455 auto *d = static_cast<QAbstractPrintDialogPrivate *>(d_ptr.data());
456 QDialog::done(result);
457 if (result == Accepted)
458 emit accepted(printer: printer());
459 if (d->receiverToDisconnectOnClose) {
460 disconnect(sender: this, SIGNAL(accepted(QPrinter*)),
461 receiver: d->receiverToDisconnectOnClose, member: d->memberToDisconnectOnClose);
462 d->receiverToDisconnectOnClose = nullptr;
463 }
464 d->memberToDisconnectOnClose.clear();
465}
466
467/*!
468 \since 4.5
469 \overload
470
471 Opens the dialog and connects its accepted() signal to the slot specified
472 by \a receiver and \a member.
473
474 The signal will be disconnected from the slot when the dialog is closed.
475*/
476void QPrintDialog::open(QObject *receiver, const char *member)
477{
478 auto *d = static_cast<QAbstractPrintDialogPrivate *>(d_ptr.data());
479 connect(sender: this, SIGNAL(accepted(QPrinter*)), receiver, member);
480 d->receiverToDisconnectOnClose = receiver;
481 d->memberToDisconnectOnClose = member;
482 QDialog::open();
483}
484
485QT_END_NAMESPACE
486

source code of qtbase/src/printsupport/dialogs/qabstractprintdialog.cpp