1 /****************************************************************************
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtGui module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qabstractprintdialog_p.h"
43 #include "qcoreapplication.h"
44 #include "qprintdialog.h"
46 #include "private/qprinter_p.h"
48 #ifndef QT_NO_PRINTDIALOG
53 class QPrintDialogPrivate : public QAbstractPrintDialogPrivate
58 \class QAbstractPrintDialog
59 \brief The QAbstractPrintDialog class provides a base implementation for
60 print dialogs used to configure printers.
64 This class implements getter and setter functions that are used to
65 customize settings shown in print dialogs, but it is not used directly.
66 Use QPrintDialog to display a print dialog in your application.
68 In Symbian, there is no support for printing. Hence, this dialog should not
71 \sa QPrintDialog, QPrinter, {Printing with Qt}
75 \enum QAbstractPrintDialog::PrintRange
77 Used to specify the print range selection option.
79 \value AllPages All pages should be printed.
80 \value Selection Only the selection should be printed.
81 \value PageRange The specified page range should be printed.
82 \value CurrentPage Only the currently visible page should be printed. (This value was introduced in 4.7.)
84 \sa QPrinter::PrintRange
88 \enum QAbstractPrintDialog::PrintDialogOption
90 Used to specify which parts of the print dialog should be visible.
92 \value None None of the options are enabled.
93 \value PrintToFile The print to file option is enabled.
94 \value PrintSelection The print selection option is enabled.
95 \value PrintPageRange The page range selection option is enabled.
96 \value PrintShowPageSize Show the page size + margins page only if this is enabled.
97 \value PrintCollateCopies The collate copies option is enabled
98 \value PrintCurrentPage The print current page option is enabled (This value was introduced in 4.7.)
100 This value is obsolete and does nothing since Qt 4.5:
102 \value DontUseSheet In previous versions of Qt, exec() the print dialog
103 would create a sheet by default the dialog was given a parent.
104 This is no longer supported in Qt 4.5. If you want to use sheets, use
105 QPrintDialog::open() instead.
109 Constructs an abstract print dialog for \a printer with \a parent
112 QAbstractPrintDialog::QAbstractPrintDialog(QPrinter *printer, QWidget *parent)
113 : QDialog(*(new QAbstractPrintDialogPrivate), parent)
115 Q_D(QAbstractPrintDialog);
116 setWindowTitle(QCoreApplication::translate("QPrintDialog", "Print"));
117 d->setPrinter(printer);
123 QAbstractPrintDialog::QAbstractPrintDialog(QAbstractPrintDialogPrivate &ptr,
126 : QDialog(ptr, parent)
128 Q_D(QAbstractPrintDialog);
129 setWindowTitle(QCoreApplication::translate("QPrintDialog", "Print"));
130 d->setPrinter(printer);
136 QAbstractPrintDialog::~QAbstractPrintDialog()
138 Q_D(QAbstractPrintDialog);
144 Sets the given \a option to be enabled if \a on is true;
145 otherwise, clears the given \a option.
147 \sa options, testOption()
149 void QPrintDialog::setOption(PrintDialogOption option, bool on)
152 if (!(d->pd->options & option) != !on)
153 setOptions(d->pd->options ^ option);
157 Returns true if the given \a option is enabled; otherwise, returns
160 \sa options, setOption()
162 bool QPrintDialog::testOption(PrintDialogOption option) const
164 Q_D(const QPrintDialog);
165 return (d->pd->options & option) != 0;
169 \property QPrintDialog::options
170 \brief the various options that affect the look and feel of the dialog
173 By default, all options are disabled.
175 Options should be set before showing the dialog. Setting them while the
176 dialog is visible is not guaranteed to have an immediate effect on the
177 dialog (depending on the option and on the platform).
179 \sa setOption(), testOption()
181 void QPrintDialog::setOptions(PrintDialogOptions options)
185 PrintDialogOptions changed = (options ^ d->pd->options);
189 d->pd->options = options;
192 QPrintDialog::PrintDialogOptions QPrintDialog::options() const
194 Q_D(const QPrintDialog);
195 return d->pd->options;
201 Use QPrintDialog::setOptions() instead.
203 void QAbstractPrintDialog::setEnabledOptions(PrintDialogOptions options)
205 Q_D(QAbstractPrintDialog);
206 d->pd->options = options;
212 Use QPrintDialog::setOption(\a option, true) instead.
214 void QAbstractPrintDialog::addEnabledOption(PrintDialogOption option)
216 Q_D(QAbstractPrintDialog);
217 d->pd->options |= option;
223 Use QPrintDialog::options() instead.
225 QAbstractPrintDialog::PrintDialogOptions QAbstractPrintDialog::enabledOptions() const
227 Q_D(const QAbstractPrintDialog);
228 return d->pd->options;
234 Use QPrintDialog::testOption(\a option) instead.
236 bool QAbstractPrintDialog::isOptionEnabled(PrintDialogOption option) const
238 Q_D(const QAbstractPrintDialog);
239 return d->pd->options & option;
243 Sets the print range option in to be \a range.
245 void QAbstractPrintDialog::setPrintRange(PrintRange range)
247 Q_D(QAbstractPrintDialog);
248 d->pd->printRange = range;
252 Returns the print range.
254 QAbstractPrintDialog::PrintRange QAbstractPrintDialog::printRange() const
256 Q_D(const QAbstractPrintDialog);
257 return d->pd->printRange;
261 Sets the page range in this dialog to be from \a min to \a max. This also
262 enables the PrintPageRange option.
264 void QAbstractPrintDialog::setMinMax(int min, int max)
266 Q_D(QAbstractPrintDialog);
267 Q_ASSERT_X(min <= max, "QAbstractPrintDialog::setMinMax",
268 "'min' must be less than or equal to 'max'");
269 d->pd->minPage = min;
270 d->pd->maxPage = max;
271 d->pd->options |= PrintPageRange;
275 Returns the minimum page in the page range.
276 By default, this value is set to 1.
278 int QAbstractPrintDialog::minPage() const
280 Q_D(const QAbstractPrintDialog);
281 return d->pd->minPage;
285 Returns the maximum page in the page range. As of Qt 4.4, this
286 function returns INT_MAX by default. Previous versions returned 1
289 int QAbstractPrintDialog::maxPage() const
291 Q_D(const QAbstractPrintDialog);
292 return d->pd->maxPage;
296 Sets the range in the print dialog to be from \a from to \a to.
298 void QAbstractPrintDialog::setFromTo(int from, int to)
300 Q_D(QAbstractPrintDialog);
301 Q_ASSERT_X(from <= to, "QAbstractPrintDialog::setFromTo",
302 "'from' must be less than or equal to 'to'");
303 d->pd->fromPage = from;
306 if (d->pd->minPage == 0 && d->pd->maxPage == 0)
311 Returns the first page to be printed
312 By default, this value is set to 0.
314 int QAbstractPrintDialog::fromPage() const
316 Q_D(const QAbstractPrintDialog);
317 return d->pd->fromPage;
321 Returns the last page to be printed.
322 By default, this value is set to 0.
324 int QAbstractPrintDialog::toPage() const
326 Q_D(const QAbstractPrintDialog);
327 return d->pd->toPage;
332 Returns the printer that this printer dialog operates
335 QPrinter *QAbstractPrintDialog::printer() const
337 Q_D(const QAbstractPrintDialog);
341 void QAbstractPrintDialogPrivate::setPrinter(QPrinter *newPrinter)
344 printer = newPrinter;
347 printer = new QPrinter;
350 pd = printer->d_func();
354 \fn int QAbstractPrintDialog::exec()
356 This virtual function is called to pop up the dialog. It must be
357 reimplemented in subclasses.
363 \brief The QPrintDialog class provides a dialog for specifying
364 the printer's configuration.
366 \ingroup standard-dialogs
369 The dialog allows users to change document-related settings, such
370 as the paper size and orientation, type of print (color or
371 grayscale), range of pages, and number of copies to print.
373 Controls are also provided to enable users to choose from the
374 printers available, including any configured network printers.
376 Typically, QPrintDialog objects are constructed with a QPrinter
377 object, and executed using the exec() function.
379 \snippet doc/src/snippets/code/src_gui_dialogs_qabstractprintdialog.cpp 0
381 If the dialog is accepted by the user, the QPrinter object is
382 correctly configured for printing.
386 \o \inlineimage plastique-printdialog.png
387 \o \inlineimage plastique-printdialog-properties.png
390 The printer dialog (shown above in Plastique style) enables access to common
391 printing properties. On X11 platforms that use the CUPS printing system, the
392 settings for each available printer can be modified via the dialog's
393 \gui{Properties} push button.
395 On Windows and Mac OS X, the native print dialog is used, which means that
396 some QWidget and QDialog properties set on the dialog won't be respected.
397 The native print dialog on Mac OS X does not support setting printer options,
398 i.e. setOptions() and setOption() have no effect.
400 In Qt 4.4, it was possible to use the static functions to show a sheet on
401 Mac OS X. This is no longer supported in Qt 4.5. If you want this
402 functionality, use QPrintDialog::open().
404 \sa QPageSetupDialog, QPrinter, {Pixelator Example}, {Order Form Example},
405 {Image Viewer Example}, {Scribble Example}
409 \fn QPrintDialog::QPrintDialog(QPrinter *printer, QWidget *parent)
411 Constructs a new modal printer dialog for the given \a printer
412 with the given \a parent.
416 \fn QPrintDialog::~QPrintDialog()
418 Destroys the print dialog.
422 \fn int QPrintDialog::exec()
429 Set a list of widgets as \a tabs to be shown on the print dialog, if supported.
431 Currently this option is only supported on X11.
433 Setting the option tabs will transfer their ownership to the print dialog.
435 void QAbstractPrintDialog::setOptionTabs(const QList<QWidget*> &tabs)
437 Q_D(QAbstractPrintDialog);
443 \fn void QPrintDialog::accepted(QPrinter *printer)
445 This signal is emitted when the user accepts the values set in the print dialog.
446 The \a printer parameter includes the printer that the settings were applied to.
450 \fn QPrinter *QPrintDialog::printer()
452 Returns the printer that this printer dialog operates
453 on. This can be useful when using the QPrintDialog::open() method.
457 Closes the dialog and sets its result code to \a result. If this dialog
458 is shown with exec(), done() causes the local event loop to finish,
459 and exec() to return \a result.
461 \note This function does not apply to the Native Print Dialog on the Mac
462 OS X and Windows platforms, because the dialog is required to be modal
463 and only the user can close it.
467 void QPrintDialog::done(int result)
470 QDialog::done(result);
471 if (result == Accepted)
472 emit accepted(printer());
473 if (d->receiverToDisconnectOnClose) {
474 disconnect(this, SIGNAL(accepted(QPrinter*)),
475 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
476 d->receiverToDisconnectOnClose = 0;
478 d->memberToDisconnectOnClose.clear();
485 Opens the dialog and connects its accepted() signal to the slot specified
486 by \a receiver and \a member.
488 The signal will be disconnected from the slot when the dialog is closed.
490 void QPrintDialog::open(QObject *receiver, const char *member)
493 connect(this, SIGNAL(accepted(QPrinter*)), receiver, member);
494 d->receiverToDisconnectOnClose = receiver;
495 d->memberToDisconnectOnClose = member;
501 #endif // QT_NO_PRINTDIALOG