1 /****************************************************************************
3 ** Copyright (C) 2012 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 status = PMSessionBeginCGDocumentNoDialog(d->session, d->settings, d->format);
95 if (status != noErr) {
96 d->state = QPrinter::Error;
100 d->state = QPrinter::Active;
106 bool QMacPrintEngine::end()
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;
116 d->paintEngine->end();
117 if (d->state != QPrinter::Idle)
119 d->state = QPrinter::Idle;
124 QMacPrintEngine::paintEngine() const
126 return d_func()->paintEngine;
129 Qt::HANDLE QMacPrintEngine::handle() const
131 QCoreGraphicsPaintEngine *cgEngine = static_cast<QCoreGraphicsPaintEngine*>(paintEngine());
132 return cgEngine->d_func()->hd;
135 QMacPrintEnginePrivate::~QMacPrintEnginePrivate()
141 void QMacPrintEnginePrivate::setPaperSize(QPrinter::PaperSize ps)
143 Q_Q(QMacPrintEngine);
144 QSizeF newSize = qt_paperSizeToQSizeF(ps);
145 QCFType<CFArrayRef> formats;
148 if (PMSessionGetCurrentPrinter(session, &printer) == noErr
149 && PMSessionCreatePageFormatList(session, printer, &formats) == noErr) {
150 CFIndex total = CFArrayGetCount(formats);
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");
173 QPrinter::PaperSize QMacPrintEnginePrivate::paperSize() const
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;
184 return QPrinter::Custom;
187 QList<QVariant> QMacPrintEnginePrivate::supportedResolutions() const
189 Q_ASSERT_X(session, "QMacPrinterEngine::supportedResolutions",
190 "must have a valid printer session");
192 QList<QVariant> resolutions;
194 if (PMSessionGetCurrentPrinter(session, &printer) == noErr) {
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);
207 if (PMPrinterGetPrinterResolution(printer, kPMDefaultResolution, &res) == noErr) {
208 QVariant var(int(res.hRes));
209 if (!resolutions.contains(var))
210 resolutions.append(var);
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)));
220 qWarning("QMacPrintEngine::supportedResolutions: Unexpected error: %ld", long(status));
226 bool QMacPrintEnginePrivate::shouldSuppressStatus() const
228 if (suppressStatus == true)
231 // Supress displaying the automatic progress dialog if we are printing
232 // from a non-gui thread.
233 return (qApp->thread() != QThread::currentThread());
236 QPrinter::PrinterState QMacPrintEngine::printerState() const
238 return d_func()->state;
241 bool QMacPrintEngine::newPage()
243 Q_D(QMacPrintEngine);
244 Q_ASSERT(d->state == QPrinter::Active);
246 PMSessionEndPageNoDialog(d->session);
248 if (err == kPMCancel) {
249 // User canceled, we need to abort!
252 // Not sure what the problem is...
253 qWarning("QMacPrintEngine::newPage: Cannot end current page. %ld", long(err));
254 d->state = QPrinter::Error;
258 return d->newPage_helper();
261 bool QMacPrintEngine::abort()
263 Q_D(QMacPrintEngine);
264 if (d->state != QPrinter::Active)
267 d->state = QPrinter::Aborted;
271 static inline int qt_get_PDMWidth(PMPageFormat pformat, bool fullPage,
272 const PMResolution &resolution)
276 qreal hRatio = resolution.hRes / 72;
278 if (PMGetAdjustedPaperRect(pformat, &r) == noErr)
279 val = qRound((r.right - r.left) * hRatio);
281 if (PMGetAdjustedPageRect(pformat, &r) == noErr)
282 val = qRound((r.right - r.left) * hRatio);
287 static inline int qt_get_PDMHeight(PMPageFormat pformat, bool fullPage,
288 const PMResolution &resolution)
292 qreal vRatio = resolution.vRes / 72;
294 if (PMGetAdjustedPaperRect(pformat, &r) == noErr)
295 val = qRound((r.bottom - r.top) * vRatio);
297 if (PMGetAdjustedPageRect(pformat, &r) == noErr)
298 val = qRound((r.bottom - r.top) * vRatio);
304 int QMacPrintEngine::metric(QPaintDevice::PaintDeviceMetric m) const
306 Q_D(const QMacPrintEngine);
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);
315 QList<QVariant> margins = property(QPrintEngine::PPK_PageMargins).toList();
316 val -= qRound(margins.at(0).toDouble() + margins.at(2).toDouble());
319 val = qt_get_PDMWidth(d->format, property(PPK_FullPage).toBool(), d->resolution);
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);
328 QList<QVariant> margins = property(QPrintEngine::PPK_PageMargins).toList();
329 val -= qRound(margins.at(1).toDouble() + margins.at(3).toDouble());
332 val = qt_get_PDMHeight(d->format, property(PPK_FullPage).toBool(), d->resolution);
335 case QPaintDevice::PdmWidthMM:
336 val = metric(QPaintDevice::PdmWidth);
337 val = int((val * 254 + 5 * d->resolution.hRes) / (10 * d->resolution.hRes));
339 case QPaintDevice::PdmHeightMM:
340 val = metric(QPaintDevice::PdmHeight);
341 val = int((val * 254 + 5 * d->resolution.vRes) / (10 * d->resolution.vRes));
343 case QPaintDevice::PdmPhysicalDpiX:
344 case QPaintDevice::PdmPhysicalDpiY: {
346 if(PMSessionGetCurrentPrinter(d->session, &printer) == noErr) {
347 PMResolution resolution;
348 PMPrinterGetOutputResolution(printer, d->settings, &resolution);
349 val = (int)resolution.vRes;
352 //otherwise fall through
354 case QPaintDevice::PdmDpiY:
355 val = (int)d->resolution.vRes;
357 case QPaintDevice::PdmDpiX:
358 val = (int)d->resolution.hRes;
360 case QPaintDevice::PdmNumColors:
361 val = (1 << metric(QPaintDevice::PdmDepth));
363 case QPaintDevice::PdmDepth:
368 qWarning("QPrinter::metric: Invalid metric command");
373 void QMacPrintEnginePrivate::initialize()
375 Q_Q(QMacPrintEngine);
377 Q_ASSERT(!printInfo);
380 paintEngine = new QCoreGraphicsPaintEngine();
382 q->gccaps = paintEngine->gccaps;
386 QMacCocoaAutoReleasePool pool;
387 printInfo = [[NSPrintInfo alloc] initWithDictionary:[NSDictionary dictionary]];
388 session = static_cast<PMPrintSession>([printInfo PMPrintSession]);
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) {
396 for (int i = 0; i < resolutions.count(); ++i) {
397 int value = resolutions.at(i).toInt();
401 resolution.hRes = resolution.vRes = max;
403 resolution.hRes = resolution.vRes = resolutions.at(0).toInt();
405 if(resolution.hRes == 0)
406 resolution.hRes = resolution.vRes = 600;
408 resolution.hRes = resolution.vRes = qt_defaultDpi();
412 settings = static_cast<PMPrintSettings>([printInfo PMPrintSettings]);
413 format = static_cast<PMPageFormat>([printInfo PMPageFormat]);
416 QHash<QMacPrintEngine::PrintEnginePropertyKey, QVariant>::const_iterator propC;
417 for (propC = valueCache.constBegin(); propC != valueCache.constEnd(); propC++) {
418 q->setProperty(propC.key(), propC.value());
422 void QMacPrintEnginePrivate::releaseSession()
424 PMSessionEndPageNoDialog(session);
425 PMSessionEndDocumentNoDialog(session);
431 bool QMacPrintEnginePrivate::newPage_helper()
433 Q_Q(QMacPrintEngine);
434 Q_ASSERT(state == QPrinter::Active);
436 if (PMSessionError(session) != noErr) {
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
444 QCoreGraphicsPaintEngine *cgEngine = static_cast<QCoreGraphicsPaintEngine*>(paintEngine);
445 while (cgEngine->d_func()->stackCount > 0)
446 cgEngine->d_func()->restoreGraphicsState();
449 PMSessionBeginPageNoDialog(session, format, 0);
450 if(status != noErr) {
451 state = QPrinter::Error;
455 QRect page = q->property(QPrintEngine::PPK_PageRect).toRect();
456 QRect paper = q->property(QPrintEngine::PPK_PaperRect).toRect();
458 CGContextRef cgContext;
459 OSStatus err = noErr;
460 err = PMSessionGetCGGraphicsContext(session, &cgContext);
462 qWarning("QMacPrintEngine::newPage: Cannot retrieve CoreGraphics context: %ld", long(err));
463 state = QPrinter::Error;
466 cgEngine->d_func()->hd = cgContext;
468 // Set the resolution as a scaling ration of 72 (the default).
469 CGContextScaleCTM(cgContext, 72 / resolution.hRes, 72 / resolution.vRes);
471 CGContextScaleCTM(cgContext, 1, -1);
472 CGContextTranslateCTM(cgContext, 0, -paper.height());
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();
488 void QMacPrintEngine::updateState(const QPaintEngineState &state)
490 d_func()->paintEngine->updateState(state);
493 void QMacPrintEngine::drawRects(const QRectF *r, int num)
495 Q_D(QMacPrintEngine);
496 Q_ASSERT(d->state == QPrinter::Active);
497 d->paintEngine->drawRects(r, num);
500 void QMacPrintEngine::drawPoints(const QPointF *points, int pointCount)
502 Q_D(QMacPrintEngine);
503 Q_ASSERT(d->state == QPrinter::Active);
504 d->paintEngine->drawPoints(points, pointCount);
507 void QMacPrintEngine::drawEllipse(const QRectF &r)
509 Q_D(QMacPrintEngine);
510 Q_ASSERT(d->state == QPrinter::Active);
511 d->paintEngine->drawEllipse(r);
514 void QMacPrintEngine::drawLines(const QLineF *lines, int lineCount)
516 Q_D(QMacPrintEngine);
517 Q_ASSERT(d->state == QPrinter::Active);
518 d->paintEngine->drawLines(lines, lineCount);
521 void QMacPrintEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
523 Q_D(QMacPrintEngine);
524 Q_ASSERT(d->state == QPrinter::Active);
525 d->paintEngine->drawPolygon(points, pointCount, mode);
528 void QMacPrintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
530 Q_D(QMacPrintEngine);
531 Q_ASSERT(d->state == QPrinter::Active);
532 d->paintEngine->drawPixmap(r, pm, sr);
535 void QMacPrintEngine::drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, Qt::ImageConversionFlags flags)
537 Q_D(QMacPrintEngine);
538 Q_ASSERT(d->state == QPrinter::Active);
539 d->paintEngine->drawImage(r, pm, sr, flags);
542 void QMacPrintEngine::drawTextItem(const QPointF &p, const QTextItem &ti)
544 Q_D(QMacPrintEngine);
545 Q_ASSERT(d->state == QPrinter::Active);
546 d->paintEngine->drawTextItem(p, ti);
549 void QMacPrintEngine::drawTiledPixmap(const QRectF &dr, const QPixmap &pixmap, const QPointF &sr)
551 Q_D(QMacPrintEngine);
552 Q_ASSERT(d->state == QPrinter::Active);
553 d->paintEngine->drawTiledPixmap(dr, pixmap, sr);
556 void QMacPrintEngine::drawPath(const QPainterPath &path)
558 Q_D(QMacPrintEngine);
559 Q_ASSERT(d->state == QPrinter::Active);
560 d->paintEngine->drawPath(path);
564 void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &value)
566 Q_D(QMacPrintEngine);
568 d->valueCache.insert(key, value);
573 case PPK_CollateCopies:
579 case PPK_DocumentName:
583 case PPK_PaperSource:
585 case PPK_SelectionOption:
587 case PPK_Resolution: {
590 if (PMSessionGetCurrentPrinter(d->session, &printer) != noErr)
592 if (PMPrinterGetPrinterResolutionCount(printer, &count) != noErr)
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;
604 int distance = qAbs(dpi - int(resolution.hRes));
605 if (distance < bestDistance) {
606 bestDistance = distance;
607 bestResolution = resolution;
612 PMSessionValidatePageFormat(d->session, d->format, kPMDontWantBoolean);
617 d->fullPage = value.toBool();
619 case PPK_CopyCount: // fallthrough
620 case PPK_NumberOfCopies:
621 PMSetCopies(d->settings, value.toInt(), false);
623 case PPK_Orientation: {
624 if (d->state == QPrinter::Active) {
625 qWarning("QMacPrintEngine::setOrientation: Orientation cannot be changed during a print job, ignoring change");
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);
636 case PPK_OutputFileName:
637 d->outputFilename = value.toString();
640 d->setPaperSize(QPrinter::PaperSize(value.toInt()));
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;
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()));
664 d->state = QPrinter::Idle;
667 case PPK_SuppressSystemPrintStatus:
668 d->suppressStatus = value.toBool();
670 case PPK_CustomPaperSize:
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());
680 case PPK_PageMargins:
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;
697 QVariant QMacPrintEngine::property(PrintEnginePropertyKey key) const
699 Q_D(const QMacPrintEngine);
702 if (!d->session && d->valueCache.contains(key))
703 return *d->valueCache.find(key);
706 case PPK_CollateCopies:
710 ret = QPrinter::Color;
714 case PPK_DocumentName:
719 case PPK_NumberOfCopies:
722 case PPK_CopyCount: {
724 PMGetCopies(d->settings, &copies);
728 case PPK_SupportsMultipleCopies:
731 case PPK_Orientation:
732 PMOrientation orientation;
733 PMGetOrientation(d->format, &orientation);
734 ret = orientation == kPMPortrait ? QPrinter::Portrait : QPrinter::Landscape;
736 case PPK_OutputFileName:
737 ret = d->outputFilename;
741 case PPK_PaperSource:
744 // PageRect is returned in device pixels
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));
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);
761 } else if (PMGetAdjustedPageRect(d->format, ¯ect) == noErr
762 && PMGetAdjustedPaperRect(d->format, &macpaper) == noErr)
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));
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));
781 ret = d->paperSize();
783 case PPK_PaperRect: {
786 if (d->hasCustomPaperSize) {
787 r = QRect(0, 0, qRound(d->customSize.width()), qRound(d->customSize.height()));
788 } else if (PMGetAdjustedPaperRect(d->format, ¯ect) == 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());
797 case PPK_PrinterName: {
799 OSStatus status = PMSessionGetCurrentPrinter(d->session, &printer);
801 qWarning("QMacPrintEngine::printerName: Failed getting current PMPrinter: %ld", long(status));
803 ret = QCFString::toQString(PMPrinterGetName(printer));
805 case PPK_Resolution: {
806 ret = d->resolution.hRes;
809 case PPK_SupportedResolutions:
810 ret = d->supportedResolutions();
812 case PPK_CustomPaperSize:
815 case PPK_PageMargins:
817 QList<QVariant> margins;
818 if (d->hasCustomPageMargins) {
819 margins << d->leftMargin << d->topMargin
820 << d->rightMargin << d->bottomMargin;
822 PMPaperMargins paperMargins;
824 PMGetPageFormatPaper(d->format, &paper);
825 PMPaperGetMargins(paper, &paperMargins);
826 margins << paperMargins.left << paperMargins.top
827 << paperMargins.right << paperMargins.bottom;
840 #endif // QT_NO_PRINTER