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 QtDeclarative 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 QDECLARATIVELISTMODEL_P_P_H
43 #define QDECLARATIVELISTMODEL_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 "qdeclarativelistmodel_p.h"
57 #include <private/qdeclarativeengine_p.h>
58 #include "qdeclarativeopenmetaobject_p.h"
59 #include <qdeclarative.h>
66 class DynamicRoleModelNode;
68 class DynamicRoleModelNodeMetaObject : public QDeclarativeOpenMetaObject
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(QDeclarativeListModel *owner, int uid);
90 static DynamicRoleModelNode *create(const QVariantMap &obj, QDeclarativeListModel *owner);
92 void updateValues(const QVariantMap &object, QList<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, QDeclarativeListModel *> *targetModelHash);
117 QDeclarativeListModel *m_owner;
119 DynamicRoleModelNodeMetaObject *m_meta;
121 friend class DynamicRoleModelNodeMetaObject;
126 class ModelNodeMetaObject : public QDeclarativeOpenMetaObject
129 ModelNodeMetaObject(ModelObject *object);
130 ~ModelNodeMetaObject();
135 void propertyWritten(int index);
142 class ModelObject : public QObject
146 ModelObject(QDeclarativeListModel *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 QList<int> &roles);
167 QDeclarativeListModel *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
209 ListLayout *subLayout;
212 const Role *getRoleOrCreate(const QString &key, const QVariant &data);
213 const Role &getRoleOrCreate(v8::Handle<v8::String> key, Role::DataType type);
214 const Role &getRoleOrCreate(const QString &key, Role::DataType type);
216 const Role &getExistingRole(int index) { return *roles.at(index); }
217 const Role *getExistingRole(const QString &key);
218 const Role *getExistingRole(v8::Handle<v8::String> key);
220 int roleCount() const { return roles.count(); }
222 static void sync(ListLayout *src, ListLayout *target);
225 const Role &createRole(const QString &key, Role::DataType type);
228 int currentBlockOffset;
229 QVector<Role *> roles;
230 QStringHash<Role *> roleHash;
238 ListElement(int existingUid);
241 static void sync(ListElement *src, ListLayout *srcLayout, ListElement *target, ListLayout *targetLayout, QHash<int, ListModel *> *targetModelHash);
245 BLOCK_SIZE = 64 - sizeof(int) - sizeof(ListElement *) - sizeof(ModelObject *)
250 void destroy(ListLayout *layout);
252 int setVariantProperty(const ListLayout::Role &role, const QVariant &d);
254 int setJsProperty(const ListLayout::Role &role, v8::Handle<v8::Value> d, QV8Engine *eng);
256 int setStringProperty(const ListLayout::Role &role, const QString &s);
257 int setDoubleProperty(const ListLayout::Role &role, double n);
258 int setBoolProperty(const ListLayout::Role &role, bool b);
259 int setListProperty(const ListLayout::Role &role, ListModel *m);
260 int setQObjectProperty(const ListLayout::Role &role, QObject *o);
261 int setVariantMapProperty(const ListLayout::Role &role, v8::Handle<v8::Object> o, QV8Engine *eng);
262 int setVariantMapProperty(const ListLayout::Role &role, QVariantMap *m);
264 void setStringPropertyFast(const ListLayout::Role &role, const QString &s);
265 void setDoublePropertyFast(const ListLayout::Role &role, double n);
266 void setBoolPropertyFast(const ListLayout::Role &role, bool b);
267 void setQObjectPropertyFast(const ListLayout::Role &role, QObject *o);
268 void setListPropertyFast(const ListLayout::Role &role, ListModel *m);
269 void setVariantMapFast(const ListLayout::Role &role, v8::Handle<v8::Object> o, QV8Engine *eng);
271 void clearProperty(const ListLayout::Role &role);
273 QVariant getProperty(const ListLayout::Role &role, const QDeclarativeListModel *owner, QV8Engine *eng);
274 ListModel *getListProperty(const ListLayout::Role &role);
275 QString *getStringProperty(const ListLayout::Role &role);
276 QObject *getQObjectProperty(const ListLayout::Role &role);
277 QDeclarativeGuard<QObject> *getGuardProperty(const ListLayout::Role &role);
278 QVariantMap *getVariantMapProperty(const ListLayout::Role &role);
280 inline char *getPropertyMemory(const ListLayout::Role &role);
282 int getUid() const { return uid; }
284 char data[BLOCK_SIZE];
288 ModelObject *m_objectCache;
290 friend class ListModel;
297 ListModel(ListLayout *layout, QDeclarativeListModel *modelCache, int uid);
302 int setOrCreateProperty(int elementIndex, const QString &key, const QVariant &data);
303 int setExistingProperty(int uid, const QString &key, v8::Handle<v8::Value> data, QV8Engine *eng);
305 QVariant getProperty(int elementIndex, int roleIndex, const QDeclarativeListModel *owner, QV8Engine *eng);
306 ListModel *getListProperty(int elementIndex, const ListLayout::Role &role);
308 int roleCount() const
310 return m_layout->roleCount();
313 const ListLayout::Role &getExistingRole(int index)
315 return m_layout->getExistingRole(index);
318 const ListLayout::Role &getOrCreateListRole(const QString &name)
320 return m_layout->getRoleOrCreate(name, ListLayout::Role::List);
323 int elementCount() const
325 return elements.count();
328 void set(int elementIndex, v8::Handle<v8::Object> object, QList<int> *roles, QV8Engine *eng);
329 void set(int elementIndex, v8::Handle<v8::Object> object, QV8Engine *eng);
331 int append(v8::Handle<v8::Object> object, QV8Engine *eng);
332 void insert(int elementIndex, v8::Handle<v8::Object> object, QV8Engine *eng);
335 void remove(int index, int count);
338 void insertElement(int index);
340 void move(int from, int to, int n);
342 int getUid() const { return m_uid; }
344 static void sync(ListModel *src, ListModel *target, QHash<int, ListModel *> *srcModelHash);
346 ModelObject *getOrCreateModelObject(QDeclarativeListModel *model, int elementIndex);
349 QPODVector<ListElement *, 4> elements;
350 ListLayout *m_layout;
353 QDeclarativeListModel *m_modelCache;
357 ElementSync() : src(0), target(0) {}
363 void newElement(int index);
365 void updateCacheIndices();
367 friend class ListElement;
368 friend class QDeclarativeListModelWorkerAgent;
373 Q_DECLARE_METATYPE(ListModel *);
377 #endif // QDECLARATIVELISTMODEL_P_P_H