1 // Commit: 5c783d0a9a912816813945387903857a314040b5
2 /****************************************************************************
4 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 ** All rights reserved.
6 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 ** This file is part of the QtDeclarative module of the Qt Toolkit.
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this
15 ** file. Please review the following information to ensure the GNU Lesser
16 ** General Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
23 ** GNU General Public License Usage
24 ** Alternatively, this file may be used under the terms of the GNU General
25 ** Public License version 3.0 as published by the Free Software Foundation
26 ** and appearing in the file LICENSE.GPL included in the packaging of this
27 ** file. Please review the following information to ensure the GNU General
28 ** Public License version 3.0 requirements will be met:
29 ** http://www.gnu.org/copyleft/gpl.html.
32 ** Alternatively, this file may be used in accordance with the terms and
33 ** conditions contained in a signed written agreement between you and Nokia.
41 ****************************************************************************/
43 #ifndef QQUICKITEM_P_H
44 #define QQUICKITEM_P_H
50 // This file is not part of the Qt API. It exists purely as an
51 // implementation detail. This header file may change from version to
52 // version without notice, or even be removed.
57 #include "qquickitem.h"
59 #include "qquickanchors_p.h"
60 #include "qquickanchors_p_p.h"
61 #include "qquickitemchangelistener_p.h"
63 #include "qquickcanvas_p.h"
66 #include "qquickclipnode_p.h"
68 #include <private/qpodvector_p.h>
69 #include <private/qdeclarativestate_p.h>
70 #include <private/qdeclarativenullablevalue_p_p.h>
71 #include <private/qdeclarativenotifier_p.h>
72 #include <private/qdeclarativeglobal_p.h>
74 #include <qdeclarative.h>
75 #include <qdeclarativecontext.h>
77 #include <QtCore/qlist.h>
78 #include <QtCore/qdebug.h>
79 #include <QtCore/qelapsedtimer.h>
84 class QQuickItemKeyFilter;
85 class QQuickLayoutMirroringAttached;
87 //### merge into private?
88 class QQuickContents : public QObject, public QQuickItemChangeListener
92 QQuickContents(QQuickItem *item);
97 void childRemoved(QQuickItem *item);
98 void childAdded(QQuickItem *item);
100 void calcGeometry() { calcWidth(); calcHeight(); }
104 void rectChanged(QRectF);
107 void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry);
108 void itemDestroyed(QQuickItem *item);
109 //void itemVisibilityChanged(QQuickItem *item)
112 void calcHeight(QQuickItem *changed = 0);
113 void calcWidth(QQuickItem *changed = 0);
122 class QQuickTransformPrivate : public QObjectPrivate
124 Q_DECLARE_PUBLIC(QQuickTransform);
126 static QQuickTransformPrivate* get(QQuickTransform *transform) { return transform->d_func(); }
128 QQuickTransformPrivate();
130 QList<QQuickItem *> items;
133 class Q_DECLARATIVE_EXPORT QQuickItemPrivate : public QObjectPrivate
135 Q_DECLARE_PUBLIC(QQuickItem)
138 static QQuickItemPrivate* get(QQuickItem *item) { return item->d_func(); }
139 static const QQuickItemPrivate* get(const QQuickItem *item) { return item->d_func(); }
142 void init(QQuickItem *parent);
144 QDeclarativeListProperty<QObject> data();
145 QDeclarativeListProperty<QObject> resources();
146 QDeclarativeListProperty<QQuickItem> children();
148 QDeclarativeListProperty<QDeclarativeState> states();
149 QDeclarativeListProperty<QDeclarativeTransition> transitions();
151 QString state() const;
152 void setState(const QString &);
154 QQuickAnchorLine left() const;
155 QQuickAnchorLine right() const;
156 QQuickAnchorLine horizontalCenter() const;
157 QQuickAnchorLine top() const;
158 QQuickAnchorLine bottom() const;
159 QQuickAnchorLine verticalCenter() const;
160 QQuickAnchorLine baseline() const;
163 static void data_append(QDeclarativeListProperty<QObject> *, QObject *);
164 static int data_count(QDeclarativeListProperty<QObject> *);
165 static QObject *data_at(QDeclarativeListProperty<QObject> *, int);
166 static void data_clear(QDeclarativeListProperty<QObject> *);
168 // resources property
169 static QObject *resources_at(QDeclarativeListProperty<QObject> *, int);
170 static void resources_append(QDeclarativeListProperty<QObject> *, QObject *);
171 static int resources_count(QDeclarativeListProperty<QObject> *);
172 static void resources_clear(QDeclarativeListProperty<QObject> *);
175 static void children_append(QDeclarativeListProperty<QQuickItem> *, QQuickItem *);
176 static int children_count(QDeclarativeListProperty<QQuickItem> *);
177 static QQuickItem *children_at(QDeclarativeListProperty<QQuickItem> *, int);
178 static void children_clear(QDeclarativeListProperty<QQuickItem> *);
180 // transform property
181 static int transform_count(QDeclarativeListProperty<QQuickTransform> *list);
182 static void transform_append(QDeclarativeListProperty<QQuickTransform> *list, QQuickTransform *);
183 static QQuickTransform *transform_at(QDeclarativeListProperty<QQuickTransform> *list, int);
184 static void transform_clear(QDeclarativeListProperty<QQuickTransform> *list);
186 QQuickAnchors *anchors() const;
187 mutable QQuickAnchors *_anchors;
188 QQuickContents *_contents;
190 QDeclarativeNullableValue<qreal> baselineOffset;
193 AnchorLines(QQuickItem *);
194 QQuickAnchorLine left;
195 QQuickAnchorLine right;
196 QQuickAnchorLine hCenter;
197 QQuickAnchorLine top;
198 QQuickAnchorLine bottom;
199 QQuickAnchorLine vCenter;
200 QQuickAnchorLine baseline;
202 mutable AnchorLines *_anchorLines;
203 AnchorLines *anchorLines() const;
216 Q_DECLARE_FLAGS(ChangeTypes, ChangeType)
218 struct ChangeListener {
219 ChangeListener(QQuickItemChangeListener *l, QQuickItemPrivate::ChangeTypes t) : listener(l), types(t) {}
220 QQuickItemChangeListener *listener;
221 QQuickItemPrivate::ChangeTypes types;
222 bool operator==(const ChangeListener &other) const { return listener == other.listener && types == other.types; }
225 void addItemChangeListener(QQuickItemChangeListener *listener, ChangeTypes types) {
226 changeListeners.append(ChangeListener(listener, types));
228 void removeItemChangeListener(QQuickItemChangeListener *, ChangeTypes types);
229 QPODVector<ChangeListener,4> changeListeners;
231 QDeclarativeStateGroup *_states();
232 QDeclarativeStateGroup *_stateGroup;
234 QQuickItem::TransformOrigin origin:5;
238 bool componentComplete:1;
244 bool notifiedFocus:1;
245 bool notifiedActiveFocus:1;
246 bool filtersChildMouseEvents:1;
247 bool explicitVisible:1;
248 bool effectiveVisible:1;
249 bool explicitEnable:1;
250 bool effectiveEnable:1;
251 bool polishScheduled:1;
252 bool inheritedLayoutMirror:1;
253 bool effectiveLayoutMirror:1;
254 bool isMirrorImplicit:1;
255 bool inheritMirrorFromParent:1;
256 bool inheritMirrorFromItem:1;
257 bool childrenDoNotOverlap:1;
260 QQuickCanvas *canvas;
261 QSGContext *sceneGraphContext() const { Q_ASSERT(canvas); return static_cast<QQuickCanvasPrivate *>(QObjectPrivate::get(canvas))->context; }
263 QQuickItem *parentItem;
264 QList<QQuickItem *> childItems;
265 QList<QQuickItem *> paintOrderChildItems() const;
266 void addChild(QQuickItem *);
267 void removeChild(QQuickItem *);
268 void siblingOrderChanged();
270 class InitializationState {
272 QQuickItem *getFocusScope(QQuickItem *item);
274 void clear(QQuickItem *focusScope);
276 QQuickItem *focusScope;
278 void initCanvas(InitializationState *, QQuickCanvas *);
280 QQuickItem *subFocusItem;
282 QTransform canvasToItemTransform() const;
283 QTransform itemToCanvasTransform() const;
284 void itemToParentTransform(QTransform &) const;
291 qreal implicitHeight;
298 QQuickLayoutMirroringAttached* attachedLayoutDirection;
300 Qt::MouseButtons acceptedMouseButtons;
301 Qt::InputMethodHints imHints;
303 QPointF transformOriginPoint;
305 virtual qreal getImplicitWidth() const;
306 virtual qreal getImplicitHeight() const;
307 virtual void implicitWidthChanged();
308 virtual void implicitHeightChanged();
310 void resolveLayoutMirror();
311 void setImplicitLayoutMirror(bool mirror, bool inherit);
312 void setLayoutMirror(bool mirror);
313 bool isMirrored() const {
314 return effectiveLayoutMirror;
317 QPointF computeTransformOrigin() const;
318 QList<QQuickTransform *> transforms;
319 virtual void transformChanged();
321 QQuickItemKeyFilter *keyHandler;
322 void deliverKeyEvent(QKeyEvent *);
323 void deliverInputMethodEvent(QInputMethodEvent *);
324 void deliverFocusEvent(QFocusEvent *);
325 void deliverMouseEvent(QMouseEvent *);
326 void deliverWheelEvent(QWheelEvent *);
327 void deliverTouchEvent(QTouchEvent *);
328 void deliverHoverEvent(QHoverEvent *);
329 void deliverDragEvent(QEvent *);
331 bool calcEffectiveVisible() const;
332 void setEffectiveVisibleRecur(bool);
333 bool calcEffectiveEnable() const;
334 void setEffectiveEnableRecur(bool);
338 TransformOrigin = 0x00000001,
339 Transform = 0x00000002,
340 BasicTransform = 0x00000004,
341 Position = 0x00000008,
345 Content = 0x00000040,
347 OpacityValue = 0x00000100,
348 ChildrenChanged = 0x00000200,
349 ChildrenStackingChanged = 0x00000400,
350 ParentChanged = 0x00000800,
355 EffectReference = 0x00008000,
356 Visible = 0x00010000,
357 HideReference = 0x00020000,
358 // When you add an attribute here, don't forget to update
361 TransformUpdateMask = TransformOrigin | Transform | BasicTransform | Position | Size | Canvas,
362 ComplexTransformUpdateMask = Transform | Canvas,
363 ContentUpdateMask = Size | Content | Smooth | Canvas,
364 ChildrenUpdateMask = ChildrenChanged | ChildrenStackingChanged | EffectReference | Canvas,
367 quint32 dirtyAttributes;
368 QString dirtyToString() const;
369 void dirty(DirtyType);
370 void addToDirtyList();
371 void removeFromDirtyList();
372 QQuickItem *nextDirtyItem;
373 QQuickItem**prevDirtyItem;
375 inline QSGTransformNode *itemNode();
376 inline QSGNode *childContainerNode();
387 QSGTransformNode *itemNodeInstance;
388 QSGOpacityNode *opacityNode;
389 QQuickDefaultClipNode *clipNode;
390 QSGRootNode *rootNode;
393 QSGNode *beforePaintNode;
395 virtual QSGTransformNode *createTransformNode();
397 // A reference from an effect item means that this item is used by the effect, so
398 // it should insert a root node.
399 void refFromEffectItem(bool hide);
400 void derefFromEffectItem(bool unhide);
404 void itemChange(QQuickItem::ItemChange, const QQuickItem::ItemChangeData &);
406 virtual void mirrorChange() {}
408 static qint64 consistentTime;
409 static void setConsistentTime(qint64 t);
410 static void start(QElapsedTimer &);
411 static qint64 elapsed(QElapsedTimer &);
412 static qint64 restart(QElapsedTimer &);
416 Key filters can be installed on a QQuickItem, but not removed. Currently they
417 are only used by attached objects (which are only destroyed on Item
418 destruction), so this isn't a problem. If in future this becomes any form
419 of public API, they will have to support removal too.
421 class QQuickItemKeyFilter
424 QQuickItemKeyFilter(QQuickItem * = 0);
425 virtual ~QQuickItemKeyFilter();
427 virtual void keyPressed(QKeyEvent *event, bool post);
428 virtual void keyReleased(QKeyEvent *event, bool post);
429 virtual void inputMethodEvent(QInputMethodEvent *event, bool post);
430 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
431 virtual void componentComplete();
436 QQuickItemKeyFilter *m_next;
439 class QQuickKeyNavigationAttachedPrivate : public QObjectPrivate
442 QQuickKeyNavigationAttachedPrivate()
444 left(0), right(0), up(0), down(0), tab(0), backtab(0),
445 leftSet(false), rightSet(false), upSet(false), downSet(false),
446 tabSet(false), backtabSet(false) {}
462 class QQuickKeyNavigationAttached : public QObject, public QQuickItemKeyFilter
465 Q_DECLARE_PRIVATE(QQuickKeyNavigationAttached)
467 Q_PROPERTY(QQuickItem *left READ left WRITE setLeft NOTIFY leftChanged)
468 Q_PROPERTY(QQuickItem *right READ right WRITE setRight NOTIFY rightChanged)
469 Q_PROPERTY(QQuickItem *up READ up WRITE setUp NOTIFY upChanged)
470 Q_PROPERTY(QQuickItem *down READ down WRITE setDown NOTIFY downChanged)
471 Q_PROPERTY(QQuickItem *tab READ tab WRITE setTab NOTIFY tabChanged)
472 Q_PROPERTY(QQuickItem *backtab READ backtab WRITE setBacktab NOTIFY backtabChanged)
473 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
478 QQuickKeyNavigationAttached(QObject * = 0);
480 QQuickItem *left() const;
481 void setLeft(QQuickItem *);
482 QQuickItem *right() const;
483 void setRight(QQuickItem *);
484 QQuickItem *up() const;
485 void setUp(QQuickItem *);
486 QQuickItem *down() const;
487 void setDown(QQuickItem *);
488 QQuickItem *tab() const;
489 void setTab(QQuickItem *);
490 QQuickItem *backtab() const;
491 void setBacktab(QQuickItem *);
493 enum Priority { BeforeItem, AfterItem };
494 Priority priority() const;
495 void setPriority(Priority);
497 static QQuickKeyNavigationAttached *qmlAttachedProperties(QObject *);
505 void backtabChanged();
506 void priorityChanged();
509 virtual void keyPressed(QKeyEvent *event, bool post);
510 virtual void keyReleased(QKeyEvent *event, bool post);
511 void setFocusNavigation(QQuickItem *currentItem, const char *dir);
514 class QQuickLayoutMirroringAttached : public QObject
517 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled RESET resetEnabled NOTIFY enabledChanged)
518 Q_PROPERTY(bool childrenInherit READ childrenInherit WRITE setChildrenInherit NOTIFY childrenInheritChanged)
521 explicit QQuickLayoutMirroringAttached(QObject *parent = 0);
523 bool enabled() const;
524 void setEnabled(bool);
527 bool childrenInherit() const;
528 void setChildrenInherit(bool);
530 static QQuickLayoutMirroringAttached *qmlAttachedProperties(QObject *);
532 void enabledChanged();
533 void childrenInheritChanged();
535 friend class QQuickItemPrivate;
536 QQuickItemPrivate *itemPrivate;
539 class QQuickKeysAttachedPrivate : public QObjectPrivate
542 QQuickKeysAttachedPrivate()
543 : QObjectPrivate(), inPress(false), inRelease(false)
544 , inIM(false), enabled(true), imeItem(0), item(0)
547 bool isConnected(const char *signalName);
557 QList<QQuickItem *> targets;
561 class QQuickKeysAttached : public QObject, public QQuickItemKeyFilter
564 Q_DECLARE_PRIVATE(QQuickKeysAttached)
566 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
567 Q_PROPERTY(QDeclarativeListProperty<QQuickItem> forwardTo READ forwardTo)
568 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
573 QQuickKeysAttached(QObject *parent=0);
574 ~QQuickKeysAttached();
576 bool enabled() const { Q_D(const QQuickKeysAttached); return d->enabled; }
577 void setEnabled(bool enabled) {
578 Q_D(QQuickKeysAttached);
579 if (enabled != d->enabled) {
580 d->enabled = enabled;
581 emit enabledChanged();
585 enum Priority { BeforeItem, AfterItem};
586 Priority priority() const;
587 void setPriority(Priority);
589 QDeclarativeListProperty<QQuickItem> forwardTo() {
590 Q_D(QQuickKeysAttached);
591 return QDeclarativeListProperty<QQuickItem>(this, d->targets);
594 virtual void componentComplete();
596 static QQuickKeysAttached *qmlAttachedProperties(QObject *);
599 void enabledChanged();
600 void priorityChanged();
601 void pressed(QQuickKeyEvent *event);
602 void released(QQuickKeyEvent *event);
603 void digit0Pressed(QQuickKeyEvent *event);
604 void digit1Pressed(QQuickKeyEvent *event);
605 void digit2Pressed(QQuickKeyEvent *event);
606 void digit3Pressed(QQuickKeyEvent *event);
607 void digit4Pressed(QQuickKeyEvent *event);
608 void digit5Pressed(QQuickKeyEvent *event);
609 void digit6Pressed(QQuickKeyEvent *event);
610 void digit7Pressed(QQuickKeyEvent *event);
611 void digit8Pressed(QQuickKeyEvent *event);
612 void digit9Pressed(QQuickKeyEvent *event);
614 void leftPressed(QQuickKeyEvent *event);
615 void rightPressed(QQuickKeyEvent *event);
616 void upPressed(QQuickKeyEvent *event);
617 void downPressed(QQuickKeyEvent *event);
618 void tabPressed(QQuickKeyEvent *event);
619 void backtabPressed(QQuickKeyEvent *event);
621 void asteriskPressed(QQuickKeyEvent *event);
622 void numberSignPressed(QQuickKeyEvent *event);
623 void escapePressed(QQuickKeyEvent *event);
624 void returnPressed(QQuickKeyEvent *event);
625 void enterPressed(QQuickKeyEvent *event);
626 void deletePressed(QQuickKeyEvent *event);
627 void spacePressed(QQuickKeyEvent *event);
628 void backPressed(QQuickKeyEvent *event);
629 void cancelPressed(QQuickKeyEvent *event);
630 void selectPressed(QQuickKeyEvent *event);
631 void yesPressed(QQuickKeyEvent *event);
632 void noPressed(QQuickKeyEvent *event);
633 void context1Pressed(QQuickKeyEvent *event);
634 void context2Pressed(QQuickKeyEvent *event);
635 void context3Pressed(QQuickKeyEvent *event);
636 void context4Pressed(QQuickKeyEvent *event);
637 void callPressed(QQuickKeyEvent *event);
638 void hangupPressed(QQuickKeyEvent *event);
639 void flipPressed(QQuickKeyEvent *event);
640 void menuPressed(QQuickKeyEvent *event);
641 void volumeUpPressed(QQuickKeyEvent *event);
642 void volumeDownPressed(QQuickKeyEvent *event);
645 virtual void keyPressed(QKeyEvent *event, bool post);
646 virtual void keyReleased(QKeyEvent *event, bool post);
647 virtual void inputMethodEvent(QInputMethodEvent *, bool post);
648 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
650 const QByteArray keyToSignal(int key) {
651 QByteArray keySignal;
652 if (key >= Qt::Key_0 && key <= Qt::Key_9) {
653 keySignal = "digit0Pressed";
654 keySignal[5] = '0' + (key - Qt::Key_0);
657 while (sigMap[i].key && sigMap[i].key != key)
659 keySignal = sigMap[i].sig;
669 static const SigMap sigMap[];
672 QSGTransformNode *QQuickItemPrivate::itemNode()
674 if (!itemNodeInstance) {
675 itemNodeInstance = createTransformNode();
676 itemNodeInstance->setFlag(QSGNode::OwnedByParent, false);
677 #ifdef QML_RUNTIME_TESTING
679 itemNodeInstance->description = QString::fromLatin1("QQuickItem(%1)").arg(QString::fromLatin1(q->metaObject()->className()));
682 return itemNodeInstance;
685 QSGNode *QQuickItemPrivate::childContainerNode()
688 groupNode = new QSGNode();
690 rootNode->appendChildNode(groupNode);
692 clipNode->appendChildNode(groupNode);
693 else if (opacityNode)
694 opacityNode->appendChildNode(groupNode);
696 itemNode()->appendChildNode(groupNode);
697 groupNode->setFlag(QSGNode::ChildrenDoNotOverlap, childrenDoNotOverlap);
698 #ifdef QML_RUNTIME_TESTING
699 groupNode->description = QLatin1String("group");
705 Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickItemPrivate::ChangeTypes);
709 QML_DECLARE_TYPE(QQuickKeysAttached)
710 QML_DECLARE_TYPEINFO(QQuickKeysAttached, QML_HAS_ATTACHED_PROPERTIES)
711 QML_DECLARE_TYPE(QQuickKeyNavigationAttached)
712 QML_DECLARE_TYPEINFO(QQuickKeyNavigationAttached, QML_HAS_ATTACHED_PROPERTIES)
713 QML_DECLARE_TYPE(QQuickLayoutMirroringAttached)
714 QML_DECLARE_TYPEINFO(QQuickLayoutMirroringAttached, QML_HAS_ATTACHED_PROPERTIES)
716 #endif // QQUICKITEM_P_H