1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QQUICKLISTMODEL_P_P_H
43 #define QQUICKLISTMODEL_P_P_H
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include "qquicklistmodel_p.h"
57 #include <private/qqmlengine_p.h>
58 #include "qqmlopenmetaobject_p.h"
66 class DynamicRoleModelNode;
68 class DynamicRoleModelNodeMetaObject : public QQmlOpenMetaObject
71 DynamicRoleModelNodeMetaObject(DynamicRoleModelNode *object);
72 ~DynamicRoleModelNodeMetaObject();
77 void propertyWrite(int index);
78 void propertyWritten(int index);
81 DynamicRoleModelNode *m_owner;
84 class DynamicRoleModelNode : public QObject
88 DynamicRoleModelNode(QQuickListModel *owner, int uid);
90 static DynamicRoleModelNode *create(const QVariantMap &obj, QQuickListModel *owner);
92 void updateValues(const QVariantMap &object, QVector<int> &roles);
94 QVariant getValue(const QString &name)
96 return m_meta->value(name.toUtf8());
99 bool setValue(const QByteArray &name, const QVariant &val)
101 return m_meta->setValue(name, val);
104 void setNodeUpdatesEnabled(bool enable)
106 m_meta->m_enabled = enable;
114 static void sync(DynamicRoleModelNode *src, DynamicRoleModelNode *target, QHash<int, QQuickListModel *> *targetModelHash);
117 QQuickListModel *m_owner;
119 DynamicRoleModelNodeMetaObject *m_meta;
121 friend class DynamicRoleModelNodeMetaObject;
126 class ModelNodeMetaObject : public QQmlOpenMetaObject
129 ModelNodeMetaObject(ModelObject *object);
130 ~ModelNodeMetaObject();
135 void propertyWritten(int index);
142 class ModelObject : public QObject
146 ModelObject(QQuickListModel *model, int elementIndex);
148 void setValue(const QByteArray &name, const QVariant &val, bool force)
151 QVariant existingValue = m_meta->value(name);
152 if (existingValue.isValid()) {
153 (*m_meta)[name] = QVariant();
156 m_meta->setValue(name, val);
159 void setNodeUpdatesEnabled(bool enable)
161 m_meta->m_enabled = enable;
165 void updateValues(const QVector<int> &roles);
167 QQuickListModel *m_model;
171 ModelNodeMetaObject *m_meta;
177 ListLayout() : currentBlock(0), currentBlockOffset(0) {}
178 ListLayout(const ListLayout *other);
185 Role() : type(Invalid), blockIndex(-1), blockOffset(-1), index(-1), subLayout(0) {}
186 explicit Role(const Role *other);
189 // This enum must be kept in sync with the roleTypeNames variable in qdeclarativelistmodel.cpp
210 ListLayout *subLayout;
213 const Role *getRoleOrCreate(const QString &key, const QVariant &data);
214 const Role &getRoleOrCreate(v8::Handle<v8::String> key, Role::DataType type);
215 const Role &getRoleOrCreate(const QString &key, Role::DataType type);
217 const Role &getExistingRole(int index) { return *roles.at(index); }
218 const Role *getExistingRole(const QString &key);
219 const Role *getExistingRole(v8::Handle<v8::String> key);
221 int roleCount() const { return roles.count(); }
223 static void sync(ListLayout *src, ListLayout *target);
226 const Role &createRole(const QString &key, Role::DataType type);
229 int currentBlockOffset;
230 QVector<Role *> roles;
231 QStringHash<Role *> roleHash;
239 ListElement(int existingUid);
242 static void sync(ListElement *src, ListLayout *srcLayout, ListElement *target, ListLayout *targetLayout, QHash<int, ListModel *> *targetModelHash);
246 BLOCK_SIZE = 64 - sizeof(int) - sizeof(ListElement *) - sizeof(ModelObject *)
251 void destroy(ListLayout *layout);
253 int setVariantProperty(const ListLayout::Role &role, const QVariant &d);
255 int setJsProperty(const ListLayout::Role &role, v8::Handle<v8::Value> d, QV8Engine *eng);
257 int setStringProperty(const ListLayout::Role &role, const QString &s);
258 int setDoubleProperty(const ListLayout::Role &role, double n);
259 int setBoolProperty(const ListLayout::Role &role, bool b);
260 int setListProperty(const ListLayout::Role &role, ListModel *m);
261 int setQObjectProperty(const ListLayout::Role &role, QObject *o);
262 int setVariantMapProperty(const ListLayout::Role &role, v8::Handle<v8::Object> o, QV8Engine *eng);
263 int setVariantMapProperty(const ListLayout::Role &role, QVariantMap *m);
264 int setDateTimeProperty(const ListLayout::Role &role, const QDateTime &dt);
266 void setStringPropertyFast(const ListLayout::Role &role, const QString &s);
267 void setDoublePropertyFast(const ListLayout::Role &role, double n);
268 void setBoolPropertyFast(const ListLayout::Role &role, bool b);
269 void setQObjectPropertyFast(const ListLayout::Role &role, QObject *o);
270 void setListPropertyFast(const ListLayout::Role &role, ListModel *m);
271 void setVariantMapFast(const ListLayout::Role &role, v8::Handle<v8::Object> o, QV8Engine *eng);
272 void setDateTimePropertyFast(const ListLayout::Role &role, const QDateTime &dt);
274 void clearProperty(const ListLayout::Role &role);
276 QVariant getProperty(const ListLayout::Role &role, const QQuickListModel *owner, QV8Engine *eng);
277 ListModel *getListProperty(const ListLayout::Role &role);
278 QString *getStringProperty(const ListLayout::Role &role);
279 QObject *getQObjectProperty(const ListLayout::Role &role);
280 QQmlGuard<QObject> *getGuardProperty(const ListLayout::Role &role);
281 QVariantMap *getVariantMapProperty(const ListLayout::Role &role);
282 QDateTime *getDateTimeProperty(const ListLayout::Role &role);
284 inline char *getPropertyMemory(const ListLayout::Role &role);
286 int getUid() const { return uid; }
288 char data[BLOCK_SIZE];
292 ModelObject *m_objectCache;
294 friend class ListModel;
301 ListModel(ListLayout *layout, QQuickListModel *modelCache, int uid);
306 int setOrCreateProperty(int elementIndex, const QString &key, const QVariant &data);
307 int setExistingProperty(int uid, const QString &key, v8::Handle<v8::Value> data, QV8Engine *eng);
309 QVariant getProperty(int elementIndex, int roleIndex, const QQuickListModel *owner, QV8Engine *eng);
310 ListModel *getListProperty(int elementIndex, const ListLayout::Role &role);
312 int roleCount() const
314 return m_layout->roleCount();
317 const ListLayout::Role &getExistingRole(int index)
319 return m_layout->getExistingRole(index);
322 const ListLayout::Role &getOrCreateListRole(const QString &name)
324 return m_layout->getRoleOrCreate(name, ListLayout::Role::List);
327 int elementCount() const
329 return elements.count();
332 void set(int elementIndex, v8::Handle<v8::Object> object, QVector<int> *roles, QV8Engine *eng);
333 void set(int elementIndex, v8::Handle<v8::Object> object, QV8Engine *eng);
335 int append(v8::Handle<v8::Object> object, QV8Engine *eng);
336 void insert(int elementIndex, v8::Handle<v8::Object> object, QV8Engine *eng);
339 void remove(int index, int count);
342 void insertElement(int index);
344 void move(int from, int to, int n);
346 int getUid() const { return m_uid; }
348 static void sync(ListModel *src, ListModel *target, QHash<int, ListModel *> *srcModelHash);
350 ModelObject *getOrCreateModelObject(QQuickListModel *model, int elementIndex);
353 QPODVector<ListElement *, 4> elements;
354 ListLayout *m_layout;
357 QQuickListModel *m_modelCache;
361 ElementSync() : src(0), target(0) {}
367 void newElement(int index);
369 void updateCacheIndices();
371 friend class ListElement;
372 friend class QQuickListModelWorkerAgent;
377 Q_DECLARE_METATYPE(ListModel *);
381 #endif // QQUICKLISTMODEL_P_P_H