1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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 "private/qdeclarativelistmodel_p.h"
57 #include "private/qdeclarativeengine_p.h"
58 #include "private/qdeclarativeopenmetaobject_p.h"
59 #include "qdeclarative.h"
61 #include <private/qscriptdeclarativeclass_p.h>
67 QT_MODULE(Declarative)
69 class QDeclarativeOpenMetaObject;
71 class QDeclarativeListModelWorkerAgent;
73 class FlatListScriptClass;
79 FlatListModel(QDeclarativeListModel *base);
82 QVariant data(int index, int role) const;
84 QList<int> roles() const;
85 QString toString(int role) const;
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);
97 friend class QDeclarativeListModelWorkerAgent;
98 friend class QDeclarativeListModel;
99 friend class FlatListScriptClass;
100 friend class FlatNodeData;
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);
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;
113 FlatListScriptClass *m_scriptClass;
114 QList<FlatNodeData *> m_nodeData;
115 QDeclarativeListModelWorkerAgent *m_parentAgent;
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.
123 class FlatListScriptClass : public QScriptDeclarativeClass
126 FlatListScriptClass(FlatListModel *model, QScriptEngine *seng);
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 *);
134 FlatListModel *m_model;
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.
141 struct FlatNodeObjectData;
150 void addData(FlatNodeObjectData *data);
151 void removeData(FlatNodeObjectData *data);
156 QSet<FlatNodeObjectData*> objects;
159 struct FlatNodeObjectData : public QScriptDeclarativeClass::Object
161 FlatNodeObjectData(FlatNodeData *data) : nodeData(data) {
162 nodeData->addData(this);
165 ~FlatNodeObjectData() {
167 nodeData->removeData(this);
170 FlatNodeData *nodeData;
175 class NestedListModel
178 NestedListModel(QDeclarativeListModel *base);
181 QHash<int,QVariant> data(int index, const QList<int> &roles, bool *hasNested = 0) const;
182 QVariant data(int index, int role) const;
184 QList<int> roles() const;
185 QString toString(int role) const;
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);
196 QVariant valueForNode(ModelNode *, bool *hasNested = 0) const;
197 void checkRoles() const;
201 QDeclarativeListModel *m_listModel;
204 friend struct ModelNode;
205 mutable QStringList roleStrings;
206 mutable bool _rolesOk;
210 class ModelNodeMetaObject;
211 class ModelObject : public QObject
215 ModelObject(ModelNode *node, NestedListModel *model, QScriptEngine *seng);
216 void setValue(const QByteArray &name, const QVariant &val);
217 void setNodeUpdatesEnabled(bool enable);
219 NestedListModel *m_model;
223 ModelNodeMetaObject *m_meta;
226 class ModelNodeMetaObject : public QDeclarativeOpenMetaObject
229 ModelNodeMetaObject(QScriptEngine *seng, ModelObject *object);
234 void propertyWritten(int index);
237 QScriptEngine *m_seng;
243 A ModelNode is created for each item in a NestedListModel.
247 ModelNode(NestedListModel *model);
250 QList<QVariant> values;
251 QHash<QString, ModelNode *> properties;
255 QDeclarativeListModel *model(const NestedListModel *model);
256 ModelObject *object(const NestedListModel *model);
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);
265 QDeclarativeListModel *modelCache;
266 ModelObject *objectCache;
269 NestedListModel *m_model;
270 int listIndex; // only used for top-level nodes within a list
276 Q_DECLARE_METATYPE(ModelNode *)
280 #endif // QDECLARATIVELISTMODEL_P_P_H