Replace 'i < len-1 && func(i+1)' by 'i+1 < len && func(i+1)'
[profile/ivi/qtbase.git] / src / gui / painting / qprintengine_mac.mm
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 #ifndef QT_MAC_USE_COCOA
94     status = d->shouldSuppressStatus() ? PMSessionBeginCGDocumentNoDialog(d->session, d->settings, d->format)
95                                        : PMSessionBeginCGDocument(d->session, d->settings, d->format);
96 #else
97     status = PMSessionBeginCGDocumentNoDialog(d->session, d->settings, d->format);
98 #endif
99
100     if (status != noErr) {
101         d->state = QPrinter::Error;
102         return false;
103     }
104
105     d->state = QPrinter::Active;
106     setActive(true);
107     d->newPage_helper();
108     return true;
109 }
110
111 bool QMacPrintEngine::end()
112 {
113     Q_D(QMacPrintEngine);
114     if (d->state == QPrinter::Aborted)
115         return true;  // I was just here a function call ago :)
116     if(d->paintEngine->type() == QPaintEngine::CoreGraphics) {
117         // We dont need the paint engine to call restoreGraphicsState()
118         static_cast<QCoreGraphicsPaintEngine*>(d->paintEngine)->d_func()->stackCount = 0;
119         static_cast<QCoreGraphicsPaintEngine*>(d->paintEngine)->d_func()->hd = 0;
120     }
121     d->paintEngine->end();
122     if (d->state != QPrinter::Idle)
123         d->releaseSession();
124     d->state  = QPrinter::Idle;
125     return true;
126 }
127
128 QPaintEngine *
129 QMacPrintEngine::paintEngine() const
130 {
131     return d_func()->paintEngine;
132 }
133
134 Qt::HANDLE QMacPrintEngine::handle() const
135 {
136     QCoreGraphicsPaintEngine *cgEngine = static_cast<QCoreGraphicsPaintEngine*>(paintEngine());
137     return cgEngine->d_func()->hd;
138 }
139
140 QMacPrintEnginePrivate::~QMacPrintEnginePrivate()
141 {
142 #ifdef QT_MAC_USE_COCOA
143     [printInfo release];
144 #endif
145     delete paintEngine;
146 }
147
148 void QMacPrintEnginePrivate::setPaperSize(QPrinter::PaperSize ps)
149 {
150     Q_Q(QMacPrintEngine);
151     QSizeF newSize = qt_paperSizeToQSizeF(ps);
152     QCFType<CFArrayRef> formats;
153     PMPrinter printer;
154
155     if (PMSessionGetCurrentPrinter(session, &printer) == noErr
156         && PMSessionCreatePageFormatList(session, printer, &formats) == noErr) {
157         CFIndex total = CFArrayGetCount(formats);
158         PMPageFormat tmp;
159         PMRect paper;
160         for (CFIndex idx = 0; idx < total; ++idx) {
161             tmp = static_cast<PMPageFormat>(
162                                         const_cast<void *>(CFArrayGetValueAtIndex(formats, idx)));
163             PMGetUnadjustedPaperRect(tmp, &paper);
164             int wMM = int((paper.right - paper.left) / 72 * 25.4 + 0.5);
165             int hMM = int((paper.bottom - paper.top) / 72 * 25.4 + 0.5);
166             if (newSize.width() == wMM && newSize.height() == hMM) {
167                 PMCopyPageFormat(tmp, format);
168                 // reset the orientation and resolution as they are lost in the copy.
169                 q->setProperty(QPrintEngine::PPK_Orientation, orient);
170                 if (PMSessionValidatePageFormat(session, format, kPMDontWantBoolean) != noErr) {
171                     // Don't know, warn for the moment.
172                     qWarning("QMacPrintEngine, problem setting format and resolution for this page size");
173                 }
174                 break;
175             }
176         }
177     }
178 }
179
180 QPrinter::PaperSize QMacPrintEnginePrivate::paperSize() const
181 {
182     PMRect paper;
183     PMGetUnadjustedPaperRect(format, &paper);
184     int wMM = int((paper.right - paper.left) / 72 * 25.4 + 0.5);
185     int hMM = int((paper.bottom - paper.top) / 72 * 25.4 + 0.5);
186     for (int i = QPrinter::A4; i < QPrinter::NPaperSize; ++i) {
187         QSizeF s = qt_paperSizeToQSizeF(QPrinter::PaperSize(i));
188         if (s.width() == wMM && s.height() == hMM)
189             return (QPrinter::PaperSize)i;
190     }
191     return QPrinter::Custom;
192 }
193
194 QList<QVariant> QMacPrintEnginePrivate::supportedResolutions() const
195 {
196     Q_ASSERT_X(session, "QMacPrinterEngine::supportedResolutions",
197                "must have a valid printer session");
198     UInt32 resCount;
199     QList<QVariant> resolutions;
200     PMPrinter printer;
201     if (PMSessionGetCurrentPrinter(session, &printer) == noErr) {
202         PMResolution res;
203         OSStatus status = PMPrinterGetPrinterResolutionCount(printer, &resCount);
204         if (status  == kPMNotImplemented) {
205 #if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5)
206             // *Sigh* we have to use the non-indexed version.
207             if (PMPrinterGetPrinterResolution(printer, kPMMinSquareResolution, &res) == noErr)
208                 resolutions.append(int(res.hRes));
209             if (PMPrinterGetPrinterResolution(printer, kPMMaxSquareResolution, &res) == noErr) {
210                 QVariant var(int(res.hRes));
211                 if (!resolutions.contains(var))
212                     resolutions.append(var);
213             }
214             if (PMPrinterGetPrinterResolution(printer, kPMDefaultResolution, &res) == noErr) {
215                 QVariant var(int(res.hRes));
216                 if (!resolutions.contains(var))
217                     resolutions.append(var);
218             }
219 #endif
220         } else if (status == noErr) {
221             // According to the docs, index start at 1.
222             for (UInt32 i = 1; i <= resCount; ++i) {
223                 if (PMPrinterGetIndexedPrinterResolution(printer, i, &res) == noErr)
224                     resolutions.append(QVariant(int(res.hRes)));
225             }
226         } else {
227             qWarning("QMacPrintEngine::supportedResolutions: Unexpected error: %ld", long(status));
228         }
229     }
230     return resolutions;
231 }
232
233 bool QMacPrintEnginePrivate::shouldSuppressStatus() const
234 {
235     if (suppressStatus == true)
236         return true;
237
238     // Supress displaying the automatic progress dialog if we are printing
239     // from a non-gui thread.
240     return (qApp->thread() != QThread::currentThread());
241 }
242
243 QPrinter::PrinterState QMacPrintEngine::printerState() const
244 {
245     return d_func()->state;
246 }
247
248 bool QMacPrintEngine::newPage()
249 {
250     Q_D(QMacPrintEngine);
251     Q_ASSERT(d->state == QPrinter::Active);
252     OSStatus err =
253 #ifndef QT_MAC_USE_COCOA
254     d->shouldSuppressStatus() ? PMSessionEndPageNoDialog(d->session)
255                               : PMSessionEndPage(d->session);
256 #else
257     PMSessionEndPageNoDialog(d->session);
258 #endif
259     if (err != noErr)  {
260         if (err == kPMCancel) {
261             // User canceled, we need to abort!
262             abort();
263         } else {
264             // Not sure what the problem is...
265             qWarning("QMacPrintEngine::newPage: Cannot end current page. %ld", long(err));
266             d->state = QPrinter::Error;
267         }
268         return false;
269     }
270     return d->newPage_helper();
271 }
272
273 bool QMacPrintEngine::abort()
274 {
275     Q_D(QMacPrintEngine);
276     if (d->state != QPrinter::Active)
277         return false;
278     bool ret = end();
279     d->state = QPrinter::Aborted;
280     return ret;
281 }
282
283 static inline int qt_get_PDMWidth(PMPageFormat pformat, bool fullPage,
284                                   const PMResolution &resolution)
285 {
286     int val = 0;
287     PMRect r;
288     qreal hRatio = resolution.hRes / 72;
289     if (fullPage) {
290         if (PMGetAdjustedPaperRect(pformat, &r) == noErr)
291             val = qRound((r.right - r.left) * hRatio);
292     } else {
293         if (PMGetAdjustedPageRect(pformat, &r) == noErr)
294             val = qRound((r.right - r.left) * hRatio);
295     }
296     return val;
297 }
298
299 static inline int qt_get_PDMHeight(PMPageFormat pformat, bool fullPage,
300                                    const PMResolution &resolution)
301 {
302     int val = 0;
303     PMRect r;
304     qreal vRatio = resolution.vRes / 72;
305     if (fullPage) {
306         if (PMGetAdjustedPaperRect(pformat, &r) == noErr)
307             val = qRound((r.bottom - r.top) * vRatio);
308     } else {
309         if (PMGetAdjustedPageRect(pformat, &r) == noErr)
310             val = qRound((r.bottom - r.top) * vRatio);
311     }
312     return val;
313 }
314
315
316 int QMacPrintEngine::metric(QPaintDevice::PaintDeviceMetric m) const
317 {
318     Q_D(const QMacPrintEngine);
319     int val = 1;
320     switch (m) {
321     case QPaintDevice::PdmWidth:
322         if (d->hasCustomPaperSize) {
323             val = qRound(d->customSize.width());
324             if (d->hasCustomPageMargins) {
325                 val -= qRound(d->leftMargin + d->rightMargin);
326             } else {
327                 QList<QVariant> margins = property(QPrintEngine::PPK_PageMargins).toList();
328                 val -= qRound(margins.at(0).toDouble() + margins.at(2).toDouble());
329             }
330         } else {
331             val = qt_get_PDMWidth(d->format, property(PPK_FullPage).toBool(), d->resolution);
332         }
333         break;
334     case QPaintDevice::PdmHeight:
335         if (d->hasCustomPaperSize) {
336             val = qRound(d->customSize.height());
337             if (d->hasCustomPageMargins) {
338                 val -= qRound(d->topMargin + d->bottomMargin);
339             } else {
340                 QList<QVariant> margins = property(QPrintEngine::PPK_PageMargins).toList();
341                 val -= qRound(margins.at(1).toDouble() + margins.at(3).toDouble());
342             }
343         } else {
344             val = qt_get_PDMHeight(d->format, property(PPK_FullPage).toBool(), d->resolution);
345         }
346         break;
347     case QPaintDevice::PdmWidthMM:
348         val = metric(QPaintDevice::PdmWidth);
349         val = int((val * 254 + 5 * d->resolution.hRes) / (10 * d->resolution.hRes));
350         break;
351     case QPaintDevice::PdmHeightMM:
352         val = metric(QPaintDevice::PdmHeight);
353         val = int((val * 254 + 5 * d->resolution.vRes) / (10 * d->resolution.vRes));
354         break;
355     case QPaintDevice::PdmPhysicalDpiX:
356     case QPaintDevice::PdmPhysicalDpiY: {
357         PMPrinter printer;
358         if(PMSessionGetCurrentPrinter(d->session, &printer) == noErr) {
359             PMResolution resolution;
360 #ifndef QT_MAC_USE_COCOA
361 #  if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
362             if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) {
363                 PMPrinterGetOutputResolution(printer, d->settings, &resolution);
364             } else
365 #  endif
366             {
367                 PMPrinterGetPrinterResolution(printer, kPMCurrentValue, &resolution);
368             }
369 #else
370             PMPrinterGetOutputResolution(printer, d->settings, &resolution);
371 #endif
372             val = (int)resolution.vRes;
373             break;
374         }
375         //otherwise fall through
376     }
377     case QPaintDevice::PdmDpiY:
378         val = (int)d->resolution.vRes;
379         break;
380     case QPaintDevice::PdmDpiX:
381         val = (int)d->resolution.hRes;
382         break;
383     case QPaintDevice::PdmNumColors:
384         val = (1 << metric(QPaintDevice::PdmDepth));
385         break;
386     case QPaintDevice::PdmDepth:
387         val = 24;
388         break;
389     default:
390         val = 0;
391         qWarning("QPrinter::metric: Invalid metric command");
392     }
393     return val;
394 }
395
396 void QMacPrintEnginePrivate::initialize()
397 {
398     Q_Q(QMacPrintEngine);
399
400 #ifndef QT_MAC_USE_COCOA
401     Q_ASSERT(!session);
402 #else
403     Q_ASSERT(!printInfo);
404 #endif
405
406     if (!paintEngine)
407         paintEngine = new QCoreGraphicsPaintEngine();
408
409     q->gccaps = paintEngine->gccaps;
410
411     fullPage = false;
412
413 #ifndef QT_MAC_USE_COCOA
414     if (PMCreateSession(&session) != 0)
415         session = 0;
416 #else
417     QMacCocoaAutoReleasePool pool;
418     printInfo = [[NSPrintInfo alloc] initWithDictionary:[NSDictionary dictionary]];
419     session = static_cast<PMPrintSession>([printInfo PMPrintSession]);
420 #endif
421
422     PMPrinter printer;
423     if (session && PMSessionGetCurrentPrinter(session, &printer) == noErr) {
424         QList<QVariant> resolutions = supportedResolutions();
425         if (!resolutions.isEmpty() && mode != QPrinter::ScreenResolution) {
426             if (resolutions.count() > 1 && mode == QPrinter::HighResolution) {
427                 int max = 0;
428                 for (int i = 0; i < resolutions.count(); ++i) {
429                     int value = resolutions.at(i).toInt();
430                     if (value > max)
431                         max = value;
432                 }
433                 resolution.hRes = resolution.vRes = max;
434             } else {
435                 resolution.hRes = resolution.vRes = resolutions.at(0).toInt();
436             }
437             if(resolution.hRes == 0)
438                 resolution.hRes = resolution.vRes = 600;
439         } else {
440             resolution.hRes = resolution.vRes = qt_defaultDpi();
441         }
442     }
443
444 #ifndef QT_MAC_USE_COCOA
445     bool settingsInitialized = (settings != 0);
446     bool settingsOK = !settingsInitialized ? PMCreatePrintSettings(&settings) == noErr : true;
447     if (settingsOK && !settingsInitialized)
448         settingsOK = PMSessionDefaultPrintSettings(session, settings) == noErr;
449
450
451     bool formatInitialized = (format != 0);
452     bool formatOK = !formatInitialized ? PMCreatePageFormat(&format) == noErr : true;
453     if (formatOK) {
454         if (!formatInitialized) {
455             formatOK = PMSessionDefaultPageFormat(session, format) == noErr;
456         }
457         formatOK = PMSessionValidatePageFormat(session, format, kPMDontWantBoolean) == noErr;
458     }
459 #else
460     settings = static_cast<PMPrintSettings>([printInfo PMPrintSettings]);
461     format = static_cast<PMPageFormat>([printInfo PMPageFormat]);
462 #endif
463
464 #ifndef QT_MAC_USE_COCOA
465     if (!settingsOK || !formatOK) {
466         qWarning("QMacPrintEngine::initialize: Unable to initialize QPainter");
467         state = QPrinter::Error;
468     }
469 #endif
470
471     QHash<QMacPrintEngine::PrintEnginePropertyKey, QVariant>::const_iterator propC;
472     for (propC = valueCache.constBegin(); propC != valueCache.constEnd(); propC++) {
473         q->setProperty(propC.key(), propC.value());
474     }
475 }
476
477 void QMacPrintEnginePrivate::releaseSession()
478 {
479 #ifndef QT_MAC_USE_COCOA
480     if (shouldSuppressStatus()) {
481         PMSessionEndPageNoDialog(session);
482         PMSessionEndDocumentNoDialog(session);
483     } else {
484         PMSessionEndPage(session);
485         PMSessionEndDocument(session);
486     }
487     PMRelease(session);
488 #else
489     PMSessionEndPageNoDialog(session);
490     PMSessionEndDocumentNoDialog(session);
491     [printInfo release];
492 #endif
493     printInfo = 0;
494     session = 0;
495 }
496
497 bool QMacPrintEnginePrivate::newPage_helper()
498 {
499     Q_Q(QMacPrintEngine);
500     Q_ASSERT(state == QPrinter::Active);
501
502     if (PMSessionError(session) != noErr) {
503         q->abort();
504         return false;
505     }
506
507     // pop the stack of saved graphic states, in case we get the same
508     // context back - either way, the stack count should be 0 when we
509     // get the new one
510     QCoreGraphicsPaintEngine *cgEngine = static_cast<QCoreGraphicsPaintEngine*>(paintEngine);
511     while (cgEngine->d_func()->stackCount > 0)
512         cgEngine->d_func()->restoreGraphicsState();
513
514     OSStatus status =
515 #ifndef QT_MAC_USE_COCOA
516         shouldSuppressStatus() ? PMSessionBeginPageNoDialog(session, format, 0)
517                                : PMSessionBeginPage(session, format, 0);
518 #else
519         PMSessionBeginPageNoDialog(session, format, 0);
520 #endif
521     if(status != noErr) {
522         state = QPrinter::Error;
523         return false;
524     }
525
526     QRect page = q->property(QPrintEngine::PPK_PageRect).toRect();
527     QRect paper = q->property(QPrintEngine::PPK_PaperRect).toRect();
528
529     CGContextRef cgContext;
530     OSStatus err = noErr;
531     err = PMSessionGetCGGraphicsContext(session, &cgContext);
532     if(err != noErr) {
533         qWarning("QMacPrintEngine::newPage: Cannot retrieve CoreGraphics context: %ld", long(err));
534         state = QPrinter::Error;
535         return false;
536     }
537     cgEngine->d_func()->hd = cgContext;
538
539     // Set the resolution as a scaling ration of 72 (the default).
540     CGContextScaleCTM(cgContext, 72 / resolution.hRes, 72 / resolution.vRes);
541
542     CGContextScaleCTM(cgContext, 1, -1);
543     CGContextTranslateCTM(cgContext, 0, -paper.height());
544     if (!fullPage)
545         CGContextTranslateCTM(cgContext, page.x() - paper.x(), page.y() - paper.y());
546     cgEngine->d_func()->orig_xform = CGContextGetCTM(cgContext);
547     cgEngine->d_func()->setClip(0);
548     cgEngine->state->dirtyFlags = QPaintEngine::DirtyFlag(QPaintEngine::AllDirty
549                                                           & ~(QPaintEngine::DirtyClipEnabled
550                                                               | QPaintEngine::DirtyClipRegion
551                                                               | QPaintEngine::DirtyClipPath));
552     if (cgEngine->painter()->hasClipping())
553         cgEngine->state->dirtyFlags |= QPaintEngine::DirtyClipEnabled;
554     cgEngine->syncState();
555     return true;
556 }
557
558
559 void QMacPrintEngine::updateState(const QPaintEngineState &state)
560 {
561     d_func()->paintEngine->updateState(state);
562 }
563
564 void QMacPrintEngine::drawRects(const QRectF *r, int num)
565 {
566     Q_D(QMacPrintEngine);
567     Q_ASSERT(d->state == QPrinter::Active);
568     d->paintEngine->drawRects(r, num);
569 }
570
571 void QMacPrintEngine::drawPoints(const QPointF *points, int pointCount)
572 {
573     Q_D(QMacPrintEngine);
574     Q_ASSERT(d->state == QPrinter::Active);
575     d->paintEngine->drawPoints(points, pointCount);
576 }
577
578 void QMacPrintEngine::drawEllipse(const QRectF &r)
579 {
580     Q_D(QMacPrintEngine);
581     Q_ASSERT(d->state == QPrinter::Active);
582     d->paintEngine->drawEllipse(r);
583 }
584
585 void QMacPrintEngine::drawLines(const QLineF *lines, int lineCount)
586 {
587     Q_D(QMacPrintEngine);
588     Q_ASSERT(d->state == QPrinter::Active);
589     d->paintEngine->drawLines(lines, lineCount);
590 }
591
592 void QMacPrintEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
593 {
594     Q_D(QMacPrintEngine);
595     Q_ASSERT(d->state == QPrinter::Active);
596     d->paintEngine->drawPolygon(points, pointCount, mode);
597 }
598
599 void QMacPrintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
600 {
601     Q_D(QMacPrintEngine);
602     Q_ASSERT(d->state == QPrinter::Active);
603     d->paintEngine->drawPixmap(r, pm, sr);
604 }
605
606 void QMacPrintEngine::drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, Qt::ImageConversionFlags flags)
607 {
608     Q_D(QMacPrintEngine);
609     Q_ASSERT(d->state == QPrinter::Active);
610     d->paintEngine->drawImage(r, pm, sr, flags);
611 }
612
613 void QMacPrintEngine::drawTextItem(const QPointF &p, const QTextItem &ti)
614 {
615     Q_D(QMacPrintEngine);
616     Q_ASSERT(d->state == QPrinter::Active);
617     d->paintEngine->drawTextItem(p, ti);
618 }
619
620 void QMacPrintEngine::drawTiledPixmap(const QRectF &dr, const QPixmap &pixmap, const QPointF &sr)
621 {
622     Q_D(QMacPrintEngine);
623     Q_ASSERT(d->state == QPrinter::Active);
624     d->paintEngine->drawTiledPixmap(dr, pixmap, sr);
625 }
626
627 void QMacPrintEngine::drawPath(const QPainterPath &path)
628 {
629     Q_D(QMacPrintEngine);
630     Q_ASSERT(d->state == QPrinter::Active);
631     d->paintEngine->drawPath(path);
632 }
633
634
635 void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &value)
636 {
637     Q_D(QMacPrintEngine);
638
639     d->valueCache.insert(key, value);
640     if (!d->session)
641         return;
642
643     switch (key) {
644     case PPK_CollateCopies:
645         break;
646     case PPK_ColorMode:
647         break;
648     case PPK_Creator:
649         break;
650     case PPK_DocumentName:
651         break;
652     case PPK_PageOrder:
653         break;
654     case PPK_PaperSource:
655         break;
656     case PPK_SelectionOption:
657         break;
658     case PPK_Resolution:  {
659         PMPrinter printer;
660         UInt32 count;
661         if (PMSessionGetCurrentPrinter(d->session, &printer) != noErr)
662             break;
663         if (PMPrinterGetPrinterResolutionCount(printer, &count) != noErr)
664             break;
665         PMResolution resolution = { 0.0, 0.0 };
666         PMResolution bestResolution = { 0.0, 0.0 };
667         int dpi = value.toInt();
668         int bestDistance = INT_MAX;
669         for (UInt32 i = 1; i <= count; ++i) {  // Yes, it starts at 1
670             if (PMPrinterGetIndexedPrinterResolution(printer, i, &resolution) == noErr) {
671                 if (dpi == int(resolution.hRes)) {
672                     bestResolution = resolution;
673                     break;
674                 } else {
675                     int distance = qAbs(dpi - int(resolution.hRes));
676                     if (distance < bestDistance) {
677                         bestDistance = distance;
678                         bestResolution = resolution;
679                     }
680                 }
681             }
682         }
683         PMSessionValidatePageFormat(d->session, d->format, kPMDontWantBoolean);
684         break;
685     }
686
687     case PPK_FullPage:
688         d->fullPage = value.toBool();
689         break;
690     case PPK_CopyCount: // fallthrough
691     case PPK_NumberOfCopies:
692         PMSetCopies(d->settings, value.toInt(), false);
693         break;
694     case PPK_Orientation: {
695         if (d->state == QPrinter::Active) {
696             qWarning("QMacPrintEngine::setOrientation: Orientation cannot be changed during a print job, ignoring change");
697         } else {
698             QPrinter::Orientation newOrientation = QPrinter::Orientation(value.toInt());
699             if (d->hasCustomPaperSize && (d->orient != newOrientation))
700                 d->customSize = QSizeF(d->customSize.height(), d->customSize.width());
701             d->orient = newOrientation;
702             PMOrientation o = d->orient == QPrinter::Portrait ? kPMPortrait : kPMLandscape;
703             PMSetOrientation(d->format, o, false);
704             PMSessionValidatePageFormat(d->session, d->format, kPMDontWantBoolean);
705         }
706         break; }
707     case PPK_OutputFileName:
708         d->outputFilename = value.toString();
709         break;
710     case PPK_PaperSize:
711         d->setPaperSize(QPrinter::PaperSize(value.toInt()));
712         break;
713     case PPK_PrinterName: {
714         bool printerNameSet = false;
715         OSStatus status = noErr;
716         QCFType<CFArrayRef> printerList;
717         status = PMServerCreatePrinterList(kPMServerLocal, &printerList);
718         if (status == noErr) {
719             CFIndex count = CFArrayGetCount(printerList);
720             for (CFIndex i=0; i<count; ++i) {
721                 PMPrinter printer = static_cast<PMPrinter>(const_cast<void *>(CFArrayGetValueAtIndex(printerList, i)));
722                 QString name = QCFString::toQString(PMPrinterGetName(printer));
723                 if (name == value.toString()) {
724                     status = PMSessionSetCurrentPMPrinter(d->session, printer);
725                     printerNameSet = true;
726                     break;
727                 }
728             }
729         }
730         if (status != noErr)
731             qWarning("QMacPrintEngine::setPrinterName: Error setting printer: %ld", long(status));
732         if (!printerNameSet) {
733             qWarning("QMacPrintEngine::setPrinterName: Failed to set printer named '%s'.", qPrintable(value.toString()));
734             d->releaseSession();
735             d->state = QPrinter::Idle;
736         }
737         break; }
738     case PPK_SuppressSystemPrintStatus:
739         d->suppressStatus = value.toBool();
740         break;
741     case PPK_CustomPaperSize:
742     {
743         PMOrientation orientation;
744         PMGetOrientation(d->format, &orientation);
745         d->hasCustomPaperSize = true;
746         d->customSize = value.toSizeF();
747         if (orientation != kPMPortrait)
748             d->customSize = QSizeF(d->customSize.height(), d->customSize.width());
749         break;
750     }
751     case PPK_PageMargins:
752     {
753         QList<QVariant> margins(value.toList());
754         Q_ASSERT(margins.size() == 4);
755         d->leftMargin = margins.at(0).toDouble();
756         d->topMargin = margins.at(1).toDouble();
757         d->rightMargin = margins.at(2).toDouble();
758         d->bottomMargin = margins.at(3).toDouble();
759         d->hasCustomPageMargins = true;
760         break;
761     }
762
763     default:
764         break;
765     }
766 }
767
768 QVariant QMacPrintEngine::property(PrintEnginePropertyKey key) const
769 {
770     Q_D(const QMacPrintEngine);
771     QVariant ret;
772
773     if (!d->session && d->valueCache.contains(key))
774         return *d->valueCache.find(key);
775
776     switch (key) {
777     case PPK_CollateCopies:
778         ret = false;
779         break;
780     case PPK_ColorMode:
781         ret = QPrinter::Color;
782         break;
783     case PPK_Creator:
784         break;
785     case PPK_DocumentName:
786         break;
787     case PPK_FullPage:
788         ret = d->fullPage;
789         break;
790     case PPK_NumberOfCopies:
791         ret = 1;
792         break;
793     case PPK_CopyCount: {
794         UInt32 copies = 1;
795         PMGetCopies(d->settings, &copies);
796         ret = (uint) copies;
797         break;
798     }
799     case PPK_SupportsMultipleCopies:
800         ret = true;
801         break;
802     case PPK_Orientation:
803         PMOrientation orientation;
804         PMGetOrientation(d->format, &orientation);
805         ret = orientation == kPMPortrait ? QPrinter::Portrait : QPrinter::Landscape;
806         break;
807     case PPK_OutputFileName:
808         ret = d->outputFilename;
809         break;
810     case PPK_PageOrder:
811         break;
812     case PPK_PaperSource:
813         break;
814     case PPK_PageRect: {
815         // PageRect is returned in device pixels
816         QRect r;
817         PMRect macrect, macpaper;
818         qreal hRatio = d->resolution.hRes / 72;
819         qreal vRatio = d->resolution.vRes / 72;
820         if (d->hasCustomPaperSize) {
821             r = QRect(0, 0, qRound(d->customSize.width() * hRatio), qRound(d->customSize.height() * vRatio));
822             if (d->hasCustomPageMargins) {
823                 r.adjust(qRound(d->leftMargin * hRatio), qRound(d->topMargin * vRatio), 
824                          -qRound(d->rightMargin * hRatio), -qRound(d->bottomMargin * vRatio));
825             } else {
826                 QList<QVariant> margins = property(QPrintEngine::PPK_PageMargins).toList();
827                 r.adjust(qRound(margins.at(0).toDouble() * hRatio),
828                          qRound(margins.at(1).toDouble() * vRatio),
829                          -qRound(margins.at(2).toDouble() * hRatio),
830                          -qRound(margins.at(3).toDouble()) * vRatio);
831             }
832         } else if (PMGetAdjustedPageRect(d->format, &macrect) == noErr
833                    && PMGetAdjustedPaperRect(d->format, &macpaper) == noErr)
834         {
835             if (d->fullPage || d->hasCustomPageMargins) {
836                 r.setCoords(int(macpaper.left * hRatio), int(macpaper.top * vRatio),
837                             int(macpaper.right * hRatio), int(macpaper.bottom * vRatio));
838                 r.translate(-r.x(), -r.y());
839                 if (d->hasCustomPageMargins) {
840                     r.adjust(qRound(d->leftMargin * hRatio), qRound(d->topMargin * vRatio),
841                              -qRound(d->rightMargin * hRatio), -qRound(d->bottomMargin * vRatio));
842                 }
843             } else {
844                 r.setCoords(int(macrect.left * hRatio), int(macrect.top * vRatio),
845                             int(macrect.right * hRatio), int(macrect.bottom * vRatio));
846                 r.translate(int(-macpaper.left * hRatio), int(-macpaper.top * vRatio));
847             } 
848         }
849         ret = r;
850         break; }
851     case PPK_PaperSize:
852         ret = d->paperSize();
853         break;
854     case PPK_PaperRect: {
855         QRect r;
856         PMRect macrect;
857         if (d->hasCustomPaperSize) {
858             r = QRect(0, 0, qRound(d->customSize.width()), qRound(d->customSize.height()));
859         } else if (PMGetAdjustedPaperRect(d->format, &macrect) == noErr) {
860             qreal hRatio = d->resolution.hRes / 72;
861             qreal vRatio = d->resolution.vRes / 72;
862             r.setCoords(int(macrect.left * hRatio), int(macrect.top * vRatio),
863                         int(macrect.right * hRatio), int(macrect.bottom * vRatio));
864             r.translate(-r.x(), -r.y());
865         }
866         ret = r;
867         break; }
868     case PPK_PrinterName: {
869         PMPrinter printer;
870         OSStatus status = PMSessionGetCurrentPrinter(d->session, &printer);
871         if (status != noErr)
872             qWarning("QMacPrintEngine::printerName: Failed getting current PMPrinter: %ld", long(status));
873         if (printer)
874             ret = QCFString::toQString(PMPrinterGetName(printer));
875         break; }
876     case PPK_Resolution: {
877         ret = d->resolution.hRes;
878         break;
879     }
880     case PPK_SupportedResolutions:
881         ret = d->supportedResolutions();
882         break;
883     case PPK_CustomPaperSize:
884         ret = d->customSize;
885         break;
886     case PPK_PageMargins:
887     {
888         QList<QVariant> margins;
889         if (d->hasCustomPageMargins) {
890             margins << d->leftMargin << d->topMargin
891                     << d->rightMargin << d->bottomMargin;
892         } else {
893             PMPaperMargins paperMargins;
894             PMPaper paper;
895             PMGetPageFormatPaper(d->format, &paper);
896             PMPaperGetMargins(paper, &paperMargins);
897             margins << paperMargins.left << paperMargins.top
898                     << paperMargins.right << paperMargins.bottom;
899         }
900         ret = margins;
901         break;
902     }
903     default:
904         break;
905     }
906     return ret;
907 }
908
909 QT_END_NAMESPACE
910
911 #endif // QT_NO_PRINTER