Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / util / qdeclarativelistmodel_p_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
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 "private/qdeclarativelistmodel_p.h"
57 #include "private/qdeclarativeengine_p.h"
58 #include "private/qdeclarativeopenmetaobject_p.h"
59 #include "qdeclarative.h"
60
61 #include <private/qscriptdeclarativeclass_p.h>
62
63 QT_BEGIN_HEADER
64
65 QT_BEGIN_NAMESPACE
66
67 QT_MODULE(Declarative)
68
69 class QDeclarativeOpenMetaObject;
70 class QScriptEngine;
71 class QDeclarativeListModelWorkerAgent;
72 struct ModelNode;
73 class FlatListScriptClass;
74 class FlatNodeData;
75
76 class FlatListModel
77 {
78 public:
79     FlatListModel(QDeclarativeListModel *base);
80     ~FlatListModel();
81
82     QVariant data(int index, int role) const;
83
84     QList<int> roles() const;
85     QString toString(int role) const;
86
87     int count() const;
88     void clear();
89     void remove(int index);
90     bool insert(int index, const QScriptValue&);
91     QScriptValue get(int index) const;
92     void set(int index, const QScriptValue&, QList<int> *roles);
93     void setProperty(int index, const QString& property, const QVariant& value, QList<int> *roles);
94     void move(int from, int to, int count);
95
96 private:    
97     friend class QDeclarativeListModelWorkerAgent;
98     friend class QDeclarativeListModel;
99     friend class FlatListScriptClass;
100     friend class FlatNodeData;
101
102     bool addValue(const QScriptValue &value, QHash<int, QVariant> *row, QList<int> *roles);
103     void insertedNode(int index);
104     void removedNode(int index);
105     void moveNodes(int from, int to, int n);
106
107     QScriptEngine *m_scriptEngine;
108     QHash<int, QString> m_roles;
109     QHash<QString, int> m_strings;
110     QList<QHash<int, QVariant> > m_values;
111     QDeclarativeListModel *m_listModel;
112
113     FlatListScriptClass *m_scriptClass;
114     QList<FlatNodeData *> m_nodeData;
115     QDeclarativeListModelWorkerAgent *m_parentAgent;
116 };
117
118
119 /*
120     Created when get() is called on a FlatListModel. This allows changes to the
121     object returned by get() to be tracked, and passed onto the model.
122 */
123 class FlatListScriptClass : public QScriptDeclarativeClass
124 {
125 public:
126     FlatListScriptClass(FlatListModel *model, QScriptEngine *seng);
127
128     Value property(Object *, const Identifier &);
129     void setProperty(Object *, const Identifier &name, const QScriptValue &);
130     QScriptClass::QueryFlags queryProperty(Object *, const Identifier &, QScriptClass::QueryFlags flags);
131     bool compare(Object *, Object *);
132
133 private:
134     FlatListModel *m_model;
135 };
136
137 /*
138     FlatNodeData and FlatNodeObjectData allow objects returned by get() to still
139     point to the correct list index if move(), insert() or remove() are called.
140 */
141 struct FlatNodeObjectData;
142 class FlatNodeData
143 {
144 public:
145     FlatNodeData(int i)
146         : index(i) {}
147
148     ~FlatNodeData();
149
150     void addData(FlatNodeObjectData *data);
151     void removeData(FlatNodeObjectData *data);
152
153     int index;
154
155 private:
156     QSet<FlatNodeObjectData*> objects;
157 };
158
159 struct FlatNodeObjectData : public QScriptDeclarativeClass::Object
160 {
161     FlatNodeObjectData(FlatNodeData *data) : nodeData(data) {
162         nodeData->addData(this);
163     }
164
165     ~FlatNodeObjectData() {
166         if (nodeData)
167             nodeData->removeData(this);
168     }
169
170     FlatNodeData *nodeData;
171 };
172
173
174
175 class NestedListModel
176 {
177 public:
178     NestedListModel(QDeclarativeListModel *base);
179     ~NestedListModel();
180
181     QHash<int,QVariant> data(int index, const QList<int> &roles, bool *hasNested = 0) const;
182     QVariant data(int index, int role) const;
183
184     QList<int> roles() const;
185     QString toString(int role) const;
186
187     int count() const;
188     void clear();
189     void remove(int index);
190     bool insert(int index, const QScriptValue&);
191     QScriptValue get(int index) const;
192     void set(int index, const QScriptValue&, QList<int> *roles);
193     void setProperty(int index, const QString& property, const QVariant& value, QList<int> *roles);
194     void move(int from, int to, int count);
195
196     QVariant valueForNode(ModelNode *, bool *hasNested = 0) const;
197     void checkRoles() const;
198
199     ModelNode *_root;
200     bool m_ownsRoot;
201     QDeclarativeListModel *m_listModel;
202
203 private:
204     friend struct ModelNode;
205     mutable QStringList roleStrings;
206     mutable bool _rolesOk;
207 };
208
209
210 class ModelNodeMetaObject;
211 class ModelObject : public QObject
212 {
213     Q_OBJECT
214 public:
215     ModelObject(ModelNode *node, NestedListModel *model, QScriptEngine *seng);
216     void setValue(const QByteArray &name, const QVariant &val);
217     void setNodeUpdatesEnabled(bool enable);
218
219     NestedListModel *m_model;
220     ModelNode *m_node;
221
222 private:
223     ModelNodeMetaObject *m_meta;
224 };
225
226 class ModelNodeMetaObject : public QDeclarativeOpenMetaObject
227 {
228 public:
229     ModelNodeMetaObject(QScriptEngine *seng, ModelObject *object);
230
231     bool m_enabled;
232
233 protected:
234     void propertyWritten(int index);
235
236 private:
237     QScriptEngine *m_seng;
238     ModelObject *m_obj;
239 };
240
241
242 /*
243     A ModelNode is created for each item in a NestedListModel.
244 */
245 struct ModelNode
246 {
247     ModelNode(NestedListModel *model);
248     ~ModelNode();
249
250     QList<QVariant> values;
251     QHash<QString, ModelNode *> properties;
252
253     void clear();
254
255     QDeclarativeListModel *model(const NestedListModel *model);
256     ModelObject *object(const NestedListModel *model);
257
258     bool setObjectValue(const QScriptValue& valuemap, bool writeToCache = true);
259     void setListValue(const QScriptValue& valuelist);
260     bool setProperty(const QString& prop, const QVariant& val);
261     void changedProperty(const QString &name) const;
262     void updateListIndexes();
263     static void dump(ModelNode *node, int ind);
264
265     QDeclarativeListModel *modelCache;
266     ModelObject *objectCache;
267     bool isArray;
268
269     NestedListModel *m_model;
270     int listIndex;  // only used for top-level nodes within a list
271 };
272
273
274 QT_END_NAMESPACE
275
276 Q_DECLARE_METATYPE(ModelNode *)
277
278 QT_END_HEADER
279
280 #endif // QDECLARATIVELISTMODEL_P_P_H
281