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 QDECLARATIVEXMLLISTMODEL_H
43 #define QDECLARATIVEXMLLISTMODEL_H
45 #include <qdeclarative.h>
46 #include <qdeclarativeinfo.h>
48 #include <QtCore/qurl.h>
49 #include <QtCore/qstringlist.h>
50 #include <QtScript/qscriptvalue.h>
52 #include <private/qlistmodelinterface_p.h>
58 QT_MODULE(Declarative)
60 class QDeclarativeContext;
61 class QDeclarativeXmlListModelRole;
62 class QDeclarativeXmlListModelPrivate;
64 struct QDeclarativeXmlQueryResult {
67 QList<QList<QVariant> > data;
68 QList<QPair<int, int> > inserted;
69 QList<QPair<int, int> > removed;
70 QStringList keyRoleResultsCache;
73 class Q_AUTOTEST_EXPORT QDeclarativeXmlListModel : public QListModelInterface, public QDeclarativeParserStatus
76 Q_INTERFACES(QDeclarativeParserStatus)
79 Q_PROPERTY(Status status READ status NOTIFY statusChanged)
80 Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
81 Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
82 Q_PROPERTY(QString xml READ xml WRITE setXml NOTIFY xmlChanged)
83 Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged)
84 Q_PROPERTY(QString namespaceDeclarations READ namespaceDeclarations WRITE setNamespaceDeclarations NOTIFY namespaceDeclarationsChanged)
85 Q_PROPERTY(QDeclarativeListProperty<QDeclarativeXmlListModelRole> roles READ roleObjects)
86 Q_PROPERTY(int count READ count NOTIFY countChanged)
87 Q_CLASSINFO("DefaultProperty", "roles")
90 QDeclarativeXmlListModel(QObject *parent = 0);
91 ~QDeclarativeXmlListModel();
93 virtual QHash<int,QVariant> data(int index, const QList<int> &roles = (QList<int>())) const;
94 virtual QVariant data(int index, int role) const;
95 virtual int count() const;
96 virtual QList<int> roles() const;
97 virtual QString toString(int role) const;
99 QDeclarativeListProperty<QDeclarativeXmlListModelRole> roleObjects();
102 void setSource(const QUrl&);
105 void setXml(const QString&);
107 QString query() const;
108 void setQuery(const QString&);
110 QString namespaceDeclarations() const;
111 void setNamespaceDeclarations(const QString&);
113 Q_INVOKABLE QScriptValue get(int index) const;
115 enum Status { Null, Ready, Loading, Error };
116 Status status() const;
117 qreal progress() const;
119 Q_INVOKABLE QString errorString() const;
121 virtual void classBegin();
122 virtual void componentComplete();
125 void statusChanged(QDeclarativeXmlListModel::Status);
126 void progressChanged(qreal progress);
128 void sourceChanged();
131 void namespaceDeclarationsChanged();
134 // ### need to use/expose Expiry to guess when to call this?
135 // ### property to auto-call this on reasonable Expiry?
136 // ### LastModified/Age also useful to guess.
137 // ### Probably also applies to other network-requesting types.
141 void requestFinished();
142 void requestProgress(qint64,qint64);
144 void queryCompleted(const QDeclarativeXmlQueryResult &);
145 void queryError(void* object, const QString& error);
148 Q_DECLARE_PRIVATE(QDeclarativeXmlListModel)
149 Q_DISABLE_COPY(QDeclarativeXmlListModel)
152 class Q_AUTOTEST_EXPORT QDeclarativeXmlListModelRole : public QObject
155 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
156 Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged)
157 Q_PROPERTY(bool isKey READ isKey WRITE setIsKey NOTIFY isKeyChanged)
159 QDeclarativeXmlListModelRole() : m_isKey(false) {}
160 ~QDeclarativeXmlListModelRole() {}
162 QString name() const { return m_name; }
163 void setName(const QString &name) {
170 QString query() const { return m_query; }
171 void setQuery(const QString &query)
173 if (query.startsWith(QLatin1Char('/'))) {
174 qmlInfo(this) << tr("An XmlRole query must not start with '/'");
177 if (m_query == query)
183 bool isKey() const { return m_isKey; }
184 void setIsKey(bool b) {
192 return !m_name.isEmpty() && !m_query.isEmpty();
208 QML_DECLARE_TYPE(QDeclarativeXmlListModel)
209 QML_DECLARE_TYPE(QDeclarativeXmlListModelRole)
213 #endif // QDECLARATIVEXMLLISTMODEL_H