1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qquickvisualitemmodel_p.h"
43 #include "qquickitem.h"
45 #include <QtCore/qcoreapplication.h>
46 #include <QtQml/qqmlcontext.h>
47 #include <QtQml/qqmlengine.h>
49 #include <private/qquickchangeset_p.h>
50 #include <private/qqmlglobal_p.h>
51 #include <private/qobject_p.h>
53 #include <QtCore/qhash.h>
54 #include <QtCore/qlist.h>
58 QHash<QObject*, QQuickVisualItemModelAttached*> QQuickVisualItemModelAttached::attachedProperties;
61 class QQuickVisualItemModelPrivate : public QObjectPrivate
63 Q_DECLARE_PUBLIC(QQuickVisualItemModel)
65 QQuickVisualItemModelPrivate() : QObjectPrivate() {}
67 static void children_append(QQmlListProperty<QQuickItem> *prop, QQuickItem *item) {
68 static_cast<QQuickVisualItemModelPrivate *>(prop->data)->children.append(Item(item));
69 static_cast<QQuickVisualItemModelPrivate *>(prop->data)->itemAppended();
70 static_cast<QQuickVisualItemModelPrivate *>(prop->data)->emitChildrenChanged();
73 static int children_count(QQmlListProperty<QQuickItem> *prop) {
74 return static_cast<QQuickVisualItemModelPrivate *>(prop->data)->children.count();
77 static QQuickItem *children_at(QQmlListProperty<QQuickItem> *prop, int index) {
78 return static_cast<QQuickVisualItemModelPrivate *>(prop->data)->children.at(index).item;
82 Q_Q(QQuickVisualItemModel);
83 QQuickVisualItemModelAttached *attached = QQuickVisualItemModelAttached::properties(children.last().item);
84 attached->setIndex(children.count()-1);
85 QQuickChangeSet changeSet;
86 changeSet.insert(children.count() - 1, 1);
87 emit q->modelUpdated(changeSet, false);
88 emit q->countChanged();
91 void emitChildrenChanged() {
92 Q_Q(QQuickVisualItemModel);
93 emit q->childrenChanged();
96 int indexOf(QQuickItem *item) const {
97 for (int i = 0; i < children.count(); ++i)
98 if (children.at(i).item == item)
105 Item(QQuickItem *i) : item(i), ref(0) {}
107 void addRef() { ++ref; }
108 bool deref() { return --ref == 0; }
114 QList<Item> children;
119 \qmlclass VisualItemModel QQuickVisualItemModel
120 \inqmlmodule QtQuick 2
121 \ingroup qtquick-models
122 \brief Defines items to be used added to a view
124 A VisualItemModel contains the visual items to be used in a view.
125 When a VisualItemModel is used in a view, the view does not require
126 a delegate since the VisualItemModel already contains the visual
129 An item can determine its index within the
130 model via the \l{VisualItemModel::index}{index} attached property.
132 The example below places three colored rectangles in a ListView.
139 Rectangle { height: 30; width: 80; color: "red" }
140 Rectangle { height: 30; width: 80; color: "green" }
141 Rectangle { height: 30; width: 80; color: "blue" }
151 \image visualitemmodel.png
153 \sa {declarative/modelviews/visualitemmodel}{VisualItemModel example}
155 QQuickVisualItemModel::QQuickVisualItemModel(QObject *parent)
156 : QQuickVisualModel(*(new QQuickVisualItemModelPrivate), parent)
161 \qmlattachedproperty int QtQuick2::VisualItemModel::index
162 This attached property holds the index of this delegate's item within the model.
164 It is attached to each instance of the delegate.
167 QQmlListProperty<QQuickItem> QQuickVisualItemModel::children()
169 Q_D(QQuickVisualItemModel);
170 return QQmlListProperty<QQuickItem>(this, d, d->children_append,
171 d->children_count, d->children_at);
175 \qmlproperty int QtQuick2::VisualItemModel::count
177 The number of items in the model. This property is readonly.
179 int QQuickVisualItemModel::count() const
181 Q_D(const QQuickVisualItemModel);
182 return d->children.count();
185 bool QQuickVisualItemModel::isValid() const
190 QQuickItem *QQuickVisualItemModel::item(int index, bool)
192 Q_D(QQuickVisualItemModel);
193 QQuickVisualItemModelPrivate::Item &item = d->children[index];
196 emit initItem(index, item.item);
197 emit createdItem(index, item.item);
202 QQuickVisualModel::ReleaseFlags QQuickVisualItemModel::release(QQuickItem *item)
204 Q_D(QQuickVisualItemModel);
205 int idx = d->indexOf(item);
207 if (d->children[idx].deref()) {
208 // XXX todo - the original did item->scene()->removeItem(). Why?
209 item->setParentItem(0);
211 return QQuickVisualModel::Referenced;
217 QString QQuickVisualItemModel::stringValue(int index, const QString &name)
219 Q_D(QQuickVisualItemModel);
220 if (index < 0 || index >= d->children.count())
222 return QQmlEngine::contextForObject(d->children.at(index).item)->contextProperty(name).toString();
225 int QQuickVisualItemModel::indexOf(QQuickItem *item, QObject *) const
227 Q_D(const QQuickVisualItemModel);
228 return d->indexOf(item);
231 QQuickVisualItemModelAttached *QQuickVisualItemModel::qmlAttachedProperties(QObject *obj)
233 return QQuickVisualItemModelAttached::properties(obj);