Qt 4.8
qnsview.mm
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 plugins 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 "qnsview.h"
43 
44 #include <QtGui/QWindowSystemInterface>
45 
46 #include <QtCore/QDebug>
47 
48 @implementation QNSView
49 
50 - (id) init
51 {
52  self = [super init];
53  if (self) {
54  m_cgImage = 0;
55  m_widget = 0;
57  }
58  return self;
59 }
60 
61 - (id)initWithWidget:(QWidget *)widget {
62  self = [self init];
63  if (self) {
64  m_widget = widget;
65  }
66  return self;
67 }
68 
69 - (void) setImage:(QImage *)image
70 {
71  CGImageRelease(m_cgImage);
72 
73  const uchar *imageData = image->bits();
74  int bitDepth = image->depth();
75  int colorBufferSize = 8;
76  int bytesPrLine = image->bytesPerLine();
77  int width = image->width();
78  int height = image->height();
79 
80  CGColorSpaceRef cgColourSpaceRef = CGColorSpaceCreateDeviceRGB();
81 
82  CGDataProviderRef cgDataProviderRef = CGDataProviderCreateWithData(
83  NULL,
84  imageData,
85  image->byteCount(),
86  NULL);
87 
88  m_cgImage = CGImageCreate(width,
89  height,
90  colorBufferSize,
91  bitDepth,
92  bytesPrLine,
93  cgColourSpaceRef,
94  kCGImageAlphaNone,
95  cgDataProviderRef,
96  NULL,
97  false,
98  kCGRenderingIntentDefault);
99 
100  CGColorSpaceRelease(cgColourSpaceRef);
101 
102 }
103 
104 - (void) drawRect:(NSRect)dirtyRect
105 {
106  if (!m_cgImage)
107  return;
108 
109  CGRect dirtyCGRect = NSRectToCGRect(dirtyRect);
110 
111  NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext currentContext];
112  CGContextRef cgContext = (CGContextRef) [nsGraphicsContext graphicsPort];
113 
114  CGContextSaveGState( cgContext );
115  int dy = dirtyCGRect.origin.y + CGRectGetMaxY(dirtyCGRect);
116  CGContextTranslateCTM(cgContext, 0, dy);
117  CGContextScaleCTM(cgContext, 1, -1);
118 
119  CGImageRef subImage = CGImageCreateWithImageInRect(m_cgImage, dirtyCGRect);
120  CGContextDrawImage(cgContext,dirtyCGRect,subImage);
121 
122  CGContextRestoreGState(cgContext);
123 
124  CGImageRelease(subImage);
125 
126 }
127 
128 - (BOOL) isFlipped
129 {
130  return YES;
131 }
132 
133 - (void)handleMouseEvent:(NSEvent *)theEvent;
134 {
135  NSPoint point = [self convertPoint: [theEvent locationInWindow] fromView: nil];
136  QPoint qt_localPoint(point.x,point.y);
137 
138  NSTimeInterval timestamp = [theEvent timestamp];
139  ulong qt_timestamp = timestamp * 1000;
140 
142 
143 }
144  - (void)mouseDown:(NSEvent *)theEvent
145  {
147  [self handleMouseEvent:theEvent];
148  }
149  - (void)mouseDragged:(NSEvent *)theEvent
150  {
151  if (!(m_buttons & Qt::LeftButton))
152  qWarning("Internal Mousebutton tracking invalid(missing Qt::LeftButton");
153  [self handleMouseEvent:theEvent];
154  }
155  - (void)mouseUp:(NSEvent *)theEvent
156  {
157  m_buttons &= QFlag(~int(Qt::LeftButton));
158  [self handleMouseEvent:theEvent];
159  }
160 
161 - (void)mouseMoved:(NSEvent *)theEvent
162 {
163  qDebug() << "mouseMove";
164  [self handleMouseEvent:theEvent];
165 }
166 - (void)mouseEntered:(NSEvent *)theEvent
167 {
168  Q_UNUSED(theEvent);
170 }
171 - (void)mouseExited:(NSEvent *)theEvent
172 {
173  Q_UNUSED(theEvent);
175 }
176 - (void)rightMouseDown:(NSEvent *)theEvent
177 {
179  [self handleMouseEvent:theEvent];
180 }
181 - (void)rightMouseDragged:(NSEvent *)theEvent
182 {
183  if (!(m_buttons & Qt::LeftButton))
184  qWarning("Internal Mousebutton tracking invalid(missing Qt::LeftButton");
185  [self handleMouseEvent:theEvent];
186 }
187 - (void)rightMouseUp:(NSEvent *)theEvent
188 {
189  m_buttons &= QFlag(~int(Qt::RightButton));
190  [self handleMouseEvent:theEvent];
191 }
192 - (void)otherMouseDown:(NSEvent *)theEvent
193 {
195  [self handleMouseEvent:theEvent];
196 }
197 - (void)otherMouseDragged:(NSEvent *)theEvent
198 {
199  if (!(m_buttons & Qt::LeftButton))
200  qWarning("Internal Mousebutton tracking invalid(missing Qt::LeftButton");
201  [self handleMouseEvent:theEvent];
202 }
203 - (void)otherMouseUp:(NSEvent *)theEvent
204 {
205  m_buttons &= QFlag(~int(Qt::MiddleButton));
206  [self handleMouseEvent:theEvent];
207 }
208 
209 
210 
211 @end
QPointer< QWidget > widget
The QFlag class is a helper data type for QFlags.
Definition: qglobal.h:2289
BOOL isFlipped()
Definition: qnsview.mm:128
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
int byteCount() const
Returns the number of bytes occupied by the image data.
Definition: qimage.cpp:1800
struct CGImage * CGImageRef
int bytesPerLine() const
Returns the number of bytes per image scanline.
Definition: qimage.cpp:1812
Q_CORE_EXPORT void qDebug(const char *,...)
unsigned char uchar
Definition: qglobal.h:994
static bool init
CGImageRef m_cgImage
Definition: qnsview.h:50
void handleMouseEvent:(NSEvent *theEvent)
Definition: qnsview.mm:133
static void handleMouseEvent(QWidget *w, const QPoint &local, const QPoint &global, Qt::MouseButtons b)
tlw == 0 means that ev is in global coords only
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
int depth() const
Returns the depth of the image.
Definition: qimage.cpp:1620
unsigned long ulong
Definition: qglobal.h:997
Qt::MouseButtons m_buttons
Definition: qnsview.h:52
struct CGRect CGRect
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
struct CGPoint NSPoint
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
id init()
Definition: qnsview.mm:50
struct CGContext * CGContextRef
static void handleEnterEvent(QWidget *w)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
static void handleLeaveEvent(QWidget *w)
QWidget * m_widget
Definition: qnsview.h:51