source: trunk/src/gui/painting/qprinter.cpp@ 778

Last change on this file since 778 was 651, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 66.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qprinter_p.h"
43#include "qprinter.h"
44#include "qprintengine.h"
45#include "qprinterinfo.h"
46#include "qlist.h"
47#include <qpagesetupdialog.h>
48#include <qapplication.h>
49#include <qfileinfo.h>
50#if !defined(QT_NO_CUPS) && (!defined(QT_NO_LIBRARY) || defined(Q_WS_PM))
51#include "private/qcups_p.h"
52#endif
53
54#ifndef QT_NO_PRINTER
55
56#if defined (Q_WS_WIN)
57#include <private/qprintengine_win_p.h>
58#elif defined (Q_WS_MAC)
59#include <private/qprintengine_mac_p.h>
60#elif defined (QTOPIA_PRINTENGINE)
61#include <private/qprintengine_qws_p.h>
62#endif
63#include <private/qprintengine_ps_p.h>
64
65#if defined(Q_WS_X11)
66#include <private/qt_x11_p.h>
67#endif
68
69#ifndef QT_NO_PDF
70#include "qprintengine_pdf_p.h"
71#endif
72
73#include <qpicture.h>
74#include <private/qpaintengine_preview_p.h>
75
76#if defined(QT3_SUPPORT)
77# include "qprintdialog.h"
78#endif // QT3_SUPPORT
79
80QT_BEGIN_NAMESPACE
81
82#define ABORT_IF_ACTIVE(location) \
83 if (d->printEngine->printerState() == QPrinter::Active) { \
84 qWarning("%s: Cannot be changed while printer is active", location); \
85 return; \
86 }
87
88// NB! This table needs to be in sync with QPrinter::PaperSize
89static const float qt_paperSizes[][2] = {
90 {210, 297}, // A4
91 {176, 250}, // B5
92 {215.9f, 279.4f}, // Letter
93 {215.9f, 355.6f}, // Legal
94 {190.5f, 254}, // Executive
95 {841, 1189}, // A0
96 {594, 841}, // A1
97 {420, 594}, // A2
98 {297, 420}, // A3
99 {148, 210}, // A5
100 {105, 148}, // A6
101 {74, 105}, // A7
102 {52, 74}, // A8
103 {37, 52}, // A8
104 {1000, 1414}, // B0
105 {707, 1000}, // B1
106 {31, 44}, // B10
107 {500, 707}, // B2
108 {353, 500}, // B3
109 {250, 353}, // B4
110 {125, 176}, // B6
111 {88, 125}, // B7
112 {62, 88}, // B8
113 {33, 62}, // B9
114 {163, 229}, // C5E
115 {105, 241}, // US Common
116 {110, 220}, // DLE
117 {210, 330}, // Folio
118 {431.8f, 279.4f}, // Ledger
119 {279.4f, 431.8f} // Tabloid
120};
121
122/// return the multiplier of converting from the unit value to postscript-points.
123double qt_multiplierForUnit(QPrinter::Unit unit, int resolution)
124{
125 switch(unit) {
126 case QPrinter::Millimeter:
127 return 2.83464566929;
128 case QPrinter::Point:
129 return 1.0;
130 case QPrinter::Inch:
131 return 72.0;
132 case QPrinter::Pica:
133 return 12;
134 case QPrinter::Didot:
135 return 1.065826771;
136 case QPrinter::Cicero:
137 return 12.789921252;
138 case QPrinter::DevicePixel:
139 return 72.0/resolution;
140 }
141 return 1.0;
142}
143
144// not static: it's needed in qpagesetupdialog_unix.cpp
145QSizeF qt_printerPaperSize(QPrinter::Orientation orientation,
146 QPrinter::PaperSize paperSize,
147 QPrinter::Unit unit,
148 int resolution)
149{
150 int width_index = 0;
151 int height_index = 1;
152 if (orientation == QPrinter::Landscape) {
153 width_index = 1;
154 height_index = 0;
155 }
156 const qreal multiplier = qt_multiplierForUnit(unit, resolution);
157 return QSizeF((qt_paperSizes[paperSize][width_index] * 72 / 25.4) / multiplier,
158 (qt_paperSizes[paperSize][height_index] * 72 / 25.4) / multiplier);
159}
160
161
162// returns the actual num copies set on a printer, not
163// the number that is documented in QPrinter::numCopies()
164int qt_printerRealNumCopies(QPaintEngine *engine)
165{
166 int numCopies = 1;
167 if (engine->type() == QPaintEngine::PostScript
168 || engine->type() == QPaintEngine::Pdf)
169 {
170 QPdfBaseEngine *base = static_cast<QPdfBaseEngine *>(engine);
171 numCopies = base->d_func()->copies;
172 }
173#ifdef Q_WS_WIN
174 else if (engine->type() == QPaintEngine::Windows) {
175 QWin32PrintEngine *base = static_cast<QWin32PrintEngine *>(engine);
176 numCopies = base->d_func()->num_copies;
177 }
178#endif
179 return numCopies;
180}
181
182void QPrinterPrivate::createDefaultEngines()
183{
184 QPrinter::OutputFormat realOutputFormat = outputFormat;
185#if !defined (QTOPIA_PRINTENGINE)
186#if (defined (Q_OS_UNIX) && ! defined (Q_WS_MAC)) || defined (Q_WS_PM)
187 if(outputFormat == QPrinter::NativeFormat) {
188 realOutputFormat = QPrinter::PostScriptFormat;
189 }
190#endif
191#endif
192
193 switch (realOutputFormat) {
194 case QPrinter::NativeFormat: {
195#if defined (Q_WS_WIN)
196 QWin32PrintEngine *winEngine = new QWin32PrintEngine(printerMode);
197 paintEngine = winEngine;
198 printEngine = winEngine;
199#elif defined (Q_WS_MAC)
200 QMacPrintEngine *macEngine = new QMacPrintEngine(printerMode);
201 paintEngine = macEngine;
202 printEngine = macEngine;
203#elif defined (QTOPIA_PRINTENGINE)
204 QtopiaPrintEngine *qwsEngine = new QtopiaPrintEngine(printerMode);
205 paintEngine = qwsEngine;
206 printEngine = qwsEngine;
207#elif defined (Q_OS_UNIX) || defined (Q_WS_PM)
208 Q_ASSERT(false);
209#endif
210 }
211 break;
212 case QPrinter::PdfFormat: {
213 QPdfEngine *pdfEngine = new QPdfEngine(printerMode);
214 paintEngine = pdfEngine;
215 printEngine = pdfEngine;
216 }
217 break;
218 case QPrinter::PostScriptFormat: {
219 QPSPrintEngine *psEngine = new QPSPrintEngine(printerMode);
220 paintEngine = psEngine;
221 printEngine = psEngine;
222 }
223 break;
224 }
225 use_default_engine = true;
226 had_default_engines = true;
227}
228
229#ifndef QT_NO_PRINTPREVIEWWIDGET
230QList<const QPicture *> QPrinterPrivate::previewPages() const
231{
232 if (previewEngine)
233 return previewEngine->pages();
234 return QList<const QPicture *>();
235}
236
237void QPrinterPrivate::setPreviewMode(bool enable)
238{
239 Q_Q(QPrinter);
240 if (enable) {
241 if (!previewEngine)
242 previewEngine = new QPreviewPaintEngine;
243 had_default_engines = use_default_engine;
244 use_default_engine = false;
245 realPrintEngine = printEngine;
246 realPaintEngine = paintEngine;
247 q->setEngines(previewEngine, previewEngine);
248 previewEngine->setProxyEngines(realPrintEngine, realPaintEngine);
249 } else {
250 q->setEngines(realPrintEngine, realPaintEngine);
251 use_default_engine = had_default_engines;
252 }
253}
254#endif // QT_NO_PRINTPREVIEWWIDGET
255
256void QPrinterPrivate::addToManualSetList(QPrintEngine::PrintEnginePropertyKey key)
257{
258 for (int c = 0; c < manualSetList.size(); ++c) {
259 if (manualSetList[c] == key) return;
260 }
261 manualSetList.append(key);
262}
263
264
265/*!
266 \class QPrinter
267 \reentrant
268
269 \brief The QPrinter class is a paint device that paints on a printer.
270
271 \ingroup printing
272
273
274 This device represents a series of pages of printed output, and is
275 used in almost exactly the same way as other paint devices such as
276 QWidget and QPixmap.
277 A set of additional functions are provided to manage device-specific
278 features, such as orientation and resolution, and to step through
279 the pages in a document as it is generated.
280
281 When printing directly to a printer on Windows or Mac OS X, QPrinter uses
282 the built-in printer drivers. On X11, QPrinter uses the
283 \l{Common Unix Printing System (CUPS)} or the standard Unix \l lpr utility
284 to send PostScript or PDF output to the printer. As an alternative,
285 the printProgram() function can be used to specify the command or utility
286 to use instead of the system default. On OS/2, QPrinter also uses CUPS
287 but it does not provide support for the \l lpr utility.
288
289 Note that setting parameters like paper size and resolution on an
290 invalid printer is undefined. You can use QPrinter::isValid() to
291 verify this before changing any parameters.
292
293 QPrinter supports a number of parameters, most of which can be
294 changed by the end user through a \l{QPrintDialog}{print dialog}. In
295 general, QPrinter passes these functions onto the underlying QPrintEngine.
296
297 The most important parameters are:
298 \list
299 \i setOrientation() tells QPrinter which page orientation to use.
300 \i setPaperSize() tells QPrinter what paper size to expect from the
301 printer.
302 \i setResolution() tells QPrinter what resolution you wish the
303 printer to provide, in dots per inch (DPI).
304 \i setFullPage() tells QPrinter whether you want to deal with the
305 full page or just with the part the printer can draw on.
306 \i setNumCopies() tells QPrinter how many copies of the document
307 it should print.
308 \endlist
309
310 Many of these functions can only be called before the actual printing
311 begins (i.e., before QPainter::begin() is called). This usually makes
312 sense because, for example, it's not possible to change the number of
313 copies when you are halfway through printing. There are also some
314 settings that the user sets (through the printer dialog) and that
315 applications are expected to obey. See QAbstractPrintDialog's
316 documentation for more details.
317
318 When QPainter::begin() is called, the QPrinter it operates on is prepared for
319 a new page, enabling the QPainter to be used immediately to paint the first
320 page in a document. Once the first page has been painted, newPage() can be
321 called to request a new blank page to paint on, or QPainter::end() can be
322 called to finish printing. The second page and all following pages are
323 prepared using a call to newPage() before they are painted.
324
325 The first page in a document does not need to be preceded by a call to
326 newPage(). You only need to calling newPage() after QPainter::begin() if you
327 need to insert a blank page at the beginning of a printed document.
328 Similarly, calling newPage() after the last page in a document is painted will
329 result in a trailing blank page appended to the end of the printed document.
330
331 If you want to abort the print job, abort() will try its best to
332 stop printing. It may cancel the entire job or just part of it.
333
334 Since QPrinter can print to any QPrintEngine subclass, it is possible to
335 extend printing support to cover new types of printing subsystem by
336 subclassing QPrintEngine and reimplementing its interface.
337
338 \sa QPrintDialog, {Printing with Qt}
339*/
340
341/*!
342 \enum QPrinter::PrinterState
343
344 \value Idle
345 \value Active
346 \value Aborted
347 \value Error
348*/
349
350/*!
351 \enum QPrinter::PrinterMode
352
353 This enum describes the mode the printer should work in. It
354 basically presets a certain resolution and working mode.
355
356 \value ScreenResolution Sets the resolution of the print device to
357 the screen resolution. This has the big advantage that the results
358 obtained when painting on the printer will match more or less
359 exactly the visible output on the screen. It is the easiest to
360 use, as font metrics on the screen and on the printer are the
361 same. This is the default value. ScreenResolution will produce a
362 lower quality output than HighResolution and should only be used
363 for drafts.
364
365 \value PrinterResolution This value is deprecated. Is is
366 equivalent to ScreenResolution on Unix and HighResolution on
367 Windows and Mac. Due do the difference between ScreenResolution
368 and HighResolution, use of this value may lead to non-portable
369 printer code.
370
371 \value HighResolution On Windows, sets the printer resolution to that
372 defined for the printer in use. For PostScript printing, sets the
373 resolution of the PostScript driver to 1200 dpi.
374
375 \note When rendering text on a QPrinter device, it is important
376 to realize that the size of text, when specified in points, is
377 independent of the resolution specified for the device itself.
378 Therefore, it may be useful to specify the font size in pixels
379 when combining text with graphics to ensure that their relative
380 sizes are what you expect.
381*/
382
383/*!
384 \enum QPrinter::Orientation
385
386 This enum type (not to be confused with \c Orientation) is used
387 to specify each page's orientation.
388
389 \value Portrait the page's height is greater than its width.
390
391 \value Landscape the page's width is greater than its height.
392
393 This type interacts with \l QPrinter::PaperSize and
394 QPrinter::setFullPage() to determine the final size of the page
395 available to the application.
396*/
397
398
399/*!
400 \enum QPrinter::PrintRange
401
402 Used to specify the print range selection option.
403
404 \value AllPages All pages should be printed.
405 \value Selection Only the selection should be printed.
406 \value PageRange The specified page range should be printed.
407
408 \sa QAbstractPrintDialog::PrintRange
409*/
410
411/*!
412 \enum QPrinter::PrinterOption
413 \compat
414
415 Use QAbstractPrintDialog::PrintDialogOption instead.
416
417 \value PrintToFile
418 \value PrintSelection
419 \value PrintPageRange
420*/
421
422/*!
423 \enum QPrinter::PageSize
424
425 \obsolete
426 Use QPrinter::PaperSize instead.
427
428 \value A0 841 x 1189 mm
429 \value A1 594 x 841 mm
430 \value A2 420 x 594 mm
431 \value A3 297 x 420 mm
432 \value A4 210 x 297 mm, 8.26 x 11.69 inches
433 \value A5 148 x 210 mm
434 \value A6 105 x 148 mm
435 \value A7 74 x 105 mm
436 \value A8 52 x 74 mm
437 \value A9 37 x 52 mm
438 \value B0 1030 x 1456 mm
439 \value B1 728 x 1030 mm
440 \value B10 32 x 45 mm
441 \value B2 515 x 728 mm
442 \value B3 364 x 515 mm
443 \value B4 257 x 364 mm
444 \value B5 182 x 257 mm, 7.17 x 10.13 inches
445 \value B6 128 x 182 mm
446 \value B7 91 x 128 mm
447 \value B8 64 x 91 mm
448 \value B9 45 x 64 mm
449 \value C5E 163 x 229 mm
450 \value Comm10E 105 x 241 mm, U.S. Common 10 Envelope
451 \value DLE 110 x 220 mm
452 \value Executive 7.5 x 10 inches, 191 x 254 mm
453 \value Folio 210 x 330 mm
454 \value Ledger 432 x 279 mm
455 \value Legal 8.5 x 14 inches, 216 x 356 mm
456 \value Letter 8.5 x 11 inches, 216 x 279 mm
457 \value Tabloid 279 x 432 mm
458 \value Custom Unknown, or a user defined size.
459
460 \omitvalue NPageSize
461 */
462
463/*!
464 \enum QPrinter::PaperSize
465 \since 4.4
466
467 This enum type specifies what paper size QPrinter should use.
468 QPrinter does not check that the paper size is available; it just
469 uses this information, together with QPrinter::Orientation and
470 QPrinter::setFullPage(), to determine the printable area.
471
472 The defined sizes (with setFullPage(true)) are:
473
474 \value A0 841 x 1189 mm
475 \value A1 594 x 841 mm
476 \value A2 420 x 594 mm
477 \value A3 297 x 420 mm
478 \value A4 210 x 297 mm, 8.26 x 11.69 inches
479 \value A5 148 x 210 mm
480 \value A6 105 x 148 mm
481 \value A7 74 x 105 mm
482 \value A8 52 x 74 mm
483 \value A9 37 x 52 mm
484 \value B0 1000 x 1414 mm
485 \value B1 707 x 1000 mm
486 \value B2 500 x 707 mm
487 \value B3 353 x 500 mm
488 \value B4 250 x 353 mm
489 \value B5 176 x 250 mm, 6.93 x 9.84 inches
490 \value B6 125 x 176 mm
491 \value B7 88 x 125 mm
492 \value B8 62 x 88 mm
493 \value B9 33 x 62 mm
494 \value B10 31 x 44 mm
495 \value C5E 163 x 229 mm
496 \value Comm10E 105 x 241 mm, U.S. Common 10 Envelope
497 \value DLE 110 x 220 mm
498 \value Executive 7.5 x 10 inches, 190.5 x 254 mm
499 \value Folio 210 x 330 mm
500 \value Ledger 431.8 x 279.4 mm
501 \value Legal 8.5 x 14 inches, 215.9 x 355.6 mm
502 \value Letter 8.5 x 11 inches, 215.9 x 279.4 mm
503 \value Tabloid 279.4 x 431.8 mm
504 \value Custom Unknown, or a user defined size.
505
506 With setFullPage(false) (the default), the metrics will be a bit
507 smaller; how much depends on the printer in use.
508
509 \omitvalue NPageSize
510 \omitvalue NPaperSize
511*/
512
513
514/*!
515 \enum QPrinter::PageOrder
516
517 This enum type is used by QPrinter to tell the application program
518 how to print.
519
520 \value FirstPageFirst the lowest-numbered page should be printed
521 first.
522
523 \value LastPageFirst the highest-numbered page should be printed
524 first.
525*/
526
527/*!
528 \enum QPrinter::ColorMode
529
530 This enum type is used to indicate whether QPrinter should print
531 in color or not.
532
533 \value Color print in color if available, otherwise in grayscale.
534
535 \value GrayScale print in grayscale, even on color printers.
536*/
537
538/*!
539 \enum QPrinter::PaperSource
540
541 This enum type specifies what paper source QPrinter is to use.
542 QPrinter does not check that the paper source is available; it
543 just uses this information to try and set the paper source.
544 Whether it will set the paper source depends on whether the
545 printer has that particular source.
546
547 \warning This is currently only implemented for Windows.
548
549 \value Auto
550 \value Cassette
551 \value Envelope
552 \value EnvelopeManual
553 \value FormSource
554 \value LargeCapacity
555 \value LargeFormat
556 \value Lower
557 \value MaxPageSource
558 \value Middle
559 \value Manual
560 \value OnlyOne
561 \value Tractor
562 \value SmallFormat
563*/
564
565/*!
566 \enum QPrinter::Unit
567 \since 4.4
568
569 This enum type is used to specify the measurement unit for page and
570 paper sizes.
571
572 \value Millimeter
573 \value Point
574 \value Inch
575 \value Pica
576 \value Didot
577 \value Cicero
578 \value DevicePixel
579
580 Note the difference between Point and DevicePixel. The Point unit is
581 defined to be 1/72th of an inch, while the DevicePixel unit is
582 resolution dependant and is based on the actual pixels, or dots, on
583 the printer.
584*/
585
586
587/*
588 \enum QPrinter::PrintRange
589
590 This enum is used to specify which print range the application
591 should use to print.
592
593 \value AllPages All the pages should be printed.
594 \value Selection Only the selection should be printed.
595 \value PageRange Print according to the from page and to page options.
596
597 \sa setPrintRange(), printRange()
598*/
599
600/*
601 \enum QPrinter::PrinterOption
602
603 This enum describes various printer options that appear in the
604 printer setup dialog. It is used to enable and disable these
605 options in the setup dialog.
606
607 \value PrintToFile Describes if print to file should be enabled.
608 \value PrintSelection Describes if printing selections should be enabled.
609 \value PrintPageRange Describes if printing page ranges (from, to) should
610 be enabled
611
612 \sa setOptionEnabled(), isOptionEnabled()
613*/
614
615/*!
616 Creates a new printer object with the given \a mode.
617*/
618QPrinter::QPrinter(PrinterMode mode)
619 : QPaintDevice(),
620 d_ptr(new QPrinterPrivate(this))
621{
622 init(mode);
623 QPrinterInfo defPrn(QPrinterInfo::defaultPrinter());
624 if (!defPrn.isNull()) {
625 setPrinterName(defPrn.printerName());
626 } else if (QPrinterInfo::availablePrinters().isEmpty()
627 && d_ptr->paintEngine->type() != QPaintEngine::Windows
628 && d_ptr->paintEngine->type() != QPaintEngine::MacPrinter) {
629 setOutputFormat(QPrinter::PdfFormat);
630 }
631}
632
633/*!
634 \since 4.4
635
636 Creates a new printer object with the given \a printer and \a mode.
637*/
638QPrinter::QPrinter(const QPrinterInfo& printer, PrinterMode mode)
639 : QPaintDevice(),
640 d_ptr(new QPrinterPrivate(this))
641{
642 init(mode);
643 setPrinterName(printer.printerName());
644}
645
646void QPrinter::init(PrinterMode mode)
647{
648#if !defined(Q_WS_X11)
649 if (!qApp) {
650#else
651 if (!qApp || !X11) {
652#endif
653 qFatal("QPrinter: Must construct a QApplication before a QPaintDevice");
654 return;
655 }
656 Q_D(QPrinter);
657
658 d->printerMode = mode;
659 d->outputFormat = QPrinter::NativeFormat;
660 d->createDefaultEngines();
661
662#ifndef QT_NO_PRINTPREVIEWWIDGET
663 d->previewEngine = 0;
664#endif
665 d->realPrintEngine = 0;
666 d->realPaintEngine = 0;
667
668 // Note: On OS/2, sending PDF data doesn't yet work with the current eCUPS
669 // (which is version 1.3.11 of CUPS), so don't force the PDF format there
670#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) && !defined(Q_WS_PM)
671 if (QCUPSSupport::cupsVersion() >= 10200 && QCUPSSupport().currentPPD()) {
672 setOutputFormat(QPrinter::PdfFormat);
673 d->outputFormat = QPrinter::NativeFormat;
674 }
675#endif
676}
677
678/*!
679 This function is used by subclasses of QPrinter to specify custom
680 print and paint engines (\a printEngine and \a paintEngine,
681 respectively).
682
683 QPrinter does not take ownership of the engines, so you need to
684 manage these engine instances yourself.
685
686 Note that changing the engines will reset the printer state and
687 all its properties.
688
689 \sa printEngine() paintEngine() setOutputFormat()
690
691 \since 4.1
692*/
693void QPrinter::setEngines(QPrintEngine *printEngine, QPaintEngine *paintEngine)
694{
695 Q_D(QPrinter);
696
697 if (d->use_default_engine)
698 delete d->printEngine;
699
700 d->printEngine = printEngine;
701 d->paintEngine = paintEngine;
702 d->use_default_engine = false;
703}
704
705/*!
706 Destroys the printer object and frees any allocated resources. If
707 the printer is destroyed while a print job is in progress this may
708 or may not affect the print job.
709*/
710QPrinter::~QPrinter()
711{
712 Q_D(QPrinter);
713 if (d->use_default_engine)
714 delete d->printEngine;
715#ifndef QT_NO_PRINTPREVIEWWIDGET
716 delete d->previewEngine;
717#endif
718}
719
720/*!
721 \enum QPrinter::OutputFormat
722
723 The OutputFormat enum is used to describe the format QPrinter should
724 use for printing.
725
726 \value NativeFormat QPrinter will print output using a method defined
727 by the platform it is running on. This mode is the default when printing
728 directly to a printer.
729
730 \value PdfFormat QPrinter will generate its output as a searchable PDF file.
731 This mode is the default when printing to a file.
732
733 \value PostScriptFormat QPrinter will generate its output as in the PostScript format.
734 (This feature was introduced in Qt 4.2.)
735
736 \sa outputFormat(), setOutputFormat(), setOutputFileName()
737*/
738
739/*!
740 \since 4.1
741
742 Sets the output format for this printer to \a format.
743*/
744void QPrinter::setOutputFormat(OutputFormat format)
745{
746
747#ifndef QT_NO_PDF
748 Q_D(QPrinter);
749 if (d->validPrinter && d->outputFormat == format)
750 return;
751 d->outputFormat = format;
752
753 QPrintEngine *oldPrintEngine = d->printEngine;
754 QPaintEngine *oldPaintEngine = d->paintEngine; // same as the above - shouldn't be deleted
755 const bool def_engine = d->use_default_engine;
756 d->printEngine = 0;
757
758 d->createDefaultEngines();
759
760 if (oldPrintEngine) {
761 for (int i = 0; i < d->manualSetList.size(); ++i) {
762 QPrintEngine::PrintEnginePropertyKey key = d->manualSetList[i];
763 QVariant prop;
764 // PPK_NumberOfCopies need special treatmeant since it in most cases
765 // will return 1, disregarding the actual value that was set
766 if (key == QPrintEngine::PPK_NumberOfCopies)
767 prop = QVariant(qt_printerRealNumCopies(oldPaintEngine));
768 else
769 prop = oldPrintEngine->property(key);
770 if (prop.isValid())
771 d->printEngine->setProperty(key, prop);
772 }
773 }
774
775 if (def_engine)
776 delete oldPrintEngine;
777
778 if (d->outputFormat == QPrinter::PdfFormat || d->outputFormat == QPrinter::PostScriptFormat)
779 d->validPrinter = true;
780#else
781 Q_UNUSED(format);
782#endif
783}
784
785/*!
786 \since 4.1
787
788 Returns the output format for this printer.
789*/
790QPrinter::OutputFormat QPrinter::outputFormat() const
791{
792 Q_D(const QPrinter);
793 return d->outputFormat;
794}
795
796
797
798/*! \internal
799*/
800int QPrinter::devType() const
801{
802 return QInternal::Printer;
803}
804
805/*!
806 Returns the printer name. This value is initially set to the name
807 of the default printer.
808
809 \sa setPrinterName()
810*/
811QString QPrinter::printerName() const
812{
813 Q_D(const QPrinter);
814 return d->printEngine->property(QPrintEngine::PPK_PrinterName).toString();
815}
816
817/*!
818 Sets the printer name to \a name.
819
820 \sa printerName(), isValid()
821*/
822void QPrinter::setPrinterName(const QString &name)
823{
824 Q_D(QPrinter);
825 ABORT_IF_ACTIVE("QPrinter::setPrinterName");
826
827#if defined(Q_OS_UNIX) && !defined(QT_NO_CUPS)
828 if(d->use_default_engine
829 && d->outputFormat == QPrinter::NativeFormat) {
830 // Note: On OS/2, sending PDF data doesn't yet work with the current eCUPS
831 // (which is version 1.3.11 of CUPS), so don't force the PDF format there
832#ifndef Q_WS_PM
833 if (QCUPSSupport::cupsVersion() >= 10200
834 && QCUPSSupport::printerHasPPD(name.toLocal8Bit().constData()))
835 setOutputFormat(QPrinter::PdfFormat);
836 else
837#endif
838 setOutputFormat(QPrinter::PostScriptFormat);
839 d->outputFormat = QPrinter::NativeFormat;
840 }
841#endif
842
843 QList<QPrinterInfo> prnList = QPrinterInfo::availablePrinters();
844 if (name.isEmpty()) {
845 d->validPrinter = d->outputFormat == QPrinter::PdfFormat || d->outputFormat == QPrinter::PostScriptFormat;
846 } else {
847 d->validPrinter = false;
848 for (int i = 0; i < prnList.size(); ++i) {
849 if (prnList[i].printerName() == name) {
850 d->validPrinter = true;
851 break;
852 }
853 }
854 }
855
856 d->printEngine->setProperty(QPrintEngine::PPK_PrinterName, name);
857 d->addToManualSetList(QPrintEngine::PPK_PrinterName);
858}
859
860
861/*!
862 \since 4.4
863
864 Returns true if the printer currently selected is a valid printer
865 in the system, or a pure PDF/PostScript printer; otherwise returns false.
866
867 To detect other failures check the output of QPainter::begin() or QPrinter::newPage().
868
869 \snippet doc/src/snippets/printing-qprinter/errors.cpp 0
870
871 \sa setPrinterName()
872*/
873bool QPrinter::isValid() const
874{
875 Q_D(const QPrinter);
876#if defined(Q_WS_X11)
877 if (!qApp || !X11) {
878 return false;
879 }
880#endif
881 return d->validPrinter;
882}
883
884
885/*!
886 \fn bool QPrinter::outputToFile() const
887
888 Returns true if the output should be written to a file, or false
889 if the output should be sent directly to the printer. The default
890 setting is false.
891
892 \sa setOutputToFile(), setOutputFileName()
893*/
894
895
896/*!
897 \fn void QPrinter::setOutputToFile(bool enable)
898
899 Specifies whether the output should be written to a file or sent
900 directly to the printer.
901
902 Will output to a file if \a enable is true, or will output
903 directly to the printer if \a enable is false.
904
905 \sa outputToFile(), setOutputFileName()
906*/
907
908
909/*!
910 \fn QString QPrinter::outputFileName() const
911
912 Returns the name of the output file. By default, this is an empty string
913 (indicating that the printer shouldn't print to file).
914
915 \sa QPrintEngine::PrintEnginePropertyKey
916
917*/
918
919QString QPrinter::outputFileName() const
920{
921 Q_D(const QPrinter);
922 return d->printEngine->property(QPrintEngine::PPK_OutputFileName).toString();
923}
924
925/*!
926 Sets the name of the output file to \a fileName.
927
928 Setting a null or empty name (0 or "") disables printing to a file.
929 Setting a non-empty name enables printing to a file.
930
931 This can change the value of outputFormat(). If the file name has the
932 suffix ".ps" then PostScript is automatically selected as output format.
933 If the file name has the ".pdf" suffix PDF is generated. If the file name
934 has a suffix other than ".ps" and ".pdf", the output format used is the
935 one set with setOutputFormat().
936
937 QPrinter uses Qt's cross-platform PostScript or PDF print engines
938 respectively. If you can produce this format natively, for example
939 Mac OS X can generate PDF's from its print engine, set the output format
940 back to NativeFormat.
941
942 \sa outputFileName() setOutputToFile() setOutputFormat()
943*/
944
945void QPrinter::setOutputFileName(const QString &fileName)
946{
947 Q_D(QPrinter);
948 ABORT_IF_ACTIVE("QPrinter::setOutputFileName");
949
950 QFileInfo fi(fileName);
951 if (!fi.suffix().compare(QLatin1String("ps"), Qt::CaseInsensitive))
952 setOutputFormat(QPrinter::PostScriptFormat);
953 else if (!fi.suffix().compare(QLatin1String("pdf"), Qt::CaseInsensitive))
954 setOutputFormat(QPrinter::PdfFormat);
955 else if (fileName.isEmpty())
956 setOutputFormat(QPrinter::NativeFormat);
957
958 d->printEngine->setProperty(QPrintEngine::PPK_OutputFileName, fileName);
959 d->addToManualSetList(QPrintEngine::PPK_OutputFileName);
960}
961
962
963/*!
964 Returns the name of the program that sends the print output to the
965 printer.
966
967 The default is to return an empty string; meaning that QPrinter will try to
968 be smart in a system-dependent way. On X11 only, you can set it to something
969 different to use a specific print program. On the other platforms, this
970 returns an empty string.
971
972 \sa setPrintProgram(), setPrinterSelectionOption()
973*/
974QString QPrinter::printProgram() const
975{
976 Q_D(const QPrinter);
977 return d->printEngine->property(QPrintEngine::PPK_PrinterProgram).toString();
978}
979
980
981/*!
982 Sets the name of the program that should do the print job to \a
983 printProg.
984
985 On X11, this function sets the program to call with the PostScript
986 output. On other platforms, it has no effect.
987
988 \sa printProgram()
989*/
990void QPrinter::setPrintProgram(const QString &printProg)
991{
992 Q_D(QPrinter);
993 ABORT_IF_ACTIVE("QPrinter::setPrintProgram");
994 d->printEngine->setProperty(QPrintEngine::PPK_PrinterProgram, printProg);
995 d->addToManualSetList(QPrintEngine::PPK_PrinterProgram);
996}
997
998
999/*!
1000 Returns the document name.
1001
1002 \sa setDocName(), QPrintEngine::PrintEnginePropertyKey
1003*/
1004QString QPrinter::docName() const
1005{
1006 Q_D(const QPrinter);
1007 return d->printEngine->property(QPrintEngine::PPK_DocumentName).toString();
1008}
1009
1010
1011/*!
1012 Sets the document name to \a name.
1013
1014 On X11, the document name is for example used as the default
1015 output filename in QPrintDialog. Note that the document name does
1016 not affect the file name if the printer is printing to a file.
1017 Use the setOutputFile() function for this.
1018
1019 \sa docName(), QPrintEngine::PrintEnginePropertyKey
1020*/
1021void QPrinter::setDocName(const QString &name)
1022{
1023 Q_D(QPrinter);
1024 ABORT_IF_ACTIVE("QPrinter::setDocName");
1025 d->printEngine->setProperty(QPrintEngine::PPK_DocumentName, name);
1026 d->addToManualSetList(QPrintEngine::PPK_DocumentName);
1027}
1028
1029
1030/*!
1031 Returns the name of the application that created the document.
1032
1033 \sa setCreator()
1034*/
1035QString QPrinter::creator() const
1036{
1037 Q_D(const QPrinter);
1038 return d->printEngine->property(QPrintEngine::PPK_Creator).toString();
1039}
1040
1041
1042/*!
1043 Sets the name of the application that created the document to \a
1044 creator.
1045
1046 This function is only applicable to the X11 version of Qt. If no
1047 creator name is specified, the creator will be set to "Qt"
1048 followed by some version number.
1049
1050 \sa creator()
1051*/
1052void QPrinter::setCreator(const QString &creator)
1053{
1054 Q_D(QPrinter);
1055 ABORT_IF_ACTIVE("QPrinter::setCreator");
1056 d->printEngine->setProperty(QPrintEngine::PPK_Creator, creator);
1057 d->addToManualSetList(QPrintEngine::PPK_Creator);
1058}
1059
1060
1061/*!
1062 Returns the orientation setting. This is driver-dependent, but is usually
1063 QPrinter::Portrait.
1064
1065 \sa setOrientation()
1066*/
1067QPrinter::Orientation QPrinter::orientation() const
1068{
1069 Q_D(const QPrinter);
1070 return QPrinter::Orientation(d->printEngine->property(QPrintEngine::PPK_Orientation).toInt());
1071}
1072
1073
1074/*!
1075 Sets the print orientation to \a orientation.
1076
1077 The orientation can be either QPrinter::Portrait or
1078 QPrinter::Landscape.
1079
1080 The printer driver reads this setting and prints using the
1081 specified orientation.
1082
1083 On Windows, this option can be changed while printing and will
1084 take effect from the next call to newPage().
1085
1086 On Mac OS X, changing the orientation during a print job has no effect.
1087
1088 \sa orientation()
1089*/
1090
1091void QPrinter::setOrientation(Orientation orientation)
1092{
1093 Q_D(QPrinter);
1094 d->printEngine->setProperty(QPrintEngine::PPK_Orientation, orientation);
1095 d->addToManualSetList(QPrintEngine::PPK_Orientation);
1096}
1097
1098
1099/*!
1100 \since 4.4
1101 Returns the printer paper size. The default value is driver-dependent.
1102
1103 \sa setPaperSize() pageRect() paperRect()
1104*/
1105
1106QPrinter::PaperSize QPrinter::paperSize() const
1107{
1108 Q_D(const QPrinter);
1109 return QPrinter::PaperSize(d->printEngine->property(QPrintEngine::PPK_PaperSize).toInt());
1110}
1111
1112/*!
1113 \since 4.4
1114
1115 Sets the printer paper size to \a newPaperSize if that size is
1116 supported. The result is undefined if \a newPaperSize is not
1117 supported.
1118
1119 The default paper size is driver-dependent.
1120
1121 This function is useful mostly for setting a default value that
1122 the user can override in the print dialog.
1123
1124 \sa paperSize() PaperSize setFullPage() setResolution() pageRect() paperRect()
1125*/
1126void QPrinter::setPaperSize(PaperSize newPaperSize)
1127{
1128 Q_D(QPrinter);
1129 if (d->paintEngine->type() != QPaintEngine::Pdf)
1130 ABORT_IF_ACTIVE("QPrinter::setPaperSize");
1131 if (newPaperSize < 0 || newPaperSize >= NPaperSize) {
1132 qWarning("QPrinter::setPaperSize: Illegal paper size %d", newPaperSize);
1133 return;
1134 }
1135 d->printEngine->setProperty(QPrintEngine::PPK_PaperSize, newPaperSize);
1136 d->addToManualSetList(QPrintEngine::PPK_PaperSize);
1137 d->hasUserSetPageSize = true;
1138}
1139
1140/*!
1141 \obsolete
1142
1143 Returns the printer page size. The default value is driver-dependent.
1144
1145 Use paperSize() instead.
1146*/
1147QPrinter::PageSize QPrinter::pageSize() const
1148{
1149 return paperSize();
1150}
1151
1152
1153/*!
1154 \obsolete
1155
1156 Sets the printer page size based on \a newPageSize.
1157
1158 Use setPaperSize() instead.
1159*/
1160
1161void QPrinter::setPageSize(PageSize newPageSize)
1162{
1163 setPaperSize(newPageSize);
1164}
1165
1166/*!
1167 \since 4.4
1168
1169 Sets the paper size based on \a paperSize in \a unit.
1170
1171 \sa paperSize()
1172*/
1173
1174void QPrinter::setPaperSize(const QSizeF &paperSize, QPrinter::Unit unit)
1175{
1176 Q_D(QPrinter);
1177 if (d->paintEngine->type() != QPaintEngine::Pdf)
1178 ABORT_IF_ACTIVE("QPrinter::setPaperSize");
1179 const qreal multiplier = qt_multiplierForUnit(unit, resolution());
1180 QSizeF size(paperSize.width() * multiplier, paperSize.height() * multiplier);
1181 d->printEngine->setProperty(QPrintEngine::PPK_CustomPaperSize, size);
1182 d->addToManualSetList(QPrintEngine::PPK_CustomPaperSize);
1183 d->hasUserSetPageSize = true;
1184}
1185
1186/*!
1187 \since 4.4
1188
1189 Returns the paper size in \a unit.
1190
1191 \sa setPaperSize()
1192*/
1193
1194QSizeF QPrinter::paperSize(Unit unit) const
1195{
1196 Q_D(const QPrinter);
1197 int res = resolution();
1198 const qreal multiplier = qt_multiplierForUnit(unit, res);
1199 PaperSize paperType = paperSize();
1200 if (paperType == Custom) {
1201 QSizeF size = d->printEngine->property(QPrintEngine::PPK_CustomPaperSize).toSizeF();
1202 return QSizeF(size.width() / multiplier, size.height() / multiplier);
1203 }
1204 else {
1205 return qt_printerPaperSize(orientation(), paperType, unit, res);
1206 }
1207}
1208
1209/*!
1210 Sets the page order to \a pageOrder.
1211
1212 The page order can be QPrinter::FirstPageFirst or
1213 QPrinter::LastPageFirst. The application is responsible for
1214 reading the page order and printing accordingly.
1215
1216 This function is mostly useful for setting a default value that
1217 the user can override in the print dialog.
1218
1219 This function is only supported under X11.
1220*/
1221
1222void QPrinter::setPageOrder(PageOrder pageOrder)
1223{
1224 Q_D(QPrinter);
1225 ABORT_IF_ACTIVE("QPrinter::setPageOrder");
1226 d->printEngine->setProperty(QPrintEngine::PPK_PageOrder, pageOrder);
1227 d->addToManualSetList(QPrintEngine::PPK_PageOrder);
1228}
1229
1230
1231/*!
1232 Returns the current page order.
1233
1234 The default page order is \c FirstPageFirst.
1235*/
1236
1237QPrinter::PageOrder QPrinter::pageOrder() const
1238{
1239 Q_D(const QPrinter);
1240 return QPrinter::PageOrder(d->printEngine->property(QPrintEngine::PPK_PageOrder).toInt());
1241}
1242
1243
1244/*!
1245 Sets the printer's color mode to \a newColorMode, which can be
1246 either \c Color or \c GrayScale.
1247
1248 \sa colorMode()
1249*/
1250
1251void QPrinter::setColorMode(ColorMode newColorMode)
1252{
1253 Q_D(QPrinter);
1254 ABORT_IF_ACTIVE("QPrinter::setColorMode");
1255 d->printEngine->setProperty(QPrintEngine::PPK_ColorMode, newColorMode);
1256 d->addToManualSetList(QPrintEngine::PPK_ColorMode);
1257}
1258
1259
1260/*!
1261 Returns the current color mode.
1262
1263 \sa setColorMode()
1264*/
1265QPrinter::ColorMode QPrinter::colorMode() const
1266{
1267 Q_D(const QPrinter);
1268 return QPrinter::ColorMode(d->printEngine->property(QPrintEngine::PPK_ColorMode).toInt());
1269}
1270
1271
1272/*!
1273 Returns the number of copies to be printed. The default value is 1.
1274
1275 On Windows, Mac OS X and X11 systems that support CUPS, this will always
1276 return 1 as these operating systems can internally handle the number
1277 of copies.
1278
1279 On X11, this value will return the number of times the application is
1280 required to print in order to match the number specified in the printer setup
1281 dialog. This has been done since some printer drivers are not capable of
1282 buffering up the copies and in those cases the application must make an
1283 explicit call to the print code for each copy.
1284
1285 \sa setNumCopies(), actualNumCopies()
1286*/
1287
1288int QPrinter::numCopies() const
1289{
1290 Q_D(const QPrinter);
1291 return d->printEngine->property(QPrintEngine::PPK_NumberOfCopies).toInt();
1292}
1293
1294
1295/*!
1296 \since 4.6
1297
1298 Returns the number of copies that will be printed. The default
1299 value is 1.
1300
1301 This function always returns the actual value specified in the print
1302 dialog or using setNumCopies().
1303
1304 \sa setNumCopies(), numCopies()
1305*/
1306int QPrinter::actualNumCopies() const
1307{
1308 Q_D(const QPrinter);
1309 return qt_printerRealNumCopies(d->paintEngine);
1310}
1311
1312
1313
1314/*!
1315 Sets the number of copies to be printed to \a numCopies.
1316
1317 The printer driver reads this setting and prints the specified
1318 number of copies.
1319
1320 \sa numCopies()
1321*/
1322
1323void QPrinter::setNumCopies(int numCopies)
1324{
1325 Q_D(QPrinter);
1326 ABORT_IF_ACTIVE("QPrinter::setNumCopies");
1327 d->printEngine->setProperty(QPrintEngine::PPK_NumberOfCopies, numCopies);
1328 d->addToManualSetList(QPrintEngine::PPK_NumberOfCopies);
1329}
1330
1331
1332/*!
1333 \since 4.1
1334
1335 Returns true if collation is turned on when multiple copies is selected.
1336 Returns false if it is turned off when multiple copies is selected.
1337 When collating is turned off the printing of each individual page will be repeated
1338 the numCopies() amount before the next page is started. With collating turned on
1339 all pages are printed before the next copy of those pages is started.
1340
1341 \sa setCollateCopies()
1342*/
1343bool QPrinter::collateCopies() const
1344{
1345 Q_D(const QPrinter);
1346 return d->printEngine->property(QPrintEngine::PPK_CollateCopies).toBool();
1347}
1348
1349
1350/*!
1351 \since 4.1
1352
1353 Sets the default value for collation checkbox when the print
1354 dialog appears. If \a collate is true, it will enable
1355 setCollateCopiesEnabled(). The default value is false. This value
1356 will be changed by what the user presses in the print dialog.
1357
1358 \sa collateCopies()
1359*/
1360void QPrinter::setCollateCopies(bool collate)
1361{
1362 Q_D(QPrinter);
1363 ABORT_IF_ACTIVE("QPrinter::setCollateCopies");
1364 d->printEngine->setProperty(QPrintEngine::PPK_CollateCopies, collate);
1365 d->addToManualSetList(QPrintEngine::PPK_CollateCopies);
1366}
1367
1368
1369
1370/*!
1371 If \a fp is true, enables support for painting over the entire page;
1372 otherwise restricts painting to the printable area reported by the
1373 device.
1374
1375 By default, full page printing is disabled. In this case, the origin
1376 of the QPrinter's coordinate system coincides with the top-left
1377 corner of the printable area.
1378
1379 If full page printing is enabled, the origin of the QPrinter's
1380 coordinate system coincides with the top-left corner of the paper
1381 itself. In this case, the
1382 \l{QPaintDevice::PaintDeviceMetric}{device metrics} will report
1383 the exact same dimensions as indicated by \l{PaperSize}. It may not
1384 be possible to print on the entire physical page because of the
1385 printer's margins, so the application must account for the margins
1386 itself.
1387
1388 \sa fullPage(), setPaperSize(), width(), height(), {Printing with Qt}
1389*/
1390
1391void QPrinter::setFullPage(bool fp)
1392{
1393 Q_D(QPrinter);
1394 d->printEngine->setProperty(QPrintEngine::PPK_FullPage, fp);
1395 d->addToManualSetList(QPrintEngine::PPK_FullPage);
1396}
1397
1398
1399/*!
1400 Returns true if the origin of the printer's coordinate system is
1401 at the corner of the page and false if it is at the edge of the
1402 printable area.
1403
1404 See setFullPage() for details and caveats.
1405
1406 \sa setFullPage() PaperSize
1407*/
1408
1409bool QPrinter::fullPage() const
1410{
1411 Q_D(const QPrinter);
1412 return d->printEngine->property(QPrintEngine::PPK_FullPage).toBool();
1413}
1414
1415
1416/*!
1417 Requests that the printer prints at \a dpi or as near to \a dpi as
1418 possible.
1419
1420 This setting affects the coordinate system as returned by, for
1421 example QPainter::viewport().
1422
1423 This function must be called before QPainter::begin() to have an effect on
1424 all platforms.
1425
1426 \sa resolution() setPaperSize()
1427*/
1428
1429void QPrinter::setResolution(int dpi)
1430{
1431 Q_D(QPrinter);
1432 ABORT_IF_ACTIVE("QPrinter::setResolution");
1433 d->printEngine->setProperty(QPrintEngine::PPK_Resolution, dpi);
1434 d->addToManualSetList(QPrintEngine::PPK_Resolution);
1435}
1436
1437
1438/*!
1439 Returns the current assumed resolution of the printer, as set by
1440 setResolution() or by the printer driver.
1441
1442 \sa setResolution()
1443*/
1444
1445int QPrinter::resolution() const
1446{
1447 Q_D(const QPrinter);
1448 return d->printEngine->property(QPrintEngine::PPK_Resolution).toInt();
1449}
1450
1451/*!
1452 Sets the paper source setting to \a source.
1453
1454 Windows only: This option can be changed while printing and will
1455 take effect from the next call to newPage()
1456
1457 \sa paperSource()
1458*/
1459