1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** 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 <QtDeclarative/private/qdeclarativeengine_p.h>
57 #include "QtQuick1/private/qdeclarativelistmodel_p.h"
58 #include "QtQuick1/private/qdeclarativeopenmetaobject_p.h"
59 #include <QtDeclarative/qdeclarative.h>
61 #include <QtDeclarative/private/qscriptdeclarativeclass_p.h>
70 class QDeclarative1OpenMetaObject;
71 class QDeclarative1ListModelWorkerAgent;
73 class FlatListScriptClass_1;
79 FlatListModel_1(QDeclarative1ListModel *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 QDeclarative1ListModelWorkerAgent;
98 friend class QDeclarative1ListModel;
99 friend class FlatListScriptClass_1;
100 friend class FlatNodeData_1;
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 QDeclarative1ListModel *m_listModel;
113 FlatListScriptClass_1 *m_scriptClass;
114 QList<FlatNodeData_1 *> m_nodeData;
115 QDeclarative1ListModelWorkerAgent *m_parentAgent;
120 Created when get() is called on a FlatListModel_1. This allows changes to the
121 object returned by get() to be tracked, and passed onto the model.
123 class FlatListScriptClass_1 : public QScriptDeclarativeClass
126 FlatListScriptClass_1(FlatListModel_1 *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_1 *m_model;
138 FlatNodeData_1 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;
145 FlatNodeData_1(int i)
150 void addData(FlatNodeObjectData *data);
151 void removeData(FlatNodeObjectData *data);
156 QSet<FlatNodeObjectData*> objects;
159 struct FlatNodeObjectData : public QScriptDeclarativeClass::Object
161 FlatNodeObjectData(FlatNodeData_1 *data) : nodeData(data) {
162 nodeData->addData(this);
165 ~FlatNodeObjectData() {
167 nodeData->removeData(this);
170 FlatNodeData_1 *nodeData;
175 class NestedListModel_1
178 NestedListModel_1(QDeclarative1ListModel *base);
179 ~NestedListModel_1();
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 QDeclarative1ListModel *m_listModel;
204 friend struct ModelNode;
205 mutable QStringList roleStrings;
206 mutable bool _rolesOk;
210 class ModelNodeMetaObject_1;
211 class ModelObject_1 : public QObject
215 ModelObject_1(ModelNode *node, NestedListModel_1 *model, QScriptEngine *seng);
216 void setValue(const QByteArray &name, const QVariant &val);
217 void setNodeUpdatesEnabled(bool enable);
219 NestedListModel_1 *m_model;
223 ModelNodeMetaObject_1 *m_meta;
226 class ModelNodeMetaObject_1 : public QDeclarative1OpenMetaObject
229 ModelNodeMetaObject_1(QScriptEngine *seng, ModelObject_1 *object);
234 void propertyWritten(int index);
237 QScriptEngine *m_seng;
238 ModelObject_1 *m_obj;
243 A ModelNode is created for each item in a NestedListModel_1.
247 ModelNode(NestedListModel_1 *model);
250 QList<QVariant> values;
251 QHash<QString, ModelNode *> properties;
255 QDeclarative1ListModel *model(const NestedListModel_1 *model);
256 ModelObject_1 *object(const NestedListModel_1 *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 QDeclarative1ListModel *modelCache;
266 ModelObject_1 *objectCache;
269 NestedListModel_1 *m_model;
270 int listIndex; // only used for top-level nodes within a list
275 Q_DECLARE_METATYPE(ModelNode *)
279 #endif // QDECLARATIVELISTMODEL_P_P_H