42 #include <private/qprintengine_mac_p.h> 45 #include <QtCore/qcoreapplication.h> 65 if (!static_cast<QPrinter *>(dev)->isValid())
71 d->paintEngine->state =
state;
72 d->paintEngine->begin(dev);
75 if (PMSessionValidatePrintSettings(
d->session,
d->settings, kPMDontWantBoolean) != noErr
76 || PMSessionValidatePageFormat(
d->session,
d->format, kPMDontWantBoolean) != noErr) {
81 if (!
d->outputFilename.isEmpty()) {
86 if (PMSessionSetDestination(
d->session,
d->settings, kPMDestinationFile,
87 kPMDocumentFormatPDF, outFile) != noErr) {
88 qWarning(
"QMacPrintEngine::begin: Problem setting file [%s]",
d->outputFilename.toUtf8().constData());
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) {
121 d->paintEngine->end();
131 return d_func()->paintEngine;
137 return cgEngine->d_func()->hd;
142 #ifdef QT_MAC_USE_COCOA 151 if (hasCustomPaperSize) {
152 PMRelease(customPaper);
158 if (PMSessionGetCurrentPrinter(session, &printer) == noErr) {
162 if (PMSessionCreatePageFormatList(session, printer, &formats) == noErr) {
163 CFIndex total = CFArrayGetCount(formats);
166 for (CFIndex idx = 0; idx < total; ++idx) {
167 tmp =
static_cast<PMPageFormat
>(
const_cast<void *
>(CFArrayGetValueAtIndex(formats, idx)));
168 PMGetUnadjustedPaperRect(tmp, &paper);
169 int wMM = int((paper.right - paper.left) / 72 * 25.4 + 0.5);
170 int hMM = int((paper.bottom - paper.top) / 72 * 25.4 + 0.5);
171 if (newSize.
width() == wMM && newSize.
height() == hMM) {
172 PMCopyPageFormat(tmp,
format);
175 if (PMSessionValidatePageFormat(session,
format, kPMDontWantBoolean) != noErr) {
177 qWarning(
"QMacPrintEngine, problem setting format and resolution for this page size");
185 PMPaperMargins paperMargins;
186 paperMargins.left = leftMargin;
187 paperMargins.top = topMargin;
188 paperMargins.right = rightMargin;
189 paperMargins.bottom = bottomMargin;
190 PMPaperCreateCustom(printer, paperId,
QCFString(
"Custom size"), customSize.width(), customSize.height(), &paperMargins, &customPaper);
192 PMCreatePageFormatWithPMPaper(&tmp, customPaper);
193 PMCopyPageFormat(tmp,
format);
194 if (PMSessionValidatePageFormat(session,
format, kPMDontWantBoolean) != noErr) {
196 qWarning(
"QMacPrintEngine, problem setting paper name");
204 if (hasCustomPaperSize)
207 PMGetUnadjustedPaperRect(
format, &paper);
208 int wMM = int((paper.right - paper.left) / 72 * 25.4 + 0.5);
209 int hMM = int((paper.bottom - paper.top) / 72 * 25.4 + 0.5);
220 Q_ASSERT_X(session,
"QMacPrinterEngine::supportedResolutions",
221 "must have a valid printer session");
225 if (PMSessionGetCurrentPrinter(session, &printer) == noErr) {
227 OSStatus status = PMPrinterGetPrinterResolutionCount(printer, &resCount);
228 if (status == kPMNotImplemented) {
229 #if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5) 231 if (PMPrinterGetPrinterResolution(printer, kPMMinSquareResolution, &res) == noErr)
232 resolutions.
append(
int(res.hRes));
233 if (PMPrinterGetPrinterResolution(printer, kPMMaxSquareResolution, &res) == noErr) {
238 if (PMPrinterGetPrinterResolution(printer, kPMDefaultResolution, &res) == noErr) {
244 }
else if (status == noErr) {
246 for (UInt32 i = 1; i <= resCount; ++i) {
247 if (PMPrinterGetIndexedPrinterResolution(printer, i, &res) == noErr)
251 qWarning(
"QMacPrintEngine::supportedResolutions: Unexpected error: %ld",
long(status));
259 if (suppressStatus ==
true)
269 return d_func()->state;
277 #ifndef QT_MAC_USE_COCOA 278 d->shouldSuppressStatus() ? PMSessionEndPageNoDialog(
d->session)
279 : PMSessionEndPage(
d->session);
281 PMSessionEndPageNoDialog(
d->session);
284 if (err == kPMCancel) {
289 qWarning(
"QMacPrintEngine::newPage: Cannot end current page. %ld",
long(err));
294 return d->newPage_helper();
308 const PMResolution &resolution)
312 qreal hRatio = resolution.hRes / 72;
314 if (PMGetAdjustedPaperRect(pformat, &r) == noErr)
315 val =
qRound((r.right - r.left) * hRatio);
317 if (PMGetAdjustedPageRect(pformat, &r) == noErr)
318 val =
qRound((r.right - r.left) * hRatio);
324 const PMResolution &resolution)
328 qreal vRatio = resolution.vRes / 72;
330 if (PMGetAdjustedPaperRect(pformat, &r) == noErr)
331 val =
qRound((r.bottom - r.top) * vRatio);
333 if (PMGetAdjustedPageRect(pformat, &r) == noErr)
334 val =
qRound((r.bottom - r.top) * vRatio);
346 if (
d->hasCustomPaperSize) {
347 val =
qRound(
d->customSize.width());
348 if (
d->hasCustomPageMargins) {
349 val -=
qRound(
d->leftMargin +
d->rightMargin);
359 if (
d->hasCustomPaperSize) {
360 val =
qRound(
d->customSize.height());
361 if (
d->hasCustomPageMargins) {
362 val -=
qRound(
d->topMargin +
d->bottomMargin);
373 val = int((val * 254 + 5 *
d->resolution.hRes) / (10 *
d->resolution.hRes));
377 val = int((val * 254 + 5 *
d->resolution.vRes) / (10 *
d->resolution.vRes));
382 if(PMSessionGetCurrentPrinter(
d->session, &printer) == noErr) {
383 PMResolution resolution;
384 #ifndef QT_MAC_USE_COCOA 385 # if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) 387 PMPrinterGetOutputResolution(printer,
d->settings, &resolution);
391 PMPrinterGetPrinterResolution(printer, kPMCurrentValue, &resolution);
394 PMPrinterGetOutputResolution(printer,
d->settings, &resolution);
396 val = (int)resolution.vRes;
402 val = (int)
d->resolution.vRes;
405 val = (
int)
d->resolution.hRes;
415 qWarning(
"QPrinter::metric: Invalid metric command");
424 #ifndef QT_MAC_USE_COCOA 437 #ifndef QT_MAC_USE_COCOA 438 if (PMCreateSession(&session) != 0)
442 printInfo = [[
NSPrintInfo alloc] initWithDictionary:[NSDictionary dictionary]];
443 session =
static_cast<PMPrintSession
>([printInfo PMPrintSession]);
447 if (session && PMSessionGetCurrentPrinter(session, &printer) == noErr) {
452 for (
int i = 0; i < resolutions.
count(); ++i) {
453 int value = resolutions.
at(i).
toInt();
457 resolution.hRes = resolution.vRes = max;
459 resolution.hRes = resolution.vRes = resolutions.
at(0).
toInt();
461 if(resolution.hRes == 0)
462 resolution.hRes = resolution.vRes = 600;
468 #ifndef QT_MAC_USE_COCOA 469 bool settingsInitialized = (settings != 0);
470 bool settingsOK = !settingsInitialized ? PMCreatePrintSettings(&settings) == noErr :
true;
471 if (settingsOK && !settingsInitialized)
472 settingsOK = PMSessionDefaultPrintSettings(session, settings) == noErr;
475 bool formatInitialized = (
format != 0);
476 bool formatOK = !formatInitialized ? PMCreatePageFormat(&
format) == noErr :
true;
478 if (!formatInitialized) {
479 formatOK = PMSessionDefaultPageFormat(session,
format) == noErr;
481 formatOK = PMSessionValidatePageFormat(session,
format, kPMDontWantBoolean) == noErr;
484 settings =
static_cast<PMPrintSettings
>([printInfo PMPrintSettings]);
485 format =
static_cast<PMPageFormat
>([printInfo PMPageFormat]);
488 #ifndef QT_MAC_USE_COCOA 489 if (!settingsOK || !formatOK) {
490 qWarning(
"QMacPrintEngine::initialize: Unable to initialize QPainter");
497 q->setProperty(propC.
key(), propC.
value());
503 #ifndef QT_MAC_USE_COCOA 504 if (shouldSuppressStatus()) {
505 PMSessionEndPageNoDialog(session);
506 PMSessionEndDocumentNoDialog(session);
508 PMSessionEndPage(session);
509 PMSessionEndDocument(session);
513 PMSessionEndPageNoDialog(session);
514 PMSessionEndDocumentNoDialog(session);
517 if (hasCustomPaperSize)
518 PMRelease(customPaper);
528 if (PMSessionError(session) != noErr) {
537 while (cgEngine->d_func()->stackCount > 0)
538 cgEngine->d_func()->restoreGraphicsState();
541 #ifndef QT_MAC_USE_COCOA 542 shouldSuppressStatus() ? PMSessionBeginPageNoDialog(session,
format, 0)
543 : PMSessionBeginPage(session,
format, 0);
545 PMSessionBeginPageNoDialog(session,
format, 0);
547 if(status != noErr) {
557 err = PMSessionGetCGGraphicsContext(session, &cgContext);
559 qWarning(
"QMacPrintEngine::newPage: Cannot retrieve CoreGraphics context: %ld",
long(err));
563 cgEngine->d_func()->hd = cgContext;
566 CGContextScaleCTM(cgContext, 72 / resolution.hRes, 72 / resolution.vRes);
568 CGContextScaleCTM(cgContext, 1, -1);
569 CGContextTranslateCTM(cgContext, 0, -paper.
height());
571 CGContextTranslateCTM(cgContext, page.
x() - paper.
x(), page.
y() - paper.
y());
572 cgEngine->d_func()->orig_xform = CGContextGetCTM(cgContext);
573 cgEngine->d_func()->setClip(0);
587 d_func()->paintEngine->updateState(state);
594 d->paintEngine->drawRects(r, num);
601 d->paintEngine->drawPoints(points, pointCount);
608 d->paintEngine->drawEllipse(r);
615 d->paintEngine->drawLines(lines, lineCount);
622 d->paintEngine->drawPolygon(points, pointCount, mode);
629 d->paintEngine->drawPixmap(r, pm, sr);
636 d->paintEngine->drawImage(r, pm, sr, flags);
643 d->paintEngine->drawTextItem(p, ti);
650 d->paintEngine->drawTiledPixmap(dr, pixmap, sr);
657 d->paintEngine->drawPath(path);
665 d->valueCache.insert(key, value);
687 if (PMSessionGetCurrentPrinter(
d->session, &printer) != noErr)
689 if (PMPrinterGetPrinterResolutionCount(printer, &count) != noErr)
691 PMResolution resolution = { 0.0, 0.0 };
692 PMResolution bestResolution = { 0.0, 0.0 };
693 int dpi = value.
toInt();
695 for (UInt32 i = 1; i <= count; ++i) {
696 if (PMPrinterGetIndexedPrinterResolution(printer, i, &resolution) == noErr) {
697 if (dpi ==
int(resolution.hRes)) {
698 bestResolution = resolution;
702 if (distance < bestDistance) {
704 bestResolution = resolution;
709 PMSessionValidatePageFormat(
d->session,
d->format, kPMDontWantBoolean);
718 PMSetCopies(
d->settings, value.
toInt(),
false);
722 qWarning(
"QMacPrintEngine::setOrientation: Orientation cannot be changed during a print job, ignoring change");
725 if (
d->hasCustomPaperSize && (
d->orient != newOrientation))
726 d->customSize =
QSizeF(
d->customSize.height(),
d->customSize.width());
727 d->orient = newOrientation;
729 PMSetOrientation(
d->format, o,
false);
730 PMSessionValidatePageFormat(
d->session,
d->format, kPMDontWantBoolean);
740 bool printerNameSet =
false;
743 status = PMServerCreatePrinterList(kPMServerLocal, &printerList);
744 if (status == noErr) {
745 CFIndex count = CFArrayGetCount(printerList);
746 for (CFIndex i=0; i<count; ++i) {
747 PMPrinter printer =
static_cast<PMPrinter
>(
const_cast<void *
>(CFArrayGetValueAtIndex(printerList, i)));
750 status = PMSessionSetCurrentPMPrinter(
d->session, printer);
751 printerNameSet =
true;
757 qWarning(
"QMacPrintEngine::setPrinterName: Error setting printer: %ld",
long(status));
758 if (!printerNameSet) {
765 d->suppressStatus = value.
toBool();
769 PMOrientation orientation;
770 PMGetOrientation(
d->format, &orientation);
772 if (orientation != kPMPortrait)
773 d->customSize =
QSizeF(
d->customSize.height(),
d->customSize.width());
781 d->leftMargin = margins.at(0).toDouble();
782 d->topMargin = margins.at(1).toDouble();
783 d->rightMargin = margins.at(2).toDouble();
784 d->bottomMargin = margins.at(3).toDouble();
785 d->hasCustomPageMargins =
true;
799 if (!
d->session &&
d->valueCache.contains(key))
800 return *
d->valueCache.find(key);
821 PMGetCopies(
d->settings, &copies);
829 PMOrientation orientation;
830 PMGetOrientation(
d->format, &orientation);
834 ret =
d->outputFilename;
843 PMRect macrect, macpaper;
844 qreal hRatio =
d->resolution.hRes / 72;
845 qreal vRatio =
d->resolution.vRes / 72;
846 if (
d->hasCustomPaperSize) {
847 r =
QRect(0, 0,
qRound(
d->customSize.width() * hRatio),
qRound(
d->customSize.height() * vRatio));
848 if (
d->hasCustomPageMargins) {
850 -
qRound(
d->rightMargin * hRatio), -
qRound(
d->bottomMargin * vRatio));
858 }
else if (PMGetAdjustedPageRect(
d->format, ¯ect) == noErr
859 && PMGetAdjustedPaperRect(
d->format, &macpaper) == noErr)
861 if (
d->fullPage ||
d->hasCustomPageMargins) {
862 r.
setCoords(
int(macpaper.left * hRatio),
int(macpaper.top * vRatio),
863 int(macpaper.right * hRatio),
int(macpaper.bottom * vRatio));
865 if (
d->hasCustomPageMargins) {
867 -
qRound(
d->rightMargin * hRatio), -
qRound(
d->bottomMargin * vRatio));
870 r.
setCoords(
int(macrect.left * hRatio),
int(macrect.top * vRatio),
871 int(macrect.right * hRatio),
int(macrect.bottom * vRatio));
872 r.
translate(
int(-macpaper.left * hRatio),
int(-macpaper.top * vRatio));
878 ret =
d->paperSize();
883 qreal hRatio =
d->resolution.hRes / 72;
884 qreal vRatio =
d->resolution.vRes / 72;
885 if (
d->hasCustomPaperSize) {
886 r =
QRect(0, 0,
qRound(
d->customSize.width() * hRatio),
qRound(
d->customSize.height() * vRatio));
887 }
else if (PMGetAdjustedPaperRect(
d->format, ¯ect) == noErr) {
888 r.
setCoords(
int(macrect.left * hRatio),
int(macrect.top * vRatio),
889 int(macrect.right * hRatio),
int(macrect.bottom * vRatio));
898 qWarning(
"QMacPrintEngine::printerName: Failed getting current PMPrinter: %ld",
long(status));
903 ret =
d->resolution.hRes;
907 ret =
d->supportedResolutions();
915 if (
d->hasCustomPageMargins) {
916 margins <<
d->leftMargin <<
d->topMargin
917 <<
d->rightMargin <<
d->bottomMargin;
919 PMPaperMargins paperMargins;
921 PMGetPageFormatPaper(
d->format, &paper);
922 PMPaperGetMargins(paper, &paperMargins);
923 margins << paperMargins.left << paperMargins.top
924 << paperMargins.right << paperMargins.bottom;
937 #endif // QT_NO_PRINTER The QVariant class acts like a union for the most common Qt data types.
#define QT_END_NAMESPACE
This macro expands to.
void syncState()
Updates all dirty states in this engine.
PaintEngineFeatures gccaps
QSize toSize() const
Returns an integer based copy of this size.
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
static int qt_get_PDMWidth(PMPageFormat pformat, bool fullPage, const PMResolution &resolution)
QPaintEngineState * state
static QUuid createUuid()
On any platform other than Windows, this function returns a new UUID with variant QUuid::DCE and vers...
PrinterMode
This enum describes the mode the printer should work in.
void setProperty(PrintEnginePropertyKey key, const QVariant &value)
Sets the print engine's property specified by key to the given value.
The QPointF class defines a point in the plane using floating point precision.
~QMacPrintEnginePrivate()
bool begin(QPaintDevice *dev)
Reimplement this function to initialise your paint engine when painting is to start on the paint devi...
virtual void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
Reimplement this virtual function to draw the polygon defined by the pointCount first points in point...
Orientation
This enum type (not to be confused with Orientation) is used to specify each page's orientation...
static CFStringRef toCFStringRef(const QString &str)
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
static QString toQString(CFStringRef cfstr)
static int qt_get_PDMHeight(PMPageFormat pformat, bool fullPage, const PMResolution &resolution)
QList< QVariant > toList() const
Returns the variant as a QVariantList if the variant has type() List or StringList ; otherwise return...
static LibLoadStatus status
bool toBool() const
Returns the variant as a bool if the variant has type() Bool.
QPaintEngine::DirtyFlags dirtyFlags
int count(const T &t) const
Returns the number of occurrences of value in the list.
int height() const
Returns the height of the rectangle.
The QString class provides a Unicode character string.
void updateState(const QPaintEngineState &state)
Reimplement this function to update the state of a paint engine.
QPrinter::PrinterState printerState() const
Returns the current state of the printer being used by the print engine.
The QHash class is a template class that provides a hash-table-based dictionary.
virtual void drawPoints(const QPointF *p, int pointCount)
Draws the first pointCount points in the buffer points.
Q_DECL_CONSTEXPR T qAbs(const T &t)
virtual int devType() const
The QSizeF class defines the size of a two-dimensional object using floating point precision...
const T value(const Key &key) const
Returns the value associated with the key.
DirtyFlag
QPaintEngine::updateState().
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
QPaintEngine * paintEngine() const
The QLineF class provides a two-dimensional vector using floating point precision.
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
virtual void drawRects(const QRectF *r, int num)
Draws the first rectCount rectangles in the buffer rects.
static bool toBool(Register *reg, int type, bool *ok=0)
QPrinter::PaperSize paperSize() const
int width() const
Returns the width.
static QThread * currentThread()
Returns a pointer to a QThread which manages the currently executing thread.
static QString toString(Register *reg, int type, bool *ok=0)
void append(const T &t)
Inserts value at the end of the list.
QPainter * painter() const
Returns the paint engine's painter.
#define QT_BEGIN_NAMESPACE
This macro expands to.
The QRectF class defines a rectangle in the plane using floating point precision. ...
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
bool hasClipping() const
Returns true if clipping has been set; otherwise returns false.
void setActive(bool newState)
Sets the active state of the paint engine to state.
QVariant property(PrintEnginePropertyKey key) const
Returns the print engine's property specified by key.
const T & at(int i) const
Returns the item at index position i in the list.
void setPaperSize(QPrinter::PaperSize ps)
void adjust(int x1, int y1, int x2, int y2)
Adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle.
Q_CORE_EXPORT void qWarning(const char *,...)
The QImage class provides a hardware-independent image representation that allows direct access to th...
virtual void drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s)
Reimplement this function to draw the pixmap in the given rect, starting at the given p...
virtual void drawLines(const QLineF *lines, int lineCount)
The default implementation splits the list of lines in lines into lineCount separate calls to drawPat...
void setCoords(int x1, int y1, int x2, int y2)
Sets the coordinates of the rectangle's top-left corner to (x1, y1), and the coordinates of its botto...
virtual void drawPath(const QPainterPath &)
The default implementation ignores the path and does nothing.
virtual void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, Qt::ImageConversionFlags flags)
Reimplement this function to draw the part of the image specified by the sr rectangle in the given re...
The QPaintEngine class provides an abstract definition of how QPainter draws to a given device on a g...
bool shouldSuppressStatus() const
QSizeF toSizeF() const
Returns the variant as a QSizeF if the variant has type() SizeF ; otherwise returns an invalid QSizeF...
virtual void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
Reimplement this function to draw the part of the pm specified by the sr rectangle in the given r...
bool end()
Reimplement this function to finish painting on the current paint device.
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the hash.
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
#define Q_ASSERT_X(cond, where, what)
int y() const
Returns the y-coordinate of the rectangle's top edge.
Qt::HANDLE handle() const
Q_GUI_EXPORT int qt_defaultDpi()
PaperSize
This enum type specifies what paper size QPrinter should use.
const Key key(const T &value) const
Returns the first key mapped to value.
int x() const
Returns the x-coordinate of the rectangle's left edge.
QMacPrintEngine(QPrinter::PrinterMode mode)
int metric(QPaintDevice::PaintDeviceMetric) const
Returns the metric for the given id.
static const char * outFile
int height() const
Returns the height.
The QRect class defines a rectangle in the plane using integer precision.
QSizeF qt_paperSizeToQSizeF(QPrinter::PaperSize size)
QList< QVariant > supportedResolutions() const
double toDouble(bool *ok=0) const
Returns the variant as a double if the variant has type() Double , QMetaType::Float ...
The QPixmap class is an off-screen image representation that can be used as a paint device...
The QPaintEngineState class provides information about the active paint engine's current state...
static const MacVersion MacintoshVersion
the version of the Macintosh operating system on which the application is run (Mac only)...
The QSize class defines the size of a two-dimensional object using integer point precision.
bool abort()
Instructs the print engine to abort the printing process.
struct CGContext * CGContextRef
The QTextItem class provides all the information required to draw text in a custom paint engine...
void translate(int dx, int dy)
Moves the rectangle dx along the x axis and dy along the y axis, relative to the current position...
#define qPrintable(string)
bool newPage()
Instructs the print engine to start a new page.
Q_DECL_CONSTEXPR int qRound(qreal d)
virtual void drawEllipse(const QRectF &r)
Reimplement this function to draw the largest ellipse that can be contained within rectangle rect...
virtual void drawTextItem(const QPointF &p, const QTextItem &ti)
This function draws the text item textItem at position p.
PrintEnginePropertyKey
This enum is used to communicate properties between the print engine and QPrinter.