src/gui/painting/qprinter.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 "qprinter_p.h"
       
    43 #include "qprinter.h"
       
    44 #include "qprintengine.h"
       
    45 #include "qprinterinfo.h"
       
    46 #include "qlist.h"
       
    47 #include <qpagesetupdialog.h>
       
    48 #include <qapplication.h>
       
    49 #include <qfileinfo.h>
       
    50 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
       
    51 #include "private/qcups_p.h"
       
    52 #endif
       
    53 
       
    54 #ifndef QT_NO_PRINTER
       
    55 
       
    56 #if defined (Q_WS_WIN)
       
    57 #include <private/qprintengine_win_p.h>
       
    58 #elif defined (Q_WS_MAC)
       
    59 #include <private/qprintengine_mac_p.h>
       
    60 #elif defined (QTOPIA_PRINTENGINE)
       
    61 #include <private/qprintengine_qws_p.h>
       
    62 #endif
       
    63 #include <private/qprintengine_ps_p.h>
       
    64 
       
    65 #if defined(Q_WS_X11)
       
    66 #include <private/qt_x11_p.h>
       
    67 #endif
       
    68 
       
    69 #ifndef QT_NO_PDF
       
    70 #include "qprintengine_pdf_p.h"
       
    71 #endif
       
    72 
       
    73 #include <qpicture.h>
       
    74 #include <private/qpaintengine_preview_p.h>
       
    75 
       
    76 #if defined(QT3_SUPPORT)
       
    77 #  include "qprintdialog.h"
       
    78 #endif // QT3_SUPPORT
       
    79 
       
    80 QT_BEGIN_NAMESPACE
       
    81 
       
    82 #define ABORT_IF_ACTIVE(location) \
       
    83     if (d->printEngine->printerState() == QPrinter::Active) { \
       
    84         qWarning("%s: Cannot be changed while printer is active", location); \
       
    85         return; \
       
    86     }
       
    87 
       
    88 // NB! This table needs to be in sync with QPrinter::PaperSize
       
    89 static const float qt_paperSizes[][2] = {
       
    90     {210, 297}, // A4
       
    91     {176, 250}, // B5
       
    92     {215.9f, 279.4f}, // Letter
       
    93     {215.9f, 355.6f}, // Legal
       
    94     {190.5f, 254}, // Executive
       
    95     {841, 1189}, // A0
       
    96     {594, 841}, // A1
       
    97     {420, 594}, // A2
       
    98     {297, 420}, // A3
       
    99     {148, 210}, // A5
       
   100     {105, 148}, // A6
       
   101     {74, 105}, // A7
       
   102     {52, 74}, // A8
       
   103     {37, 52}, // A8
       
   104     {1000, 1414}, // B0
       
   105     {707, 1000}, // B1
       
   106     {31, 44}, // B10
       
   107     {500, 707}, // B2
       
   108     {353, 500}, // B3
       
   109     {250, 353}, // B4
       
   110     {125, 176}, // B6
       
   111     {88, 125}, // B7
       
   112     {62, 88}, // B8
       
   113     {33, 62}, // B9
       
   114     {163, 229}, // C5E
       
   115     {105, 241}, // US Common
       
   116     {110, 220}, // DLE
       
   117     {210, 330}, // Folio
       
   118     {431.8f, 279.4f}, // Ledger
       
   119     {279.4f, 431.8f} // Tabloid
       
   120 };
       
   121 
       
   122 /// return the multiplier of converting from the unit value to postscript-points.
       
   123 double qt_multiplierForUnit(QPrinter::Unit unit, int resolution)
       
   124 {
       
   125     switch(unit) {
       
   126     case QPrinter::Millimeter:
       
   127         return 2.83464566929;
       
   128     case QPrinter::Point:
       
   129         return 1.0;
       
   130     case QPrinter::Inch:
       
   131         return 72.0;
       
   132     case QPrinter::Pica:
       
   133         return 12;
       
   134     case QPrinter::Didot:
       
   135         return 1.065826771;
       
   136     case QPrinter::Cicero:
       
   137         return 12.789921252;
       
   138     case QPrinter::DevicePixel:
       
   139         return 72.0/resolution;
       
   140     }
       
   141     return 1.0;
       
   142 }
       
   143 
       
   144 // not static: it's needed in qpagesetupdialog_unix.cpp
       
   145 QSizeF qt_printerPaperSize(QPrinter::Orientation orientation,
       
   146                            QPrinter::PaperSize paperSize,
       
   147                            QPrinter::Unit unit,
       
   148                            int resolution)
       
   149 {
       
   150     int width_index = 0;
       
   151     int height_index = 1;
       
   152     if (orientation == QPrinter::Landscape) {
       
   153         width_index = 1;
       
   154         height_index = 0;
       
   155     }
       
   156     const qreal multiplier = qt_multiplierForUnit(unit, resolution);
       
   157     return QSizeF((qt_paperSizes[paperSize][width_index] * 72 / 25.4) / multiplier,
       
   158                   (qt_paperSizes[paperSize][height_index] * 72 / 25.4) / multiplier);
       
   159 }
       
   160 
       
   161 
       
   162 // returns the actual num copies set on a printer, not
       
   163 // the number that is documented in QPrinter::numCopies()
       
   164 int qt_printerRealNumCopies(QPaintEngine *engine)
       
   165 {
       
   166     int numCopies = 1;
       
   167     if (engine->type() == QPaintEngine::PostScript
       
   168         || engine->type() == QPaintEngine::Pdf)
       
   169     {
       
   170         QPdfBaseEngine *base = static_cast<QPdfBaseEngine *>(engine);
       
   171         numCopies = base->d_func()->copies;
       
   172     }
       
   173 #ifdef Q_WS_WIN
       
   174     else if (engine->type() == QPaintEngine::Windows) {
       
   175         QWin32PrintEngine *base = static_cast<QWin32PrintEngine *>(engine);
       
   176         numCopies = base->d_func()->num_copies;
       
   177     }
       
   178 #endif
       
   179     return numCopies;
       
   180 }
       
   181 
       
   182 void QPrinterPrivate::createDefaultEngines()
       
   183 {
       
   184     QPrinter::OutputFormat realOutputFormat = outputFormat;
       
   185 #if !defined (QTOPIA_PRINTENGINE)
       
   186 #if defined (Q_OS_UNIX) && ! defined (Q_WS_MAC)
       
   187     if(outputFormat == QPrinter::NativeFormat) {
       
   188         realOutputFormat = QPrinter::PostScriptFormat;
       
   189     }
       
   190 #endif
       
   191 #endif
       
   192 
       
   193     switch (realOutputFormat) {
       
   194     case QPrinter::NativeFormat: {
       
   195 #if defined (Q_WS_WIN)
       
   196         QWin32PrintEngine *winEngine = new QWin32PrintEngine(printerMode);
       
   197         paintEngine = winEngine;
       
   198         printEngine = winEngine;
       
   199 #elif defined (Q_WS_MAC)
       
   200         QMacPrintEngine *macEngine = new QMacPrintEngine(printerMode);
       
   201         paintEngine = macEngine;
       
   202         printEngine = macEngine;
       
   203 #elif defined (QTOPIA_PRINTENGINE)
       
   204         QtopiaPrintEngine *qwsEngine = new QtopiaPrintEngine(printerMode);
       
   205         paintEngine = qwsEngine;
       
   206         printEngine = qwsEngine;
       
   207 #elif defined (Q_OS_UNIX)
       
   208         Q_ASSERT(false);
       
   209 #endif
       
   210         }
       
   211         break;
       
   212     case QPrinter::PdfFormat: {
       
   213         QPdfEngine *pdfEngine = new QPdfEngine(printerMode);
       
   214         paintEngine = pdfEngine;
       
   215         printEngine = pdfEngine;
       
   216     }
       
   217         break;
       
   218     case QPrinter::PostScriptFormat: {
       
   219         QPSPrintEngine *psEngine = new QPSPrintEngine(printerMode);
       
   220         paintEngine = psEngine;
       
   221         printEngine = psEngine;
       
   222     }
       
   223         break;
       
   224     }
       
   225     use_default_engine = true;
       
   226     had_default_engines = true;
       
   227 }
       
   228 
       
   229 #ifndef QT_NO_PRINTPREVIEWWIDGET
       
   230 QList<const QPicture *> QPrinterPrivate::previewPages() const
       
   231 {
       
   232     if (previewEngine)
       
   233         return previewEngine->pages();
       
   234     return QList<const QPicture *>();
       
   235 }
       
   236 
       
   237 void QPrinterPrivate::setPreviewMode(bool enable)
       
   238 {
       
   239     Q_Q(QPrinter);
       
   240     if (enable) {
       
   241         if (!previewEngine)
       
   242             previewEngine = new QPreviewPaintEngine;
       
   243         had_default_engines = use_default_engine;
       
   244         use_default_engine = false;
       
   245         realPrintEngine = printEngine;
       
   246         realPaintEngine = paintEngine;
       
   247         q->setEngines(previewEngine, previewEngine);
       
   248         previewEngine->setProxyEngines(realPrintEngine, realPaintEngine);
       
   249     } else {
       
   250         q->setEngines(realPrintEngine, realPaintEngine);
       
   251         use_default_engine = had_default_engines;
       
   252     }
       
   253 }
       
   254 #endif // QT_NO_PRINTPREVIEWWIDGET
       
   255 
       
   256 void QPrinterPrivate::addToManualSetList(QPrintEngine::PrintEnginePropertyKey key)
       
   257 {
       
   258     for (int c = 0; c < manualSetList.size(); ++c) {
       
   259         if (manualSetList[c] == key) return;
       
   260     }
       
   261     manualSetList.append(key);
       
   262 }
       
   263 
       
   264 
       
   265 /*!
       
   266   \class QPrinter
       
   267   \reentrant
       
   268 
       
   269   \brief The QPrinter class is a paint device that paints on a printer.
       
   270 
       
   271   \ingroup printing
       
   272 
       
   273 
       
   274   This device represents a series of pages of printed output, and is
       
   275   used in almost exactly the same way as other paint devices such as
       
   276   QWidget and QPixmap.
       
   277   A set of additional functions are provided to manage device-specific
       
   278   features, such as orientation and resolution, and to step through
       
   279   the pages in a document as it is generated.
       
   280 
       
   281   When printing directly to a printer on Windows or Mac OS X, QPrinter uses
       
   282   the built-in printer drivers. On X11, QPrinter uses the
       
   283   \l{Common Unix Printing System (CUPS)} or the standard Unix \l lpr utility
       
   284   to send PostScript or PDF output to the printer. As an alternative,
       
   285   the printProgram() function can be used to specify the command or utility
       
   286   to use instead of the system default.
       
   287 
       
   288   Note that setting parameters like paper size and resolution on an
       
   289   invalid printer is undefined. You can use QPrinter::isValid() to
       
   290   verify this before changing any parameters.
       
   291 
       
   292   QPrinter supports a number of parameters, most of which can be
       
   293   changed by the end user through a \l{QPrintDialog}{print dialog}. In
       
   294   general, QPrinter passes these functions onto the underlying QPrintEngine.
       
   295 
       
   296   The most important parameters are:
       
   297   \list
       
   298   \i setOrientation() tells QPrinter which page orientation to use.
       
   299   \i setPaperSize() tells QPrinter what paper size to expect from the
       
   300   printer.
       
   301   \i setResolution() tells QPrinter what resolution you wish the
       
   302   printer to provide, in dots per inch (DPI).
       
   303   \i setFullPage() tells QPrinter whether you want to deal with the
       
   304   full page or just with the part the printer can draw on.
       
   305   \i setNumCopies() tells QPrinter how many copies of the document
       
   306   it should print.
       
   307   \endlist
       
   308 
       
   309   Many of these functions can only be called before the actual printing
       
   310   begins (i.e., before QPainter::begin() is called). This usually makes
       
   311   sense because, for example, it's not possible to change the number of
       
   312   copies when you are halfway through printing. There are also some
       
   313   settings that the user sets (through the printer dialog) and that
       
   314   applications are expected to obey. See QAbstractPrintDialog's
       
   315   documentation for more details.
       
   316 
       
   317   When QPainter::begin() is called, the QPrinter it operates on is prepared for
       
   318   a new page, enabling the QPainter to be used immediately to paint the first
       
   319   page in a document. Once the first page has been painted, newPage() can be
       
   320   called to request a new blank page to paint on, or QPainter::end() can be
       
   321   called to finish printing. The second page and all following pages are
       
   322   prepared using a call to newPage() before they are painted.
       
   323 
       
   324   The first page in a document does not need to be preceded by a call to
       
   325   newPage(). You only need to calling newPage() after QPainter::begin() if you
       
   326   need to insert a blank page at the beginning of a printed document.
       
   327   Similarly, calling newPage() after the last page in a document is painted will
       
   328   result in a trailing blank page appended to the end of the printed document.
       
   329 
       
   330   If you want to abort the print job, abort() will try its best to
       
   331   stop printing. It may cancel the entire job or just part of it.
       
   332 
       
   333   Since QPrinter can print to any QPrintEngine subclass, it is possible to
       
   334   extend printing support to cover new types of printing subsystem by
       
   335   subclassing QPrintEngine and reimplementing its interface.
       
   336 
       
   337   \sa QPrintDialog, {Printing with Qt}
       
   338 */
       
   339 
       
   340 /*!
       
   341     \enum QPrinter::PrinterState
       
   342 
       
   343     \value Idle
       
   344     \value Active
       
   345     \value Aborted
       
   346     \value Error
       
   347 */
       
   348 
       
   349 /*!
       
   350     \enum QPrinter::PrinterMode
       
   351 
       
   352     This enum describes the mode the printer should work in. It
       
   353     basically presets a certain resolution and working mode.
       
   354 
       
   355     \value ScreenResolution Sets the resolution of the print device to
       
   356     the screen resolution. This has the big advantage that the results
       
   357     obtained when painting on the printer will match more or less
       
   358     exactly the visible output on the screen. It is the easiest to
       
   359     use, as font metrics on the screen and on the printer are the
       
   360     same. This is the default value. ScreenResolution will produce a
       
   361     lower quality output than HighResolution and should only be used
       
   362     for drafts.
       
   363 
       
   364     \value PrinterResolution This value is deprecated. Is is
       
   365     equivalent to ScreenResolution on Unix and HighResolution on
       
   366     Windows and Mac. Due do the difference between ScreenResolution
       
   367     and HighResolution, use of this value may lead to non-portable
       
   368     printer code.
       
   369 
       
   370     \value HighResolution On Windows, sets the printer resolution to that
       
   371     defined for the printer in use. For PostScript printing, sets the
       
   372     resolution of the PostScript driver to 1200 dpi.
       
   373 
       
   374     \note When rendering text on a QPrinter device, it is important
       
   375     to realize that the size of text, when specified in points, is
       
   376     independent of the resolution specified for the device itself.
       
   377     Therefore, it may be useful to specify the font size in pixels
       
   378     when combining text with graphics to ensure that their relative
       
   379     sizes are what you expect.
       
   380 */
       
   381 
       
   382 /*!
       
   383   \enum QPrinter::Orientation
       
   384 
       
   385   This enum type (not to be confused with \c Orientation) is used
       
   386   to specify each page's orientation.
       
   387 
       
   388   \value Portrait the page's height is greater than its width.
       
   389 
       
   390   \value Landscape the page's width is greater than its height.
       
   391 
       
   392   This type interacts with \l QPrinter::PaperSize and
       
   393   QPrinter::setFullPage() to determine the final size of the page
       
   394   available to the application.
       
   395 */
       
   396 
       
   397 
       
   398 /*!
       
   399     \enum QPrinter::PrintRange
       
   400 
       
   401     Used to specify the print range selection option.
       
   402 
       
   403     \value AllPages All pages should be printed.
       
   404     \value Selection Only the selection should be printed.
       
   405     \value PageRange The specified page range should be printed.
       
   406 
       
   407     \sa QAbstractPrintDialog::PrintRange
       
   408 */
       
   409 
       
   410 /*!
       
   411     \enum QPrinter::PrinterOption
       
   412     \compat
       
   413 
       
   414     Use QAbstractPrintDialog::PrintDialogOption instead.
       
   415 
       
   416     \value PrintToFile
       
   417     \value PrintSelection
       
   418     \value PrintPageRange
       
   419 */
       
   420 
       
   421 /*!
       
   422   \enum QPrinter::PageSize
       
   423 
       
   424   \obsolete
       
   425   Use QPrinter::PaperSize instead.
       
   426 
       
   427   \value A0 841 x 1189 mm
       
   428   \value A1 594 x 841 mm
       
   429   \value A2 420 x 594 mm
       
   430   \value A3 297 x 420 mm
       
   431   \value A4 210 x 297 mm, 8.26 x 11.69 inches
       
   432   \value A5 148 x 210 mm
       
   433   \value A6 105 x 148 mm
       
   434   \value A7 74 x 105 mm
       
   435   \value A8 52 x 74 mm
       
   436   \value A9 37 x 52 mm
       
   437   \value B0 1030 x 1456 mm
       
   438   \value B1 728 x 1030 mm
       
   439   \value B10 32 x 45 mm
       
   440   \value B2 515 x 728 mm
       
   441   \value B3 364 x 515 mm
       
   442   \value B4 257 x 364 mm
       
   443   \value B5 182 x 257 mm, 7.17 x 10.13 inches
       
   444   \value B6 128 x 182 mm
       
   445   \value B7 91 x 128 mm
       
   446   \value B8 64 x 91 mm
       
   447   \value B9 45 x 64 mm
       
   448   \value C5E 163 x 229 mm
       
   449   \value Comm10E 105 x 241 mm, U.S. Common 10 Envelope
       
   450   \value DLE 110 x 220 mm
       
   451   \value Executive 7.5 x 10 inches, 191 x 254 mm
       
   452   \value Folio 210 x 330 mm
       
   453   \value Ledger 432 x 279 mm
       
   454   \value Legal 8.5 x 14 inches, 216 x 356 mm
       
   455   \value Letter 8.5 x 11 inches, 216 x 279 mm
       
   456   \value Tabloid 279 x 432 mm
       
   457   \value Custom Unknown, or a user defined size.
       
   458 
       
   459   \omitvalue NPageSize
       
   460   */
       
   461 
       
   462 /*!
       
   463   \enum QPrinter::PaperSize
       
   464   \since 4.4
       
   465 
       
   466   This enum type specifies what paper size QPrinter should use.
       
   467   QPrinter does not check that the paper size is available; it just
       
   468   uses this information, together with QPrinter::Orientation and
       
   469   QPrinter::setFullPage(), to determine the printable area.
       
   470 
       
   471   The defined sizes (with setFullPage(true)) are:
       
   472 
       
   473   \value A0 841 x 1189 mm
       
   474   \value A1 594 x 841 mm
       
   475   \value A2 420 x 594 mm
       
   476   \value A3 297 x 420 mm
       
   477   \value A4 210 x 297 mm, 8.26 x 11.69 inches
       
   478   \value A5 148 x 210 mm
       
   479   \value A6 105 x 148 mm
       
   480   \value A7 74 x 105 mm
       
   481   \value A8 52 x 74 mm
       
   482   \value A9 37 x 52 mm
       
   483   \value B0 1000 x 1414 mm
       
   484   \value B1 707 x 1000 mm
       
   485   \value B2 500 x 707 mm
       
   486   \value B3 353 x 500 mm
       
   487   \value B4 250 x 353 mm
       
   488   \value B5 176 x 250 mm, 6.93 x 9.84 inches
       
   489   \value B6 125 x 176 mm
       
   490   \value B7 88 x 125 mm
       
   491   \value B8 62 x 88 mm
       
   492   \value B9 33 x 62 mm
       
   493   \value B10 31 x 44 mm
       
   494   \value C5E 163 x 229 mm
       
   495   \value Comm10E 105 x 241 mm, U.S. Common 10 Envelope
       
   496   \value DLE 110 x 220 mm
       
   497   \value Executive 7.5 x 10 inches, 190.5 x 254 mm
       
   498   \value Folio 210 x 330 mm
       
   499   \value Ledger 431.8 x 279.4 mm
       
   500   \value Legal 8.5 x 14 inches, 215.9 x 355.6 mm
       
   501   \value Letter 8.5 x 11 inches, 215.9 x 279.4 mm
       
   502   \value Tabloid 279.4 x 431.8 mm
       
   503   \value Custom Unknown, or a user defined size.
       
   504 
       
   505   With setFullPage(false) (the default), the metrics will be a bit
       
   506   smaller; how much depends on the printer in use.
       
   507 
       
   508   \omitvalue NPageSize
       
   509   \omitvalue NPaperSize
       
   510 */
       
   511 
       
   512 
       
   513 /*!
       
   514   \enum QPrinter::PageOrder
       
   515 
       
   516   This enum type is used by QPrinter to tell the application program
       
   517   how to print.
       
   518 
       
   519   \value FirstPageFirst  the lowest-numbered page should be printed
       
   520   first.
       
   521 
       
   522   \value LastPageFirst  the highest-numbered page should be printed
       
   523   first.
       
   524 */
       
   525 
       
   526 /*!
       
   527   \enum QPrinter::ColorMode
       
   528 
       
   529   This enum type is used to indicate whether QPrinter should print
       
   530   in color or not.
       
   531 
       
   532   \value Color  print in color if available, otherwise in grayscale.
       
   533 
       
   534   \value GrayScale  print in grayscale, even on color printers.
       
   535 */
       
   536 
       
   537 /*!
       
   538   \enum QPrinter::PaperSource
       
   539 
       
   540   This enum type specifies what paper source QPrinter is to use.
       
   541   QPrinter does not check that the paper source is available; it
       
   542   just uses this information to try and set the paper source.
       
   543   Whether it will set the paper source depends on whether the
       
   544   printer has that particular source.
       
   545 
       
   546   \warning This is currently only implemented for Windows.
       
   547 
       
   548   \value Auto
       
   549   \value Cassette
       
   550   \value Envelope
       
   551   \value EnvelopeManual
       
   552   \value FormSource
       
   553   \value LargeCapacity
       
   554   \value LargeFormat
       
   555   \value Lower
       
   556   \value MaxPageSource
       
   557   \value Middle
       
   558   \value Manual
       
   559   \value OnlyOne
       
   560   \value Tractor
       
   561   \value SmallFormat
       
   562 */
       
   563 
       
   564 /*!
       
   565   \enum QPrinter::Unit
       
   566   \since 4.4
       
   567 
       
   568   This enum type is used to specify the measurement unit for page and
       
   569   paper sizes.
       
   570 
       
   571   \value Millimeter
       
   572   \value Point
       
   573   \value Inch
       
   574   \value Pica
       
   575   \value Didot
       
   576   \value Cicero
       
   577   \value DevicePixel
       
   578 
       
   579   Note the difference between Point and DevicePixel. The Point unit is
       
   580   defined to be 1/72th of an inch, while the DevicePixel unit is
       
   581   resolution dependant and is based on the actual pixels, or dots, on
       
   582   the printer.
       
   583 */
       
   584 
       
   585 
       
   586 /*
       
   587   \enum QPrinter::PrintRange
       
   588 
       
   589   This enum is used to specify which print range the application
       
   590   should use to print.
       
   591 
       
   592   \value AllPages All the pages should be printed.
       
   593   \value Selection Only the selection should be printed.
       
   594   \value PageRange Print according to the from page and to page options.
       
   595 
       
   596   \sa setPrintRange(), printRange()
       
   597 */
       
   598 
       
   599 /*
       
   600   \enum QPrinter::PrinterOption
       
   601 
       
   602   This enum describes various printer options that appear in the
       
   603   printer setup dialog. It is used to enable and disable these
       
   604   options in the setup dialog.
       
   605 
       
   606   \value PrintToFile Describes if print to file should be enabled.
       
   607   \value PrintSelection Describes if printing selections should be enabled.
       
   608   \value PrintPageRange Describes if printing page ranges (from, to) should
       
   609   be enabled
       
   610 
       
   611   \sa setOptionEnabled(), isOptionEnabled()
       
   612 */
       
   613 
       
   614 /*!
       
   615     Creates a new printer object with the given \a mode.
       
   616 */
       
   617 QPrinter::QPrinter(PrinterMode mode)
       
   618     : QPaintDevice(),
       
   619       d_ptr(new QPrinterPrivate(this))
       
   620 {
       
   621     init(mode);
       
   622     QPrinterInfo defPrn(QPrinterInfo::defaultPrinter());
       
   623     if (!defPrn.isNull()) {
       
   624         setPrinterName(defPrn.printerName());
       
   625     } else if (QPrinterInfo::availablePrinters().isEmpty()
       
   626                && d_ptr->paintEngine->type() != QPaintEngine::Windows
       
   627                && d_ptr->paintEngine->type() != QPaintEngine::MacPrinter) {
       
   628         setOutputFormat(QPrinter::PdfFormat);
       
   629     }
       
   630 }
       
   631 
       
   632 /*!
       
   633     \since 4.4
       
   634 
       
   635     Creates a new printer object with the given \a printer and \a mode.
       
   636 */
       
   637 QPrinter::QPrinter(const QPrinterInfo& printer, PrinterMode mode)
       
   638     : QPaintDevice(),
       
   639       d_ptr(new QPrinterPrivate(this))
       
   640 {
       
   641     init(mode);
       
   642     setPrinterName(printer.printerName());
       
   643 }
       
   644 
       
   645 void QPrinter::init(PrinterMode mode)
       
   646 {
       
   647 #if !defined(Q_WS_X11)
       
   648     if (!qApp) {
       
   649 #else
       
   650     if (!qApp || !X11) {
       
   651 #endif
       
   652         qFatal("QPrinter: Must construct a QApplication before a QPaintDevice");
       
   653         return;
       
   654     }
       
   655     Q_D(QPrinter);
       
   656 
       
   657     d->printerMode = mode;
       
   658     d->outputFormat = QPrinter::NativeFormat;
       
   659     d->createDefaultEngines();
       
   660 
       
   661 #ifndef QT_NO_PRINTPREVIEWWIDGET
       
   662     d->previewEngine = 0;
       
   663 #endif
       
   664     d->realPrintEngine = 0;
       
   665     d->realPaintEngine = 0;
       
   666 
       
   667 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
       
   668     if (QCUPSSupport::cupsVersion() >= 10200 && QCUPSSupport().currentPPD()) {
       
   669         setOutputFormat(QPrinter::PdfFormat);
       
   670         d->outputFormat = QPrinter::NativeFormat;
       
   671     }
       
   672 #endif
       
   673 }
       
   674 
       
   675 /*!
       
   676     This function is used by subclasses of QPrinter to specify custom
       
   677     print and paint engines (\a printEngine and \a paintEngine,
       
   678     respectively).
       
   679 
       
   680     QPrinter does not take ownership of the engines, so you need to
       
   681     manage these engine instances yourself.
       
   682 
       
   683     Note that changing the engines will reset the printer state and
       
   684     all its properties.
       
   685 
       
   686     \sa printEngine() paintEngine() setOutputFormat()
       
   687 
       
   688     \since 4.1
       
   689 */
       
   690 void QPrinter::setEngines(QPrintEngine *printEngine, QPaintEngine *paintEngine)
       
   691 {
       
   692     Q_D(QPrinter);
       
   693 
       
   694     if (d->use_default_engine)
       
   695         delete d->printEngine;
       
   696 
       
   697     d->printEngine = printEngine;
       
   698     d->paintEngine = paintEngine;
       
   699     d->use_default_engine = false;
       
   700 }
       
   701 
       
   702 /*!
       
   703     Destroys the printer object and frees any allocated resources. If
       
   704     the printer is destroyed while a print job is in progress this may
       
   705     or may not affect the print job.
       
   706 */
       
   707 QPrinter::~QPrinter()
       
   708 {
       
   709     Q_D(QPrinter);
       
   710     if (d->use_default_engine)
       
   711         delete d->printEngine;
       
   712 #ifndef QT_NO_PRINTPREVIEWWIDGET
       
   713     delete d->previewEngine;
       
   714 #endif
       
   715 }
       
   716 
       
   717 /*!
       
   718     \enum QPrinter::OutputFormat
       
   719 
       
   720     The OutputFormat enum is used to describe the format QPrinter should
       
   721     use for printing.
       
   722 
       
   723     \value NativeFormat QPrinter will print output using a method defined
       
   724     by the platform it is running on. This mode is the default when printing
       
   725     directly to a printer.
       
   726 
       
   727     \value PdfFormat QPrinter will generate its output as a searchable PDF file.
       
   728     This mode is the default when printing to a file.
       
   729 
       
   730     \value PostScriptFormat QPrinter will generate its output as in the PostScript format.
       
   731     (This feature was introduced in Qt 4.2.)
       
   732 
       
   733     \sa outputFormat(), setOutputFormat(), setOutputFileName()
       
   734 */
       
   735 
       
   736 /*!
       
   737     \since 4.1
       
   738 
       
   739     Sets the output format for this printer to \a format.
       
   740 */
       
   741 void QPrinter::setOutputFormat(OutputFormat format)
       
   742 {
       
   743 
       
   744 #ifndef QT_NO_PDF
       
   745     Q_D(QPrinter);
       
   746     if (d->validPrinter && d->outputFormat == format)
       
   747         return;
       
   748     d->outputFormat = format;
       
   749 
       
   750     QPrintEngine *oldPrintEngine = d->printEngine;
       
   751     QPaintEngine *oldPaintEngine = d->paintEngine; // same as the above - shouldn't be deleted
       
   752     const bool def_engine = d->use_default_engine;
       
   753     d->printEngine = 0;
       
   754 
       
   755     d->createDefaultEngines();
       
   756 
       
   757     if (oldPrintEngine) {
       
   758         for (int i = 0; i < d->manualSetList.size(); ++i) {
       
   759             QPrintEngine::PrintEnginePropertyKey key = d->manualSetList[i];
       
   760             QVariant prop;
       
   761             // PPK_NumberOfCopies need special treatmeant since it in most cases
       
   762             // will return 1, disregarding the actual value that was set
       
   763             if (key == QPrintEngine::PPK_NumberOfCopies)
       
   764                 prop = QVariant(qt_printerRealNumCopies(oldPaintEngine));
       
   765             else
       
   766                 prop = oldPrintEngine->property(key);
       
   767             if (prop.isValid())
       
   768                 d->printEngine->setProperty(key, prop);
       
   769         }
       
   770     }
       
   771 
       
   772     if (def_engine)
       
   773         delete oldPrintEngine;
       
   774 
       
   775     if (d->outputFormat == QPrinter::PdfFormat || d->outputFormat == QPrinter::PostScriptFormat)
       
   776         d->validPrinter = true;
       
   777 #else
       
   778     Q_UNUSED(format);
       
   779 #endif
       
   780 }
       
   781 
       
   782 /*!
       
   783     \since 4.1
       
   784 
       
   785     Returns the output format for this printer.
       
   786 */
       
   787 QPrinter::OutputFormat QPrinter::outputFormat() const
       
   788 {
       
   789     Q_D(const QPrinter);
       
   790     return d->outputFormat;
       
   791 }
       
   792 
       
   793 
       
   794 
       
   795 /*! \internal
       
   796 */
       
   797 int QPrinter::devType() const
       
   798 {
       
   799     return QInternal::Printer;
       
   800 }
       
   801 
       
   802 /*!
       
   803     Returns the printer name. This value is initially set to the name
       
   804     of the default printer.
       
   805 
       
   806     \sa setPrinterName()
       
   807 */
       
   808 QString QPrinter::printerName() const
       
   809 {
       
   810     Q_D(const QPrinter);
       
   811     return d->printEngine->property(QPrintEngine::PPK_PrinterName).toString();
       
   812 }
       
   813 
       
   814 /*!
       
   815     Sets the printer name to \a name.
       
   816 
       
   817     \sa printerName(), isValid()
       
   818 */
       
   819 void QPrinter::setPrinterName(const QString &name)
       
   820 {
       
   821     Q_D(QPrinter);
       
   822     ABORT_IF_ACTIVE("QPrinter::setPrinterName");
       
   823 
       
   824 #if defined(Q_OS_UNIX) && !defined(QT_NO_CUPS)
       
   825     if(d->use_default_engine
       
   826         && d->outputFormat == QPrinter::NativeFormat) {
       
   827         if (QCUPSSupport::cupsVersion() >= 10200
       
   828             && QCUPSSupport::printerHasPPD(name.toLocal8Bit().constData()))
       
   829             setOutputFormat(QPrinter::PdfFormat);
       
   830         else
       
   831             setOutputFormat(QPrinter::PostScriptFormat);
       
   832         d->outputFormat = QPrinter::NativeFormat;
       
   833     }
       
   834 #endif
       
   835 
       
   836     QList<QPrinterInfo> prnList = QPrinterInfo::availablePrinters();
       
   837     if (name.isEmpty()) {
       
   838         d->validPrinter = d->outputFormat == QPrinter::PdfFormat || d->outputFormat == QPrinter::PostScriptFormat;
       
   839     } else {
       
   840         d->validPrinter = false;
       
   841         for (int i = 0; i < prnList.size(); ++i) {
       
   842             if (prnList[i].printerName() == name) {
       
   843                 d->validPrinter = true;
       
   844                 break;
       
   845             }
       
   846         }
       
   847     }
       
   848 
       
   849     d->printEngine->setProperty(QPrintEngine::PPK_PrinterName, name);
       
   850     d->addToManualSetList(QPrintEngine::PPK_PrinterName);
       
   851 }
       
   852 
       
   853 
       
   854 /*!
       
   855   \since 4.4
       
   856 
       
   857   Returns true if the printer currently selected is a valid printer
       
   858   in the system, or a pure PDF/PostScript printer; otherwise returns false.
       
   859 
       
   860   To detect other failures check the output of QPainter::begin() or QPrinter::newPage().
       
   861 
       
   862   \snippet doc/src/snippets/printing-qprinter/errors.cpp 0
       
   863 
       
   864   \sa setPrinterName()
       
   865 */
       
   866 bool QPrinter::isValid() const
       
   867 {
       
   868     Q_D(const QPrinter);
       
   869 #if defined(Q_WS_X11)
       
   870     if (!qApp || !X11) {
       
   871         return false;
       
   872     }
       
   873 #endif
       
   874     return d->validPrinter;
       
   875 }
       
   876 
       
   877 
       
   878 /*!
       
   879   \fn bool QPrinter::outputToFile() const
       
   880 
       
   881   Returns true if the output should be written to a file, or false
       
   882   if the output should be sent directly to the printer. The default
       
   883   setting is false.
       
   884 
       
   885   \sa setOutputToFile(), setOutputFileName()
       
   886 */
       
   887 
       
   888 
       
   889 /*!
       
   890   \fn void QPrinter::setOutputToFile(bool enable)
       
   891 
       
   892   Specifies whether the output should be written to a file or sent
       
   893   directly to the printer.
       
   894 
       
   895   Will output to a file if \a enable is true, or will output
       
   896   directly to the printer if \a enable is false.
       
   897 
       
   898   \sa outputToFile(), setOutputFileName()
       
   899 */
       
   900 
       
   901 
       
   902 /*!
       
   903   \fn QString QPrinter::outputFileName() const
       
   904 
       
   905   Returns the name of the output file. By default, this is an empty string
       
   906   (indicating that the printer shouldn't print to file).
       
   907 
       
   908   \sa QPrintEngine::PrintEnginePropertyKey
       
   909 
       
   910 */
       
   911 
       
   912 QString QPrinter::outputFileName() const
       
   913 {
       
   914     Q_D(const QPrinter);
       
   915     return d->printEngine->property(QPrintEngine::PPK_OutputFileName).toString();
       
   916 }
       
   917 
       
   918 /*!
       
   919     Sets the name of the output file to \a fileName.
       
   920 
       
   921     Setting a null or empty name (0 or "") disables printing to a file.
       
   922     Setting a non-empty name enables printing to a file.
       
   923 
       
   924     This can change the value of outputFormat().  If the file name has the
       
   925     suffix ".ps" then PostScript is automatically selected as output format.
       
   926     If the file name has the ".pdf" suffix PDF is generated. If the file name
       
   927     has a suffix other than ".ps" and ".pdf", the output format used is the
       
   928     one set with setOutputFormat().
       
   929 
       
   930     QPrinter uses Qt's cross-platform PostScript or PDF print engines
       
   931     respectively. If you can produce this format natively, for example
       
   932     Mac OS X can generate PDF's from its print engine, set the output format
       
   933     back to NativeFormat.
       
   934 
       
   935     \sa outputFileName() setOutputToFile() setOutputFormat()
       
   936 */
       
   937 
       
   938 void QPrinter::setOutputFileName(const QString &fileName)
       
   939 {
       
   940     Q_D(QPrinter);
       
   941     ABORT_IF_ACTIVE("QPrinter::setOutputFileName");
       
   942 
       
   943     QFileInfo fi(fileName);
       
   944     if (!fi.suffix().compare(QLatin1String("ps"), Qt::CaseInsensitive))
       
   945         setOutputFormat(QPrinter::PostScriptFormat);
       
   946     else if (!fi.suffix().compare(QLatin1String("pdf"), Qt::CaseInsensitive))
       
   947         setOutputFormat(QPrinter::PdfFormat);
       
   948     else if (fileName.isEmpty())
       
   949         setOutputFormat(QPrinter::NativeFormat);
       
   950 
       
   951     d->printEngine->setProperty(QPrintEngine::PPK_OutputFileName, fileName);
       
   952     d->addToManualSetList(QPrintEngine::PPK_OutputFileName);
       
   953 }
       
   954 
       
   955 
       
   956 /*!
       
   957   Returns the name of the program that sends the print output to the
       
   958   printer.
       
   959 
       
   960   The default is to return an empty string; meaning that QPrinter will try to
       
   961   be smart in a system-dependent way. On X11 only, you can set it to something
       
   962   different to use a specific print program. On the other platforms, this
       
   963   returns an empty string.
       
   964 
       
   965   \sa setPrintProgram(), setPrinterSelectionOption()
       
   966 */
       
   967 QString QPrinter::printProgram() const
       
   968 {
       
   969     Q_D(const QPrinter);
       
   970     return d->printEngine->property(QPrintEngine::PPK_PrinterProgram).toString();
       
   971 }
       
   972 
       
   973 
       
   974 /*!
       
   975   Sets the name of the program that should do the print job to \a
       
   976   printProg.
       
   977 
       
   978   On X11, this function sets the program to call with the PostScript
       
   979   output. On other platforms, it has no effect.
       
   980 
       
   981   \sa printProgram()
       
   982 */
       
   983 void QPrinter::setPrintProgram(const QString &printProg)
       
   984 {
       
   985     Q_D(QPrinter);
       
   986     ABORT_IF_ACTIVE("QPrinter::setPrintProgram");
       
   987     d->printEngine->setProperty(QPrintEngine::PPK_PrinterProgram, printProg);
       
   988     d->addToManualSetList(QPrintEngine::PPK_PrinterProgram);
       
   989 }
       
   990 
       
   991 
       
   992 /*!
       
   993   Returns the document name.
       
   994 
       
   995   \sa setDocName(), QPrintEngine::PrintEnginePropertyKey
       
   996 */
       
   997 QString QPrinter::docName() const
       
   998 {
       
   999     Q_D(const QPrinter);
       
  1000     return d->printEngine->property(QPrintEngine::PPK_DocumentName).toString();
       
  1001 }
       
  1002 
       
  1003 
       
  1004 /*!
       
  1005   Sets the document name to \a name.
       
  1006 
       
  1007   On X11, the document name is for example used as the default
       
  1008   output filename in QPrintDialog. Note that the document name does
       
  1009   not affect the file name if the printer is printing to a file.
       
  1010   Use the setOutputFile() function for this.
       
  1011 
       
  1012   \sa docName(), QPrintEngine::PrintEnginePropertyKey
       
  1013 */
       
  1014 void QPrinter::setDocName(const QString &name)
       
  1015 {
       
  1016     Q_D(QPrinter);
       
  1017     ABORT_IF_ACTIVE("QPrinter::setDocName");
       
  1018     d->printEngine->setProperty(QPrintEngine::PPK_DocumentName, name);
       
  1019     d->addToManualSetList(QPrintEngine::PPK_DocumentName);
       
  1020 }
       
  1021 
       
  1022 
       
  1023 /*!
       
  1024   Returns the name of the application that created the document.
       
  1025 
       
  1026   \sa setCreator()
       
  1027 */
       
  1028 QString QPrinter::creator() const
       
  1029 {
       
  1030     Q_D(const QPrinter);
       
  1031     return d->printEngine->property(QPrintEngine::PPK_Creator).toString();
       
  1032 }
       
  1033 
       
  1034 
       
  1035 /*!
       
  1036   Sets the name of the application that created the document to \a
       
  1037   creator.
       
  1038 
       
  1039   This function is only applicable to the X11 version of Qt. If no
       
  1040   creator name is specified, the creator will be set to "Qt"
       
  1041   followed by some version number.
       
  1042 
       
  1043   \sa creator()
       
  1044 */
       
  1045 void QPrinter::setCreator(const QString &creator)
       
  1046 {
       
  1047     Q_D(QPrinter);
       
  1048     ABORT_IF_ACTIVE("QPrinter::setCreator");
       
  1049     d->printEngine->setProperty(QPrintEngine::PPK_Creator, creator);
       
  1050     d->addToManualSetList(QPrintEngine::PPK_Creator);
       
  1051 }
       
  1052 
       
  1053 
       
  1054 /*!
       
  1055   Returns the orientation setting. This is driver-dependent, but is usually
       
  1056   QPrinter::Portrait.
       
  1057 
       
  1058   \sa setOrientation()
       
  1059 */
       
  1060 QPrinter::Orientation QPrinter::orientation() const
       
  1061 {
       
  1062     Q_D(const QPrinter);
       
  1063     return QPrinter::Orientation(d->printEngine->property(QPrintEngine::PPK_Orientation).toInt());
       
  1064 }
       
  1065 
       
  1066 
       
  1067 /*!
       
  1068   Sets the print orientation to \a orientation.
       
  1069 
       
  1070   The orientation can be either QPrinter::Portrait or
       
  1071   QPrinter::Landscape.
       
  1072 
       
  1073   The printer driver reads this setting and prints using the
       
  1074   specified orientation.
       
  1075 
       
  1076   On Windows, this option can be changed while printing and will
       
  1077   take effect from the next call to newPage().
       
  1078 
       
  1079   On Mac OS X, changing the orientation during a print job has no effect.
       
  1080 
       
  1081   \sa orientation()
       
  1082 */
       
  1083 
       
  1084 void QPrinter::setOrientation(Orientation orientation)
       
  1085 {
       
  1086     Q_D(QPrinter);
       
  1087     d->printEngine->setProperty(QPrintEngine::PPK_Orientation, orientation);
       
  1088     d->addToManualSetList(QPrintEngine::PPK_Orientation);
       
  1089 }
       
  1090 
       
  1091 
       
  1092 /*!
       
  1093     \since 4.4
       
  1094     Returns the printer paper size. The default value is driver-dependent.
       
  1095 
       
  1096     \sa setPaperSize() pageRect() paperRect()
       
  1097 */
       
  1098 
       
  1099 QPrinter::PaperSize QPrinter::paperSize() const
       
  1100 {
       
  1101     Q_D(const QPrinter);
       
  1102     return QPrinter::PaperSize(d->printEngine->property(QPrintEngine::PPK_PaperSize).toInt());
       
  1103 }
       
  1104 
       
  1105 /*!
       
  1106     \since 4.4
       
  1107 
       
  1108     Sets the printer paper size to \a newPaperSize if that size is
       
  1109     supported. The result is undefined if \a newPaperSize is not
       
  1110     supported.
       
  1111 
       
  1112     The default paper size is driver-dependent.
       
  1113 
       
  1114     This function is useful mostly for setting a default value that
       
  1115     the user can override in the print dialog.
       
  1116 
       
  1117     \sa paperSize() PaperSize setFullPage() setResolution() pageRect() paperRect()
       
  1118 */
       
  1119 void QPrinter::setPaperSize(PaperSize newPaperSize)
       
  1120 {
       
  1121     Q_D(QPrinter);
       
  1122     if (d->paintEngine->type() != QPaintEngine::Pdf)
       
  1123         ABORT_IF_ACTIVE("QPrinter::setPaperSize");
       
  1124     if (newPaperSize < 0 || newPaperSize >= NPaperSize) {
       
  1125         qWarning("QPrinter::setPaperSize: Illegal paper size %d", newPaperSize);
       
  1126         return;
       
  1127     }
       
  1128     d->printEngine->setProperty(QPrintEngine::PPK_PaperSize, newPaperSize);
       
  1129     d->addToManualSetList(QPrintEngine::PPK_PaperSize);
       
  1130     d->hasUserSetPageSize = true;
       
  1131 }
       
  1132 
       
  1133 /*!
       
  1134     \obsolete
       
  1135 
       
  1136     Returns the printer page size. The default value is driver-dependent.
       
  1137 
       
  1138     Use paperSize() instead.
       
  1139 */
       
  1140 QPrinter::PageSize QPrinter::pageSize() const
       
  1141 {
       
  1142     return paperSize();
       
  1143 }
       
  1144 
       
  1145 
       
  1146 /*!
       
  1147     \obsolete
       
  1148 
       
  1149     Sets the printer page size based on \a newPageSize.
       
  1150 
       
  1151     Use setPaperSize() instead.
       
  1152 */
       
  1153 
       
  1154 void QPrinter::setPageSize(PageSize newPageSize)
       
  1155 {
       
  1156     setPaperSize(newPageSize);
       
  1157 }
       
  1158 
       
  1159 /*!
       
  1160     \since 4.4
       
  1161 
       
  1162     Sets the paper size based on \a paperSize in \a unit.
       
  1163 
       
  1164     \sa paperSize()
       
  1165 */
       
  1166 
       
  1167 void QPrinter::setPaperSize(const QSizeF &paperSize, QPrinter::Unit unit)
       
  1168 {
       
  1169     Q_D(QPrinter);
       
  1170     if (d->paintEngine->type() != QPaintEngine::Pdf)
       
  1171         ABORT_IF_ACTIVE("QPrinter::setPaperSize");
       
  1172     const qreal multiplier = qt_multiplierForUnit(unit, resolution());
       
  1173     QSizeF size(paperSize.width() * multiplier, paperSize.height() * multiplier);
       
  1174     d->printEngine->setProperty(QPrintEngine::PPK_CustomPaperSize, size);
       
  1175     d->addToManualSetList(QPrintEngine::PPK_CustomPaperSize);
       
  1176     d->hasUserSetPageSize = true;
       
  1177 }
       
  1178 
       
  1179 /*!
       
  1180     \since 4.4
       
  1181 
       
  1182     Returns the paper size in \a unit.
       
  1183 
       
  1184     \sa setPaperSize()
       
  1185 */
       
  1186 
       
  1187 QSizeF QPrinter::paperSize(Unit unit) const
       
  1188 {
       
  1189     Q_D(const QPrinter);
       
  1190     int res = resolution();
       
  1191     const qreal multiplier = qt_multiplierForUnit(unit, res);
       
  1192     PaperSize paperType = paperSize();
       
  1193     if (paperType == Custom) {
       
  1194         QSizeF size = d->printEngine->property(QPrintEngine::PPK_CustomPaperSize).toSizeF();
       
  1195         return QSizeF(size.width() / multiplier, size.height() / multiplier);
       
  1196     }
       
  1197     else {
       
  1198         return qt_printerPaperSize(orientation(), paperType, unit, res);
       
  1199     }
       
  1200 }
       
  1201 
       
  1202 /*!
       
  1203     Sets the page order to \a pageOrder.
       
  1204 
       
  1205     The page order can be QPrinter::FirstPageFirst or
       
  1206     QPrinter::LastPageFirst. The application is responsible for
       
  1207     reading the page order and printing accordingly.
       
  1208 
       
  1209     This function is mostly useful for setting a default value that
       
  1210     the user can override in the print dialog.
       
  1211 
       
  1212     This function is only supported under X11.
       
  1213 */
       
  1214 
       
  1215 void QPrinter::setPageOrder(PageOrder pageOrder)
       
  1216 {
       
  1217     Q_D(QPrinter);
       
  1218     ABORT_IF_ACTIVE("QPrinter::setPageOrder");
       
  1219     d->printEngine->setProperty(QPrintEngine::PPK_PageOrder, pageOrder);
       
  1220     d->addToManualSetList(QPrintEngine::PPK_PageOrder);
       
  1221 }
       
  1222 
       
  1223 
       
  1224 /*!
       
  1225   Returns the current page order.
       
  1226 
       
  1227   The default page order is \c FirstPageFirst.
       
  1228 */
       
  1229 
       
  1230 QPrinter::PageOrder QPrinter::pageOrder() const
       
  1231 {
       
  1232     Q_D(const QPrinter);
       
  1233     return QPrinter::PageOrder(d->printEngine->property(QPrintEngine::PPK_PageOrder).toInt());
       
  1234 }
       
  1235 
       
  1236 
       
  1237 /*!
       
  1238   Sets the printer's color mode to \a newColorMode, which can be
       
  1239   either \c Color or \c GrayScale.
       
  1240 
       
  1241   \sa colorMode()
       
  1242 */
       
  1243 
       
  1244 void QPrinter::setColorMode(ColorMode newColorMode)
       
  1245 {
       
  1246     Q_D(QPrinter);
       
  1247     ABORT_IF_ACTIVE("QPrinter::setColorMode");
       
  1248     d->printEngine->setProperty(QPrintEngine::PPK_ColorMode, newColorMode);
       
  1249     d->addToManualSetList(QPrintEngine::PPK_ColorMode);
       
  1250 }
       
  1251 
       
  1252 
       
  1253 /*!
       
  1254   Returns the current color mode.
       
  1255 
       
  1256   \sa setColorMode()
       
  1257 */
       
  1258 QPrinter::ColorMode QPrinter::colorMode() const
       
  1259 {
       
  1260     Q_D(const QPrinter);
       
  1261     return QPrinter::ColorMode(d->printEngine->property(QPrintEngine::PPK_ColorMode).toInt());
       
  1262 }
       
  1263 
       
  1264 
       
  1265 /*!
       
  1266   Returns the number of copies to be printed. The default value is 1.
       
  1267 
       
  1268   On Windows, Mac OS X and X11 systems that support CUPS, this will always
       
  1269   return 1 as these operating systems can internally handle the number
       
  1270   of copies.
       
  1271 
       
  1272   On X11, this value will return the number of times the application is
       
  1273   required to print in order to match the number specified in the printer setup
       
  1274   dialog. This has been done since some printer drivers are not capable of
       
  1275   buffering up the copies and in those cases the application must make an
       
  1276   explicit call to the print code for each copy.
       
  1277 
       
  1278   \sa setNumCopies(), actualNumCopies()
       
  1279 */
       
  1280 
       
  1281 int QPrinter::numCopies() const
       
  1282 {
       
  1283     Q_D(const QPrinter);
       
  1284    return d->printEngine->property(QPrintEngine::PPK_NumberOfCopies).toInt();
       
  1285 }
       
  1286 
       
  1287 
       
  1288 /*!
       
  1289     \since 4.6
       
  1290 
       
  1291     Returns the number of copies that will be printed. The default
       
  1292     value is 1.
       
  1293 
       
  1294     This function always returns the actual value specified in the print
       
  1295     dialog or using setNumCopies().
       
  1296 
       
  1297     \sa setNumCopies(), numCopies()
       
  1298 */
       
  1299 int QPrinter::actualNumCopies() const
       
  1300 {
       
  1301     Q_D(const QPrinter);
       
  1302     return qt_printerRealNumCopies(d->paintEngine);
       
  1303 }
       
  1304 
       
  1305 
       
  1306 
       
  1307 /*!
       
  1308   Sets the number of copies to be printed to \a numCopies.
       
  1309 
       
  1310   The printer driver reads this setting and prints the specified
       
  1311   number of copies.
       
  1312 
       
  1313   \sa numCopies()
       
  1314 */
       
  1315 
       
  1316 void QPrinter::setNumCopies(int numCopies)
       
  1317 {
       
  1318     Q_D(QPrinter);
       
  1319     ABORT_IF_ACTIVE("QPrinter::setNumCopies");
       
  1320     d->printEngine->setProperty(QPrintEngine::PPK_NumberOfCopies, numCopies);
       
  1321     d->addToManualSetList(QPrintEngine::PPK_NumberOfCopies);
       
  1322 }
       
  1323 
       
  1324 
       
  1325 /*!
       
  1326     \since 4.1
       
  1327 
       
  1328     Returns true if collation is turned on when multiple copies is selected.
       
  1329     Returns false if it is turned off when multiple copies is selected.
       
  1330     When collating is turned off the printing of each individual page will be repeated
       
  1331     the numCopies() amount before the next page is started. With collating turned on
       
  1332     all pages are printed before the next copy of those pages is started.
       
  1333 
       
  1334     \sa setCollateCopies()
       
  1335 */
       
  1336 bool QPrinter::collateCopies() const
       
  1337 {
       
  1338     Q_D(const QPrinter);
       
  1339     return d->printEngine->property(QPrintEngine::PPK_CollateCopies).toBool();
       
  1340 }
       
  1341 
       
  1342 
       
  1343 /*!
       
  1344     \since 4.1
       
  1345 
       
  1346     Sets the default value for collation checkbox when the print
       
  1347     dialog appears.  If \a collate is true, it will enable
       
  1348     setCollateCopiesEnabled().  The default value is false. This value
       
  1349     will be changed by what the user presses in the print dialog.
       
  1350 
       
  1351     \sa collateCopies()
       
  1352 */
       
  1353 void QPrinter::setCollateCopies(bool collate)
       
  1354 {
       
  1355     Q_D(QPrinter);
       
  1356     ABORT_IF_ACTIVE("QPrinter::setCollateCopies");
       
  1357     d->printEngine->setProperty(QPrintEngine::PPK_CollateCopies, collate);
       
  1358     d->addToManualSetList(QPrintEngine::PPK_CollateCopies);
       
  1359 }
       
  1360 
       
  1361 
       
  1362 
       
  1363 /*!
       
  1364   If \a fp is true, enables support for painting over the entire page;
       
  1365   otherwise restricts painting to the printable area reported by the
       
  1366   device.
       
  1367 
       
  1368   By default, full page printing is disabled. In this case, the origin
       
  1369   of the QPrinter's coordinate system coincides with the top-left
       
  1370   corner of the printable area.
       
  1371 
       
  1372   If full page printing is enabled, the origin of the QPrinter's
       
  1373   coordinate system coincides with the top-left corner of the paper
       
  1374   itself. In this case, the
       
  1375   \l{QPaintDevice::PaintDeviceMetric}{device metrics} will report
       
  1376   the exact same dimensions as indicated by \l{PaperSize}. It may not
       
  1377   be possible to print on the entire physical page because of the
       
  1378   printer's margins, so the application must account for the margins
       
  1379   itself.
       
  1380 
       
  1381   \sa fullPage(), setPaperSize(), width(), height(), {Printing with Qt}
       
  1382 */
       
  1383 
       
  1384 void QPrinter::setFullPage(bool fp)
       
  1385 {
       
  1386     Q_D(QPrinter);
       
  1387     d->printEngine->setProperty(QPrintEngine::PPK_FullPage, fp);
       
  1388     d->addToManualSetList(QPrintEngine::PPK_FullPage);
       
  1389 }
       
  1390 
       
  1391 
       
  1392 /*!
       
  1393   Returns true if the origin of the printer's coordinate system is
       
  1394   at the corner of the page and false if it is at the edge of the
       
  1395   printable area.
       
  1396 
       
  1397   See setFullPage() for details and caveats.
       
  1398 
       
  1399   \sa setFullPage() PaperSize
       
  1400 */
       
  1401 
       
  1402 bool QPrinter::fullPage() const
       
  1403 {
       
  1404     Q_D(const QPrinter);
       
  1405     return d->printEngine->property(QPrintEngine::PPK_FullPage).toBool();
       
  1406 }
       
  1407 
       
  1408 
       
  1409 /*!
       
  1410   Requests that the printer prints at \a dpi or as near to \a dpi as
       
  1411   possible.
       
  1412 
       
  1413   This setting affects the coordinate system as returned by, for
       
  1414   example QPainter::viewport().
       
  1415 
       
  1416   This function must be called before QPainter::begin() to have an effect on
       
  1417   all platforms.
       
  1418 
       
  1419   \sa resolution() setPaperSize()
       
  1420 */
       
  1421 
       
  1422 void QPrinter::setResolution(int dpi)
       
  1423 {
       
  1424     Q_D(QPrinter);
       
  1425     ABORT_IF_ACTIVE("QPrinter::setResolution");
       
  1426     d->printEngine->setProperty(QPrintEngine::PPK_Resolution, dpi);
       
  1427     d->addToManualSetList(QPrintEngine::PPK_Resolution);
       
  1428 }
       
  1429 
       
  1430 
       
  1431 /*!
       
  1432   Returns the current assumed resolution of the printer, as set by
       
  1433   setResolution() or by the printer driver.
       
  1434 
       
  1435   \sa setResolution()
       
  1436 */
       
  1437 
       
  1438 int QPrinter::resolution() const
       
  1439 {
       
  1440     Q_D(const QPrinter);
       
  1441     return d->printEngine->property(QPrintEngine::PPK_Resolution).toInt();
       
  1442 }
       
  1443 
       
  1444 /*!
       
  1445   Sets the paper source setting to \a source.
       
  1446 
       
  1447   Windows only: This option can be changed while printing and will
       
  1448   take effect from the next call to newPage()
       
  1449 
       
  1450   \sa paperSource()
       
  1451 */
       
  1452 
       
  1453 void QPrinter::setPaperSource(PaperSource source)
       
  1454 {
       
  1455     Q_D(QPrinter);
       
  1456     d->printEngine->setProperty(QPrintEngine::PPK_PaperSource, source);
       
  1457     d->addToManualSetList(QPrintEngine::PPK_PaperSource);
       
  1458 }
       
  1459 
       
  1460 /*!
       
  1461     Returns the printer's paper source. This is \c Manual or a printer
       
  1462     tray or paper cassette.
       
  1463 */
       
  1464 QPrinter::PaperSource QPrinter::paperSource() const
       
  1465 {
       
  1466     Q_D(const QPrinter);
       
  1467     return QPrinter::PaperSource(d->printEngine->property(QPrintEngine::PPK_PaperSource).toInt());
       
  1468 }
       
  1469 
       
  1470 
       
  1471 /*!
       
  1472   \since 4.1
       
  1473 
       
  1474   Enabled or disables font embedding depending on \a enable.
       
  1475 
       
  1476   Currently this option is only supported on X11.
       
  1477 
       
  1478   \sa fontEmbeddingEnabled()
       
  1479 */
       
  1480 void QPrinter::setFontEmbeddingEnabled(bool enable)
       
  1481 {
       
  1482     Q_D(QPrinter);
       
  1483     d->printEngine->setProperty(QPrintEngine::PPK_FontEmbedding, enable);
       
  1484     d->addToManualSetList(QPrintEngine::PPK_FontEmbedding);
       
  1485 }
       
  1486 
       
  1487 /*!
       
  1488   \since 4.1
       
  1489 
       
  1490   Returns true if font embedding is enabled.
       
  1491 
       
  1492   Currently this option is only supported on X11.
       
  1493 
       
  1494   \sa setFontEmbeddingEnabled()
       
  1495 */
       
  1496 bool QPrinter::fontEmbeddingEnabled() const
       
  1497 {
       
  1498     Q_D(const QPrinter);
       
  1499     return d->printEngine->property(QPrintEngine::PPK_FontEmbedding).toBool();
       
  1500 }
       
  1501 
       
  1502 /*!
       
  1503     \enum QPrinter::DuplexMode
       
  1504     \since 4.4
       
  1505 
       
  1506     This enum is used to indicate whether printing will occur on one or both sides
       
  1507     of each sheet of paper (simplex or duplex printing).
       
  1508 
       
  1509     \value DuplexNone       Single sided (simplex) printing only.
       
  1510     \value DuplexAuto       The printer's default setting is used to determine whether
       
  1511                             duplex printing is used.
       
  1512     \value DuplexLongSide   Both sides of each sheet of paper are used for printing.
       
  1513                             The paper is turned over its longest edge before the second
       
  1514                             side is printed
       
  1515     \value DuplexShortSide  Both sides of each sheet of paper are used for printing.
       
  1516                             The paper is turned over its shortest edge before the second
       
  1517                             side is printed
       
  1518 */
       
  1519 
       
  1520 /*!
       
  1521   \since 4.2
       
  1522 
       
  1523   Enables double sided printing if \a doubleSided is true; otherwise disables it.
       
  1524 
       
  1525   Currently this option is only supported on X11.
       
  1526 */
       
  1527 void QPrinter::setDoubleSidedPrinting(bool doubleSided)
       
  1528 {
       
  1529     setDuplex(doubleSided ? DuplexAuto : DuplexNone);
       
  1530 }
       
  1531 
       
  1532 
       
  1533 /*!
       
  1534   \since 4.2
       
  1535 
       
  1536   Returns true if double side printing is enabled.
       
  1537 
       
  1538   Currently this option is only supported on X11.
       
  1539 */
       
  1540 bool QPrinter::doubleSidedPrinting() const
       
  1541 {
       
  1542     return duplex() != DuplexNone;
       
  1543 }
       
  1544 
       
  1545 /*!
       
  1546   \since 4.4
       
  1547 
       
  1548   Enables double sided printing based on the \a duplex mode.
       
  1549 
       
  1550   Currently this option is only supported on X11.
       
  1551 */
       
  1552 void QPrinter::setDuplex(DuplexMode duplex)
       
  1553 {
       
  1554     Q_D(QPrinter);
       
  1555     d->printEngine->setProperty(QPrintEngine::PPK_Duplex, duplex);
       
  1556     d->addToManualSetList(QPrintEngine::PPK_Duplex);
       
  1557 }
       
  1558 
       
  1559 /*!
       
  1560   \since 4.4
       
  1561 
       
  1562   Returns the current duplex mode.
       
  1563 
       
  1564   Currently this option is only supported on X11.
       
  1565 */
       
  1566 QPrinter::DuplexMode QPrinter::duplex() const
       
  1567 {
       
  1568     Q_D(const QPrinter);
       
  1569     return static_cast <DuplexMode> (d->printEngine->property(QPrintEngine::PPK_Duplex).toInt());
       
  1570 }
       
  1571 
       
  1572 /*!
       
  1573     \since 4.4
       
  1574 
       
  1575     Returns the page's rectangle in \a unit; this is usually smaller
       
  1576     than the paperRect() since the page normally has margins between
       
  1577     its borders and the paper.
       
  1578 
       
  1579     \sa paperSize()
       
  1580 */
       
  1581 QRectF QPrinter::pageRect(Unit unit) const
       
  1582 {
       
  1583     Q_D(const QPrinter);
       
  1584     int res = resolution();
       
  1585     const qreal multiplier = qt_multiplierForUnit(unit, res);
       
  1586     // the page rect is in device pixels
       
  1587     QRect devRect(d->printEngine->property(QPrintEngine::PPK_PageRect).toRect());
       
  1588     if (unit == DevicePixel)
       
  1589         return devRect;
       
  1590     QRectF diRect(devRect.x()*72.0/res,
       
  1591                   devRect.y()*72.0/res,
       
  1592                   devRect.width()*72.0/res,
       
  1593                   devRect.height()*72.0/res);
       
  1594     return QRectF(diRect.x()/multiplier, diRect.y()/multiplier,
       
  1595                   diRect.width()/multiplier, diRect.height()/multiplier);
       
  1596 }
       
  1597 
       
  1598 
       
  1599 /*!
       
  1600     \since 4.4
       
  1601 
       
  1602     Returns the paper's rectangle in \a unit; this is usually larger
       
  1603     than the pageRect().
       
  1604 
       
  1605    \sa pageRect()
       
  1606 */
       
  1607 QRectF QPrinter::paperRect(Unit unit) const
       
  1608 {
       
  1609     Q_D(const QPrinter);
       
  1610     int res = resolution();
       
  1611     const qreal multiplier = qt_multiplierForUnit(unit, resolution());
       
  1612     // the page rect is in device pixels
       
  1613     QRect devRect(d->printEngine->property(QPrintEngine::PPK_PaperRect).toRect());
       
  1614     if (unit == DevicePixel)
       
  1615         return devRect;
       
  1616     QRectF diRect(devRect.x()*72.0/res,
       
  1617                   devRect.y()*72.0/res,
       
  1618                   devRect.width()*72.0/res,
       
  1619                   devRect.height()*72.0/res);
       
  1620     return QRectF(diRect.x()/multiplier, diRect.y()/multiplier,
       
  1621                   diRect.width()/multiplier, diRect.height()/multiplier);
       
  1622 }
       
  1623 
       
  1624 /*!
       
  1625     Returns the page's rectangle; this is usually smaller than the
       
  1626     paperRect() since the page normally has margins between its
       
  1627     borders and the paper.
       
  1628 
       
  1629     The unit of the returned rectangle is DevicePixel.
       
  1630 
       
  1631     \sa paperSize()
       
  1632 */
       
  1633 QRect QPrinter::pageRect() const
       
  1634 {
       
  1635     Q_D(const QPrinter);
       
  1636     return d->printEngine->property(QPrintEngine::PPK_PageRect).toRect();
       
  1637 }
       
  1638 
       
  1639 /*!
       
  1640     Returns the paper's rectangle; this is usually larger than the
       
  1641     pageRect().
       
  1642 
       
  1643     The unit of the returned rectangle is DevicePixel.
       
  1644 
       
  1645     \sa pageRect()
       
  1646 */
       
  1647 QRect QPrinter::paperRect() const
       
  1648 {
       
  1649     Q_D(const QPrinter);
       
  1650     return d->printEngine->property(QPrintEngine::PPK_PaperRect).toRect();
       
  1651 }
       
  1652 
       
  1653 
       
  1654 /*!
       
  1655     \since 4.4
       
  1656 
       
  1657     This function sets the \a left, \a top, \a right and \a bottom
       
  1658     page margins for this printer. The unit of the margins are
       
  1659     specified with the \a unit parameter.
       
  1660 */
       
  1661 void QPrinter::setPageMargins(qreal left, qreal top, qreal right, qreal bottom, QPrinter::Unit unit)
       
  1662 {
       
  1663     Q_D(QPrinter);
       
  1664     const qreal multiplier = qt_multiplierForUnit(unit, resolution());
       
  1665     QList<QVariant> margins;
       
  1666     margins << (left * multiplier) << (top * multiplier)
       
  1667             << (right * multiplier) << (bottom * multiplier);
       
  1668     d->printEngine->setProperty(QPrintEngine::PPK_PageMargins, margins);
       
  1669     d->addToManualSetList(QPrintEngine::PPK_PageMargins);
       
  1670     d->hasCustomPageMargins = true;
       
  1671 }
       
  1672 
       
  1673 /*!
       
  1674     \since 4.4
       
  1675 
       
  1676     Returns the page margins for this printer in \a left, \a top, \a
       
  1677     right, \a bottom. The unit of the returned margins are specified
       
  1678     with the \a unit parameter.
       
  1679 */
       
  1680 void QPrinter::getPageMargins(qreal *left, qreal *top, qreal *right, qreal *bottom, QPrinter::Unit unit) const
       
  1681 {
       
  1682     Q_D(const QPrinter);
       
  1683     Q_ASSERT(left && top && right && bottom);
       
  1684     const qreal multiplier = qt_multiplierForUnit(unit, resolution());
       
  1685     QList<QVariant> margins(d->printEngine->property(QPrintEngine::PPK_PageMargins).toList());
       
  1686     *left = margins.at(0).toReal() / multiplier;
       
  1687     *top = margins.at(1).toReal() / multiplier;
       
  1688     *right = margins.at(2).toReal() / multiplier;
       
  1689     *bottom = margins.at(3).toReal() / multiplier;
       
  1690 }
       
  1691 
       
  1692 /*!
       
  1693     \internal
       
  1694 
       
  1695     Returns the metric for the given \a id.
       
  1696 */
       
  1697 int QPrinter::metric(PaintDeviceMetric id) const
       
  1698 {
       
  1699     Q_D(const QPrinter);
       
  1700     return d->printEngine->metric(id);
       
  1701 }
       
  1702 
       
  1703 /*!
       
  1704     Returns the paint engine used by the printer.
       
  1705 */
       
  1706 QPaintEngine *QPrinter::paintEngine() const
       
  1707 {
       
  1708     Q_D(const QPrinter);
       
  1709     return d->paintEngine;
       
  1710 }
       
  1711 
       
  1712 /*!
       
  1713     \since 4.1
       
  1714 
       
  1715     Returns the print engine used by the printer.
       
  1716 */
       
  1717 QPrintEngine *QPrinter::printEngine() const
       
  1718 {
       
  1719     Q_D(const QPrinter);
       
  1720     return d->printEngine;
       
  1721 }
       
  1722 
       
  1723 #if defined (Q_WS_WIN)
       
  1724 /*!
       
  1725     Sets the page size to be used by the printer under Windows to \a
       
  1726     pageSize.
       
  1727 
       
  1728     \warning This function is not portable so you may prefer to use
       
  1729     setPaperSize() instead.
       
  1730 
       
  1731     \sa winPageSize()
       
  1732 */
       
  1733 void QPrinter::setWinPageSize(int pageSize)
       
  1734 {
       
  1735     Q_D(QPrinter);
       
  1736     ABORT_IF_ACTIVE("QPrinter::setWinPageSize");
       
  1737     d->printEngine->setProperty(QPrintEngine::PPK_WindowsPageSize, pageSize);
       
  1738     d->addToManualSetList(QPrintEngine::PPK_WindowsPageSize);
       
  1739 }
       
  1740 
       
  1741 /*!
       
  1742     Returns the page size used by the printer under Windows.
       
  1743 
       
  1744     \warning This function is not portable so you may prefer to use
       
  1745     paperSize() instead.
       
  1746 
       
  1747     \sa setWinPageSize()
       
  1748 */
       
  1749 int QPrinter::winPageSize() const
       
  1750 {
       
  1751     Q_D(const QPrinter);
       
  1752     return d->printEngine->property(QPrintEngine::PPK_WindowsPageSize).toInt();
       
  1753 }
       
  1754 #endif // Q_WS_WIN
       
  1755 
       
  1756 /*!
       
  1757     Returns a list of the resolutions (a list of dots-per-inch
       
  1758     integers) that the printer says it supports.
       
  1759 
       
  1760     For X11 where all printing is directly to postscript, this
       
  1761     function will always return a one item list containing only the
       
  1762     postscript resolution, i.e., 72 (72 dpi -- but see PrinterMode).
       
  1763 */
       
  1764 QList<int> QPrinter::supportedResolutions() const
       
  1765 {
       
  1766     Q_D(const QPrinter);
       
  1767     QList<QVariant> varlist
       
  1768         = d->printEngine->property(QPrintEngine::PPK_SupportedResolutions).toList();
       
  1769     QList<int> intlist;
       
  1770     for (int i=0; i<varlist.size(); ++i)
       
  1771         intlist << varlist.at(i).toInt();
       
  1772     return intlist;
       
  1773 }
       
  1774 
       
  1775 /*!
       
  1776     Tells the printer to eject the current page and to continue
       
  1777     printing on a new page. Returns true if this was successful;
       
  1778     otherwise returns false.
       
  1779 
       
  1780     Calling newPage() on an inactive QPrinter object will always
       
  1781     fail.
       
  1782 */
       
  1783 bool QPrinter::newPage()
       
  1784 {
       
  1785     Q_D(QPrinter);
       
  1786     if (d->printEngine->printerState() != QPrinter::Active)
       
  1787         return false;
       
  1788     return d->printEngine->newPage();
       
  1789 }
       
  1790 
       
  1791 /*!
       
  1792     Aborts the current print run. Returns true if the print run was
       
  1793     successfully aborted and printerState() will return QPrinter::Aborted; otherwise
       
  1794     returns false.
       
  1795 
       
  1796     It is not always possible to abort a print job. For example,
       
  1797     all the data has gone to the printer but the printer cannot or
       
  1798     will not cancel the job when asked to.
       
  1799 */
       
  1800 bool QPrinter::abort()
       
  1801 {
       
  1802     Q_D(QPrinter);
       
  1803     return d->printEngine->abort();
       
  1804 }
       
  1805 
       
  1806 /*!
       
  1807     Returns the current state of the printer. This may not always be
       
  1808     accurate (for example if the printer doesn't have the capability
       
  1809     of reporting its state to the operating system).
       
  1810 */
       
  1811 QPrinter::PrinterState QPrinter::printerState() const
       
  1812 {
       
  1813     Q_D(const QPrinter);
       
  1814     return d->printEngine->printerState();
       
  1815 }
       
  1816 
       
  1817 
       
  1818 /*! \fn void QPrinter::margins(uint *top, uint *left, uint *bottom, uint *right) const
       
  1819 
       
  1820     Sets *\a top, *\a left, *\a bottom, *\a right to be the top,
       
  1821     left, bottom, and right margins.
       
  1822 
       
  1823     This function has been superseded by paperRect() and pageRect().
       
  1824     Use paperRect().top() - pageRect().top() for the top margin,
       
  1825     paperRect().left() - pageRect().left() for the left margin,
       
  1826     paperRect().bottom() - pageRect().bottom() for the bottom margin,
       
  1827     and papaerRect().right() - pageRect().right() for the right
       
  1828     margin.
       
  1829 
       
  1830     \oldcode
       
  1831         uint rightMargin;
       
  1832         uint bottomMargin;
       
  1833         printer->margins(0, 0, &bottomMargin, &rightMargin);
       
  1834     \newcode
       
  1835         int rightMargin = printer->paperRect().right() - printer->pageRect().right();
       
  1836         int bottomMargin = printer->paperRect().bottom() - printer->pageRect().bottom();
       
  1837     \endcode
       
  1838 */
       
  1839 
       
  1840 /*! \fn QSize QPrinter::margins() const
       
  1841 
       
  1842     \overload
       
  1843 
       
  1844     Returns a QSize containing the left margin and the top margin.
       
  1845 
       
  1846     This function has been superseded by paperRect() and pageRect().
       
  1847     Use paperRect().left() - pageRect().left() for the left margin,
       
  1848     and paperRect().top() - pageRect().top() for the top margin.
       
  1849 
       
  1850     \oldcode
       
  1851         QSize margins = printer->margins();
       
  1852         int leftMargin = margins.width();
       
  1853         int topMargin = margins.height();
       
  1854     \newcode
       
  1855         int leftMargin = printer->paperRect().left() - printer->pageRect().left();
       
  1856         int topMargin = printer->paperRect().top() - printer->pageRect().top();
       
  1857     \endcode
       
  1858 */
       
  1859 
       
  1860 /*! \fn bool QPrinter::aborted()
       
  1861 
       
  1862     Use printerState() == QPrinter::Aborted instead.
       
  1863 */
       
  1864 
       
  1865 #ifdef Q_WS_WIN
       
  1866 /*!
       
  1867     \internal
       
  1868 */
       
  1869 HDC QPrinter::getDC() const
       
  1870 {
       
  1871     Q_D(const QPrinter);
       
  1872     return d->printEngine->getPrinterDC();
       
  1873 }
       
  1874 
       
  1875 /*!
       
  1876     \internal
       
  1877 */
       
  1878 void QPrinter::releaseDC(HDC hdc) const
       
  1879 {
       
  1880     Q_D(const QPrinter);
       
  1881     d->printEngine->releasePrinterDC(hdc);
       
  1882 }
       
  1883 
       
  1884 /*!
       
  1885     Returns the supported paper sizes for this printer.
       
  1886 
       
  1887     The values will be either a value that matches an entry in the
       
  1888     QPrinter::PaperSource enum or a driver spesific value. The driver
       
  1889     spesific values are greater than the constant DMBIN_USER declared
       
  1890     in wingdi.h.
       
  1891 
       
  1892     \warning This function is only available in windows.
       
  1893 */
       
  1894 
       
  1895 QList<QPrinter::PaperSource> QPrinter::supportedPaperSources() const
       
  1896 {
       
  1897     Q_D(const QPrinter);
       
  1898     QVariant v = d->printEngine->property(QPrintEngine::PPK_PaperSources);
       
  1899 
       
  1900     QList<QVariant> variant_list = v.toList();
       
  1901     QList<QPrinter::PaperSource> int_list;
       
  1902     for (int i=0; i<variant_list.size(); ++i)
       
  1903         int_list << (QPrinter::PaperSource) variant_list.at(i).toInt();
       
  1904 
       
  1905     return int_list;
       
  1906 }
       
  1907 
       
  1908 #endif
       
  1909 
       
  1910 /*!
       
  1911     \fn QString QPrinter::printerSelectionOption() const
       
  1912 
       
  1913     Returns the printer options selection string. This is useful only
       
  1914     if the print command has been explicitly set.
       
  1915 
       
  1916     The default value (an empty string) implies that the printer should
       
  1917     be selected in a system-dependent manner.
       
  1918 
       
  1919     Any other value implies that the given value should be used.
       
  1920 
       
  1921     \warning This function is not available on Windows.
       
  1922 
       
  1923     \sa setPrinterSelectionOption()
       
  1924 */
       
  1925 
       
  1926 /*!
       
  1927     \fn void QPrinter::setPrinterSelectionOption(const QString &option)
       
  1928 
       
  1929     Sets the printer to use \a option to select the printer. \a option
       
  1930     is null by default (which implies that Qt should be smart enough
       
  1931     to guess correctly), but it can be set to other values to use a
       
  1932     specific printer selection option.
       
  1933 
       
  1934     If the printer selection option is changed while the printer is
       
  1935     active, the current print job may or may not be affected.
       
  1936 
       
  1937     \warning This function is not available on Windows.
       
  1938 
       
  1939     \sa printerSelectionOption()
       
  1940 */
       
  1941 
       
  1942 #ifndef Q_WS_WIN
       
  1943 QString QPrinter::printerSelectionOption() const
       
  1944 {
       
  1945     Q_D(const QPrinter);
       
  1946     return d->printEngine->property(QPrintEngine::PPK_SelectionOption).toString();
       
  1947 }
       
  1948 
       
  1949 void QPrinter::setPrinterSelectionOption(const QString &option)
       
  1950 {
       
  1951     Q_D(QPrinter);
       
  1952     d->printEngine->setProperty(QPrintEngine::PPK_SelectionOption, option);
       
  1953     d->addToManualSetList(QPrintEngine::PPK_SelectionOption);
       
  1954 }
       
  1955 #endif
       
  1956 
       
  1957 /*!
       
  1958     \since 4.1
       
  1959     \fn int QPrinter::fromPage() const
       
  1960 
       
  1961     Returns the number of the first page in a range of pages to be printed
       
  1962     (the "from page" setting). Pages in a document are numbered according to
       
  1963     the convention that the first page is page 1.
       
  1964 
       
  1965     By default, this function returns a special value of 0, meaning that
       
  1966     the "from page" setting is unset.
       
  1967 
       
  1968     \note If fromPage() and toPage() both return 0, this indicates that
       
  1969     \e{the whole document will be printed}.
       
  1970 
       
  1971     \sa setFromTo(), toPage()
       
  1972 */
       
  1973 
       
  1974 int QPrinter::fromPage() const
       
  1975 {
       
  1976     Q_D(const QPrinter);
       
  1977     return d->fromPage;
       
  1978 }
       
  1979 
       
  1980 /*!
       
  1981     \since 4.1
       
  1982 
       
  1983     Returns the number of the last page in a range of pages to be printed
       
  1984     (the "to page" setting). Pages in a document are numbered according to
       
  1985     the convention that the first page is page 1.
       
  1986 
       
  1987     By default, this function returns a special value of 0, meaning that
       
  1988     the "to page" setting is unset.
       
  1989 
       
  1990     \note If fromPage() and toPage() both return 0, this indicates that
       
  1991     \e{the whole document will be printed}.
       
  1992 
       
  1993     The programmer is responsible for reading this setting and
       
  1994     printing accordingly.
       
  1995 
       
  1996     \sa setFromTo(), fromPage()
       
  1997 */
       
  1998 
       
  1999 int QPrinter::toPage() const
       
  2000 {
       
  2001     Q_D(const QPrinter);
       
  2002     return d->toPage;
       
  2003 }
       
  2004 
       
  2005 /*!
       
  2006     \since 4.1
       
  2007 
       
  2008     Sets the range of pages to be printed to cover the pages with numbers
       
  2009     specified by \a from and \a to, where \a from corresponds to the first
       
  2010     page in the range and \a to corresponds to the last.
       
  2011 
       
  2012     \note Pages in a document are numbered according to the convention that
       
  2013     the first page is page 1. However, if \a from and \a to are both set to 0,
       
  2014     the \e{whole document will be printed}.
       
  2015 
       
  2016     This function is mostly used to set a default value that the user can
       
  2017     override in the print dialog when you call setup().
       
  2018 
       
  2019     \sa fromPage(), toPage()
       
  2020 */
       
  2021 
       
  2022 void QPrinter::setFromTo(int from, int to)
       
  2023 {
       
  2024     Q_D(QPrinter);
       
  2025     if (from > to) {
       
  2026         qWarning() << "QPrinter::setFromTo: 'from' must be less than or equal to 'to'";
       
  2027         from = to;
       
  2028     }
       
  2029     d->fromPage = from;
       
  2030     d->toPage = to;
       
  2031 
       
  2032     if (d->minPage == 0 && d->maxPage == 0) {
       
  2033         d->minPage = 1;
       
  2034         d->maxPage = to;
       
  2035         d->options |= QAbstractPrintDialog::PrintPageRange;
       
  2036     }
       
  2037 }
       
  2038 
       
  2039 /*!
       
  2040     \since 4.1
       
  2041 
       
  2042     Sets the print range option in to be \a range.
       
  2043 */
       
  2044 void QPrinter::setPrintRange( PrintRange range )
       
  2045 {
       
  2046     Q_D(QPrinter);
       
  2047     d->printRange = QAbstractPrintDialog::PrintRange(range);
       
  2048 }
       
  2049 
       
  2050 /*!
       
  2051     \since 4.1
       
  2052 
       
  2053     Returns the page range of the QPrinter. After the print setup
       
  2054     dialog has been opened, this function returns the value selected
       
  2055     by the user.
       
  2056 
       
  2057     \sa setPrintRange()
       
  2058 */
       
  2059 QPrinter::PrintRange QPrinter::printRange() const
       
  2060 {
       
  2061     Q_D(const QPrinter);
       
  2062     return PrintRange(d->printRange);
       
  2063 }
       
  2064 
       
  2065 #if defined(QT3_SUPPORT)
       
  2066 
       
  2067 void QPrinter::setOutputToFile(bool f)
       
  2068 {
       
  2069     if (f) {
       
  2070         if (outputFileName().isEmpty())
       
  2071             setOutputFileName(QLatin1String("untitled_printer_document"));
       
  2072     } else {
       
  2073         setOutputFileName(QString());
       
  2074     }
       
  2075 }
       
  2076 
       
  2077 bool qt_compat_QPrinter_printSetup(QPrinter *printer, QPrinterPrivate *pd, QWidget *parent)
       
  2078 {
       
  2079     Q_UNUSED(pd);
       
  2080     QPrintDialog dlg(printer, parent);
       
  2081     return dlg.exec() != 0;
       
  2082 }
       
  2083 
       
  2084 
       
  2085 #ifdef Q_WS_MAC
       
  2086 bool qt_compat_QPrinter_pageSetup(QPrinter *p, QWidget *parent)
       
  2087 {
       
  2088     QPageSetupDialog psd(p, parent);
       
  2089     return psd.exec() != 0;
       
  2090 }
       
  2091 
       
  2092 /*!
       
  2093     Executes a page setup dialog so that the user can configure the type of
       
  2094     page used for printing. Returns true if the contents of the dialog are
       
  2095     accepted; returns false if the dialog is canceled.
       
  2096 */
       
  2097 bool QPrinter::pageSetup(QWidget *parent)
       
  2098 {
       
  2099     return qt_compat_QPrinter_pageSetup(this, parent);
       
  2100 }
       
  2101 
       
  2102 /*!
       
  2103     Executes a print setup dialog so that the user can configure the printing
       
  2104     process. Returns true if the contents of the dialog are accepted; returns
       
  2105     false if the dialog is canceled.
       
  2106 */
       
  2107 bool QPrinter::printSetup(QWidget *parent)
       
  2108 {
       
  2109     Q_D(QPrinter);
       
  2110     return qt_compat_QPrinter_printSetup(this, d, parent);
       
  2111 }
       
  2112 #endif // Q_WS_MAC
       
  2113 
       
  2114 /*!
       
  2115     Use QPrintDialog instead.
       
  2116 
       
  2117     \oldcode
       
  2118         if (printer->setup(parent))
       
  2119             ...
       
  2120     \newcode
       
  2121         QPrintDialog dialog(printer, parent);
       
  2122         if (dialog.exec())
       
  2123             ...
       
  2124     \endcode
       
  2125 */
       
  2126 bool QPrinter::setup(QWidget *parent)
       
  2127 {
       
  2128     Q_D(QPrinter);
       
  2129     return qt_compat_QPrinter_printSetup(this, d, parent)
       
  2130 #ifdef Q_WS_MAC
       
  2131         && qt_compat_QPrinter_pageSetup(this, parent);
       
  2132 #endif
       
  2133         ;
       
  2134 }
       
  2135 
       
  2136 /*!
       
  2137     Use QPrintDialog::minPage() instead.
       
  2138 */
       
  2139 int QPrinter::minPage() const
       
  2140 {
       
  2141     Q_D(const QPrinter);
       
  2142     return d->minPage;
       
  2143 }
       
  2144 
       
  2145 /*!
       
  2146     Use QPrintDialog::maxPage() instead.
       
  2147 */
       
  2148 int QPrinter::maxPage() const
       
  2149 {
       
  2150     Q_D(const QPrinter);
       
  2151     return d->maxPage;
       
  2152 }
       
  2153 
       
  2154 /*!
       
  2155     Use QPrintDialog::setMinMax() instead.
       
  2156 */
       
  2157 void QPrinter::setMinMax( int minPage, int maxPage )
       
  2158 {
       
  2159     Q_D(QPrinter);
       
  2160     Q_ASSERT_X(minPage <= maxPage, "QPrinter::setMinMax",
       
  2161                "'min' must be less than or equal to 'max'");
       
  2162     d->minPage = minPage;
       
  2163     d->maxPage = maxPage;
       
  2164     d->options |= QPrintDialog::PrintPageRange;
       
  2165 }
       
  2166 
       
  2167 /*!
       
  2168     Returns true if the printer is set up to collate copies of printed documents;
       
  2169     otherwise returns false.
       
  2170 
       
  2171     Use QPrintDialog::isOptionEnabled(QPrintDialog::PrintCollateCopies)
       
  2172     instead.
       
  2173 
       
  2174     \sa collateCopies()
       
  2175 */
       
  2176 bool QPrinter::collateCopiesEnabled() const
       
  2177 {
       
  2178     Q_D(const QPrinter);
       
  2179     return (d->options & QPrintDialog::PrintCollateCopies);
       
  2180 }
       
  2181 
       
  2182 /*!
       
  2183     Use QPrintDialog::setOption(QPrintDialog::PrintCollateCopies)
       
  2184     or QPrintDialog::setOptions(QPrintDialog::options()
       
  2185     & ~QPrintDialog::PrintCollateCopies) instead, depending on \a
       
  2186     enable.
       
  2187 */
       
  2188 void QPrinter::setCollateCopiesEnabled(bool enable)
       
  2189 {
       
  2190     Q_D(QPrinter);
       
  2191 
       
  2192     if (enable)
       
  2193         d->options |= QPrintDialog::PrintCollateCopies;
       
  2194     else
       
  2195         d->options &= ~QPrintDialog::PrintCollateCopies;
       
  2196 }
       
  2197 
       
  2198 /*!
       
  2199     Use QPrintDialog instead.
       
  2200 */
       
  2201 void QPrinter::setOptionEnabled( PrinterOption option, bool enable )
       
  2202 {
       
  2203     Q_D(QPrinter);
       
  2204     if (enable)
       
  2205         d->options |= QPrintDialog::PrintDialogOption(1 << option);
       
  2206     else
       
  2207         d->options &= ~QPrintDialog::PrintDialogOption(1 << option);
       
  2208 }
       
  2209 
       
  2210 /*!
       
  2211     Use QPrintDialog instead.
       
  2212 */
       
  2213 bool QPrinter::isOptionEnabled( PrinterOption option ) const
       
  2214 {
       
  2215     Q_D(const QPrinter);
       
  2216     return (d->options & QPrintDialog::PrintDialogOption(option));
       
  2217 }
       
  2218 
       
  2219 #endif // QT3_SUPPORT
       
  2220 
       
  2221 /*!
       
  2222     \class QPrintEngine
       
  2223     \reentrant
       
  2224 
       
  2225     \ingroup printing
       
  2226 
       
  2227     \brief The QPrintEngine class defines an interface for how QPrinter
       
  2228     interacts with a given printing subsystem.
       
  2229 
       
  2230     The common case when creating your own print engine is to derive from both
       
  2231     QPaintEngine and QPrintEngine. Various properties of a print engine are
       
  2232     given with property() and set with setProperty().
       
  2233 
       
  2234     \sa QPaintEngine
       
  2235 */
       
  2236 
       
  2237 /*!
       
  2238     \enum QPrintEngine::PrintEnginePropertyKey
       
  2239 
       
  2240     This enum is used to communicate properties between the print
       
  2241     engine and QPrinter. A property may or may not be supported by a
       
  2242     given print engine.
       
  2243 
       
  2244     \value PPK_CollateCopies A boolean value indicating whether the
       
  2245     printout should be collated or not.
       
  2246 
       
  2247     \value PPK_ColorMode Refers to QPrinter::ColorMode, either color or
       
  2248     monochrome.
       
  2249 
       
  2250     \value PPK_Creator A string describing the document's creator.
       
  2251 
       
  2252     \value PPK_Duplex A boolean value indicating whether both sides of
       
  2253     the printer paper should be used for the printout.
       
  2254 
       
  2255     \value PPK_DocumentName A string describing the document name in
       
  2256     the spooler.
       
  2257 
       
  2258     \value PPK_FontEmbedding A boolean value indicating whether data for
       
  2259     the document's fonts should be embedded in the data sent to the
       
  2260     printer.
       
  2261 
       
  2262     \value PPK_FullPage A boolean describing if the printer should be
       
  2263     full page or not.
       
  2264 
       
  2265     \value PPK_NumberOfCopies An integer specifying the number of
       
  2266     copies
       
  2267 
       
  2268     \value PPK_Orientation Specifies a QPrinter::Orientation value.
       
  2269 
       
  2270     \value PPK_OutputFileName The output file name as a string. An
       
  2271     empty file name indicates that the printer should not print to a file.
       
  2272 
       
  2273     \value PPK_PageOrder Specifies a QPrinter::PageOrder value.
       
  2274 
       
  2275     \value PPK_PageRect A QRect specifying the page rectangle
       
  2276 
       
  2277     \value PPK_PageSize Obsolete. Use PPK_PaperSize instead.
       
  2278 
       
  2279     \value PPK_PaperRect A QRect specifying the paper rectangle.
       
  2280 
       
  2281     \value PPK_PaperSource Specifies a QPrinter::PaperSource value.
       
  2282 
       
  2283     \value PPK_PaperSources Specifies more than one QPrinter::PaperSource value.
       
  2284 
       
  2285     \value PPK_PaperSize Specifies a QPrinter::PaperSize value.
       
  2286 
       
  2287     \value PPK_PrinterName A string specifying the name of the printer.
       
  2288 
       
  2289     \value PPK_PrinterProgram A string specifying the name of the
       
  2290     printer program used for printing,
       
  2291 
       
  2292     \value PPK_Resolution An integer describing the dots per inch for
       
  2293     this printer.
       
  2294 
       
  2295     \value PPK_SelectionOption
       
  2296 
       
  2297     \value PPK_SupportedResolutions A list of integer QVariants
       
  2298     describing the set of supported resolutions that the printer has.
       
  2299 
       
  2300     \value PPK_SuppressSystemPrintStatus Suppress the built-in dialog for showing
       
  2301     printing progress. As of 4.1 this only has effect on Mac OS X where, by default,
       
  2302     a status dialog is shown.
       
  2303 
       
  2304     \value PPK_WindowsPageSize An integer specifying a DM_PAPER entry
       
  2305     on Windows.
       
  2306 
       
  2307     \value PPK_CustomPaperSize A QSizeF specifying a custom paper size
       
  2308     in the QPrinter::Point unit.
       
  2309 
       
  2310     \value PPK_PageMargins A QList<QVariant> containing the left, top,
       
  2311     right and bottom margin values.
       
  2312 
       
  2313     \value PPK_CustomBase Basis for extension.
       
  2314 */
       
  2315 
       
  2316 /*!
       
  2317     \fn QPrintEngine::~QPrintEngine()
       
  2318 
       
  2319     Destroys the print engine.
       
  2320 */
       
  2321 
       
  2322 /*!
       
  2323     \fn void QPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &value)
       
  2324 
       
  2325     Sets the print engine's property specified by \a key to the given \a value.
       
  2326 
       
  2327     \sa property()
       
  2328 */
       
  2329 
       
  2330 /*!
       
  2331     \fn void QPrintEngine::property(PrintEnginePropertyKey key) const
       
  2332 
       
  2333     Returns the print engine's property specified by \a key.
       
  2334 
       
  2335     \sa setProperty()
       
  2336 */
       
  2337 
       
  2338 /*!
       
  2339     \fn bool QPrintEngine::newPage()
       
  2340 
       
  2341     Instructs the print engine to start a new page. Returns true if
       
  2342     the printer was able to create the new page; otherwise returns false.
       
  2343 */
       
  2344 
       
  2345 /*!
       
  2346     \fn bool QPrintEngine::abort()
       
  2347 
       
  2348     Instructs the print engine to abort the printing process. Returns
       
  2349     true if successful; otherwise returns false.
       
  2350 */
       
  2351 
       
  2352 /*!
       
  2353     \fn int QPrintEngine::metric(QPaintDevice::PaintDeviceMetric id) const
       
  2354 
       
  2355     Returns the metric for the given \a id.
       
  2356 */
       
  2357 
       
  2358 /*!
       
  2359     \fn QPrinter::PrinterState QPrintEngine::printerState() const
       
  2360 
       
  2361     Returns the current state of the printer being used by the print engine.
       
  2362 */
       
  2363 
       
  2364 /*!
       
  2365     \fn HDC QPrintEngine::getPrinterDC() const
       
  2366     \internal
       
  2367 */
       
  2368 
       
  2369 /*!
       
  2370     \fn void QPrintEngine::releasePrinterDC(HDC) const
       
  2371     \internal
       
  2372 */
       
  2373 
       
  2374 /*
       
  2375     Returns the dimensions for the given paper size, \a size, in millimeters.
       
  2376 */
       
  2377 QSizeF qt_paperSizeToQSizeF(QPrinter::PaperSize size)
       
  2378 {
       
  2379     if (size == QPrinter::Custom) return QSizeF(0, 0);
       
  2380     return QSizeF(qt_paperSizes[size][0], qt_paperSizes[size][1]);
       
  2381 }
       
  2382 
       
  2383 /*
       
  2384     Returns the PaperSize type that matches \a size, where \a size
       
  2385     is in millimeters.
       
  2386 
       
  2387     Because dimensions may not always be completely accurate (for
       
  2388     example when converting between units), a particular PaperSize
       
  2389     will be returned if it matches within -1/+1 millimeters.
       
  2390 */
       
  2391 QPrinter::PaperSize qSizeFTopaperSize(const QSizeF& size)
       
  2392 {
       
  2393     for (int i = 0; i < static_cast<int>(QPrinter::NPaperSize); ++i) {
       
  2394         if (qt_paperSizes[i][0] >= size.width() - 1 &&
       
  2395                 qt_paperSizes[i][0] <= size.width() + 1 &&
       
  2396                 qt_paperSizes[i][1] >= size.height() - 1 &&
       
  2397                 qt_paperSizes[i][1] <= size.height() + 1) {
       
  2398             return QPrinter::PaperSize(i);
       
  2399         }
       
  2400     }
       
  2401 
       
  2402     return QPrinter::Custom;
       
  2403 }
       
  2404 
       
  2405 QT_END_NAMESPACE
       
  2406 
       
  2407 #endif // QT_NO_PRINTER