1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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 <private/qlistmodelinterface_p.h>
54 #include <QtScript/qscriptvalue.h>
60 QT_MODULE(Declarative)
63 class NestedListModel;
64 class QDeclarativeListModelWorkerAgent;
66 class FlatListScriptClass;
67 class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeListModel : public QListModelInterface
70 Q_PROPERTY(int count READ count NOTIFY countChanged)
73 QDeclarativeListModel(QObject *parent=0);
74 ~QDeclarativeListModel();
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;
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();
91 QDeclarativeListModelWorkerAgent *agent();
97 friend class QDeclarativeListModelParser;
98 friend class QDeclarativeListModelWorkerAgent;
99 friend class FlatListModel;
100 friend class FlatListScriptClass;
101 friend struct ModelNode;
103 // Constructs a flat list model for a worker agent
104 QDeclarativeListModel(const QDeclarativeListModel *orig, QDeclarativeListModelWorkerAgent *parent);
106 void set(int index, const QScriptValue&, QList<int> *roles);
107 void setProperty(int index, const QString& property, const QVariant& value, QList<int> *roles);
110 bool inWorkerThread() const;
112 inline bool canMove(int from, int to, int n) const { return !(from+n > count() || to+n > count() || from < 0 || to < 0 || n < 0); }
114 QDeclarativeListModelWorkerAgent *m_agent;
115 NestedListModel *m_nested;
116 FlatListModel *m_flat;
120 class QDeclarativeListElement : public QObject
125 class QDeclarativeListModelParser : public QDeclarativeCustomParser
128 QByteArray compile(const QList<QDeclarativeCustomParserProperty> &);
129 void setCustomData(QObject *, const QByteArray &);
132 struct ListInstruction
134 enum { Push, Pop, Value, Set } type;
141 ListInstruction *instructions() const;
143 bool compileProperty(const QDeclarativeCustomParserProperty &prop, QList<ListInstruction> &instr, QByteArray &data);
145 bool definesEmptyList(const QString &);
147 QByteArray listElementTypeName;
153 QML_DECLARE_TYPE(QDeclarativeListModel)
154 QML_DECLARE_TYPE(QDeclarativeListElement)
158 #endif // QDECLARATIVELISTMODEL_H