Docs - add missing images and code, clean up sections
[profile/ivi/qtdeclarative.git] / src / qml / qml / qquicklistmodel_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQUICKLISTMODEL_H
43 #define QQUICKLISTMODEL_H
44
45 #include <qqml.h>
46 #include <private/qqmlcustomparser_p.h>
47
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>
54
55 #include <private/qv8engine_p.h>
56 #include <private/qpodvector_p.h>
57
58 QT_BEGIN_HEADER
59
60 QT_BEGIN_NAMESPACE
61
62
63 class QQuickListModelWorkerAgent;
64 class ListModel;
65 class ListLayout;
66
67 class Q_QML_PRIVATE_EXPORT QQuickListModel : public QAbstractListModel
68 {
69     Q_OBJECT
70     Q_PROPERTY(int count READ count NOTIFY countChanged)
71     Q_PROPERTY(bool dynamicRoles READ dynamicRoles WRITE setDynamicRoles)
72
73 public:
74     QQuickListModel(QObject *parent=0);
75     ~QQuickListModel();
76
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;
81
82     QVariant data(int index, int role) const;
83     int count() const;
84
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();
94
95     QQuickListModelWorkerAgent *agent();
96
97     bool dynamicRoles() const { return m_dynamicRoles; }
98     void setDynamicRoles(bool enableDynamicRoles);
99
100 Q_SIGNALS:
101     void countChanged();
102
103 private:
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;
112
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);
116
117     QV8Engine *engine() const;
118
119     inline bool canMove(int from, int to, int n) const { return !(from+n > count() || to+n > count() || from < 0 || to < 0 || n < 0); }
120
121     QQuickListModelWorkerAgent *m_agent;
122     mutable QV8Engine *m_engine;
123     bool m_mainThread;
124     bool m_primary;
125
126     bool m_dynamicRoles;
127
128     ListLayout *m_layout;
129     ListModel *m_listModel;
130
131     QVector<class DynamicRoleModelNode *> m_modelObjects;
132     QVector<QString> m_roles;
133     int m_uid;
134
135     struct ElementSync
136     {
137         ElementSync() : src(0), target(0) {}
138
139         DynamicRoleModelNode *src;
140         DynamicRoleModelNode *target;
141     };
142
143     int getUid() const { return m_uid; }
144
145     static void sync(QQuickListModel *src, QQuickListModel *target, QHash<int, QQuickListModel *> *targetModelHash);
146     static QQuickListModel *createWithOwner(QQuickListModel *newOwner);
147
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);
152 };
153
154 // ### FIXME
155 class QQuickListElement : public QObject
156 {
157 Q_OBJECT
158 };
159
160 class QQuickListModelParser : public QQmlCustomParser
161 {
162 public:
163     QQuickListModelParser() : QQmlCustomParser(QQmlCustomParser::AcceptsSignalHandlers) {}
164     QByteArray compile(const QList<QQmlCustomParserProperty> &);
165     void setCustomData(QObject *, const QByteArray &);
166
167 private:
168     struct ListInstruction
169     {
170         enum { Push, Pop, Value, Set } type;
171         int dataIdx;
172     };
173     struct ListModelData
174     {
175         int dataOffset;
176         int instrCount;
177         ListInstruction *instructions() const;
178     };
179     bool compileProperty(const QQmlCustomParserProperty &prop, QList<ListInstruction> &instr, QByteArray &data);
180
181     bool definesEmptyList(const QString &);
182
183     QString listElementTypeName;
184
185     struct DataStackElement
186     {
187         DataStackElement() : model(0), elementIndex(0) {}
188
189         QString name;
190         ListModel *model;
191         int elementIndex;
192     };
193 };
194
195 QT_END_NAMESPACE
196
197 QML_DECLARE_TYPE(QQuickListModel)
198 QML_DECLARE_TYPE(QQuickListElement)
199
200 QT_END_HEADER
201
202 #endif // QQUICKLISTMODEL_H