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 ** 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.
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.
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.
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.
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>
75 #include <QtWidgets/private/qgraphicsitem_p.h>
81 class QDeclarativeItemKeyFilter;
82 class QDeclarative1LayoutMirroringAttached;
84 //### merge into private?
85 class QDeclarative1Contents : public QObject, public QDeclarativeItemChangeListener
89 QDeclarative1Contents(QDeclarativeItem *item);
90 ~QDeclarative1Contents();
94 void childRemoved(QDeclarativeItem *item);
95 void childAdded(QDeclarativeItem *item);
97 void calcGeometry() { calcWidth(); calcHeight(); }
101 void rectChanged(QRectF);
104 void itemGeometryChanged(QDeclarativeItem *item, const QRectF &newGeometry, const QRectF &oldGeometry);
105 void itemDestroyed(QDeclarativeItem *item);
106 //void itemVisibilityChanged(QDeclarativeItem *item)
109 void calcHeight(QDeclarativeItem *changed = 0);
110 void calcWidth(QDeclarativeItem *changed = 0);
112 QDeclarativeItem *m_item;
119 class Q_QTQUICK1_EXPORT QDeclarativeItemPrivate : public QGraphicsItemPrivate
121 Q_DECLARE_PUBLIC(QDeclarativeItem)
124 QDeclarativeItemPrivate()
125 : _anchors(0), _contents(0),
128 _stateGroup(0), origin(QDeclarativeItem::Center),
129 widthValid(false), heightValid(false),
130 componentComplete(true), keepMouse(false),
131 smooth(false), transformOriginDirty(true), doneEventPreHandler(false),
132 inheritedLayoutMirror(false), effectiveLayoutMirror(false), isMirrorImplicit(true),
133 inheritMirrorFromParent(false), inheritMirrorFromItem(false), keyHandler(0),
134 mWidth(0), mHeight(0), mImplicitWidth(0), mImplicitHeight(0), attachedLayoutDirection(0), hadSubFocusItem(false)
136 QGraphicsItemPrivate::acceptedMouseButtons = 0;
137 isDeclarativeItem = 1;
138 QGraphicsItemPrivate::flags = QGraphicsItem::GraphicsItemFlags(
139 QGraphicsItem::ItemHasNoContents
140 | QGraphicsItem::ItemIsFocusable
141 | QGraphicsItem::ItemNegativeZStacksBehindParent);
144 void init(QDeclarativeItem *parent)
146 Q_Q(QDeclarativeItem);
148 QDeclarative_setParent_noEvent(q, parent);
149 q->setParentItem(parent);
150 QDeclarativeItemPrivate *parentPrivate = QDeclarativeItemPrivate::get(parent);
151 setImplicitLayoutMirror(parentPrivate->inheritedLayoutMirror, parentPrivate->inheritMirrorFromParent);
153 baselineOffset.invalidate();
154 mouseSetsFocus = false;
157 bool isMirrored() const {
158 return effectiveLayoutMirror;
161 // Private Properties
163 void setWidth(qreal);
166 qreal height() const;
167 void setHeight(qreal);
170 virtual qreal implicitWidth() const;
171 virtual qreal implicitHeight() const;
172 virtual void implicitWidthChanged();
173 virtual void implicitHeightChanged();
175 void resolveLayoutMirror();
176 void setImplicitLayoutMirror(bool mirror, bool inherit);
177 void setLayoutMirror(bool mirror);
179 QDeclarativeListProperty<QObject> data();
180 QDeclarativeListProperty<QObject> resources();
182 QDeclarativeListProperty<QDeclarative1State> states();
183 QDeclarativeListProperty<QDeclarative1Transition> transitions();
185 QString state() const;
186 void setState(const QString &);
188 QDeclarative1AnchorLine left() const;
189 QDeclarative1AnchorLine right() const;
190 QDeclarative1AnchorLine horizontalCenter() const;
191 QDeclarative1AnchorLine top() const;
192 QDeclarative1AnchorLine bottom() const;
193 QDeclarative1AnchorLine verticalCenter() const;
194 QDeclarative1AnchorLine baseline() const;
197 static void data_append(QDeclarativeListProperty<QObject> *, QObject *);
198 static int data_count(QDeclarativeListProperty<QObject> *);
199 static QObject *data_at(QDeclarativeListProperty<QObject> *, int);
200 static void data_clear(QDeclarativeListProperty<QObject> *);
202 // resources property
203 static QObject *resources_at(QDeclarativeListProperty<QObject> *, int);
204 static void resources_append(QDeclarativeListProperty<QObject> *, QObject *);
205 static int resources_count(QDeclarativeListProperty<QObject> *);
206 static void resources_clear(QDeclarativeListProperty<QObject> *);
208 // transform property
209 static int transform_count(QDeclarativeListProperty<QGraphicsTransform> *list);
210 static void transform_append(QDeclarativeListProperty<QGraphicsTransform> *list, QGraphicsTransform *);
211 static QGraphicsTransform *transform_at(QDeclarativeListProperty<QGraphicsTransform> *list, int);
212 static void transform_clear(QDeclarativeListProperty<QGraphicsTransform> *list);
214 static QDeclarativeItemPrivate* get(QDeclarativeItem *item)
216 return item->d_func();
219 // Accelerated property accessors
220 QDeclarativeNotifier parentNotifier;
221 static void parentProperty(QObject *o, void *rv, QDeclarativeNotifierEndpoint *e);
223 QDeclarative1Anchors *anchors() {
225 Q_Q(QDeclarativeItem);
226 _anchors = new QDeclarative1Anchors(q);
227 if (!componentComplete)
228 _anchors->classBegin();
232 QDeclarative1Anchors *_anchors;
233 QDeclarative1Contents *_contents;
235 QDeclarativeNullableValue<qreal> baselineOffset;
238 AnchorLines(QGraphicsObject *);
239 QDeclarative1AnchorLine left;
240 QDeclarative1AnchorLine right;
241 QDeclarative1AnchorLine hCenter;
242 QDeclarative1AnchorLine top;
243 QDeclarative1AnchorLine bottom;
244 QDeclarative1AnchorLine vCenter;
245 QDeclarative1AnchorLine baseline;
247 mutable AnchorLines *_anchorLines;
248 AnchorLines *anchorLines() const {
249 Q_Q(const QDeclarativeItem);
250 if (!_anchorLines) _anchorLines =
251 new AnchorLines(const_cast<QDeclarativeItem *>(q));
263 Q_DECLARE_FLAGS(ChangeTypes, ChangeType)
265 struct ChangeListener {
266 ChangeListener(QDeclarativeItemChangeListener *l, QDeclarativeItemPrivate::ChangeTypes t) : listener(l), types(t) {}
267 QDeclarativeItemChangeListener *listener;
268 QDeclarativeItemPrivate::ChangeTypes types;
269 bool operator==(const ChangeListener &other) const { return listener == other.listener && types == other.types; }
272 void addItemChangeListener(QDeclarativeItemChangeListener *listener, ChangeTypes types) {
273 changeListeners.append(ChangeListener(listener, types));
275 void removeItemChangeListener(QDeclarativeItemChangeListener *, ChangeTypes types);
276 QPODVector<ChangeListener,4> changeListeners;
278 QDeclarative1StateGroup *_states();
279 QDeclarative1StateGroup *_stateGroup;
281 QDeclarativeItem::TransformOrigin origin:5;
284 bool componentComplete:1;
287 bool transformOriginDirty : 1;
288 bool doneEventPreHandler : 1;
289 bool inheritedLayoutMirror:1;
290 bool effectiveLayoutMirror:1;
291 bool isMirrorImplicit:1;
292 bool inheritMirrorFromParent:1;
293 bool inheritMirrorFromItem:1;
295 QDeclarativeItemKeyFilter *keyHandler;
299 qreal mImplicitWidth;
300 qreal mImplicitHeight;
302 QDeclarative1LayoutMirroringAttached* attachedLayoutDirection;
304 bool hadSubFocusItem;
306 QPointF computeTransformOrigin() const;
308 virtual void setPosHelper(const QPointF &pos)
310 Q_Q(QDeclarativeItem);
311 QRectF oldGeometry(this->pos.x(), this->pos.y(), mWidth, mHeight);
312 QGraphicsItemPrivate::setPosHelper(pos);
313 q->geometryChanged(QRectF(this->pos.x(), this->pos.y(), mWidth, mHeight), oldGeometry);
316 // Reimplemented from QGraphicsItemPrivate
317 virtual void subFocusItemChange()
319 bool hasSubFocusItem = subFocusItem != 0;
320 if (((flags & QGraphicsItem::ItemIsFocusScope) || !parent) && hasSubFocusItem != hadSubFocusItem)
321 emit q_func()->activeFocusChanged(hasSubFocusItem);
322 //see also QDeclarativeItemPrivate::focusChanged
323 hadSubFocusItem = hasSubFocusItem;
326 // Reimplemented from QGraphicsItemPrivate
327 virtual void focusScopeItemChange(bool isSubFocusItem)
329 emit q_func()->focusChanged(isSubFocusItem);
333 // Reimplemented from QGraphicsItemPrivate
334 virtual void siblingOrderChange()
336 Q_Q(QDeclarativeItem);
337 for(int ii = 0; ii < changeListeners.count(); ++ii) {
338 const QDeclarativeItemPrivate::ChangeListener &change = changeListeners.at(ii);
339 if (change.types & QDeclarativeItemPrivate::SiblingOrder) {
340 change.listener->itemSiblingOrderChanged(q);
345 // Reimplemented from QGraphicsItemPrivate
346 virtual void transformChanged();
348 virtual void focusChanged(bool);
350 virtual void mirrorChange() {};
352 static qint64 consistentTime;
353 static void setConsistentTime(qint64 t);
354 static void start(QElapsedTimer &);
355 static qint64 elapsed(QElapsedTimer &);
356 static qint64 restart(QElapsedTimer &);
360 Key filters can be installed on a QDeclarativeItem, but not removed. Currently they
361 are only used by attached objects (which are only destroyed on Item
362 destruction), so this isn't a problem. If in future this becomes any form
363 of public API, they will have to support removal too.
365 class QDeclarativeItemKeyFilter
368 QDeclarativeItemKeyFilter(QDeclarativeItem * = 0);
369 virtual ~QDeclarativeItemKeyFilter();
371 virtual void keyPressed(QKeyEvent *event, bool post);
372 virtual void keyReleased(QKeyEvent *event, bool post);
373 virtual void inputMethodEvent(QInputMethodEvent *event, bool post);
374 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
375 virtual void componentComplete();
380 QDeclarativeItemKeyFilter *m_next;
383 class QDeclarative1KeyNavigationAttachedPrivate : public QObjectPrivate
386 QDeclarative1KeyNavigationAttachedPrivate()
388 left(0), right(0), up(0), down(0), tab(0), backtab(0),
389 leftSet(false), rightSet(false), upSet(false), downSet(false),
390 tabSet(false), backtabSet(false) {}
392 QDeclarativeItem *left;
393 QDeclarativeItem *right;
394 QDeclarativeItem *up;
395 QDeclarativeItem *down;
396 QDeclarativeItem *tab;
397 QDeclarativeItem *backtab;
406 class QDeclarative1KeyNavigationAttached : public QObject, public QDeclarativeItemKeyFilter
409 Q_DECLARE_PRIVATE(QDeclarative1KeyNavigationAttached)
411 Q_PROPERTY(QDeclarativeItem *left READ left WRITE setLeft NOTIFY leftChanged)
412 Q_PROPERTY(QDeclarativeItem *right READ right WRITE setRight NOTIFY rightChanged)
413 Q_PROPERTY(QDeclarativeItem *up READ up WRITE setUp NOTIFY upChanged)
414 Q_PROPERTY(QDeclarativeItem *down READ down WRITE setDown NOTIFY downChanged)
415 Q_PROPERTY(QDeclarativeItem *tab READ tab WRITE setTab NOTIFY tabChanged)
416 Q_PROPERTY(QDeclarativeItem *backtab READ backtab WRITE setBacktab NOTIFY backtabChanged)
417 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
422 QDeclarative1KeyNavigationAttached(QObject * = 0);
424 QDeclarativeItem *left() const;
425 void setLeft(QDeclarativeItem *);
426 QDeclarativeItem *right() const;
427 void setRight(QDeclarativeItem *);
428 QDeclarativeItem *up() const;
429 void setUp(QDeclarativeItem *);
430 QDeclarativeItem *down() const;
431 void setDown(QDeclarativeItem *);
432 QDeclarativeItem *tab() const;
433 void setTab(QDeclarativeItem *);
434 QDeclarativeItem *backtab() const;
435 void setBacktab(QDeclarativeItem *);
437 enum Priority { BeforeItem, AfterItem };
438 Priority priority() const;
439 void setPriority(Priority);
441 static QDeclarative1KeyNavigationAttached *qmlAttachedProperties(QObject *);
449 void backtabChanged();
450 void priorityChanged();
453 virtual void keyPressed(QKeyEvent *event, bool post);
454 virtual void keyReleased(QKeyEvent *event, bool post);
455 void setFocusNavigation(QDeclarativeItem *currentItem, const char *dir);
458 class QDeclarative1LayoutMirroringAttached : public QObject
461 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled RESET resetEnabled NOTIFY enabledChanged)
462 Q_PROPERTY(bool childrenInherit READ childrenInherit WRITE setChildrenInherit NOTIFY childrenInheritChanged)
465 explicit QDeclarative1LayoutMirroringAttached(QObject *parent = 0);
467 bool enabled() const;
468 void setEnabled(bool);
471 bool childrenInherit() const;
472 void setChildrenInherit(bool);
474 static QDeclarative1LayoutMirroringAttached *qmlAttachedProperties(QObject *);
476 void enabledChanged();
477 void childrenInheritChanged();
479 friend class QDeclarativeItemPrivate;
480 QDeclarativeItemPrivate *itemPrivate;
483 class QDeclarative1KeysAttachedPrivate : public QObjectPrivate
486 QDeclarative1KeysAttachedPrivate()
487 : QObjectPrivate(), inPress(false), inRelease(false)
488 , inIM(false), enabled(true), imeItem(0), item(0)
491 bool isConnected(const char *signalName);
493 QGraphicsItem *finalFocusProxy(QGraphicsItem *item) const
496 while ((fp = item->focusProxy()))
508 QGraphicsItem *imeItem;
509 QList<QDeclarativeItem *> targets;
510 QDeclarativeItem *item;
513 class QDeclarative1KeysAttached : public QObject, public QDeclarativeItemKeyFilter
516 Q_DECLARE_PRIVATE(QDeclarative1KeysAttached)
518 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
519 Q_PROPERTY(QDeclarativeListProperty<QDeclarativeItem> forwardTo READ forwardTo)
520 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
525 QDeclarative1KeysAttached(QObject *parent=0);
526 ~QDeclarative1KeysAttached();
528 bool enabled() const { Q_D(const QDeclarative1KeysAttached); return d->enabled; }
529 void setEnabled(bool enabled) {
530 Q_D(QDeclarative1KeysAttached);
531 if (enabled != d->enabled) {
532 d->enabled = enabled;
533 emit enabledChanged();
537 enum Priority { BeforeItem, AfterItem};
538 Priority priority() const;
539 void setPriority(Priority);
541 QDeclarativeListProperty<QDeclarativeItem> forwardTo() {
542 Q_D(QDeclarative1KeysAttached);
543 return QDeclarativeListProperty<QDeclarativeItem>(this, d->targets);
546 virtual void componentComplete();
548 static QDeclarative1KeysAttached *qmlAttachedProperties(QObject *);
551 void enabledChanged();
552 void priorityChanged();
553 void pressed(QDeclarative1KeyEvent *event);
554 void released(QDeclarative1KeyEvent *event);
555 void digit0Pressed(QDeclarative1KeyEvent *event);
556 void digit1Pressed(QDeclarative1KeyEvent *event);
557 void digit2Pressed(QDeclarative1KeyEvent *event);
558 void digit3Pressed(QDeclarative1KeyEvent *event);
559 void digit4Pressed(QDeclarative1KeyEvent *event);
560 void digit5Pressed(QDeclarative1KeyEvent *event);
561 void digit6Pressed(QDeclarative1KeyEvent *event);
562 void digit7Pressed(QDeclarative1KeyEvent *event);
563 void digit8Pressed(QDeclarative1KeyEvent *event);
564 void digit9Pressed(QDeclarative1KeyEvent *event);
566 void leftPressed(QDeclarative1KeyEvent *event);
567 void rightPressed(QDeclarative1KeyEvent *event);
568 void upPressed(QDeclarative1KeyEvent *event);
569 void downPressed(QDeclarative1KeyEvent *event);
570 void tabPressed(QDeclarative1KeyEvent *event);
571 void backtabPressed(QDeclarative1KeyEvent *event);
573 void asteriskPressed(QDeclarative1KeyEvent *event);
574 void numberSignPressed(QDeclarative1KeyEvent *event);
575 void escapePressed(QDeclarative1KeyEvent *event);
576 void returnPressed(QDeclarative1KeyEvent *event);
577 void enterPressed(QDeclarative1KeyEvent *event);
578 void deletePressed(QDeclarative1KeyEvent *event);
579 void spacePressed(QDeclarative1KeyEvent *event);
580 void backPressed(QDeclarative1KeyEvent *event);
581 void cancelPressed(QDeclarative1KeyEvent *event);
582 void selectPressed(QDeclarative1KeyEvent *event);
583 void yesPressed(QDeclarative1KeyEvent *event);
584 void noPressed(QDeclarative1KeyEvent *event);
585 void context1Pressed(QDeclarative1KeyEvent *event);
586 void context2Pressed(QDeclarative1KeyEvent *event);
587 void context3Pressed(QDeclarative1KeyEvent *event);
588 void context4Pressed(QDeclarative1KeyEvent *event);
589 void callPressed(QDeclarative1KeyEvent *event);
590 void hangupPressed(QDeclarative1KeyEvent *event);
591 void flipPressed(QDeclarative1KeyEvent *event);
592 void menuPressed(QDeclarative1KeyEvent *event);
593 void volumeUpPressed(QDeclarative1KeyEvent *event);
594 void volumeDownPressed(QDeclarative1KeyEvent *event);
597 virtual void keyPressed(QKeyEvent *event, bool post);
598 virtual void keyReleased(QKeyEvent *event, bool post);
599 virtual void inputMethodEvent(QInputMethodEvent *, bool post);
600 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
602 const QByteArray keyToSignal(int key) {
603 QByteArray keySignal;
604 if (key >= Qt::Key_0 && key <= Qt::Key_9) {
605 keySignal = "digit0Pressed";
606 keySignal[5] = '0' + (key - Qt::Key_0);
609 while (sigMap[i].key && sigMap[i].key != key)
611 keySignal = sigMap[i].sig;
621 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