1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
301
302
303
304
305
306
307
308
309
310
311
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <private/qprintengine_mac_p.h>
#include <qdebug.h>
#include <qthread.h>
#include <QtCore/qcoreapplication.h>
#ifndef QT_NO_PRINTER
QT_BEGIN_NAMESPACE
extern QSizeF qt_paperSizeToQSizeF(QPrinter::PaperSize size);
QMacPrintEngine::QMacPrintEngine(QPrinter::PrinterMode mode) : QPaintEngine(*(new QMacPrintEnginePrivate))
{
Q_D(QMacPrintEngine);
d->mode = mode;
d->initialize();
}
bool QMacPrintEngine::begin(QPaintDevice *dev)
{
Q_D(QMacPrintEngine);
Q_ASSERT(dev && dev->devType() == QInternal::Printer);
if (!static_cast<QPrinter *>(dev)->isValid())
return false;
if (d->state == QPrinter::Idle && !d->isPrintSessionInitialized()) // Need to reinitialize
d->initialize();
d->paintEngine->state = state;
d->paintEngine->begin(dev);
Q_ASSERT_X(d->state == QPrinter::Idle, "QMacPrintEngine", "printer already active");
if (PMSessionValidatePrintSettings(d->session, d->settings, kPMDontWantBoolean) != noErr
|| PMSessionValidatePageFormat(d->session, d->format, kPMDontWantBoolean) != noErr) {
d->state = QPrinter::Error;
return false;
}
if (!d->outputFilename.isEmpty()) {
QCFType<CFURLRef> outFile = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault,
QCFString(d->outputFilename),
kCFURLPOSIXPathStyle,
false);
if (PMSessionSetDestination(d->session, d->settings, kPMDestinationFile,
kPMDocumentFormatPDF, outFile) != noErr) {
qWarning("QMacPrintEngine::begin: Problem setting file [%s]", d->outputFilename.toUtf8().constData());
return false;
}
}
OSStatus status = noErr;
#ifndef QT_MAC_USE_COCOA
status = d->shouldSuppressStatus() ? PMSessionBeginCGDocumentNoDialog(d->session, d->settings, d->format)
: PMSessionBeginCGDocument(d->session, d->settings, d->format);
#else
status = PMSessionBeginCGDocumentNoDialog(d->session, d->settings, d->format);
#endif
if (status != noErr) {
d->state = QPrinter::Error;
return false;
}
d->state = QPrinter::Active;
setActive(true);
d->newPage_helper();
return true;
}
bool QMacPrintEngine::end()
{
Q_D(QMacPrintEngine);
if (d->state == QPrinter::Aborted)
return true; // I was just here a function call ago :)
if(d->paintEngine->type() == QPaintEngine::CoreGraphics) {
// We dont need the paint engine to call restoreGraphicsState()
static_cast<QCoreGraphicsPaintEngine*>(d->paintEngine)->d_func()->stackCount = 0;
static_cast<QCoreGraphicsPaintEngine*>(d->paintEngine)->d_func()->hd = 0;
}
d->paintEngine->end();
if (d->state != QPrinter::Idle)
d->releaseSession();
d->state = QPrinter::Idle;
return true;
}
QPaintEngine *
QMacPrintEngine::paintEngine() const
{
return d_func()->paintEngine;
}
Qt::HANDLE QMacPrintEngine::handle() const
{
QCoreGraphicsPaintEngine *cgEngine = static_cast<QCoreGraphicsPaintEngine*>(paintEngine());
return cgEngine->d_func()->hd;
}
QMacPrintEnginePrivate::~QMacPrintEnginePrivate()
{
#ifdef QT_MAC_USE_COCOA
[printInfo release];
#endif
delete paintEngine;
}
void QMacPrintEnginePrivate::setPaperSize(QPrinter::PaperSize ps)
{
Q_Q(QMacPrintEngine);
QSize newSize = qt_paperSizeToQSizeF(ps).toSize();
QCFType<CFArrayRef> formats;
PMPrinter printer;
if (PMSessionGetCurrentPrinter(session, &printer) == noErr
&& PMSessionCreatePageFormatList(session, printer, &formats) == noErr) {
CFIndex total = CFArrayGetCount(formats);
PMPageFormat tmp;
PMRect paper;
for (CFIndex idx = 0; idx < total; ++idx) {
tmp = static_cast<PMPageFormat>(
const_cast<void *>(CFArrayGetValueAtIndex(formats, idx)));
PMGetUnadjustedPaperRect(tmp, &paper);
int wMM = int((paper.right - paper.left) / 72 * 25.4 + 0.5);
int hMM = int((paper.bottom - paper.top) / 72 * 25.4 + 0.5);
if (newSize.width() == wMM && newSize.height() == hMM) {
PMCopyPageFormat(tmp, format);
// reset the orientation and resolution as they are lost in the copy.
q->setProperty(QPrintEngine::PPK_Orientation, orient);
if (PMSessionValidatePageFormat(session, format, kPMDontWantBoolean) != noErr) {
// Don't know, warn for the moment.
qWarning("QMacPrintEngine, problem setting format and resolution for this page size");
}
break;
}
}
}
}
QPrinter::PaperSize QMacPrintEnginePrivate::paperSize() const
{
if (hasCustomPaperSize)
return QPrinter::Custom;
PMRect paper;
PMGetUnadjustedPaperRect(format, &paper);
int wMM = int((paper.right - paper.left) / 72 * 25.4 + 0.5);
int hMM = int((paper.bottom - paper.top) / 72 * 25.4 + 0.5);
for (int i = QPrinter::A4; i < QPrinter::NPaperSize; ++i) {
QSize s = qt_paperSizeToQSizeF(QPrinter::PaperSize(i)).toSize();
if (s.width() == wMM && s.height() == hMM)
return (QPrinter::PaperSize)i;
}
return QPrinter::Custom;
}
QList<QVariant> QMacPrintEnginePrivate::supportedResolutions() const
{
Q_ASSERT_X(session, "QMacPrinterEngine::supportedResolutions",
"must have a valid printer session");
UInt32 resCount;
QList<QVariant> resolutions;
PMPrinter printer;
if (PMSessionGetCurrentPrinter(session, &printer) == noErr) {
PMResolution res;
OSStatus status = PMPrinterGetPrinterResolutionCount(printer, &resCount);
if (status == kPMNotImplemented) {
#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5)
// *Sigh* we have to use the non-indexed version.
if (PMPrinterGetPrinterResolution(printer, kPMMinSquareResolution, &res) == noErr)
resolutions.append(int(res.hRes));
if (PMPrinterGetPrinterResolution(printer, kPMMaxSquareResolution, &res) == noErr) {
QVariant var(int(res.hRes));
if (!resolutions.contains(var))
resolutions.append(var);
}
if (PMPrinterGetPrinterResolution(printer, kPMDefaultResolution, &res) == noErr) {
QVariant var(int(res.hRes));
if (!resolutions.contains(var))
resolutions.append(var);
}
#endif
} else if (status == noErr) {
// According to the docs, index start at 1.
for (UInt32 i = 1; i <= resCount; ++i) {
if (PMPrinterGetIndexedPrinterResolution(printer, i, &res) == noErr)
resolutions.append(QVariant(int(res.hRes)));
}
} else {
qWarning("QMacPrintEngine::supportedResolutions: Unexpected error: %ld", long(status));
}
}
return resolutions;
}
bool QMacPrintEnginePrivate::shouldSuppressStatus() const
{
if (suppressStatus == true)
return true;
// Supress displaying the automatic progress dialog if we are printing
// from a non-gui thread.
return (qApp->thread() != QThread::currentThread());
}
QPrinter::PrinterState QMacPrintEngine::printerState() const
{
return d_func()->state;
}
bool QMacPrintEngine::newPage()
{
Q_D(QMacPrintEngine);
Q_ASSERT(d->state == QPrinter::Active);
OSStatus err =
#ifndef QT_MAC_USE_COCOA
d->shouldSuppressStatus() ? PMSessionEndPageNoDialog(d->session)
: PMSessionEndPage(d->session);
#else
PMSessionEndPageNoDialog(d->session);
#endif
if (err != noErr) {
if (err == kPMCancel) {
// User canceled, we need to abort!
abort();
} else {
// Not sure what the problem is...
qWarning("QMacPrintEngine::newPage: Cannot end current page. %ld", long(err));
d->state = QPrinter::Error;
}
return false;
}
return d->newPage_helper();
}
bool QMacPrintEngine::abort()
{
Q_D(QMacPrintEngine);
if (d->state != QPrinter::Active)
return false;
bool ret = end();
d->state = QPrinter::Aborted;
return ret;
}
static inline int qt_get_PDMWidth(PMPageFormat pformat, bool fullPage,
const PMResolution &resolution)
{
int val = 0;
PMRect r;
qreal hRatio = resolution.hRes / 72;
if (fullPage) {
if (PMGetAdjustedPaperRect(pformat, &r) == noErr)
val = qRound((r.right - r.left) * hRatio);
} else {
if (PMGetAdjustedPageRect(pformat, &r) == noErr)
val = qRound((r.right - r.left) * hRatio);
}
return val;
}
static inline int qt_get_PDMHeight(PMPageFormat pformat, bool fullPage,
const PMResolution &resolution)
{
int val = 0;
PMRect r;
qreal vRatio = resolution.vRes / 72;
if (fullPage) {
if (PMGetAdjustedPaperRect(pformat, &r) == noErr)
val = qRound((r.bottom - r.top) * vRatio);
} else {
if (PMGetAdjustedPageRect(pformat, &r) == noErr)
val = qRound((r.bottom - r.top) * vRatio);
}
return val;
}
int QMacPrintEngine::metric(QPaintDevice::PaintDeviceMetric m) const
{
Q_D(const QMacPrintEngine);
int val = 1;
switch (m) {
case QPaintDevice::PdmWidth:
if (d->hasCustomPaperSize) {
val = qRound(d->customSize.width());
if (d->hasCustomPageMargins) {
val -= qRound(d->leftMargin + d->rightMargin);
} else {
QList<QVariant> margins = property(QPrintEngine::PPK_PageMargins).toList();
val -= qRound(margins.at(0).toDouble() + margins.at(2).toDouble());
}
} else {
val = qt_get_PDMWidth(d->format, property(PPK_FullPage).toBool(), d->resolution);
}
break;
case QPaintDevice::PdmHeight:
if (d->hasCustomPaperSize) {
val = qRound(d->customSize.height());
if (d->hasCustomPageMargins) {
val -= qRound(d->topMargin + d->bottomMargin);
} else {
QList<QVariant> margins = property(QPrintEngine::PPK_PageMargins).toList();
val -= qRound(margins.at(1).toDouble() + margins.at(3).toDouble());
}
} else {
val = qt_get_PDMHeight(d->format, property(PPK_FullPage).toBool(), d->resolution);
}
break;
case QPaintDevice::PdmWidthMM:
val = metric(QPaintDevice::PdmWidth);
val = int((val * 254 + 5 * d->resolution.hRes) / (10 * d->resolution.hRes));
break;
case QPaintDevice::PdmHeightMM:
val = metric(QPaintDevice::PdmHeight);
val = int((val * 254 + 5 * d->resolution.vRes) / (10 * d->resolution.vRes));
break;
case QPaintDevice::PdmPhysicalDpiX:
case QPaintDevice::PdmPhysicalDpiY: {
PMPrinter printer;
if(PMSessionGetCurrentPrinter(d->session, &printer) == noErr) {
PMResolution resolution;
#ifndef QT_MAC_USE_COCOA
# if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) {
PMPrinterGetOutputResolution(printer, d->settings, &resolution);
} else
# endif
{
PMPrinterGetPrinterResolution(printer, kPMCurrentValue, &resolution);
}
#else
PMPrinterGetOutputResolution(printer, d->settings, &resolution);
#endif
val = (int)resolution.vRes;
break;
}
//otherwise fall through
}
case QPaintDevice::PdmDpiY:
val = (int)d->resolution.vRes;
break;
case QPaintDevice::PdmDpiX:
val = (int)d->resolution.hRes;
break;
case QPaintDevice::PdmNumColors:
val = (1 << metric(QPaintDevice::PdmDepth));
break;
case QPaintDevice::PdmDepth:
val = 24;
break;
default:
val = 0;
qWarning("QPrinter::metric: Invalid metric command");
}
return val;
}
void QMacPrintEnginePrivate::initialize()
{
Q_Q(QMacPrintEngine);
#ifndef QT_MAC_USE_COCOA
Q_ASSERT(!session);
#else
Q_ASSERT(!printInfo);
#endif
if (!paintEngine)
paintEngine = new QCoreGraphicsPaintEngine();
q->gccaps = paintEngine->gccaps;
fullPage = false;
#ifndef QT_MAC_USE_COCOA
if (PMCreateSession(&session) != 0)
session = 0;
#else
QMacCocoaAutoReleasePool pool;
printInfo = [[NSPrintInfo alloc] initWithDictionary:[NSDictionary dictionary]];
session = static_cast<PMPrintSession>([printInfo PMPrintSession]);
#endif
PMPrinter printer;
if (session && PMSessionGetCurrentPrinter(session, &printer) == noErr) {
QList<QVariant> resolutions = supportedResolutions();
if (!resolutions.isEmpty() && mode != QPrinter::ScreenResolution) {
if (resolutions.count() > 1 && mode == QPrinter::HighResolution) {
int max = 0;
for (int i = 0; i < resolutions.count(); ++i) {
int value = resolutions.at(i).toInt();
if (value > max)
max = value;
}
resolution.hRes = resolution.vRes = max;
} else {
resolution.hRes = resolution.vRes = resolutions.at(0).toInt();
}
if(resolution.hRes == 0)
resolution.hRes = resolution.vRes = 600;
} else {
resolution.hRes = resolution.vRes = qt_defaultDpi();
}
}
#ifndef QT_MAC_USE_COCOA
bool settingsInitialized = (settings != 0);
bool settingsOK = !settingsInitialized ? PMCreatePrintSettings(&settings) == noErr : true;
if (settingsOK && !settingsInitialized)
settingsOK = PMSessionDefaultPrintSettings(session, settings) == noErr;
bool formatInitialized = (format != 0);
bool formatOK = !formatInitialized ? PMCreatePageFormat(&format) == noErr : true;
if (formatOK) {
if (!formatInitialized) {
formatOK = PMSessionDefaultPageFormat(session, format) == noErr;
}
formatOK = PMSessionValidatePageFormat(session, format, kPMDontWantBoolean) == noErr;
}
#else
settings = static_cast<PMPrintSettings>([printInfo PMPrintSettings]);
format = static_cast<PMPageFormat>([printInfo PMPageFormat]);
#endif
#ifndef QT_MAC_USE_COCOA
if (!settingsOK || !formatOK) {
qWarning("QMacPrintEngine::initialize: Unable to initialize QPainter");
state = QPrinter::Error;
}
#endif
QHash<QMacPrintEngine::PrintEnginePropertyKey, QVariant>::const_iterator propC;
for (propC = valueCache.constBegin(); propC != valueCache.constEnd(); propC++) {
q->setProperty(propC.key(), propC.value());
}
}
void QMacPrintEnginePrivate::releaseSession()
{
#ifndef QT_MAC_USE_COCOA
if (shouldSuppressStatus()) {
PMSessionEndPageNoDialog(session);
PMSessionEndDocumentNoDialog(session);
} else {
PMSessionEndPage(session);
PMSessionEndDocument(session);
}
PMRelease(session);
#else
PMSessionEndPageNoDialog(session);
PMSessionEndDocumentNoDialog(session);
[printInfo release];
#endif
printInfo = 0;
session = 0;
}
bool QMacPrintEnginePrivate::newPage_helper()
{
Q_Q(QMacPrintEngine);
Q_ASSERT(state == QPrinter::Active);
if (PMSessionError(session) != noErr) {
q->abort();
return false;
}
// pop the stack of saved graphic states, in case we get the same
// context back - either way, the stack count should be 0 when we
// get the new one
QCoreGraphicsPaintEngine *cgEngine = static_cast<QCoreGraphicsPaintEngine*>(paintEngine);
while (cgEngine->d_func()->stackCount > 0)
cgEngine->d_func()->restoreGraphicsState();
OSStatus status =
#ifndef QT_MAC_USE_COCOA
shouldSuppressStatus() ? PMSessionBeginPageNoDialog(session, format, 0)
: PMSessionBeginPage(session, format, 0);
#else
PMSessionBeginPageNoDialog(session, format, 0);
#endif
if(status != noErr) {
state = QPrinter::Error;
return false;
}
QRect page = q->property(QPrintEngine::PPK_PageRect).toRect();
QRect paper = q->property(QPrintEngine::PPK_PaperRect).toRect();
CGContextRef cgContext;
OSStatus err = noErr;
err = PMSessionGetCGGraphicsContext(session, &cgContext);
if(err != noErr) {
qWarning("QMacPrintEngine::newPage: Cannot retrieve CoreGraphics context: %ld", long(err));
state = QPrinter::Error;
return false;
}
cgEngine->d_func()->hd = cgContext;
// Set the resolution as a scaling ration of 72 (the default).
CGContextScaleCTM(cgContext, 72 / resolution.hRes, 72 / resolution.vRes);
CGContextScaleCTM(cgContext, 1, -1);
CGContextTranslateCTM(cgContext, 0, -paper.height());
if (!fullPage)
CGContextTranslateCTM(cgContext, page.x() - paper.x(), page.y() - paper.y());
cgEngine->d_func()->orig_xform = CGContextGetCTM(cgContext);
cgEngine->d_func()->setClip(0);
cgEngine->state->dirtyFlags = QPaintEngine::DirtyFlag(QPaintEngine::AllDirty
& ~(QPaintEngine::DirtyClipEnabled
| QPaintEngine::DirtyClipRegion
| QPaintEngine::DirtyClipPath));
if (cgEngine->painter()->hasClipping())
cgEngine->state->dirtyFlags |= QPaintEngine::DirtyClipEnabled;
cgEngine->syncState();
return true;
}
void QMacPrintEngine::updateState(const QPaintEngineState &state)
{
d_func()->paintEngine->updateState(state);
}
void QMacPrintEngine::drawRects(const QRectF *r, int num)
{
Q_D(QMacPrintEngine);
Q_ASSERT(d->state == QPrinter::Active);
d->paintEngine->drawRects(r, num);
}
void QMacPrintEngine::drawPoints(const QPointF *points, int pointCount)
{
Q_D(QMacPrintEngine);
Q_ASSERT(d->state == QPrinter::Active);
d->paintEngine->drawPoints(points, pointCount);
}
void QMacPrintEngine::drawEllipse(const QRectF &r)
{
Q_D(QMacPrintEngine);
Q_ASSERT(d->state == QPrinter::Active);
d->paintEngine->drawEllipse(r);
}
void QMacPrintEngine::drawLines(const QLineF *lines, int lineCount)
{
Q_D(QMacPrintEngine);
Q_ASSERT(d->state == QPrinter::Active);
d->paintEngine->drawLines(lines, lineCount);
}
void QMacPrintEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
{
Q_D(QMacPrintEngine);
Q_ASSERT(d->state == QPrinter::Active);
d->paintEngine->drawPolygon(points, pointCount, mode);
}
void QMacPrintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
{
Q_D(QMacPrintEngine);
Q_ASSERT(d->state == QPrinter::Active);
d->paintEngine->drawPixmap(r, pm, sr);
}
void QMacPrintEngine::drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, Qt::ImageConversionFlags flags)
{
Q_D(QMacPrintEngine);
Q_ASSERT(d->state == QPrinter::Active);
d->paintEngine->drawImage(r, pm, sr, flags);
}
void QMacPrintEngine::drawTextItem(const QPointF &p, const QTextItem &ti)
{
Q_D(QMacPrintEngine);
Q_ASSERT(d->state == QPrinter::Active);
d->paintEngine->drawTextItem(p, ti);
}
void QMacPrintEngine::drawTiledPixmap(const QRectF &dr, const QPixmap &pixmap, const QPointF &sr)
{
Q_D(QMacPrintEngine);
Q_ASSERT(d->state == QPrinter::Active);
d->paintEngine->drawTiledPixmap(dr, pixmap, sr);
}
void QMacPrintEngine::drawPath(const QPainterPath &path)
{
Q_D(QMacPrintEngine);
Q_ASSERT(d->state == QPrinter::Active);
d->paintEngine->drawPath(path);
}
void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &value)
{
Q_D(QMacPrintEngine);
d->valueCache.insert(key, value);
if (!d->session)
return;
switch (key) {
case PPK_CollateCopies:
break;
case PPK_ColorMode:
break;
case PPK_Creator:
break;
case PPK_DocumentName:
break;
case PPK_PageOrder:
break;
case PPK_PaperSource:
break;
case PPK_SelectionOption:
break;
case PPK_Resolution: {
PMPrinter printer;
UInt32 count;
if (PMSessionGetCurrentPrinter(d->session, &printer) != noErr)
break;
if (PMPrinterGetPrinterResolutionCount(printer, &count) != noErr)
break;
PMResolution resolution = { 0.0, 0.0 };
PMResolution bestResolution = { 0.0, 0.0 };
int dpi = value.toInt();
int bestDistance = INT_MAX;
for (UInt32 i = 1; i <= count; ++i) { // Yes, it starts at 1
if (PMPrinterGetIndexedPrinterResolution(printer, i, &resolution) == noErr) {
if (dpi == int(resolution.hRes)) {
bestResolution = resolution;
break;
} else {
int distance = qAbs(dpi - int(resolution.hRes));
if (distance < bestDistance) {
bestDistance = distance;
bestResolution = resolution;
}
}
}
}
PMSessionValidatePageFormat(d->session, d->format, kPMDontWantBoolean);
break;
}
case PPK_FullPage:
d->fullPage = value.toBool();
break;
case PPK_CopyCount: // fallthrough
case PPK_NumberOfCopies:
PMSetCopies(d->settings, value.toInt(), false);
break;
case PPK_Orientation: {
if (d->state == QPrinter::Active) {
qWarning("QMacPrintEngine::setOrientation: Orientation cannot be changed during a print job, ignoring change");
} else {
QPrinter::Orientation newOrientation = QPrinter::Orientation(value.toInt());
if (d->hasCustomPaperSize && (d->orient != newOrientation))
d->customSize = QSizeF(d->customSize.height(), d->customSize.width());
d->orient = newOrientation;
PMOrientation o = d->orient == QPrinter::Portrait ? kPMPortrait : kPMLandscape;
PMSetOrientation(d->format, o, false);
PMSessionValidatePageFormat(d->session, d->format, kPMDontWantBoolean);
}
break; }
case PPK_OutputFileName:
d->outputFilename = value.toString();
break;
case PPK_PaperSize:
d->setPaperSize(QPrinter::PaperSize(value.toInt()));
break;
case PPK_PrinterName: {
bool printerNameSet = false;
OSStatus status = noErr;
QCFType<CFArrayRef> printerList;
status = PMServerCreatePrinterList(kPMServerLocal, &printerList);
if (status == noErr) {
CFIndex count = CFArrayGetCount(printerList);
for (CFIndex i=0; i<count; ++i) {
PMPrinter printer = static_cast<PMPrinter>(const_cast<void *>(CFArrayGetValueAtIndex(printerList, i)));
QString name = QCFString::toQString(PMPrinterGetName(printer));
if (name == value.toString()) {
status = PMSessionSetCurrentPMPrinter(d->session, printer);
printerNameSet = true;
break;
}
}
}
if (status != noErr)
qWarning("QMacPrintEngine::setPrinterName: Error setting printer: %ld", long(status));
if (!printerNameSet) {
qWarning("QMacPrintEngine::setPrinterName: Failed to set printer named '%s'.", qPrintable(value.toString()));
d->releaseSession();
d->state = QPrinter::Idle;
}
break; }
case PPK_SuppressSystemPrintStatus:
d->suppressStatus = value.toBool();
break;
case PPK_CustomPaperSize:
{
PMOrientation orientation;
PMGetOrientation(d->format, &orientation);
d->hasCustomPaperSize = true;
d->customSize = value.toSizeF();
if (orientation != kPMPortrait)
d->customSize = QSizeF(d->customSize.height(), d->customSize.width());
break;
}
case PPK_PageMargins:
{
QList<QVariant> margins(value.toList());
Q_ASSERT(margins.size() == 4);
d->leftMargin = margins.at(0).toDouble();
d->topMargin = margins.at(1).toDouble();
d->rightMargin = margins.at(2).toDouble();
d->bottomMargin = margins.at(3).toDouble();
d->hasCustomPageMargins = true;
break;
}
default:
break;
}
}
QVariant QMacPrintEngine::property(PrintEnginePropertyKey key) const
{
Q_D(const QMacPrintEngine);
QVariant ret;
if (!d->session && d->valueCache.contains(key))
return *d->valueCache.find(key);
switch (key) {
case PPK_CollateCopies:
ret = false;
break;
case PPK_ColorMode:
ret = QPrinter::Color;
break;
case PPK_Creator:
break;
case PPK_DocumentName:
break;
case PPK_FullPage:
ret = d->fullPage;
break;
case PPK_NumberOfCopies:
ret = 1;
break;
case PPK_CopyCount: {
UInt32 copies = 1;
PMGetCopies(d->settings, &copies);
ret = (uint) copies;
break;
}
case PPK_SupportsMultipleCopies:
ret = true;
break;
case PPK_Orientation:
PMOrientation orientation;
PMGetOrientation(d->format, &orientation);
ret = orientation == kPMPortrait ? QPrinter::Portrait : QPrinter::Landscape;
break;
case PPK_OutputFileName:
ret = d->outputFilename;
break;
case PPK_PageOrder:
break;
case PPK_PaperSource:
break;
case PPK_PageRect: {
// PageRect is returned in device pixels
QRect r;
PMRect macrect, macpaper;
qreal hRatio = d->resolution.hRes / 72;
qreal vRatio = d->resolution.vRes / 72;
if (d->hasCustomPaperSize) {
r = QRect(0, 0, qRound(d->customSize.width() * hRatio), qRound(d->customSize.height() * vRatio));
if (d->hasCustomPageMargins) {
r.adjust(qRound(d->leftMargin * hRatio), qRound(d->topMargin * vRatio),
-qRound(d->rightMargin * hRatio), -qRound(d->bottomMargin * vRatio));
} else {
QList<QVariant> margins = property(QPrintEngine::PPK_PageMargins).toList();
r.adjust(qRound(margins.at(0).toDouble() * hRatio),
qRound(margins.at(1).toDouble() * vRatio),
-qRound(margins.at(2).toDouble() * hRatio),
-qRound(margins.at(3).toDouble()) * vRatio);
}
} else if (PMGetAdjustedPageRect(d->format, ¯ect) == noErr
&& PMGetAdjustedPaperRect(d->format, &macpaper) == noErr)
{
if (d->fullPage || d->hasCustomPageMargins) {
r.setCoords(int(macpaper.left * hRatio), int(macpaper.top * vRatio),
int(macpaper.right * hRatio), int(macpaper.bottom * vRatio));
r.translate(-r.x(), -r.y());
if (d->hasCustomPageMargins) {
r.adjust(qRound(d->leftMargin * hRatio), qRound(d->topMargin * vRatio),
-qRound(d->rightMargin * hRatio), -qRound(d->bottomMargin * vRatio));
}
} else {
r.setCoords(int(macrect.left * hRatio), int(macrect.top * vRatio),
int(macrect.right * hRatio), int(macrect.bottom * vRatio));
r.translate(int(-macpaper.left * hRatio), int(-macpaper.top * vRatio));
}
}
ret = r;
break; }
case PPK_PaperSize:
ret = d->paperSize();
break;
case PPK_PaperRect: {
QRect r;
PMRect macrect;
qreal hRatio = d->resolution.hRes / 72;
qreal vRatio = d->resolution.vRes / 72;
if (d->hasCustomPaperSize) {
r = QRect(0, 0, qRound(d->customSize.width() * hRatio), qRound(d->customSize.height() * vRatio));
} else if (PMGetAdjustedPaperRect(d->format, ¯ect) == noErr) {
r.setCoords(int(macrect.left * hRatio), int(macrect.top * vRatio),
int(macrect.right * hRatio), int(macrect.bottom * vRatio));
r.translate(-r.x(), -r.y());
}
ret = r;
break; }
case PPK_PrinterName: {
PMPrinter printer;
OSStatus status = PMSessionGetCurrentPrinter(d->session, &printer);
if (status != noErr)
qWarning("QMacPrintEngine::printerName: Failed getting current PMPrinter: %ld", long(status));
if (printer)
ret = QCFString::toQString(PMPrinterGetName(printer));
break; }
case PPK_Resolution: {
ret = d->resolution.hRes;
break;
}
case PPK_SupportedResolutions:
ret = d->supportedResolutions();
break;
case PPK_CustomPaperSize:
ret = d->customSize;
break;
case PPK_PageMargins:
{
QList<QVariant> margins;
if (d->hasCustomPageMargins) {
margins << d->leftMargin << d->topMargin
<< d->rightMargin << d->bottomMargin;
} else {
PMPaperMargins paperMargins;
PMPaper paper;
PMGetPageFormatPaper(d->format, &paper);
PMPaperGetMargins(paper, &paperMargins);
margins << paperMargins.left << paperMargins.top
<< paperMargins.right << paperMargins.bottom;
}
ret = margins;
break;
}
default:
break;
}
return ret;
}
QT_END_NAMESPACE
#endif // QT_NO_PRINTER
|