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