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 QPdfPrintEngine *pdfEngine =
new QPdfPrintEngine(printerMode, pdfVersion);
77 paintEngine = pdfEngine;
78 printEngine = pdfEngine;
81 use_default_engine =
true;
82 had_default_engines =
true;
86void QPrinterPrivate::changeEngines(QPrinter::OutputFormat format,
const QPrinterInfo &printer)
88 QPrintEngine *oldPrintEngine = printEngine;
89 const bool def_engine = use_default_engine;
91 initEngines(format, printer);
94 const auto properties = m_properties;
95 for (
const auto &key : properties) {
100 if (key == QPrintEngine::PPK_NumberOfCopies)
101 prop = QVariant(q_ptr->copyCount());
102 else if (key != QPrintEngine::PPK_PrinterName)
103 prop = oldPrintEngine->property(key);
105 setProperty(key, prop);
110 delete oldPrintEngine;
113#if QT_CONFIG(printpreviewwidget)
114QList<
const QPicture *> QPrinterPrivate::previewPages()
const
117 return previewEngine->pages();
118 return QList<
const QPicture *>();
121bool QPrinterPrivate::previewMode()
const
123 return (previewEngine !=
nullptr) && (previewEngine == printEngine);
126void QPrinterPrivate::setPreviewMode(
bool enable)
131 previewEngine =
new QPreviewPaintEngine;
132 had_default_engines = use_default_engine;
133 use_default_engine =
false;
134 realPrintEngine = printEngine;
135 realPaintEngine = paintEngine;
136 q->setEngines(previewEngine, previewEngine);
137 previewEngine->setProxyEngines(realPrintEngine, realPaintEngine);
139 q->setEngines(realPrintEngine, realPaintEngine);
140 use_default_engine = had_default_engines;
145void QPrinterPrivate::setProperty(QPrintEngine::PrintEnginePropertyKey key,
const QVariant &value)
147 printEngine->setProperty(key, value);
148 m_properties.insert(key);
164 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
166 if (pd->paintEngine->type() != QPaintEngine::Pdf
167 && pd->printEngine->printerState() == QPrinter::Active) {
168 qWarning(
"QPrinter::setPageLayout: Cannot be changed while printer is active");
173 pd->setProperty(QPrintEngine::PPK_QPageLayout, QVariant::fromValue(newPageLayout));
175 return pageLayout().isEquivalentTo(newPageLayout);
180 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
182 if (pd->paintEngine->type() != QPaintEngine::Pdf
183 && pd->printEngine->printerState() == QPrinter::Active) {
184 qWarning(
"QPrinter::setPageLayout: Cannot be changed while printer is active");
190 pd->setProperty(QPrintEngine::PPK_QPageSize, QVariant::fromValue(pageSize));
192 return pageLayout().pageSize().isEquivalentTo(pageSize);
197 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
200 pd->setProperty(QPrintEngine::PPK_Orientation, orientation);
202 return pageLayout().orientation() == orientation;
207 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
210 std::pair<QMarginsF, QPageLayout::Unit> pair(margins, units);
211 pd->setProperty(QPrintEngine::PPK_QPageMargins, QVariant::fromValue(pair));
213 return pageLayout().margins() == margins && pageLayout().units() == units;
218 QPrinterPrivate *pd = QPrinterPrivate::get(m_printer);
220 return qvariant_cast<QPageLayout>(pd->printEngine->property(QPrintEngine::PPK_QPageLayout));
228
229
230
231
232
233
234
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
303
304
305
306
307
308
309
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
345
346
347
348
349
350
351
352
353
354
355
358
359
360
361
362
363
364
365
366
367
368
371
372
373
374
375
376
377
378
379
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
433
434
435QPrinter::QPrinter(PrinterMode mode)
436 : QPagedPaintDevice(
new QPrinterPagedPaintDevicePrivate(
this)),
437 d_ptr(
new QPrinterPrivate(
this))
439 d_ptr->init(QPrinterInfo(), mode);
443
444
445
446
447QPrinter::QPrinter(
const QPrinterInfo& printer, PrinterMode mode)
448 : QPagedPaintDevice(
new QPrinterPagedPaintDevicePrivate(
this)),
449 d_ptr(
new QPrinterPrivate(
this))
451 d_ptr->init(printer, mode);
454QPrinterPrivate::~QPrinterPrivate()
457void QPrinterPrivate::init(
const QPrinterInfo &printer, QPrinter::PrinterMode mode)
459 if (Q_UNLIKELY(!QCoreApplication::instance())) {
460 qFatal(
"QPrinter: Must construct a QCoreApplication before a QPrinter");
466 initEngines(QPrinter::NativeFormat, printer);
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484void QPrinter::setEngines(QPrintEngine *printEngine, QPaintEngine *paintEngine)
488 if (d->use_default_engine)
489 delete d->printEngine;
491 d->printEngine = printEngine;
492 d->paintEngine = paintEngine;
493 d->use_default_engine =
false;
497
498
499
500
504 if (d->use_default_engine)
505 delete d->printEngine;
506#if QT_CONFIG(printpreviewwidget)
507 delete d->previewEngine;
512
513
514
515
516
517
518
519
520
521
522
523
524
525
528
529
530
531
532
533
534
535
536
537
538
539
540
541void QPrinter::setOutputFormat(OutputFormat format)
545 if (d->outputFormat == format)
548 if (format == QPrinter::NativeFormat) {
549 QPrinterInfo printerToUse = d->findValidPrinter();
550 if (!printerToUse.isNull())
551 d->changeEngines(format, printerToUse);
553 d->changeEngines(format, QPrinterInfo());
558
559
560
561
562QPrinter::OutputFormat QPrinter::outputFormat()
const
565 return d->outputFormat;
569
570
571
572
573
574
575void QPrinter::setPdfVersion(PdfVersion version)
579 if (d->pdfVersion == version)
582 d->pdfVersion = version;
584 if (d->outputFormat == QPrinter::PdfFormat) {
585 d->changeEngines(d->outputFormat, QPrinterInfo());
590
591
592
593
594QPrinter::PdfVersion QPrinter::pdfVersion()
const
597 return d->pdfVersion;
601
602int QPrinter::devType()
const
604 return QInternal::Printer;
608
609
610
611
612
613QString QPrinter::printerName()
const
616 return d->printEngine->property(QPrintEngine::PPK_PrinterName).toString();
620
621
622
623
624
625
626
627
628
629
630void QPrinter::setPrinterName(
const QString &name)
635 if (printerName() == name)
638 if (name.isEmpty()) {
639 setOutputFormat(QPrinter::PdfFormat);
643 QPrinterInfo printerToUse = QPrinterInfo::printerInfo(name);
644 if (printerToUse.isNull())
647 if (outputFormat() == QPrinter::PdfFormat) {
648 d->changeEngines(QPrinter::NativeFormat, printerToUse);
650 d->setProperty(QPrintEngine::PPK_PrinterName, name);
655
656
657
658
659
660
661
662
663
664
665
666bool QPrinter::isValid()
const
671 return d->validPrinter;
675
676
677
678
679
680
681
682
684QString QPrinter::outputFileName()
const
687 return d->printEngine->property(QPrintEngine::PPK_OutputFileName).toString();
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
709void QPrinter::setOutputFileName(
const QString &fileName)
714 QFileInfo fi(fileName);
715 if (!fi.suffix().compare(
"pdf"_L1, Qt::CaseInsensitive))
716 setOutputFormat(QPrinter::PdfFormat);
717 else if (fileName.isEmpty())
718 setOutputFormat(QPrinter::NativeFormat);
720 d->setProperty(QPrintEngine::PPK_OutputFileName, fileName);
725
726
727
728
729
730
731
732
733
734
735QString QPrinter::printProgram()
const
738 return d->printEngine->property(QPrintEngine::PPK_PrinterProgram).toString();
743
744
745
746
747
748
749
750
751void QPrinter::setPrintProgram(
const QString &printProg)
755 d->setProperty(QPrintEngine::PPK_PrinterProgram, printProg);
760
761
762
763
764QString QPrinter::docName()
const
767 return d->printEngine->property(QPrintEngine::PPK_DocumentName).toString();
772
773
774
775
776
777
778
779
780
781void QPrinter::setDocName(
const QString &name)
785 d->setProperty(QPrintEngine::PPK_DocumentName, name);
790
791
792
793
794QString QPrinter::creator()
const
797 return d->printEngine->property(QPrintEngine::PPK_Creator).toString();
802
803
804
805
806
807
808
809
810
811void QPrinter::setCreator(
const QString &creator)
815 d->setProperty(QPrintEngine::PPK_Creator, creator);
819
820
821
822
823
824
825
826
827
828
829
831void QPrinter::setPageOrder(PageOrder pageOrder)
833 d->pageOrderAscending = (pageOrder == FirstPageFirst);
837 d->setProperty(QPrintEngine::PPK_PageOrder, pageOrder);
842
843
844
845
847QPrinter::PageOrder QPrinter::pageOrder()
const
850 return QPrinter::PageOrder(d->printEngine->property(QPrintEngine::PPK_PageOrder).toInt());
855
856
857
858
859
861void QPrinter::setColorMode(ColorMode newColorMode)
865 d->setProperty(QPrintEngine::PPK_ColorMode, newColorMode);
870
871
872
873
874QPrinter::ColorMode QPrinter::colorMode()
const
877 return QPrinter::ColorMode(d->printEngine->property(QPrintEngine::PPK_ColorMode).toInt());
881
882
883
884
885
886
887
888
889
891void QPrinter::setCopyCount(
int count)
895 d->setProperty(QPrintEngine::PPK_CopyCount, count);
899
900
901
902
903
904
906int QPrinter::copyCount()
const
909 return d->printEngine->property(QPrintEngine::PPK_CopyCount).toInt();
913
914
915
916
917
918
919
920
921
922
923
924
926bool QPrinter::supportsMultipleCopies()
const
929 return d->printEngine->property(QPrintEngine::PPK_SupportsMultipleCopies).toBool();
933
934
935
936
937
938
939
940
941
942
943bool QPrinter::collateCopies()
const
946 return d->printEngine->property(QPrintEngine::PPK_CollateCopies).toBool();
951
952
953
954
955
956
957
958
959
960void QPrinter::setCollateCopies(
bool collate)
964 d->setProperty(QPrintEngine::PPK_CollateCopies, collate);
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
990void QPrinter::setFullPage(
bool fp)
994 d->setProperty(QPrintEngine::PPK_FullPage, fp);
999
1000
1001
1002
1003
1004
1005
1006
1008bool QPrinter::fullPage()
const
1010 Q_D(
const QPrinter);
1011 return d->printEngine->property(QPrintEngine::PPK_FullPage).toBool();
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1028void QPrinter::setResolution(
int dpi)
1032 d->setProperty(QPrintEngine::PPK_Resolution, dpi);
1037
1038
1039
1040
1041
1043int QPrinter::resolution()
const
1045 Q_D(
const QPrinter);
1046 return d->printEngine->property(QPrintEngine::PPK_Resolution).toInt();
1050
1051
1052
1053
1054
1055
1056
1058void QPrinter::setPaperSource(PaperSource source)
1061 d->setProperty(QPrintEngine::PPK_PaperSource, source);
1065
1066
1067
1068QPrinter::PaperSource QPrinter::paperSource()
const
1070 Q_D(
const QPrinter);
1071 return QPrinter::PaperSource(d->printEngine->property(QPrintEngine::PPK_PaperSource).toInt());
1076
1077
1078
1079
1080
1081
1082void QPrinter::setFontEmbeddingEnabled(
bool enable)
1085 d->setProperty(QPrintEngine::PPK_FontEmbedding, enable);
1089
1090
1091
1092
1093
1094
1095bool QPrinter::fontEmbeddingEnabled()
const
1097 Q_D(
const QPrinter);
1098 return d->printEngine->property(QPrintEngine::PPK_FontEmbedding).toBool();
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1120
1121
1122
1123
1124
1125
1126void QPrinter::setDuplex(DuplexMode duplex)
1129 d->setProperty(QPrintEngine::PPK_Duplex, duplex);
1133
1134
1135
1136
1137
1138
1139QPrinter::DuplexMode QPrinter::duplex()
const
1141 Q_D(
const QPrinter);
1142 return static_cast <DuplexMode> (d->printEngine->property(QPrintEngine::PPK_Duplex).toInt());
1146
1147
1148
1149
1150
1151
1152
1153
1154QRectF QPrinter::pageRect(Unit unit)
const
1156 if (unit == QPrinter::DevicePixel)
1157 return pageLayout().paintRectPixels(resolution());
1159 return pageLayout().paintRect(QPageLayout::Unit(unit));
1164
1165
1166
1167
1168
1169
1170
1171QRectF QPrinter::paperRect(Unit unit)
const
1173 if (unit == QPrinter::DevicePixel)
1174 return pageLayout().fullRectPixels(resolution());
1176 return pageLayout().fullRect(QPageLayout::Unit(unit));
1180
1181
1182
1183
1184int QPrinter::metric(PaintDeviceMetric id)
const
1186 Q_D(
const QPrinter);
1187 return d->printEngine->metric(id);
1191
1192
1193QPaintEngine *QPrinter::paintEngine()
const
1195 Q_D(
const QPrinter);
1196 return d->paintEngine;
1200
1201
1202
1203
1204QPrintEngine *QPrinter::printEngine()
const
1206 Q_D(
const QPrinter);
1207 return d->printEngine;
1211
1212
1213
1214
1215
1216
1217
1218QList<
int> QPrinter::supportedResolutions()
const
1220 Q_D(
const QPrinter);
1221 const QList<QVariant> varlist
1222 = d->printEngine->property(QPrintEngine::PPK_SupportedResolutions).toList();
1224 intlist.reserve(varlist.size());
1225 for (
const auto &var : varlist)
1226 intlist << var.toInt();
1231
1232
1233
1234
1235
1236
1237
1238bool QPrinter::newPage()
1241 if (d->printEngine->printerState() != QPrinter::Active)
1243 return d->printEngine->newPage();
1247
1248
1249
1250
1251
1252
1253
1254
1255bool QPrinter::abort()
1258 return d->printEngine->abort();
1262
1263
1264
1265
1266QPrinter::PrinterState QPrinter::printerState()
const
1268 Q_D(
const QPrinter);
1269 return d->printEngine->printerState();
1272#if defined(Q_OS_WIN) || defined(Q_QDOC)
1274
1275
1276
1277
1278
1279
1280
1281
1282
1284QList<QPrinter::PaperSource> QPrinter::supportedPaperSources()
const
1286 Q_D(
const QPrinter);
1287 QVariant v = d->printEngine->property(QPrintEngine::PPK_PaperSources);
1289 const QList<QVariant> variant_list = v.toList();
1290 QList<QPrinter::PaperSource> int_list;
1291 int_list.reserve(variant_list.size());
1292 for (
const auto &variant : variant_list)
1293 int_list << QPrinter::PaperSource(variant.toInt());
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1316QString QPrinter::printerSelectionOption()
const
1318 Q_D(
const QPrinter);
1319 return d->printEngine->property(QPrintEngine::PPK_SelectionOption).toString();
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1338void QPrinter::setPrinterSelectionOption(
const QString &option)
1341 d->setProperty(QPrintEngine::PPK_SelectionOption, option);
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1361int QPrinter::fromPage()
const
1363 return d->pageRanges.firstPage();
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1385int QPrinter::toPage()
const
1387 return d->pageRanges.lastPage();
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1407void QPrinter::setFromTo(
int from,
int to)
1409 d->pageRanges.clear();
1411 d->pageRanges.addRange(from, to);
1415
1416
1417
1418
1419void QPrinter::setPrintRange( PrintRange range )
1421 d->printSelectionOnly = (range == Selection);
1424 d->printRange = range;
1428
1429
1430
1431
1432
1433
1434
1435
1436QPrinter::PrintRange QPrinter::printRange()
const
1438 Q_D(
const QPrinter);
1439 return d->printRange;
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1461
1462
1463
1464
1465
1466
1467
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
1549
1550
1551
1552
1555
1556
1557
1558
1559
1560
1563
1564
1565
1566
1567
1568
1571
1572
1573
1574
1575
1578
1579
1580
1581
1582
1585
1586
1587
1588
1591
1592
1593
1594
1596#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)