Fix test fails related to QTBUG-22237
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgvisualitemmodel.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qsgvisualitemmodel_p.h"
43 #include "qsgitem.h"
44
45 #include <QtCore/qcoreapplication.h>
46 #include <QtDeclarative/qdeclarativecontext.h>
47 #include <QtDeclarative/qdeclarativeengine.h>
48
49 #include <private/qdeclarativechangeset_p.h>
50 #include <private/qdeclarativeglobal_p.h>
51 #include <private/qobject_p.h>
52
53 #include <QtCore/qhash.h>
54 #include <QtCore/qlist.h>
55
56 QT_BEGIN_NAMESPACE
57
58 QHash<QObject*, QSGVisualItemModelAttached*> QSGVisualItemModelAttached::attachedProperties;
59
60
61 class QSGVisualItemModelPrivate : public QObjectPrivate
62 {
63     Q_DECLARE_PUBLIC(QSGVisualItemModel)
64 public:
65     QSGVisualItemModelPrivate() : QObjectPrivate() {}
66
67     static void children_append(QDeclarativeListProperty<QSGItem> *prop, QSGItem *item) {
68         QDeclarative_setParent_noEvent(item, prop->object);
69         static_cast<QSGVisualItemModelPrivate *>(prop->data)->children.append(Item(item));
70         static_cast<QSGVisualItemModelPrivate *>(prop->data)->itemAppended();
71         static_cast<QSGVisualItemModelPrivate *>(prop->data)->emitChildrenChanged();
72     }
73
74     static int children_count(QDeclarativeListProperty<QSGItem> *prop) {
75         return static_cast<QSGVisualItemModelPrivate *>(prop->data)->children.count();
76     }
77
78     static QSGItem *children_at(QDeclarativeListProperty<QSGItem> *prop, int index) {
79         return static_cast<QSGVisualItemModelPrivate *>(prop->data)->children.at(index).item;
80     }
81
82     void itemAppended() {
83         Q_Q(QSGVisualItemModel);
84         QSGVisualItemModelAttached *attached = QSGVisualItemModelAttached::properties(children.last().item);
85         attached->setIndex(children.count()-1);
86         QDeclarativeChangeSet changeSet;
87         changeSet.insert(children.count() - 1, 1);
88         emit q->modelUpdated(changeSet, false);
89         emit q->countChanged();
90     }
91
92     void emitChildrenChanged() {
93         Q_Q(QSGVisualItemModel);
94         emit q->childrenChanged();
95     }
96
97     int indexOf(QSGItem *item) const {
98         for (int i = 0; i < children.count(); ++i)
99             if (children.at(i).item == item)
100                 return i;
101         return -1;
102     }
103
104     class Item {
105     public:
106         Item(QSGItem *i) : item(i), ref(0) {}
107
108         void addRef() { ++ref; }
109         bool deref() { return --ref == 0; }
110
111         QSGItem *item;
112         int ref;
113     };
114
115     QList<Item> children;
116 };
117
118
119 /*!
120     \qmlclass VisualItemModel QSGVisualItemModel
121     \inqmlmodule QtQuick 2
122     \ingroup qml-working-with-data
123     \brief The VisualItemModel allows items to be provided to a view.
124
125     A VisualItemModel contains the visual items to be used in a view.
126     When a VisualItemModel is used in a view, the view does not require
127     a delegate since the VisualItemModel already contains the visual
128     delegate (items).
129
130     An item can determine its index within the
131     model via the \l{VisualItemModel::index}{index} attached property.
132
133     The example below places three colored rectangles in a ListView.
134     \code
135     import QtQuick 1.0
136
137     Rectangle {
138         VisualItemModel {
139             id: itemModel
140             Rectangle { height: 30; width: 80; color: "red" }
141             Rectangle { height: 30; width: 80; color: "green" }
142             Rectangle { height: 30; width: 80; color: "blue" }
143         }
144
145         ListView {
146             anchors.fill: parent
147             model: itemModel
148         }
149     }
150     \endcode
151
152     \image visualitemmodel.png
153
154     \sa {declarative/modelviews/visualitemmodel}{VisualItemModel example}
155 */
156 QSGVisualItemModel::QSGVisualItemModel(QObject *parent)
157     : QSGVisualModel(*(new QSGVisualItemModelPrivate), parent)
158 {
159 }
160
161 /*!
162     \qmlattachedproperty int QtQuick2::VisualItemModel::index
163     This attached property holds the index of this delegate's item within the model.
164
165     It is attached to each instance of the delegate.
166 */
167
168 QDeclarativeListProperty<QSGItem> QSGVisualItemModel::children()
169 {
170     Q_D(QSGVisualItemModel);
171     return QDeclarativeListProperty<QSGItem>(this, d, d->children_append,
172                                                       d->children_count, d->children_at);
173 }
174
175 /*!
176     \qmlproperty int QtQuick2::VisualItemModel::count
177
178     The number of items in the model.  This property is readonly.
179 */
180 int QSGVisualItemModel::count() const
181 {
182     Q_D(const QSGVisualItemModel);
183     return d->children.count();
184 }
185
186 bool QSGVisualItemModel::isValid() const
187 {
188     return true;
189 }
190
191 QSGItem *QSGVisualItemModel::item(int index, bool)
192 {
193     Q_D(QSGVisualItemModel);
194     QSGVisualItemModelPrivate::Item &item = d->children[index];
195     item.addRef();
196     return item.item;
197 }
198
199 QSGVisualModel::ReleaseFlags QSGVisualItemModel::release(QSGItem *item)
200 {
201     Q_D(QSGVisualItemModel);
202     int idx = d->indexOf(item);
203     if (idx >= 0) {
204         if (d->children[idx].deref()) {
205             // XXX todo - the original did item->scene()->removeItem().  Why?
206             item->setParentItem(0);
207             QDeclarative_setParent_noEvent(item, this);
208         }
209     }
210     return 0;
211 }
212
213 bool QSGVisualItemModel::completePending() const
214 {
215     return false;
216 }
217
218 void QSGVisualItemModel::completeItem()
219 {
220     // Nothing to do
221 }
222
223 QString QSGVisualItemModel::stringValue(int index, const QString &name)
224 {
225     Q_D(QSGVisualItemModel);
226     if (index < 0 || index >= d->children.count())
227         return QString();
228     return QDeclarativeEngine::contextForObject(d->children.at(index).item)->contextProperty(name).toString();
229 }
230
231 int QSGVisualItemModel::indexOf(QSGItem *item, QObject *) const
232 {
233     Q_D(const QSGVisualItemModel);
234     return d->indexOf(item);
235 }
236
237 QSGVisualItemModelAttached *QSGVisualItemModel::qmlAttachedProperties(QObject *obj)
238 {
239     return QSGVisualItemModelAttached::properties(obj);
240 }
241
242 QT_END_NAMESPACE
243