source: trunk/src/gui/painting/qprintengine_mac.mm@ 308

Last change on this file since 308 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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