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);
37#define ABORT_IF_ACTIVE_RETURN(location, retValue)
38 if (d->printEngine->printerState() == QPrinter::Active) {
39 qWarning("%s: Cannot be changed while printer is active", location);
43Q_GUI_EXPORT
extern qreal qt_pixelMultiplier(
int resolution);
46QPrinterInfo QPrinterPrivate::findValidPrinter(
const QPrinterInfo &printer)
49 QPrinterInfo printerToUse = printer;
50 if (printerToUse.isNull()) {
51 printerToUse = QPrinterInfo::defaultPrinter();
52 if (printerToUse.isNull()) {
53 QStringList availablePrinterNames = QPrinterInfo::availablePrinterNames();
54 if (!availablePrinterNames.isEmpty())
55 printerToUse = QPrinterInfo::printerInfo(availablePrinterNames.at(0));
61void QPrinterPrivate::initEngines(QPrinter::OutputFormat format,
const QPrinterInfo &printer)
64 outputFormat = QPrinter::PdfFormat;
65 QPlatformPrinterSupport *ps =
nullptr;
69 if (format == QPrinter::NativeFormat) {
70 ps = QPlatformPrinterSupportPlugin::get();
71 QPrinterInfo printerToUse = findValidPrinter(printer);
72 if (ps && !printerToUse.isNull()) {
73 outputFormat = QPrinter::NativeFormat;
74 printerName = printerToUse.printerName();
78 if (outputFormat == QPrinter::NativeFormat) {
79 printEngine = ps->createNativePrintEngine(printerMode, printerName);
80 paintEngine = ps->createPaintEngine(printEngine, printerMode);
82 static const QHash<QPrinter::PdfVersion, QPdfEngine::PdfVersion> engineMapping {
83 {QPrinter::PdfVersion_1_4, QPdfEngine::Version_1_4},
84 {QPrinter::PdfVersion_A1b, QPdfEngine::Version_A1b},
85 {QPrinter::PdfVersion_1_6, QPdfEngine::Version_1_6}
87 const auto pdfEngineVersion = engineMapping.value(pdfVersion, QPdfEngine::Version_1_4);
88 QPdfPrintEngine *pdfEngine =
new QPdfPrintEngine(printerMode, pdfEngineVersion);
89 paintEngine = pdfEngine;
90 printEngine = pdfEngine;
93 use_default_engine =
true;
94 had_default_engines =
true;
98void QPrinterPrivate::changeEngines(QPrinter::OutputFormat format,
const QPrinterInfo &printer)
100 QPrintEngine *oldPrintEngine = printEngine;
101 const bool def_engine = use_default_engine;
103 initEngines(format, printer);
105 if (oldPrintEngine) {
106 const auto properties = m_properties;
107 for (
const auto &key : properties) {
112 if (key == QPrintEngine::PPK_NumberOfCopies)
113 prop = QVariant(q_ptr->copyCount());
114 else if (key != QPrintEngine::PPK_PrinterName)
115 prop = oldPrintEngine->property(key);
117 setProperty(key, prop);
122 delete oldPrintEngine;
125#if QT_CONFIG(printpreviewwidget)
126QList<
const QPicture *> QPrinterPrivate::previewPages()
const
129 return previewEngine->pages();
130 return QList<
const QPicture *>();
133bool QPrinterPrivate::previewMode()
const
135 return (previewEngine !=
nullptr) && (previewEngine == printEngine);
138void QPrinterPrivate::setPreviewMode(
bool enable)
143 previewEngine =
new QPreviewPaintEngine;
144 had_default_engines = use_default_engine;
145 use_default_engine =
false;
146 realPrintEngine = printEngine;
147 realPaintEngine = paintEngine;
148 q->setEngines(previewEngine, previewEngine);
149 previewEngine->setProxyEngines(realPrintEngine, realPaintEngine);
151 q->setEngines(realPrintEngine, realPaintEngine);
152 use_default_engine = had_default_engines;
157void QPrinterPrivate::setProperty(QPrintEngine::PrintEnginePropertyKey key,
const QVariant &value)
159 printEngine->setProperty(key, value);
160 m_properties.insert(key);
176 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
178 if (pd->paintEngine->type() != QPaintEngine::Pdf
179 && pd->printEngine->printerState() == QPrinter::Active) {
180 qWarning(
"QPrinter::setPageLayout: Cannot be changed while printer is active");
185 pd->setProperty(QPrintEngine::PPK_QPageLayout, QVariant::fromValue(newPageLayout));
187 return pageLayout().isEquivalentTo(newPageLayout);
192 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
194 if (pd->paintEngine->type() != QPaintEngine::Pdf
195 && pd->printEngine->printerState() == QPrinter::Active) {
196 qWarning(
"QPrinter::setPageLayout: Cannot be changed while printer is active");
202 pd->setProperty(QPrintEngine::PPK_QPageSize, QVariant::fromValue(pageSize));
204 return pageLayout().pageSize().isEquivalentTo(pageSize);
209 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
212 pd->setProperty(QPrintEngine::PPK_Orientation, orientation);
214 return pageLayout().orientation() == orientation;
219 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
222 std::pair<QMarginsF, QPageLayout::Unit> pair(margins, units);
223 pd->setProperty(QPrintEngine::PPK_QPageMargins, QVariant::fromValue(pair));
225 return pageLayout().margins() == margins && pageLayout().units() == units;
230 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
232 return qvariant_cast<QPageLayout>(pd->printEngine->property(QPrintEngine::PPK_QPageLayout));
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
308
309
310
311
312
315
316
317
318
319
320
321
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
350
351
352
353
354
357
358
359
360
361
362
363
364
365
366
367
370
371
372
373
374
375
376
377
378
379
380
383
384
385
386
387
388
389
390
391
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
445
446
447QPrinter::QPrinter(PrinterMode mode)
448 : QPagedPaintDevice(
new QPrinterPagedPaintDevicePrivate(
this)),
449 d_ptr(
new QPrinterPrivate(
this))
451 d_ptr->init(QPrinterInfo(), mode);
455
456
457
458
459QPrinter::QPrinter(
const QPrinterInfo& printer, PrinterMode mode)
460 : QPagedPaintDevice(
new QPrinterPagedPaintDevicePrivate(
this)),
461 d_ptr(
new QPrinterPrivate(
this))
463 d_ptr->init(printer, mode);
466void QPrinterPrivate::init(
const QPrinterInfo &printer, QPrinter::PrinterMode mode)
468 if (Q_UNLIKELY(!QCoreApplication::instance())) {
469 qFatal(
"QPrinter: Must construct a QCoreApplication before a QPrinter");
475 initEngines(QPrinter::NativeFormat, printer);
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493void QPrinter::setEngines(QPrintEngine *printEngine, QPaintEngine *paintEngine)
497 if (d->use_default_engine)
498 delete d->printEngine;
500 d->printEngine = printEngine;
501 d->paintEngine = paintEngine;
502 d->use_default_engine =
false;
506
507
508
509
513 if (d->use_default_engine)
514 delete d->printEngine;
515#if QT_CONFIG(printpreviewwidget)
516 delete d->previewEngine;
521
522
523
524
525
526
527
528
529
530
531
532
533
534
537
538
539
540
541
542
543
544
545
546
547
548
549
550void QPrinter::setOutputFormat(OutputFormat format)
554 if (d->outputFormat == format)
557 if (format == QPrinter::NativeFormat) {
558 QPrinterInfo printerToUse = d->findValidPrinter();
559 if (!printerToUse.isNull())
560 d->changeEngines(format, printerToUse);
562 d->changeEngines(format, QPrinterInfo());
567
568
569
570
571QPrinter::OutputFormat QPrinter::outputFormat()
const
574 return d->outputFormat;
578
579
580
581
582
583
584void QPrinter::setPdfVersion(PdfVersion version)
588 if (d->pdfVersion == version)
591 d->pdfVersion = version;
593 if (d->outputFormat == QPrinter::PdfFormat) {
594 d->changeEngines(d->outputFormat, QPrinterInfo());
599
600
601
602
603QPrinter::PdfVersion QPrinter::pdfVersion()
const
606 return d->pdfVersion;
610
611int QPrinter::devType()
const
613 return QInternal::Printer;
617
618
619
620
621
622QString QPrinter::printerName()
const
625 return d->printEngine->property(QPrintEngine::PPK_PrinterName).toString();
629
630
631
632
633
634
635
636
637
638
639void QPrinter::setPrinterName(
const QString &name)
644 if (printerName() == name)
647 if (name.isEmpty()) {
648 setOutputFormat(QPrinter::PdfFormat);
652 QPrinterInfo printerToUse = QPrinterInfo::printerInfo(name);
653 if (printerToUse.isNull())
656 if (outputFormat() == QPrinter::PdfFormat) {
657 d->changeEngines(QPrinter::NativeFormat, printerToUse);
659 d->setProperty(QPrintEngine::PPK_PrinterName, name);
664
665
666
667
668
669
670
671
672
673
674
675bool QPrinter::isValid()
const
680 return d->validPrinter;
684
685
686
687
688
689
690
691
693QString QPrinter::outputFileName()
const
696 return d->printEngine->property(QPrintEngine::PPK_OutputFileName).toString();
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
718void QPrinter::setOutputFileName(
const QString &fileName)
723 QFileInfo fi(fileName);
724 if (!fi.suffix().compare(
"pdf"_L1, Qt::CaseInsensitive))
725 setOutputFormat(QPrinter::PdfFormat);
726 else if (fileName.isEmpty())
727 setOutputFormat(QPrinter::NativeFormat);
729 d->setProperty(QPrintEngine::PPK_OutputFileName, fileName);
734
735
736
737
738
739
740
741
742
743
744QString QPrinter::printProgram()
const
747 return d->printEngine->property(QPrintEngine::PPK_PrinterProgram).toString();
752
753
754
755
756
757
758
759
760void QPrinter::setPrintProgram(
const QString &printProg)
764 d->setProperty(QPrintEngine::PPK_PrinterProgram, printProg);
769
770
771
772
773QString QPrinter::docName()
const
776 return d->printEngine->property(QPrintEngine::PPK_DocumentName).toString();
781
782
783
784
785
786
787
788
789
790void QPrinter::setDocName(
const QString &name)
794 d->setProperty(QPrintEngine::PPK_DocumentName, name);
799
800
801
802
803QString QPrinter::creator()
const
806 return d->printEngine->property(QPrintEngine::PPK_Creator).toString();
811
812
813
814
815
816
817
818
819
820void QPrinter::setCreator(
const QString &creator)
824 d->setProperty(QPrintEngine::PPK_Creator, creator);
828
829
830
831
832
833
834
835
836
837
838
840void QPrinter::setPageOrder(PageOrder pageOrder)
842 d->pageOrderAscending = (pageOrder == FirstPageFirst);
846 d->setProperty(QPrintEngine::PPK_PageOrder, pageOrder);
851
852
853
854
856QPrinter::PageOrder QPrinter::pageOrder()
const
859 return QPrinter::PageOrder(d->printEngine->property(QPrintEngine::PPK_PageOrder).toInt());
864
865
866
867
868
870void QPrinter::setColorMode(ColorMode newColorMode)
874 d->setProperty(QPrintEngine::PPK_ColorMode, newColorMode);
879
880
881
882
883QPrinter::ColorMode QPrinter::colorMode()
const
886 return QPrinter::ColorMode(d->printEngine->property(QPrintEngine::PPK_ColorMode).toInt());
890
891
892
893
894
895
896
897
898
900void QPrinter::setCopyCount(
int count)
904 d->setProperty(QPrintEngine::PPK_CopyCount, count);
908
909
910
911
912
913
915int QPrinter::copyCount()
const
918 return d->printEngine->property(QPrintEngine::PPK_CopyCount).toInt();
922
923
924
925
926
927
928
929
930
931
932
933
935bool QPrinter::supportsMultipleCopies()
const
938 return d->printEngine->property(QPrintEngine::PPK_SupportsMultipleCopies).toBool();
942
943
944
945
946
947
948
949
950
951
952bool QPrinter::collateCopies()
const
955 return d->printEngine->property(QPrintEngine::PPK_CollateCopies).toBool();
960
961
962
963
964
965
966
967
968
969void QPrinter::setCollateCopies(
bool collate)
973 d->setProperty(QPrintEngine::PPK_CollateCopies, collate);
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
999void QPrinter::setFullPage(
bool fp)
1003 d->setProperty(QPrintEngine::PPK_FullPage, fp);
1008
1009
1010
1011
1012
1013
1014
1015
1017bool QPrinter::fullPage()
const
1019 Q_D(
const QPrinter);
1020 return d->printEngine->property(QPrintEngine::PPK_FullPage).toBool();
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1037void QPrinter::setResolution(
int dpi)
1041 d->setProperty(QPrintEngine::PPK_Resolution, dpi);
1046
1047
1048
1049
1050
1052int QPrinter::resolution()
const
1054 Q_D(
const QPrinter);
1055 return d->printEngine->property(QPrintEngine::PPK_Resolution).toInt();
1059
1060
1061
1062
1063
1064
1065
1067void QPrinter::setPaperSource(PaperSource source)
1070 d->setProperty(QPrintEngine::PPK_PaperSource, source);
1074
1075
1076
1077QPrinter::PaperSource QPrinter::paperSource()
const
1079 Q_D(
const QPrinter);
1080 return QPrinter::PaperSource(d->printEngine->property(QPrintEngine::PPK_PaperSource).toInt());
1085
1086
1087
1088
1089
1090
1091void QPrinter::setFontEmbeddingEnabled(
bool enable)
1094 d->setProperty(QPrintEngine::PPK_FontEmbedding, enable);
1098
1099
1100
1101
1102
1103
1104bool QPrinter::fontEmbeddingEnabled()
const
1106 Q_D(
const QPrinter);
1107 return d->printEngine->property(QPrintEngine::PPK_FontEmbedding).toBool();
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1129
1130
1131
1132
1133
1134
1135void QPrinter::setDuplex(DuplexMode duplex)
1138 d->setProperty(QPrintEngine::PPK_Duplex, duplex);
1142
1143
1144
1145
1146
1147
1148QPrinter::DuplexMode QPrinter::duplex()
const
1150 Q_D(
const QPrinter);
1151 return static_cast <DuplexMode> (d->printEngine->property(QPrintEngine::PPK_Duplex).toInt());
1155
1156
1157
1158
1159
1160
1161
1162
1163QRectF QPrinter::pageRect(Unit unit)
const
1165 if (unit == QPrinter::DevicePixel)
1166 return pageLayout().paintRectPixels(resolution());
1168 return pageLayout().paintRect(QPageLayout::Unit(unit));
1173
1174
1175
1176
1177
1178
1179
1180QRectF QPrinter::paperRect(Unit unit)
const
1182 if (unit == QPrinter::DevicePixel)
1183 return pageLayout().fullRectPixels(resolution());
1185 return pageLayout().fullRect(QPageLayout::Unit(unit));
1189
1190
1191
1192
1193int QPrinter::metric(PaintDeviceMetric id)
const
1195 Q_D(
const QPrinter);
1196 return d->printEngine->metric(id);
1200
1201
1202QPaintEngine *QPrinter::paintEngine()
const
1204 Q_D(
const QPrinter);
1205 return d->paintEngine;
1209
1210
1211
1212
1213QPrintEngine *QPrinter::printEngine()
const
1215 Q_D(
const QPrinter);
1216 return d->printEngine;
1220
1221
1222
1223
1224
1225
1226
1227QList<
int> QPrinter::supportedResolutions()
const
1229 Q_D(
const QPrinter);
1230 const QList<QVariant> varlist
1231 = d->printEngine->property(QPrintEngine::PPK_SupportedResolutions).toList();
1233 intlist.reserve(varlist.size());
1234 for (
const auto &var : varlist)
1235 intlist << var.toInt();
1240
1241
1242
1243
1244
1245
1246
1247bool QPrinter::newPage()
1250 if (d->printEngine->printerState() != QPrinter::Active)
1252 return d->printEngine->newPage();
1256
1257
1258
1259
1260
1261
1262
1263
1264bool QPrinter::abort()
1267 return d->printEngine->abort();
1271
1272
1273
1274
1275QPrinter::PrinterState QPrinter::printerState()
const
1277 Q_D(
const QPrinter);
1278 return d->printEngine->printerState();
1281#if defined(Q_OS_WIN) || defined(Q_QDOC)
1283
1284
1285
1286
1287
1288
1289
1290
1291
1293QList<QPrinter::PaperSource> QPrinter::supportedPaperSources()
const
1295 Q_D(
const QPrinter);
1296 QVariant v = d->printEngine->property(QPrintEngine::PPK_PaperSources);
1298 const QList<QVariant> variant_list = v.toList();
1299 QList<QPrinter::PaperSource> int_list;
1300 int_list.reserve(variant_list.size());
1301 for (
const auto &variant : variant_list)
1302 int_list << QPrinter::PaperSource(variant.toInt());
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1325QString QPrinter::printerSelectionOption()
const
1327 Q_D(
const QPrinter);
1328 return d->printEngine->property(QPrintEngine::PPK_SelectionOption).toString();
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1347void QPrinter::setPrinterSelectionOption(
const QString &option)
1350 d->setProperty(QPrintEngine::PPK_SelectionOption, option);
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1370int QPrinter::fromPage()
const
1372 return d->pageRanges.firstPage();
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1394int QPrinter::toPage()
const
1396 return d->pageRanges.lastPage();
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1416void QPrinter::setFromTo(
int from,
int to)
1418 d->pageRanges.clear();
1420 d->pageRanges.addRange(from, to);
1424
1425
1426
1427
1428void QPrinter::setPrintRange( PrintRange range )
1430 d->printSelectionOnly = (range == Selection);
1433 d->printRange = range;
1437
1438
1439
1440
1441
1442
1443
1444
1445QPrinter::PrintRange QPrinter::printRange()
const
1447 Q_D(
const QPrinter);
1448 return d->printRange;
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
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
1554
1555
1558
1559
1560
1561
1564
1565
1566
1567
1568
1569
1572
1573
1574
1575
1576
1577
1580
1581
1582
1583
1584
1587
1588
1589
1590
1591
1594
1595
1596
1597
1600
1601
1602
1603
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
QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits)
#define ABORT_IF_ACTIVE(location)