1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include <private/qprintengine_mac_p.h>
45 #include <QtCore/qcoreapplication.h>
51 extern QSizeF qt_paperSizeToQSizeF(QPrinter::PaperSize size);
53 QMacPrintEngine::QMacPrintEngine(QPrinter::PrinterMode mode) : QPaintEngine(*(new QMacPrintEnginePrivate))
60 bool QMacPrintEngine::begin(QPaintDevice *dev)
64 Q_ASSERT(dev && dev->devType() == QInternal::Printer);
65 if (!static_cast<QPrinter *>(dev)->isValid())
68 if (d->state == QPrinter::Idle && !d->isPrintSessionInitialized()) // Need to reinitialize
71 d->paintEngine->state = state;
72 d->paintEngine->begin(dev);
73 Q_ASSERT_X(d->state == QPrinter::Idle, "QMacPrintEngine", "printer already active");
75 if (PMSessionValidatePrintSettings(d->session, d->settings, kPMDontWantBoolean) != noErr
76 || PMSessionValidatePageFormat(d->session, d->format, kPMDontWantBoolean) != noErr) {
77 d->state = QPrinter::Error;
81 if (!d->outputFilename.isEmpty()) {
82 QCFType<CFURLRef> outFile = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault,
83 QCFString(d->outputFilename),
86 if (PMSessionSetDestination(d->session, d->settings, kPMDestinationFile,
87 kPMDocumentFormatPDF, outFile) != noErr) {
88 qWarning("QMacPrintEngine::begin: Problem setting file [%s]", d->outputFilename.toUtf8().constData());
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);
97 status = PMSessionBeginCGDocumentNoDialog(d->session, d->settings, d->format);
100 if (status != noErr) {
101 d->state = QPrinter::Error;
105 d->state = QPrinter::Active;
111 bool QMacPrintEngine::end()
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;
121 d->paintEngine->end();
122 if (d->state != QPrinter::Idle)
124 d->state = QPrinter::Idle;
129 QMacPrintEngine::paintEngine() const
131 return d_func()->paintEngine;
134 Qt::HANDLE QMacPrintEngine::handle() const
136 QCoreGraphicsPaintEngine *cgEngine = static_cast<QCoreGraphicsPaintEngine*>(paintEngine());
137 return cgEngine->d_func()->hd;
140 QMacPrintEnginePrivate::~QMacPrintEnginePrivate()
142 #ifdef QT_MAC_USE_COCOA
148 void QMacPrintEnginePrivate::setPaperSize(QPrinter::PaperSize ps)
150 Q_Q(QMacPrintEngine);
151 QSizeF newSize = qt_paperSizeToQSizeF(ps);
152 QCFType<CFArrayRef> formats;
155 if (PMSessionGetCurrentPrinter(session, &printer) == noErr
156 && PMSessionCreatePageFormatList(session, printer, &formats) == noErr) {
157 CFIndex total = CFArrayGetCount(formats);
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");
180 QPrinter::PaperSize QMacPrintEnginePrivate::paperSize() const
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;
191 return QPrinter::Custom;
194 QList<QVariant> QMacPrintEnginePrivate::supportedResolutions() const
196 Q_ASSERT_X(session, "QMacPrinterEngine::supportedResolutions",
197 "must have a valid printer session");
199 QList<QVariant> resolutions;
201 if (PMSessionGetCurrentPrinter(session, &printer) == noErr) {
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);
214 if (PMPrinterGetPrinterResolution(printer, kPMDefaultResolution, &res) == noErr) {
215 QVariant var(int(res.hRes));
216 if (!resolutions.contains(var))
217 resolutions.append(var);
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)));
227 qWarning("QMacPrintEngine::supportedResolutions: Unexpected error: %ld", long(status));
233 bool QMacPrintEnginePrivate::shouldSuppressStatus() const
235 if (suppressStatus == true)
238 // Supress displaying the automatic progress dialog if we are printing
239 // from a non-gui thread.
240 return (qApp->thread() != QThread::currentThread());
243 QPrinter::PrinterState QMacPrintEngine::printerState() const
245 return d_func()->state;
248 bool QMacPrintEngine::newPage()
250 Q_D(QMacPrintEngine);
251 Q_ASSERT(d->state == QPrinter::Active);
253 #ifndef QT_MAC_USE_COCOA
254 d->shouldSuppressStatus() ? PMSessionEndPageNoDialog(d->session)
255 : PMSessionEndPage(d->session);
257 PMSessionEndPageNoDialog(d->session);
260 if (err == kPMCancel) {
261 // User canceled, we need to abort!
264 // Not sure what the problem is...
265 qWarning("QMacPrintEngine::newPage: Cannot end current page. %ld", long(err));
266 d->state = QPrinter::Error;
270 return d->newPage_helper();
273 bool QMacPrintEngine::abort()
275 Q_D(QMacPrintEngine);
276 if (d->state != QPrinter::Active)
279 d->state = QPrinter::Aborted;
283 static inline int qt_get_PDMWidth(PMPageFormat pformat, bool fullPage,
284 const PMResolution &resolution)
288 qreal hRatio = resolution.hRes / 72;
290 if (PMGetAdjustedPaperRect(pformat, &r) == noErr)
291 val = qRound((r.right - r.left) * hRatio);
293 if (PMGetAdjustedPageRect(pformat, &r) == noErr)
294 val = qRound((r.right - r.left) * hRatio);
299 static inline int qt_get_PDMHeight(PMPageFormat pformat, bool fullPage,
300 const PMResolution &resolution)
304 qreal vRatio = resolution.vRes / 72;
306 if (PMGetAdjustedPaperRect(pformat, &r) == noErr)
307 val = qRound((r.bottom - r.top) * vRatio);
309 if (PMGetAdjustedPageRect(pformat, &r) == noErr)
310 val = qRound((r.bottom - r.top) * vRatio);
316 int QMacPrintEngine::metric(QPaintDevice::PaintDeviceMetric m) const
318 Q_D(const QMacPrintEngine);
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);
327 QList<QVariant> margins = property(QPrintEngine::PPK_PageMargins).toList();
328 val -= qRound(margins.at(0).toDouble() + margins.at(2).toDouble());
331 val = qt_get_PDMWidth(d->format, property(PPK_FullPage).toBool(), d->resolution);
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);
340 QList<QVariant> margins = property(QPrintEngine::PPK_PageMargins).toList();
341 val -= qRound(margins.at(1).toDouble() + margins.at(3).toDouble());
344 val = qt_get_PDMHeight(d->format, property(PPK_FullPage).toBool(), d->resolution);
347 case QPaintDevice::PdmWidthMM:
348 val = metric(QPaintDevice::PdmWidth);
349 val = int((val * 254 + 5 * d->resolution.hRes) / (10 * d->resolution.hRes));
351 case QPaintDevice::PdmHeightMM:
352 val = metric(QPaintDevice::PdmHeight);
353 val = int((val * 254 + 5 * d->resolution.vRes) / (10 * d->resolution.vRes));
355 case QPaintDevice::PdmPhysicalDpiX:
356 case QPaintDevice::PdmPhysicalDpiY: {
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);
367 PMPrinterGetPrinterResolution(printer, kPMCurrentValue, &resolution);
370 PMPrinterGetOutputResolution(printer, d->settings, &resolution);
372 val = (int)resolution.vRes;
375 //otherwise fall through
377 case QPaintDevice::PdmDpiY:
378 val = (int)d->resolution.vRes;
380 case QPaintDevice::PdmDpiX:
381 val = (int)d->resolution.hRes;
383 case QPaintDevice::PdmNumColors:
384 val = (1 << metric(QPaintDevice::PdmDepth));
386 case QPaintDevice::PdmDepth:
391 qWarning("QPrinter::metric: Invalid metric command");
396 void QMacPrintEnginePrivate::initialize()
398 Q_Q(QMacPrintEngine);
400 #ifndef QT_MAC_USE_COCOA
403 Q_ASSERT(!printInfo);
407 paintEngine = new QCoreGraphicsPaintEngine();
409 q->gccaps = paintEngine->gccaps;
413 #ifndef QT_MAC_USE_COCOA
414 if (PMCreateSession(&session) != 0)
417 QMacCocoaAutoReleasePool pool;
418 printInfo = [[NSPrintInfo alloc] initWithDictionary:[NSDictionary dictionary]];
419 session = static_cast<PMPrintSession>([printInfo PMPrintSession]);
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) {
428 for (int i = 0; i < resolutions.count(); ++i) {
429 int value = resolutions.at(i).toInt();
433 resolution.hRes = resolution.vRes = max;
435 resolution.hRes = resolution.vRes = resolutions.at(0).toInt();
437 if(resolution.hRes == 0)
438 resolution.hRes = resolution.vRes = 600;
440 resolution.hRes = resolution.vRes = qt_defaultDpi();
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;
451 bool formatInitialized = (format != 0);
452 bool formatOK = !formatInitialized ? PMCreatePageFormat(&format) == noErr : true;
454 if (!formatInitialized) {
455 formatOK = PMSessionDefaultPageFormat(session, format) == noErr;
457 formatOK = PMSessionValidatePageFormat(session, format, kPMDontWantBoolean) == noErr;
460 settings = static_cast<PMPrintSettings>([printInfo PMPrintSettings]);
461 format = static_cast<PMPageFormat>([printInfo PMPageFormat]);
464 #ifndef QT_MAC_USE_COCOA
465 if (!settingsOK || !formatOK) {
466 qWarning("QMacPrintEngine::initialize: Unable to initialize QPainter");
467 state = QPrinter::Error;
471 QHash<QMacPrintEngine::PrintEnginePropertyKey, QVariant>::const_iterator propC;
472 for (propC = valueCache.constBegin(); propC != valueCache.constEnd(); propC++) {
473 q->setProperty(propC.key(), propC.value());
477 void QMacPrintEnginePrivate::releaseSession()
479 #ifndef QT_MAC_USE_COCOA
480 if (shouldSuppressStatus()) {
481 PMSessionEndPageNoDialog(session);
482 PMSessionEndDocumentNoDialog(session);
484 PMSessionEndPage(session);
485 PMSessionEndDocument(session);
489 PMSessionEndPageNoDialog(session);
490 PMSessionEndDocumentNoDialog(session);
497 bool QMacPrintEnginePrivate::newPage_helper()
499 Q_Q(QMacPrintEngine);
500 Q_ASSERT(state == QPrinter::Active);
502 if (PMSessionError(session) != noErr) {
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
510 QCoreGraphicsPaintEngine *cgEngine = static_cast<QCoreGraphicsPaintEngine*>(paintEngine);
511 while (cgEngine->d_func()->stackCount > 0)
512 cgEngine->d_func()->restoreGraphicsState();
515 #ifndef QT_MAC_USE_COCOA
516 shouldSuppressStatus() ? PMSessionBeginPageNoDialog(session, format, 0)
517 : PMSessionBeginPage(session, format, 0);
519 PMSessionBeginPageNoDialog(session, format, 0);
521 if(status != noErr) {
522 state = QPrinter::Error;
526 QRect page = q->property(QPrintEngine::PPK_PageRect).toRect();
527 QRect paper = q->property(QPrintEngine::PPK_PaperRect).toRect();
529 CGContextRef cgContext;
530 OSStatus err = noErr;
531 err = PMSessionGetCGGraphicsContext(session, &cgContext);
533 qWarning("QMacPrintEngine::newPage: Cannot retrieve CoreGraphics context: %ld", long(err));
534 state = QPrinter::Error;
537 cgEngine->d_func()->hd = cgContext;
539 // Set the resolution as a scaling ration of 72 (the default).
540 CGContextScaleCTM(cgContext, 72 / resolution.hRes, 72 / resolution.vRes);
542 CGContextScaleCTM(cgContext, 1, -1);
543 CGContextTranslateCTM(cgContext, 0, -paper.height());
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();
559 void QMacPrintEngine::updateState(const QPaintEngineState &state)
561 d_func()->paintEngine->updateState(state);
564 void QMacPrintEngine::drawRects(const QRectF *r, int num)
566 Q_D(QMacPrintEngine);
567 Q_ASSERT(d->state == QPrinter::Active);
568 d->paintEngine->drawRects(r, num);
571 void QMacPrintEngine::drawPoints(const QPointF *points, int pointCount)
573 Q_D(QMacPrintEngine);
574 Q_ASSERT(d->state == QPrinter::Active);
575 d->paintEngine->drawPoints(points, pointCount);
578 void QMacPrintEngine::drawEllipse(const QRectF &r)
580 Q_D(QMacPrintEngine);
581 Q_ASSERT(d->state == QPrinter::Active);
582 d->paintEngine->drawEllipse(r);
585 void QMacPrintEngine::drawLines(const QLineF *lines, int lineCount)
587 Q_D(QMacPrintEngine);
588 Q_ASSERT(d->state == QPrinter::Active);
589 d->paintEngine->drawLines(lines, lineCount);
592 void QMacPrintEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
594 Q_D(QMacPrintEngine);
595 Q_ASSERT(d->state == QPrinter::Active);
596 d->paintEngine->drawPolygon(points, pointCount, mode);
599 void QMacPrintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
601 Q_D(QMacPrintEngine);
602 Q_ASSERT(d->state == QPrinter::Active);
603 d->paintEngine->drawPixmap(r, pm, sr);
606 void QMacPrintEngine::drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, Qt::ImageConversionFlags flags)
608 Q_D(QMacPrintEngine);
609 Q_ASSERT(d->state == QPrinter::Active);
610 d->paintEngine->drawImage(r, pm, sr, flags);
613 void QMacPrintEngine::drawTextItem(const QPointF &p, const QTextItem &ti)
615 Q_D(QMacPrintEngine);
616 Q_ASSERT(d->state == QPrinter::Active);
617 d->paintEngine->drawTextItem(p, ti);
620 void QMacPrintEngine::drawTiledPixmap(const QRectF &dr, const QPixmap &pixmap, const QPointF &sr)
622 Q_D(QMacPrintEngine);
623 Q_ASSERT(d->state == QPrinter::Active);
624 d->paintEngine->drawTiledPixmap(dr, pixmap, sr);
627 void QMacPrintEngine::drawPath(const QPainterPath &path)
629 Q_D(QMacPrintEngine);
630 Q_ASSERT(d->state == QPrinter::Active);
631 d->paintEngine->drawPath(path);
635 void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &value)
637 Q_D(QMacPrintEngine);
639 d->valueCache.insert(key, value);
644 case PPK_CollateCopies:
650 case PPK_DocumentName:
654 case PPK_PaperSource:
656 case PPK_SelectionOption:
658 case PPK_Resolution: {
661 if (PMSessionGetCurrentPrinter(d->session, &printer) != noErr)
663 if (PMPrinterGetPrinterResolutionCount(printer, &count) != noErr)
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;
675 int distance = qAbs(dpi - int(resolution.hRes));
676 if (distance < bestDistance) {
677 bestDistance = distance;
678 bestResolution = resolution;
683 PMSessionValidatePageFormat(d->session, d->format, kPMDontWantBoolean);
688 d->fullPage = value.toBool();
690 case PPK_CopyCount: // fallthrough
691 case PPK_NumberOfCopies:
692 PMSetCopies(d->settings, value.toInt(), false);
694 case PPK_Orientation: {
695 if (d->state == QPrinter::Active) {
696 qWarning("QMacPrintEngine::setOrientation: Orientation cannot be changed during a print job, ignoring change");
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);
707 case PPK_OutputFileName:
708 d->outputFilename = value.toString();
711 d->setPaperSize(QPrinter::PaperSize(value.toInt()));
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;
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()));
735 d->state = QPrinter::Idle;
738 case PPK_SuppressSystemPrintStatus:
739 d->suppressStatus = value.toBool();
741 case PPK_CustomPaperSize:
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());
751 case PPK_PageMargins:
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;
768 QVariant QMacPrintEngine::property(PrintEnginePropertyKey key) const
770 Q_D(const QMacPrintEngine);
773 if (!d->session && d->valueCache.contains(key))
774 return *d->valueCache.find(key);
777 case PPK_CollateCopies:
781 ret = QPrinter::Color;
785 case PPK_DocumentName:
790 case PPK_NumberOfCopies:
793 case PPK_CopyCount: {
795 PMGetCopies(d->settings, &copies);
799 case PPK_SupportsMultipleCopies:
802 case PPK_Orientation:
803 PMOrientation orientation;
804 PMGetOrientation(d->format, &orientation);
805 ret = orientation == kPMPortrait ? QPrinter::Portrait : QPrinter::Landscape;
807 case PPK_OutputFileName:
808 ret = d->outputFilename;
812 case PPK_PaperSource:
815 // PageRect is returned in device pixels
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));
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);
832 } else if (PMGetAdjustedPageRect(d->format, ¯ect) == noErr
833 && PMGetAdjustedPaperRect(d->format, &macpaper) == noErr)
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));
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));
852 ret = d->paperSize();
854 case PPK_PaperRect: {
857 if (d->hasCustomPaperSize) {
858 r = QRect(0, 0, qRound(d->customSize.width()), qRound(d->customSize.height()));
859 } else if (PMGetAdjustedPaperRect(d->format, ¯ect) == 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());
868 case PPK_PrinterName: {
870 OSStatus status = PMSessionGetCurrentPrinter(d->session, &printer);
872 qWarning("QMacPrintEngine::printerName: Failed getting current PMPrinter: %ld", long(status));
874 ret = QCFString::toQString(PMPrinterGetName(printer));
876 case PPK_Resolution: {
877 ret = d->resolution.hRes;
880 case PPK_SupportedResolutions:
881 ret = d->supportedResolutions();
883 case PPK_CustomPaperSize:
886 case PPK_PageMargins:
888 QList<QVariant> margins;
889 if (d->hasCustomPageMargins) {
890 margins << d->leftMargin << d->topMargin
891 << d->rightMargin << d->bottomMargin;
893 PMPaperMargins paperMargins;
895 PMGetPageFormatPaper(d->format, &paper);
896 PMPaperGetMargins(paper, &paperMargins);
897 margins << paperMargins.left << paperMargins.top
898 << paperMargins.right << paperMargins.bottom;
911 #endif // QT_NO_PRINTER