Merge branch 'master' of git://gitorious.org/qt/qtdeclarative into api_changes
[profile/ivi/qtdeclarative.git] / src / qml / qml / qquicklistmodelworkeragent.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qquicklistmodelworkeragent_p.h"
43 #include "qquicklistmodel_p_p.h"
44 #include <private/qqmldata_p.h>
45 #include <private/qqmlengine_p.h>
46 #include <qqmlinfo.h>
47
48 #include <QtCore/qcoreevent.h>
49 #include <QtCore/qcoreapplication.h>
50 #include <QtCore/qdebug.h>
51
52
53 QT_BEGIN_NAMESPACE
54
55
56 void QQuickListModelWorkerAgent::Data::clearChange(int uid)
57 {
58     for (int i=0 ; i < changes.count() ; ++i) {
59         if (changes[i].modelUid == uid) {
60             changes.removeAt(i);
61             --i;
62         }
63     }
64 }
65
66 void QQuickListModelWorkerAgent::Data::insertChange(int uid, int index, int count)
67 {
68     Change c = { uid, Change::Inserted, index, count, 0, QList<int>() };
69     changes << c;
70 }
71
72 void QQuickListModelWorkerAgent::Data::removeChange(int uid, int index, int count)
73 {
74     Change c = { uid, Change::Removed, index, count, 0, QList<int>() };
75     changes << c;
76 }
77
78 void QQuickListModelWorkerAgent::Data::moveChange(int uid, int index, int count, int to)
79 {
80     Change c = { uid, Change::Moved, index, count, to, QList<int>() };
81     changes << c;
82 }
83
84 void QQuickListModelWorkerAgent::Data::changedChange(int uid, int index, int count, const QList<int> &roles)
85 {
86     Change c = { uid, Change::Changed, index, count, 0, roles };
87     changes << c;
88 }
89
90 QQuickListModelWorkerAgent::QQuickListModelWorkerAgent(QQuickListModel *model)
91 : m_ref(1), m_orig(model), m_copy(new QQuickListModel(model, this))
92 {
93 }
94
95 QQuickListModelWorkerAgent::~QQuickListModelWorkerAgent()
96 {
97     mutex.lock();
98     syncDone.wakeAll();
99     mutex.unlock();
100 }
101
102 void QQuickListModelWorkerAgent::setV8Engine(QV8Engine *eng)
103 {
104     m_copy->m_engine = eng;
105 }
106
107 void QQuickListModelWorkerAgent::addref()
108 {
109     m_ref.ref();
110 }
111
112 void QQuickListModelWorkerAgent::release()
113 {
114     bool del = !m_ref.deref();
115
116     if (del)
117         deleteLater();
118 }
119
120 void QQuickListModelWorkerAgent::modelDestroyed()
121 {
122     m_orig = 0;
123 }
124
125 int QQuickListModelWorkerAgent::count() const
126 {
127     return m_copy->count();
128 }
129
130 void QQuickListModelWorkerAgent::clear()
131 {
132     m_copy->clear();
133 }
134
135 void QQuickListModelWorkerAgent::remove(QQmlV8Function *args)
136 {
137     m_copy->remove(args);
138 }
139
140 void QQuickListModelWorkerAgent::append(QQmlV8Function *args)
141 {
142     m_copy->append(args);
143 }
144
145 void QQuickListModelWorkerAgent::insert(QQmlV8Function *args)
146 {
147     m_copy->insert(args);
148 }
149
150 QQmlV8Handle QQuickListModelWorkerAgent::get(int index) const
151 {
152     return m_copy->get(index);
153 }
154
155 void QQuickListModelWorkerAgent::set(int index, const QQmlV8Handle &value)
156 {
157     m_copy->set(index, value);
158 }
159
160 void QQuickListModelWorkerAgent::setProperty(int index, const QString& property, const QVariant& value)
161 {
162     m_copy->setProperty(index, property, value);
163 }
164
165 void QQuickListModelWorkerAgent::move(int from, int to, int count)
166 {
167     m_copy->move(from, to, count);
168 }
169
170 void QQuickListModelWorkerAgent::sync()
171 {
172     Sync *s = new Sync;
173     s->data = data;
174     s->list = m_copy;
175     data.changes.clear();
176
177     mutex.lock();
178     QCoreApplication::postEvent(this, s);
179     syncDone.wait(&mutex);
180     mutex.unlock();
181 }
182
183 bool QQuickListModelWorkerAgent::event(QEvent *e)
184 {
185     if (e->type() == QEvent::User) {
186         bool cc = false;
187         QMutexLocker locker(&mutex);
188         if (m_orig) {
189             Sync *s = static_cast<Sync *>(e);
190             const QList<Change> &changes = s->data.changes;
191
192             cc = m_orig->count() != s->list->count();
193
194             QHash<int, QQuickListModel *> targetModelDynamicHash;
195             QHash<int, ListModel *> targetModelStaticHash;
196
197             Q_ASSERT(m_orig->m_dynamicRoles == s->list->m_dynamicRoles);
198             if (m_orig->m_dynamicRoles)
199                 QQuickListModel::sync(s->list, m_orig, &targetModelDynamicHash);
200             else
201                 ListModel::sync(s->list->m_listModel, m_orig->m_listModel, &targetModelStaticHash);
202
203             for (int ii = 0; ii < changes.count(); ++ii) {
204                 const Change &change = changes.at(ii);
205
206                 QQuickListModel *model = 0;
207                 if (m_orig->m_dynamicRoles) {
208                     model = targetModelDynamicHash.value(change.modelUid);
209                 } else {
210                     ListModel *lm = targetModelStaticHash.value(change.modelUid);
211                     if (lm)
212                         model = lm->m_modelCache;
213                 }
214
215                 if (model) {
216                     switch (change.type) {
217                     case Change::Inserted:
218                         emit model->itemsInserted(change.index, change.count);
219                         break;
220                     case Change::Removed:
221                         emit model->itemsRemoved(change.index, change.count);
222                         break;
223                     case Change::Moved:
224                         emit model->itemsMoved(change.index, change.to, change.count);
225                         break;
226                     case Change::Changed:
227                         emit model->itemsChanged(change.index, change.count, change.roles);
228                         break;
229                     }
230                 }
231             }
232         }
233
234         syncDone.wakeAll();
235         locker.unlock();
236
237         if (cc)
238             emit m_orig->countChanged();
239         return true;
240     }
241
242     return QObject::event(e);
243 }
244
245 QT_END_NAMESPACE
246