10#include <qpa/qplatformprintplugin.h>
11#include <qpa/qplatformprintersupport.h>
15#include <qcoreapplication.h>
18#include <private/qpagedpaintdevice_p.h>
23#if QT_CONFIG(printpreviewwidget)
24#include <private/qpaintengine_preview_p.h>
29using namespace Qt::StringLiterals;
31#define ABORT_IF_ACTIVE(location)
32 if (d->printEngine->printerState() == QPrinter::Active) {
33 qWarning("%s: Cannot be changed while printer is active", location);
37Q_GUI_EXPORT
extern qreal qt_pixelMultiplier(
int resolution);
40QPrinterInfo QPrinterPrivate::findValidPrinter(
const QPrinterInfo &printer)
43 QPrinterInfo printerToUse = printer;
44 if (printerToUse.isNull()) {
45 printerToUse = QPrinterInfo::defaultPrinter();
46 if (printerToUse.isNull()) {
47 QStringList availablePrinterNames = QPrinterInfo::availablePrinterNames();
48 if (!availablePrinterNames.isEmpty())
49 printerToUse = QPrinterInfo::printerInfo(availablePrinterNames.at(0));
55void QPrinterPrivate::initEngines(QPrinter::OutputFormat format,
const QPrinterInfo &printer)
58 outputFormat = QPrinter::PdfFormat;
59 QPlatformPrinterSupport *ps =
nullptr;
63 if (format == QPrinter::NativeFormat) {
64 ps = QPlatformPrinterSupportPlugin::get();
65 QPrinterInfo printerToUse = findValidPrinter(printer);
66 if (ps && !printerToUse.isNull()) {
67 outputFormat = QPrinter::NativeFormat;
68 printerName = printerToUse.printerName();
72 if (outputFormat == QPrinter::NativeFormat) {
73 printEngine = ps->createNativePrintEngine(printerMode, printerName);
74 paintEngine = ps->createPaintEngine(printEngine, printerMode);
76 static const QHash<QPrinter::PdfVersion, QPdfEngine::PdfVersion> engineMapping {
77 {QPrinter::PdfVersion_1_4, QPdfEngine::PdfVersion::PdfVersion_1_4},
78 {QPrinter::PdfVersion_A1b, QPdfEngine::PdfVersion::PdfVersion_A1b},
79 {QPrinter::PdfVersion_1_6, QPdfEngine::PdfVersion::PdfVersion_1_6},
80 {QPrinter::PdfVersion_X4, QPdfEngine::PdfVersion::PdfVersion_X4 },
82 const auto pdfEngineVersion = engineMapping.value(pdfVersion, QPdfEngine::PdfVersion::PdfVersion_1_4);
83 QPdfPrintEngine *pdfEngine =
new QPdfPrintEngine(printerMode, pdfEngineVersion);
84 paintEngine = pdfEngine;
85 printEngine = pdfEngine;
88 use_default_engine =
true;
89 had_default_engines =
true;
93void QPrinterPrivate::changeEngines(QPrinter::OutputFormat format,
const QPrinterInfo &printer)
95 QPrintEngine *oldPrintEngine = printEngine;
96 const bool def_engine = use_default_engine;
98 initEngines(format, printer);
100 if (oldPrintEngine) {
101 const auto properties = m_properties;
102 for (
const auto &key : properties) {
107 if (key == QPrintEngine::PPK_NumberOfCopies)
108 prop = QVariant(q_ptr->copyCount());
109 else if (key != QPrintEngine::PPK_PrinterName)
110 prop = oldPrintEngine->property(key);
112 setProperty(key, prop);
117 delete oldPrintEngine;
120#if QT_CONFIG(printpreviewwidget)
121QList<
const QPicture *> QPrinterPrivate::previewPages()
const
124 return previewEngine->pages();
125 return QList<
const QPicture *>();
128bool QPrinterPrivate::previewMode()
const
130 return (previewEngine !=
nullptr) && (previewEngine == printEngine);
133void QPrinterPrivate::setPreviewMode(
bool enable)
138 previewEngine =
new QPreviewPaintEngine;
139 had_default_engines = use_default_engine;
140 use_default_engine =
false;
141 realPrintEngine = printEngine;
142 realPaintEngine = paintEngine;
143 q->setEngines(previewEngine, previewEngine);
144 previewEngine->setProxyEngines(realPrintEngine, realPaintEngine);
146 q->setEngines(realPrintEngine, realPaintEngine);
147 use_default_engine = had_default_engines;
152void QPrinterPrivate::setProperty(QPrintEngine::PrintEnginePropertyKey key,
const QVariant &value)
154 printEngine->setProperty(key, value);
155 m_properties.insert(key);
171 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
173 if (pd->paintEngine->type() != QPaintEngine::Pdf
174 && pd->printEngine->printerState() == QPrinter::Active) {
175 qWarning(
"QPrinter::setPageLayout: Cannot be changed while printer is active");
180 pd->setProperty(QPrintEngine::PPK_QPageLayout, QVariant::fromValue(newPageLayout));
182 return pageLayout().isEquivalentTo(newPageLayout);
187 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
189 if (pd->paintEngine->type() != QPaintEngine::Pdf
190 && pd->printEngine->printerState() == QPrinter::Active) {
191 qWarning(
"QPrinter::setPageLayout: Cannot be changed while printer is active");
197 pd->setProperty(QPrintEngine::PPK_QPageSize, QVariant::fromValue(pageSize));
199 return pageLayout().pageSize().isEquivalentTo(pageSize);
204 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
207 pd->setProperty(QPrintEngine::PPK_Orientation, orientation);
209 return pageLayout().orientation() == orientation;
214 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
217 std::pair<QMarginsF, QPageLayout::Unit> pair(margins, units);
218 pd->setProperty(QPrintEngine::PPK_QPageMargins, QVariant::fromValue(pair));
220 return pageLayout().margins() == margins && pageLayout().units() == units;
225 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
227 return qvariant_cast<QPageLayout>(pd->printEngine->property(QPrintEngine::PPK_QPageLayout));
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
310
311
312
313
314
315
316
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
352
353
354
355
356
357
358
359
360
361
362
365
366
367
368
369
370
371
372
373
374
375
378
379
380
381
382
383
384
385
386
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
440
441
442QPrinter::QPrinter(PrinterMode mode)
443 : QPagedPaintDevice(
new QPrinterPagedPaintDevicePrivate(
this)),
444 d_ptr(
new QPrinterPrivate(
this))
446 d_ptr->init(QPrinterInfo(), mode);
450
451
452
453
454QPrinter::QPrinter(
const QPrinterInfo& printer, PrinterMode mode)
455 : QPagedPaintDevice(
new QPrinterPagedPaintDevicePrivate(
this)),
456 d_ptr(
new QPrinterPrivate(
this))
458 d_ptr->init(printer, mode);
461QPrinterPrivate::~QPrinterPrivate()
464void QPrinterPrivate::init(
const QPrinterInfo &printer, QPrinter::PrinterMode mode)
466 if (Q_UNLIKELY(!QCoreApplication::instance())) {
467 qFatal(
"QPrinter: Must construct a QCoreApplication before a QPrinter");
473 initEngines(QPrinter::NativeFormat, printer);
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491void QPrinter::setEngines(QPrintEngine *printEngine, QPaintEngine *paintEngine)
495 if (d->use_default_engine)
496 delete d->printEngine;
498 d->printEngine = printEngine;
499 d->paintEngine = paintEngine;
500 d->use_default_engine =
false;
504
505
506
507
511 if (d->use_default_engine)
512 delete d->printEngine;
513#if QT_CONFIG(printpreviewwidget)
514 delete d->previewEngine;
519
520
521
522
523
524
525
526
527
528
529
530
531
532
535
536
537
538
539
540
541
542
543
544
545
546
547
548void QPrinter::setOutputFormat(OutputFormat format)
552 if (d->outputFormat == format)
555 if (format == QPrinter::NativeFormat) {
556 QPrinterInfo printerToUse = d->findValidPrinter();
557 if (!printerToUse.isNull())
558 d->changeEngines(format, printerToUse);
560 d->changeEngines(format, QPrinterInfo());
565
566
567
568
569QPrinter::OutputFormat QPrinter::outputFormat()
const
572 return d->outputFormat;
576
577
578
579
580
581
582void QPrinter::setPdfVersion(PdfVersion version)
586 if (d->pdfVersion == version)
589 d->pdfVersion = version;
591 if (d->outputFormat == QPrinter::PdfFormat) {
592 d->changeEngines(d->outputFormat, QPrinterInfo());
597
598
599
600
601QPrinter::PdfVersion QPrinter::pdfVersion()
const
604 return d->pdfVersion;
608
609int QPrinter::devType()
const
611 return QInternal::Printer;
615
616
617
618
619
620QString QPrinter::printerName()
const
623 return d->printEngine->property(QPrintEngine::PPK_PrinterName).toString();
627
628
629
630
631
632
633
634
635
636
637void QPrinter::setPrinterName(
const QString &name)
642 if (printerName() == name)
645 if (name.isEmpty()) {
646 setOutputFormat(QPrinter::PdfFormat);
650 QPrinterInfo printerToUse = QPrinterInfo::printerInfo(name);
651 if (printerToUse.isNull())
654 if (outputFormat() == QPrinter::PdfFormat) {
655 d->changeEngines(QPrinter::NativeFormat, printerToUse);
657 d->setProperty(QPrintEngine::PPK_PrinterName, name);
662
663
664
665
666
667
668
669
670
671
672
673bool QPrinter::isValid()
const
678 return d->validPrinter;
682
683
684
685
686
687
688
689
691QString QPrinter::outputFileName()
const
694 return d->printEngine->property(QPrintEngine::PPK_OutputFileName).toString();
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
716void QPrinter::setOutputFileName(
const QString &fileName)
721 QFileInfo fi(fileName);
722 if (!fi.suffix().compare(
"pdf"_L1, Qt::CaseInsensitive))
723 setOutputFormat(QPrinter::PdfFormat);
724 else if (fileName.isEmpty())
725 setOutputFormat(QPrinter::NativeFormat);
727 d->setProperty(QPrintEngine::PPK_OutputFileName, fileName);
732
733
734
735
736
737
738
739
740
741
742QString QPrinter::printProgram()
const
745 return d->printEngine->property(QPrintEngine::PPK_PrinterProgram).toString();
750
751
752
753
754
755
756
757
758void QPrinter::setPrintProgram(
const QString &printProg)
762 d->setProperty(QPrintEngine::PPK_PrinterProgram, printProg);
767
768
769
770
771QString QPrinter::docName()
const
774 return d->printEngine->property(QPrintEngine::PPK_DocumentName).toString();
779
780
781
782
783
784
785
786
787
788void QPrinter::setDocName(
const QString &name)
792 d->setProperty(QPrintEngine::PPK_DocumentName, name);
797
798
799
800
801QString QPrinter::creator()
const
804 return d->printEngine->property(QPrintEngine::PPK_Creator).toString();
809
810
811
812
813
814
815
816
817
818void QPrinter::setCreator(
const QString &creator)
822 d->setProperty(QPrintEngine::PPK_Creator, creator);
826
827
828
829
830
831
832
833
834
835
836
838void QPrinter::setPageOrder(PageOrder pageOrder)
840 d->pageOrderAscending = (pageOrder == FirstPageFirst);
844 d->setProperty(QPrintEngine::PPK_PageOrder, pageOrder);
849
850
851
852
854QPrinter::PageOrder QPrinter::pageOrder()
const
857 return QPrinter::PageOrder(d->printEngine->property(QPrintEngine::PPK_PageOrder).toInt());
862
863
864
865
866
868void QPrinter::setColorMode(ColorMode newColorMode)
872 d->setProperty(QPrintEngine::PPK_ColorMode, newColorMode);
877
878
879
880
881QPrinter::ColorMode QPrinter::colorMode()
const
884 return QPrinter::ColorMode(d->printEngine->property(QPrintEngine::PPK_ColorMode).toInt());
888
889
890
891
892
893
894
895
896
898void QPrinter::setCopyCount(
int count)
902 d->setProperty(QPrintEngine::PPK_CopyCount, count);
906
907
908
909
910
911
913int QPrinter::copyCount()
const
916 return d->printEngine->property(QPrintEngine::PPK_CopyCount).toInt();
920
921
922
923
924
925
926
927
928
929
930
931
933bool QPrinter::supportsMultipleCopies()
const
936 return d->printEngine->property(QPrintEngine::PPK_SupportsMultipleCopies).toBool();
940
941
942
943
944
945
946
947
948
949
950bool QPrinter::collateCopies()
const
953 return d->printEngine->property(QPrintEngine::PPK_CollateCopies).toBool();
958
959
960
961
962
963
964
965
966
967void QPrinter::setCollateCopies(
bool collate)
971 d->setProperty(QPrintEngine::PPK_CollateCopies, collate);
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
997void QPrinter::setFullPage(
bool fp)
1001 d->setProperty(QPrintEngine::PPK_FullPage, fp);
1006
1007
1008
1009
1010
1011
1012
1013
1015bool QPrinter::fullPage()
const
1017 Q_D(
const QPrinter);
1018 return d->printEngine->property(QPrintEngine::PPK_FullPage).toBool();
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1035void QPrinter::setResolution(
int dpi)
1039 d->setProperty(QPrintEngine::PPK_Resolution, dpi);
1044
1045
1046
1047
1048
1050int QPrinter::resolution()
const
1052 Q_D(
const QPrinter);
1053 return d->printEngine->property(QPrintEngine::PPK_Resolution).toInt();
1057
1058
1059
1060
1061
1062
1063
1065void QPrinter::setPaperSource(PaperSource source)
1068 d->setProperty(QPrintEngine::PPK_PaperSource, source);
1072
1073
1074
1075QPrinter::PaperSource QPrinter::paperSource()
const
1077 Q_D(
const QPrinter);
1078 return QPrinter::PaperSource(d->printEngine->property(QPrintEngine::PPK_PaperSource).toInt());
1083
1084
1085
1086
1087
1088
1089void QPrinter::setFontEmbeddingEnabled(
bool enable)
1092 d->setProperty(QPrintEngine::PPK_FontEmbedding, enable);
1096
1097
1098
1099
1100
1101
1102bool QPrinter::fontEmbeddingEnabled()
const
1104 Q_D(
const QPrinter);
1105 return d->printEngine->property(QPrintEngine::PPK_FontEmbedding).toBool();
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1127
1128
1129
1130
1131
1132
1133void QPrinter::setDuplex(DuplexMode duplex)
1136 d->setProperty(QPrintEngine::PPK_Duplex, duplex);
1140
1141
1142
1143
1144
1145
1146QPrinter::DuplexMode QPrinter::duplex()
const
1148 Q_D(
const QPrinter);
1149 return static_cast <DuplexMode> (d->printEngine->property(QPrintEngine::PPK_Duplex).toInt());
1153
1154
1155
1156
1157
1158
1159
1160
1161QRectF QPrinter::pageRect(Unit unit)
const
1163 if (unit == QPrinter::DevicePixel)
1164 return pageLayout().paintRectPixels(resolution());
1166 return pageLayout().paintRect(QPageLayout::Unit(unit));
1171
1172
1173
1174
1175
1176
1177
1178QRectF QPrinter::paperRect(Unit unit)
const
1180 if (unit == QPrinter::DevicePixel)
1181 return pageLayout().fullRectPixels(resolution());
1183 return pageLayout().fullRect(QPageLayout::Unit(unit));
1187
1188
1189
1190
1191int QPrinter::metric(PaintDeviceMetric id)
const
1193 Q_D(
const QPrinter);
1194 return d->printEngine->metric(id);
1198
1199
1200QPaintEngine *QPrinter::paintEngine()
const
1202 Q_D(
const QPrinter);
1203 return d->paintEngine;
1207
1208
1209
1210
1211QPrintEngine *QPrinter::printEngine()
const
1213 Q_D(
const QPrinter);
1214 return d->printEngine;
1218
1219
1220
1221
1222
1223
1224
1225QList<
int> QPrinter::supportedResolutions()
const
1227 Q_D(
const QPrinter);
1228 const QList<QVariant> varlist
1229 = d->printEngine->property(QPrintEngine::PPK_SupportedResolutions).toList();
1231 intlist.reserve(varlist.size());
1232 for (
const auto &var : varlist)
1233 intlist << var.toInt();
1238
1239
1240
1241
1242
1243
1244
1245bool QPrinter::newPage()
1248 if (d->printEngine->printerState() != QPrinter::Active)
1250 return d->printEngine->newPage();
1254
1255
1256
1257
1258
1259
1260
1261
1262bool QPrinter::abort()
1265 return d->printEngine->abort();
1269
1270
1271
1272
1273QPrinter::PrinterState QPrinter::printerState()
const
1275 Q_D(
const QPrinter);
1276 return d->printEngine->printerState();
1279#if defined(Q_OS_WIN) || defined(Q_QDOC)
1281
1282
1283
1284
1285
1286
1287
1288
1289
1291QList<QPrinter::PaperSource> QPrinter::supportedPaperSources()
const
1293 Q_D(
const QPrinter);
1294 QVariant v = d->printEngine->property(QPrintEngine::PPK_PaperSources);
1296 const QList<QVariant> variant_list = v.toList();
1297 QList<QPrinter::PaperSource> int_list;
1298 int_list.reserve(variant_list.size());
1299 for (
const auto &variant : variant_list)
1300 int_list << QPrinter::PaperSource(variant.toInt());
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1323QString QPrinter::printerSelectionOption()
const
1325 Q_D(
const QPrinter);
1326 return d->printEngine->property(QPrintEngine::PPK_SelectionOption).toString();
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1345void QPrinter::setPrinterSelectionOption(
const QString &option)
1348 d->setProperty(QPrintEngine::PPK_SelectionOption, option);
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1368int QPrinter::fromPage()
const
1370 return d->pageRanges.firstPage();
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1392int QPrinter::toPage()
const
1394 return d->pageRanges.lastPage();
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1414void QPrinter::setFromTo(
int from,
int to)
1416 d->pageRanges.clear();
1418 d->pageRanges.addRange(from, to);
1422
1423
1424
1425
1426void QPrinter::setPrintRange( PrintRange range )
1428 d->printSelectionOnly = (range == Selection);
1431 d->printRange = range;
1435
1436
1437
1438
1439
1440
1441
1442
1443QPrinter::PrintRange QPrinter::printRange()
const
1445 Q_D(
const QPrinter);
1446 return d->printRange;
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1556
1557
1558
1559
1562
1563
1564
1565
1566
1567
1570
1571
1572
1573
1574
1575
1578
1579
1580
1581
1582
1585
1586
1587
1588
1589
1592
1593
1594
1595
1598
1599
1600
1601
1603#undef ABORT_IF_ACTIVE
bool setPageLayout(const QPageLayout &newPageLayout) override
bool setPageMargins(const QMarginsF &margins, QPageLayout::Unit units) override
virtual ~QPrinterPagedPaintDevicePrivate()
QPageLayout pageLayout() const override
QPrinterPagedPaintDevicePrivate(QPrinter *p)
bool setPageOrientation(QPageLayout::Orientation orientation) override
bool setPageSize(const QPageSize &pageSize) override
Combined button and popup list for selecting options.
QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits)
#define ABORT_IF_ACTIVE(location)