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 PrintToFile The print to file option is enabled.
82 \value PrintSelection The print selection option is enabled.
83 \value PrintPageRange The page range selection option is enabled.
84 \value PrintShowPageSize Show the page size + margins page only if this is enabled.
85 \value PrintCollateCopies The collate copies option is enabled
86 \value PrintCurrentPage The print current page option is enabled
87*/
88
89/*!
90 Constructs an abstract print dialog for \a printer with \a parent
91 as parent widget.
92*/
93QAbstractPrintDialog::QAbstractPrintDialog(QPrinter *printer, QWidget *parent)
94 : QDialog(*(new QAbstractPrintDialogPrivate), parent)
95{
96 Q_D(QAbstractPrintDialog);
97 setWindowTitle(QCoreApplication::translate("QPrintDialog", "Print"));
98 d->setPrinter(printer);
99 d->minPage = printer->fromPage();
100 int to = printer->toPage();
101 d->maxPage = to > 0 ? to : INT_MAX;
102}
103
104/*!
105 \internal
106*/
107QAbstractPrintDialog::QAbstractPrintDialog(QAbstractPrintDialogPrivate &ptr,
108 QPrinter *printer,
109 QWidget *parent)
110 : QDialog(ptr, parent)
111{
112 Q_D(QAbstractPrintDialog);
113 setWindowTitle(QCoreApplication::translate("QPrintDialog", "Print"));
114 d->setPrinter(printer);
115}
116
117/*!
118 \internal
119*/
120QAbstractPrintDialog::~QAbstractPrintDialog()
121{
122 Q_D(QAbstractPrintDialog);
123 if (d->ownsPrinter)
124 delete d->printer;
125}
126
127/*!
128 Sets the given \a option to be enabled if \a on is true;
129 otherwise, clears the given \a option.
130
131 \sa options, testOption()
132*/
133void QPrintDialog::setOption(PrintDialogOption option, bool on)
134{
135 auto *d = static_cast<QAbstractPrintDialogPrivate *>(d_ptr.data());
136 if (!(d->options & option) != !on)
137 setOptions(d->options ^ option);
138}
139
140/*!
141 Returns \c true if the given \a option is enabled; otherwise, returns
142 false.
143
144 \sa options, setOption()
145*/
146bool QPrintDialog::testOption(PrintDialogOption option) const
147{
148 auto *d = static_cast<const QAbstractPrintDialogPrivate *>(d_ptr.data());
149 return (d->options & option) != 0;
150}
151
152/*!
153 \property QPrintDialog::options
154 \brief the various options that affect the look and feel of the dialog
155 \since 4.5
156
157 By default, all options are disabled.
158
159 Options should be set before showing the dialog. Setting them while the
160 dialog is visible is not guaranteed to have an immediate effect on the
161 dialog (depending on the option and on the platform).
162
163 \sa setOption(), testOption()
164*/
165void QPrintDialog::setOptions(PrintDialogOptions options)
166{
167 auto *d = static_cast<QAbstractPrintDialogPrivate *>(d_ptr.data());
168
169 PrintDialogOptions changed = (options ^ d->options);
170 if (!changed)
171 return;
172
173 d->options = options;
174}
175
176QPrintDialog::PrintDialogOptions QPrintDialog::options() const
177{
178 auto *d = static_cast<const QAbstractPrintDialogPrivate *>(d_ptr.data());
179 return d->options;
180}
181
182/*!
183 Sets the print range option in to be \a range.
184 */
185void QAbstractPrintDialog::setPrintRange(PrintRange range)
186{
187 Q_D(QAbstractPrintDialog);
188 d->printer->setPrintRange(QPrinter::PrintRange(range));
189}
190
191/*!
192 Returns the print range.
193*/
194QAbstractPrintDialog::PrintRange QAbstractPrintDialog::printRange() const
195{
196 Q_D(const QAbstractPrintDialog);
197 return QAbstractPrintDialog::PrintRange(d->pd->printRange);
198}
199
200/*!
201 Sets the page range in this dialog to be from \a min to \a max. This also
202 enables the PrintPageRange option.
203*/
204void QAbstractPrintDialog::setMinMax(int min, int max)
205{
206 Q_D(QAbstractPrintDialog);
207 Q_ASSERT_X(min <= max, "QAbstractPrintDialog::setMinMax",
208 "'min' must be less than or equal to 'max'");
209 d->minPage = min;
210 d->maxPage = max;
211 d->options |= PrintPageRange;
212}
213
214/*!
215 Returns the minimum page in the page range.
216 By default, this value is set to 1.
217*/
218int QAbstractPrintDialog::minPage() const
219{
220 Q_D(const QAbstractPrintDialog);
221 return d->minPage;
222}
223
224/*!
225 Returns the maximum page in the page range. As of Qt 4.4, this
226 function returns INT_MAX by default. Previous versions returned 1
227 by default.
228*/
229int QAbstractPrintDialog::maxPage() const
230{
231 Q_D(const QAbstractPrintDialog);
232 return d->maxPage;
233}
234
235/*!
236 Sets the range in the print dialog to be from \a from to \a to.
237*/
238void QAbstractPrintDialog::setFromTo(int from, int to)
239{
240 Q_D(QAbstractPrintDialog);
241 Q_ASSERT_X(from <= to, "QAbstractPrintDialog::setFromTo",
242 "'from' must be less than or equal to 'to'");
243 d->printer->setFromTo(from, to);
244
245 if (d->minPage == 0 && d->maxPage == 0)
246 setMinMax(1, to);
247}
248
249/*!
250 Returns the first page to be printed
251 By default, this value is set to 0.
252*/
253int QAbstractPrintDialog::fromPage() const
254{
255 Q_D(const QAbstractPrintDialog);
256 return d->printer->fromPage();
257}
258
259/*!
260 Returns the last page to be printed.
261 By default, this value is set to 0.
262*/
263int QAbstractPrintDialog::toPage() const
264{
265 Q_D(const QAbstractPrintDialog);
266 return d->printer->toPage();
267}
268
269
270/*!
271 Returns the printer that this printer dialog operates
272 on.
273*/
274QPrinter *QAbstractPrintDialog::printer() const
275{
276 Q_D(const QAbstractPrintDialog);
277 return d->printer;
278}
279
280void QAbstractPrintDialogPrivate::setPrinter(QPrinter *newPrinter)
281{
282 if (newPrinter) {
283 printer = newPrinter;
284 ownsPrinter = false;
285 if (printer->fromPage() || printer->toPage())
286 options |= QAbstractPrintDialog::PrintPageRange;
287 } else {
288 printer = new QPrinter;
289 ownsPrinter = true;
290 }
291 pd = printer->d_func();
292}
293
294/*!
295 \class QPrintDialog
296
297 \brief The QPrintDialog class provides a dialog for specifying
298 the printer's configuration.
299
300 \ingroup standard-dialogs
301 \ingroup printing
302 \inmodule QtPrintSupport
303
304 The dialog allows users to change document-related settings, such
305 as the paper size and orientation, type of print (color or
306 grayscale), range of pages, and number of copies to print.
307
308 Controls are also provided to enable users to choose from the
309 printers available, including any configured network printers.
310
311 Typically, QPrintDialog objects are constructed with a QPrinter
312 object, and executed using the exec() function.
313
314 \snippet code/src_gui_dialogs_qabstractprintdialog.cpp 0
315
316 If the dialog is accepted by the user, the QPrinter object is
317 correctly configured for printing.
318
319 \table
320 \row
321 \li \inlineimage plastique-printdialog.png
322 \li \inlineimage plastique-printdialog-properties.png
323 \endtable
324
325 The printer dialog (shown above in Plastique style) enables access to common
326 printing properties. On X11 platforms that use the CUPS printing system, the
327 settings for each available printer can be modified via the dialog's
328 \uicontrol{Properties} push button.
329
330 On Windows and \macos, the native print dialog is used, which means that
331 some QWidget and QDialog properties set on the dialog won't be respected.
332 The native print dialog on \macos does not support setting printer options,
333 i.e. setOptions() and setOption() have no effect.
334
335 In Qt 4.4, it was possible to use the static functions to show a sheet on
336 \macos. This is no longer supported in Qt 4.5. If you want this
337 functionality, use QPrintDialog::open().
338
339 \sa QPageSetupDialog, QPrinter
340*/
341
342/*!
343 \fn QPrintDialog::QPrintDialog(QPrinter *printer, QWidget *parent)
344
345 Constructs a new modal printer dialog for the given \a printer
346 with the given \a parent.
347*/
348
349/*!
350 \fn QPrintDialog::~QPrintDialog()
351
352 Destroys the print dialog.
353*/
354
355/*!
356 \fn int QPrintDialog::exec()
357 \reimp
358*/
359
360/*!
361 \since 4.4
362
363 Set a list of widgets as \a tabs to be shown on the print dialog, if supported.
364
365 Currently this option is only supported on X11.
366
367 Setting the option tabs will transfer their ownership to the print dialog.
368*/
369void QAbstractPrintDialog::setOptionTabs(const QList<QWidget*> &tabs)
370{
371 Q_D(QAbstractPrintDialog);
372 d->setTabs(tabs);
373}
374
375/*!
376
377 \fn void QPrintDialog::accepted(QPrinter *printer)
378
379 This signal is emitted when the user accepts the values set in the print dialog.
380 The \a printer parameter includes the printer that the settings were applied to.
381*/
382
383/*!
384 \fn QPrinter *QPrintDialog::printer()
385
386 Returns the printer that this printer dialog operates
387 on. This can be useful when using the QPrintDialog::open() method.
388*/
389
390/*!
391 Closes the dialog and sets its result code to \a result. If this dialog
392 is shown with exec(), done() causes the local event loop to finish,
393 and exec() to return \a result.
394
395 \note This function does not apply to the Native Print Dialog on the Mac
396 \macos and Windows platforms, because the dialog is required to be modal
397 and only the user can close it.
398
399 \sa QDialog::done()
400*/
401void QPrintDialog::done(int result)
402{
403 auto *d = static_cast<QAbstractPrintDialogPrivate *>(d_ptr.data());
404 QDialog::done(result);
405 if (result == Accepted)
406 emit accepted(printer());
407 if (d->receiverToDisconnectOnClose) {
408 disconnect(this, SIGNAL(accepted(QPrinter*)),
409 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
410 d->receiverToDisconnectOnClose = nullptr;
411 }
412 d->memberToDisconnectOnClose.clear();
413}
414
415/*!
416 \since 4.5
417 \overload
418
419 Opens the dialog and connects its accepted() signal to the slot specified
420 by \a receiver and \a member.
421
422 The signal will be disconnected from the slot when the dialog is closed.
423*/
424void QPrintDialog::open(QObject *receiver, const char *member)
425{
426 auto *d = static_cast<QAbstractPrintDialogPrivate *>(d_ptr.data());
427 connect(this, SIGNAL(accepted(QPrinter*)), receiver, member);
428 d->receiverToDisconnectOnClose = receiver;
429 d->memberToDisconnectOnClose = member;
430 QDialog::open();
431}
432
433QT_END_NAMESPACE
434