1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #ifndef QDECLARATIVEITEM_P_H
43 #define QDECLARATIVEITEM_P_H
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.
56 #include "qdeclarativeitem.h"
58 #include "private/qdeclarativeanchors_p.h"
59 #include "private/qdeclarativeanchors_p_p.h"
60 #include "private/qdeclarativeitemchangelistener_p.h"
61 #include <private/qpodvector_p.h>
63 #include <private/qdeclarativestate_p.h>
64 #include <private/qdeclarativenullablevalue_p_p.h>
65 #include <private/qdeclarativenotifier_p.h>
66 #include <private/qdeclarativeglobal_p.h>
68 #include <qdeclarative.h>
69 #include <qdeclarativecontext.h>
71 #include <QtCore/qlist.h>
72 #include <QtCore/qdebug.h>
74 #include <private/qgraphicsitem_p.h>
79 class QDeclarativeItemKeyFilter;
80 class QDeclarativeLayoutMirroringAttached;
82 //### merge into private?
83 class QDeclarativeContents : public QObject, public QDeclarativeItemChangeListener
87 QDeclarativeContents(QDeclarativeItem *item);
88 ~QDeclarativeContents();
92 void childRemoved(QDeclarativeItem *item);
93 void childAdded(QDeclarativeItem *item);
95 void calcGeometry() { calcWidth(); calcHeight(); }
99 void rectChanged(QRectF);
102 void itemGeometryChanged(QDeclarativeItem *item, const QRectF &newGeometry, const QRectF &oldGeometry);
103 void itemDestroyed(QDeclarativeItem *item);
104 //void itemVisibilityChanged(QDeclarativeItem *item)
107 void calcHeight(QDeclarativeItem *changed = 0);
108 void calcWidth(QDeclarativeItem *changed = 0);
110 QDeclarativeItem *m_item;
117 class Q_DECLARATIVE_EXPORT QDeclarativeItemPrivate : public QGraphicsItemPrivate
119 Q_DECLARE_PUBLIC(QDeclarativeItem)
122 QDeclarativeItemPrivate()
123 : _anchors(0), _contents(0),
126 _stateGroup(0), origin(QDeclarativeItem::Center),
127 widthValid(false), heightValid(false),
128 componentComplete(true), keepMouse(false),
129 smooth(false), transformOriginDirty(true), doneEventPreHandler(false),
130 inheritedLayoutMirror(false), effectiveLayoutMirror(false), isMirrorImplicit(true),
131 inheritMirrorFromParent(false), inheritMirrorFromItem(false), keyHandler(0),
132 mWidth(0), mHeight(0), mImplicitWidth(0), mImplicitHeight(0), attachedLayoutDirection(0), hadSubFocusItem(false)
134 QGraphicsItemPrivate::acceptedMouseButtons = 0;
135 isDeclarativeItem = 1;
136 QGraphicsItemPrivate::flags = QGraphicsItem::GraphicsItemFlags(
137 QGraphicsItem::ItemHasNoContents
138 | QGraphicsItem::ItemIsFocusable
139 | QGraphicsItem::ItemNegativeZStacksBehindParent);
142 void init(QDeclarativeItem *parent)
144 Q_Q(QDeclarativeItem);
146 QDeclarative_setParent_noEvent(q, parent);
147 q->setParentItem(parent);
148 QDeclarativeItemPrivate *parentPrivate = QDeclarativeItemPrivate::get(parent);
149 setImplicitLayoutMirror(parentPrivate->inheritedLayoutMirror, parentPrivate->inheritMirrorFromParent);
151 baselineOffset.invalidate();
152 mouseSetsFocus = false;
155 bool isMirrored() const {
156 return effectiveLayoutMirror;
159 // Private Properties
161 void setWidth(qreal);
164 qreal height() const;
165 void setHeight(qreal);
168 virtual qreal implicitWidth() const;
169 virtual qreal implicitHeight() const;
170 virtual void implicitWidthChanged();
171 virtual void implicitHeightChanged();
173 void resolveLayoutMirror();
174 void setImplicitLayoutMirror(bool mirror, bool inherit);
175 void setLayoutMirror(bool mirror);
177 QDeclarativeListProperty<QObject> data();
178 QDeclarativeListProperty<QObject> resources();
180 QDeclarativeListProperty<QDeclarativeState> states();
181 QDeclarativeListProperty<QDeclarativeTransition> transitions();
183 QString state() const;
184 void setState(const QString &);
186 QDeclarativeAnchorLine left() const;
187 QDeclarativeAnchorLine right() const;
188 QDeclarativeAnchorLine horizontalCenter() const;
189 QDeclarativeAnchorLine top() const;
190 QDeclarativeAnchorLine bottom() const;
191 QDeclarativeAnchorLine verticalCenter() const;
192 QDeclarativeAnchorLine baseline() const;
195 static void data_append(QDeclarativeListProperty<QObject> *, QObject *);
196 static int data_count(QDeclarativeListProperty<QObject> *);
197 static QObject *data_at(QDeclarativeListProperty<QObject> *, int);
198 static void data_clear(QDeclarativeListProperty<QObject> *);
200 // resources property
201 static QObject *resources_at(QDeclarativeListProperty<QObject> *, int);
202 static void resources_append(QDeclarativeListProperty<QObject> *, QObject *);
203 static int resources_count(QDeclarativeListProperty<QObject> *);
204 static void resources_clear(QDeclarativeListProperty<QObject> *);
206 // transform property
207 static int transform_count(QDeclarativeListProperty<QGraphicsTransform> *list);
208 static void transform_append(QDeclarativeListProperty<QGraphicsTransform> *list, QGraphicsTransform *);
209 static QGraphicsTransform *transform_at(QDeclarativeListProperty<QGraphicsTransform> *list, int);
210 static void transform_clear(QDeclarativeListProperty<QGraphicsTransform> *list);
212 static QDeclarativeItemPrivate* get(QDeclarativeItem *item)
214 return item->d_func();
217 // Accelerated property accessors
218 QDeclarativeNotifier parentNotifier;
219 static void parentProperty(QObject *o, void *rv, QDeclarativeNotifierEndpoint *e);
221 QDeclarativeAnchors *anchors() {
223 Q_Q(QDeclarativeItem);
224 _anchors = new QDeclarativeAnchors(q);
225 if (!componentComplete)
226 _anchors->classBegin();
230 QDeclarativeAnchors *_anchors;
231 QDeclarativeContents *_contents;
233 QDeclarativeNullableValue<qreal> baselineOffset;
236 AnchorLines(QGraphicsObject *);
237 QDeclarativeAnchorLine left;
238 QDeclarativeAnchorLine right;
239 QDeclarativeAnchorLine hCenter;
240 QDeclarativeAnchorLine top;
241 QDeclarativeAnchorLine bottom;
242 QDeclarativeAnchorLine vCenter;
243 QDeclarativeAnchorLine baseline;
245 mutable AnchorLines *_anchorLines;
246 AnchorLines *anchorLines() const {
247 Q_Q(const QDeclarativeItem);
248 if (!_anchorLines) _anchorLines =
249 new AnchorLines(const_cast<QDeclarativeItem *>(q));
261 Q_DECLARE_FLAGS(ChangeTypes, ChangeType)
263 struct ChangeListener {
264 ChangeListener(QDeclarativeItemChangeListener *l, QDeclarativeItemPrivate::ChangeTypes t) : listener(l), types(t) {}
265 QDeclarativeItemChangeListener *listener;
266 QDeclarativeItemPrivate::ChangeTypes types;
267 bool operator==(const ChangeListener &other) const { return listener == other.listener && types == other.types; }
270 void addItemChangeListener(QDeclarativeItemChangeListener *listener, ChangeTypes types) {
271 changeListeners.append(ChangeListener(listener, types));
273 void removeItemChangeListener(QDeclarativeItemChangeListener *, ChangeTypes types);
274 QPODVector<ChangeListener,4> changeListeners;
276 QDeclarativeStateGroup *_states();
277 QDeclarativeStateGroup *_stateGroup;
279 QDeclarativeItem::TransformOrigin origin:5;
282 bool componentComplete:1;
285 bool transformOriginDirty : 1;
286 bool doneEventPreHandler : 1;
287 bool inheritedLayoutMirror:1;
288 bool effectiveLayoutMirror:1;
289 bool isMirrorImplicit:1;
290 bool inheritMirrorFromParent:1;
291 bool inheritMirrorFromItem:1;
293 QDeclarativeItemKeyFilter *keyHandler;
297 qreal mImplicitWidth;
298 qreal mImplicitHeight;
300 QDeclarativeLayoutMirroringAttached* attachedLayoutDirection;
302 bool hadSubFocusItem;
304 QPointF computeTransformOrigin() const;
306 virtual void setPosHelper(const QPointF &pos)
308 Q_Q(QDeclarativeItem);
309 QRectF oldGeometry(this->pos.x(), this->pos.y(), mWidth, mHeight);
310 QGraphicsItemPrivate::setPosHelper(pos);
311 q->geometryChanged(QRectF(this->pos.x(), this->pos.y(), mWidth, mHeight), oldGeometry);
314 // Reimplemented from QGraphicsItemPrivate
315 virtual void subFocusItemChange()
317 bool hasSubFocusItem = subFocusItem != 0;
318 if (((flags & QGraphicsItem::ItemIsFocusScope) || !parent) && hasSubFocusItem != hadSubFocusItem)
319 emit q_func()->activeFocusChanged(hasSubFocusItem);
320 //see also QDeclarativeItemPrivate::focusChanged
321 hadSubFocusItem = hasSubFocusItem;
324 // Reimplemented from QGraphicsItemPrivate
325 virtual void focusScopeItemChange(bool isSubFocusItem)
327 emit q_func()->focusChanged(isSubFocusItem);
331 // Reimplemented from QGraphicsItemPrivate
332 virtual void siblingOrderChange()
334 Q_Q(QDeclarativeItem);
335 for(int ii = 0; ii < changeListeners.count(); ++ii) {
336 const QDeclarativeItemPrivate::ChangeListener &change = changeListeners.at(ii);
337 if (change.types & QDeclarativeItemPrivate::SiblingOrder) {
338 change.listener->itemSiblingOrderChanged(q);
343 // Reimplemented from QGraphicsItemPrivate
344 virtual void transformChanged();
346 virtual void focusChanged(bool);
348 virtual void mirrorChange() {};
350 static qint64 consistentTime;
351 static void setConsistentTime(qint64 t);
352 static void start(QElapsedTimer &);
353 static qint64 elapsed(QElapsedTimer &);
354 static qint64 restart(QElapsedTimer &);
358 Key filters can be installed on a QDeclarativeItem, but not removed. Currently they
359 are only used by attached objects (which are only destroyed on Item
360 destruction), so this isn't a problem. If in future this becomes any form
361 of public API, they will have to support removal too.
363 class QDeclarativeItemKeyFilter
366 QDeclarativeItemKeyFilter(QDeclarativeItem * = 0);
367 virtual ~QDeclarativeItemKeyFilter();
369 virtual void keyPressed(QKeyEvent *event, bool post);
370 virtual void keyReleased(QKeyEvent *event, bool post);
371 virtual void inputMethodEvent(QInputMethodEvent *event, bool post);
372 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
373 virtual void componentComplete();
378 QDeclarativeItemKeyFilter *m_next;
381 class QDeclarativeKeyNavigationAttachedPrivate : public QObjectPrivate
384 QDeclarativeKeyNavigationAttachedPrivate()
385 : QObjectPrivate(), left(0), right(0), up(0), down(0), tab(0), backtab(0) {}
387 QDeclarativeItem *left;
388 QDeclarativeItem *right;
389 QDeclarativeItem *up;
390 QDeclarativeItem *down;
391 QDeclarativeItem *tab;
392 QDeclarativeItem *backtab;
395 class QDeclarativeKeyNavigationAttached : public QObject, public QDeclarativeItemKeyFilter
398 Q_DECLARE_PRIVATE(QDeclarativeKeyNavigationAttached)
400 Q_PROPERTY(QDeclarativeItem *left READ left WRITE setLeft NOTIFY leftChanged)
401 Q_PROPERTY(QDeclarativeItem *right READ right WRITE setRight NOTIFY rightChanged)
402 Q_PROPERTY(QDeclarativeItem *up READ up WRITE setUp NOTIFY upChanged)
403 Q_PROPERTY(QDeclarativeItem *down READ down WRITE setDown NOTIFY downChanged)
404 Q_PROPERTY(QDeclarativeItem *tab READ tab WRITE setTab NOTIFY tabChanged)
405 Q_PROPERTY(QDeclarativeItem *backtab READ backtab WRITE setBacktab NOTIFY backtabChanged)
406 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
411 QDeclarativeKeyNavigationAttached(QObject * = 0);
413 QDeclarativeItem *left() const;
414 void setLeft(QDeclarativeItem *);
415 QDeclarativeItem *right() const;
416 void setRight(QDeclarativeItem *);
417 QDeclarativeItem *up() const;
418 void setUp(QDeclarativeItem *);
419 QDeclarativeItem *down() const;
420 void setDown(QDeclarativeItem *);
421 QDeclarativeItem *tab() const;
422 void setTab(QDeclarativeItem *);
423 QDeclarativeItem *backtab() const;
424 void setBacktab(QDeclarativeItem *);
426 enum Priority { BeforeItem, AfterItem };
427 Priority priority() const;
428 void setPriority(Priority);
430 static QDeclarativeKeyNavigationAttached *qmlAttachedProperties(QObject *);
438 void backtabChanged();
439 void priorityChanged();
442 virtual void keyPressed(QKeyEvent *event, bool post);
443 virtual void keyReleased(QKeyEvent *event, bool post);
444 void setFocusNavigation(QDeclarativeItem *currentItem, const char *dir);
447 class QDeclarativeLayoutMirroringAttached : public QObject
450 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled RESET resetEnabled NOTIFY enabledChanged)
451 Q_PROPERTY(bool childrenInherit READ childrenInherit WRITE setChildrenInherit NOTIFY childrenInheritChanged)
454 explicit QDeclarativeLayoutMirroringAttached(QObject *parent = 0);
456 bool enabled() const;
457 void setEnabled(bool);
460 bool childrenInherit() const;
461 void setChildrenInherit(bool);
463 static QDeclarativeLayoutMirroringAttached *qmlAttachedProperties(QObject *);
465 void enabledChanged();
466 void childrenInheritChanged();
468 friend class QDeclarativeItemPrivate;
469 QDeclarativeItemPrivate *itemPrivate;
472 class QDeclarativeKeysAttachedPrivate : public QObjectPrivate
475 QDeclarativeKeysAttachedPrivate()
476 : QObjectPrivate(), inPress(false), inRelease(false)
477 , inIM(false), enabled(true), imeItem(0), item(0)
480 bool isConnected(const char *signalName);
482 QGraphicsItem *finalFocusProxy(QGraphicsItem *item) const
485 while ((fp = item->focusProxy()))
497 QGraphicsItem *imeItem;
498 QList<QDeclarativeItem *> targets;
499 QDeclarativeItem *item;
502 class QDeclarativeKeysAttached : public QObject, public QDeclarativeItemKeyFilter
505 Q_DECLARE_PRIVATE(QDeclarativeKeysAttached)
507 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
508 Q_PROPERTY(QDeclarativeListProperty<QDeclarativeItem> forwardTo READ forwardTo)
509 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
514 QDeclarativeKeysAttached(QObject *parent=0);
515 ~QDeclarativeKeysAttached();
517 bool enabled() const { Q_D(const QDeclarativeKeysAttached); return d->enabled; }
518 void setEnabled(bool enabled) {
519 Q_D(QDeclarativeKeysAttached);
520 if (enabled != d->enabled) {
521 d->enabled = enabled;
522 emit enabledChanged();
526 enum Priority { BeforeItem, AfterItem};
527 Priority priority() const;
528 void setPriority(Priority);
530 QDeclarativeListProperty<QDeclarativeItem> forwardTo() {
531 Q_D(QDeclarativeKeysAttached);
532 return QDeclarativeListProperty<QDeclarativeItem>(this, d->targets);
535 virtual void componentComplete();
537 static QDeclarativeKeysAttached *qmlAttachedProperties(QObject *);
540 void enabledChanged();
541 void priorityChanged();
542 void pressed(QDeclarativeKeyEvent *event);
543 void released(QDeclarativeKeyEvent *event);
544 void digit0Pressed(QDeclarativeKeyEvent *event);
545 void digit1Pressed(QDeclarativeKeyEvent *event);
546 void digit2Pressed(QDeclarativeKeyEvent *event);
547 void digit3Pressed(QDeclarativeKeyEvent *event);
548 void digit4Pressed(QDeclarativeKeyEvent *event);
549 void digit5Pressed(QDeclarativeKeyEvent *event);
550 void digit6Pressed(QDeclarativeKeyEvent *event);
551 void digit7Pressed(QDeclarativeKeyEvent *event);
552 void digit8Pressed(QDeclarativeKeyEvent *event);
553 void digit9Pressed(QDeclarativeKeyEvent *event);
555 void leftPressed(QDeclarativeKeyEvent *event);
556 void rightPressed(QDeclarativeKeyEvent *event);
557 void upPressed(QDeclarativeKeyEvent *event);
558 void downPressed(QDeclarativeKeyEvent *event);
559 void tabPressed(QDeclarativeKeyEvent *event);
560 void backtabPressed(QDeclarativeKeyEvent *event);
562 void asteriskPressed(QDeclarativeKeyEvent *event);
563 void numberSignPressed(QDeclarativeKeyEvent *event);
564 void escapePressed(QDeclarativeKeyEvent *event);
565 void returnPressed(QDeclarativeKeyEvent *event);
566 void enterPressed(QDeclarativeKeyEvent *event);
567 void deletePressed(QDeclarativeKeyEvent *event);
568 void spacePressed(QDeclarativeKeyEvent *event);
569 void backPressed(QDeclarativeKeyEvent *event);
570 void cancelPressed(QDeclarativeKeyEvent *event);
571 void selectPressed(QDeclarativeKeyEvent *event);
572 void yesPressed(QDeclarativeKeyEvent *event);
573 void noPressed(QDeclarativeKeyEvent *event);
574 void context1Pressed(QDeclarativeKeyEvent *event);
575 void context2Pressed(QDeclarativeKeyEvent *event);
576 void context3Pressed(QDeclarativeKeyEvent *event);
577 void context4Pressed(QDeclarativeKeyEvent *event);
578 void callPressed(QDeclarativeKeyEvent *event);
579 void hangupPressed(QDeclarativeKeyEvent *event);
580 void flipPressed(QDeclarativeKeyEvent *event);
581 void menuPressed(QDeclarativeKeyEvent *event);
582 void volumeUpPressed(QDeclarativeKeyEvent *event);
583 void volumeDownPressed(QDeclarativeKeyEvent *event);
586 virtual void keyPressed(QKeyEvent *event, bool post);
587 virtual void keyReleased(QKeyEvent *event, bool post);
588 virtual void inputMethodEvent(QInputMethodEvent *, bool post);
589 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
591 const QByteArray keyToSignal(int key) {
592 QByteArray keySignal;
593 if (key >= Qt::Key_0 && key <= Qt::Key_9) {
594 keySignal = "digit0Pressed";
595 keySignal[5] = '0' + (key - Qt::Key_0);
598 while (sigMap[i].key && sigMap[i].key != key)
600 keySignal = sigMap[i].sig;
610 static const SigMap sigMap[];
613 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeItemPrivate::ChangeTypes);
617 QML_DECLARE_TYPE(QDeclarativeKeysAttached)
618 QML_DECLARE_TYPEINFO(QDeclarativeKeysAttached, QML_HAS_ATTACHED_PROPERTIES)
619 QML_DECLARE_TYPE(QDeclarativeKeyNavigationAttached)
620 QML_DECLARE_TYPEINFO(QDeclarativeKeyNavigationAttached, QML_HAS_ATTACHED_PROPERTIES)
621 QML_DECLARE_TYPE(QDeclarativeLayoutMirroringAttached)
622 QML_DECLARE_TYPEINFO(QDeclarativeLayoutMirroringAttached, QML_HAS_ATTACHED_PROPERTIES)
624 #endif // QDECLARATIVEITEM_P_H