/src/gui/dialogs/qabstractprintdialog.cpp

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