1 // Commit: a9f1eaa6a368bf7a72b52c428728a3e3e0a76209
2 /****************************************************************************
4 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 ** All rights reserved.
6 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 ** This file is part of the QtDeclarative module of the Qt Toolkit.
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** No Commercial Usage
12 ** This file contains pre-release code and may not be distributed.
13 ** You may use this file in accordance with the terms and conditions
14 ** contained in the Technology Preview License Agreement accompanying
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
41 ****************************************************************************/
43 #include "qsgrepeater_p.h"
44 #include "qsgrepeater_p_p.h"
45 #include "qsgvisualitemmodel_p.h"
47 #include <private/qdeclarativeglobal_p.h>
48 #include <private/qdeclarativelistaccessor_p.h>
49 #include <private/qlistmodelinterface_p.h>
53 QSGRepeaterPrivate::QSGRepeaterPrivate()
54 : model(0), ownModel(false)
58 QSGRepeaterPrivate::~QSGRepeaterPrivate()
64 QSGRepeater::QSGRepeater(QSGItem *parent)
65 : QSGItem(*(new QSGRepeaterPrivate), parent)
69 QSGRepeater::~QSGRepeater()
73 QVariant QSGRepeater::model() const
75 Q_D(const QSGRepeater);
79 void QSGRepeater::setModel(const QVariant &model)
82 if (d->dataSource == model)
87 disconnect(d->model, SIGNAL(itemsInserted(int,int)), this, SLOT(itemsInserted(int,int)));
88 disconnect(d->model, SIGNAL(itemsRemoved(int,int)), this, SLOT(itemsRemoved(int,int)));
89 disconnect(d->model, SIGNAL(itemsMoved(int,int,int)), this, SLOT(itemsMoved(int,int,int)));
90 disconnect(d->model, SIGNAL(modelReset()), this, SLOT(modelReset()));
92 disconnect(d->model, SIGNAL(createdItem(int,QSGItem*)), this, SLOT(createdItem(int,QSGItem*)));
93 disconnect(d->model, SIGNAL(destroyingItem(QSGItem*)), this, SLOT(destroyingItem(QSGItem*)));
96 d->dataSource = model;
97 QObject *object = qvariant_cast<QObject*>(model);
98 QSGVisualModel *vim = 0;
99 if (object && (vim = qobject_cast<QSGVisualModel *>(object))) {
107 d->model = new QSGVisualDataModel(qmlContext(this));
110 if (QSGVisualDataModel *dataModel = qobject_cast<QSGVisualDataModel*>(d->model))
111 dataModel->setModel(model);
114 connect(d->model, SIGNAL(itemsInserted(int,int)), this, SLOT(itemsInserted(int,int)));
115 connect(d->model, SIGNAL(itemsRemoved(int,int)), this, SLOT(itemsRemoved(int,int)));
116 connect(d->model, SIGNAL(itemsMoved(int,int,int)), this, SLOT(itemsMoved(int,int,int)));
117 connect(d->model, SIGNAL(modelReset()), this, SLOT(modelReset()));
119 connect(d->model, SIGNAL(createdItem(int,QSGItem*)), this, SLOT(createdItem(int,QSGItem*)));
120 connect(d->model, SIGNAL(destroyingItem(QSGItem*)), this, SLOT(destroyingItem(QSGItem*)));
128 QDeclarativeComponent *QSGRepeater::delegate() const
130 Q_D(const QSGRepeater);
132 if (QSGVisualDataModel *dataModel = qobject_cast<QSGVisualDataModel*>(d->model))
133 return dataModel->delegate();
139 void QSGRepeater::setDelegate(QDeclarativeComponent *delegate)
142 if (QSGVisualDataModel *dataModel = qobject_cast<QSGVisualDataModel*>(d->model))
143 if (delegate == dataModel->delegate())
147 d->model = new QSGVisualDataModel(qmlContext(this));
150 if (QSGVisualDataModel *dataModel = qobject_cast<QSGVisualDataModel*>(d->model)) {
151 dataModel->setDelegate(delegate);
153 emit delegateChanged();
157 int QSGRepeater::count() const
159 Q_D(const QSGRepeater);
161 return d->model->count();
165 QSGItem *QSGRepeater::itemAt(int index) const
167 Q_D(const QSGRepeater);
168 if (index >= 0 && index < d->deletables.count())
169 return d->deletables[index];
173 void QSGRepeater::componentComplete()
175 QSGItem::componentComplete();
179 void QSGRepeater::itemChange(ItemChange change, const ItemChangeData &value)
181 QSGItem::itemChange(change, value);
182 if (change == ItemParentHasChanged) {
187 void QSGRepeater::clear()
190 bool complete = isComponentComplete();
193 while (d->deletables.count() > 0) {
194 QSGItem *item = d->deletables.takeLast();
196 emit itemRemoved(d->deletables.count()-1, item);
197 d->model->release(item);
200 d->deletables.clear();
203 void QSGRepeater::regenerate()
206 if (!isComponentComplete())
211 if (!d->model || !d->model->count() || !d->model->isValid() || !parentItem() || !isComponentComplete())
214 for (int ii = 0; ii < count(); ++ii) {
215 QSGItem *item = d->model->item(ii);
217 QDeclarative_setParent_noEvent(item, parentItem());
218 item->setParentItem(parentItem());
219 item->stackBefore(this);
220 d->deletables << item;
221 emit itemAdded(ii, item);
226 void QSGRepeater::itemsInserted(int index, int count)
229 if (!isComponentComplete())
231 for (int i = 0; i < count; ++i) {
232 int modelIndex = index + i;
233 QSGItem *item = d->model->item(modelIndex);
235 QDeclarative_setParent_noEvent(item, parentItem());
236 item->setParentItem(parentItem());
237 if (modelIndex < d->deletables.count())
238 item->stackBefore(d->deletables.at(modelIndex));
240 item->stackBefore(this);
241 d->deletables.insert(modelIndex, item);
242 emit itemAdded(modelIndex, item);
248 void QSGRepeater::itemsRemoved(int index, int count)
251 if (!isComponentComplete() || count <= 0)
254 QSGItem *item = d->deletables.takeAt(index);
255 emit itemRemoved(index, item);
257 d->model->release(item);
264 void QSGRepeater::itemsMoved(int from, int to, int count)
267 if (!isComponentComplete() || count <= 0)
269 if (from + count > d->deletables.count()) {
273 QList<QSGItem*> removed;
274 int removedCount = count;
275 while (removedCount--)
276 removed << d->deletables.takeAt(from);
277 for (int i = 0; i < count; ++i)
278 d->deletables.insert(to + i, removed.at(i));
279 d->deletables.last()->stackBefore(this);
280 for (int i = d->model->count()-1; i > 0; --i) {
281 QSGItem *item = d->deletables.at(i-1);
282 item->stackBefore(d->deletables.at(i));
286 void QSGRepeater::modelReset()
288 if (!isComponentComplete())