PageRenderTime 101ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/printsupport/dialogs/qabstractprintdialog.cpp

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