Add DropArea item and Drag attached property.
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgcanvas_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QSGCANVAS_P_H
43 #define QSGCANVAS_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "qsgitem.h"
57 #include "qsgcanvas.h"
58 #include <private/qdeclarativeguard_p.h>
59
60 #include <private/qsgcontext_p.h>
61 #include <private/qsgdrag_p.h>
62
63 #include <QtCore/qthread.h>
64 #include <QtCore/qmutex.h>
65 #include <QtCore/qwaitcondition.h>
66 #include <private/qwindow_p.h>
67 #include <private/qopengl_p.h>
68 #include <qopenglcontext.h>
69 #include <QtGui/qopenglframebufferobject.h>
70 #include <QtGui/qevent.h>
71 #include <QtGui/qinputpanel.h>
72
73 QT_BEGIN_NAMESPACE
74
75 //Make it easy to identify and customize the root item if needed
76 class QSGRootItem : public QSGItem
77 {
78     Q_OBJECT
79 public:
80     QSGRootItem();
81 };
82
83 class QSGCanvasPrivate;
84
85 class QTouchEvent;
86 class QSGCanvasRenderLoop;
87 class QSGCanvasIncubationController;
88
89 class QSGCanvasPrivate : public QWindowPrivate
90 {
91 public:
92     Q_DECLARE_PUBLIC(QSGCanvas)
93
94     static inline QSGCanvasPrivate *get(QSGCanvas *c) { return c->d_func(); }
95
96     QSGCanvasPrivate();
97     virtual ~QSGCanvasPrivate();
98
99     void init(QSGCanvas *);
100
101     QSGRootItem *rootItem;
102
103     QSGItem *activeFocusItem;
104     QSGItem *mouseGrabberItem;
105     QSGDragGrabber dragGrabber;
106
107     // Mouse positions are saved in widget coordinates
108     QPointF lastMousePosition;
109     void translateTouchEvent(QTouchEvent *touchEvent);
110     static void transformTouchPoints(QList<QTouchEvent::TouchPoint> &touchPoints, const QTransform &transform);
111     bool deliverInitialMousePressEvent(QSGItem *, QMouseEvent *);
112     bool deliverMouseEvent(QMouseEvent *);
113     bool sendFilteredMouseEvent(QSGItem *, QSGItem *, QMouseEvent *);
114     bool deliverWheelEvent(QSGItem *, QWheelEvent *);
115     bool deliverTouchPoints(QSGItem *, QTouchEvent *, const QList<QTouchEvent::TouchPoint> &, QSet<int> *,
116             QHash<QSGItem *, QList<QTouchEvent::TouchPoint> > *);
117     bool deliverTouchEvent(QTouchEvent *);
118     bool deliverHoverEvent(QSGItem *, const QPointF &scenePos, const QPointF &lastScenePos, Qt::KeyboardModifiers modifiers, bool &accepted);
119     bool sendHoverEvent(QEvent::Type, QSGItem *, const QPointF &scenePos, const QPointF &lastScenePos,
120                         Qt::KeyboardModifiers modifiers, bool accepted);
121     bool clearHover();
122     void deliverDragEvent(QSGDragGrabber *, QEvent *);
123     bool deliverDragEvent(QSGDragGrabber *, QSGItem *, QDragMoveEvent *);
124
125     QList<QSGItem*> hoverItems;
126     enum FocusOption {
127         DontChangeFocusProperty = 0x01,
128     };
129     Q_DECLARE_FLAGS(FocusOptions, FocusOption)
130
131     void setFocusInScope(QSGItem *scope, QSGItem *item, FocusOptions = 0);
132     void clearFocusInScope(QSGItem *scope, QSGItem *item, FocusOptions = 0);
133     void notifyFocusChangesRecur(QSGItem **item, int remaining);
134
135     void updateInputMethodData();
136     void updateFocusItemTransform();
137
138     void dirtyItem(QSGItem *);
139     void cleanup(QSGNode *);
140
141     void initializeSceneGraph();
142     void polishItems();
143     void syncSceneGraph();
144     void renderSceneGraph(const QSize &size);
145
146     QSGItem::UpdatePaintNodeData updatePaintNodeData;
147
148     QSGItem *dirtyItemList;
149     QList<QSGNode *> cleanupNodeList;
150
151     QSet<QSGItem *> itemsToPolish;
152
153     void updateDirtyNodes();
154     void cleanupNodes();
155     bool updateEffectiveOpacity(QSGItem *);
156     void updateEffectiveOpacityRoot(QSGItem *, qreal);
157     void updateDirtyNode(QSGItem *);
158
159     QSGContext *context;
160
161     uint vsyncAnimations : 1;
162
163     QSGCanvasRenderLoop *thread;
164     QSize widgetSize;
165     QSize viewportSize;
166
167     QAnimationDriver *animationDriver;
168
169     QOpenGLFramebufferObject *renderTarget;
170
171     QHash<int, QSGItem *> itemForTouchPointId;
172
173     mutable QSGCanvasIncubationController *incubationController;
174 };
175
176 class QSGCanvasRenderLoop
177 {
178 public:
179     QSGCanvasRenderLoop()
180         : d(0)
181         , renderer(0)
182         , gl(0)
183     {
184     }
185     virtual ~QSGCanvasRenderLoop()
186     {
187         delete gl;
188     }
189
190     friend class QSGCanvasPrivate;
191
192     virtual void paint() = 0;
193     virtual void resize(const QSize &size) = 0;
194     virtual void startRendering() = 0;
195     virtual void stopRendering() = 0;
196     virtual QImage grab() = 0;
197     virtual void setWindowSize(const QSize &size) = 0;
198     virtual void maybeUpdate() = 0;
199     virtual bool isRunning() const = 0;
200     virtual void animationStarted() = 0;
201     virtual void animationStopped() = 0;
202     virtual void moveContextToThread(QSGContext *) { }
203     virtual bool *allowMainThreadProcessing() { return 0; }
204
205 protected:
206     void initializeSceneGraph() { d->initializeSceneGraph(); }
207     void syncSceneGraph() { d->syncSceneGraph(); }
208     void renderSceneGraph(const QSize &size) { d->renderSceneGraph(size); }
209     void polishItems() { d->polishItems(); }
210     QAnimationDriver *animationDriver() const { return d->animationDriver; }
211
212     inline QOpenGLContext *glContext() const { return gl; }
213     void createGLContext();
214     void makeCurrent() { gl->makeCurrent(renderer); }
215     void doneCurrent() { gl->doneCurrent(); }
216     void swapBuffers() {
217         gl->swapBuffers(renderer);
218         emit renderer->frameSwapped();
219     }
220
221 private:
222     QSGCanvasPrivate *d;
223     QSGCanvas *renderer;
224
225     QOpenGLContext *gl;
226 };
227
228 class QSGCanvasRenderThread : public QThread, public QSGCanvasRenderLoop
229 {
230     Q_OBJECT
231 public:
232     QSGCanvasRenderThread()
233         : mutex(QMutex::NonRecursive)
234         , allowMainThreadProcessingFlag(true)
235         , animationRunning(false)
236         , isGuiBlocked(0)
237         , isPaintCompleted(false)
238         , isGuiBlockPending(false)
239         , isRenderBlocked(false)
240         , isExternalUpdatePending(false)
241         , syncAlreadyHappened(false)
242         , inSync(false)
243         , doGrab(false)
244         , shouldExit(false)
245         , hasExited(false)
246         , renderThreadAwakened(false)
247     {}
248
249     inline void lock() { mutex.lock(); }
250     inline void unlock() { mutex.unlock(); }
251     inline void wait() { condition.wait(&mutex); }
252     inline void wake() { condition.wakeOne(); }
253
254     void lockInGui();
255     void unlockInGui();
256
257     void paint();
258     void resize(const QSize &size);
259     void startRendering();
260     void stopRendering();
261     void exhaustSyncEvent();
262     void sync(bool guiAlreadyLocked);
263     bool isRunning() const { return QThread::isRunning(); }
264     void setWindowSize(const QSize &size) { windowSize = size; }
265     void maybeUpdate();
266     void moveContextToThread(QSGContext *c) { c->moveToThread(this); }
267     bool *allowMainThreadProcessing() { return &allowMainThreadProcessingFlag; }
268
269     bool event(QEvent *);
270
271     QImage grab();
272
273 public slots:
274     void animationStarted();
275     void animationStopped();
276
277 public:
278     QMutex mutex;
279     QWaitCondition condition;
280
281     bool allowMainThreadProcessingFlag;
282
283     QSize windowSize;
284     QSize renderedSize;
285
286     uint animationRunning: 1;
287     int isGuiBlocked;
288     uint isPaintCompleted : 1;
289     uint isGuiBlockPending : 1;
290     uint isRenderBlocked : 1;
291     uint isExternalUpdatePending : 1;
292     uint syncAlreadyHappened : 1;
293     uint inSync : 1;
294     uint doGrab : 1;
295     uint shouldExit : 1;
296     uint hasExited : 1;
297     uint renderThreadAwakened : 1;
298
299     QImage grabContent;
300
301     void run();
302 };
303
304 Q_DECLARE_OPERATORS_FOR_FLAGS(QSGCanvasPrivate::FocusOptions)
305
306 QT_END_NAMESPACE
307
308 #endif // QSGCANVAS_P_H