Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativelistmodel_p_p.h
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 QtDeclarative 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 #ifndef QDECLARATIVELISTMODEL_P_P_H
43 #define QDECLARATIVELISTMODEL_P_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
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.
52 //
53 // We mean it.
54 //
55
56 #include "qdeclarativelistmodel_p.h"
57 #include <private/qdeclarativeengine_p.h>
58 #include "qdeclarativeopenmetaobject_p.h"
59 #include <qdeclarative.h>
60
61 QT_BEGIN_HEADER
62
63 QT_BEGIN_NAMESPACE
64
65
66 class DynamicRoleModelNode;
67
68 class DynamicRoleModelNodeMetaObject : public QDeclarativeOpenMetaObject
69 {
70 public:
71     DynamicRoleModelNodeMetaObject(DynamicRoleModelNode *object);
72     ~DynamicRoleModelNodeMetaObject();
73
74     bool m_enabled;
75
76 protected:
77     void propertyWrite(int index);
78     void propertyWritten(int index);
79
80 private:
81     DynamicRoleModelNode *m_owner;
82 };
83
84 class DynamicRoleModelNode : public QObject
85 {
86     Q_OBJECT
87 public:
88     DynamicRoleModelNode(QDeclarativeListModel *owner, int uid);
89
90     static DynamicRoleModelNode *create(const QVariantMap &obj, QDeclarativeListModel *owner);
91
92     void updateValues(const QVariantMap &object, QList<int> &roles);
93
94     QVariant getValue(const QString &name)
95     {
96         return m_meta->value(name.toUtf8());
97     }
98
99     bool setValue(const QByteArray &name, const QVariant &val)
100     {
101         return m_meta->setValue(name, val);
102     }
103
104     void setNodeUpdatesEnabled(bool enable)
105     {
106         m_meta->m_enabled = enable;
107     }
108
109     int getUid() const
110     {
111         return m_uid;
112     }
113
114     static void sync(DynamicRoleModelNode *src, DynamicRoleModelNode *target, QHash<int, QDeclarativeListModel *> *targetModelHash);
115
116 private:
117     QDeclarativeListModel *m_owner;
118     int m_uid;
119     DynamicRoleModelNodeMetaObject *m_meta;
120
121     friend class DynamicRoleModelNodeMetaObject;
122 };
123
124 class ModelObject;
125
126 class ModelNodeMetaObject : public QDeclarativeOpenMetaObject
127 {
128 public:
129     ModelNodeMetaObject(ModelObject *object);
130     ~ModelNodeMetaObject();
131
132     bool m_enabled;
133
134 protected:
135     void propertyWritten(int index);
136
137 private:
138
139     ModelObject *m_obj;
140 };
141
142 class ModelObject : public QObject
143 {
144     Q_OBJECT
145 public:
146     ModelObject(QDeclarativeListModel *model, int elementIndex);
147
148     void setValue(const QByteArray &name, const QVariant &val, bool force)
149     {
150         if (force) {
151             QVariant existingValue = m_meta->value(name);
152             if (existingValue.isValid()) {
153                 (*m_meta)[name] = QVariant();
154             }
155         }
156         m_meta->setValue(name, val);
157     }
158
159     void setNodeUpdatesEnabled(bool enable)
160     {
161         m_meta->m_enabled = enable;
162     }
163
164     void updateValues();
165     void updateValues(const QList<int> &roles);
166
167     QDeclarativeListModel *m_model;
168     int m_elementIndex;
169
170 private:
171     ModelNodeMetaObject *m_meta;
172 };
173
174 class ListLayout
175 {
176 public:
177     ListLayout() : currentBlock(0), currentBlockOffset(0) {}
178     ListLayout(const ListLayout *other);
179     ~ListLayout();
180
181     class Role
182     {
183     public:
184
185         Role() : type(Invalid), blockIndex(-1), blockOffset(-1), index(-1), subLayout(0) {}
186         explicit Role(const Role *other);
187         ~Role();
188
189         // This enum must be kept in sync with the roleTypeNames variable in qdeclarativelistmodel.cpp
190         enum DataType
191         {
192             Invalid = -1,
193
194             String,
195             Number,
196             Bool,
197             List,
198             QObject,
199             VariantMap,
200
201             MaxDataType
202         };
203
204         QString name;
205         DataType type;
206         int blockIndex;
207         int blockOffset;
208         int index;
209         ListLayout *subLayout;
210     };
211
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);
215
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);
219
220     int roleCount() const { return roles.count(); }
221
222     static void sync(ListLayout *src, ListLayout *target);
223
224 private:
225     const Role &createRole(const QString &key, Role::DataType type);
226
227     int currentBlock;
228     int currentBlockOffset;
229     QVector<Role *> roles;
230     QStringHash<Role *> roleHash;
231 };
232
233 class ListElement
234 {
235 public:
236
237     ListElement();
238     ListElement(int existingUid);
239     ~ListElement();
240
241     static void sync(ListElement *src, ListLayout *srcLayout, ListElement *target, ListLayout *targetLayout, QHash<int, ListModel *> *targetModelHash);
242
243     enum
244     {
245         BLOCK_SIZE = 64 - sizeof(int) - sizeof(ListElement *) - sizeof(ModelObject *)
246     };
247
248 private:
249
250     void destroy(ListLayout *layout);
251
252     int setVariantProperty(const ListLayout::Role &role, const QVariant &d);
253
254     int setJsProperty(const ListLayout::Role &role, v8::Handle<v8::Value> d, QV8Engine *eng);
255
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);
263
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);
270
271     void clearProperty(const ListLayout::Role &role);
272
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);
279
280     inline char *getPropertyMemory(const ListLayout::Role &role);
281
282     int getUid() const { return uid; }
283
284     char data[BLOCK_SIZE];
285     ListElement *next;
286
287     int uid;
288     ModelObject *m_objectCache;
289
290     friend class ListModel;
291 };
292
293 class ListModel
294 {
295 public:
296
297     ListModel(ListLayout *layout, QDeclarativeListModel *modelCache, int uid);
298     ~ListModel() {}
299
300     void destroy();
301
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);
304
305     QVariant getProperty(int elementIndex, int roleIndex, const QDeclarativeListModel *owner, QV8Engine *eng);
306     ListModel *getListProperty(int elementIndex, const ListLayout::Role &role);
307
308     int roleCount() const
309     {
310         return m_layout->roleCount();
311     }
312
313     const ListLayout::Role &getExistingRole(int index)
314     {
315         return m_layout->getExistingRole(index);
316     }
317
318     const ListLayout::Role &getOrCreateListRole(const QString &name)
319     {
320         return m_layout->getRoleOrCreate(name, ListLayout::Role::List);
321     }
322
323     int elementCount() const
324     {
325         return elements.count();
326     }
327
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);
330
331     int append(v8::Handle<v8::Object> object, QV8Engine *eng);
332     void insert(int elementIndex, v8::Handle<v8::Object> object, QV8Engine *eng);
333
334     void clear();
335     void remove(int index, int count);
336
337     int appendElement();
338     void insertElement(int index);
339
340     void move(int from, int to, int n);
341
342     int getUid() const { return m_uid; }
343
344     static void sync(ListModel *src, ListModel *target, QHash<int, ListModel *> *srcModelHash);
345
346     ModelObject *getOrCreateModelObject(QDeclarativeListModel *model, int elementIndex);
347
348 private:
349     QPODVector<ListElement *, 4> elements;
350     ListLayout *m_layout;
351     int m_uid;
352
353     QDeclarativeListModel *m_modelCache;
354
355     struct ElementSync
356     {
357         ElementSync() : src(0), target(0) {}
358
359         ListElement *src;
360         ListElement *target;
361     };
362
363     void newElement(int index);
364
365     void updateCacheIndices();
366
367     friend class ListElement;
368     friend class QDeclarativeListModelWorkerAgent;
369 };
370
371 QT_END_NAMESPACE
372
373 Q_DECLARE_METATYPE(ListModel *);
374
375 QT_END_HEADER
376
377 #endif // QDECLARATIVELISTMODEL_P_P_H
378