Qt 4.8
qdeclarativelistmodelworkeragent.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qdeclarativelistmodelworkeragent_p.h"
43 #include "private/qdeclarativelistmodel_p_p.h"
44 #include "private/qdeclarativedata_p.h"
45 #include "private/qdeclarativeengine_p.h"
46 #include "qdeclarativeinfo.h"
47 
48 #include <QtCore/qcoreevent.h>
49 #include <QtCore/qcoreapplication.h>
50 #include <QtCore/qdebug.h>
51 
52 
54 
55 
57 {
58  changes.clear();
59 }
60 
62 {
64  changes << c;
65 }
66 
68 {
70  changes << c;
71 }
72 
74 {
75  Change c = { Change::Moved, index, count, to, QList<int>() };
76  changes << c;
77 }
78 
80 {
81  Change c = { Change::Changed, index, count, 0, roles };
82  changes << c;
83 }
84 
86  : m_engine(0),
87  m_ref(1),
88  m_orig(model),
89  m_copy(new QDeclarativeListModel(model, this))
90 {
91 }
92 
94 {
95 }
96 
98 {
99  m_engine = eng;
100  if (m_copy->m_flat)
101  m_copy->m_flat->m_scriptEngine = eng;
102 }
103 
105 {
106  return m_engine;
107 }
108 
110 {
111  m_ref.ref();
112 }
113 
115 {
116  bool del = !m_ref.deref();
117 
118  if (del)
119  delete this;
120 }
121 
123 {
124  return m_copy->count();
125 }
126 
128 {
129  data.clearChange();
130  data.removeChange(0, m_copy->count());
131  m_copy->clear();
132 }
133 
135 {
136  int count = m_copy->count();
137  m_copy->remove(index);
138 
139  if (m_copy->count() != count)
140  data.removeChange(index, 1);
141 }
142 
144 {
145  int count = m_copy->count();
146  m_copy->append(value);
147 
148  if (m_copy->count() != count)
149  data.insertChange(m_copy->count() - 1, 1);
150 }
151 
153 {
154  int count = m_copy->count();
155  m_copy->insert(index, value);
156 
157  if (m_copy->count() != count)
158  data.insertChange(index, 1);
159 }
160 
162 {
163  return m_copy->get(index);
164 }
165 
167 {
168  QList<int> roles;
169  m_copy->set(index, value, &roles);
170  if (!roles.isEmpty())
171  data.changedChange(index, 1, roles);
172 }
173 
175 {
176  QList<int> roles;
177  m_copy->setProperty(index, property, value, &roles);
178  if (!roles.isEmpty())
179  data.changedChange(index, 1, roles);
180 }
181 
183 {
184  m_copy->move(from, to, count);
185  data.moveChange(from, to, count);
186 }
187 
189 {
190  Sync *s = new Sync;
191  s->data = data;
192  s->list = m_copy;
193  data.changes.clear();
194 
195  mutex.lock();
197  syncDone.wait(&mutex);
198  mutex.unlock();
199 }
200 
202 {
203  data.changedChange(index, count, roles);
204 }
205 
207 {
208  if (e->type() == QEvent::User) {
209  QMutexLocker locker(&mutex);
210  Sync *s = static_cast<Sync *>(e);
211 
212  const QList<Change> &changes = s->data.changes;
213 
214  if (m_copy) {
215  bool cc = m_orig->count() != s->list->count();
216 
217  FlatListModel *orig = m_orig->m_flat;
218  FlatListModel *copy = s->list->m_flat;
219  if (!orig || !copy) {
220  syncDone.wakeAll();
221  return QObject::event(e);
222  }
223 
224  orig->m_roles = copy->m_roles;
225  orig->m_strings = copy->m_strings;
226  orig->m_values = copy->m_values;
227 
228  // update the orig->m_nodeData list
229  for (int ii = 0; ii < changes.count(); ++ii) {
230  const Change &change = changes.at(ii);
231  switch (change.type) {
232  case Change::Inserted:
233  orig->insertedNode(change.index);
234  break;
235  case Change::Removed:
236  orig->removedNode(change.index);
237  break;
238  case Change::Moved:
239  orig->moveNodes(change.index, change.to, change.count);
240  break;
241  case Change::Changed:
242  break;
243  }
244  }
245 
246  syncDone.wakeAll();
247  locker.unlock();
248 
249  for (int ii = 0; ii < changes.count(); ++ii) {
250  const Change &change = changes.at(ii);
251  switch (change.type) {
252  case Change::Inserted:
253  emit m_orig->itemsInserted(change.index, change.count);
254  break;
255  case Change::Removed:
256  emit m_orig->itemsRemoved(change.index, change.count);
257  break;
258  case Change::Moved:
259  emit m_orig->itemsMoved(change.index, change.to, change.count);
260  break;
261  case Change::Changed:
262  emit m_orig->itemsChanged(change.index, change.count, change.roles);
263  break;
264  }
265  }
266 
267  if (cc)
269  } else {
270  syncDone.wakeAll();
271  }
272  }
273 
274  return QObject::event(e);
275 }
276 
278 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void lock()
Locks the mutex.
Definition: qmutex.cpp:151
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
void moveNodes(int from, int to, int n)
static void postEvent(QObject *receiver, QEvent *event)
Adds the event event, with the object receiver as the receiver of the event, to an event queue and re...
void changedData(int index, int count, const QList< int > &roles)
void unlock()
Unlocks this mutex locker.
Definition: qmutex.h:117
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
bool ref()
Atomically increments the value of this QAtomicInt.
Q_INVOKABLE void move(int from, int to, int count)
The QString class provides a Unicode character string.
Definition: qstring.h:83
enum QDeclarativeListModelWorkerAgent::Change::@153 type
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
void itemsRemoved(int index, int count)
Emit this signal when count items are removed at index.
void insertedNode(int index)
Q_INVOKABLE void move(int from, int to, int count)
Q_INVOKABLE void setProperty(int index, const QString &property, const QVariant &value)
Q_INVOKABLE void set(int index, const QScriptValue &)
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
The QScriptEngine class provides an environment for evaluating Qt Script code.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Q_INVOKABLE void set(int index, const QScriptValue &)
Q_INVOKABLE void append(const QScriptValue &)
Q_INVOKABLE QScriptValue get(int index) const
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
bool deref()
Atomically decrements the value of this QAtomicInt.
QList< QHash< int, QVariant > > m_values
Q_INVOKABLE void setProperty(int index, const QString &property, const QVariant &value)
void removedNode(int index)
QHash< int, QString > m_roles
void unlock()
Unlocks the mutex.
Definition: qmutex.cpp:296
bool wait(QMutex *mutex, unsigned long time=ULONG_MAX)
void itemsChanged(int index, int count, const QList< int > &roles)
Emit this signal when count items at index have had their roles changed.
QHash< QString, int > m_strings
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
Q_INVOKABLE void insert(int index, const QScriptValue &)
void itemsInserted(int index, int count)
Emit this signal when count items are inserted at index.
quint16 index
QVariant property(const char *name) const
Returns the value of the object&#39;s name property.
Definition: qobject.cpp:3807
void itemsMoved(int from, int to, int count)
Emit this signal when count items are moved from index from to index to.
QScriptEngine * m_scriptEngine
Q_INVOKABLE void insert(int index, const QScriptValue &)
void changedChange(int index, int count, const QList< int > &roles)
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
Q_INVOKABLE QScriptValue get(int index) const
Q_INVOKABLE void append(const QScriptValue &)
Q_INVOKABLE void remove(int index)