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