1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QQUICKLISTMODEL_H
43 #define QQUICKLISTMODEL_H
46 #include <private/qqmlcustomparser_p.h>
48 #include <QtCore/QObject>
49 #include <QtCore/QStringList>
50 #include <QtCore/QHash>
51 #include <QtCore/QList>
52 #include <QtCore/QVariant>
53 #include <QtCore/qabstractitemmodel.h>
55 #include <private/qv8engine_p.h>
56 #include <private/qpodvector_p.h>
63 class QQuickListModelWorkerAgent;
67 class Q_QML_PRIVATE_EXPORT QQuickListModel : public QAbstractListModel
70 Q_PROPERTY(int count READ count NOTIFY countChanged)
71 Q_PROPERTY(bool dynamicRoles READ dynamicRoles WRITE setDynamicRoles)
74 QQuickListModel(QObject *parent=0);
77 QModelIndex index(int row, int column, const QModelIndex &parent) const;
78 int rowCount(const QModelIndex &parent) const;
79 QVariant data(const QModelIndex &index, int role) const;
80 QHash<int,QByteArray> roleNames() const;
82 QVariant data(int index, int role) const;
85 Q_INVOKABLE void clear();
86 Q_INVOKABLE void remove(QQmlV8Function *args);
87 Q_INVOKABLE void append(QQmlV8Function *args);
88 Q_INVOKABLE void insert(QQmlV8Function *args);
89 Q_INVOKABLE QQmlV8Handle get(int index) const;
90 Q_INVOKABLE void set(int index, const QQmlV8Handle &);
91 Q_INVOKABLE void setProperty(int index, const QString& property, const QVariant& value);
92 Q_INVOKABLE void move(int from, int to, int count);
93 Q_INVOKABLE void sync();
95 QQuickListModelWorkerAgent *agent();
97 bool dynamicRoles() const { return m_dynamicRoles; }
98 void setDynamicRoles(bool enableDynamicRoles);
104 friend class QQuickListModelParser;
105 friend class QQuickListModelWorkerAgent;
106 friend class ModelObject;
107 friend class ModelNodeMetaObject;
108 friend class ListModel;
109 friend class ListElement;
110 friend class DynamicRoleModelNode;
111 friend class DynamicRoleModelNodeMetaObject;
113 // Constructs a flat list model for a worker agent
114 QQuickListModel(QQuickListModel *orig, QQuickListModelWorkerAgent *agent);
115 QQuickListModel(const QQuickListModel *owner, ListModel *data, QV8Engine *eng, QObject *parent=0);
117 QV8Engine *engine() const;
119 inline bool canMove(int from, int to, int n) const { return !(from+n > count() || to+n > count() || from < 0 || to < 0 || n < 0); }
121 QQuickListModelWorkerAgent *m_agent;
122 mutable QV8Engine *m_engine;
128 ListLayout *m_layout;
129 ListModel *m_listModel;
131 QVector<class DynamicRoleModelNode *> m_modelObjects;
132 QVector<QString> m_roles;
137 ElementSync() : src(0), target(0) {}
139 DynamicRoleModelNode *src;
140 DynamicRoleModelNode *target;
143 int getUid() const { return m_uid; }
145 static void sync(QQuickListModel *src, QQuickListModel *target, QHash<int, QQuickListModel *> *targetModelHash);
146 static QQuickListModel *createWithOwner(QQuickListModel *newOwner);
148 void emitItemsChanged(int index, int count, const QVector<int> &roles);
149 void emitItemsRemoved(int index, int count);
150 void emitItemsInserted(int index, int count);
151 void emitItemsMoved(int from, int to, int n);
155 class QQuickListElement : public QObject
160 class QQuickListModelParser : public QQmlCustomParser
163 QQuickListModelParser() : QQmlCustomParser(QQmlCustomParser::AcceptsSignalHandlers) {}
164 QByteArray compile(const QList<QQmlCustomParserProperty> &);
165 void setCustomData(QObject *, const QByteArray &);
168 struct ListInstruction
170 enum { Push, Pop, Value, Set } type;
177 ListInstruction *instructions() const;
179 bool compileProperty(const QQmlCustomParserProperty &prop, QList<ListInstruction> &instr, QByteArray &data);
181 bool definesEmptyList(const QString &);
183 QString listElementTypeName;
185 struct DataStackElement
187 DataStackElement() : model(0), elementIndex(0) {}
197 QML_DECLARE_TYPE(QQuickListModel)
198 QML_DECLARE_TYPE(QQuickListElement)
202 #endif // QQUICKLISTMODEL_H