Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / qtquick1 / graphicsitems / qdeclarativeitem.cpp
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 QtDeclarative 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 #include "QtQuick1/qdeclarativeitem.h"
43
44 #include "QtQuick1/private/qdeclarativeevents_p_p.h"
45 #include <QtDeclarative/private/qdeclarativeengine_p.h>
46 #include <private/qgraphicsitem_p.h>
47 #include <QtQuick1/private/qdeclarativeitem_p.h>
48
49 #include <QtDeclarative/qdeclarativeengine.h>
50 #include <QtQuick1/private/qdeclarativeopenmetaobject_p.h>
51 #include <QtQuick1/private/qdeclarativestate_p.h>
52 #include <QtQuick1/qdeclarativeview.h>
53 #include <QtQuick1/private/qdeclarativestategroup_p.h>
54 #include <QtDeclarative/qdeclarativecomponent.h>
55 #include <QtDeclarative/qdeclarativeinfo.h>
56 // ### Due to the workaround mentioned in accessibleRole()
57 #include <QtQuick1/private/qdeclarativetext_p.h>
58
59 #include <QDebug>
60 #include <QPen>
61 #include <QEvent>
62 #include <QGraphicsSceneMouseEvent>
63 #include <QtCore/qnumeric.h>
64 #include <QtDeclarative/qjsengine.h>
65
66 #include <private/qv8engine_p.h>
67 #include <QtWidgets/qgraphicstransform.h>
68 #include <private/qlistmodelinterface_p.h>
69 #include <QAccessible>
70 #include <QtQuick1/private/qdeclarativeaccessibleattached_p.h>
71
72 #include <float.h>
73
74 QT_BEGIN_NAMESPACE
75
76 /*!
77     \qmlclass Transform QGraphicsTransform
78     \inqmlmodule QtQuick 1
79     \ingroup qml-transform-elements
80     \since QtQuick 1.0
81     \brief The Transform elements provide a way of building advanced transformations on Items.
82
83     The Transform element is a base type which cannot be instantiated directly.
84     The following concrete Transform types are available:
85
86     \list
87     \o \l Rotation
88     \o \l Scale
89     \o \l Translate
90     \endlist
91
92     The Transform elements let you create and control advanced transformations that can be configured
93     independently using specialized properties.
94
95     You can assign any number of Transform elements to an \l Item. Each Transform is applied in order,
96     one at a time.
97 */
98
99 /*!
100     \qmlclass Translate QDeclarative1Translate
101     \inqmlmodule QtQuick 1
102     \ingroup qml-transform-elements
103     \since QtQuick 1.0
104     \brief The Translate object provides a way to move an Item without changing its x or y properties.
105
106     The Translate object provides independent control over position in addition to the Item's x and y properties.
107
108     The following example moves the Y axis of the \l Rectangle elements while still allowing the \l Row element
109     to lay the items out as if they had not been transformed:
110     \qml
111     import QtQuick 1.0
112
113     Row {
114         Rectangle {
115             width: 100; height: 100
116             color: "blue"
117             transform: Translate { y: 20 }
118         }
119         Rectangle {
120             width: 100; height: 100
121             color: "red"
122             transform: Translate { y: -20 }
123         }
124     }
125     \endqml
126
127     \image translate.png
128 */
129
130 /*!
131     \qmlproperty real QtQuick1::Translate::x
132
133     The translation along the X axis.
134 */
135
136 /*!
137     \qmlproperty real QtQuick1::Translate::y
138
139     The translation along the Y axis.
140 */
141
142 /*!
143     \qmlclass Scale QGraphicsScale
144     \inqmlmodule QtQuick 1
145     \ingroup qml-transform-elements
146     \since QtQuick 1.0
147     \brief The Scale element provides a way to scale an Item.
148
149     The Scale element gives more control over scaling than using \l Item's \l{Item::scale}{scale} property. Specifically,
150     it allows a different scale for the x and y axes, and allows the scale to be relative to an
151     arbitrary point.
152
153     The following example scales the X axis of the Rectangle, relative to its interior point 25, 25:
154     \qml
155     Rectangle {
156         width: 100; height: 100
157         color: "blue"
158         transform: Scale { origin.x: 25; origin.y: 25; xScale: 3}
159     }
160     \endqml
161
162     \sa Rotation, Translate
163 */
164
165 /*!
166     \qmlproperty real QtQuick1::Scale::origin.x
167     \qmlproperty real QtQuick1::Scale::origin.y
168
169     The point that the item is scaled from (i.e., the point that stays fixed relative to the parent as
170     the rest of the item grows). By default the origin is 0, 0.
171 */
172
173 /*!
174     \qmlproperty real QtQuick1::Scale::xScale
175
176     The scaling factor for the X axis.
177 */
178
179 /*!
180     \qmlproperty real QtQuick1::Scale::yScale
181
182     The scaling factor for the Y axis.
183 */
184
185 /*!
186     \qmlclass Rotation QGraphicsRotation
187     \inqmlmodule QtQuick 1
188     \ingroup qml-transform-elements
189     \since QtQuick 1.0
190     \brief The Rotation object provides a way to rotate an Item.
191
192     The Rotation object gives more control over rotation than using \l Item's \l{Item::rotation}{rotation} property.
193     Specifically, it allows (z axis) rotation to be relative to an arbitrary point.
194
195     The following example rotates a Rectangle around its interior point 25, 25:
196     \qml
197     Rectangle {
198         width: 100; height: 100
199         color: "blue"
200         transform: Rotation { origin.x: 25; origin.y: 25; angle: 45}
201     }
202     \endqml
203
204     Rotation also provides a way to specify 3D-like rotations for Items. For these types of
205     rotations you must specify the axis to rotate around in addition to the origin point.
206
207     The following example shows various 3D-like rotations applied to an \l Image.
208     \snippet doc/src/snippets/qtquick1/rotation.qml 0
209
210     \image axisrotation.png
211
212     \sa {declarative/ui-components/dialcontrol}{Dial Control example}, {declarative/toys/clocks}{Clocks example}
213 */
214
215 /*!
216     \qmlproperty real QtQuick1::Rotation::origin.x
217     \qmlproperty real QtQuick1::Rotation::origin.y
218
219     The origin point of the rotation (i.e., the point that stays fixed relative to the parent as
220     the rest of the item rotates). By default the origin is 0, 0.
221 */
222
223 /*!
224     \qmlproperty real QtQuick1::Rotation::axis.x
225     \qmlproperty real QtQuick1::Rotation::axis.y
226     \qmlproperty real QtQuick1::Rotation::axis.z
227
228     The axis to rotate around. For simple (2D) rotation around a point, you do not need to specify an axis,
229     as the default axis is the z axis (\c{ axis { x: 0; y: 0; z: 1 } }).
230
231     For a typical 3D-like rotation you will usually specify both the origin and the axis.
232
233     \image 3d-rotation-axis.png
234 */
235
236 /*!
237     \qmlproperty real QtQuick1::Rotation::angle
238
239     The angle to rotate, in degrees clockwise.
240 */
241
242 QDeclarative1Contents::QDeclarative1Contents(QDeclarativeItem *item) : m_item(item), m_x(0), m_y(0), m_width(0), m_height(0)
243 {
244     //### optimize
245     connect(this, SIGNAL(rectChanged(QRectF)), m_item, SIGNAL(childrenRectChanged(QRectF)));
246 }
247
248 QDeclarative1Contents::~QDeclarative1Contents()
249 {
250     QList<QGraphicsItem *> children = m_item->childItems();
251     for (int i = 0; i < children.count(); ++i) {
252         QDeclarativeItem *child = qobject_cast<QDeclarativeItem *>(children.at(i));
253         if(!child)//### Should this be ignoring non-QDeclarativeItem graphicsobjects?
254             continue;
255         QDeclarativeItemPrivate::get(child)->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Destroyed);
256     }
257 }
258
259 QRectF QDeclarative1Contents::rectF() const
260 {
261     return QRectF(m_x, m_y, m_width, m_height);
262 }
263
264 void QDeclarative1Contents::calcHeight(QDeclarativeItem *changed)
265 {
266     qreal oldy = m_y;
267     qreal oldheight = m_height;
268
269     if (changed) {
270         qreal top = oldy;
271         qreal bottom = oldy + oldheight;
272         qreal y = changed->y();
273         if (y + changed->height() > bottom)
274             bottom = y + changed->height();
275         if (y < top)
276             top = y;
277         m_y = top;
278         m_height = bottom - top;
279     } else {
280         qreal top = FLT_MAX;
281         qreal bottom = 0;
282         QList<QGraphicsItem *> children = m_item->childItems();
283         for (int i = 0; i < children.count(); ++i) {
284             QDeclarativeItem *child = qobject_cast<QDeclarativeItem *>(children.at(i));
285             if(!child)//### Should this be ignoring non-QDeclarativeItem graphicsobjects?
286                 continue;
287             qreal y = child->y();
288             if (y + child->height() > bottom)
289                 bottom = y + child->height();
290             if (y < top)
291                 top = y;
292         }
293         if (!children.isEmpty())
294             m_y = top;
295         m_height = qMax(bottom - top, qreal(0.0));
296     }
297
298     if (m_height != oldheight || m_y != oldy)
299         emit rectChanged(rectF());
300 }
301
302 void QDeclarative1Contents::calcWidth(QDeclarativeItem *changed)
303 {
304     qreal oldx = m_x;
305     qreal oldwidth = m_width;
306
307     if (changed) {
308         qreal left = oldx;
309         qreal right = oldx + oldwidth;
310         qreal x = changed->x();
311         if (x + changed->width() > right)
312             right = x + changed->width();
313         if (x < left)
314             left = x;
315         m_x = left;
316         m_width = right - left;
317     } else {
318         qreal left = FLT_MAX;
319         qreal right = 0;
320         QList<QGraphicsItem *> children = m_item->childItems();
321         for (int i = 0; i < children.count(); ++i) {
322             QDeclarativeItem *child = qobject_cast<QDeclarativeItem *>(children.at(i));
323             if(!child)//### Should this be ignoring non-QDeclarativeItem graphicsobjects?
324                 continue;
325             qreal x = child->x();
326             if (x + child->width() > right)
327                 right = x + child->width();
328             if (x < left)
329                 left = x;
330         }
331         if (!children.isEmpty())
332             m_x = left;
333         m_width = qMax(right - left, qreal(0.0));
334     }
335
336     if (m_width != oldwidth || m_x != oldx)
337         emit rectChanged(rectF());
338 }
339
340 void QDeclarative1Contents::complete()
341 {
342     QList<QGraphicsItem *> children = m_item->childItems();
343     for (int i = 0; i < children.count(); ++i) {
344         QDeclarativeItem *child = qobject_cast<QDeclarativeItem *>(children.at(i));
345         if(!child)//### Should this be ignoring non-QDeclarativeItem graphicsobjects?
346             continue;
347         QDeclarativeItemPrivate::get(child)->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Destroyed);
348         //###what about changes to visibility?
349     }
350
351     calcGeometry();
352 }
353
354 void QDeclarative1Contents::itemGeometryChanged(QDeclarativeItem *changed, const QRectF &newGeometry, const QRectF &oldGeometry)
355 {
356     Q_UNUSED(changed)
357     //### we can only pass changed if the left edge has moved left, or the right edge has moved right
358     if (newGeometry.width() != oldGeometry.width() || newGeometry.x() != oldGeometry.x())
359         calcWidth(/*changed*/);
360     if (newGeometry.height() != oldGeometry.height() || newGeometry.y() != oldGeometry.y())
361         calcHeight(/*changed*/);
362 }
363
364 void QDeclarative1Contents::itemDestroyed(QDeclarativeItem *item)
365 {
366     if (item)
367         QDeclarativeItemPrivate::get(item)->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Destroyed);
368     calcGeometry();
369 }
370
371 void QDeclarative1Contents::childRemoved(QDeclarativeItem *item)
372 {
373     if (item)
374         QDeclarativeItemPrivate::get(item)->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Destroyed);
375     calcGeometry();
376 }
377
378 void QDeclarative1Contents::childAdded(QDeclarativeItem *item)
379 {
380     if (item)
381         QDeclarativeItemPrivate::get(item)->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Destroyed);
382     calcWidth(item);
383     calcHeight(item);
384 }
385
386 QDeclarativeItemKeyFilter::QDeclarativeItemKeyFilter(QDeclarativeItem *item)
387 : m_processPost(false), m_next(0)
388 {
389     QDeclarativeItemPrivate *p =
390         item?static_cast<QDeclarativeItemPrivate *>(QGraphicsItemPrivate::get(item)):0;
391     if (p) {
392         m_next = p->keyHandler;
393         p->keyHandler = this;
394     }
395 }
396
397 QDeclarativeItemKeyFilter::~QDeclarativeItemKeyFilter()
398 {
399 }
400
401 void QDeclarativeItemKeyFilter::keyPressed(QKeyEvent *event, bool post)
402 {
403     if (m_next) m_next->keyPressed(event, post);
404 }
405
406 void QDeclarativeItemKeyFilter::keyReleased(QKeyEvent *event, bool post)
407 {
408     if (m_next) m_next->keyReleased(event, post);
409 }
410
411 void QDeclarativeItemKeyFilter::inputMethodEvent(QInputMethodEvent *event, bool post)
412 {
413     if (m_next) m_next->inputMethodEvent(event, post);
414 }
415
416 QVariant QDeclarativeItemKeyFilter::inputMethodQuery(Qt::InputMethodQuery query) const
417 {
418     if (m_next) return m_next->inputMethodQuery(query);
419     return QVariant();
420 }
421
422 void QDeclarativeItemKeyFilter::componentComplete()
423 {
424     if (m_next) m_next->componentComplete();
425 }
426
427
428 /*!
429     \qmlclass KeyNavigation QDeclarative1KeyNavigationAttached
430     \inqmlmodule QtQuick 1
431     \ingroup qml-basic-interaction-elements
432     \since QtQuick 1.0
433     \brief The KeyNavigation attached property supports key navigation by arrow keys.
434
435     Key-based user interfaces commonly allow the use of arrow keys to navigate between
436     focusable items.  The KeyNavigation attached property enables this behavior by providing a
437     convenient way to specify the item that should gain focus when an arrow or tab key is pressed.
438
439     The following example provides key navigation for a 2x2 grid of items:
440
441     \snippet doc/src/snippets/qtquick1/keynavigation.qml 0
442
443     The top-left item initially receives focus by setting \l {Item::}{focus} to
444     \c true. When an arrow key is pressed, the focus will move to the
445     appropriate item, as defined by the value that has been set for
446     the KeyNavigation \l left, \l right, \l up or \l down properties.
447
448     Note that if a KeyNavigation attached property receives the key press and release
449     events for a requested arrow or tab key, the event is accepted and does not
450     propagate any further.
451
452     By default, KeyNavigation receives key events after the item to which it is attached.
453     If the item accepts the key event, the KeyNavigation attached property will not
454     receive an event for that key.  Setting the \l priority property to
455     \c KeyNavigation.BeforeItem allows the event to be used for key navigation
456     before the item, rather than after.
457
458     If item to which the focus is switching is not enabled or visible, an attempt will
459     be made to skip this item and focus on the next. This is possible if there are
460     a chain of items with the same KeyNavigation handler. If multiple items in a row are not enabled
461     or visible, they will also be skipped.
462
463     KeyNavigation will implicitly set the other direction to return focus to this item. So if you set
464     \l left to another item, \l right will be set on that item's KeyNavigation to set focus back to this
465     item. However, if that item's KeyNavigation has had right explicitly set then no change will occur.
466     This means that the above example could have been written, with the same behaviour, without specifing
467     KeyNavigation.right or KeyNavigation.down for any of the items.
468
469     \sa {Keys}{Keys attached property}
470 */
471
472 /*!
473     \qmlproperty Item QtQuick1::KeyNavigation::left
474     \qmlproperty Item QtQuick1::KeyNavigation::right
475     \qmlproperty Item QtQuick1::KeyNavigation::up
476     \qmlproperty Item QtQuick1::KeyNavigation::down
477     \qmlproperty Item QtQuick1::KeyNavigation::tab
478     \qmlproperty Item QtQuick1::KeyNavigation::backtab
479
480     These properties hold the item to assign focus to
481     when the left, right, up or down cursor keys, or the
482     tab key are pressed.
483 */
484
485 /*!
486     \qmlproperty Item QtQuick1::KeyNavigation::tab
487     \qmlproperty Item QtQuick1::KeyNavigation::backtab
488
489     These properties hold the item to assign focus to
490     when the Tab key or Shift+Tab key combination (Backtab) are pressed.
491 */
492
493 QDeclarative1KeyNavigationAttached::QDeclarative1KeyNavigationAttached(QObject *parent)
494 : QObject(*(new QDeclarative1KeyNavigationAttachedPrivate), parent),
495   QDeclarativeItemKeyFilter(qobject_cast<QDeclarativeItem*>(parent))
496 {
497     m_processPost = true;
498 }
499
500 QDeclarative1KeyNavigationAttached *
501 QDeclarative1KeyNavigationAttached::qmlAttachedProperties(QObject *obj)
502 {
503     return new QDeclarative1KeyNavigationAttached(obj);
504 }
505
506 QDeclarativeItem *QDeclarative1KeyNavigationAttached::left() const
507 {
508     Q_D(const QDeclarative1KeyNavigationAttached);
509     return d->left;
510 }
511
512 void QDeclarative1KeyNavigationAttached::setLeft(QDeclarativeItem *i)
513 {
514     Q_D(QDeclarative1KeyNavigationAttached);
515     if (d->left == i)
516         return;
517     d->left = i;
518     d->leftSet = true;
519     QDeclarative1KeyNavigationAttached* other =
520             qobject_cast<QDeclarative1KeyNavigationAttached*>(qmlAttachedPropertiesObject<QDeclarative1KeyNavigationAttached>(i));
521     if(other && !other->d_func()->rightSet){
522         other->d_func()->right = qobject_cast<QDeclarativeItem*>(parent());
523         emit other->rightChanged();
524     }
525     emit leftChanged();
526 }
527
528 QDeclarativeItem *QDeclarative1KeyNavigationAttached::right() const
529 {
530     Q_D(const QDeclarative1KeyNavigationAttached);
531     return d->right;
532 }
533
534 void QDeclarative1KeyNavigationAttached::setRight(QDeclarativeItem *i)
535 {
536     Q_D(QDeclarative1KeyNavigationAttached);
537     if (d->right == i)
538         return;
539     d->right = i;
540     d->rightSet = true;
541     QDeclarative1KeyNavigationAttached* other =
542             qobject_cast<QDeclarative1KeyNavigationAttached*>(qmlAttachedPropertiesObject<QDeclarative1KeyNavigationAttached>(i));
543     if(other && !other->d_func()->leftSet){
544         other->d_func()->left = qobject_cast<QDeclarativeItem*>(parent());
545         emit other->leftChanged();
546     }
547     emit rightChanged();
548 }
549
550 QDeclarativeItem *QDeclarative1KeyNavigationAttached::up() const
551 {
552     Q_D(const QDeclarative1KeyNavigationAttached);
553     return d->up;
554 }
555
556 void QDeclarative1KeyNavigationAttached::setUp(QDeclarativeItem *i)
557 {
558     Q_D(QDeclarative1KeyNavigationAttached);
559     if (d->up == i)
560         return;
561     d->up = i;
562     d->upSet = true;
563     QDeclarative1KeyNavigationAttached* other =
564             qobject_cast<QDeclarative1KeyNavigationAttached*>(qmlAttachedPropertiesObject<QDeclarative1KeyNavigationAttached>(i));
565     if(other && !other->d_func()->downSet){
566         other->d_func()->down = qobject_cast<QDeclarativeItem*>(parent());
567         emit other->downChanged();
568     }
569     emit upChanged();
570 }
571
572 QDeclarativeItem *QDeclarative1KeyNavigationAttached::down() const
573 {
574     Q_D(const QDeclarative1KeyNavigationAttached);
575     return d->down;
576 }
577
578 void QDeclarative1KeyNavigationAttached::setDown(QDeclarativeItem *i)
579 {
580     Q_D(QDeclarative1KeyNavigationAttached);
581     if (d->down == i)
582         return;
583     d->down = i;
584     d->downSet = true;
585     QDeclarative1KeyNavigationAttached* other =
586             qobject_cast<QDeclarative1KeyNavigationAttached*>(qmlAttachedPropertiesObject<QDeclarative1KeyNavigationAttached>(i));
587     if(other && !other->d_func()->upSet){
588         other->d_func()->up = qobject_cast<QDeclarativeItem*>(parent());
589         emit other->upChanged();
590     }
591     emit downChanged();
592 }
593
594 QDeclarativeItem *QDeclarative1KeyNavigationAttached::tab() const
595 {
596     Q_D(const QDeclarative1KeyNavigationAttached);
597     return d->tab;
598 }
599
600 void QDeclarative1KeyNavigationAttached::setTab(QDeclarativeItem *i)
601 {
602     Q_D(QDeclarative1KeyNavigationAttached);
603     if (d->tab == i)
604         return;
605     d->tab = i;
606     d->tabSet = true;
607     QDeclarative1KeyNavigationAttached* other =
608             qobject_cast<QDeclarative1KeyNavigationAttached*>(qmlAttachedPropertiesObject<QDeclarative1KeyNavigationAttached>(i));
609     if(other && !other->d_func()->backtabSet){
610         other->d_func()->backtab = qobject_cast<QDeclarativeItem*>(parent());
611         emit other->backtabChanged();
612     }
613     emit tabChanged();
614 }
615
616 QDeclarativeItem *QDeclarative1KeyNavigationAttached::backtab() const
617 {
618     Q_D(const QDeclarative1KeyNavigationAttached);
619     return d->backtab;
620 }
621
622 void QDeclarative1KeyNavigationAttached::setBacktab(QDeclarativeItem *i)
623 {
624     Q_D(QDeclarative1KeyNavigationAttached);
625     if (d->backtab == i)
626         return;
627     d->backtab = i;
628     d->backtabSet = true;
629     QDeclarative1KeyNavigationAttached* other =
630             qobject_cast<QDeclarative1KeyNavigationAttached*>(qmlAttachedPropertiesObject<QDeclarative1KeyNavigationAttached>(i));
631     if(other && !other->d_func()->tabSet){
632         other->d_func()->tab = qobject_cast<QDeclarativeItem*>(parent());
633         emit other->tabChanged();
634     }
635     emit backtabChanged();
636 }
637
638 /*!
639     \qmlproperty enumeration QtQuick1::KeyNavigation::priority
640
641     This property determines whether the keys are processed before
642     or after the attached item's own key handling.
643
644     \list
645     \o KeyNavigation.BeforeItem - process the key events before normal
646     item key processing.  If the event is used for key navigation, it will be accepted and will not
647     be passed on to the item.
648     \o KeyNavigation.AfterItem (default) - process the key events after normal item key
649     handling.  If the item accepts the key event it will not be
650     handled by the KeyNavigation attached property handler.
651     \endlist
652 */
653 QDeclarative1KeyNavigationAttached::Priority QDeclarative1KeyNavigationAttached::priority() const
654 {
655     return m_processPost ? AfterItem : BeforeItem;
656 }
657
658 void QDeclarative1KeyNavigationAttached::setPriority(Priority order)
659 {
660     bool processPost = order == AfterItem;
661     if (processPost != m_processPost) {
662         m_processPost = processPost;
663         emit priorityChanged();
664     }
665 }
666
667 void QDeclarative1KeyNavigationAttached::keyPressed(QKeyEvent *event, bool post)
668 {
669     Q_D(QDeclarative1KeyNavigationAttached);
670     event->ignore();
671
672     if (post != m_processPost) {
673         QDeclarativeItemKeyFilter::keyPressed(event, post);
674         return;
675     }
676
677     bool mirror = false;
678     switch(event->key()) {
679     case Qt::Key_Left: {
680         if (QDeclarativeItem *parentItem = qobject_cast<QDeclarativeItem*>(parent()))
681             mirror = QDeclarativeItemPrivate::get(parentItem)->effectiveLayoutMirror;
682         QDeclarativeItem* leftItem = mirror ? d->right : d->left;
683         if (leftItem) {
684             setFocusNavigation(leftItem, mirror ? "right" : "left");
685             event->accept();
686         }
687         break;
688     }
689     case Qt::Key_Right: {
690         if (QDeclarativeItem *parentItem = qobject_cast<QDeclarativeItem*>(parent()))
691             mirror = QDeclarativeItemPrivate::get(parentItem)->effectiveLayoutMirror;
692         QDeclarativeItem* rightItem = mirror ? d->left : d->right;
693         if (rightItem) {
694             setFocusNavigation(rightItem, mirror ? "left" : "right");
695             event->accept();
696         }
697         break;
698     }
699     case Qt::Key_Up:
700         if (d->up) {
701             setFocusNavigation(d->up, "up");
702             event->accept();
703         }
704         break;
705     case Qt::Key_Down:
706         if (d->down) {
707             setFocusNavigation(d->down, "down");
708             event->accept();
709         }
710         break;
711     case Qt::Key_Tab:
712         if (d->tab) {
713             setFocusNavigation(d->tab, "tab");
714             event->accept();
715         }
716         break;
717     case Qt::Key_Backtab:
718         if (d->backtab) {
719             setFocusNavigation(d->backtab, "backtab");
720             event->accept();
721         }
722         break;
723     default:
724         break;
725     }
726
727     if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyPressed(event, post);
728 }
729
730 void QDeclarative1KeyNavigationAttached::keyReleased(QKeyEvent *event, bool post)
731 {
732     Q_D(QDeclarative1KeyNavigationAttached);
733     event->ignore();
734
735     if (post != m_processPost) {
736         QDeclarativeItemKeyFilter::keyReleased(event, post);
737         return;
738     }
739
740     bool mirror = false;
741     switch(event->key()) {
742     case Qt::Key_Left:
743         if (QDeclarativeItem *parentItem = qobject_cast<QDeclarativeItem*>(parent()))
744             mirror = QDeclarativeItemPrivate::get(parentItem)->effectiveLayoutMirror;
745         if (mirror ? d->right : d->left)
746             event->accept();
747         break;
748     case Qt::Key_Right:
749         if (QDeclarativeItem *parentItem = qobject_cast<QDeclarativeItem*>(parent()))
750             mirror = QDeclarativeItemPrivate::get(parentItem)->effectiveLayoutMirror;
751         if (mirror ? d->left : d->right)
752             event->accept();
753         break;
754     case Qt::Key_Up:
755         if (d->up) {
756             event->accept();
757         }
758         break;
759     case Qt::Key_Down:
760         if (d->down) {
761             event->accept();
762         }
763         break;
764     case Qt::Key_Tab:
765         if (d->tab) {
766             event->accept();
767         }
768         break;
769     case Qt::Key_Backtab:
770         if (d->backtab) {
771             event->accept();
772         }
773         break;
774     default:
775         break;
776     }
777
778     if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyReleased(event, post);
779 }
780
781 void QDeclarative1KeyNavigationAttached::setFocusNavigation(QDeclarativeItem *currentItem, const char *dir)
782 {
783     QDeclarativeItem *initialItem = currentItem;
784     bool isNextItem = false;
785     do {
786         isNextItem = false;
787         if (currentItem->isVisible() && currentItem->isEnabled()) {
788             currentItem->setFocus(true);
789         } else {
790             QObject *attached =
791                 qmlAttachedPropertiesObject<QDeclarative1KeyNavigationAttached>(currentItem, false);
792             if (attached) {
793                 QDeclarativeItem *tempItem = qvariant_cast<QDeclarativeItem*>(attached->property(dir));
794                 if (tempItem) {
795                     currentItem = tempItem;
796                     isNextItem = true;
797                 }
798             }
799         }
800     }
801     while (currentItem != initialItem && isNextItem);
802 }
803
804 /*!
805     \qmlclass LayoutMirroring QDeclarative1LayoutMirroringAttached
806     \inqmlmodule QtQuick 1
807     \since QtQuick 1.1
808     \ingroup qml-utility-elements
809     \brief The LayoutMirroring attached property is used to mirror layout behavior.
810
811     The LayoutMirroring attached property is used to horizontally mirror \l {anchor-layout}{Item anchors},
812     \l{Using QML Positioner and Repeater Items}{positioner} elements (such as \l Row and \l Grid)
813     and views (such as \l GridView and horizontal \l ListView). Mirroring is a visual change: left
814     anchors become right anchors, and positioner elements like \l Grid and \l Row reverse the
815     horizontal layout of child items.
816
817     Mirroring is enabled for an item by setting the \l enabled property to true. By default, this
818     only affects the item itself; setting the \l childrenInherit property to true propagates the mirroring
819     behavior to all child elements as well. If the \c LayoutMirroring attached property has not been defined
820     for an item, mirroring is not enabled.
821
822     The following example shows mirroring in action. The \l Row below is specified as being anchored
823     to the left of its parent. However, since mirroring has been enabled, the anchor is horizontally
824     reversed and it is now anchored to the right. Also, since items in a \l Row are positioned
825     from left to right by default, they are now positioned from right to left instead, as demonstrated
826     by the numbering and opacity of the items:
827
828     \snippet doc/src/snippets/qtquick1/layoutmirroring.qml 0
829
830     \image layoutmirroring.png
831
832     Layout mirroring is useful when it is necessary to support both left-to-right and right-to-left
833     layout versions of an application to target different language areas. The \l childrenInherit
834     property allows layout mirroring to be applied without manually setting layout configurations
835     for every item in an application. Keep in mind, however, that mirroring does not affect any
836     positioning that is defined by the \l Item \l {Item::}{x} coordinate value, so even with
837     mirroring enabled, it will often be necessary to apply some layout fixes to support the
838     desired layout direction. Also, it may be necessary to disable the mirroring of individual
839     child items (by setting \l {enabled}{LayoutMirroring.enabled} to false for such items) if
840     mirroring is not the desired behavior, or if the child item already implements mirroring in
841     some custom way.
842
843     See \l {QML Right-to-left User Interfaces} for further details on using \c LayoutMirroring and
844     other related features to implement right-to-left support for an application.
845 */
846
847 /*!
848     \qmlproperty bool QtQuick1::LayoutMirroring::enabled
849
850     This property holds whether the item's layout is mirrored horizontally. Setting this to true
851     horizontally reverses \l {anchor-layout}{anchor} settings such that left anchors become right,
852     and right anchors become left. For \l{Using QML Positioner and Repeater Items}{positioner} elements
853     (such as \l Row and \l Grid) and view elements (such as \l {GridView}{GridView} and \l {ListView}{ListView})
854     this also mirrors the horizontal layout direction of the item.
855
856     The default value is false.
857 */
858
859 /*!
860     \qmlproperty bool QtQuick1::LayoutMirroring::childrenInherit
861
862     This property holds whether the \l {enabled}{LayoutMirroring.enabled} value for this item
863     is inherited by its children.
864
865     The default value is false.
866 */
867
868 QDeclarative1LayoutMirroringAttached::QDeclarative1LayoutMirroringAttached(QObject *parent) : QObject(parent), itemPrivate(0)
869 {
870     if (QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(parent)) {
871         itemPrivate = QDeclarativeItemPrivate::get(item);
872         itemPrivate->attachedLayoutDirection = this;
873     } else
874         qmlInfo(parent) << tr("LayoutDirection attached property only works with Items");
875 }
876
877 QDeclarative1LayoutMirroringAttached * QDeclarative1LayoutMirroringAttached::qmlAttachedProperties(QObject *object)
878 {
879     return new QDeclarative1LayoutMirroringAttached(object);
880 }
881
882 bool QDeclarative1LayoutMirroringAttached::enabled() const
883 {
884     return itemPrivate ? itemPrivate->effectiveLayoutMirror : false;
885 }
886
887 void QDeclarative1LayoutMirroringAttached::setEnabled(bool enabled)
888 {
889     if (!itemPrivate)
890         return;
891
892     itemPrivate->isMirrorImplicit = false;
893     if (enabled != itemPrivate->effectiveLayoutMirror) {
894         itemPrivate->setLayoutMirror(enabled);
895         if (itemPrivate->inheritMirrorFromItem)
896              itemPrivate->resolveLayoutMirror();
897     }
898 }
899
900 void QDeclarative1LayoutMirroringAttached::resetEnabled()
901 {
902     if (itemPrivate && !itemPrivate->isMirrorImplicit) {
903         itemPrivate->isMirrorImplicit = true;
904         itemPrivate->resolveLayoutMirror();
905     }
906 }
907
908 bool QDeclarative1LayoutMirroringAttached::childrenInherit() const
909 {
910     return itemPrivate ? itemPrivate->inheritMirrorFromItem : false;
911 }
912
913 void QDeclarative1LayoutMirroringAttached::setChildrenInherit(bool childrenInherit) {
914     if (itemPrivate && childrenInherit != itemPrivate->inheritMirrorFromItem) {
915         itemPrivate->inheritMirrorFromItem = childrenInherit;
916         itemPrivate->resolveLayoutMirror();
917         childrenInheritChanged();
918     }
919 }
920
921 void QDeclarativeItemPrivate::resolveLayoutMirror()
922 {
923     Q_Q(QDeclarativeItem);
924     if (QDeclarativeItem *parentItem = q->parentItem()) {
925         QDeclarativeItemPrivate *parentPrivate = QDeclarativeItemPrivate::get(parentItem);
926         setImplicitLayoutMirror(parentPrivate->inheritedLayoutMirror, parentPrivate->inheritMirrorFromParent);
927     } else {
928         setImplicitLayoutMirror(isMirrorImplicit ? false : effectiveLayoutMirror, inheritMirrorFromItem);
929     }
930 }
931
932 void QDeclarativeItemPrivate::setImplicitLayoutMirror(bool mirror, bool inherit)
933 {
934     inherit = inherit || inheritMirrorFromItem;
935     if (!isMirrorImplicit && inheritMirrorFromItem)
936         mirror = effectiveLayoutMirror;
937     if (mirror == inheritedLayoutMirror && inherit == inheritMirrorFromParent)
938         return;
939
940     inheritMirrorFromParent = inherit;
941     inheritedLayoutMirror = inheritMirrorFromParent ? mirror : false;
942
943     if (isMirrorImplicit)
944         setLayoutMirror(inherit ? inheritedLayoutMirror : false);
945     for (int i = 0; i < children.count(); ++i) {
946         if (QDeclarativeItem *child = qobject_cast<QDeclarativeItem *>(children.at(i))) {
947             QDeclarativeItemPrivate *childPrivate = QDeclarativeItemPrivate::get(child);
948             childPrivate->setImplicitLayoutMirror(inheritedLayoutMirror, inheritMirrorFromParent);
949         }
950     }
951 }
952
953 void QDeclarativeItemPrivate::setLayoutMirror(bool mirror)
954 {
955     if (mirror != effectiveLayoutMirror) {
956         effectiveLayoutMirror = mirror;
957         if (_anchors) {
958             _anchors->d_func()->fillChanged();
959             _anchors->d_func()->centerInChanged();
960             _anchors->d_func()->updateHorizontalAnchors();
961             emit _anchors->mirroredChanged();
962         }
963         mirrorChange();
964         if (attachedLayoutDirection) {
965             emit attachedLayoutDirection->enabledChanged();
966         }
967     }
968 }
969
970 /*!
971     \qmlclass Keys QDeclarative1KeysAttached
972     \inqmlmodule QtQuick 1
973     \ingroup qml-basic-interaction-elements
974     \since QtQuick 1.0
975     \brief The Keys attached property provides key handling to Items.
976
977     All visual primitives support key handling via the Keys
978     attached property.  Keys can be handled via the onPressed
979     and onReleased signal properties.
980
981     The signal properties have a \l KeyEvent parameter, named
982     \e event which contains details of the event.  If a key is
983     handled \e event.accepted should be set to true to prevent the
984     event from propagating up the item hierarchy.
985
986     \section1 Example Usage
987
988     The following example shows how the general onPressed handler can
989     be used to test for a certain key; in this case, the left cursor
990     key:
991
992     \snippet doc/src/snippets/qtquick1/keys/keys-pressed.qml key item
993
994     Some keys may alternatively be handled via specific signal properties,
995     for example \e onSelectPressed.  These handlers automatically set
996     \e event.accepted to true.
997
998     \snippet doc/src/snippets/qtquick1/keys/keys-handler.qml key item
999
1000     See \l{Qt::Key}{Qt.Key} for the list of keyboard codes.
1001
1002     \section1 Key Handling Priorities
1003
1004     The Keys attached property can be configured to handle key events
1005     before or after the item it is attached to. This makes it possible
1006     to intercept events in order to override an item's default behavior,
1007     or act as a fallback for keys not handled by the item.
1008
1009     If \l priority is Keys.BeforeItem (default) the order of key event processing is:
1010
1011     \list 1
1012     \o Items specified in \c forwardTo
1013     \o specific key handlers, e.g. onReturnPressed
1014     \o onKeyPress, onKeyRelease handlers
1015     \o Item specific key handling, e.g. TextInput key handling
1016     \o parent item
1017     \endlist
1018
1019     If priority is Keys.AfterItem the order of key event processing is:
1020
1021     \list 1
1022     \o Item specific key handling, e.g. TextInput key handling
1023     \o Items specified in \c forwardTo
1024     \o specific key handlers, e.g. onReturnPressed
1025     \o onKeyPress, onKeyRelease handlers
1026     \o parent item
1027     \endlist
1028
1029     If the event is accepted during any of the above steps, key
1030     propagation stops.
1031
1032     \sa KeyEvent, {KeyNavigation}{KeyNavigation attached property}
1033 */
1034
1035 /*!
1036     \qmlproperty bool QtQuick1::Keys::enabled
1037
1038     This flags enables key handling if true (default); otherwise
1039     no key handlers will be called.
1040 */
1041
1042 /*!
1043     \qmlproperty enumeration QtQuick1::Keys::priority
1044
1045     This property determines whether the keys are processed before
1046     or after the attached item's own key handling.
1047
1048     \list
1049     \o Keys.BeforeItem (default) - process the key events before normal
1050     item key processing.  If the event is accepted it will not
1051     be passed on to the item.
1052     \o Keys.AfterItem - process the key events after normal item key
1053     handling.  If the item accepts the key event it will not be
1054     handled by the Keys attached property handler.
1055     \endlist
1056 */
1057
1058 /*!
1059     \qmlproperty list<Object> QtQuick1::Keys::forwardTo
1060
1061     This property provides a way to forward key presses, key releases, and keyboard input
1062     coming from input methods to other items. This can be useful when you want
1063     one item to handle some keys (e.g. the up and down arrow keys), and another item to
1064     handle other keys (e.g. the left and right arrow keys).  Once an item that has been
1065     forwarded keys accepts the event it is no longer forwarded to items later in the
1066     list.
1067
1068     This example forwards key events to two lists:
1069     \qml
1070     Item {
1071         ListView {
1072             id: list1
1073             // ...
1074         }
1075         ListView {
1076             id: list2
1077             // ...
1078         }
1079         Keys.forwardTo: [list1, list2]
1080         focus: true
1081     }
1082     \endqml
1083 */
1084
1085 /*!
1086     \qmlsignal QtQuick1::Keys::onPressed(KeyEvent event)
1087
1088     This handler is called when a key has been pressed. The \a event
1089     parameter provides information about the event.
1090 */
1091
1092 /*!
1093     \qmlsignal QtQuick1::Keys::onReleased(KeyEvent event)
1094
1095     This handler is called when a key has been released. The \a event
1096     parameter provides information about the event.
1097 */
1098
1099 /*!
1100     \qmlsignal QtQuick1::Keys::onDigit0Pressed(KeyEvent event)
1101
1102     This handler is called when the digit '0' has been pressed. The \a event
1103     parameter provides information about the event.
1104 */
1105
1106 /*!
1107     \qmlsignal QtQuick1::Keys::onDigit1Pressed(KeyEvent event)
1108
1109     This handler is called when the digit '1' has been pressed. The \a event
1110     parameter provides information about the event.
1111 */
1112
1113 /*!
1114     \qmlsignal QtQuick1::Keys::onDigit2Pressed(KeyEvent event)
1115
1116     This handler is called when the digit '2' has been pressed. The \a event
1117     parameter provides information about the event.
1118 */
1119
1120 /*!
1121     \qmlsignal QtQuick1::Keys::onDigit3Pressed(KeyEvent event)
1122
1123     This handler is called when the digit '3' has been pressed. The \a event
1124     parameter provides information about the event.
1125 */
1126
1127 /*!
1128     \qmlsignal QtQuick1::Keys::onDigit4Pressed(KeyEvent event)
1129
1130     This handler is called when the digit '4' has been pressed. The \a event
1131     parameter provides information about the event.
1132 */
1133
1134 /*!
1135     \qmlsignal QtQuick1::Keys::onDigit5Pressed(KeyEvent event)
1136
1137     This handler is called when the digit '5' has been pressed. The \a event
1138     parameter provides information about the event.
1139 */
1140
1141 /*!
1142     \qmlsignal QtQuick1::Keys::onDigit6Pressed(KeyEvent event)
1143
1144     This handler is called when the digit '6' has been pressed. The \a event
1145     parameter provides information about the event.
1146 */
1147
1148 /*!
1149     \qmlsignal QtQuick1::Keys::onDigit7Pressed(KeyEvent event)
1150
1151     This handler is called when the digit '7' has been pressed. The \a event
1152     parameter provides information about the event.
1153 */
1154
1155 /*!
1156     \qmlsignal QtQuick1::Keys::onDigit8Pressed(KeyEvent event)
1157
1158     This handler is called when the digit '8' has been pressed. The \a event
1159     parameter provides information about the event.
1160 */
1161
1162 /*!
1163     \qmlsignal QtQuick1::Keys::onDigit9Pressed(KeyEvent event)
1164
1165     This handler is called when the digit '9' has been pressed. The \a event
1166     parameter provides information about the event.
1167 */
1168
1169 /*!
1170     \qmlsignal QtQuick1::Keys::onLeftPressed(KeyEvent event)
1171
1172     This handler is called when the Left arrow has been pressed. The \a event
1173     parameter provides information about the event.
1174 */
1175
1176 /*!
1177     \qmlsignal QtQuick1::Keys::onRightPressed(KeyEvent event)
1178
1179     This handler is called when the Right arrow has been pressed. The \a event
1180     parameter provides information about the event.
1181 */
1182
1183 /*!
1184     \qmlsignal QtQuick1::Keys::onUpPressed(KeyEvent event)
1185
1186     This handler is called when the Up arrow has been pressed. The \a event
1187     parameter provides information about the event.
1188 */
1189
1190 /*!
1191     \qmlsignal QtQuick1::Keys::onDownPressed(KeyEvent event)
1192
1193     This handler is called when the Down arrow has been pressed. The \a event
1194     parameter provides information about the event.
1195 */
1196
1197 /*!
1198     \qmlsignal QtQuick1::Keys::onTabPressed(KeyEvent event)
1199
1200     This handler is called when the Tab key has been pressed. The \a event
1201     parameter provides information about the event.
1202 */
1203
1204 /*!
1205     \qmlsignal QtQuick1::Keys::onBacktabPressed(KeyEvent event)
1206
1207     This handler is called when the Shift+Tab key combination (Backtab) has
1208     been pressed. The \a event parameter provides information about the event.
1209 */
1210
1211 /*!
1212     \qmlsignal QtQuick1::Keys::onAsteriskPressed(KeyEvent event)
1213
1214     This handler is called when the Asterisk '*' has been pressed. The \a event
1215     parameter provides information about the event.
1216 */
1217
1218 /*!
1219     \qmlsignal QtQuick1::Keys::onEscapePressed(KeyEvent event)
1220
1221     This handler is called when the Escape key has been pressed. The \a event
1222     parameter provides information about the event.
1223 */
1224
1225 /*!
1226     \qmlsignal QtQuick1::Keys::onReturnPressed(KeyEvent event)
1227
1228     This handler is called when the Return key has been pressed. The \a event
1229     parameter provides information about the event.
1230 */
1231
1232 /*!
1233     \qmlsignal QtQuick1::Keys::onEnterPressed(KeyEvent event)
1234
1235     This handler is called when the Enter key has been pressed. The \a event
1236     parameter provides information about the event.
1237 */
1238
1239 /*!
1240     \qmlsignal QtQuick1::Keys::onDeletePressed(KeyEvent event)
1241
1242     This handler is called when the Delete key has been pressed. The \a event
1243     parameter provides information about the event.
1244 */
1245
1246 /*!
1247     \qmlsignal QtQuick1::Keys::onSpacePressed(KeyEvent event)
1248
1249     This handler is called when the Space key has been pressed. The \a event
1250     parameter provides information about the event.
1251 */
1252
1253 /*!
1254     \qmlsignal QtQuick1::Keys::onBackPressed(KeyEvent event)
1255
1256     This handler is called when the Back key has been pressed. The \a event
1257     parameter provides information about the event.
1258 */
1259
1260 /*!
1261     \qmlsignal QtQuick1::Keys::onCancelPressed(KeyEvent event)
1262
1263     This handler is called when the Cancel key has been pressed. The \a event
1264     parameter provides information about the event.
1265 */
1266
1267 /*!
1268     \qmlsignal QtQuick1::Keys::onSelectPressed(KeyEvent event)
1269
1270     This handler is called when the Select key has been pressed. The \a event
1271     parameter provides information about the event.
1272 */
1273
1274 /*!
1275     \qmlsignal QtQuick1::Keys::onYesPressed(KeyEvent event)
1276
1277     This handler is called when the Yes key has been pressed. The \a event
1278     parameter provides information about the event.
1279 */
1280
1281 /*!
1282     \qmlsignal QtQuick1::Keys::onNoPressed(KeyEvent event)
1283
1284     This handler is called when the No key has been pressed. The \a event
1285     parameter provides information about the event.
1286 */
1287
1288 /*!
1289     \qmlsignal QtQuick1::Keys::onContext1Pressed(KeyEvent event)
1290
1291     This handler is called when the Context1 key has been pressed. The \a event
1292     parameter provides information about the event.
1293 */
1294
1295 /*!
1296     \qmlsignal QtQuick1::Keys::onContext2Pressed(KeyEvent event)
1297
1298     This handler is called when the Context2 key has been pressed. The \a event
1299     parameter provides information about the event.
1300 */
1301
1302 /*!
1303     \qmlsignal QtQuick1::Keys::onContext3Pressed(KeyEvent event)
1304
1305     This handler is called when the Context3 key has been pressed. The \a event
1306     parameter provides information about the event.
1307 */
1308
1309 /*!
1310     \qmlsignal QtQuick1::Keys::onContext4Pressed(KeyEvent event)
1311
1312     This handler is called when the Context4 key has been pressed. The \a event
1313     parameter provides information about the event.
1314 */
1315
1316 /*!
1317     \qmlsignal QtQuick1::Keys::onCallPressed(KeyEvent event)
1318
1319     This handler is called when the Call key has been pressed. The \a event
1320     parameter provides information about the event.
1321 */
1322
1323 /*!
1324     \qmlsignal QtQuick1::Keys::onHangupPressed(KeyEvent event)
1325
1326     This handler is called when the Hangup key has been pressed. The \a event
1327     parameter provides information about the event.
1328 */
1329
1330 /*!
1331     \qmlsignal QtQuick1::Keys::onFlipPressed(KeyEvent event)
1332
1333     This handler is called when the Flip key has been pressed. The \a event
1334     parameter provides information about the event.
1335 */
1336
1337 /*!
1338     \qmlsignal QtQuick1::Keys::onMenuPressed(KeyEvent event)
1339
1340     This handler is called when the Menu key has been pressed. The \a event
1341     parameter provides information about the event.
1342 */
1343
1344 /*!
1345     \qmlsignal QtQuick1::Keys::onVolumeUpPressed(KeyEvent event)
1346
1347     This handler is called when the VolumeUp key has been pressed. The \a event
1348     parameter provides information about the event.
1349 */
1350
1351 /*!
1352     \qmlsignal QtQuick1::Keys::onVolumeDownPressed(KeyEvent event)
1353
1354     This handler is called when the VolumeDown key has been pressed. The \a event
1355     parameter provides information about the event.
1356 */
1357
1358 const QDeclarative1KeysAttached::SigMap QDeclarative1KeysAttached::sigMap[] = {
1359     { Qt::Key_Left, "leftPressed" },
1360     { Qt::Key_Right, "rightPressed" },
1361     { Qt::Key_Up, "upPressed" },
1362     { Qt::Key_Down, "downPressed" },
1363     { Qt::Key_Tab, "tabPressed" },
1364     { Qt::Key_Backtab, "backtabPressed" },
1365     { Qt::Key_Asterisk, "asteriskPressed" },
1366     { Qt::Key_NumberSign, "numberSignPressed" },
1367     { Qt::Key_Escape, "escapePressed" },
1368     { Qt::Key_Return, "returnPressed" },
1369     { Qt::Key_Enter, "enterPressed" },
1370     { Qt::Key_Delete, "deletePressed" },
1371     { Qt::Key_Space, "spacePressed" },
1372     { Qt::Key_Back, "backPressed" },
1373     { Qt::Key_Cancel, "cancelPressed" },
1374     { Qt::Key_Select, "selectPressed" },
1375     { Qt::Key_Yes, "yesPressed" },
1376     { Qt::Key_No, "noPressed" },
1377     { Qt::Key_Context1, "context1Pressed" },
1378     { Qt::Key_Context2, "context2Pressed" },
1379     { Qt::Key_Context3, "context3Pressed" },
1380     { Qt::Key_Context4, "context4Pressed" },
1381     { Qt::Key_Call, "callPressed" },
1382     { Qt::Key_Hangup, "hangupPressed" },
1383     { Qt::Key_Flip, "flipPressed" },
1384     { Qt::Key_Menu, "menuPressed" },
1385     { Qt::Key_VolumeUp, "volumeUpPressed" },
1386     { Qt::Key_VolumeDown, "volumeDownPressed" },
1387     { 0, 0 }
1388 };
1389
1390 bool QDeclarative1KeysAttachedPrivate::isConnected(const char *signalName)
1391 {
1392     return isSignalConnected(signalIndex(signalName));
1393 }
1394
1395 QDeclarative1KeysAttached::QDeclarative1KeysAttached(QObject *parent)
1396 : QObject(*(new QDeclarative1KeysAttachedPrivate), parent),
1397   QDeclarativeItemKeyFilter(qobject_cast<QDeclarativeItem*>(parent))
1398 {
1399     Q_D(QDeclarative1KeysAttached);
1400     m_processPost = false;
1401     d->item = qobject_cast<QDeclarativeItem*>(parent);
1402 }
1403
1404 QDeclarative1KeysAttached::~QDeclarative1KeysAttached()
1405 {
1406 }
1407
1408 QDeclarative1KeysAttached::Priority QDeclarative1KeysAttached::priority() const
1409 {
1410     return m_processPost ? AfterItem : BeforeItem;
1411 }
1412
1413 void QDeclarative1KeysAttached::setPriority(Priority order)
1414 {
1415     bool processPost = order == AfterItem;
1416     if (processPost != m_processPost) {
1417         m_processPost = processPost;
1418         emit priorityChanged();
1419     }
1420 }
1421
1422 void QDeclarative1KeysAttached::componentComplete()
1423 {
1424     Q_D(QDeclarative1KeysAttached);
1425     if (d->item) {
1426         for (int ii = 0; ii < d->targets.count(); ++ii) {
1427             QGraphicsItem *targetItem = d->finalFocusProxy(d->targets.at(ii));
1428             if (targetItem && (targetItem->flags() & QGraphicsItem::ItemAcceptsInputMethod)) {
1429                 d->item->setFlag(QGraphicsItem::ItemAcceptsInputMethod);
1430                 break;
1431             }
1432         }
1433     }
1434 }
1435
1436 void QDeclarative1KeysAttached::keyPressed(QKeyEvent *event, bool post)
1437 {
1438     Q_D(QDeclarative1KeysAttached);
1439     if (post != m_processPost || !d->enabled || d->inPress) {
1440         event->ignore();
1441         QDeclarativeItemKeyFilter::keyPressed(event, post);
1442         return;
1443     }
1444
1445     // first process forwards
1446     if (d->item && d->item->scene()) {
1447         d->inPress = true;
1448         for (int ii = 0; ii < d->targets.count(); ++ii) {
1449             QGraphicsItem *i = d->finalFocusProxy(d->targets.at(ii));
1450             if (i && i->isVisible()) {
1451                 d->item->scene()->sendEvent(i, event);
1452                 if (event->isAccepted()) {
1453                     d->inPress = false;
1454                     return;
1455                 }
1456             }
1457         }
1458         d->inPress = false;
1459     }
1460
1461     QDeclarative1KeyEvent ke(*event);
1462     QByteArray keySignal = keyToSignal(event->key());
1463     if (!keySignal.isEmpty()) {
1464         keySignal += "(QDeclarative1KeyEvent*)";
1465         if (d->isConnected(keySignal)) {
1466             // If we specifically handle a key then default to accepted
1467             ke.setAccepted(true);
1468             int idx = QDeclarative1KeysAttached::staticMetaObject.indexOfSignal(keySignal);
1469             metaObject()->method(idx).invoke(this, Qt::DirectConnection, Q_ARG(QDeclarative1KeyEvent*, &ke));
1470         }
1471     }
1472     if (!ke.isAccepted())
1473         emit pressed(&ke);
1474     event->setAccepted(ke.isAccepted());
1475
1476     if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyPressed(event, post);
1477 }
1478
1479 void QDeclarative1KeysAttached::keyReleased(QKeyEvent *event, bool post)
1480 {
1481     Q_D(QDeclarative1KeysAttached);
1482     if (post != m_processPost || !d->enabled || d->inRelease) {
1483         event->ignore();
1484         QDeclarativeItemKeyFilter::keyReleased(event, post);
1485         return;
1486     }
1487
1488     if (d->item && d->item->scene()) {
1489         d->inRelease = true;
1490         for (int ii = 0; ii < d->targets.count(); ++ii) {
1491             QGraphicsItem *i = d->finalFocusProxy(d->targets.at(ii));
1492             if (i && i->isVisible()) {
1493                 d->item->scene()->sendEvent(i, event);
1494                 if (event->isAccepted()) {
1495                     d->inRelease = false;
1496                     return;
1497                 }
1498             }
1499         }
1500         d->inRelease = false;
1501     }
1502
1503     QDeclarative1KeyEvent ke(*event);
1504     emit released(&ke);
1505     event->setAccepted(ke.isAccepted());
1506
1507     if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyReleased(event, post);
1508 }
1509
1510 void QDeclarative1KeysAttached::inputMethodEvent(QInputMethodEvent *event, bool post)
1511 {
1512     Q_D(QDeclarative1KeysAttached);
1513     if (post == m_processPost && d->item && !d->inIM && d->item->scene()) {
1514         d->inIM = true;
1515         for (int ii = 0; ii < d->targets.count(); ++ii) {
1516             QGraphicsItem *i = d->finalFocusProxy(d->targets.at(ii));
1517             if (i && i->isVisible() && (i->flags() & QGraphicsItem::ItemAcceptsInputMethod)) {
1518                 d->item->scene()->sendEvent(i, event);
1519                 if (event->isAccepted()) {
1520                     d->imeItem = i;
1521                     d->inIM = false;
1522                     return;
1523                 }
1524             }
1525         }
1526         d->inIM = false;
1527     }
1528     if (!event->isAccepted()) QDeclarativeItemKeyFilter::inputMethodEvent(event, post);
1529 }
1530
1531 class QDeclarativeItemAccessor : public QGraphicsItem
1532 {
1533 public:
1534     QVariant doInputMethodQuery(Qt::InputMethodQuery query) const {
1535         return QGraphicsItem::inputMethodQuery(query);
1536     }
1537 };
1538
1539 QVariant QDeclarative1KeysAttached::inputMethodQuery(Qt::InputMethodQuery query) const
1540 {
1541     Q_D(const QDeclarative1KeysAttached);
1542     if (d->item) {
1543         for (int ii = 0; ii < d->targets.count(); ++ii) {
1544                 QGraphicsItem *i = d->finalFocusProxy(d->targets.at(ii));
1545             if (i && i->isVisible() && (i->flags() & QGraphicsItem::ItemAcceptsInputMethod) && i == d->imeItem) { //### how robust is i == d->imeItem check?
1546                 QVariant v = static_cast<QDeclarativeItemAccessor *>(i)->doInputMethodQuery(query);
1547                 if (v.userType() == QVariant::RectF)
1548                     v = d->item->mapRectFromItem(i, v.toRectF());  //### cost?
1549                 return v;
1550             }
1551         }
1552     }
1553     return QDeclarativeItemKeyFilter::inputMethodQuery(query);
1554 }
1555
1556 QDeclarative1KeysAttached *QDeclarative1KeysAttached::qmlAttachedProperties(QObject *obj)
1557 {
1558     return new QDeclarative1KeysAttached(obj);
1559 }
1560
1561 /*!
1562     \class QDeclarativeItem
1563     \since QtQuick 1.0
1564     \brief The QDeclarativeItem class provides the most basic of all visual items in QML.
1565
1566     All visual items in Qt Declarative inherit from QDeclarativeItem.  Although QDeclarativeItem
1567     has no visual appearance, it defines all the properties that are
1568     common across visual items - such as the x and y position, the
1569     width and height, \l {anchor-layout}{anchoring} and key handling.
1570
1571     You can subclass QDeclarativeItem to provide your own custom visual item that inherits
1572     these features. Note that, because it does not draw anything, QDeclarativeItem sets the
1573     QGraphicsItem::ItemHasNoContents flag. If you subclass QDeclarativeItem to create a visual
1574     item, you will need to unset this flag.
1575
1576 */
1577
1578 /*!
1579     \qmlclass Item QDeclarativeItem
1580     \inqmlmodule QtQuick 1
1581     \ingroup qml-basic-visual-elements
1582     \since QtQuick 1.0
1583     \brief The Item is the most basic of all visual items in QML.
1584
1585     All visual items in Qt Declarative inherit from Item.  Although Item
1586     has no visual appearance, it defines all the properties that are
1587     common across visual items - such as the x and y position, the
1588     width and height, \l {anchor-layout}{anchoring} and key handling.
1589
1590     Item is also useful for grouping items together.
1591
1592     \qml
1593     Item {
1594         Image {
1595             source: "tile.png"
1596         }
1597         Image {
1598             x: 80
1599             width: 100
1600             height: 100
1601             source: "tile.png"
1602         }
1603         Image {
1604             x: 190
1605             width: 100
1606             height: 100
1607             fillMode: Image.Tile
1608             source: "tile.png"
1609         }
1610     }
1611     \endqml
1612
1613
1614     \section1 Key Handling
1615
1616     Key handling is available to all Item-based visual elements via the \l {Keys}{Keys}
1617     attached property.  The \e Keys attached property provides basic handlers such
1618     as \l {Keys::onPressed}{onPressed} and \l {Keys::onReleased}{onReleased},
1619     as well as handlers for specific keys, such as
1620     \l {Keys::onCancelPressed}{onCancelPressed}.  The example below
1621     assigns \l {qmlfocus}{focus} to the item and handles
1622     the Left key via the general \e onPressed handler and the Select key via the
1623     onSelectPressed handler:
1624
1625     \qml
1626     Item {
1627         focus: true
1628         Keys.onPressed: {
1629             if (event.key == Qt.Key_Left) {
1630                 console.log("move left");
1631                 event.accepted = true;
1632             }
1633         }
1634         Keys.onSelectPressed: console.log("Selected");
1635     }
1636     \endqml
1637
1638     See the \l {Keys}{Keys} attached property for detailed documentation.
1639
1640     \section1 Layout Mirroring
1641
1642     Item layouts can be mirrored using the \l {LayoutMirroring}{LayoutMirroring} attached property.
1643
1644 */
1645
1646 /*!
1647     \fn void QDeclarativeItem::childrenRectChanged(const QRectF &)
1648     \internal
1649 */
1650
1651 /*!
1652     \fn void QDeclarativeItem::baselineOffsetChanged(qreal)
1653     \internal
1654 */
1655
1656 /*!
1657     \fn void QDeclarativeItem::stateChanged(const QString &state)
1658     \internal
1659 */
1660
1661 /*!
1662     \fn void QDeclarativeItem::parentChanged(QDeclarativeItem *)
1663     \internal
1664 */
1665
1666 /*!
1667     \fn void QDeclarativeItem::smoothChanged(bool)
1668     \internal
1669 */
1670
1671 /*!
1672     \fn void QDeclarativeItem::clipChanged(bool)
1673     \internal
1674 */
1675
1676 /*! \fn void QDeclarativeItem::transformOriginChanged(TransformOrigin)
1677   \internal
1678 */
1679
1680 /*!
1681     \fn void QDeclarativeItem::focusChanged(bool)
1682     \internal
1683 */
1684
1685 /*!
1686     \fn void QDeclarativeItem::activeFocusChanged(bool)
1687     \internal
1688 */
1689
1690 // ### Must fix
1691 namespace {
1692 struct RegisterAnchorLineAtStartup {
1693     RegisterAnchorLineAtStartup() {
1694         qRegisterMetaType<QDeclarative1AnchorLine>("QDeclarative1AnchorLine");
1695     }
1696 };
1697 static RegisterAnchorLineAtStartup registerAnchorLineAtStartup;
1698 }
1699
1700
1701 /*!
1702     \fn QDeclarativeItem::QDeclarativeItem(QDeclarativeItem *parent)
1703
1704     Constructs a QDeclarativeItem with the given \a parent.
1705 */
1706 QDeclarativeItem::QDeclarativeItem(QDeclarativeItem* parent)
1707   : QGraphicsObject(*(new QDeclarativeItemPrivate), parent, 0)
1708 {
1709     Q_D(QDeclarativeItem);
1710     d->init(parent);
1711 }
1712
1713 /*! \internal
1714 */
1715 QDeclarativeItem::QDeclarativeItem(QDeclarativeItemPrivate &dd, QDeclarativeItem *parent)
1716   : QGraphicsObject(dd, parent, 0)
1717 {
1718     Q_D(QDeclarativeItem);
1719     d->init(parent);
1720 }
1721
1722 /*!
1723     Destroys the QDeclarativeItem.
1724 */
1725 QDeclarativeItem::~QDeclarativeItem()
1726 {
1727     Q_D(QDeclarativeItem);
1728     for (int ii = 0; ii < d->changeListeners.count(); ++ii) {
1729         QDeclarative1AnchorsPrivate *anchor = d->changeListeners.at(ii).listener->anchorPrivate();
1730         if (anchor)
1731             anchor->clearItem(this);
1732     }
1733     if (!d->parent || (parentItem() && !parentItem()->QGraphicsItem::d_ptr->inDestructor)) {
1734         for (int ii = 0; ii < d->changeListeners.count(); ++ii) {
1735             QDeclarative1AnchorsPrivate *anchor = d->changeListeners.at(ii).listener->anchorPrivate();
1736             if (anchor && anchor->item && anchor->item->parentItem() != this) //child will be deleted anyway
1737                 anchor->updateOnComplete();
1738         }
1739     }
1740     for(int ii = 0; ii < d->changeListeners.count(); ++ii) {
1741         const QDeclarativeItemPrivate::ChangeListener &change = d->changeListeners.at(ii);
1742         if (change.types & QDeclarativeItemPrivate::Destroyed)
1743             change.listener->itemDestroyed(this);
1744     }
1745     d->changeListeners.clear();
1746     delete d->_anchorLines; d->_anchorLines = 0;
1747     delete d->_anchors; d->_anchors = 0;
1748     delete d->_stateGroup; d->_stateGroup = 0;
1749     delete d->_contents; d->_contents = 0;
1750 }
1751
1752 /*!
1753     \qmlproperty enumeration QtQuick1::Item::transformOrigin
1754     This property holds the origin point around which scale and rotation transform.
1755
1756     Nine transform origins are available, as shown in the image below.
1757
1758     \image declarative-transformorigin.png
1759
1760     This example rotates an image around its bottom-right corner.
1761     \qml
1762     Image {
1763         source: "myimage.png"
1764         transformOrigin: Item.BottomRight
1765         rotation: 45
1766     }
1767     \endqml
1768
1769     The default transform origin is \c Item.Center.
1770
1771     To set an arbitrary transform origin point use the \l Scale or \l Rotation
1772     transform elements.
1773 */
1774
1775 /*!
1776     \qmlproperty Item QtQuick1::Item::parent
1777     This property holds the parent of the item.
1778 */
1779
1780 /*!
1781     \property QDeclarativeItem::parent
1782     This property holds the parent of the item.
1783 */
1784 void QDeclarativeItem::setParentItem(QDeclarativeItem *parent)
1785 {
1786     Q_D(QDeclarativeItem);
1787     QGraphicsObject::setParentItem(parent);
1788     if (d->isAccessible && parentItem()) {
1789         parentItem()->d_func()->setAccessibleFlagAndListener();
1790     }
1791 }
1792
1793 /*!
1794     Returns the QDeclarativeItem parent of this item.
1795 */
1796 QDeclarativeItem *QDeclarativeItem::parentItem() const
1797 {
1798     return qobject_cast<QDeclarativeItem *>(QGraphicsObject::parentItem());
1799 }
1800
1801 /*!
1802     \qmlproperty real QtQuick1::Item::childrenRect.x
1803     \qmlproperty real QtQuick1::Item::childrenRect.y
1804     \qmlproperty real QtQuick1::Item::childrenRect.width
1805     \qmlproperty real QtQuick1::Item::childrenRect.height
1806
1807     The childrenRect properties allow an item access to the geometry of its
1808     children. This property is useful if you have an item that needs to be
1809     sized to fit its children.
1810 */
1811
1812
1813 /*!
1814     \qmlproperty list<Item> QtQuick1::Item::children
1815     \qmlproperty list<Object> QtQuick1::Item::resources
1816
1817     The children property contains the list of visual children of this item.
1818     The resources property contains non-visual resources that you want to
1819     reference by name.
1820
1821     Generally you can rely on Item's default property to handle all this for
1822     you, but it can come in handy in some cases.
1823
1824     \qml
1825     Item {
1826         children: [
1827             Text {},
1828             Rectangle {}
1829         ]
1830         resources: [
1831             Component {
1832                 id: myComponent
1833                 Text {}
1834             }
1835         ]
1836     }
1837     \endqml
1838 */
1839
1840 /*!
1841     Returns true if construction of the QML component is complete; otherwise
1842     returns false.
1843
1844     It is often desirable to delay some processing until the component is
1845     completed.
1846
1847     \sa componentComplete()
1848 */
1849 bool QDeclarativeItem::isComponentComplete() const
1850 {
1851     Q_D(const QDeclarativeItem);
1852     return d->componentComplete;
1853 }
1854
1855 void QDeclarativeItemPrivate::data_append(QDeclarativeListProperty<QObject> *prop, QObject *o)
1856 {
1857     if (!o)
1858         return;
1859
1860     QDeclarativeItem *that = static_cast<QDeclarativeItem *>(prop->object);
1861
1862     // This test is measurably (albeit only slightly) faster than qobject_cast<>()
1863     const QMetaObject *mo = o->metaObject();
1864     while (mo && mo != &QGraphicsObject::staticMetaObject) mo = mo->d.superdata;
1865
1866     if (mo) {
1867         QGraphicsObject *graphicsObject = static_cast<QGraphicsObject *>(o);
1868         QDeclarativeItemPrivate *contentItemPrivate = static_cast<QDeclarativeItemPrivate *>(QGraphicsItemPrivate::get(graphicsObject));
1869         if (contentItemPrivate->componentComplete) {
1870             graphicsObject->setParentItem(that);
1871         } else {
1872             contentItemPrivate->setParentItemHelper(that, /*newParentVariant=*/0, /*thisPointerVariant=*/0);
1873         }
1874     } else {
1875         o->setParent(that);
1876     }
1877 }
1878
1879 static inline int children_count_helper(QDeclarativeListProperty<QObject> *prop)
1880 {
1881     QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast<QGraphicsObject *>(prop->object));
1882     return d->children.count();
1883 }
1884
1885 static inline QObject *children_at_helper(QDeclarativeListProperty<QObject> *prop, int index)
1886 {
1887     QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast<QGraphicsObject *>(prop->object));
1888     if (index >= 0 && index < d->children.count())
1889         return d->children.at(index)->toGraphicsObject();
1890     else
1891         return 0;
1892 }
1893
1894 static inline void children_clear_helper(QDeclarativeListProperty<QObject> *prop)
1895 {
1896     QDeclarativeItemPrivate *d = static_cast<QDeclarativeItemPrivate*>(QGraphicsItemPrivate::get(static_cast<QGraphicsObject *>(prop->object)));
1897     int childCount = d->children.count();
1898     if (d->componentComplete) {
1899         for (int index = 0 ;index < childCount; index++)
1900             d->children.at(0)->setParentItem(0);
1901     } else {
1902         for (int index = 0 ;index < childCount; index++)
1903             QGraphicsItemPrivate::get(d->children.at(0))->setParentItemHelper(0, /*newParentVariant=*/0, /*thisPointerVariant=*/0);
1904     }
1905 }
1906
1907 int QDeclarativeItemPrivate::data_count(QDeclarativeListProperty<QObject> *prop)
1908 {
1909     return resources_count(prop) + children_count_helper(prop);
1910 }
1911
1912 QObject *QDeclarativeItemPrivate::data_at(QDeclarativeListProperty<QObject> *prop, int i)
1913 {
1914     int resourcesCount = resources_count(prop);
1915     if (i < resourcesCount)
1916         return resources_at(prop, i);
1917     const int j = i - resourcesCount;
1918     if (j < children_count_helper(prop))
1919         return children_at_helper(prop, j);
1920     return 0;
1921 }
1922
1923 void QDeclarativeItemPrivate::data_clear(QDeclarativeListProperty<QObject> *prop)
1924 {
1925     resources_clear(prop);
1926     children_clear_helper(prop);
1927 }
1928
1929 QObject *QDeclarativeItemPrivate::resources_at(QDeclarativeListProperty<QObject> *prop, int index)
1930 {
1931     const QObjectList children = prop->object->children();
1932     if (index < children.count())
1933         return children.at(index);
1934     else
1935         return 0;
1936 }
1937
1938 void QDeclarativeItemPrivate::resources_append(QDeclarativeListProperty<QObject> *prop, QObject *o)
1939 {
1940     o->setParent(prop->object);
1941 }
1942
1943 int QDeclarativeItemPrivate::resources_count(QDeclarativeListProperty<QObject> *prop)
1944 {
1945     return prop->object->children().count();
1946 }
1947
1948 void QDeclarativeItemPrivate::resources_clear(QDeclarativeListProperty<QObject> *prop)
1949 {
1950     const QObjectList children = prop->object->children();
1951     for (int index = 0; index < children.count(); index++)
1952         children.at(index)->setParent(0);
1953 }
1954
1955 int QDeclarativeItemPrivate::transform_count(QDeclarativeListProperty<QGraphicsTransform> *list)
1956 {
1957     QGraphicsObject *object = qobject_cast<QGraphicsObject *>(list->object);
1958     if (object) {
1959         QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(object);
1960         return d->transformData ? d->transformData->graphicsTransforms.size() : 0;
1961     } else {
1962         return 0;
1963     }
1964 }
1965
1966 void QDeclarativeItemPrivate::transform_append(QDeclarativeListProperty<QGraphicsTransform> *list, QGraphicsTransform *item)
1967 {
1968     QGraphicsObject *object = qobject_cast<QGraphicsObject *>(list->object);
1969     if (object && item) // QGraphicsItem applies the list in the wrong order, so we prepend.
1970         QGraphicsItemPrivate::get(object)->prependGraphicsTransform(item);
1971 }
1972
1973 QGraphicsTransform *QDeclarativeItemPrivate::transform_at(QDeclarativeListProperty<QGraphicsTransform> *list, int idx)
1974 {
1975     QGraphicsObject *object = qobject_cast<QGraphicsObject *>(list->object);
1976     if (object) {
1977         QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(object);
1978         if (!d->transformData)
1979             return 0;
1980         return d->transformData->graphicsTransforms.at(idx);
1981     } else {
1982         return 0;
1983     }
1984 }
1985
1986 void QDeclarativeItemPrivate::transform_clear(QDeclarativeListProperty<QGraphicsTransform> *list)
1987 {
1988     QGraphicsObject *object = qobject_cast<QGraphicsObject *>(list->object);
1989     if (object) {
1990         QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(object);
1991         if (!d->transformData)
1992             return;
1993         object->setTransformations(QList<QGraphicsTransform *>());
1994     }
1995 }
1996
1997 /*!
1998     \qmlproperty list<Object> QtQuick1::Item::data
1999     \default
2000
2001     The data property allows you to freely mix visual children and resources
2002     in an item.  If you assign a visual item to the data list it becomes
2003     a child and if you assign any other object type, it is added as a resource.
2004
2005     So you can write:
2006     \qml
2007     Item {
2008         Text {}
2009         Rectangle {}
2010         Timer {}
2011     }
2012     \endqml
2013
2014     instead of:
2015     \qml
2016     Item {
2017         children: [
2018             Text {},
2019             Rectangle {}
2020         ]
2021         resources: [
2022             Timer {}
2023         ]
2024     }
2025     \endqml
2026
2027     data is a behind-the-scenes property: you should never need to explicitly
2028     specify it.
2029  */
2030
2031 QDeclarativeListProperty<QObject> QDeclarativeItemPrivate::data()
2032 {
2033     return QDeclarativeListProperty<QObject>(q_func(), 0, QDeclarativeItemPrivate::data_append,
2034                                              QDeclarativeItemPrivate::data_count,
2035                                              QDeclarativeItemPrivate::data_at,
2036                                              QDeclarativeItemPrivate::data_clear
2037                                              );
2038 }
2039
2040 /*!
2041     \property QDeclarativeItem::childrenRect
2042     \brief The geometry of an item's children.
2043
2044     This property holds the (collective) position and size of the item's children.
2045 */
2046 QRectF QDeclarativeItem::childrenRect()
2047 {
2048     Q_D(QDeclarativeItem);
2049     if (!d->_contents) {
2050         d->_contents = new QDeclarative1Contents(this);
2051         if (d->componentComplete)
2052             d->_contents->complete();
2053     }
2054     return d->_contents->rectF();
2055 }
2056
2057 bool QDeclarativeItem::clip() const
2058 {
2059     return flags() & ItemClipsChildrenToShape;
2060 }
2061
2062 void QDeclarativeItem::setClip(bool c)
2063 {
2064     if (clip() == c)
2065         return;
2066     setFlag(ItemClipsChildrenToShape, c);
2067     emit clipChanged(c);
2068 }
2069
2070 /*!
2071   \qmlproperty real QtQuick1::Item::x
2072   \qmlproperty real QtQuick1::Item::y
2073   \qmlproperty real QtQuick1::Item::width
2074   \qmlproperty real QtQuick1::Item::height
2075
2076   Defines the item's position and size relative to its parent.
2077
2078   \qml
2079   Item { x: 100; y: 100; width: 100; height: 100 }
2080   \endqml
2081  */
2082
2083 /*!
2084   \qmlproperty real QtQuick1::Item::z
2085
2086   Sets the stacking order of sibling items.  By default the stacking order is 0.
2087
2088   Items with a higher stacking value are drawn on top of siblings with a
2089   lower stacking order.  Items with the same stacking value are drawn
2090   bottom up in the order they appear.  Items with a negative stacking
2091   value are drawn under their parent's content.
2092
2093   The following example shows the various effects of stacking order.
2094
2095   \table
2096   \row
2097   \o \image declarative-item_stacking1.png
2098   \o Same \c z - later children above earlier children:
2099   \qml
2100   Item {
2101       Rectangle {
2102           color: "red"
2103           width: 100; height: 100
2104       }
2105       Rectangle {
2106           color: "blue"
2107           x: 50; y: 50; width: 100; height: 100
2108       }
2109   }
2110   \endqml
2111   \row
2112   \o \image declarative-item_stacking2.png
2113   \o Higher \c z on top:
2114   \qml
2115   Item {
2116       Rectangle {
2117           z: 1
2118           color: "red"
2119           width: 100; height: 100
2120       }
2121       Rectangle {
2122           color: "blue"
2123           x: 50; y: 50; width: 100; height: 100
2124       }
2125   }
2126   \endqml
2127   \row
2128   \o \image declarative-item_stacking3.png
2129   \o Same \c z - children above parents:
2130   \qml
2131   Item {
2132       Rectangle {
2133           color: "red"
2134           width: 100; height: 100
2135           Rectangle {
2136               color: "blue"
2137               x: 50; y: 50; width: 100; height: 100
2138           }
2139       }
2140   }
2141   \endqml
2142   \row
2143   \o \image declarative-item_stacking4.png
2144   \o Lower \c z below:
2145   \qml
2146   Item {
2147       Rectangle {
2148           color: "red"
2149           width: 100; height: 100
2150           Rectangle {
2151               z: -1
2152               color: "blue"
2153               x: 50; y: 50; width: 100; height: 100
2154           }
2155       }
2156   }
2157   \endqml
2158   \endtable
2159  */
2160
2161 /*!
2162     \qmlproperty bool QtQuick1::Item::visible
2163
2164     This property holds whether the item is visible. By default this is true.
2165
2166     Setting this property directly affects the \c visible value of child
2167     items. When set to \c false, the \c visible values of all child items also
2168     become \c false. When set to \c true, the \c visible values of child items
2169     are returned to \c true, unless they have explicitly been set to \c false.
2170
2171     (Because of this flow-on behavior, using the \c visible property may not
2172     have the intended effect if a property binding should only respond to
2173     explicit property changes. In such cases it may be better to use the
2174     \l opacity property instead.)
2175
2176     Setting this property to \c false automatically causes \l focus to be set
2177     to \c false, and this item will longer receive mouse and keyboard events.
2178     (In contrast, setting the \l opacity to 0 does not affect the \l focus
2179     property and the receiving of key events.)
2180
2181     \note This property's value is only affected by changes to this property or
2182     the parent's \c visible property. It does not change, for example, if this
2183     item moves off-screen, or if the \l opacity changes to 0.
2184 */
2185
2186
2187 /*!
2188   This function is called to handle this item's changes in
2189   geometry from \a oldGeometry to \a newGeometry. If the two
2190   geometries are the same, it doesn't do anything.
2191  */
2192 void QDeclarativeItem::geometryChanged(const QRectF &newGeometry,
2193                               const QRectF &oldGeometry)
2194 {
2195     Q_D(QDeclarativeItem);
2196
2197     if (d->_anchors)
2198         d->_anchors->d_func()->updateMe();
2199
2200     if (transformOrigin() != QDeclarativeItem::TopLeft
2201         && (newGeometry.width() != oldGeometry.width() || newGeometry.height() != oldGeometry.height())) {
2202         if (d->transformData) {
2203             QPointF origin = d->computeTransformOrigin();
2204             if (transformOriginPoint() != origin)
2205                 setTransformOriginPoint(origin);
2206         } else {
2207             d->transformOriginDirty = true;
2208         }
2209     }
2210
2211     for(int ii = 0; ii < d->changeListeners.count(); ++ii) {
2212         const QDeclarativeItemPrivate::ChangeListener &change = d->changeListeners.at(ii);
2213         if (change.types & QDeclarativeItemPrivate::Geometry)
2214             change.listener->itemGeometryChanged(this, newGeometry, oldGeometry);
2215     }
2216
2217     if (newGeometry.width() != oldGeometry.width())
2218         emit widthChanged();
2219     if (newGeometry.height() != oldGeometry.height())
2220         emit heightChanged();
2221 }
2222
2223 void QDeclarativeItemPrivate::removeItemChangeListener(QDeclarativeItemChangeListener *listener, ChangeTypes types)
2224 {
2225     ChangeListener change(listener, types);
2226     changeListeners.removeOne(change);
2227 }
2228
2229 /*! \internal */
2230 void QDeclarativeItem::keyPressEvent(QKeyEvent *event)
2231 {
2232     Q_D(QDeclarativeItem);
2233     keyPressPreHandler(event);
2234     if (event->isAccepted())
2235         return;
2236     if (d->keyHandler)
2237         d->keyHandler->keyPressed(event, true);
2238     else
2239         event->ignore();
2240 }
2241
2242 /*! \internal */
2243 void QDeclarativeItem::keyReleaseEvent(QKeyEvent *event)
2244 {
2245     Q_D(QDeclarativeItem);
2246     keyReleasePreHandler(event);
2247     if (event->isAccepted())
2248         return;
2249     if (d->keyHandler)
2250         d->keyHandler->keyReleased(event, true);
2251     else
2252         event->ignore();
2253 }
2254
2255 /*! \internal */
2256 void QDeclarativeItem::inputMethodEvent(QInputMethodEvent *event)
2257 {
2258     Q_D(QDeclarativeItem);
2259     inputMethodPreHandler(event);
2260     if (event->isAccepted())
2261         return;
2262     if (d->keyHandler)
2263         d->keyHandler->inputMethodEvent(event, true);
2264     else
2265         event->ignore();
2266 }
2267
2268 /*! \internal */
2269 QVariant QDeclarativeItem::inputMethodQuery(Qt::InputMethodQuery query) const
2270 {
2271     Q_D(const QDeclarativeItem);
2272     QVariant v;
2273     if (d->keyHandler)
2274         v = d->keyHandler->inputMethodQuery(query);
2275
2276     if (!v.isValid())
2277         v = QGraphicsObject::inputMethodQuery(query);
2278
2279     return v;
2280 }
2281
2282 /*!
2283   \internal
2284  */
2285 void QDeclarativeItem::keyPressPreHandler(QKeyEvent *event)
2286 {
2287     Q_D(QDeclarativeItem);
2288     if (d->keyHandler && !d->doneEventPreHandler)
2289         d->keyHandler->keyPressed(event, false);
2290     else
2291         event->ignore();
2292     d->doneEventPreHandler = true;
2293 }
2294
2295 /*!
2296   \internal
2297  */
2298 void QDeclarativeItem::keyReleasePreHandler(QKeyEvent *event)
2299 {
2300     Q_D(QDeclarativeItem);
2301     if (d->keyHandler && !d->doneEventPreHandler)
2302         d->keyHandler->keyReleased(event, false);
2303     else
2304         event->ignore();
2305     d->doneEventPreHandler = true;
2306 }
2307
2308 /*!
2309   \internal
2310  */
2311 void QDeclarativeItem::inputMethodPreHandler(QInputMethodEvent *event)
2312 {
2313     Q_D(QDeclarativeItem);
2314     if (d->keyHandler && !d->doneEventPreHandler)
2315         d->keyHandler->inputMethodEvent(event, false);
2316     else
2317         event->ignore();
2318     d->doneEventPreHandler = true;
2319 }
2320
2321 /*!
2322     \internal
2323 */
2324 QDeclarative1AnchorLine QDeclarativeItemPrivate::left() const
2325 {
2326     return anchorLines()->left;
2327 }
2328
2329 /*!
2330     \internal
2331 */
2332 QDeclarative1AnchorLine QDeclarativeItemPrivate::right() const
2333 {
2334     return anchorLines()->right;
2335 }
2336
2337 /*!
2338     \internal
2339 */
2340 QDeclarative1AnchorLine QDeclarativeItemPrivate::horizontalCenter() const
2341 {
2342     return anchorLines()->hCenter;
2343 }
2344
2345 /*!
2346     \internal
2347 */
2348 QDeclarative1AnchorLine QDeclarativeItemPrivate::top() const
2349 {
2350     return anchorLines()->top;
2351 }
2352
2353 /*!
2354     \internal
2355 */
2356 QDeclarative1AnchorLine QDeclarativeItemPrivate::bottom() const
2357 {
2358     return anchorLines()->bottom;
2359 }
2360
2361 /*!
2362     \internal
2363 */
2364 QDeclarative1AnchorLine QDeclarativeItemPrivate::verticalCenter() const
2365 {
2366     return anchorLines()->vCenter;
2367 }
2368
2369
2370 /*!
2371     \internal
2372 */
2373 QDeclarative1AnchorLine QDeclarativeItemPrivate::baseline() const
2374 {
2375     return anchorLines()->baseline;
2376 }
2377
2378 /*!
2379   \qmlproperty AnchorLine QtQuick1::Item::anchors.top
2380   \qmlproperty AnchorLine QtQuick1::Item::anchors.bottom
2381   \qmlproperty AnchorLine QtQuick1::Item::anchors.left
2382   \qmlproperty AnchorLine QtQuick1::Item::anchors.right
2383   \qmlproperty AnchorLine QtQuick1::Item::anchors.horizontalCenter
2384   \qmlproperty AnchorLine QtQuick1::Item::anchors.verticalCenter
2385   \qmlproperty AnchorLine QtQuick1::Item::anchors.baseline
2386
2387   \qmlproperty Item QtQuick1::Item::anchors.fill
2388   \qmlproperty Item QtQuick1::Item::anchors.centerIn
2389
2390   \qmlproperty real QtQuick1::Item::anchors.margins
2391   \qmlproperty real QtQuick1::Item::anchors.topMargin
2392   \qmlproperty real QtQuick1::Item::anchors.bottomMargin
2393   \qmlproperty real QtQuick1::Item::anchors.leftMargin
2394   \qmlproperty real QtQuick1::Item::anchors.rightMargin
2395   \qmlproperty real QtQuick1::Item::anchors.horizontalCenterOffset
2396   \qmlproperty real QtQuick1::Item::anchors.verticalCenterOffset
2397   \qmlproperty real QtQuick1::Item::anchors.baselineOffset
2398
2399   \qmlproperty bool QtQuick1::Item::anchors.mirrored
2400
2401   Anchors provide a way to position an item by specifying its
2402   relationship with other items.
2403
2404   Margins apply to top, bottom, left, right, and fill anchors.
2405   The \c anchors.margins property can be used to set all of the various margins at once, to the same value.
2406   Note that margins are anchor-specific and are not applied if an item does not
2407   use anchors.
2408
2409   Offsets apply for horizontal center, vertical center, and baseline anchors.
2410
2411   \table
2412   \row
2413   \o \image declarative-anchors_example.png
2414   \o Text anchored to Image, horizontally centered and vertically below, with a margin.
2415   \qml
2416   Item {
2417       Image {
2418           id: pic
2419           // ...
2420       }
2421       Text {
2422           id: label
2423           anchors.horizontalCenter: pic.horizontalCenter
2424           anchors.top: pic.bottom
2425           anchors.topMargin: 5
2426           // ...
2427       }
2428   }
2429   \endqml
2430   \row
2431   \o \image declarative-anchors_example2.png
2432   \o
2433   Left of Text anchored to right of Image, with a margin. The y
2434   property of both defaults to 0.
2435
2436   \qml
2437   Item {
2438       Image {
2439           id: pic
2440           // ...
2441       }
2442       Text {
2443           id: label
2444           anchors.left: pic.right
2445           anchors.leftMargin: 5
2446           // ...
2447       }
2448   }
2449   \endqml
2450   \endtable
2451
2452   \c anchors.fill provides a convenient way for one item to have the
2453   same geometry as another item, and is equivalent to connecting all
2454   four directional anchors.
2455
2456   To clear an anchor value, set it to \c undefined.
2457
2458   \c anchors.mirrored returns true it the layout has been \l {LayoutMirroring}{mirrored}.
2459
2460   \note You can only anchor an item to siblings or a parent.
2461
2462   For more information see \l {anchor-layout}{Anchor Layouts}.
2463 */
2464
2465 /*!
2466   \property QDeclarativeItem::baselineOffset
2467   \brief The position of the item's baseline in local coordinates.
2468
2469   The baseline of a \l Text item is the imaginary line on which the text
2470   sits. Controls containing text usually set their baseline to the
2471   baseline of their text.
2472
2473   For non-text items, a default baseline offset of 0 is used.
2474 */
2475 qreal QDeclarativeItem::baselineOffset() const
2476 {
2477     Q_D(const QDeclarativeItem);
2478     if (!d->baselineOffset.isValid()) {
2479         return 0.0;
2480     } else
2481         return d->baselineOffset;
2482 }
2483
2484 void QDeclarativeItem::setBaselineOffset(qreal offset)
2485 {
2486     Q_D(QDeclarativeItem);
2487     if (offset == d->baselineOffset)
2488         return;
2489
2490     d->baselineOffset = offset;
2491
2492     for(int ii = 0; ii < d->changeListeners.count(); ++ii) {
2493         const QDeclarativeItemPrivate::ChangeListener &change = d->changeListeners.at(ii);
2494         if (change.types & QDeclarativeItemPrivate::Geometry) {
2495             QDeclarative1AnchorsPrivate *anchor = change.listener->anchorPrivate();
2496             if (anchor)
2497                 anchor->updateVerticalAnchors();
2498         }
2499     }
2500     emit baselineOffsetChanged(offset);
2501 }
2502
2503 /*!
2504   \qmlproperty real QtQuick1::Item::rotation
2505   This property holds the rotation of the item in degrees clockwise.
2506
2507   This specifies how many degrees to rotate the item around its transformOrigin.
2508   The default rotation is 0 degrees (i.e. not rotated at all).
2509
2510   \table
2511   \row
2512   \o \image declarative-rotation.png
2513   \o
2514   \qml
2515   Rectangle {
2516       color: "blue"
2517       width: 100; height: 100
2518       Rectangle {
2519           color: "red"
2520           x: 25; y: 25; width: 50; height: 50
2521           rotation: 30
2522       }
2523   }
2524   \endqml
2525   \endtable
2526
2527   \sa transform, Rotation
2528 */
2529
2530 /*!
2531   \qmlproperty real QtQuick1::Item::scale
2532   This property holds the scale of the item.
2533
2534   A scale of less than 1 means the item will be displayed smaller than
2535   normal, and a scale of greater than 1 means the item will be
2536   displayed larger than normal.  A negative scale means the item will
2537   be mirrored.
2538
2539   By default, items are displayed at a scale of 1 (i.e. at their
2540   normal size).
2541
2542   Scaling is from the item's transformOrigin.
2543
2544   \table
2545   \row
2546   \o \image declarative-scale.png
2547   \o
2548   \qml
2549   Rectangle {
2550       color: "blue"
2551       width: 100; height: 100
2552       Rectangle {
2553           color: "green"
2554           width: 25; height: 25
2555       }
2556       Rectangle {
2557           color: "red"
2558           x: 25; y: 25; width: 50; height: 50
2559           scale: 1.4
2560       }
2561   }
2562   \endqml
2563   \endtable
2564
2565   \sa transform, Scale
2566 */
2567
2568 /*!
2569   \qmlproperty real QtQuick1::Item::opacity
2570
2571   This property holds the opacity of the item.  Opacity is specified as a
2572   number between 0 (fully transparent) and 1 (fully opaque).  The default is 1.
2573
2574   When this property is set, the specified opacity is also applied
2575   individually to child items.  In almost all cases this is what you want,
2576   but in some cases it may produce undesired results. For example in the
2577   second set of rectangles below, the red rectangle has specified an opacity
2578   of 0.5, which affects the opacity of its blue child rectangle even though
2579   the child has not specified an opacity.
2580
2581   \table
2582   \row
2583   \o \image declarative-item_opacity1.png
2584   \o
2585   \qml
2586     Item {
2587         Rectangle {
2588             color: "red"
2589             width: 100; height: 100
2590             Rectangle {
2591                 color: "blue"
2592                 x: 50; y: 50; width: 100; height: 100
2593             }
2594         }
2595     }
2596   \endqml
2597   \row
2598   \o \image declarative-item_opacity2.png
2599   \o
2600   \qml
2601     Item {
2602         Rectangle {
2603             opacity: 0.5
2604             color: "red"
2605             width: 100; height: 100
2606             Rectangle {
2607                 color: "blue"
2608                 x: 50; y: 50; width: 100; height: 100
2609             }
2610         }
2611     }
2612   \endqml
2613   \endtable
2614
2615   If an item's opacity is set to 0, the item will no longer receive mouse
2616   events, but will continue to receive key events and will retain the keyboard
2617   \l focus if it has been set. (In contrast, setting the \l visible property
2618   to \c false stops both mouse and keyboard events, and also removes focus
2619   from the item.)
2620 */
2621
2622 /*!
2623   Returns a value indicating whether mouse input should
2624   remain with this item exclusively.
2625
2626   \sa setKeepMouseGrab()
2627  */
2628 bool QDeclarativeItem::keepMouseGrab() const
2629 {
2630     Q_D(const QDeclarativeItem);
2631     return d->keepMouse;
2632 }
2633
2634 /*!
2635   The flag indicating whether the mouse should remain
2636   with this item is set to \a keep.
2637
2638   This is useful for items that wish to grab and keep mouse
2639   interaction following a predefined gesture.  For example,
2640   an item that is interested in horizontal mouse movement
2641   may set keepMouseGrab to true once a threshold has been
2642   exceeded.  Once keepMouseGrab has been set to true, filtering
2643   items will not react to mouse events.
2644
2645   If the item does not indicate that it wishes to retain mouse grab,
2646   a filtering item may steal the grab. For example, Flickable may attempt
2647   to steal a mouse grab if it detects that the user has begun to
2648   move the viewport.
2649
2650   \sa keepMouseGrab()
2651  */
2652 void QDeclarativeItem::setKeepMouseGrab(bool keep)
2653 {
2654     Q_D(QDeclarativeItem);
2655     d->keepMouse = keep;
2656 }
2657
2658 /*!
2659     \qmlmethod object QtQuick1::Item::mapFromItem(Item item, real x, real y)
2660
2661     Maps the point (\a x, \a y), which is in \a item's coordinate system, to
2662     this item's coordinate system, and returns an object with \c x and \c y
2663     properties matching the mapped cooordinate.
2664
2665     If \a item is a \c null value, this maps the point from the coordinate
2666     system of the root QML view.
2667 */
2668 void QDeclarativeItem::mapFromItem(QDeclarativeV8Function *args) const
2669 {
2670     if (args->Length() != 0) {
2671         v8::Local<v8::Value> item = (*args)[0];
2672         QV8Engine *engine = args->engine();
2673
2674         QDeclarativeItem *itemObj = 0;
2675         if (!item->IsNull())
2676             itemObj = qobject_cast<QDeclarativeItem*>(engine->toQObject(item));
2677
2678         if (!itemObj && !item->IsNull()) {
2679             qmlInfo(this) << "mapFromItem() given argument \"" << engine->toString(item->ToString())
2680                           << "\" which is neither null nor an Item";
2681             return;
2682         }
2683
2684         v8::Local<v8::Object> rv = v8::Object::New();
2685         args->returnValue(rv);
2686
2687         qreal x = (args->Length() > 1)?(*args)[1]->NumberValue():0;
2688         qreal y = (args->Length() > 2)?(*args)[2]->NumberValue():0;
2689
2690         QPointF p = QGraphicsItem::mapFromItem(itemObj, x, y);
2691
2692         rv->Set(v8::String::New("x"), v8::Number::New(p.x()));
2693         rv->Set(v8::String::New("y"), v8::Number::New(p.y()));
2694     }
2695 }
2696
2697 /*!
2698     \qmlmethod object QtQuick1::Item::mapToItem(Item item, real x, real y)
2699
2700     Maps the point (\a x, \a y), which is in this item's coordinate system, to
2701     \a item's coordinate system, and returns an object with \c x and \c y
2702     properties matching the mapped cooordinate.
2703
2704     If \a item is a \c null value, this maps \a x and \a y to the coordinate
2705     system of the root QML view.
2706 */
2707 void QDeclarativeItem::mapToItem(QDeclarativeV8Function *args) const
2708 {
2709     if (args->Length() != 0) {
2710         v8::Local<v8::Value> item = (*args)[0];
2711         QV8Engine *engine = args->engine();
2712
2713         QDeclarativeItem *itemObj = 0;
2714         if (!item->IsNull())
2715             itemObj = qobject_cast<QDeclarativeItem*>(engine->toQObject(item));
2716
2717         if (!itemObj && !item->IsNull()) {
2718             qmlInfo(this) << "mapToItem() given argument \"" << engine->toString(item->ToString())
2719                           << "\" which is neither null nor an Item";
2720             return;
2721         }
2722
2723         v8::Local<v8::Object> rv = v8::Object::New();
2724         args->returnValue(rv);
2725
2726         qreal x = (args->Length() > 1)?(*args)[1]->NumberValue():0;
2727         qreal y = (args->Length() > 2)?(*args)[2]->NumberValue():0;
2728
2729         QPointF p = QGraphicsItem::mapToItem(itemObj, x, y);
2730
2731         rv->Set(v8::String::New("x"), v8::Number::New(p.x()));
2732         rv->Set(v8::String::New("y"), v8::Number::New(p.y()));
2733     }
2734 }
2735
2736 /*!
2737     \qmlmethod QtQuick1::Item::forceActiveFocus()
2738
2739     Forces active focus on the item.
2740
2741     This method sets focus on the item and makes sure that all the focus scopes
2742     higher in the object hierarchy are also given the focus.
2743 */
2744
2745 /*!
2746     Forces active focus on the item.
2747
2748     This method sets focus on the item and makes sure that all the focus scopes
2749     higher in the object hierarchy are also given the focus.
2750 */
2751 void QDeclarativeItem::forceActiveFocus()
2752 {
2753     setFocus(true);
2754     QGraphicsItem *parent = parentItem();
2755     while (parent) {
2756         if (parent->flags() & QGraphicsItem::ItemIsFocusScope)
2757             parent->setFocus(Qt::OtherFocusReason);
2758         parent = parent->parentItem();
2759     }
2760 }
2761
2762
2763 /*!
2764   \qmlmethod QtQuick1::Item::childAt(real x, real y)
2765
2766   Returns the visible child item at point (\a x, \a y), which is in this
2767   item's coordinate system, or \c null if there is no such item.
2768 */
2769
2770 /*!
2771   Returns the visible child item at point (\a x, \a y), which is in this
2772   item's coordinate system, or 0 if there is no such item.
2773 */
2774 QDeclarativeItem *QDeclarativeItem::childAt(qreal x, qreal y) const
2775 {
2776     const QList<QGraphicsItem *> children = childItems();
2777     for (int i = children.count()-1; i >= 0; --i) {
2778         if (QDeclarativeItem *child = qobject_cast<QDeclarativeItem *>(children.at(i))) {
2779             if (child->isVisible() && child->x() <= x
2780                 && child->x() + child->width() >= x
2781                 && child->y() <= y
2782                 && child->y() + child->height() >= y)
2783                 return child;
2784         }
2785     }
2786     return 0;
2787 }
2788
2789 void QDeclarativeItemPrivate::focusChanged(bool flag)
2790 {
2791     Q_Q(QDeclarativeItem);
2792     if (!(flags & QGraphicsItem::ItemIsFocusScope) && parent)
2793         emit q->activeFocusChanged(flag);   //see also QDeclarativeItemPrivate::subFocusItemChange()
2794     emit q->focusChanged(flag);
2795 }
2796
2797 QDeclarativeListProperty<QObject> QDeclarativeItemPrivate::resources()
2798 {
2799     return QDeclarativeListProperty<QObject>(q_func(), 0, QDeclarativeItemPrivate::resources_append,
2800                                              QDeclarativeItemPrivate::resources_count,
2801                                              QDeclarativeItemPrivate::resources_at,
2802                                              QDeclarativeItemPrivate::resources_clear
2803                                              );
2804 }
2805
2806 /*!
2807   \qmlproperty list<State> QtQuick1::Item::states
2808   This property holds a list of states defined by the item.
2809
2810   \qml
2811   Item {
2812       states: [
2813           State {
2814               // ...
2815           },
2816           State {
2817               // ...
2818           }
2819           // ...
2820       ]
2821   }
2822   \endqml
2823
2824   \sa {qmlstate}{States}
2825 */
2826
2827 QDeclarativeListProperty<QDeclarative1State> QDeclarativeItemPrivate::states()
2828 {
2829     return _states()->statesProperty();
2830 }
2831
2832 /*!
2833   \qmlproperty list<Transition> QtQuick1::Item::transitions
2834   This property holds a list of transitions defined by the item.
2835
2836   \qml
2837   Item {
2838       transitions: [
2839           Transition {
2840               // ...
2841           },
2842           Transition {
2843               // ...
2844           }
2845           // ...
2846       ]
2847   }
2848   \endqml
2849
2850   \sa {QML Animation and Transitions}{Transitions}
2851 */
2852
2853
2854 QDeclarativeListProperty<QDeclarative1Transition> QDeclarativeItemPrivate::transitions()
2855 {
2856     return _states()->transitionsProperty();
2857 }
2858
2859 /*
2860   \qmlproperty list<Filter> QtQuick1::Item::filter
2861   This property holds a list of graphical filters to be applied to the item.
2862
2863   \l {Filter}{Filters} include things like \l {Blur}{blurring}
2864   the item, or giving it a \l Reflection.  Some
2865   filters may not be available on all canvases; if a filter is not
2866   available on a certain canvas, it will simply not be applied for
2867   that canvas (but the QML will still be considered valid).
2868
2869   \qml
2870   Item {
2871       filter: [
2872           Blur {
2873               // ...
2874           },
2875           Reflection {
2876               // ...
2877           }
2878           // ...
2879       ]
2880   }
2881   \endqml
2882 */
2883
2884 /*!
2885   \qmlproperty bool QtQuick1::Item::clip
2886   This property holds whether clipping is enabled. The default clip value is \c false.
2887
2888   If clipping is enabled, an item will clip its own painting, as well
2889   as the painting of its children, to its bounding rectangle.
2890
2891   Non-rectangular clipping regions are not supported for performance reasons.
2892 */
2893
2894 /*!
2895   \property QDeclarativeItem::clip
2896   This property holds whether clipping is enabled. The default clip value is \c false.
2897
2898   If clipping is enabled, an item will clip its own painting, as well
2899   as the painting of its children, to its bounding rectangle. If you set
2900   clipping during an item's paint operation, remember to re-set it to
2901   prevent clipping the rest of your scene.
2902
2903   Non-rectangular clipping regions are not supported for performance reasons.
2904 */
2905
2906 /*!
2907   \qmlproperty string QtQuick1::Item::state
2908
2909   This property holds the name of the current state of the item.
2910
2911   This property is often used in scripts to change between states. For
2912   example:
2913
2914   \js
2915   function toggle() {
2916       if (button.state == 'On')
2917           button.state = 'Off';
2918       else
2919           button.state = 'On';
2920   }
2921   \endjs
2922
2923   If the item is in its base state (i.e. no explicit state has been
2924   set), \c state will be a blank string. Likewise, you can return an
2925   item to its base state by setting its current state to \c ''.
2926
2927   \sa {qmlstates}{States}
2928 */
2929
2930 QString QDeclarativeItemPrivate::state() const
2931 {
2932     if (!_stateGroup)
2933         return QString();
2934     else
2935         return _stateGroup->state();
2936 }
2937
2938 void QDeclarativeItemPrivate::setState(const QString &state)
2939 {
2940     _states()->setState(state);
2941 }
2942
2943 /*!
2944   \qmlproperty list<Transform> QtQuick1::Item::transform
2945   This property holds the list of transformations to apply.
2946
2947   For more information see \l Transform.
2948 */
2949
2950 /*! \internal */
2951 QDeclarativeListProperty<QGraphicsTransform> QDeclarativeItem::transform()
2952 {
2953     return QDeclarativeListProperty<QGraphicsTransform>(this, 0, QDeclarativeItemPrivate::transform_append,
2954                                                         QDeclarativeItemPrivate::transform_count,
2955                                                         QDeclarativeItemPrivate::transform_at,
2956                                                         QDeclarativeItemPrivate::transform_clear);
2957 }
2958
2959 /*!
2960   \internal
2961
2962   classBegin() is called when the item is constructed, but its
2963   properties have not yet been set.
2964
2965   \sa componentComplete(), isComponentComplete()
2966 */
2967 void QDeclarativeItem::classBegin()
2968 {
2969     Q_D(QDeclarativeItem);
2970     d->componentComplete = false;
2971     if (d->_stateGroup)
2972         d->_stateGroup->classBegin();
2973     if (d->_anchors)
2974         d->_anchors->classBegin();
2975 }
2976
2977 /*!
2978   \internal
2979
2980   componentComplete() is called when all items in the component
2981   have been constructed.  It is often desirable to delay some
2982   processing until the component is complete an all bindings in the
2983   component have been resolved.
2984 */
2985 void QDeclarativeItem::componentComplete()
2986 {
2987     Q_D(QDeclarativeItem);
2988     d->componentComplete = true;
2989     if (d->_stateGroup)
2990         d->_stateGroup->componentComplete();
2991     if (d->_anchors) {
2992         d->_anchors->componentComplete();
2993         d->_anchors->d_func()->updateOnComplete();
2994     }
2995     if (d->keyHandler)
2996         d->keyHandler->componentComplete();
2997     if (d->_contents)
2998         d->_contents->complete();
2999 }
3000
3001 QDeclarative1StateGroup *QDeclarativeItemPrivate::_states()
3002 {
3003     Q_Q(QDeclarativeItem);
3004     if (!_stateGroup) {
3005         _stateGroup = new QDeclarative1StateGroup;
3006         if (!componentComplete)
3007             _stateGroup->classBegin();
3008         QObject::connect(_stateGroup, SIGNAL(stateChanged(QString)),
3009                          q, SIGNAL(stateChanged(QString)));
3010     }
3011
3012     return _stateGroup;
3013 }
3014
3015 QDeclarativeItemPrivate::AnchorLines::AnchorLines(QGraphicsObject *q)
3016 {
3017     left.item = q;
3018     left.anchorLine = QDeclarative1AnchorLine::Left;
3019     right.item = q;
3020     right.anchorLine = QDeclarative1AnchorLine::Right;
3021     hCenter.item = q;
3022     hCenter.anchorLine = QDeclarative1AnchorLine::HCenter;
3023     top.item = q;
3024     top.anchorLine = QDeclarative1AnchorLine::Top;
3025     bottom.item = q;
3026     bottom.anchorLine = QDeclarative1AnchorLine::Bottom;
3027     vCenter.item = q;
3028     vCenter.anchorLine = QDeclarative1AnchorLine::VCenter;
3029     baseline.item = q;
3030     baseline.anchorLine = QDeclarative1AnchorLine::Baseline;
3031 }
3032
3033 void QDeclarativeItemPrivate::setAccessibleFlagAndListener()
3034 {
3035     Q_Q(QDeclarativeItem);
3036     QDeclarativeItem *item = q;
3037     while (item) {
3038         if (item->d_func()->isAccessible)
3039             break; // already set - grandparents should have the flag set as well.
3040
3041 //        if (qmlEngine(item) != 0) {
3042 //            item->d_func()->addItemChangeListener(QDeclarativeEnginePrivate::getAccessibilityUpdateManager(qmlEngine(item)),
3043 //                QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Visibility |
3044 //                QDeclarativeItemPrivate::Opacity | QDeclarativeItemPrivate::Destroyed);
3045 //        }
3046
3047         item->d_func()->isAccessible = true;
3048         item = item->parentItem();
3049     }
3050 }
3051
3052 QPointF QDeclarativeItemPrivate::computeTransformOrigin() const
3053 {
3054     Q_Q(const QDeclarativeItem);
3055
3056     QRectF br = q->boundingRect();
3057
3058     switch(origin) {
3059     default:
3060     case QDeclarativeItem::TopLeft:
3061         return QPointF(0, 0);
3062     case QDeclarativeItem::Top:
3063         return QPointF(br.width() / 2., 0);
3064     case QDeclarativeItem::TopRight:
3065         return QPointF(br.width(), 0);
3066     case QDeclarativeItem::Left:
3067         return QPointF(0, br.height() / 2.);
3068     case QDeclarativeItem::Center:
3069         return QPointF(br.width() / 2., br.height() / 2.);
3070     case QDeclarativeItem::Right:
3071         return QPointF(br.width(), br.height() / 2.);
3072     case QDeclarativeItem::BottomLeft:
3073         return QPointF(0, br.height());
3074     case QDeclarativeItem::Bottom:
3075         return QPointF(br.width() / 2., br.height());
3076     case QDeclarativeItem::BottomRight:
3077         return QPointF(br.width(), br.height());
3078     }
3079 }
3080
3081 /*! \internal */
3082 bool QDeclarativeItem::sceneEvent(QEvent *event)
3083 {
3084     Q_D(QDeclarativeItem);
3085     if (event->type() == QEvent::KeyPress) {
3086         QKeyEvent *k = static_cast<QKeyEvent *>(event);
3087         if ((k->key() == Qt::Key_Tab || k->key() == Qt::Key_Backtab) &&
3088             !(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) {
3089             keyPressEvent(static_cast<QKeyEvent *>(event));
3090             if (!event->isAccepted())
3091                 return QGraphicsItem::sceneEvent(event);
3092             else
3093                 return true;
3094         } else {
3095             return QGraphicsItem::sceneEvent(event);
3096         }
3097     } else {
3098         bool rv = QGraphicsItem::sceneEvent(event);
3099
3100         if (event->type() == QEvent::FocusIn ||
3101             event->type() == QEvent::FocusOut) {
3102             d->focusChanged(hasActiveFocus());
3103         }
3104         return rv;
3105     }
3106 }
3107
3108 /*!
3109     \internal
3110
3111     Note that unlike QGraphicsItems, QDeclarativeItem::itemChange() is \e not called
3112     during initial widget polishing. Items wishing to optimize start-up construction
3113     should instead consider using componentComplete().
3114 */
3115 QVariant QDeclarativeItem::itemChange(GraphicsItemChange change,
3116                                        const QVariant &value)
3117 {
3118     Q_D(QDeclarativeItem);
3119     switch (change) {
3120     case ItemParentHasChanged:
3121         d->resolveLayoutMirror();
3122         emit parentChanged(parentItem());
3123         break;
3124     case ItemVisibleHasChanged: {
3125             for(int ii = 0; ii < d->changeListeners.count(); ++ii) {
3126                 const QDeclarativeItemPrivate::ChangeListener &change = d->changeListeners.at(ii);
3127                 if (change.types & QDeclarativeItemPrivate::Visibility) {
3128                     change.listener->itemVisibilityChanged(this);
3129                 }
3130             }
3131         }
3132         break;
3133     case ItemOpacityHasChanged: {
3134             for(int ii = 0; ii < d->changeListeners.count(); ++ii) {
3135                 const QDeclarativeItemPrivate::ChangeListener &change = d->changeListeners.at(ii);
3136                 if (change.types & QDeclarativeItemPrivate::Opacity) {
3137                     change.listener->itemOpacityChanged(this);
3138                 }
3139             }
3140         }
3141         break;
3142     case ItemChildAddedChange:
3143         if (d->_contents && d->componentComplete)
3144             d->_contents->childAdded(qobject_cast<QDeclarativeItem*>(
3145                     value.value<QGraphicsItem*>()));
3146         break;
3147     case ItemChildRemovedChange:
3148         if (d->_contents && d->componentComplete)
3149             d->_contents->childRemoved(qobject_cast<QDeclarativeItem*>(
3150                     value.value<QGraphicsItem*>()));
3151         break;
3152     default:
3153         break;
3154     }
3155
3156     return QGraphicsItem::itemChange(change, value);
3157 }
3158
3159 /*! \internal */
3160 QRectF QDeclarativeItem::boundingRect() const
3161 {
3162     Q_D(const QDeclarativeItem);
3163     return QRectF(0, 0, d->mWidth, d->mHeight);
3164 }
3165
3166 /*!
3167     \enum QDeclarativeItem::TransformOrigin
3168
3169     Controls the point about which simple transforms like scale apply.
3170
3171     \value TopLeft The top-left corner of the item.
3172     \value Top The center point of the top of the item.
3173     \value TopRight The top-right corner of the item.
3174     \value Left The left most point of the vertical middle.
3175     \value Center The center of the item.
3176     \value Right The right most point of the vertical middle.
3177     \value BottomLeft The bottom-left corner of the item.
3178     \value Bottom The center point of the bottom of the item.
3179     \value BottomRight The bottom-right corner of the item.
3180 */
3181
3182 /*!
3183     Returns the current transform origin.
3184 */
3185 QDeclarativeItem::TransformOrigin QDeclarativeItem::transformOrigin() const
3186 {
3187     Q_D(const QDeclarativeItem);
3188     return d->origin;
3189 }
3190
3191 /*!
3192     Set the transform \a origin.
3193 */
3194 void QDeclarativeItem::setTransformOrigin(TransformOrigin origin)
3195 {
3196     Q_D(QDeclarativeItem);
3197     if (origin != d->origin) {
3198         d->origin = origin;
3199         if (d->transformData)
3200             QGraphicsItem::setTransformOriginPoint(d->computeTransformOrigin());
3201         else
3202             d->transformOriginDirty = true;
3203         emit transformOriginChanged(d->origin);
3204     }
3205 }
3206
3207 void QDeclarativeItemPrivate::transformChanged()
3208 {
3209     Q_Q(QDeclarativeItem);
3210     if (transformOriginDirty) {
3211         q->QGraphicsItem::setTransformOriginPoint(computeTransformOrigin());
3212         transformOriginDirty = false;
3213     }
3214 }
3215
3216 /*!
3217     \property QDeclarativeItem::smooth
3218     \brief whether the item is smoothly transformed.
3219
3220     This property is provided purely for the purpose of optimization. Turning
3221     smooth transforms off is faster, but looks worse; turning smooth
3222     transformations on is slower, but looks better.
3223
3224     By default smooth transformations are off.
3225 */
3226
3227 /*!
3228     Returns true if the item should be drawn with antialiasing and
3229     smooth pixmap filtering, false otherwise.
3230
3231     The default is false.
3232
3233     \sa setSmooth()
3234 */
3235 bool QDeclarativeItem::smooth() const
3236 {
3237     Q_D(const QDeclarativeItem);
3238     return d->smooth;
3239 }
3240
3241 /*!
3242     Sets whether the item should be drawn with antialiasing and
3243     smooth pixmap filtering to \a smooth.
3244
3245     \sa smooth()
3246 */
3247 void QDeclarativeItem::setSmooth(bool smooth)
3248 {
3249     Q_D(QDeclarativeItem);
3250     if (d->smooth == smooth)
3251         return;
3252     d->smooth = smooth;
3253     emit smoothChanged(smooth);
3254     update();
3255 }
3256
3257 /*!
3258   \property QDeclarativeItem::anchors
3259   \internal
3260 */
3261
3262 /*!
3263   \property QDeclarativeItem::left
3264   \internal
3265 */
3266
3267 /*!
3268   \property QDeclarativeItem::right
3269   \internal
3270 */
3271
3272 /*!
3273   \property QDeclarativeItem::horizontalCenter
3274   \internal
3275 */
3276
3277 /*!
3278   \property QDeclarativeItem::top
3279   \internal
3280 */
3281
3282 /*!
3283   \property QDeclarativeItem::bottom
3284   \internal
3285 */
3286
3287 /*!
3288   \property QDeclarativeItem::verticalCenter
3289   \internal
3290 */
3291
3292 /*!
3293   \property QDeclarativeItem::focus
3294   \internal
3295 */
3296
3297 /*!
3298   \property QDeclarativeItem::transform
3299   \internal
3300 */
3301
3302 /*!
3303   \property QDeclarativeItem::transformOrigin
3304   \internal
3305 */
3306
3307 /*!
3308   \property QDeclarativeItem::activeFocus
3309   \internal
3310 */
3311
3312 /*!
3313   \property QDeclarativeItem::baseline
3314   \internal
3315 */
3316
3317 /*!
3318   \property QDeclarativeItem::data
3319   \internal
3320 */
3321
3322 /*!
3323   \property QDeclarativeItem::resources
3324   \internal
3325 */
3326
3327 /*!
3328   \property QDeclarativeItem::state
3329   \internal
3330 */
3331
3332 /*!
3333   \property QDeclarativeItem::states
3334   \internal
3335 */
3336
3337 /*!
3338   \property QDeclarativeItem::transformOriginPoint
3339   \internal
3340 */
3341
3342 /*!
3343   \property QDeclarativeItem::transitions
3344   \internal
3345 */
3346
3347 /*!
3348     \internal
3349     Return the width of the item
3350 */
3351 qreal QDeclarativeItem::width() const
3352 {
3353     Q_D(const QDeclarativeItem);
3354     return d->width();
3355 }
3356
3357 /*!
3358     \internal
3359     Set the width of the item
3360 */
3361 void QDeclarativeItem::setWidth(qreal w)
3362 {
3363     Q_D(QDeclarativeItem);
3364     d->setWidth(w);
3365 }
3366
3367 /*!
3368     \internal
3369     Reset the width of the item
3370 */
3371 void QDeclarativeItem::resetWidth()
3372 {
3373     Q_D(QDeclarativeItem);
3374     d->resetWidth();
3375 }
3376
3377 /*!
3378     \internal
3379     Return the width of the item
3380 */
3381 qreal QDeclarativeItemPrivate::width() const
3382 {
3383     return mWidth;
3384 }
3385
3386 /*!
3387     \internal
3388 */
3389 void QDeclarativeItemPrivate::setWidth(qreal w)
3390 {
3391     Q_Q(QDeclarativeItem);
3392     if (qIsNaN(w))
3393         return;
3394
3395     widthValid = true;
3396     if (mWidth == w)
3397         return;
3398
3399     qreal oldWidth = mWidth;
3400
3401     q->prepareGeometryChange();
3402     mWidth = w;
3403
3404     q->geometryChanged(QRectF(q->x(), q->y(), width(), height()),
3405                     QRectF(q->x(), q->y(), oldWidth, height()));
3406 }
3407
3408 /*!
3409     \internal
3410 */
3411 void QDeclarativeItemPrivate::resetWidth()
3412 {
3413     Q_Q(QDeclarativeItem);
3414     widthValid = false;
3415     q->setImplicitWidth(q->implicitWidth());
3416 }
3417
3418 void QDeclarativeItemPrivate::implicitWidthChanged()
3419 {
3420     Q_Q(QDeclarativeItem);
3421     emit q->implicitWidthChanged();
3422 }
3423
3424 qreal QDeclarativeItemPrivate::implicitWidth() const
3425 {
3426     return mImplicitWidth;
3427 }
3428
3429 /*!
3430     Returns the width of the item that is implied by other properties that determine the content.
3431 */
3432 qreal QDeclarativeItem::implicitWidth() const
3433 {
3434     Q_D(const QDeclarativeItem);
3435     return d->implicitWidth();
3436 }
3437
3438 /*!
3439     Sets the implied width of the item to \a w.
3440     This is the width implied by other properties that determine the content.
3441 */
3442 void QDeclarativeItem::setImplicitWidth(qreal w)
3443 {
3444     Q_D(QDeclarativeItem);
3445     bool changed = w != d->mImplicitWidth;
3446     d->mImplicitWidth = w;
3447     if (d->mWidth == w || widthValid()) {
3448         if (changed)
3449             d->implicitWidthChanged();
3450         return;
3451     }
3452
3453     qreal oldWidth = d->mWidth;
3454
3455     prepareGeometryChange();
3456     d->mWidth = w;
3457
3458     geometryChanged(QRectF(x(), y(), width(), height()),
3459                     QRectF(x(), y(), oldWidth, height()));
3460
3461     if (changed)
3462         d->implicitWidthChanged();
3463 }
3464
3465 /*!
3466     Returns whether the width property has been set explicitly.
3467 */
3468 bool QDeclarativeItem::widthValid() const
3469 {
3470     Q_D(const QDeclarativeItem);
3471     return d->widthValid;
3472 }
3473
3474 /*!
3475     \internal
3476     Return the height of the item
3477 */
3478 qreal QDeclarativeItem::height() const
3479 {
3480     Q_D(const QDeclarativeItem);
3481     return d->height();
3482 }
3483
3484 /*!
3485     \internal
3486     Set the height of the item
3487 */
3488 void QDeclarativeItem::setHeight(qreal h)
3489 {
3490     Q_D(QDeclarativeItem);
3491     d->setHeight(h);
3492 }
3493
3494 /*!
3495     \internal
3496     Reset the height of the item
3497 */
3498 void QDeclarativeItem::resetHeight()
3499 {
3500     Q_D(QDeclarativeItem);
3501     d->resetHeight();
3502 }
3503
3504 /*!
3505     \internal
3506 */
3507 qreal QDeclarativeItemPrivate::height() const
3508 {
3509     return mHeight;
3510 }
3511
3512 /*!
3513     \internal
3514 */
3515 void QDeclarativeItemPrivate::setHeight(qreal h)
3516 {
3517     Q_Q(QDeclarativeItem);
3518     if (qIsNaN(h))
3519         return;
3520
3521     heightValid = true;
3522     if (mHeight == h)
3523         return;
3524
3525     qreal oldHeight = mHeight;
3526
3527     q->prepareGeometryChange();
3528     mHeight = h;
3529
3530     q->geometryChanged(QRectF(q->x(), q->y(), width(), height()),
3531                     QRectF(q->x(), q->y(), width(), oldHeight));
3532 }
3533
3534 /*!
3535     \internal
3536 */
3537 void QDeclarativeItemPrivate::resetHeight()
3538 {
3539     Q_Q(QDeclarativeItem);
3540     heightValid = false;
3541     q->setImplicitHeight(q->implicitHeight());
3542 }
3543
3544 void QDeclarativeItemPrivate::implicitHeightChanged()
3545 {
3546     Q_Q(QDeclarativeItem);
3547     emit q->implicitHeightChanged();
3548 }
3549
3550 qreal QDeclarativeItemPrivate::implicitHeight() const
3551 {
3552     return mImplicitHeight;
3553 }
3554
3555 /*!
3556     Returns the height of the item that is implied by other properties that determine the content.
3557 */
3558 qreal QDeclarativeItem::implicitHeight() const
3559 {
3560     Q_D(const QDeclarativeItem);
3561     return d->implicitHeight();
3562 }
3563
3564 /*!
3565     \qmlproperty real QtQuick1::Item::implicitWidth
3566     \qmlproperty real QtQuick1::Item::implicitHeight
3567     \since Quick 1.1
3568
3569     Defines the natural width or height of the Item if no \l width or \l height is specified.
3570
3571     The default implicit size for most items is 0x0, however some elements have an inherent
3572     implicit size which cannot be overridden, e.g. Image, Text.
3573
3574     Setting the implicit size is useful for defining components that have a preferred size
3575     based on their content, for example:
3576
3577     \qml
3578     // Label.qml
3579     import QtQuick 1.1
3580
3581     Item {
3582         property alias icon: image.source
3583         property alias label: text.text
3584         implicitWidth: text.implicitWidth + image.implicitWidth
3585         implicitHeight: Math.max(text.implicitHeight, image.implicitHeight)
3586         Image { id: image }
3587         Text {
3588             id: text
3589             wrapMode: Text.Wrap
3590             anchors.left: image.right; anchors.right: parent.right
3591             anchors.verticalCenter: parent.verticalCenter
3592         }
3593     }
3594     \endqml
3595
3596     \bold Note: using implicitWidth of Text or TextEdit and setting the width explicitly
3597     incurs a performance penalty as the text must be laid out twice.
3598 */
3599
3600
3601 /*!
3602     Sets the implied height of the item to \a h.
3603     This is the height implied by other properties that determine the content.
3604 */
3605 void QDeclarativeItem::setImplicitHeight(qreal h)
3606 {
3607     Q_D(QDeclarativeItem);
3608     bool changed = h != d->mImplicitHeight;
3609     d->mImplicitHeight = h;
3610     if (d->mHeight == h || heightValid()) {
3611         if (changed)
3612             d->implicitHeightChanged();
3613         return;
3614     }
3615
3616     qreal oldHeight = d->mHeight;
3617
3618     prepareGeometryChange();
3619     d->mHeight = h;
3620
3621     geometryChanged(QRectF(x(), y(), width(), height()),
3622                     QRectF(x(), y(), width(), oldHeight));
3623
3624     if (changed)
3625         d->implicitHeightChanged();
3626 }
3627
3628 /*!
3629     Returns whether the height property has been set explicitly.
3630 */
3631 bool QDeclarativeItem::heightValid() const
3632 {
3633     Q_D(const QDeclarativeItem);
3634     return d->heightValid;
3635 }
3636
3637 /*! \internal */
3638 void QDeclarativeItem::setSize(const QSizeF &size)
3639 {
3640     Q_D(QDeclarativeItem);
3641     d->heightValid = true;
3642     d->widthValid = true;
3643
3644     if (d->height() == size.height() && d->width() == size.width())
3645         return;
3646
3647     qreal oldHeight = d->height();
3648     qreal oldWidth = d->width();
3649
3650     prepareGeometryChange();
3651     d->setHeight(size.height());
3652     d->setWidth(size.width());
3653
3654     geometryChanged(QRectF(x(), y(), width(), height()),
3655                     QRectF(x(), y(), oldWidth, oldHeight));
3656 }
3657
3658 /*!
3659   \qmlproperty bool QtQuick1::Item::activeFocus
3660
3661   This property indicates whether the item has active focus.
3662
3663   An item with active focus will receive keyboard input,
3664   or is a FocusScope ancestor of the item that will receive keyboard input.
3665
3666   Usually, activeFocus is gained by setting focus on an item and its enclosing
3667   FocusScopes. In the following example \c input will have activeFocus.
3668   \qml
3669   Rectangle {
3670       FocusScope {
3671           focus: true
3672           TextInput {
3673               id: input
3674               focus: true
3675           }
3676       }
3677   }
3678   \endqml
3679
3680   \sa focus, {qmlfocus}{Keyboard Focus}
3681 */
3682
3683 /*! \internal */
3684 bool QDeclarativeItem::hasActiveFocus() const
3685 {
3686     Q_D(const QDeclarativeItem);
3687     return (focusItem() && focusItem()->isVisible()) && (focusItem() == this ||
3688            (d->flags & QGraphicsItem::ItemIsFocusScope && focusItem() != 0));
3689 }
3690
3691 /*!
3692   \qmlproperty bool QtQuick1::Item::focus
3693   This property indicates whether the item has focus within the enclosing focus scope. If true, this item
3694   will gain active focus when the enclosing focus scope gains active focus.
3695   In the following example, \c input will be given active focus when \c scope gains active focus.
3696   \qml
3697   Rectangle {
3698       FocusScope {
3699           id: scope
3700           TextInput {
3701               id: input
3702               focus: true
3703           }
3704       }
3705   }
3706   \endqml
3707
3708   For the purposes of this property, the scene as a whole is assumed to act like a focus scope.
3709   On a practical level, that means the following QML will give active focus to \c input on startup.
3710
3711   \qml
3712   Rectangle {
3713       TextInput {
3714           id: input
3715           focus: true
3716       }
3717   }
3718   \endqml
3719
3720   \sa activeFocus, {qmlfocus}{Keyboard Focus}
3721 */
3722
3723 /*! \internal */
3724 bool QDeclarativeItem::hasFocus() const
3725 {
3726     Q_D(const QDeclarativeItem);
3727     QGraphicsItem *p = d->parent;
3728     while (p) {
3729         if (p->flags() & QGraphicsItem::ItemIsFocusScope) {
3730             return p->focusScopeItem() == this;
3731         }
3732         p = p->parentItem();
3733     }
3734
3735     return hasActiveFocus();
3736 }
3737
3738 /*! \internal */
3739 void QDeclarativeItem::setFocus(bool focus)
3740 {
3741     if (focus)
3742         QGraphicsItem::setFocus(Qt::OtherFocusReason);
3743     else
3744         QGraphicsItem::clearFocus();
3745 }
3746
3747 /*!
3748     \internal
3749 */
3750 void QDeclarativeItem::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *)
3751 {
3752 }
3753
3754 /*!
3755     \internal
3756 */
3757 bool QDeclarativeItem::event(QEvent *ev)
3758 {
3759     Q_D(QDeclarativeItem);
3760     switch (ev->type()) {
3761     case QEvent::KeyPress:
3762     case QEvent::KeyRelease:
3763     case QEvent::InputMethod:
3764         d->doneEventPreHandler = false;
3765         break;
3766     default:
3767         break;
3768     }
3769
3770     return QGraphicsObject::event(ev);
3771 }
3772
3773 #ifndef QT_NO_DEBUG_STREAM
3774 QDebug operator<<(QDebug debug, QDeclarativeItem *item)
3775 {
3776     if (!item) {
3777         debug << "QDeclarativeItem(0)";
3778         return debug;
3779     }
3780
3781     debug << item->metaObject()->className() << "(this =" << ((void*)item)
3782           << ", parent =" << ((void*)item->parentItem())
3783           << ", geometry =" << QRectF(item->pos(), QSizeF(item->width(), item->height()))
3784           << ", z =" << item->zValue() << ')';
3785     return debug;
3786 }
3787 #endif
3788
3789 qint64 QDeclarativeItemPrivate::consistentTime = -1;
3790 void QDeclarativeItemPrivate::setConsistentTime(qint64 t)
3791 {
3792     consistentTime = t;
3793 }
3794
3795 class QElapsedTimerConsistentTimeHack_1
3796 {
3797 public:
3798     void start() {
3799         t1 = QDeclarativeItemPrivate::consistentTime;
3800         t2 = 0;
3801     }
3802     qint64 elapsed() {
3803         return QDeclarativeItemPrivate::consistentTime - t1;
3804     }
3805     qint64 restart() {
3806         qint64 val = QDeclarativeItemPrivate::consistentTime - t1;
3807         t1 = QDeclarativeItemPrivate::consistentTime;
3808         t2 = 0;
3809         return val;
3810     }
3811
3812 private:
3813     qint64 t1;
3814     qint64 t2;
3815 };
3816
3817 void QDeclarativeItemPrivate::start(QElapsedTimer &t)
3818 {
3819     if (QDeclarativeItemPrivate::consistentTime == -1)
3820         t.start();
3821     else
3822         ((QElapsedTimerConsistentTimeHack_1*)&t)->start();
3823 }
3824
3825 qint64 QDeclarativeItemPrivate::elapsed(QElapsedTimer &t)
3826 {
3827     if (QDeclarativeItemPrivate::consistentTime == -1)
3828         return t.elapsed();
3829     else
3830         return ((QElapsedTimerConsistentTimeHack_1*)&t)->elapsed();
3831 }
3832
3833 qint64 QDeclarativeItemPrivate::restart(QElapsedTimer &t)
3834 {
3835     if (QDeclarativeItemPrivate::consistentTime == -1)
3836         return t.restart();
3837     else
3838         return ((QElapsedTimerConsistentTimeHack_1*)&t)->restart();
3839 }
3840
3841 QT_END_NAMESPACE
3842
3843 #include <moc_qdeclarativeitem.cpp>