Qt 4.8
qdeclarativelistmodel_p_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
64 
66 
67 QT_MODULE(Declarative)
68 
70 class QScriptEngine;
72 struct ModelNode;
74 class FlatNodeData;
75 
77 {
78 public:
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:
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 
112 
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 */
124 {
125 public:
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:
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;
143 {
144 public:
146  : index(i) {}
147 
148  ~FlatNodeData();
149 
150  void addData(FlatNodeObjectData *data);
151  void removeData(FlatNodeObjectData *data);
152 
153  int index;
154 
155 private:
157 };
158 
160 {
161  FlatNodeObjectData(FlatNodeData *data) : nodeData(data) {
162  nodeData->addData(this);
163  }
164 
166  if (nodeData)
167  nodeData->removeData(this);
168  }
169 
171 };
172 
173 
174 
176 {
177 public:
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 
202 
203 private:
204  friend struct ModelNode;
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 
221 
222 private:
224 };
225 
227 {
228 public:
230 
231  bool m_enabled;
232 
233 protected:
234  void propertyWritten(int index);
235 
236 private:
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 
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 
267  bool isArray;
268 
270  int listIndex; // only used for top-level nodes within a list
271 };
272 
273 
275 
277 
279 
280 #endif // QDECLARATIVELISTMODEL_P_P_H
281 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
void setProperty(int index, const QString &property, const QVariant &value, QList< int > *roles)
void move(int from, int to, int count)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void moveNodes(int from, int to, int n)
#define QT_MODULE(x)
Definition: qglobal.h:2783
QHash< QString, ModelNode * > properties
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
ModelNodeMetaObject * m_meta
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QVariant data(int index, int role) const
friend class FlatListScriptClass
bool insert(int index, const QScriptValue &)
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
QSet< FlatNodeObjectData * > objects
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
static const uint base
Definition: qurl.cpp:268
The QScriptDeclarativeClass::Value class acts as a container for JavaScript data types.
void insertedNode(int index)
QDeclarativeListModel * m_listModel
FlatListScriptClass * m_scriptClass
The QScriptEngine class provides an environment for evaluating Qt Script code.
QList< FlatNodeData * > m_nodeData
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool compare(const QVariant::Private *a, const QVariant::Private *b)
Compares a to b.
Definition: qvariant.cpp:383
QDeclarativeListModel * m_listModel
QList< QVariant > values
const char * name
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QList< QHash< int, QVariant > > m_values
FlatNodeObjectData(FlatNodeData *data)
void removedNode(int index)
QHash< int, QString > m_roles
#define Q_OBJECT
Definition: qobjectdefs.h:157
#define Q_DECLARE_METATYPE(TYPE)
This macro makes the type Type known to QMetaType as long as it provides a public default constructor...
Definition: qmetatype.h:265
NestedListModel * m_model
QHash< QString, int > m_strings
FlatListModel(QDeclarativeListModel *base)
QDeclarativeListModel * modelCache
QDeclarativeListModelWorkerAgent * m_parentAgent
const char * property
Definition: qwizard.cpp:138
QList< int > roles() const
static QString dump(const QByteArray &)
quint16 index
QScriptEngine * m_scriptEngine
QString toString(int role) const
ModelObject * objectCache
NestedListModel * m_model
bool addValue(const QScriptValue &value, QHash< int, QVariant > *row, QList< int > *roles)
#define QT_END_HEADER
Definition: qglobal.h:137
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57