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_H
43 #define QDECLARATIVELISTMODEL_H
45 #include <qdeclarative.h>
46 #include <private/qdeclarativecustomparser_p.h>
48 #include <QtCore/QObject>
49 #include <QtCore/QStringList>
50 #include <QtCore/QHash>
51 #include <QtCore/QList>
52 #include <QtCore/QVariant>
53 #include "qlistmodelinterface_p.h"
55 #include <private/qv8engine_p.h>
56 #include <private/qpodvector_p.h>
63 class QDeclarativeListModelWorkerAgent;
67 class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeListModel : public QListModelInterface
70 Q_PROPERTY(int count READ count NOTIFY countChanged)
71 Q_PROPERTY(bool dynamicRoles READ dynamicRoles WRITE setDynamicRoles)
74 QDeclarativeListModel(QObject *parent=0);
75 ~QDeclarativeListModel();
77 virtual QList<int> roles() const;
78 virtual QString toString(int role) const;
79 virtual int count() const;
80 virtual QVariant data(int index, int role) const;
82 Q_INVOKABLE void clear();
83 Q_INVOKABLE void remove(QDeclarativeV8Function *args);
84 Q_INVOKABLE void append(QDeclarativeV8Function *args);
85 Q_INVOKABLE void insert(QDeclarativeV8Function *args);
86 Q_INVOKABLE QDeclarativeV8Handle get(int index) const;
87 Q_INVOKABLE void set(int index, const QDeclarativeV8Handle &);
88 Q_INVOKABLE void setProperty(int index, const QString& property, const QVariant& value);
89 Q_INVOKABLE void move(int from, int to, int count);
90 Q_INVOKABLE void sync();
92 QDeclarativeListModelWorkerAgent *agent();
94 bool dynamicRoles() const { return m_dynamicRoles; }
95 void setDynamicRoles(bool enableDynamicRoles);
101 friend class QDeclarativeListModelParser;
102 friend class QDeclarativeListModelWorkerAgent;
103 friend class ModelObject;
104 friend class ModelNodeMetaObject;
105 friend class ListModel;
106 friend class ListElement;
107 friend class DynamicRoleModelNode;
108 friend class DynamicRoleModelNodeMetaObject;
110 // Constructs a flat list model for a worker agent
111 QDeclarativeListModel(QDeclarativeListModel *orig, QDeclarativeListModelWorkerAgent *agent);
112 QDeclarativeListModel(const QDeclarativeListModel *owner, ListModel *data, QV8Engine *eng, QObject *parent=0);
114 QV8Engine *engine() const;
116 inline bool canMove(int from, int to, int n) const { return !(from+n > count() || to+n > count() || from < 0 || to < 0 || n < 0); }
118 QDeclarativeListModelWorkerAgent *m_agent;
119 mutable QV8Engine *m_engine;
125 ListLayout *m_layout;
126 ListModel *m_listModel;
128 QVector<class DynamicRoleModelNode *> m_modelObjects;
129 QVector<QString> m_roles;
134 ElementSync() : src(0), target(0) {}
136 DynamicRoleModelNode *src;
137 DynamicRoleModelNode *target;
140 int getUid() const { return m_uid; }
142 static void sync(QDeclarativeListModel *src, QDeclarativeListModel *target, QHash<int, QDeclarativeListModel *> *targetModelHash);
143 static QDeclarativeListModel *createWithOwner(QDeclarativeListModel *newOwner);
145 void emitItemsChanged(int index, int count, const QList<int> &roles);
146 void emitItemsRemoved(int index, int count);
147 void emitItemsInserted(int index, int count);
148 void emitItemsMoved(int from, int to, int n);
152 class QDeclarativeListElement : public QObject
157 class QDeclarativeListModelParser : public QDeclarativeCustomParser
160 QDeclarativeListModelParser() : QDeclarativeCustomParser(QDeclarativeCustomParser::AcceptsSignalHandlers) {}
161 QByteArray compile(const QList<QDeclarativeCustomParserProperty> &);
162 void setCustomData(QObject *, const QByteArray &);
165 struct ListInstruction
167 enum { Push, Pop, Value, Set } type;
174 ListInstruction *instructions() const;
176 bool compileProperty(const QDeclarativeCustomParserProperty &prop, QList<ListInstruction> &instr, QByteArray &data);
178 bool definesEmptyList(const QString &);
180 QString listElementTypeName;
182 struct DataStackElement
184 DataStackElement() : model(0), elementIndex(0) {}
194 QML_DECLARE_TYPE(QDeclarativeListModel)
195 QML_DECLARE_TYPE(QDeclarativeListElement)
199 #endif // QDECLARATIVELISTMODEL_H