e2e55e5028e221f87c362397e94528797b17ed59
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativelistmodel_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVELISTMODEL_H
43 #define QDECLARATIVELISTMODEL_H
44
45 #include <qdeclarative.h>
46 #include <private/qdeclarativecustomparser_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 "qlistmodelinterface_p.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 QDeclarativeListModelWorkerAgent;
64 class ListModel;
65 class ListLayout;
66
67 class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeListModel : public QListModelInterface
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     QDeclarativeListModel(QObject *parent=0);
75     ~QDeclarativeListModel();
76
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;
81
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();
91
92     QDeclarativeListModelWorkerAgent *agent();
93
94     bool dynamicRoles() const { return m_dynamicRoles; }
95     void setDynamicRoles(bool enableDynamicRoles);
96
97 Q_SIGNALS:
98     void countChanged();
99
100 private:
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;
109
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);
113
114     QV8Engine *engine() const;
115
116     inline bool canMove(int from, int to, int n) const { return !(from+n > count() || to+n > count() || from < 0 || to < 0 || n < 0); }
117
118     QDeclarativeListModelWorkerAgent *m_agent;
119     mutable QV8Engine *m_engine;
120     bool m_mainThread;
121     bool m_primary;
122
123     bool m_dynamicRoles;
124
125     ListLayout *m_layout;
126     ListModel *m_listModel;
127
128     QVector<class DynamicRoleModelNode *> m_modelObjects;
129     QVector<QString> m_roles;
130     int m_uid;
131
132     struct ElementSync
133     {
134         ElementSync() : src(0), target(0) {}
135
136         DynamicRoleModelNode *src;
137         DynamicRoleModelNode *target;
138     };
139
140     int getUid() const { return m_uid; }
141
142     static void sync(QDeclarativeListModel *src, QDeclarativeListModel *target, QHash<int, QDeclarativeListModel *> *targetModelHash);
143     static QDeclarativeListModel *createWithOwner(QDeclarativeListModel *newOwner);
144
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);
149 };
150
151 // ### FIXME
152 class QDeclarativeListElement : public QObject
153 {
154 Q_OBJECT
155 };
156
157 class QDeclarativeListModelParser : public QDeclarativeCustomParser
158 {
159 public:
160     QDeclarativeListModelParser() : QDeclarativeCustomParser(QDeclarativeCustomParser::AcceptsSignalHandlers) {}
161     QByteArray compile(const QList<QDeclarativeCustomParserProperty> &);
162     void setCustomData(QObject *, const QByteArray &);
163
164 private:
165     struct ListInstruction
166     {
167         enum { Push, Pop, Value, Set } type;
168         int dataIdx;
169     };
170     struct ListModelData
171     {
172         int dataOffset;
173         int instrCount;
174         ListInstruction *instructions() const;
175     };
176     bool compileProperty(const QDeclarativeCustomParserProperty &prop, QList<ListInstruction> &instr, QByteArray &data);
177
178     bool definesEmptyList(const QString &);
179
180     QString listElementTypeName;
181
182     struct DataStackElement
183     {
184         DataStackElement() : model(0), elementIndex(0) {}
185
186         QString name;
187         ListModel *model;
188         int elementIndex;
189     };
190 };
191
192 QT_END_NAMESPACE
193
194 QML_DECLARE_TYPE(QDeclarativeListModel)
195 QML_DECLARE_TYPE(QDeclarativeListElement)
196
197 QT_END_HEADER
198
199 #endif // QDECLARATIVELISTMODEL_H