| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtGui module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qprinter.h" |
| 41 | #include "qprinter_p.h" |
| 42 | |
| 43 | #ifndef QT_NO_PRINTER |
| 44 | |
| 45 | #include <qpa/qplatformprintplugin.h> |
| 46 | #include <qpa/qplatformprintersupport.h> |
| 47 | |
| 48 | #include "qprintengine.h" |
| 49 | #include "qlist.h" |
| 50 | #include <qcoreapplication.h> |
| 51 | #include <qfileinfo.h> |
| 52 | #include <QtGui/qrangecollection.h> |
| 53 | |
| 54 | #include <private/qpagedpaintdevice_p.h> |
| 55 | |
| 56 | #include "qprintengine_pdf_p.h" |
| 57 | |
| 58 | #include <qpicture.h> |
| 59 | #if QT_CONFIG(printpreviewwidget) |
| 60 | #include <private/qpaintengine_preview_p.h> |
| 61 | #endif |
| 62 | |
| 63 | QT_BEGIN_NAMESPACE |
| 64 | |
| 65 | #define ABORT_IF_ACTIVE(location) \ |
| 66 | if (d->printEngine->printerState() == QPrinter::Active) { \ |
| 67 | qWarning("%s: Cannot be changed while printer is active", location); \ |
| 68 | return; \ |
| 69 | } |
| 70 | |
| 71 | #define ABORT_IF_ACTIVE_RETURN(location, retValue) \ |
| 72 | if (d->printEngine->printerState() == QPrinter::Active) { \ |
| 73 | qWarning("%s: Cannot be changed while printer is active", location); \ |
| 74 | return retValue; \ |
| 75 | } |
| 76 | |
| 77 | extern qreal qt_pixelMultiplier(int resolution); |
| 78 | extern QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits); |
| 79 | |
| 80 | QPrinterInfo QPrinterPrivate::findValidPrinter(const QPrinterInfo &printer) |
| 81 | { |
| 82 | // Try find a valid printer to use, either the one given, the default or the first available |
| 83 | QPrinterInfo printerToUse = printer; |
| 84 | if (printerToUse.isNull()) { |
| 85 | printerToUse = QPrinterInfo::defaultPrinter(); |
| 86 | if (printerToUse.isNull()) { |
| 87 | QStringList availablePrinterNames = QPrinterInfo::availablePrinterNames(); |
| 88 | if (!availablePrinterNames.isEmpty()) |
| 89 | printerToUse = QPrinterInfo::printerInfo(availablePrinterNames.at(0)); |
| 90 | } |
| 91 | } |
| 92 | return printerToUse; |
| 93 | } |
| 94 | |
| 95 | void QPrinterPrivate::initEngines(QPrinter::OutputFormat format, const QPrinterInfo &printer) |
| 96 | { |
| 97 | // Default to PdfFormat |
| 98 | outputFormat = QPrinter::PdfFormat; |
| 99 | QPlatformPrinterSupport *ps = nullptr; |
| 100 | QString printerName; |
| 101 | |
| 102 | // Only set NativeFormat if we have a valid plugin and printer to use |
| 103 | if (format == QPrinter::NativeFormat) { |
| 104 | ps = QPlatformPrinterSupportPlugin::get(); |
| 105 | QPrinterInfo printerToUse = findValidPrinter(printer); |
| 106 | if (ps && !printerToUse.isNull()) { |
| 107 | outputFormat = QPrinter::NativeFormat; |
| 108 | printerName = printerToUse.printerName(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (outputFormat == QPrinter::NativeFormat) { |
| 113 | printEngine = ps->createNativePrintEngine(printerMode, printerName); |
| 114 | paintEngine = ps->createPaintEngine(printEngine, printerMode); |
| 115 | } else { |
| 116 | static const QHash<QPrinter::PdfVersion, QPdfEngine::PdfVersion> engineMapping { |
| 117 | {QPrinter::PdfVersion_1_4, QPdfEngine::Version_1_4}, |
| 118 | {QPrinter::PdfVersion_A1b, QPdfEngine::Version_A1b}, |
| 119 | {QPrinter::PdfVersion_1_6, QPdfEngine::Version_1_6} |
| 120 | }; |
| 121 | const auto pdfEngineVersion = engineMapping.value(pdfVersion, QPdfEngine::Version_1_4); |
| 122 | QPdfPrintEngine *pdfEngine = new QPdfPrintEngine(printerMode, pdfEngineVersion); |
| 123 | paintEngine = pdfEngine; |
| 124 | printEngine = pdfEngine; |
| 125 | } |
| 126 | |
| 127 | use_default_engine = true; |
| 128 | had_default_engines = true; |
| 129 | validPrinter = true; |
| 130 | } |
| 131 | |
| 132 | void QPrinterPrivate::changeEngines(QPrinter::OutputFormat format, const QPrinterInfo &printer) |
| 133 | { |
| 134 | QPrintEngine *oldPrintEngine = printEngine; |
| 135 | const bool def_engine = use_default_engine; |
| 136 | |
| 137 | initEngines(format, printer); |
| 138 | |
| 139 | if (oldPrintEngine) { |
| 140 | const auto properties = m_properties; // take a copy: setProperty() below modifies m_properties |
| 141 | for (const auto &key : properties) { |
| 142 | QVariant prop; |
| 143 | // PPK_NumberOfCopies need special treatmeant since it in most cases |
| 144 | // will return 1, disregarding the actual value that was set |
| 145 | // PPK_PrinterName also needs special treatment as initEngines has set it already |
| 146 | if (key == QPrintEngine::PPK_NumberOfCopies) |
| 147 | prop = QVariant(q_ptr->copyCount()); |
| 148 | else if (key != QPrintEngine::PPK_PrinterName) |
| 149 | prop = oldPrintEngine->property(key); |
| 150 | if (prop.isValid()) |
| 151 | setProperty(key, prop); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (def_engine) |
| 156 | delete oldPrintEngine; |
| 157 | } |
| 158 | |
| 159 | #if QT_CONFIG(printpreviewwidget) |
| 160 | QList<const QPicture *> QPrinterPrivate::previewPages() const |
| 161 | { |
| 162 | if (previewEngine) |
| 163 | return previewEngine->pages(); |
| 164 | return QList<const QPicture *>(); |
| 165 | } |
| 166 | |
| 167 | void QPrinterPrivate::setPreviewMode(bool enable) |
| 168 | { |
| 169 | Q_Q(QPrinter); |
| 170 | if (enable) { |
| 171 | if (!previewEngine) |
| 172 | previewEngine = new QPreviewPaintEngine; |
| 173 | had_default_engines = use_default_engine; |
| 174 | use_default_engine = false; |
| 175 | realPrintEngine = printEngine; |
| 176 | realPaintEngine = paintEngine; |
| 177 | q->setEngines(previewEngine, previewEngine); |
| 178 | previewEngine->setProxyEngines(realPrintEngine, realPaintEngine); |
| 179 | } else { |
| 180 | q->setEngines(realPrintEngine, realPaintEngine); |
| 181 | use_default_engine = had_default_engines; |
| 182 | } |
| 183 | } |
| 184 | #endif // QT_CONFIG(printpreviewwidget) |
| 185 | |
| 186 | void QPrinterPrivate::setProperty(QPrintEngine::PrintEnginePropertyKey key, const QVariant &value) |
| 187 | { |
| 188 | printEngine->setProperty(key, value); |
| 189 | m_properties.insert(key); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | class QPrinterPagedPaintDevicePrivate : public QPagedPaintDevicePrivate |
| 194 | { |
| 195 | public: |
| 196 | QPrinterPagedPaintDevicePrivate(QPrinter *p) |
| 197 | : QPagedPaintDevicePrivate(), m_printer(p) |
| 198 | {} |
| 199 | |
| 200 | virtual ~QPrinterPagedPaintDevicePrivate() |
| 201 | {} |
| 202 | |
| 203 | bool setPageLayout(const QPageLayout &newPageLayout) override |
| 204 | { |
| 205 | QPrinterPrivate *pd = QPrinterPrivate::get(m_printer); |
| 206 | |
| 207 | if (pd->paintEngine->type() != QPaintEngine::Pdf |
| 208 | && pd->printEngine->printerState() == QPrinter::Active) { |
| 209 | qWarning("QPrinter::setPageLayout: Cannot be changed while printer is active" ); |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | // Try to set the print engine page layout |
| 214 | pd->setProperty(QPrintEngine::PPK_QPageLayout, QVariant::fromValue(newPageLayout)); |
| 215 | |
| 216 | return pageLayout().isEquivalentTo(newPageLayout); |
| 217 | } |
| 218 | |
| 219 | bool setPageSize(const QPageSize &pageSize) override |
| 220 | { |
| 221 | QPrinterPrivate *pd = QPrinterPrivate::get(m_printer); |
| 222 | |
| 223 | if (pd->paintEngine->type() != QPaintEngine::Pdf |
| 224 | && pd->printEngine->printerState() == QPrinter::Active) { |
| 225 | qWarning("QPrinter::setPageLayout: Cannot be changed while printer is active" ); |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | // Try to set the print engine page size |
| 231 | pd->setProperty(QPrintEngine::PPK_QPageSize, QVariant::fromValue(pageSize)); |
| 232 | |
| 233 | return pageLayout().pageSize().isEquivalentTo(pageSize); |
| 234 | } |
| 235 | |
| 236 | bool setPageOrientation(QPageLayout::Orientation orientation) override |
| 237 | { |
| 238 | QPrinterPrivate *pd = QPrinterPrivate::get(m_printer); |
| 239 | |
| 240 | // Set the print engine value |
| 241 | pd->setProperty(QPrintEngine::PPK_Orientation, orientation); |
| 242 | |
| 243 | return pageLayout().orientation() == orientation; |
| 244 | } |
| 245 | |
| 246 | bool setPageMargins(const QMarginsF &margins, QPageLayout::Unit units) override |
| 247 | { |
| 248 | QPrinterPrivate *pd = QPrinterPrivate::get(m_printer); |
| 249 | |
| 250 | // Try to set print engine margins |
| 251 | QPair<QMarginsF, QPageLayout::Unit> pair = qMakePair(margins, units); |
| 252 | pd->setProperty(QPrintEngine::PPK_QPageMargins, QVariant::fromValue(pair)); |
| 253 | |
| 254 | return pageLayout().margins() == margins && pageLayout().units() == units; |
| 255 | } |
| 256 | |
| 257 | QPageLayout pageLayout() const override |
| 258 | { |
| 259 | QPrinterPrivate *pd = QPrinterPrivate::get(m_printer); |
| 260 | |
| 261 | return qvariant_cast<QPageLayout>(pd->printEngine->property(QPrintEngine::PPK_QPageLayout)); |
| 262 | } |
| 263 | |
| 264 | QPrinter *m_printer; |
| 265 | }; |
| 266 | |
| 267 | |
| 268 | /*! |
| 269 | \class QPrinter |
| 270 | \reentrant |
| 271 | |
| 272 | \brief The QPrinter class is a paint device that paints on a printer. |
| 273 | |
| 274 | \ingroup printing |
| 275 | \inmodule QtPrintSupport |
| 276 | |
| 277 | |
| 278 | This device represents a series of pages of printed output, and is |
| 279 | used in almost exactly the same way as other paint devices such as |
| 280 | QWidget and QPixmap. |
| 281 | A set of additional functions are provided to manage device-specific |
| 282 | features, such as orientation and resolution, and to step through |
| 283 | the pages in a document as it is generated. |
| 284 | |
| 285 | When printing directly to a printer on Windows or \macos, QPrinter uses |
| 286 | the built-in printer drivers. On X11, QPrinter uses the |
| 287 | \l{Common Unix Printing System (CUPS)} |
| 288 | to send PDF output to the printer. As an alternative, |
| 289 | the printProgram() function can be used to specify the command or utility |
| 290 | to use instead of the system default. |
| 291 | |
| 292 | Note that setting parameters like paper size and resolution on an |
| 293 | invalid printer is undefined. You can use QPrinter::isValid() to |
| 294 | verify this before changing any parameters. |
| 295 | |
| 296 | QPrinter supports a number of parameters, most of which can be |
| 297 | changed by the end user through a \l{QPrintDialog}{print dialog}. In |
| 298 | general, QPrinter passes these functions onto the underlying QPrintEngine. |
| 299 | |
| 300 | The most important parameters are: |
| 301 | \list |
| 302 | \li setPageLayout() tells QPrinter which page orientation to use, and |
| 303 | what size to expect from the printer. |
| 304 | \li setResolution() tells QPrinter what resolution you wish the |
| 305 | printer to provide, in dots per inch (DPI). |
| 306 | \li setFullPage() tells QPrinter whether you want to deal with the |
| 307 | full page or just with the part the printer can draw on. |
| 308 | \li setCopyCount() tells QPrinter how many copies of the document |
| 309 | it should print. |
| 310 | \endlist |
| 311 | |
| 312 | Many of these functions can only be called before the actual printing |
| 313 | begins (i.e., before QPainter::begin() is called). This usually makes |
| 314 | sense because, for example, it's not possible to change the number of |
| 315 | copies when you are halfway through printing. There are also some |
| 316 | settings that the user sets (through the printer dialog) and that |
| 317 | applications are expected to obey. See QAbstractPrintDialog's |
| 318 | documentation for more details. |
| 319 | |
| 320 | When QPainter::begin() is called, the QPrinter it operates on is prepared for |
| 321 | a new page, enabling the QPainter to be used immediately to paint the first |
| 322 | page in a document. Once the first page has been painted, newPage() can be |
| 323 | called to request a new blank page to paint on, or QPainter::end() can be |
| 324 | called to finish printing. The second page and all following pages are |
| 325 | prepared using a call to newPage() before they are painted. |
| 326 | |
| 327 | The first page in a document does not need to be preceded by a call to |
| 328 | newPage(). You only need to calling newPage() after QPainter::begin() if you |
| 329 | need to insert a blank page at the beginning of a printed document. |
| 330 | Similarly, calling newPage() after the last page in a document is painted will |
| 331 | result in a trailing blank page appended to the end of the printed document. |
| 332 | |
| 333 | If you want to abort the print job, abort() will try its best to |
| 334 | stop printing. It may cancel the entire job or just part of it. |
| 335 | |
| 336 | Since QPrinter can print to any QPrintEngine subclass, it is possible to |
| 337 | extend printing support to cover new types of printing subsystem by |
| 338 | subclassing QPrintEngine and reimplementing its interface. |
| 339 | |
| 340 | \sa QPrintDialog, {Qt Print Support} |
| 341 | */ |
| 342 | |
| 343 | /*! |
| 344 | \enum QPrinter::PrinterState |
| 345 | |
| 346 | \value Idle |
| 347 | \value Active |
| 348 | \value Aborted |
| 349 | \value Error |
| 350 | */ |
| 351 | |
| 352 | /*! |
| 353 | \enum QPrinter::PrinterMode |
| 354 | |
| 355 | This enum describes the mode the printer should work in. It |
| 356 | basically presets a certain resolution and working mode. |
| 357 | |
| 358 | \value ScreenResolution Sets the resolution of the print device to |
| 359 | the screen resolution. This has the big advantage that the results |
| 360 | obtained when painting on the printer will match more or less |
| 361 | exactly the visible output on the screen. It is the easiest to |
| 362 | use, as font metrics on the screen and on the printer are the |
| 363 | same. This is the default value. ScreenResolution will produce a |
| 364 | lower quality output than HighResolution and should only be used |
| 365 | for drafts. |
| 366 | |
| 367 | \value PrinterResolution This value is deprecated. It is |
| 368 | equivalent to ScreenResolution on Unix and HighResolution on |
| 369 | Windows and Mac. Due to the difference between ScreenResolution |
| 370 | and HighResolution, use of this value may lead to non-portable |
| 371 | printer code. |
| 372 | |
| 373 | \value HighResolution On Windows, sets the printer resolution to that |
| 374 | defined for the printer in use. For PDF printing, sets the |
| 375 | resolution of the PDF driver to 1200 dpi. |
| 376 | |
| 377 | \note When rendering text on a QPrinter device, it is important |
| 378 | to realize that the size of text, when specified in points, is |
| 379 | independent of the resolution specified for the device itself. |
| 380 | Therefore, it may be useful to specify the font size in pixels |
| 381 | when combining text with graphics to ensure that their relative |
| 382 | sizes are what you expect. |
| 383 | */ |
| 384 | |
| 385 | /*! |
| 386 | \enum QPrinter::PrintRange |
| 387 | |
| 388 | Used to specify the print range selection option. |
| 389 | |
| 390 | \value AllPages All pages should be printed. |
| 391 | \value Selection Only the selection should be printed. |
| 392 | \value PageRange The specified page range should be printed. |
| 393 | \value CurrentPage Only the current page should be printed. |
| 394 | |
| 395 | \sa setPrintRange(), printRange(), QAbstractPrintDialog::PrintRange |
| 396 | */ |
| 397 | |
| 398 | /*! |
| 399 | \enum QPrinter::PageOrder |
| 400 | |
| 401 | This enum type is used by QPrinter to tell the application program |
| 402 | how to print. |
| 403 | |
| 404 | \value FirstPageFirst the lowest-numbered page should be printed |
| 405 | first. |
| 406 | |
| 407 | \value LastPageFirst the highest-numbered page should be printed |
| 408 | first. |
| 409 | */ |
| 410 | |
| 411 | /*! |
| 412 | \enum QPrinter::ColorMode |
| 413 | |
| 414 | This enum type is used to indicate whether QPrinter should print |
| 415 | in color or not. |
| 416 | |
| 417 | \value Color print in color if available, otherwise in grayscale. |
| 418 | |
| 419 | \value GrayScale print in grayscale, even on color printers. |
| 420 | */ |
| 421 | |
| 422 | /*! |
| 423 | \enum QPrinter::PaperSource |
| 424 | |
| 425 | This enum type specifies what paper source QPrinter is to use. |
| 426 | QPrinter does not check that the paper source is available; it |
| 427 | just uses this information to try and set the paper source. |
| 428 | Whether it will set the paper source depends on whether the |
| 429 | printer has that particular source. |
| 430 | |
| 431 | \warning This is currently only implemented for Windows. |
| 432 | |
| 433 | \value Auto |
| 434 | \value Cassette |
| 435 | \value Envelope |
| 436 | \value EnvelopeManual |
| 437 | \value FormSource |
| 438 | \value LargeCapacity |
| 439 | \value LargeFormat |
| 440 | \value Lower |
| 441 | \value MaxPageSource Deprecated, use LastPaperSource instead |
| 442 | \value Middle |
| 443 | \value Manual |
| 444 | \value OnlyOne |
| 445 | \value Tractor |
| 446 | \value SmallFormat |
| 447 | \value Upper |
| 448 | \value CustomSource A PaperSource defined by the printer that is unknown to Qt |
| 449 | \value LastPaperSource The highest valid PaperSource value, currently CustomSource |
| 450 | */ |
| 451 | |
| 452 | /*! |
| 453 | \enum QPrinter::Unit |
| 454 | \since 4.4 |
| 455 | |
| 456 | This enum type is used to specify the measurement unit for page and |
| 457 | paper sizes. |
| 458 | |
| 459 | \value Millimeter |
| 460 | \value Point |
| 461 | \value Inch |
| 462 | \value Pica |
| 463 | \value Didot |
| 464 | \value Cicero |
| 465 | \value DevicePixel |
| 466 | |
| 467 | Note the difference between Point and DevicePixel. The Point unit is |
| 468 | defined to be 1/72th of an inch, while the DevicePixel unit is |
| 469 | resolution dependant and is based on the actual pixels, or dots, on |
| 470 | the printer. |
| 471 | */ |
| 472 | |
| 473 | /*! |
| 474 | Creates a new printer object with the given \a mode. |
| 475 | */ |
| 476 | QPrinter::QPrinter(PrinterMode mode) |
| 477 | : QPagedPaintDevice(new QPrinterPagedPaintDevicePrivate(this)), |
| 478 | d_ptr(new QPrinterPrivate(this)) |
| 479 | { |
| 480 | d_ptr->init(QPrinterInfo(), mode); |
| 481 | } |
| 482 | |
| 483 | /*! |
| 484 | \since 4.4 |
| 485 | |
| 486 | Creates a new printer object with the given \a printer and \a mode. |
| 487 | */ |
| 488 | QPrinter::QPrinter(const QPrinterInfo& printer, PrinterMode mode) |
| 489 | : QPagedPaintDevice(new QPrinterPagedPaintDevicePrivate(this)), |
| 490 | d_ptr(new QPrinterPrivate(this)) |
| 491 | { |
| 492 | d_ptr->init(printer, mode); |
| 493 | } |
| 494 | |
| 495 | void QPrinterPrivate::init(const QPrinterInfo &printer, QPrinter::PrinterMode mode) |
| 496 | { |
| 497 | if (Q_UNLIKELY(!QCoreApplication::instance())) { |
| 498 | qFatal("QPrinter: Must construct a QCoreApplication before a QPrinter" ); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | printerMode = mode; |
| 503 | |
| 504 | initEngines(QPrinter::NativeFormat, printer); |
| 505 | } |
| 506 | |
| 507 | /*! |
| 508 | This function is used by subclasses of QPrinter to specify custom |
| 509 | print and paint engines (\a printEngine and \a paintEngine, |
| 510 | respectively). |
| 511 | |
| 512 | QPrinter does not take ownership of the engines, so you need to |
| 513 | manage these engine instances yourself. |
| 514 | |
| 515 | Note that changing the engines will reset the printer state and |
| 516 | all its properties. |
| 517 | |
| 518 | \sa printEngine(), paintEngine(), setOutputFormat() |
| 519 | |
| 520 | \since 4.1 |
| 521 | */ |
| 522 | void QPrinter::setEngines(QPrintEngine *printEngine, QPaintEngine *paintEngine) |
| 523 | { |
| 524 | Q_D(QPrinter); |
| 525 | |
| 526 | if (d->use_default_engine) |
| 527 | delete d->printEngine; |
| 528 | |
| 529 | d->printEngine = printEngine; |
| 530 | d->paintEngine = paintEngine; |
| 531 | d->use_default_engine = false; |
| 532 | } |
| 533 | |
| 534 | /*! |
| 535 | Destroys the printer object and frees any allocated resources. If |
| 536 | the printer is destroyed while a print job is in progress this may |
| 537 | or may not affect the print job. |
| 538 | */ |
| 539 | QPrinter::~QPrinter() |
| 540 | { |
| 541 | Q_D(QPrinter); |
| 542 | if (d->use_default_engine) |
| 543 | delete d->printEngine; |
| 544 | #if QT_CONFIG(printpreviewwidget) |
| 545 | delete d->previewEngine; |
| 546 | #endif |
| 547 | } |
| 548 | |
| 549 | /*! |
| 550 | \enum QPrinter::OutputFormat |
| 551 | |
| 552 | The OutputFormat enum is used to describe the format QPrinter should |
| 553 | use for printing. |
| 554 | |
| 555 | \value NativeFormat QPrinter will print output using a method defined |
| 556 | by the platform it is running on. This mode is the default when printing |
| 557 | directly to a printer. |
| 558 | |
| 559 | \value PdfFormat QPrinter will generate its output as a searchable PDF file. |
| 560 | This mode is the default when printing to a file. |
| 561 | |
| 562 | \sa outputFormat(), setOutputFormat(), setOutputFileName() |
| 563 | */ |
| 564 | |
| 565 | /*! |
| 566 | \since 4.1 |
| 567 | |
| 568 | Sets the output format for this printer to \a format. |
| 569 | |
| 570 | If \a format is the same value as currently set then no change will be made. |
| 571 | |
| 572 | If \a format is NativeFormat then the printerName will be set to the default |
| 573 | printer. If there are no valid printers configured then no change will be made. |
| 574 | If you want to set NativeFormat with a specific printerName then use |
| 575 | setPrinterName(). |
| 576 | |
| 577 | \sa setPrinterName() |
| 578 | */ |
| 579 | void QPrinter::setOutputFormat(OutputFormat format) |
| 580 | { |
| 581 | Q_D(QPrinter); |
| 582 | |
| 583 | if (d->outputFormat == format) |
| 584 | return; |
| 585 | |
| 586 | if (format == QPrinter::NativeFormat) { |
| 587 | QPrinterInfo printerToUse = d->findValidPrinter(); |
| 588 | if (!printerToUse.isNull()) |
| 589 | d->changeEngines(format, printerToUse); |
| 590 | } else { |
| 591 | d->changeEngines(format, QPrinterInfo()); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /*! |
| 596 | \since 4.1 |
| 597 | |
| 598 | Returns the output format for this printer. |
| 599 | */ |
| 600 | QPrinter::OutputFormat QPrinter::outputFormat() const |
| 601 | { |
| 602 | Q_D(const QPrinter); |
| 603 | return d->outputFormat; |
| 604 | } |
| 605 | |
| 606 | /*! |
| 607 | \since 5.10 |
| 608 | |
| 609 | Sets the PDF version for this printer to \a version. |
| 610 | |
| 611 | If \a version is the same value as currently set then no change will be made. |
| 612 | */ |
| 613 | void QPrinter::setPdfVersion(PdfVersion version) |
| 614 | { |
| 615 | Q_D(QPrinter); |
| 616 | |
| 617 | if (d->pdfVersion == version) |
| 618 | return; |
| 619 | |
| 620 | d->pdfVersion = version; |
| 621 | |
| 622 | if (d->outputFormat == QPrinter::PdfFormat) { |
| 623 | d->changeEngines(d->outputFormat, QPrinterInfo()); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /*! |
| 628 | \since 5.10 |
| 629 | |
| 630 | Returns the PDF version for this printer. The default is \c PdfVersion_1_4. |
| 631 | */ |
| 632 | QPrinter::PdfVersion QPrinter::pdfVersion() const |
| 633 | { |
| 634 | Q_D(const QPrinter); |
| 635 | return d->pdfVersion; |
| 636 | } |
| 637 | |
| 638 | /*! \internal |
| 639 | */ |
| 640 | int QPrinter::devType() const |
| 641 | { |
| 642 | return QInternal::Printer; |
| 643 | } |
| 644 | |
| 645 | /*! |
| 646 | Returns the printer name. This value is initially set to the name |
| 647 | of the default printer. |
| 648 | |
| 649 | \sa setPrinterName() |
| 650 | */ |
| 651 | QString QPrinter::printerName() const |
| 652 | { |
| 653 | Q_D(const QPrinter); |
| 654 | return d->printEngine->property(QPrintEngine::PPK_PrinterName).toString(); |
| 655 | } |
| 656 | |
| 657 | /*! |
| 658 | Sets the printer name to \a name. |
| 659 | |
| 660 | If the \a name is empty then the output format will be set to PdfFormat. |
| 661 | |
| 662 | If the \a name is not a valid printer then no change will be made. |
| 663 | |
| 664 | If the \a name is a valid printer then the output format will be set to NativeFormat. |
| 665 | |
| 666 | \sa printerName(), isValid(), setOutputFormat() |
| 667 | */ |
| 668 | void QPrinter::setPrinterName(const QString &name) |
| 669 | { |
| 670 | Q_D(QPrinter); |
| 671 | ABORT_IF_ACTIVE("QPrinter::setPrinterName" ); |
| 672 | |
| 673 | if (printerName() == name) |
| 674 | return; |
| 675 | |
| 676 | if (name.isEmpty()) { |
| 677 | setOutputFormat(QPrinter::PdfFormat); |
| 678 | return; |
| 679 | } |
| 680 | |
| 681 | QPrinterInfo printerToUse = QPrinterInfo::printerInfo(name); |
| 682 | if (printerToUse.isNull()) |
| 683 | return; |
| 684 | |
| 685 | if (outputFormat() == QPrinter::PdfFormat) { |
| 686 | d->changeEngines(QPrinter::NativeFormat, printerToUse); |
| 687 | } else { |
| 688 | d->setProperty(QPrintEngine::PPK_PrinterName, name); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | /*! |
| 693 | \since 4.4 |
| 694 | |
| 695 | Returns \c true if the printer currently selected is a valid printer |
| 696 | in the system, or a pure PDF printer; otherwise returns \c false. |
| 697 | |
| 698 | To detect other failures check the output of QPainter::begin() or QPrinter::newPage(). |
| 699 | |
| 700 | \snippet printing-qprinter/errors.cpp 0 |
| 701 | |
| 702 | \sa setPrinterName() |
| 703 | */ |
| 704 | bool QPrinter::isValid() const |
| 705 | { |
| 706 | Q_D(const QPrinter); |
| 707 | if (!qApp) |
| 708 | return false; |
| 709 | return d->validPrinter; |
| 710 | } |
| 711 | |
| 712 | /*! |
| 713 | \fn QString QPrinter::outputFileName() const |
| 714 | |
| 715 | Returns the name of the output file. By default, this is an empty string |
| 716 | (indicating that the printer shouldn't print to file). |
| 717 | |
| 718 | \sa QPrintEngine::PrintEnginePropertyKey |
| 719 | |
| 720 | */ |
| 721 | |
| 722 | QString QPrinter::outputFileName() const |
| 723 | { |
| 724 | Q_D(const QPrinter); |
| 725 | return d->printEngine->property(QPrintEngine::PPK_OutputFileName).toString(); |
| 726 | } |
| 727 | |
| 728 | /*! |
| 729 | Sets the name of the output file to \a fileName. |
| 730 | |
| 731 | Setting a null or empty name (0 or "") disables printing to a file. |
| 732 | Setting a non-empty name enables printing to a file. |
| 733 | |
| 734 | This can change the value of outputFormat(). |
| 735 | If the file name has the ".pdf" suffix PDF is generated. If the file name |
| 736 | has a suffix other than ".pdf", the output format used is the |
| 737 | one set with setOutputFormat(). |
| 738 | |
| 739 | QPrinter uses Qt's cross-platform PDF print engines |
| 740 | respectively. If you can produce this format natively, for example |
| 741 | \macos can generate PDF's from its print engine, set the output format |
| 742 | back to NativeFormat. |
| 743 | |
| 744 | \sa outputFileName(), setOutputFormat() |
| 745 | */ |
| 746 | |
| 747 | void QPrinter::setOutputFileName(const QString &fileName) |
| 748 | { |
| 749 | Q_D(QPrinter); |
| 750 | ABORT_IF_ACTIVE("QPrinter::setOutputFileName" ); |
| 751 | |
| 752 | QFileInfo fi(fileName); |
| 753 | if (!fi.suffix().compare(QLatin1String("pdf" ), Qt::CaseInsensitive)) |
| 754 | setOutputFormat(QPrinter::PdfFormat); |
| 755 | else if (fileName.isEmpty()) |
| 756 | setOutputFormat(QPrinter::NativeFormat); |
| 757 | |
| 758 | d->setProperty(QPrintEngine::PPK_OutputFileName, fileName); |
| 759 | } |
| 760 | |
| 761 | |
| 762 | /*! |
| 763 | Returns the name of the program that sends the print output to the |
| 764 | printer. |
| 765 | |
| 766 | The default is to return an empty string; meaning that QPrinter will try to |
| 767 | be smart in a system-dependent way. On X11 only, you can set it to something |
| 768 | different to use a specific print program. On the other platforms, this |
| 769 | returns an empty string. |
| 770 | |
| 771 | \sa setPrintProgram(), setPrinterSelectionOption() |
| 772 | */ |
| 773 | QString QPrinter::printProgram() const |
| 774 | { |
| 775 | Q_D(const QPrinter); |
| 776 | return d->printEngine->property(QPrintEngine::PPK_PrinterProgram).toString(); |
| 777 | } |
| 778 | |
| 779 | |
| 780 | /*! |
| 781 | Sets the name of the program that should do the print job to \a |
| 782 | printProg. |
| 783 | |
| 784 | On X11, this function sets the program to call with the PDF |
| 785 | output. On other platforms, it has no effect. |
| 786 | |
| 787 | \sa printProgram() |
| 788 | */ |
| 789 | void QPrinter::setPrintProgram(const QString &printProg) |
| 790 | { |
| 791 | Q_D(QPrinter); |
| 792 | ABORT_IF_ACTIVE("QPrinter::setPrintProgram" ); |
| 793 | d->setProperty(QPrintEngine::PPK_PrinterProgram, printProg); |
| 794 | } |
| 795 | |
| 796 | |
| 797 | /*! |
| 798 | Returns the document name. |
| 799 | |
| 800 | \sa setDocName(), QPrintEngine::PrintEnginePropertyKey |
| 801 | */ |
| 802 | QString QPrinter::docName() const |
| 803 | { |
| 804 | Q_D(const QPrinter); |
| 805 | return d->printEngine->property(QPrintEngine::PPK_DocumentName).toString(); |
| 806 | } |
| 807 | |
| 808 | |
| 809 | /*! |
| 810 | Sets the document name to \a name. |
| 811 | |
| 812 | On X11, the document name is for example used as the default |
| 813 | output filename in QPrintDialog. Note that the document name does |
| 814 | not affect the file name if the printer is printing to a file. |
| 815 | Use the setOutputFile() function for this. |
| 816 | |
| 817 | \sa docName(), QPrintEngine::PrintEnginePropertyKey |
| 818 | */ |
| 819 | void QPrinter::setDocName(const QString &name) |
| 820 | { |
| 821 | Q_D(QPrinter); |
| 822 | ABORT_IF_ACTIVE("QPrinter::setDocName" ); |
| 823 | d->setProperty(QPrintEngine::PPK_DocumentName, name); |
| 824 | } |
| 825 | |
| 826 | |
| 827 | /*! |
| 828 | Returns the name of the application that created the document. |
| 829 | |
| 830 | \sa setCreator() |
| 831 | */ |
| 832 | QString QPrinter::creator() const |
| 833 | { |
| 834 | Q_D(const QPrinter); |
| 835 | return d->printEngine->property(QPrintEngine::PPK_Creator).toString(); |
| 836 | } |
| 837 | |
| 838 | |
| 839 | /*! |
| 840 | Sets the name of the application that created the document to \a |
| 841 | creator. |
| 842 | |
| 843 | This function is only applicable to the X11 version of Qt. If no |
| 844 | creator name is specified, the creator will be set to "Qt" |
| 845 | followed by some version number. |
| 846 | |
| 847 | \sa creator() |
| 848 | */ |
| 849 | void QPrinter::setCreator(const QString &creator) |
| 850 | { |
| 851 | Q_D(QPrinter); |
| 852 | ABORT_IF_ACTIVE("QPrinter::setCreator" ); |
| 853 | d->setProperty(QPrintEngine::PPK_Creator, creator); |
| 854 | } |
| 855 | |
| 856 | /*! |
| 857 | Sets the page order to \a pageOrder. |
| 858 | |
| 859 | The page order can be QPrinter::FirstPageFirst or |
| 860 | QPrinter::LastPageFirst. The application is responsible for |
| 861 | reading the page order and printing accordingly. |
| 862 | |
| 863 | This function is mostly useful for setting a default value that |
| 864 | the user can override in the print dialog. |
| 865 | |
| 866 | This function is only supported under X11. |
| 867 | */ |
| 868 | |
| 869 | void QPrinter::setPageOrder(PageOrder pageOrder) |
| 870 | { |
| 871 | d->pageOrderAscending = (pageOrder == FirstPageFirst); |
| 872 | |
| 873 | Q_D(QPrinter); |
| 874 | ABORT_IF_ACTIVE("QPrinter::setPageOrder" ); |
| 875 | d->setProperty(QPrintEngine::PPK_PageOrder, pageOrder); |
| 876 | } |
| 877 | |
| 878 | |
| 879 | /*! |
| 880 | Returns the current page order. |
| 881 | |
| 882 | The default page order is \c FirstPageFirst. |
| 883 | */ |
| 884 | |
| 885 | QPrinter::PageOrder QPrinter::pageOrder() const |
| 886 | { |
| 887 | Q_D(const QPrinter); |
| 888 | return QPrinter::PageOrder(d->printEngine->property(QPrintEngine::PPK_PageOrder).toInt()); |
| 889 | } |
| 890 | |
| 891 | |
| 892 | /*! |
| 893 | Sets the printer's color mode to \a newColorMode, which can be |
| 894 | either \c Color or \c GrayScale. |
| 895 | |
| 896 | \sa colorMode() |
| 897 | */ |
| 898 | |
| 899 | void QPrinter::setColorMode(ColorMode newColorMode) |
| 900 | { |
| 901 | Q_D(QPrinter); |
| 902 | ABORT_IF_ACTIVE("QPrinter::setColorMode" ); |
| 903 | d->setProperty(QPrintEngine::PPK_ColorMode, newColorMode); |
| 904 | } |
| 905 | |
| 906 | |
| 907 | /*! |
| 908 | Returns the current color mode. |
| 909 | |
| 910 | \sa setColorMode() |
| 911 | */ |
| 912 | QPrinter::ColorMode QPrinter::colorMode() const |
| 913 | { |
| 914 | Q_D(const QPrinter); |
| 915 | return QPrinter::ColorMode(d->printEngine->property(QPrintEngine::PPK_ColorMode).toInt()); |
| 916 | } |
| 917 | |
| 918 | /*! |
| 919 | \since 4.7 |
| 920 | |
| 921 | Sets the number of copies to be printed to \a count. |
| 922 | |
| 923 | The printer driver reads this setting and prints the specified number of |
| 924 | copies. |
| 925 | |
| 926 | \sa copyCount(), supportsMultipleCopies() |
| 927 | */ |
| 928 | |
| 929 | void QPrinter::setCopyCount(int count) |
| 930 | { |
| 931 | Q_D(QPrinter); |
| 932 | ABORT_IF_ACTIVE("QPrinter::setCopyCount;" ); |
| 933 | d->setProperty(QPrintEngine::PPK_CopyCount, count); |
| 934 | } |
| 935 | |
| 936 | /*! |
| 937 | \since 4.7 |
| 938 | |
| 939 | Returns the number of copies that will be printed. The default value is 1. |
| 940 | |
| 941 | \sa setCopyCount(), supportsMultipleCopies() |
| 942 | */ |
| 943 | |
| 944 | int QPrinter::copyCount() const |
| 945 | { |
| 946 | Q_D(const QPrinter); |
| 947 | return d->printEngine->property(QPrintEngine::PPK_CopyCount).toInt(); |
| 948 | } |
| 949 | |
| 950 | /*! |
| 951 | \since 4.7 |
| 952 | |
| 953 | Returns \c true if the printer supports printing multiple copies of the same |
| 954 | document in one job; otherwise false is returned. |
| 955 | |
| 956 | On most systems this function will return true. However, on X11 systems |
| 957 | that do not support CUPS, this function will return false. That means the |
| 958 | application has to handle the number of copies by printing the same |
| 959 | document the required number of times. |
| 960 | |
| 961 | \sa setCopyCount(), copyCount() |
| 962 | */ |
| 963 | |
| 964 | bool QPrinter::supportsMultipleCopies() const |
| 965 | { |
| 966 | Q_D(const QPrinter); |
| 967 | return d->printEngine->property(QPrintEngine::PPK_SupportsMultipleCopies).toBool(); |
| 968 | } |
| 969 | |
| 970 | /*! |
| 971 | \since 4.1 |
| 972 | |
| 973 | Returns \c true if collation is turned on when multiple copies is selected. |
| 974 | Returns \c false if it is turned off when multiple copies is selected. |
| 975 | When collating is turned off the printing of each individual page will be repeated |
| 976 | the numCopies() amount before the next page is started. With collating turned on |
| 977 | all pages are printed before the next copy of those pages is started. |
| 978 | |
| 979 | \sa setCollateCopies() |
| 980 | */ |
| 981 | bool QPrinter::collateCopies() const |
| 982 | { |
| 983 | Q_D(const QPrinter); |
| 984 | return d->printEngine->property(QPrintEngine::PPK_CollateCopies).toBool(); |
| 985 | } |
| 986 | |
| 987 | |
| 988 | /*! |
| 989 | \since 4.1 |
| 990 | |
| 991 | Sets the default value for collation checkbox when the print |
| 992 | dialog appears. If \a collate is true, it will enable |
| 993 | setCollateCopiesEnabled(). The default value is false. This value |
| 994 | will be changed by what the user presses in the print dialog. |
| 995 | |
| 996 | \sa collateCopies() |
| 997 | */ |
| 998 | void QPrinter::setCollateCopies(bool collate) |
| 999 | { |
| 1000 | Q_D(QPrinter); |
| 1001 | ABORT_IF_ACTIVE("QPrinter::setCollateCopies" ); |
| 1002 | d->setProperty(QPrintEngine::PPK_CollateCopies, collate); |
| 1003 | } |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | /*! |
| 1008 | If \a fp is true, enables support for painting over the entire page; |
| 1009 | otherwise restricts painting to the printable area reported by the |
| 1010 | device. |
| 1011 | |
| 1012 | By default, full page printing is disabled. In this case, the origin |
| 1013 | of the QPrinter's coordinate system coincides with the top-left |
| 1014 | corner of the printable area. |
| 1015 | |
| 1016 | If full page printing is enabled, the origin of the QPrinter's |
| 1017 | coordinate system coincides with the top-left corner of the paper |
| 1018 | itself. In this case, the |
| 1019 | \l{QPaintDevice::PaintDeviceMetric}{device metrics} will report |
| 1020 | the exact same dimensions as indicated by \{QPageSize}. It may not |
| 1021 | be possible to print on the entire physical page because of the |
| 1022 | printer's margins, so the application must account for the margins |
| 1023 | itself. |
| 1024 | |
| 1025 | \sa fullPage(), QPagedPaintDevice::pageLayout(), QPagedPaintDevice::setPageSize() |
| 1026 | */ |
| 1027 | |
| 1028 | void QPrinter::setFullPage(bool fp) |
| 1029 | { |
| 1030 | Q_D(QPrinter); |
| 1031 | // Set the print engine |
| 1032 | d->setProperty(QPrintEngine::PPK_FullPage, fp); |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | /*! |
| 1037 | Returns \c true if the origin of the printer's coordinate system is |
| 1038 | at the corner of the page and false if it is at the edge of the |
| 1039 | printable area. |
| 1040 | |
| 1041 | See setFullPage() for details and caveats. |
| 1042 | |
| 1043 | \sa setFullPage(), QPagedPaintDevice::pageLayout() |
| 1044 | */ |
| 1045 | |
| 1046 | bool QPrinter::fullPage() const |
| 1047 | { |
| 1048 | Q_D(const QPrinter); |
| 1049 | return d->printEngine->property(QPrintEngine::PPK_FullPage).toBool(); |
| 1050 | } |
| 1051 | |
| 1052 | |
| 1053 | /*! |
| 1054 | Requests that the printer prints at \a dpi or as near to \a dpi as |
| 1055 | possible. |
| 1056 | |
| 1057 | This setting affects the coordinate system as returned by, for |
| 1058 | example QPainter::viewport(). |
| 1059 | |
| 1060 | This function must be called before QPainter::begin() to have an effect on |
| 1061 | all platforms. |
| 1062 | |
| 1063 | \sa resolution(), QPagedPaintDevice::setPageSize() |
| 1064 | */ |
| 1065 | |
| 1066 | void QPrinter::setResolution(int dpi) |
| 1067 | { |
| 1068 | Q_D(QPrinter); |
| 1069 | ABORT_IF_ACTIVE("QPrinter::setResolution" ); |
| 1070 | d->setProperty(QPrintEngine::PPK_Resolution, dpi); |
| 1071 | } |
| 1072 | |
| 1073 | |
| 1074 | /*! |
| 1075 | Returns the current assumed resolution of the printer, as set by |
| 1076 | setResolution() or by the printer driver. |
| 1077 | |
| 1078 | \sa setResolution() |
| 1079 | */ |
| 1080 | |
| 1081 | int QPrinter::resolution() const |
| 1082 | { |
| 1083 | Q_D(const QPrinter); |
| 1084 | return d->printEngine->property(QPrintEngine::PPK_Resolution).toInt(); |
| 1085 | } |
| 1086 | |
| 1087 | /*! |
| 1088 | Sets the paper source setting to \a source. |
| 1089 | |
| 1090 | Windows only: This option can be changed while printing and will |
| 1091 | take effect from the next call to newPage() |
| 1092 | |
| 1093 | \sa paperSource() |
| 1094 | */ |
| 1095 | |
| 1096 | void QPrinter::setPaperSource(PaperSource source) |
| 1097 | { |
| 1098 | Q_D(QPrinter); |
| 1099 | d->setProperty(QPrintEngine::PPK_PaperSource, source); |
| 1100 | } |
| 1101 | |
| 1102 | /*! |
| 1103 | Returns the printer's paper source. This is \c Manual or a printer |
| 1104 | tray or paper cassette. |
| 1105 | */ |
| 1106 | QPrinter::PaperSource QPrinter::paperSource() const |
| 1107 | { |
| 1108 | Q_D(const QPrinter); |
| 1109 | return QPrinter::PaperSource(d->printEngine->property(QPrintEngine::PPK_PaperSource).toInt()); |
| 1110 | } |
| 1111 | |
| 1112 | |
| 1113 | /*! |
| 1114 | \since 4.1 |
| 1115 | |
| 1116 | Enabled or disables font embedding depending on \a enable. |
| 1117 | |
| 1118 | \sa fontEmbeddingEnabled() |
| 1119 | */ |
| 1120 | void QPrinter::setFontEmbeddingEnabled(bool enable) |
| 1121 | { |
| 1122 | Q_D(QPrinter); |
| 1123 | d->setProperty(QPrintEngine::PPK_FontEmbedding, enable); |
| 1124 | } |
| 1125 | |
| 1126 | /*! |
| 1127 | \since 4.1 |
| 1128 | |
| 1129 | Returns \c true if font embedding is enabled. |
| 1130 | |
| 1131 | \sa setFontEmbeddingEnabled() |
| 1132 | */ |
| 1133 | bool QPrinter::fontEmbeddingEnabled() const |
| 1134 | { |
| 1135 | Q_D(const QPrinter); |
| 1136 | return d->printEngine->property(QPrintEngine::PPK_FontEmbedding).toBool(); |
| 1137 | } |
| 1138 | |
| 1139 | /*! |
| 1140 | \enum QPrinter::DuplexMode |
| 1141 | \since 4.4 |
| 1142 | |
| 1143 | This enum is used to indicate whether printing will occur on one or both sides |
| 1144 | of each sheet of paper (simplex or duplex printing). |
| 1145 | |
| 1146 | \value DuplexNone Single sided (simplex) printing only. |
| 1147 | \value DuplexAuto The printer's default setting is used to determine whether |
| 1148 | duplex printing is used. |
| 1149 | \value DuplexLongSide Both sides of each sheet of paper are used for printing. |
| 1150 | The paper is turned over its longest edge before the second |
| 1151 | side is printed |
| 1152 | \value DuplexShortSide Both sides of each sheet of paper are used for printing. |
| 1153 | The paper is turned over its shortest edge before the second |
| 1154 | side is printed |
| 1155 | */ |
| 1156 | |
| 1157 | /*! |
| 1158 | \since 4.4 |
| 1159 | |
| 1160 | Enables double sided printing based on the \a duplex mode. |
| 1161 | |
| 1162 | \sa duplex() |
| 1163 | */ |
| 1164 | void QPrinter::setDuplex(DuplexMode duplex) |
| 1165 | { |
| 1166 | Q_D(QPrinter); |
| 1167 | d->setProperty(QPrintEngine::PPK_Duplex, duplex); |
| 1168 | } |
| 1169 | |
| 1170 | /*! |
| 1171 | \since 4.4 |
| 1172 | |
| 1173 | Returns the current duplex mode. |
| 1174 | |
| 1175 | \sa setDuplex() |
| 1176 | */ |
| 1177 | QPrinter::DuplexMode QPrinter::duplex() const |
| 1178 | { |
| 1179 | Q_D(const QPrinter); |
| 1180 | return static_cast <DuplexMode> (d->printEngine->property(QPrintEngine::PPK_Duplex).toInt()); |
| 1181 | } |
| 1182 | |
| 1183 | /*! |
| 1184 | \since 4.4 |
| 1185 | |
| 1186 | Returns the page's rectangle in \a unit; this is usually smaller |
| 1187 | than the paperRect() since the page normally has margins between |
| 1188 | its borders and the paper. |
| 1189 | |
| 1190 | \sa QPagedPaintDevice::pageLayout() |
| 1191 | */ |
| 1192 | QRectF QPrinter::(Unit unit) const |
| 1193 | { |
| 1194 | if (unit == QPrinter::DevicePixel) |
| 1195 | return pageLayout().paintRectPixels(resolution()); |
| 1196 | else |
| 1197 | return pageLayout().paintRect(QPageLayout::Unit(unit)); |
| 1198 | } |
| 1199 | |
| 1200 | |
| 1201 | /*! |
| 1202 | \since 4.4 |
| 1203 | |
| 1204 | Returns the paper's rectangle in \a unit; this is usually larger |
| 1205 | than the pageRect(). |
| 1206 | |
| 1207 | \sa pageRect() |
| 1208 | */ |
| 1209 | QRectF QPrinter::paperRect(Unit unit) const |
| 1210 | { |
| 1211 | if (unit == QPrinter::DevicePixel) |
| 1212 | return pageLayout().fullRectPixels(resolution()); |
| 1213 | else |
| 1214 | return pageLayout().fullRect(QPageLayout::Unit(unit)); |
| 1215 | } |
| 1216 | |
| 1217 | /*! |
| 1218 | \internal |
| 1219 | |
| 1220 | Returns the metric for the given \a id. |
| 1221 | */ |
| 1222 | int QPrinter::metric(PaintDeviceMetric id) const |
| 1223 | { |
| 1224 | Q_D(const QPrinter); |
| 1225 | return d->printEngine->metric(id); |
| 1226 | } |
| 1227 | |
| 1228 | /*! |
| 1229 | Returns the paint engine used by the printer. |
| 1230 | */ |
| 1231 | QPaintEngine *QPrinter::paintEngine() const |
| 1232 | { |
| 1233 | Q_D(const QPrinter); |
| 1234 | return d->paintEngine; |
| 1235 | } |
| 1236 | |
| 1237 | /*! |
| 1238 | \since 4.1 |
| 1239 | |
| 1240 | Returns the print engine used by the printer. |
| 1241 | */ |
| 1242 | QPrintEngine *QPrinter::printEngine() const |
| 1243 | { |
| 1244 | Q_D(const QPrinter); |
| 1245 | return d->printEngine; |
| 1246 | } |
| 1247 | |
| 1248 | /*! |
| 1249 | Returns a list of the resolutions (a list of dots-per-inch |
| 1250 | integers) that the printer says it supports. |
| 1251 | |
| 1252 | For X11 where all printing is directly to PDF, this |
| 1253 | function will always return a one item list containing only the |
| 1254 | PDF resolution, i.e., 72 (72 dpi -- but see PrinterMode). |
| 1255 | */ |
| 1256 | QList<int> QPrinter::supportedResolutions() const |
| 1257 | { |
| 1258 | Q_D(const QPrinter); |
| 1259 | const QList<QVariant> varlist |
| 1260 | = d->printEngine->property(QPrintEngine::PPK_SupportedResolutions).toList(); |
| 1261 | QList<int> intlist; |
| 1262 | intlist.reserve(varlist.size()); |
| 1263 | for (const auto &var : varlist) |
| 1264 | intlist << var.toInt(); |
| 1265 | return intlist; |
| 1266 | } |
| 1267 | |
| 1268 | /*! |
| 1269 | Tells the printer to eject the current page and to continue |
| 1270 | printing on a new page. Returns \c true if this was successful; |
| 1271 | otherwise returns \c false. |
| 1272 | |
| 1273 | Calling newPage() on an inactive QPrinter object will always |
| 1274 | fail. |
| 1275 | */ |
| 1276 | bool QPrinter::newPage() |
| 1277 | { |
| 1278 | Q_D(QPrinter); |
| 1279 | if (d->printEngine->printerState() != QPrinter::Active) |
| 1280 | return false; |
| 1281 | return d->printEngine->newPage(); |
| 1282 | } |
| 1283 | |
| 1284 | /*! |
| 1285 | Aborts the current print run. Returns \c true if the print run was |
| 1286 | successfully aborted and printerState() will return QPrinter::Aborted; otherwise |
| 1287 | returns \c false. |
| 1288 | |
| 1289 | It is not always possible to abort a print job. For example, |
| 1290 | all the data has gone to the printer but the printer cannot or |
| 1291 | will not cancel the job when asked to. |
| 1292 | */ |
| 1293 | bool QPrinter::abort() |
| 1294 | { |
| 1295 | Q_D(QPrinter); |
| 1296 | return d->printEngine->abort(); |
| 1297 | } |
| 1298 | |
| 1299 | /*! |
| 1300 | Returns the current state of the printer. This may not always be |
| 1301 | accurate (for example if the printer doesn't have the capability |
| 1302 | of reporting its state to the operating system). |
| 1303 | */ |
| 1304 | QPrinter::PrinterState QPrinter::printerState() const |
| 1305 | { |
| 1306 | Q_D(const QPrinter); |
| 1307 | return d->printEngine->printerState(); |
| 1308 | } |
| 1309 | |
| 1310 | #if defined(Q_OS_WIN) || defined(Q_CLANG_QDOC) |
| 1311 | /*! |
| 1312 | Returns the supported paper sizes for this printer. |
| 1313 | |
| 1314 | The values will be either a value that matches an entry in the |
| 1315 | QPrinter::PaperSource enum or a driver spesific value. The driver |
| 1316 | spesific values are greater than the constant DMBIN_USER declared |
| 1317 | in wingdi.h. |
| 1318 | |
| 1319 | \warning This function is only available in windows. |
| 1320 | */ |
| 1321 | |
| 1322 | QList<QPrinter::PaperSource> QPrinter::supportedPaperSources() const |
| 1323 | { |
| 1324 | Q_D(const QPrinter); |
| 1325 | QVariant v = d->printEngine->property(QPrintEngine::PPK_PaperSources); |
| 1326 | |
| 1327 | const QList<QVariant> variant_list = v.toList(); |
| 1328 | QList<QPrinter::PaperSource> int_list; |
| 1329 | int_list.reserve(variant_list.size()); |
| 1330 | for (const auto &variant : variant_list) |
| 1331 | int_list << QPrinter::PaperSource(variant.toInt()); |
| 1332 | |
| 1333 | return int_list; |
| 1334 | } |
| 1335 | |
| 1336 | #endif // Q_OS_WIN |
| 1337 | |
| 1338 | /*! |
| 1339 | \fn QString QPrinter::printerSelectionOption() const |
| 1340 | |
| 1341 | Returns the printer options selection string. This is useful only |
| 1342 | if the print command has been explicitly set. |
| 1343 | |
| 1344 | The default value (an empty string) implies that the printer should |
| 1345 | be selected in a system-dependent manner. |
| 1346 | |
| 1347 | Any other value implies that the given value should be used. |
| 1348 | |
| 1349 | This function always returns an empty string on Windows and Mac. |
| 1350 | |
| 1351 | \sa setPrinterSelectionOption(), setPrintProgram() |
| 1352 | */ |
| 1353 | |
| 1354 | QString QPrinter::printerSelectionOption() const |
| 1355 | { |
| 1356 | Q_D(const QPrinter); |
| 1357 | return d->printEngine->property(QPrintEngine::PPK_SelectionOption).toString(); |
| 1358 | } |
| 1359 | |
| 1360 | /*! |
| 1361 | \fn void QPrinter::setPrinterSelectionOption(const QString &option) |
| 1362 | |
| 1363 | Sets the printer to use \a option to select the printer. \a option |
| 1364 | is null by default (which implies that Qt should be smart enough |
| 1365 | to guess correctly), but it can be set to other values to use a |
| 1366 | specific printer selection option. |
| 1367 | |
| 1368 | If the printer selection option is changed while the printer is |
| 1369 | active, the current print job may or may not be affected. |
| 1370 | |
| 1371 | This function has no effect on Windows or Mac. |
| 1372 | |
| 1373 | \sa printerSelectionOption(), setPrintProgram() |
| 1374 | */ |
| 1375 | |
| 1376 | void QPrinter::setPrinterSelectionOption(const QString &option) |
| 1377 | { |
| 1378 | Q_D(QPrinter); |
| 1379 | d->setProperty(QPrintEngine::PPK_SelectionOption, option); |
| 1380 | } |
| 1381 | |
| 1382 | /*! |
| 1383 | \since 4.1 |
| 1384 | \fn int QPrinter::fromPage() const |
| 1385 | |
| 1386 | Returns the number of the first page in a range of pages to be printed |
| 1387 | (the "from page" setting). Pages in a document are numbered according to |
| 1388 | the convention that the first page is page 1. |
| 1389 | |
| 1390 | By default, this function returns a special value of 0, meaning that |
| 1391 | the "from page" setting is unset. |
| 1392 | |
| 1393 | \note If fromPage() and toPage() both return 0, this indicates that |
| 1394 | \e{the whole document will be printed}. |
| 1395 | |
| 1396 | \sa setFromTo(), toPage(), rangeCollection() |
| 1397 | */ |
| 1398 | |
| 1399 | int QPrinter::fromPage() const |
| 1400 | { |
| 1401 | return d->rangeCollection->firstPage(); |
| 1402 | } |
| 1403 | |
| 1404 | /*! |
| 1405 | \since 4.1 |
| 1406 | |
| 1407 | Returns the number of the last page in a range of pages to be printed |
| 1408 | (the "to page" setting). Pages in a document are numbered according to |
| 1409 | the convention that the first page is page 1. |
| 1410 | |
| 1411 | By default, this function returns a special value of 0, meaning that |
| 1412 | the "to page" setting is unset. |
| 1413 | |
| 1414 | \note If fromPage() and toPage() both return 0, this indicates that |
| 1415 | \e{the whole document will be printed}. |
| 1416 | |
| 1417 | The programmer is responsible for reading this setting and |
| 1418 | printing accordingly. |
| 1419 | |
| 1420 | \sa setFromTo(), fromPage(), rangeCollection() |
| 1421 | */ |
| 1422 | |
| 1423 | int QPrinter::toPage() const |
| 1424 | { |
| 1425 | return d->rangeCollection->lastPage(); |
| 1426 | } |
| 1427 | |
| 1428 | /*! |
| 1429 | \since 4.1 |
| 1430 | |
| 1431 | Sets the range of pages to be printed to cover the pages with numbers |
| 1432 | specified by \a from and \a to, where \a from corresponds to the first |
| 1433 | page in the range and \a to corresponds to the last. |
| 1434 | |
| 1435 | \note Pages in a document are numbered according to the convention that |
| 1436 | the first page is page 1. However, if \a from and \a to are both set to 0, |
| 1437 | the \e{whole document will be printed}. |
| 1438 | |
| 1439 | This function is mostly used to set a default value that the user can |
| 1440 | override in the print dialog when you call setup(). |
| 1441 | |
| 1442 | \sa fromPage(), toPage(), rangeCollection() |
| 1443 | */ |
| 1444 | |
| 1445 | void QPrinter::setFromTo(int from, int to) |
| 1446 | { |
| 1447 | d->rangeCollection->clear(); |
| 1448 | d->rangeCollection->addRange(from, to); |
| 1449 | } |
| 1450 | |
| 1451 | /*! |
| 1452 | \since 6.0 |
| 1453 | |
| 1454 | Returns the range collection associated with this device. |
| 1455 | |
| 1456 | \sa QRangeCollection, fromPage(), toPage() |
| 1457 | */ |
| 1458 | QRangeCollection *QPrinter::rangeCollection() |
| 1459 | { |
| 1460 | return d->rangeCollection; |
| 1461 | } |
| 1462 | |
| 1463 | /*! |
| 1464 | \since 4.1 |
| 1465 | |
| 1466 | Sets the print range option in to be \a range. |
| 1467 | */ |
| 1468 | void QPrinter::setPrintRange( PrintRange range ) |
| 1469 | { |
| 1470 | d->printSelectionOnly = (range == Selection); |
| 1471 | |
| 1472 | Q_D(QPrinter); |
| 1473 | d->printRange = range; |
| 1474 | } |
| 1475 | |
| 1476 | /*! |
| 1477 | \since 4.1 |
| 1478 | |
| 1479 | Returns the page range of the QPrinter. After the print setup |
| 1480 | dialog has been opened, this function returns the value selected |
| 1481 | by the user. |
| 1482 | |
| 1483 | \sa setPrintRange() |
| 1484 | */ |
| 1485 | QPrinter::PrintRange QPrinter::printRange() const |
| 1486 | { |
| 1487 | Q_D(const QPrinter); |
| 1488 | return d->printRange; |
| 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | /*! |
| 1493 | \class QPrintEngine |
| 1494 | \reentrant |
| 1495 | |
| 1496 | \ingroup printing |
| 1497 | \inmodule QtPrintSupport |
| 1498 | |
| 1499 | \brief The QPrintEngine class defines an interface for how QPrinter |
| 1500 | interacts with a given printing subsystem. |
| 1501 | |
| 1502 | The common case when creating your own print engine is to derive from both |
| 1503 | QPaintEngine and QPrintEngine. Various properties of a print engine are |
| 1504 | given with property() and set with setProperty(). |
| 1505 | |
| 1506 | \sa QPaintEngine |
| 1507 | */ |
| 1508 | |
| 1509 | /*! |
| 1510 | \enum QPrintEngine::PrintEnginePropertyKey |
| 1511 | |
| 1512 | This enum is used to communicate properties between the print |
| 1513 | engine and QPrinter. A property may or may not be supported by a |
| 1514 | given print engine. |
| 1515 | |
| 1516 | \value PPK_CollateCopies A boolean value indicating whether the |
| 1517 | printout should be collated or not. |
| 1518 | |
| 1519 | \value PPK_ColorMode Refers to QPrinter::ColorMode, either color or |
| 1520 | monochrome. |
| 1521 | |
| 1522 | \value PPK_Creator A string describing the document's creator. |
| 1523 | |
| 1524 | \value PPK_Duplex A boolean value indicating whether both sides of |
| 1525 | the printer paper should be used for the printout. |
| 1526 | |
| 1527 | \value PPK_DocumentName A string describing the document name in |
| 1528 | the spooler. |
| 1529 | |
| 1530 | \value PPK_FontEmbedding A boolean value indicating whether data for |
| 1531 | the document's fonts should be embedded in the data sent to the |
| 1532 | printer. |
| 1533 | |
| 1534 | \value PPK_FullPage A boolean describing if the printer should be |
| 1535 | full page or not. |
| 1536 | |
| 1537 | \value PPK_NumberOfCopies Obsolete. An integer specifying the number of |
| 1538 | copies. Use PPK_CopyCount instead. |
| 1539 | |
| 1540 | \value PPK_Orientation Specifies a QPageLayout::Orientation value. |
| 1541 | |
| 1542 | \value PPK_OutputFileName The output file name as a string. An |
| 1543 | empty file name indicates that the printer should not print to a file. |
| 1544 | |
| 1545 | \value PPK_PageOrder Specifies a QPrinter::PageOrder value. |
| 1546 | |
| 1547 | \value PPK_PageRect A QRect specifying the page rectangle |
| 1548 | |
| 1549 | \value PPK_PageSize Obsolete. Use PPK_PaperSize instead. |
| 1550 | |
| 1551 | \value PPK_PaperRect A QRect specifying the paper rectangle. |
| 1552 | |
| 1553 | \value PPK_PaperSource Specifies a QPrinter::PaperSource value. |
| 1554 | |
| 1555 | \value PPK_PaperSources Specifies more than one QPrinter::PaperSource value. |
| 1556 | |
| 1557 | \value PPK_PaperName A string specifying the name of the paper. |
| 1558 | |
| 1559 | \value PPK_PaperSize Specifies a QPrinter::PaperSize value. |
| 1560 | |
| 1561 | \value PPK_PrinterName A string specifying the name of the printer. |
| 1562 | |
| 1563 | \value PPK_PrinterProgram A string specifying the name of the |
| 1564 | printer program used for printing, |
| 1565 | |
| 1566 | \value PPK_Resolution An integer describing the dots per inch for |
| 1567 | this printer. |
| 1568 | |
| 1569 | \value PPK_SelectionOption |
| 1570 | |
| 1571 | \value PPK_SupportedResolutions A list of integer QVariants |
| 1572 | describing the set of supported resolutions that the printer has. |
| 1573 | |
| 1574 | \value PPK_WindowsPageSize An integer specifying a DM_PAPER entry |
| 1575 | on Windows. |
| 1576 | |
| 1577 | \value PPK_CustomPaperSize A QSizeF specifying a custom paper size |
| 1578 | in the QPrinter::Point unit. |
| 1579 | |
| 1580 | \value PPK_PageMargins A QList<QVariant> containing the left, top, |
| 1581 | right and bottom margin values in the QPrinter::Point unit. |
| 1582 | |
| 1583 | \value PPK_CopyCount An integer specifying the number of copies to print. |
| 1584 | |
| 1585 | \value PPK_SupportsMultipleCopies A boolean value indicating whether or not |
| 1586 | the printer supports printing multiple copies in one job. |
| 1587 | |
| 1588 | \value PPK_QPageSize Set the page size using a QPageSize object. |
| 1589 | |
| 1590 | \value PPK_QPageMargins Set the page margins using a QPair of QMarginsF and QPageLayout::Unit. |
| 1591 | |
| 1592 | \value PPK_QPageLayout Set the page layout using a QPageLayout object. |
| 1593 | |
| 1594 | \value PPK_CustomBase Basis for extension. |
| 1595 | */ |
| 1596 | |
| 1597 | /*! |
| 1598 | \fn QPrintEngine::~QPrintEngine() |
| 1599 | |
| 1600 | Destroys the print engine. |
| 1601 | */ |
| 1602 | |
| 1603 | /*! |
| 1604 | \fn void QPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &value) |
| 1605 | |
| 1606 | Sets the print engine's property specified by \a key to the given \a value. |
| 1607 | |
| 1608 | \sa property() |
| 1609 | */ |
| 1610 | |
| 1611 | /*! |
| 1612 | \fn void QPrintEngine::property(PrintEnginePropertyKey key) const |
| 1613 | |
| 1614 | Returns the print engine's property specified by \a key. |
| 1615 | |
| 1616 | \sa setProperty() |
| 1617 | */ |
| 1618 | |
| 1619 | /*! |
| 1620 | \fn bool QPrintEngine::newPage() |
| 1621 | |
| 1622 | Instructs the print engine to start a new page. Returns \c true if |
| 1623 | the printer was able to create the new page; otherwise returns \c false. |
| 1624 | */ |
| 1625 | |
| 1626 | /*! |
| 1627 | \fn bool QPrintEngine::abort() |
| 1628 | |
| 1629 | Instructs the print engine to abort the printing process. Returns |
| 1630 | true if successful; otherwise returns \c false. |
| 1631 | */ |
| 1632 | |
| 1633 | /*! |
| 1634 | \fn int QPrintEngine::metric(QPaintDevice::PaintDeviceMetric id) const |
| 1635 | |
| 1636 | Returns the metric for the given \a id. |
| 1637 | */ |
| 1638 | |
| 1639 | /*! |
| 1640 | \fn QPrinter::PrinterState QPrintEngine::printerState() const |
| 1641 | |
| 1642 | Returns the current state of the printer being used by the print engine. |
| 1643 | */ |
| 1644 | |
| 1645 | QT_END_NAMESPACE |
| 1646 | |
| 1647 | #endif // QT_NO_PRINTER |
| 1648 | |