1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
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 "QtQuick1/qdeclarativeitem.h"
58 #include "QtQuick1/private/qdeclarativeanchors_p.h"
59 #include "QtQuick1/private/qdeclarativeanchors_p_p.h"
60 #include "QtQuick1/private/qdeclarativeitemchangelistener_p.h"
61 #include <QtDeclarative/private/qpodvector_p.h>
63 #include <QtQuick1/private/qdeclarativestate_p.h>
64 #include <QtDeclarative/private/qdeclarativenullablevalue_p_p.h>
65 #include <QtDeclarative/private/qdeclarativenotifier_p.h>
66 #include <QtDeclarative/private/qdeclarativeglobal_p.h>
68 #include <QtDeclarative/qdeclarative.h>
69 #include <QtDeclarative/qdeclarativecontext.h>
71 #include <QtCore/qlist.h>
72 #include <QtCore/qrect.h>
73 #include <QtCore/qdebug.h>
74 #include <QtGui/qaccessible.h>
76 #include <QtWidgets/private/qgraphicsitem_p.h>
82 class QDeclarativeItemKeyFilter;
83 class QDeclarative1LayoutMirroringAttached;
85 //### merge into private?
86 class QDeclarative1Contents : public QObject, public QDeclarativeItemChangeListener
90 QDeclarative1Contents(QDeclarativeItem *item);
91 ~QDeclarative1Contents();
95 void childRemoved(QDeclarativeItem *item);
96 void childAdded(QDeclarativeItem *item);
98 void calcGeometry() { calcWidth(); calcHeight(); }
102 void rectChanged(QRectF);
105 void itemGeometryChanged(QDeclarativeItem *item, const QRectF &newGeometry, const QRectF &oldGeometry);
106 void itemDestroyed(QDeclarativeItem *item);
107 //void itemVisibilityChanged(QDeclarativeItem *item)
110 void calcHeight(QDeclarativeItem *changed = 0);
111 void calcWidth(QDeclarativeItem *changed = 0);
113 QDeclarativeItem *m_item;
120 class Q_QTQUICK1_EXPORT QDeclarativeItemPrivate : public QGraphicsItemPrivate
122 Q_DECLARE_PUBLIC(QDeclarativeItem)
125 QDeclarativeItemPrivate()
126 : _anchors(0), _contents(0),
129 _stateGroup(0), origin(QDeclarativeItem::Center),
130 widthValid(false), heightValid(false),
131 componentComplete(true), keepMouse(false),
132 smooth(false), transformOriginDirty(true), doneEventPreHandler(false),
133 inheritedLayoutMirror(false), effectiveLayoutMirror(false), isMirrorImplicit(true),
134 inheritMirrorFromParent(false), inheritMirrorFromItem(false), isAccessible(0), keyHandler(0),
135 mWidth(0), mHeight(0), mImplicitWidth(0), mImplicitHeight(0), attachedLayoutDirection(0), hadSubFocusItem(false)
137 QGraphicsItemPrivate::acceptedMouseButtons = 0;
138 isDeclarativeItem = 1;
139 QGraphicsItemPrivate::flags = QGraphicsItem::GraphicsItemFlags(
140 QGraphicsItem::ItemHasNoContents
141 | QGraphicsItem::ItemIsFocusable
142 | QGraphicsItem::ItemNegativeZStacksBehindParent);
145 void init(QDeclarativeItem *parent)
147 Q_Q(QDeclarativeItem);
149 QDeclarative_setParent_noEvent(q, parent);
150 q->setParentItem(parent);
151 QDeclarativeItemPrivate *parentPrivate = QDeclarativeItemPrivate::get(parent);
152 setImplicitLayoutMirror(parentPrivate->inheritedLayoutMirror, parentPrivate->inheritMirrorFromParent);
154 baselineOffset.invalidate();
155 mouseSetsFocus = false;
158 bool isMirrored() const {
159 return effectiveLayoutMirror;
162 // Private Properties
164 void setWidth(qreal);
167 qreal height() const;
168 void setHeight(qreal);
171 virtual qreal implicitWidth() const;
172 virtual qreal implicitHeight() const;
173 virtual void implicitWidthChanged();
174 virtual void implicitHeightChanged();
176 void resolveLayoutMirror();
177 void setImplicitLayoutMirror(bool mirror, bool inherit);
178 void setLayoutMirror(bool mirror);
180 QDeclarativeListProperty<QObject> data();
181 QDeclarativeListProperty<QObject> resources();
183 QDeclarativeListProperty<QDeclarative1State> states();
184 QDeclarativeListProperty<QDeclarative1Transition> transitions();
186 QString state() const;
187 void setState(const QString &);
189 QDeclarative1AnchorLine left() const;
190 QDeclarative1AnchorLine right() const;
191 QDeclarative1AnchorLine horizontalCenter() const;
192 QDeclarative1AnchorLine top() const;
193 QDeclarative1AnchorLine bottom() const;
194 QDeclarative1AnchorLine verticalCenter() const;
195 QDeclarative1AnchorLine baseline() const;
198 static void data_append(QDeclarativeListProperty<QObject> *, QObject *);
199 static int data_count(QDeclarativeListProperty<QObject> *);
200 static QObject *data_at(QDeclarativeListProperty<QObject> *, int);
201 static void data_clear(QDeclarativeListProperty<QObject> *);
203 // resources property
204 static QObject *resources_at(QDeclarativeListProperty<QObject> *, int);
205 static void resources_append(QDeclarativeListProperty<QObject> *, QObject *);
206 static int resources_count(QDeclarativeListProperty<QObject> *);
207 static void resources_clear(QDeclarativeListProperty<QObject> *);
209 // transform property
210 static int transform_count(QDeclarativeListProperty<QGraphicsTransform> *list);
211 static void transform_append(QDeclarativeListProperty<QGraphicsTransform> *list, QGraphicsTransform *);
212 static QGraphicsTransform *transform_at(QDeclarativeListProperty<QGraphicsTransform> *list, int);
213 static void transform_clear(QDeclarativeListProperty<QGraphicsTransform> *list);
215 static QDeclarativeItemPrivate* get(QDeclarativeItem *item)
217 return item->d_func();
220 QDeclarative1Anchors *anchors() {
222 Q_Q(QDeclarativeItem);
223 _anchors = new QDeclarative1Anchors(q);
224 if (!componentComplete)
225 _anchors->classBegin();
229 QDeclarative1Anchors *_anchors;
230 QDeclarative1Contents *_contents;
232 QDeclarativeNullableValue<qreal> baselineOffset;
235 AnchorLines(QGraphicsObject *);
236 QDeclarative1AnchorLine left;
237 QDeclarative1AnchorLine right;
238 QDeclarative1AnchorLine hCenter;
239 QDeclarative1AnchorLine top;
240 QDeclarative1AnchorLine bottom;
241 QDeclarative1AnchorLine vCenter;
242 QDeclarative1AnchorLine baseline;
244 mutable AnchorLines *_anchorLines;
245 AnchorLines *anchorLines() const {
246 Q_Q(const QDeclarativeItem);
247 if (!_anchorLines) _anchorLines =
248 new AnchorLines(const_cast<QDeclarativeItem *>(q));
260 Q_DECLARE_FLAGS(ChangeTypes, ChangeType)
262 struct ChangeListener {
263 ChangeListener(QDeclarativeItemChangeListener *l, QDeclarativeItemPrivate::ChangeTypes t) : listener(l), types(t) {}
264 QDeclarativeItemChangeListener *listener;
265 QDeclarativeItemPrivate::ChangeTypes types;
266 bool operator==(const ChangeListener &other) const { return listener == other.listener && types == other.types; }
269 void addItemChangeListener(QDeclarativeItemChangeListener *listener, ChangeTypes types) {
270 changeListeners.append(ChangeListener(listener, types));
272 void removeItemChangeListener(QDeclarativeItemChangeListener *, ChangeTypes types);
273 QPODVector<ChangeListener,4> changeListeners;
275 QDeclarative1StateGroup *_states();
276 QDeclarative1StateGroup *_stateGroup;
278 QDeclarativeItem::TransformOrigin origin:5;
281 bool componentComplete:1;
284 bool transformOriginDirty : 1;
285 bool doneEventPreHandler : 1;
286 bool inheritedLayoutMirror:1;
287 bool effectiveLayoutMirror:1;
288 bool isMirrorImplicit:1;
289 bool inheritMirrorFromParent:1;
290 bool inheritMirrorFromItem:1;
293 QDeclarativeItemKeyFilter *keyHandler;
297 qreal mImplicitWidth;
298 qreal mImplicitHeight;
300 QDeclarative1LayoutMirroringAttached* attachedLayoutDirection;
302 bool hadSubFocusItem;
303 void setAccessibleFlagAndListener();
305 QPointF computeTransformOrigin() const;
307 virtual void setPosHelper(const QPointF &pos)
309 Q_Q(QDeclarativeItem);
310 QRectF oldGeometry(this->pos.x(), this->pos.y(), mWidth, mHeight);
311 QGraphicsItemPrivate::setPosHelper(pos);
312 q->geometryChanged(QRectF(this->pos.x(), this->pos.y(), mWidth, mHeight), oldGeometry);
315 // Reimplemented from QGraphicsItemPrivate
316 virtual void subFocusItemChange()
318 bool hasSubFocusItem = subFocusItem != 0;
319 if (((flags & QGraphicsItem::ItemIsFocusScope) || !parent) && hasSubFocusItem != hadSubFocusItem)
320 emit q_func()->activeFocusChanged(hasSubFocusItem);
321 //see also QDeclarativeItemPrivate::focusChanged
322 hadSubFocusItem = hasSubFocusItem;
325 // Reimplemented from QGraphicsItemPrivate
326 virtual void focusScopeItemChange(bool isSubFocusItem)
328 emit q_func()->focusChanged(isSubFocusItem);
332 // Reimplemented from QGraphicsItemPrivate
333 virtual void siblingOrderChange()
335 Q_Q(QDeclarativeItem);
336 for(int ii = 0; ii < changeListeners.count(); ++ii) {
337 const QDeclarativeItemPrivate::ChangeListener &change = changeListeners.at(ii);
338 if (change.types & QDeclarativeItemPrivate::SiblingOrder) {
339 change.listener->itemSiblingOrderChanged(q);
344 // Reimplemented from QGraphicsItemPrivate
345 virtual void transformChanged();
347 virtual void focusChanged(bool);
349 virtual void mirrorChange() {};
351 static qint64 consistentTime;
352 static void setConsistentTime(qint64 t);
353 static void start(QElapsedTimer &);
354 static qint64 elapsed(QElapsedTimer &);
355 static qint64 restart(QElapsedTimer &);
359 Key filters can be installed on a QDeclarativeItem, but not removed. Currently they
360 are only used by attached objects (which are only destroyed on Item
361 destruction), so this isn't a problem. If in future this becomes any form
362 of public API, they will have to support removal too.
364 class QDeclarativeItemKeyFilter
367 QDeclarativeItemKeyFilter(QDeclarativeItem * = 0);
368 virtual ~QDeclarativeItemKeyFilter();
370 virtual void keyPressed(QKeyEvent *event, bool post);
371 virtual void keyReleased(QKeyEvent *event, bool post);
372 virtual void inputMethodEvent(QInputMethodEvent *event, bool post);
373 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
374 virtual void componentComplete();
379 QDeclarativeItemKeyFilter *m_next;
382 class QDeclarative1KeyNavigationAttachedPrivate : public QObjectPrivate
385 QDeclarative1KeyNavigationAttachedPrivate()
387 left(0), right(0), up(0), down(0), tab(0), backtab(0),
388 leftSet(false), rightSet(false), upSet(false), downSet(false),
389 tabSet(false), backtabSet(false) {}
391 QDeclarativeItem *left;
392 QDeclarativeItem *right;
393 QDeclarativeItem *up;
394 QDeclarativeItem *down;
395 QDeclarativeItem *tab;
396 QDeclarativeItem *backtab;
405 class QDeclarative1KeyNavigationAttached : public QObject, public QDeclarativeItemKeyFilter
408 Q_DECLARE_PRIVATE(QDeclarative1KeyNavigationAttached)
410 Q_PROPERTY(QDeclarativeItem *left READ left WRITE setLeft NOTIFY leftChanged)
411 Q_PROPERTY(QDeclarativeItem *right READ right WRITE setRight NOTIFY rightChanged)
412 Q_PROPERTY(QDeclarativeItem *up READ up WRITE setUp NOTIFY upChanged)
413 Q_PROPERTY(QDeclarativeItem *down READ down WRITE setDown NOTIFY downChanged)
414 Q_PROPERTY(QDeclarativeItem *tab READ tab WRITE setTab NOTIFY tabChanged)
415 Q_PROPERTY(QDeclarativeItem *backtab READ backtab WRITE setBacktab NOTIFY backtabChanged)
416 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
421 QDeclarative1KeyNavigationAttached(QObject * = 0);
423 QDeclarativeItem *left() const;
424 void setLeft(QDeclarativeItem *);
425 QDeclarativeItem *right() const;
426 void setRight(QDeclarativeItem *);
427 QDeclarativeItem *up() const;
428 void setUp(QDeclarativeItem *);
429 QDeclarativeItem *down() const;
430 void setDown(QDeclarativeItem *);
431 QDeclarativeItem *tab() const;
432 void setTab(QDeclarativeItem *);
433 QDeclarativeItem *backtab() const;
434 void setBacktab(QDeclarativeItem *);
436 enum Priority { BeforeItem, AfterItem };
437 Priority priority() const;
438 void setPriority(Priority);
440 static QDeclarative1KeyNavigationAttached *qmlAttachedProperties(QObject *);
448 void backtabChanged();
449 void priorityChanged();
452 virtual void keyPressed(QKeyEvent *event, bool post);
453 virtual void keyReleased(QKeyEvent *event, bool post);
454 void setFocusNavigation(QDeclarativeItem *currentItem, const char *dir);
457 class QDeclarative1LayoutMirroringAttached : public QObject
460 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled RESET resetEnabled NOTIFY enabledChanged)
461 Q_PROPERTY(bool childrenInherit READ childrenInherit WRITE setChildrenInherit NOTIFY childrenInheritChanged)
464 explicit QDeclarative1LayoutMirroringAttached(QObject *parent = 0);
466 bool enabled() const;
467 void setEnabled(bool);
470 bool childrenInherit() const;
471 void setChildrenInherit(bool);
473 static QDeclarative1LayoutMirroringAttached *qmlAttachedProperties(QObject *);
475 void enabledChanged();
476 void childrenInheritChanged();
478 friend class QDeclarativeItemPrivate;
479 QDeclarativeItemPrivate *itemPrivate;
482 class QDeclarative1KeysAttachedPrivate : public QObjectPrivate
485 QDeclarative1KeysAttachedPrivate()
486 : QObjectPrivate(), inPress(false), inRelease(false)
487 , inIM(false), enabled(true), imeItem(0), item(0)
490 bool isConnected(const char *signalName);
492 QGraphicsItem *finalFocusProxy(QGraphicsItem *item) const
495 while ((fp = item->focusProxy()))
507 QGraphicsItem *imeItem;
508 QList<QDeclarativeItem *> targets;
509 QDeclarativeItem *item;
512 class QDeclarative1KeysAttached : public QObject, public QDeclarativeItemKeyFilter
515 Q_DECLARE_PRIVATE(QDeclarative1KeysAttached)
517 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
518 Q_PROPERTY(QDeclarativeListProperty<QDeclarativeItem> forwardTo READ forwardTo)
519 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
524 QDeclarative1KeysAttached(QObject *parent=0);
525 ~QDeclarative1KeysAttached();
527 bool enabled() const { Q_D(const QDeclarative1KeysAttached); return d->enabled; }
528 void setEnabled(bool enabled) {
529 Q_D(QDeclarative1KeysAttached);
530 if (enabled != d->enabled) {
531 d->enabled = enabled;
532 emit enabledChanged();
536 enum Priority { BeforeItem, AfterItem};
537 Priority priority() const;
538 void setPriority(Priority);
540 QDeclarativeListProperty<QDeclarativeItem> forwardTo() {
541 Q_D(QDeclarative1KeysAttached);
542 return QDeclarativeListProperty<QDeclarativeItem>(this, d->targets);
545 virtual void componentComplete();
547 static QDeclarative1KeysAttached *qmlAttachedProperties(QObject *);
550 void enabledChanged();
551 void priorityChanged();
552 void pressed(QDeclarative1KeyEvent *event);
553 void released(QDeclarative1KeyEvent *event);
554 void digit0Pressed(QDeclarative1KeyEvent *event);
555 void digit1Pressed(QDeclarative1KeyEvent *event);
556 void digit2Pressed(QDeclarative1KeyEvent *event);
557 void digit3Pressed(QDeclarative1KeyEvent *event);
558 void digit4Pressed(QDeclarative1KeyEvent *event);
559 void digit5Pressed(QDeclarative1KeyEvent *event);
560 void digit6Pressed(QDeclarative1KeyEvent *event);
561 void digit7Pressed(QDeclarative1KeyEvent *event);
562 void digit8Pressed(QDeclarative1KeyEvent *event);
563 void digit9Pressed(QDeclarative1KeyEvent *event);
565 void leftPressed(QDeclarative1KeyEvent *event);
566 void rightPressed(QDeclarative1KeyEvent *event);
567 void upPressed(QDeclarative1KeyEvent *event);
568 void downPressed(QDeclarative1KeyEvent *event);
569 void tabPressed(QDeclarative1KeyEvent *event);
570 void backtabPressed(QDeclarative1KeyEvent *event);
572 void asteriskPressed(QDeclarative1KeyEvent *event);
573 void numberSignPressed(QDeclarative1KeyEvent *event);
574 void escapePressed(QDeclarative1KeyEvent *event);
575 void returnPressed(QDeclarative1KeyEvent *event);
576 void enterPressed(QDeclarative1KeyEvent *event);
577 void deletePressed(QDeclarative1KeyEvent *event);
578 void spacePressed(QDeclarative1KeyEvent *event);
579 void backPressed(QDeclarative1KeyEvent *event);
580 void cancelPressed(QDeclarative1KeyEvent *event);
581 void selectPressed(QDeclarative1KeyEvent *event);
582 void yesPressed(QDeclarative1KeyEvent *event);
583 void noPressed(QDeclarative1KeyEvent *event);
584 void context1Pressed(QDeclarative1KeyEvent *event);
585 void context2Pressed(QDeclarative1KeyEvent *event);
586 void context3Pressed(QDeclarative1KeyEvent *event);
587 void context4Pressed(QDeclarative1KeyEvent *event);
588 void callPressed(QDeclarative1KeyEvent *event);
589 void hangupPressed(QDeclarative1KeyEvent *event);
590 void flipPressed(QDeclarative1KeyEvent *event);
591 void menuPressed(QDeclarative1KeyEvent *event);
592 void volumeUpPressed(QDeclarative1KeyEvent *event);
593 void volumeDownPressed(QDeclarative1KeyEvent *event);
596 virtual void keyPressed(QKeyEvent *event, bool post);
597 virtual void keyReleased(QKeyEvent *event, bool post);
598 virtual void inputMethodEvent(QInputMethodEvent *, bool post);
599 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
601 const QByteArray keyToSignal(int key) {
602 QByteArray keySignal;
603 if (key >= Qt::Key_0 && key <= Qt::Key_9) {
604 keySignal = "digit0Pressed";
605 keySignal[5] = '0' + (key - Qt::Key_0);
608 while (sigMap[i].key && sigMap[i].key != key)
610 keySignal = sigMap[i].sig;
620 static const SigMap sigMap[];
624 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeItemPrivate::ChangeTypes);
628 QML_DECLARE_TYPE(QDeclarative1KeysAttached)
629 QML_DECLARE_TYPEINFO(QDeclarative1KeysAttached, QML_HAS_ATTACHED_PROPERTIES)
630 QML_DECLARE_TYPE(QDeclarative1KeyNavigationAttached)
631 QML_DECLARE_TYPEINFO(QDeclarative1KeyNavigationAttached, QML_HAS_ATTACHED_PROPERTIES)
632 QML_DECLARE_TYPE(QDeclarative1LayoutMirroringAttached)
633 QML_DECLARE_TYPEINFO(QDeclarative1LayoutMirroringAttached, QML_HAS_ATTACHED_PROPERTIES)
635 #endif // QDECLARATIVEITEM_P_H