Initial import from qtquick2.
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgrepeater.cpp
1 // Commit: a9f1eaa6a368bf7a72b52c428728a3e3e0a76209
2 /****************************************************************************
3 **
4 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 ** All rights reserved.
6 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 **
8 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 **
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
15 ** this package.
16 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 **
39 ** $QT_END_LICENSE$
40 **
41 ****************************************************************************/
42
43 #include "qsgrepeater_p.h"
44 #include "qsgrepeater_p_p.h"
45 #include "qsgvisualitemmodel_p.h"
46
47 #include <private/qdeclarativeglobal_p.h>
48 #include <private/qdeclarativelistaccessor_p.h>
49 #include <private/qlistmodelinterface_p.h>
50
51 QT_BEGIN_NAMESPACE
52
53 QSGRepeaterPrivate::QSGRepeaterPrivate()
54 : model(0), ownModel(false)
55 {
56 }
57
58 QSGRepeaterPrivate::~QSGRepeaterPrivate()
59 {
60     if (ownModel)
61         delete model;
62 }
63
64 QSGRepeater::QSGRepeater(QSGItem *parent)
65   : QSGItem(*(new QSGRepeaterPrivate), parent)
66 {
67 }
68
69 QSGRepeater::~QSGRepeater()
70 {
71 }
72
73 QVariant QSGRepeater::model() const
74 {
75     Q_D(const QSGRepeater);
76     return d->dataSource;
77 }
78
79 void QSGRepeater::setModel(const QVariant &model)
80 {
81     Q_D(QSGRepeater);
82     if (d->dataSource == model)
83         return;
84
85     clear();
86     if (d->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()));
91         /*
92         disconnect(d->model, SIGNAL(createdItem(int,QSGItem*)), this, SLOT(createdItem(int,QSGItem*)));
93         disconnect(d->model, SIGNAL(destroyingItem(QSGItem*)), this, SLOT(destroyingItem(QSGItem*)));
94     */
95     }
96     d->dataSource = model;
97     QObject *object = qvariant_cast<QObject*>(model);
98     QSGVisualModel *vim = 0;
99     if (object && (vim = qobject_cast<QSGVisualModel *>(object))) {
100         if (d->ownModel) {
101             delete d->model;
102             d->ownModel = false;
103         }
104         d->model = vim;
105     } else {
106         if (!d->ownModel) {
107             d->model = new QSGVisualDataModel(qmlContext(this));
108             d->ownModel = true;
109         }
110         if (QSGVisualDataModel *dataModel = qobject_cast<QSGVisualDataModel*>(d->model))
111             dataModel->setModel(model);
112     }
113     if (d->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()));
118         /*
119         connect(d->model, SIGNAL(createdItem(int,QSGItem*)), this, SLOT(createdItem(int,QSGItem*)));
120         connect(d->model, SIGNAL(destroyingItem(QSGItem*)), this, SLOT(destroyingItem(QSGItem*)));
121         */
122         regenerate();
123     }
124     emit modelChanged();
125     emit countChanged();
126 }
127
128 QDeclarativeComponent *QSGRepeater::delegate() const
129 {
130     Q_D(const QSGRepeater);
131     if (d->model) {
132         if (QSGVisualDataModel *dataModel = qobject_cast<QSGVisualDataModel*>(d->model))
133             return dataModel->delegate();
134     }
135
136     return 0;
137 }
138
139 void QSGRepeater::setDelegate(QDeclarativeComponent *delegate)
140 {
141     Q_D(QSGRepeater);
142     if (QSGVisualDataModel *dataModel = qobject_cast<QSGVisualDataModel*>(d->model))
143        if (delegate == dataModel->delegate())
144            return;
145
146     if (!d->ownModel) {
147         d->model = new QSGVisualDataModel(qmlContext(this));
148         d->ownModel = true;
149     }
150     if (QSGVisualDataModel *dataModel = qobject_cast<QSGVisualDataModel*>(d->model)) {
151         dataModel->setDelegate(delegate);
152         regenerate();
153         emit delegateChanged();
154     }
155 }
156
157 int QSGRepeater::count() const
158 {
159     Q_D(const QSGRepeater);
160     if (d->model)
161         return d->model->count();
162     return 0;
163 }
164
165 QSGItem *QSGRepeater::itemAt(int index) const
166 {
167     Q_D(const QSGRepeater);
168     if (index >= 0 && index < d->deletables.count())
169         return d->deletables[index];
170     return 0;
171 }
172
173 void QSGRepeater::componentComplete()
174 {
175     QSGItem::componentComplete();
176     regenerate();
177 }
178
179 void QSGRepeater::itemChange(ItemChange change, const ItemChangeData &value)
180 {
181     QSGItem::itemChange(change, value);
182     if (change == ItemParentHasChanged) {
183         regenerate();
184     }
185 }
186
187 void QSGRepeater::clear()
188 {
189     Q_D(QSGRepeater);
190     bool complete = isComponentComplete();
191
192     if (d->model) {
193         while (d->deletables.count() > 0) {
194             QSGItem *item = d->deletables.takeLast();
195             if (complete)
196                 emit itemRemoved(d->deletables.count()-1, item);
197             d->model->release(item);
198         }
199     }
200     d->deletables.clear();
201 }
202
203 void QSGRepeater::regenerate()
204 {
205     Q_D(QSGRepeater);
206     if (!isComponentComplete())
207         return;
208
209     clear();
210
211     if (!d->model || !d->model->count() || !d->model->isValid() || !parentItem() || !isComponentComplete())
212         return;
213
214     for (int ii = 0; ii < count(); ++ii) {
215         QSGItem *item = d->model->item(ii);
216         if (item) {
217             QDeclarative_setParent_noEvent(item, parentItem());
218             item->setParentItem(parentItem());
219             item->stackBefore(this);
220             d->deletables << item;
221             emit itemAdded(ii, item);
222         }
223     }
224 }
225
226 void QSGRepeater::itemsInserted(int index, int count)
227 {
228     Q_D(QSGRepeater);
229     if (!isComponentComplete())
230         return;
231     for (int i = 0; i < count; ++i) {
232         int modelIndex = index + i;
233         QSGItem *item = d->model->item(modelIndex);
234         if (item) {
235             QDeclarative_setParent_noEvent(item, parentItem());
236             item->setParentItem(parentItem());
237             if (modelIndex < d->deletables.count())
238                 item->stackBefore(d->deletables.at(modelIndex));
239             else
240                 item->stackBefore(this);
241             d->deletables.insert(modelIndex, item);
242             emit itemAdded(modelIndex, item);
243         }
244     }
245     emit countChanged();
246 }
247
248 void QSGRepeater::itemsRemoved(int index, int count)
249 {
250     Q_D(QSGRepeater);
251     if (!isComponentComplete() || count <= 0)
252         return;
253     while (count--) {
254         QSGItem *item = d->deletables.takeAt(index);
255         emit itemRemoved(index, item);
256         if (item)
257             d->model->release(item);
258         else
259             break;
260     }
261     emit countChanged();
262 }
263
264 void QSGRepeater::itemsMoved(int from, int to, int count)
265 {
266     Q_D(QSGRepeater);
267     if (!isComponentComplete() || count <= 0)
268         return;
269     if (from + count > d->deletables.count()) {
270         regenerate();
271         return;
272     }
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));
283     }
284 }
285
286 void QSGRepeater::modelReset()
287 {
288     if (!isComponentComplete())
289         return;
290     regenerate();
291     emit countChanged();
292 }
293
294 QT_END_NAMESPACE