Qt 4.8
qpaintdevice_mac.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qpaintdevice.h"
43 #include "qpainter.h"
44 #include "qwidget.h"
45 #include "qbitmap.h"
46 #include "qapplication.h"
47 #include "qprinter.h"
48 #include <qdebug.h>
49 #include <private/qt_mac_p.h>
50 #include <private/qprintengine_mac_p.h>
51 #include <private/qpixmap_mac_p.h>
52 #include <private/qpixmap_raster_p.h>
53 
55 
56 /*****************************************************************************
57  Internal variables and functions
58  *****************************************************************************/
59 
62 {
63  // Mac OS X currently assumes things to be 72 dpi.
64  // (see http://developer.apple.com/releasenotes/GraphicsImaging/RN-ResolutionIndependentUI/)
65  // This may need to be re-worked as we go further in the resolution-independence stuff.
66  return 72;
67 }
68 
71 {
72  // Mac OS X currently assumes things to be 72 dpi.
73  // (see http://developer.apple.com/releasenotes/GraphicsImaging/RN-ResolutionIndependentUI/)
74  // This may need to be re-worked as we go further in the resolution-independence stuff.
75  return 72;
76 }
77 
78 
92 {
93  if (device->devType() == QInternal::Pixmap) {
94  return static_cast<GrafPtr>(static_cast<const QPixmap *>(device)->macQDHandle());
95  } else if(device->devType() == QInternal::Widget) {
96  return static_cast<GrafPtr>(static_cast<const QWidget *>(device)->macQDHandle());
97  } else if(device->devType() == QInternal::Printer) {
98  QPaintEngine *engine = static_cast<const QPrinter *>(device)->paintEngine();
99  return static_cast<GrafPtr>(static_cast<const QMacPrintEngine *>(engine)->handle());
100  }
101  return 0;
102 }
103 
105 
119 {
120  if (pdev->devType() == QInternal::Pixmap) {
121  const QPixmap *pm = static_cast<const QPixmap*>(pdev);
123  uint flags = kCGImageAlphaPremultipliedFirst;
124 #ifdef kCGBitmapByteOrder32Host //only needed because CGImage.h added symbols in the minor version
125  flags |= kCGBitmapByteOrder32Host;
126 #endif
127  CGContextRef ret = 0;
128 
129  // It would make sense to put this into a mac #ifdef'ed
130  // virtual function in the QPixmapData at some point
131  if (pm->data->classId() == QPixmapData::MacClass) {
132  const QMacPixmapData *pmData = static_cast<const QMacPixmapData*>(pm->data.data());
133  ret = CGBitmapContextCreate(pmData->pixels, pmData->w, pmData->h,
134  8, pmData->bytesPerRow, colorspace,
135  flags);
136  if(!ret)
137  qWarning("QPaintDevice: Unable to create context for pixmap (%d/%d/%d)",
138  pmData->w, pmData->h, (pmData->bytesPerRow * pmData->h));
139  } else if (pm->data->classId() == QPixmapData::RasterClass) {
140  QImage *image = pm->data->buffer();
141  ret = CGBitmapContextCreate(image->bits(), image->width(), image->height(),
142  8, image->bytesPerLine(), colorspace, flags);
143  }
144 
145  CGContextTranslateCTM(ret, 0, pm->height());
146  CGContextScaleCTM(ret, 1, -1);
147  return ret;
148  } else if (pdev->devType() == QInternal::Widget) {
149  CGContextRef ret = static_cast<CGContextRef>(static_cast<const QWidget *>(pdev)->macCGHandle());
150  CGContextRetain(ret);
151  return ret;
152  } else if (pdev->devType() == QInternal::MacQuartz) {
153  return static_cast<const QMacQuartzPaintDevice *>(pdev)->cgContext();
154  }
155  return 0;
156 }
157 
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QExplicitlySharedDataPointer< QPixmapData > data
Definition: qpixmap.h:252
CGColorSpaceRef qt_mac_colorSpaceForDeviceType(const QPaintDevice *pdev)
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
int bytesPerLine() const
Returns the number of bytes per image scanline.
Definition: qimage.cpp:1812
float qt_mac_defaultDpi_y()
virtual int devType() const
Definition: qpaintdevice.h:167
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QPrinter class is a paint device that paints on a printer.
Definition: qprinter.h:66
virtual Qt::HANDLE handle() const
Q_CORE_EXPORT void qWarning(const char *,...)
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
unsigned int uint
Definition: qglobal.h:996
ClassId classId() const
The QPaintEngine class provides an abstract definition of how QPainter draws to a given device on a g...
Definition: qpaintengine.h:90
uchar * bits()
Returns a pointer to the first pixel data.
Definition: qimage.cpp:1946
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
struct CGColorSpace * CGColorSpaceRef
float qt_mac_defaultDpi_x()
Q_GUI_EXPORT CGContextRef qt_mac_cg_context(const QPaintDevice *pdev)
Returns the CoreGraphics CGContextRef of the paint device.
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
virtual QImage * buffer()
int height() const
Returns the height of the pixmap.
Definition: qpixmap.cpp:645
struct CGContext * CGContextRef
quint32 * pixels
Q_GUI_EXPORT GrafPtr qt_mac_qd_context(const QPaintDevice *device)
Returns the QuickDraw CGrafPtr of the paint device.