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