Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / util / qdeclarativelistmodel_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
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 <private/qlistmodelinterface_p.h>
54 #include <QtScript/qscriptvalue.h>
55
56 QT_BEGIN_HEADER
57
58 QT_BEGIN_NAMESPACE
59
60 QT_MODULE(Declarative)
61
62 class FlatListModel;
63 class NestedListModel;
64 class QDeclarativeListModelWorkerAgent;
65 struct ModelNode;
66 class FlatListScriptClass;
67 class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeListModel : public QListModelInterface
68 {
69     Q_OBJECT
70     Q_PROPERTY(int count READ count NOTIFY countChanged)
71
72 public:
73     QDeclarativeListModel(QObject *parent=0);
74     ~QDeclarativeListModel();
75
76     virtual QList<int> roles() const;
77     virtual QString toString(int role) const;
78     virtual int count() const;
79     virtual QVariant data(int index, int role) const;
80
81     Q_INVOKABLE void clear();
82     Q_INVOKABLE void remove(int index);
83     Q_INVOKABLE void append(const QScriptValue&);
84     Q_INVOKABLE void insert(int index, const QScriptValue&);
85     Q_INVOKABLE QScriptValue get(int index) const;
86     Q_INVOKABLE void set(int index, const QScriptValue&);
87     Q_INVOKABLE void setProperty(int index, const QString& property, const QVariant& value);
88     Q_INVOKABLE void move(int from, int to, int count);
89     Q_INVOKABLE void sync();
90
91     QDeclarativeListModelWorkerAgent *agent();
92
93 Q_SIGNALS:
94     void countChanged();
95
96 private:
97     friend class QDeclarativeListModelParser;
98     friend class QDeclarativeListModelWorkerAgent;
99     friend class FlatListModel;
100     friend class FlatListScriptClass;
101     friend struct ModelNode;
102
103     // Constructs a flat list model for a worker agent
104     QDeclarativeListModel(const QDeclarativeListModel *orig, QDeclarativeListModelWorkerAgent *parent);
105
106     void set(int index, const QScriptValue&, QList<int> *roles);
107     void setProperty(int index, const QString& property, const QVariant& value, QList<int> *roles);
108
109     bool flatten();
110     bool inWorkerThread() const;
111
112     inline bool canMove(int from, int to, int n) const { return !(from+n > count() || to+n > count() || from < 0 || to < 0 || n < 0); }
113
114     QDeclarativeListModelWorkerAgent *m_agent;
115     NestedListModel *m_nested;
116     FlatListModel *m_flat;
117 };
118
119 // ### FIXME
120 class QDeclarativeListElement : public QObject
121 {
122 Q_OBJECT
123 };
124
125 class QDeclarativeListModelParser : public QDeclarativeCustomParser
126 {
127 public:
128     QByteArray compile(const QList<QDeclarativeCustomParserProperty> &);
129     void setCustomData(QObject *, const QByteArray &);
130
131 private:
132     struct ListInstruction
133     {
134         enum { Push, Pop, Value, Set } type;
135         int dataIdx;
136     };
137     struct ListModelData
138     {
139         int dataOffset;
140         int instrCount;
141         ListInstruction *instructions() const;
142     };
143     bool compileProperty(const QDeclarativeCustomParserProperty &prop, QList<ListInstruction> &instr, QByteArray &data);
144
145     bool definesEmptyList(const QString &);
146
147     QByteArray listElementTypeName;
148 };
149
150
151 QT_END_NAMESPACE
152
153 QML_DECLARE_TYPE(QDeclarativeListModel)
154 QML_DECLARE_TYPE(QDeclarativeListElement)
155
156 QT_END_HEADER
157
158 #endif // QDECLARATIVELISTMODEL_H