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