Free ShaderEffectSource FBOs when no longer needed.
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickitem_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQUICKITEM_P_H
43 #define QQUICKITEM_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 "qquickitem.h"
57
58 #include "qquickanchors_p.h"
59 #include "qquickanchors_p_p.h"
60 #include "qquickitemchangelistener_p.h"
61
62 #include "qquickcanvas_p.h"
63
64 #include <QtQuick/qsgnode.h>
65 #include "qquickclipnode_p.h"
66
67 #include <private/qpodvector_p.h>
68 #include <QtQuick/private/qquickstate_p.h>
69 #include <private/qqmlnullablevalue_p_p.h>
70 #include <private/qqmlnotifier_p.h>
71 #include <private/qqmlglobal_p.h>
72 #include <private/qlazilyallocated_p.h>
73
74 #include <qqml.h>
75 #include <qqmlcontext.h>
76
77 #include <QtCore/qlist.h>
78 #include <QtCore/qdebug.h>
79 #include <QtCore/qelapsedtimer.h>
80
81 #include <QtQuick/private/qquickshadereffectsource_p.h>
82 #include <QtQuick/private/qquickshadereffect_p.h>
83
84 QT_BEGIN_NAMESPACE
85
86 class QNetworkReply;
87 class QQuickItemKeyFilter;
88 class QQuickLayoutMirroringAttached;
89 class QQuickScreenAttached;
90
91 class QQuickContents : public QQuickItemChangeListener
92 {
93 public:
94     QQuickContents(QQuickItem *item);
95     ~QQuickContents();
96
97     QRectF rectF() const { return QRectF(m_x, m_y, m_width, m_height); }
98
99     inline void calcGeometry(QQuickItem *changed = 0);
100     void complete();
101
102 protected:
103     void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry);
104     void itemDestroyed(QQuickItem *item);
105     void itemChildAdded(QQuickItem *, QQuickItem *);
106     void itemChildRemoved(QQuickItem *, QQuickItem *);
107     //void itemVisibilityChanged(QQuickItem *item)
108
109 private:
110     bool calcHeight(QQuickItem *changed = 0);
111     bool calcWidth(QQuickItem *changed = 0);
112     void updateRect();
113
114     QQuickItem *m_item;
115     qreal m_x;
116     qreal m_y;
117     qreal m_width;
118     qreal m_height;
119 };
120
121 void QQuickContents::calcGeometry(QQuickItem *changed)
122 {
123     bool wChanged = calcWidth(changed);
124     bool hChanged = calcHeight(changed);
125     if (wChanged || hChanged)
126         updateRect();
127 }
128
129 class QQuickTransformPrivate : public QObjectPrivate
130 {
131     Q_DECLARE_PUBLIC(QQuickTransform);
132 public:
133     static QQuickTransformPrivate* get(QQuickTransform *transform) { return transform->d_func(); }
134
135     QQuickTransformPrivate();
136
137     QList<QQuickItem *> items;
138 };
139
140
141 class QQuickItemLayer : public QObject, public QQuickItemChangeListener
142 {
143     Q_OBJECT
144     Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
145     Q_PROPERTY(QSize textureSize READ size WRITE setSize NOTIFY sizeChanged)
146     Q_PROPERTY(QRectF sourceRect READ sourceRect WRITE setSourceRect NOTIFY sourceRectChanged)
147     Q_PROPERTY(bool mipmap READ mipmap WRITE setMipmap NOTIFY mipmapChanged)
148     Q_PROPERTY(bool smooth READ smooth WRITE setSmooth NOTIFY smoothChanged)
149     Q_PROPERTY(QQuickShaderEffectSource::WrapMode wrapMode READ wrapMode WRITE setWrapMode NOTIFY wrapModeChanged)
150     Q_PROPERTY(QQuickShaderEffectSource::Format format READ format WRITE setFormat NOTIFY formatChanged)
151     Q_PROPERTY(QByteArray samplerName READ name WRITE setName NOTIFY nameChanged)
152     Q_PROPERTY(QQmlComponent *effect READ effect WRITE setEffect NOTIFY effectChanged)
153 public:
154     QQuickItemLayer(QQuickItem *item);
155     ~QQuickItemLayer();
156
157     void classBegin();
158     void componentComplete();
159
160     bool enabled() const { return m_enabled; }
161     void setEnabled(bool enabled);
162
163     bool mipmap() const { return m_mipmap; }
164     void setMipmap(bool mipmap);
165
166     bool smooth() const { return m_smooth; }
167     void setSmooth(bool s);
168
169     QSize size() const { return m_size; }
170     void setSize(const QSize &size);
171
172     QQuickShaderEffectSource::Format format() const { return m_format; }
173     void setFormat(QQuickShaderEffectSource::Format f);
174
175     QRectF sourceRect() const { return m_sourceRect; }
176     void setSourceRect(const QRectF &sourceRect);
177
178     QQuickShaderEffectSource::WrapMode wrapMode() const { return m_wrapMode; }
179     void setWrapMode(QQuickShaderEffectSource::WrapMode mode);
180
181     QByteArray name() const { return m_name; }
182     void setName(const QByteArray &name);
183
184     QQmlComponent *effect() const { return m_effectComponent; }
185     void setEffect(QQmlComponent *effect);
186
187     QQuickShaderEffectSource *effectSource() const { return m_effectSource; }
188
189     void itemGeometryChanged(QQuickItem *, const QRectF &, const QRectF &);
190     void itemOpacityChanged(QQuickItem *);
191     void itemParentChanged(QQuickItem *, QQuickItem *);
192     void itemSiblingOrderChanged(QQuickItem *);
193     void itemVisibilityChanged(QQuickItem *);
194
195     void updateMatrix();
196     void updateGeometry();
197     void updateOpacity();
198     void updateZ();
199
200 signals:
201     void enabledChanged(bool enabled);
202     void sizeChanged(const QSize &size);
203     void mipmapChanged(bool mipmap);
204     void wrapModeChanged(QQuickShaderEffectSource::WrapMode mode);
205     void nameChanged(const QByteArray &name);
206     void effectChanged(QQmlComponent *component);
207     void smoothChanged(bool smooth);
208     void formatChanged(QQuickShaderEffectSource::Format format);
209     void sourceRectChanged(const QRectF &sourceRect);
210
211 private:
212     void activate();
213     void deactivate();
214     void activateEffect();
215     void deactivateEffect();
216
217     QQuickItem *m_item;
218     bool m_enabled;
219     bool m_mipmap;
220     bool m_smooth;
221     bool m_componentComplete;
222     QQuickShaderEffectSource::WrapMode m_wrapMode;
223     QQuickShaderEffectSource::Format m_format;
224     QSize m_size;
225     QRectF m_sourceRect;
226     QByteArray m_name;
227     QQmlComponent *m_effectComponent;
228     QQuickItem *m_effect;
229     QQuickShaderEffectSource *m_effectSource;
230 };
231
232 class Q_QUICK_EXPORT QQuickItemPrivate : public QObjectPrivate
233 {
234     Q_DECLARE_PUBLIC(QQuickItem)
235
236 public:
237     static QQuickItemPrivate* get(QQuickItem *item) { return item->d_func(); }
238     static const QQuickItemPrivate* get(const QQuickItem *item) { return item->d_func(); }
239
240     static void registerAccessorProperties();
241
242     QQuickItemPrivate();
243     ~QQuickItemPrivate();
244     void init(QQuickItem *parent);
245
246     QQmlListProperty<QObject> data();
247     QQmlListProperty<QObject> resources();
248     QQmlListProperty<QQuickItem> children();
249     QQmlListProperty<QQuickItem> visibleChildren();
250
251     QQmlListProperty<QQuickState> states();
252     QQmlListProperty<QQuickTransition> transitions();
253
254     QString state() const;
255     void setState(const QString &);
256
257     QQuickAnchorLine left() const;
258     QQuickAnchorLine right() const;
259     QQuickAnchorLine horizontalCenter() const;
260     QQuickAnchorLine top() const;
261     QQuickAnchorLine bottom() const;
262     QQuickAnchorLine verticalCenter() const;
263     QQuickAnchorLine baseline() const;
264
265     QQuickItemLayer *layer() const;
266
267     // data property
268     static void data_append(QQmlListProperty<QObject> *, QObject *);
269     static int data_count(QQmlListProperty<QObject> *);
270     static QObject *data_at(QQmlListProperty<QObject> *, int);
271     static void data_clear(QQmlListProperty<QObject> *);
272
273     // resources property
274     static QObject *resources_at(QQmlListProperty<QObject> *, int);
275     static void resources_append(QQmlListProperty<QObject> *, QObject *);
276     static int resources_count(QQmlListProperty<QObject> *);
277     static void resources_clear(QQmlListProperty<QObject> *);
278
279     // children property
280     static void children_append(QQmlListProperty<QQuickItem> *, QQuickItem *);
281     static int children_count(QQmlListProperty<QQuickItem> *);
282     static QQuickItem *children_at(QQmlListProperty<QQuickItem> *, int);
283     static void children_clear(QQmlListProperty<QQuickItem> *);
284
285     // visibleChildren property
286     static void visibleChildren_append(QQmlListProperty<QQuickItem> *prop, QQuickItem *o);
287     static int visibleChildren_count(QQmlListProperty<QQuickItem> *prop);
288     static QQuickItem *visibleChildren_at(QQmlListProperty<QQuickItem> *prop, int index);
289
290     // transform property
291     static int transform_count(QQmlListProperty<QQuickTransform> *list);
292     static void transform_append(QQmlListProperty<QQuickTransform> *list, QQuickTransform *);
293     static QQuickTransform *transform_at(QQmlListProperty<QQuickTransform> *list, int);
294     static void transform_clear(QQmlListProperty<QQuickTransform> *list);
295
296     enum ChangeType {
297         Geometry = 0x01,
298         SiblingOrder = 0x02,
299         Visibility = 0x04,
300         Opacity = 0x08,
301         Destroyed = 0x10,
302         Parent = 0x20,
303         Children = 0x40,
304         Rotation = 0x80,
305     };
306
307     Q_DECLARE_FLAGS(ChangeTypes, ChangeType)
308
309     enum GeometryChangeType {
310         NoChange = 0,
311         XChange = 0x01,
312         YChange = 0x02,
313         WidthChange = 0x04,
314         HeightChange = 0x08,
315         SizeChange = WidthChange | HeightChange,
316         GeometryChange = XChange | YChange | SizeChange
317     };
318
319     Q_DECLARE_FLAGS(GeometryChangeTypes, GeometryChangeType)
320
321     struct ChangeListener {
322         ChangeListener(QQuickItemChangeListener *l, QQuickItemPrivate::ChangeTypes t) : listener(l), types(t), gTypes(GeometryChange) {}
323         ChangeListener(QQuickItemChangeListener *l, QQuickItemPrivate::GeometryChangeTypes gt) : listener(l), types(Geometry), gTypes(gt) {}
324         QQuickItemChangeListener *listener;
325         QQuickItemPrivate::ChangeTypes types;
326         QQuickItemPrivate::GeometryChangeTypes gTypes;  //NOTE: not used for ==
327         bool operator==(const ChangeListener &other) const { return listener == other.listener && types == other.types; }
328     };
329
330     struct ExtraData {
331         ExtraData();
332
333         qreal z;
334         qreal scale;
335         qreal rotation;
336         qreal opacity;
337
338         QQuickContents *contents;
339         QQuickScreenAttached *screenAttached;
340         QQuickLayoutMirroringAttached* layoutDirectionAttached;
341         QQuickItemKeyFilter *keyHandler;
342         mutable QQuickItemLayer *layer;
343         QPointF userTransformOriginPoint;
344
345         int effectRefCount;
346         int hideRefCount;
347
348         QSGOpacityNode *opacityNode;
349         QQuickDefaultClipNode *clipNode;
350         QSGRootNode *rootNode;
351         QSGNode *beforePaintNode;
352
353         // Although acceptedMouseButtons is inside ExtraData, we actually store
354         // the LeftButton flag in the extra.flag() bit.  This is because it is
355         // extremely common to set acceptedMouseButtons to LeftButton, but very
356         // rare to use any of the other buttons.
357         Qt::MouseButtons acceptedMouseButtons;
358
359         QQuickItem::TransformOrigin origin:5;
360     };
361     QLazilyAllocated<ExtraData> extra;
362
363     QQuickAnchors *anchors() const;
364     mutable QQuickAnchors *_anchors;
365
366     inline Qt::MouseButtons acceptedMouseButtons() const;
367
368     QPODVector<QQuickItemPrivate::ChangeListener,4> changeListeners;
369
370     void addItemChangeListener(QQuickItemChangeListener *listener, ChangeTypes types);
371     void removeItemChangeListener(QQuickItemChangeListener *, ChangeTypes types);
372     void updateOrAddGeometryChangeListener(QQuickItemChangeListener *listener, GeometryChangeTypes types);
373     void updateOrRemoveGeometryChangeListener(QQuickItemChangeListener *listener, GeometryChangeTypes types);
374
375     QQuickStateGroup *_states();
376     QQuickStateGroup *_stateGroup;
377
378     inline QQuickItem::TransformOrigin origin() const;
379
380     // Bit 0
381     quint32 flags:5;
382     bool widthValid:1;
383     bool heightValid:1;
384     bool baselineOffsetValid:1;
385     bool componentComplete:1;
386     bool keepMouse:1;
387     bool keepTouch:1;
388     bool hoverEnabled:1;
389     bool smooth:1;
390     bool focus:1;
391     bool activeFocus:1;
392     bool notifiedFocus:1;
393     // Bit 16
394     bool notifiedActiveFocus:1;
395     bool filtersChildMouseEvents:1;
396     bool explicitVisible:1;
397     bool effectiveVisible:1;
398     bool explicitEnable:1;
399     bool effectiveEnable:1;
400     bool polishScheduled:1;
401     bool inheritedLayoutMirror:1;
402     bool effectiveLayoutMirror:1;
403     bool isMirrorImplicit:1;
404     bool inheritMirrorFromParent:1;
405     bool inheritMirrorFromItem:1;
406     bool childrenDoNotOverlap:1;
407     bool staticSubtreeGeometry:1;
408     bool isAccessible:1;
409     // bool dummy:1
410     // Bit 32
411
412     enum DirtyType {
413         TransformOrigin         = 0x00000001,
414         Transform               = 0x00000002,
415         BasicTransform          = 0x00000004,
416         Position                = 0x00000008,
417         Size                    = 0x00000010,
418
419         ZValue                  = 0x00000020,
420         Content                 = 0x00000040,
421         Smooth                  = 0x00000080,
422         OpacityValue            = 0x00000100,
423         ChildrenChanged         = 0x00000200,
424         ChildrenStackingChanged = 0x00000400,
425         ParentChanged           = 0x00000800,
426
427         Clip                    = 0x00001000,
428         Canvas                  = 0x00002000,
429
430         EffectReference         = 0x00008000,
431         Visible                 = 0x00010000,
432         HideReference           = 0x00020000,
433         PerformanceHints        = 0x00040000,
434         // When you add an attribute here, don't forget to update
435         // dirtyToString()
436
437         TransformUpdateMask     = TransformOrigin | Transform | BasicTransform | Position |
438                                   Size | Canvas,
439         ComplexTransformUpdateMask     = Transform | Canvas,
440         ContentUpdateMask       = Size | Content | Smooth | Canvas,
441         ChildrenUpdateMask      = ChildrenChanged | ChildrenStackingChanged | EffectReference | Canvas
442     };
443
444     quint32 dirtyAttributes;
445     QString dirtyToString() const;
446     void dirty(DirtyType);
447     void addToDirtyList();
448     void removeFromDirtyList();
449     QQuickItem *nextDirtyItem;
450     QQuickItem**prevDirtyItem;
451
452     QQuickCanvas *canvas;
453     int canvasRefCount;
454     inline QSGContext *sceneGraphContext() const;
455
456     QQuickItem *parentItem;
457     QQmlNotifier parentNotifier;
458
459     QList<QQuickItem *> childItems;
460     mutable QList<QQuickItem *> *sortedChildItems;
461     QList<QQuickItem *> paintOrderChildItems() const;
462     void addChild(QQuickItem *);
463     void removeChild(QQuickItem *);
464     void siblingOrderChanged();
465
466     inline void markSortedChildrenDirty(QQuickItem *child);
467
468     class InitializationState {
469     public:
470         QQuickItem *getFocusScope(QQuickItem *item);
471         void clear();
472         void clear(QQuickItem *focusScope);
473     private:
474         QQuickItem *focusScope;
475     };
476
477     void refCanvas(QQuickCanvas *);
478     void refCanvas(InitializationState *, QQuickCanvas *);
479     void derefCanvas();
480
481     QQuickItem *subFocusItem;
482     void updateSubFocusItem(QQuickItem *scope, bool focus);
483
484     QTransform canvasToItemTransform() const;
485     QTransform itemToCanvasTransform() const;
486     void itemToParentTransform(QTransform &) const;
487
488     qreal x;
489     qreal y;
490     qreal width;
491     qreal height;
492     qreal implicitWidth;
493     qreal implicitHeight;
494
495     qreal baselineOffset;
496
497     QList<QQuickTransform *> transforms;
498
499     inline qreal z() const { return extra.isAllocated()?extra->z:0; }
500     inline qreal scale() const { return extra.isAllocated()?extra->scale:1; }
501     inline qreal rotation() const { return extra.isAllocated()?extra->rotation:0; }
502     inline qreal opacity() const { return extra.isAllocated()?extra->opacity:1; }
503
504     void setAccessibleFlagAndListener();
505
506     virtual qreal getImplicitWidth() const;
507     virtual qreal getImplicitHeight() const;
508     virtual void implicitWidthChanged();
509     virtual void implicitHeightChanged();
510
511     void resolveLayoutMirror();
512     void setImplicitLayoutMirror(bool mirror, bool inherit);
513     void setLayoutMirror(bool mirror);
514     bool isMirrored() const {
515         return effectiveLayoutMirror;
516     }
517
518     void emitChildrenRectChanged(const QRectF &rect) {
519         Q_Q(QQuickItem);
520         emit q->childrenRectChanged(rect);
521     }
522
523     QPointF computeTransformOrigin() const;
524     virtual void transformChanged();
525
526     void deliverKeyEvent(QKeyEvent *);
527     void deliverInputMethodEvent(QInputMethodEvent *);
528     void deliverFocusEvent(QFocusEvent *);
529     void deliverMouseEvent(QMouseEvent *);
530     void deliverWheelEvent(QWheelEvent *);
531     void deliverTouchEvent(QTouchEvent *);
532     void deliverHoverEvent(QHoverEvent *);
533     void deliverDragEvent(QEvent *);
534
535     bool calcEffectiveVisible() const;
536     bool setEffectiveVisibleRecur(bool);
537     bool calcEffectiveEnable() const;
538     void setEffectiveEnableRecur(QQuickItem *scope, bool);
539
540
541     inline QSGTransformNode *itemNode();
542     inline QSGNode *childContainerNode();
543
544     /*
545       QSGNode order is:
546          - itemNode
547          - (opacityNode)
548          - (clipNode)
549          - (effectNode)
550          - groupNode
551      */
552
553     QSGOpacityNode *opacityNode() const { return extra.isAllocated()?extra->opacityNode:0; }
554     QQuickDefaultClipNode *clipNode() const { return extra.isAllocated()?extra->clipNode:0; }
555     QSGRootNode *rootNode() const { return extra.isAllocated()?extra->rootNode:0; }
556
557     QSGTransformNode *itemNodeInstance;
558     QSGNode *groupNode;
559     QSGNode *paintNode;
560
561     virtual QSGTransformNode *createTransformNode();
562
563     // A reference from an effect item means that this item is used by the effect, so
564     // it should insert a root node.
565     void refFromEffectItem(bool hide);
566     void derefFromEffectItem(bool unhide);
567
568     void itemChange(QQuickItem::ItemChange, const QQuickItem::ItemChangeData &);
569
570     virtual void mirrorChange() {}
571
572     static qint64 consistentTime;
573     static void setConsistentTime(qint64 t);
574     static void start(QElapsedTimer &);
575     static qint64 elapsed(QElapsedTimer &);
576     static qint64 restart(QElapsedTimer &);
577 };
578
579 /*
580     Key filters can be installed on a QQuickItem, but not removed.  Currently they
581     are only used by attached objects (which are only destroyed on Item
582     destruction), so this isn't a problem.  If in future this becomes any form
583     of public API, they will have to support removal too.
584 */
585 class QQuickItemKeyFilter
586 {
587 public:
588     QQuickItemKeyFilter(QQuickItem * = 0);
589     virtual ~QQuickItemKeyFilter();
590
591     virtual void keyPressed(QKeyEvent *event, bool post);
592     virtual void keyReleased(QKeyEvent *event, bool post);
593     virtual void inputMethodEvent(QInputMethodEvent *event, bool post);
594     virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
595     virtual void componentComplete();
596
597     bool m_processPost;
598
599 private:
600     QQuickItemKeyFilter *m_next;
601 };
602
603 class QQuickKeyNavigationAttachedPrivate : public QObjectPrivate
604 {
605 public:
606     QQuickKeyNavigationAttachedPrivate()
607         : QObjectPrivate(),
608           left(0), right(0), up(0), down(0), tab(0), backtab(0),
609           leftSet(false), rightSet(false), upSet(false), downSet(false),
610           tabSet(false), backtabSet(false) {}
611
612     QQuickItem *left;
613     QQuickItem *right;
614     QQuickItem *up;
615     QQuickItem *down;
616     QQuickItem *tab;
617     QQuickItem *backtab;
618     bool leftSet : 1;
619     bool rightSet : 1;
620     bool upSet : 1;
621     bool downSet : 1;
622     bool tabSet : 1;
623     bool backtabSet : 1;
624 };
625
626 class QQuickKeyNavigationAttached : public QObject, public QQuickItemKeyFilter
627 {
628     Q_OBJECT
629     Q_DECLARE_PRIVATE(QQuickKeyNavigationAttached)
630
631     Q_PROPERTY(QQuickItem *left READ left WRITE setLeft NOTIFY leftChanged)
632     Q_PROPERTY(QQuickItem *right READ right WRITE setRight NOTIFY rightChanged)
633     Q_PROPERTY(QQuickItem *up READ up WRITE setUp NOTIFY upChanged)
634     Q_PROPERTY(QQuickItem *down READ down WRITE setDown NOTIFY downChanged)
635     Q_PROPERTY(QQuickItem *tab READ tab WRITE setTab NOTIFY tabChanged)
636     Q_PROPERTY(QQuickItem *backtab READ backtab WRITE setBacktab NOTIFY backtabChanged)
637     Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
638
639     Q_ENUMS(Priority)
640
641 public:
642     QQuickKeyNavigationAttached(QObject * = 0);
643
644     QQuickItem *left() const;
645     void setLeft(QQuickItem *);
646     QQuickItem *right() const;
647     void setRight(QQuickItem *);
648     QQuickItem *up() const;
649     void setUp(QQuickItem *);
650     QQuickItem *down() const;
651     void setDown(QQuickItem *);
652     QQuickItem *tab() const;
653     void setTab(QQuickItem *);
654     QQuickItem *backtab() const;
655     void setBacktab(QQuickItem *);
656
657     enum Priority { BeforeItem, AfterItem };
658     Priority priority() const;
659     void setPriority(Priority);
660
661     static QQuickKeyNavigationAttached *qmlAttachedProperties(QObject *);
662
663 Q_SIGNALS:
664     void leftChanged();
665     void rightChanged();
666     void upChanged();
667     void downChanged();
668     void tabChanged();
669     void backtabChanged();
670     void priorityChanged();
671
672 private:
673     virtual void keyPressed(QKeyEvent *event, bool post);
674     virtual void keyReleased(QKeyEvent *event, bool post);
675     void setFocusNavigation(QQuickItem *currentItem, const char *dir);
676 };
677
678 class QQuickLayoutMirroringAttached : public QObject
679 {
680     Q_OBJECT
681     Q_PROPERTY(bool enabled READ enabled WRITE setEnabled RESET resetEnabled NOTIFY enabledChanged)
682     Q_PROPERTY(bool childrenInherit READ childrenInherit WRITE setChildrenInherit NOTIFY childrenInheritChanged)
683
684 public:
685     explicit QQuickLayoutMirroringAttached(QObject *parent = 0);
686
687     bool enabled() const;
688     void setEnabled(bool);
689     void resetEnabled();
690
691     bool childrenInherit() const;
692     void setChildrenInherit(bool);
693
694     static QQuickLayoutMirroringAttached *qmlAttachedProperties(QObject *);
695 Q_SIGNALS:
696     void enabledChanged();
697     void childrenInheritChanged();
698 private:
699     friend class QQuickItemPrivate;
700     QQuickItemPrivate *itemPrivate;
701 };
702
703 class QQuickKeysAttachedPrivate : public QObjectPrivate
704 {
705 public:
706     QQuickKeysAttachedPrivate()
707         : QObjectPrivate(), inPress(false), inRelease(false)
708         , inIM(false), enabled(true), imeItem(0), item(0)
709     {}
710
711     bool isConnected(const char *signalName);
712
713     //loop detection
714     bool inPress:1;
715     bool inRelease:1;
716     bool inIM:1;
717
718     bool enabled : 1;
719
720     QQuickItem *imeItem;
721     QList<QQuickItem *> targets;
722     QQuickItem *item;
723 };
724
725 class QQuickKeysAttached : public QObject, public QQuickItemKeyFilter
726 {
727     Q_OBJECT
728     Q_DECLARE_PRIVATE(QQuickKeysAttached)
729
730     Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
731     Q_PROPERTY(QQmlListProperty<QQuickItem> forwardTo READ forwardTo)
732     Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
733
734     Q_ENUMS(Priority)
735
736 public:
737     QQuickKeysAttached(QObject *parent=0);
738     ~QQuickKeysAttached();
739
740     bool enabled() const { Q_D(const QQuickKeysAttached); return d->enabled; }
741     void setEnabled(bool enabled) {
742         Q_D(QQuickKeysAttached);
743         if (enabled != d->enabled) {
744             d->enabled = enabled;
745             emit enabledChanged();
746         }
747     }
748
749     enum Priority { BeforeItem, AfterItem};
750     Priority priority() const;
751     void setPriority(Priority);
752
753     QQmlListProperty<QQuickItem> forwardTo() {
754         Q_D(QQuickKeysAttached);
755         return QQmlListProperty<QQuickItem>(this, d->targets);
756     }
757
758     virtual void componentComplete();
759
760     static QQuickKeysAttached *qmlAttachedProperties(QObject *);
761
762 Q_SIGNALS:
763     void enabledChanged();
764     void priorityChanged();
765     void pressed(QQuickKeyEvent *event);
766     void released(QQuickKeyEvent *event);
767     void digit0Pressed(QQuickKeyEvent *event);
768     void digit1Pressed(QQuickKeyEvent *event);
769     void digit2Pressed(QQuickKeyEvent *event);
770     void digit3Pressed(QQuickKeyEvent *event);
771     void digit4Pressed(QQuickKeyEvent *event);
772     void digit5Pressed(QQuickKeyEvent *event);
773     void digit6Pressed(QQuickKeyEvent *event);
774     void digit7Pressed(QQuickKeyEvent *event);
775     void digit8Pressed(QQuickKeyEvent *event);
776     void digit9Pressed(QQuickKeyEvent *event);
777
778     void leftPressed(QQuickKeyEvent *event);
779     void rightPressed(QQuickKeyEvent *event);
780     void upPressed(QQuickKeyEvent *event);
781     void downPressed(QQuickKeyEvent *event);
782     void tabPressed(QQuickKeyEvent *event);
783     void backtabPressed(QQuickKeyEvent *event);
784
785     void asteriskPressed(QQuickKeyEvent *event);
786     void numberSignPressed(QQuickKeyEvent *event);
787     void escapePressed(QQuickKeyEvent *event);
788     void returnPressed(QQuickKeyEvent *event);
789     void enterPressed(QQuickKeyEvent *event);
790     void deletePressed(QQuickKeyEvent *event);
791     void spacePressed(QQuickKeyEvent *event);
792     void backPressed(QQuickKeyEvent *event);
793     void cancelPressed(QQuickKeyEvent *event);
794     void selectPressed(QQuickKeyEvent *event);
795     void yesPressed(QQuickKeyEvent *event);
796     void noPressed(QQuickKeyEvent *event);
797     void context1Pressed(QQuickKeyEvent *event);
798     void context2Pressed(QQuickKeyEvent *event);
799     void context3Pressed(QQuickKeyEvent *event);
800     void context4Pressed(QQuickKeyEvent *event);
801     void callPressed(QQuickKeyEvent *event);
802     void hangupPressed(QQuickKeyEvent *event);
803     void flipPressed(QQuickKeyEvent *event);
804     void menuPressed(QQuickKeyEvent *event);
805     void volumeUpPressed(QQuickKeyEvent *event);
806     void volumeDownPressed(QQuickKeyEvent *event);
807
808 private:
809     virtual void keyPressed(QKeyEvent *event, bool post);
810     virtual void keyReleased(QKeyEvent *event, bool post);
811     virtual void inputMethodEvent(QInputMethodEvent *, bool post);
812     virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
813
814     const QByteArray keyToSignal(int key) {
815         QByteArray keySignal;
816         if (key >= Qt::Key_0 && key <= Qt::Key_9) {
817             keySignal = "digit0Pressed";
818             keySignal[5] = '0' + (key - Qt::Key_0);
819         } else {
820             int i = 0;
821             while (sigMap[i].key && sigMap[i].key != key)
822                 ++i;
823             keySignal = sigMap[i].sig;
824         }
825         return keySignal;
826     }
827
828     struct SigMap {
829         int key;
830         const char *sig;
831     };
832
833     static const SigMap sigMap[];
834 };
835
836 Qt::MouseButtons QQuickItemPrivate::acceptedMouseButtons() const
837 {
838     return ((extra.flag() ? Qt::LeftButton : Qt::MouseButton(0)) |
839             (extra.isAllocated() ? extra->acceptedMouseButtons : Qt::MouseButtons(0)));
840 }
841
842 QSGContext *QQuickItemPrivate::sceneGraphContext() const
843 {
844     Q_ASSERT(canvas);
845     return static_cast<QQuickCanvasPrivate *>(QObjectPrivate::get(canvas))->context;
846 }
847
848 void QQuickItemPrivate::markSortedChildrenDirty(QQuickItem *child)
849 {
850     // If sortedChildItems == &childItems then all in childItems have z == 0
851     // and we don't need to invalidate if the changed item also has z == 0.
852     if (child->z() != 0. || sortedChildItems != &childItems) {
853         if (sortedChildItems != &childItems)
854             delete sortedChildItems;
855         sortedChildItems = 0;
856     }
857 }
858
859 QQuickItem::TransformOrigin QQuickItemPrivate::origin() const
860 {
861     return extra.isAllocated()?extra->origin:QQuickItem::Center;
862 }
863
864 inline void QQuickItemPrivate::refCanvas(QQuickCanvas *c)
865 {
866     QQuickItemPrivate::InitializationState initState;
867     initState.clear();
868     refCanvas(&initState, c);
869 }
870
871 QSGTransformNode *QQuickItemPrivate::itemNode()
872 {
873     if (!itemNodeInstance) {
874         itemNodeInstance = createTransformNode();
875         itemNodeInstance->setFlag(QSGNode::OwnedByParent, false);
876 #ifdef QML_RUNTIME_TESTING
877         Q_Q(QQuickItem);
878         itemNodeInstance->description = QString::fromLatin1("QQuickItem(%1)").arg(QString::fromLatin1(q->metaObject()->className()));
879 #endif
880     }
881     return itemNodeInstance;
882 }
883
884 QSGNode *QQuickItemPrivate::childContainerNode()
885 {
886     if (!groupNode) {
887         groupNode = new QSGNode();
888         if (rootNode())
889             rootNode()->appendChildNode(groupNode);
890         else if (clipNode())
891             clipNode()->appendChildNode(groupNode);
892         else if (opacityNode())
893             opacityNode()->appendChildNode(groupNode);
894         else
895             itemNode()->appendChildNode(groupNode);
896         groupNode->setFlag(QSGNode::ChildrenDoNotOverlap, childrenDoNotOverlap);
897         groupNode->setFlag(QSGNode::StaticSubtreeGeometry, staticSubtreeGeometry);
898 #ifdef QML_RUNTIME_TESTING
899         groupNode->description = QLatin1String("group");
900 #endif
901     }
902     return groupNode;
903 }
904
905 Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickItemPrivate::ChangeTypes);
906
907 QT_END_NAMESPACE
908
909 QML_DECLARE_TYPE(QQuickItemLayer)
910 QML_DECLARE_TYPE(QQuickKeysAttached)
911 QML_DECLARE_TYPEINFO(QQuickKeysAttached, QML_HAS_ATTACHED_PROPERTIES)
912 QML_DECLARE_TYPE(QQuickKeyNavigationAttached)
913 QML_DECLARE_TYPEINFO(QQuickKeyNavigationAttached, QML_HAS_ATTACHED_PROPERTIES)
914 QML_DECLARE_TYPE(QQuickLayoutMirroringAttached)
915 QML_DECLARE_TYPEINFO(QQuickLayoutMirroringAttached, QML_HAS_ATTACHED_PROPERTIES)
916
917 #endif // QQUICKITEM_P_H