1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qdeclarativelistmodelworkeragent_p.h"
43 #include "qdeclarativelistmodel_p_p.h"
44 #include <private/qdeclarativedata_p.h>
45 #include <private/qdeclarativeengine_p.h>
46 #include <qdeclarativeinfo.h>
48 #include <QtCore/qcoreevent.h>
49 #include <QtCore/qcoreapplication.h>
50 #include <QtCore/qdebug.h>
56 void QDeclarativeListModelWorkerAgent::Data::clearChange(int uid)
58 for (int i=0 ; i < changes.count() ; ++i) {
59 if (changes[i].modelUid == uid) {
66 void QDeclarativeListModelWorkerAgent::Data::insertChange(int uid, int index, int count)
68 Change c = { uid, Change::Inserted, index, count, 0, QList<int>() };
72 void QDeclarativeListModelWorkerAgent::Data::removeChange(int uid, int index, int count)
74 Change c = { uid, Change::Removed, index, count, 0, QList<int>() };
78 void QDeclarativeListModelWorkerAgent::Data::moveChange(int uid, int index, int count, int to)
80 Change c = { uid, Change::Moved, index, count, to, QList<int>() };
84 void QDeclarativeListModelWorkerAgent::Data::changedChange(int uid, int index, int count, const QList<int> &roles)
86 Change c = { uid, Change::Changed, index, count, 0, roles };
90 QDeclarativeListModelWorkerAgent::QDeclarativeListModelWorkerAgent(QDeclarativeListModel *model)
91 : m_ref(1), m_orig(model), m_copy(new QDeclarativeListModel(model, this))
95 QDeclarativeListModelWorkerAgent::~QDeclarativeListModelWorkerAgent()
102 void QDeclarativeListModelWorkerAgent::setV8Engine(QV8Engine *eng)
104 m_copy->m_engine = eng;
107 void QDeclarativeListModelWorkerAgent::addref()
112 void QDeclarativeListModelWorkerAgent::release()
114 bool del = !m_ref.deref();
120 int QDeclarativeListModelWorkerAgent::count() const
122 return m_copy->count();
125 void QDeclarativeListModelWorkerAgent::clear()
130 void QDeclarativeListModelWorkerAgent::remove(QDeclarativeV8Function *args)
132 m_copy->remove(args);
135 void QDeclarativeListModelWorkerAgent::append(QDeclarativeV8Function *args)
137 m_copy->append(args);
140 void QDeclarativeListModelWorkerAgent::insert(QDeclarativeV8Function *args)
142 m_copy->insert(args);
145 QDeclarativeV8Handle QDeclarativeListModelWorkerAgent::get(int index) const
147 return m_copy->get(index);
150 void QDeclarativeListModelWorkerAgent::set(int index, const QDeclarativeV8Handle &value)
152 m_copy->set(index, value);
155 void QDeclarativeListModelWorkerAgent::setProperty(int index, const QString& property, const QVariant& value)
157 m_copy->setProperty(index, property, value);
160 void QDeclarativeListModelWorkerAgent::move(int from, int to, int count)
162 m_copy->move(from, to, count);
165 void QDeclarativeListModelWorkerAgent::sync()
170 data.changes.clear();
173 QCoreApplication::postEvent(this, s);
174 syncDone.wait(&mutex);
178 bool QDeclarativeListModelWorkerAgent::event(QEvent *e)
180 if (e->type() == QEvent::User) {
182 QMutexLocker locker(&mutex);
183 Sync *s = static_cast<Sync *>(e);
185 const QList<Change> &changes = s->data.changes;
187 bool cc = m_orig->count() != s->list->count();
189 QHash<int, QDeclarativeListModel *> targetModelDynamicHash;
190 QHash<int, ListModel *> targetModelStaticHash;
192 Q_ASSERT(m_orig->m_dynamicRoles == s->list->m_dynamicRoles);
193 if (m_orig->m_dynamicRoles)
194 QDeclarativeListModel::sync(s->list, m_orig, &targetModelDynamicHash);
196 ListModel::sync(s->list->m_listModel, m_orig->m_listModel, &targetModelStaticHash);
198 for (int ii = 0; ii < changes.count(); ++ii) {
199 const Change &change = changes.at(ii);
201 QDeclarativeListModel *model = 0;
202 if (m_orig->m_dynamicRoles) {
203 model = targetModelDynamicHash.value(change.modelUid);
205 ListModel *lm = targetModelStaticHash.value(change.modelUid);
207 model = lm->m_modelCache;
211 switch (change.type) {
212 case Change::Inserted:
213 emit model->itemsInserted(change.index, change.count);
215 case Change::Removed:
216 emit model->itemsRemoved(change.index, change.count);
219 emit model->itemsMoved(change.index, change.to, change.count);
221 case Change::Changed:
222 emit model->itemsChanged(change.index, change.count, change.roles);
232 emit m_orig->countChanged();
235 return QObject::event(e);