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