1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
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>
51 #include <private/qlistmodelinterface_p.h>
52 #include <private/qv8engine_p.h>
59 class QDeclarativeContext;
60 class QDeclarativeXmlListModelRole;
61 class QDeclarativeXmlListModelPrivate;
63 struct QDeclarativeXmlQueryResult {
66 QList<QList<QVariant> > data;
67 QList<QPair<int, int> > inserted;
68 QList<QPair<int, int> > removed;
69 QStringList keyRoleResultsCache;
72 class QDeclarativeXmlListModel : public QListModelInterface, public QDeclarativeParserStatus
75 Q_INTERFACES(QDeclarativeParserStatus)
78 Q_PROPERTY(Status status READ status NOTIFY statusChanged)
79 Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
80 Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
81 Q_PROPERTY(QString xml READ xml WRITE setXml NOTIFY xmlChanged)
82 Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged)
83 Q_PROPERTY(QString namespaceDeclarations READ namespaceDeclarations WRITE setNamespaceDeclarations NOTIFY namespaceDeclarationsChanged)
84 Q_PROPERTY(QDeclarativeListProperty<QDeclarativeXmlListModelRole> roles READ roleObjects)
85 Q_PROPERTY(int count READ count NOTIFY countChanged)
86 Q_CLASSINFO("DefaultProperty", "roles")
89 QDeclarativeXmlListModel(QObject *parent = 0);
90 ~QDeclarativeXmlListModel();
92 virtual QHash<int,QVariant> data(int index, const QList<int> &roles = (QList<int>())) const;
93 virtual QVariant data(int index, int role) const;
94 virtual int count() const;
95 virtual QList<int> roles() const;
96 virtual QString toString(int role) const;
98 QDeclarativeListProperty<QDeclarativeXmlListModelRole> roleObjects();
101 void setSource(const QUrl&);
104 void setXml(const QString&);
106 QString query() const;
107 void setQuery(const QString&);
109 QString namespaceDeclarations() const;
110 void setNamespaceDeclarations(const QString&);
112 Q_INVOKABLE QDeclarativeV8Handle get(int index) const;
114 enum Status { Null, Ready, Loading, Error };
115 Status status() const;
116 qreal progress() const;
118 Q_INVOKABLE QString errorString() const;
120 virtual void classBegin();
121 virtual void componentComplete();
124 void statusChanged(QDeclarativeXmlListModel::Status);
125 void progressChanged(qreal progress);
127 void sourceChanged();
130 void namespaceDeclarationsChanged();
133 // ### need to use/expose Expiry to guess when to call this?
134 // ### property to auto-call this on reasonable Expiry?
135 // ### LastModified/Age also useful to guess.
136 // ### Probably also applies to other network-requesting types.
140 void requestFinished();
141 void requestProgress(qint64,qint64);
143 void queryCompleted(const QDeclarativeXmlQueryResult &);
144 void queryError(void* object, const QString& error);
147 Q_DECLARE_PRIVATE(QDeclarativeXmlListModel)
148 Q_DISABLE_COPY(QDeclarativeXmlListModel)
151 class QDeclarativeXmlListModelRole : public QObject
154 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
155 Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged)
156 Q_PROPERTY(bool isKey READ isKey WRITE setIsKey NOTIFY isKeyChanged)
158 QDeclarativeXmlListModelRole() : m_isKey(false) {}
159 ~QDeclarativeXmlListModelRole() {}
161 QString name() const { return m_name; }
162 void setName(const QString &name) {
169 QString query() const { return m_query; }
170 void setQuery(const QString &query)
172 if (query.startsWith(QLatin1Char('/'))) {
173 qmlInfo(this) << tr("An XmlRole query must not start with '/'");
176 if (m_query == query)
182 bool isKey() const { return m_isKey; }
183 void setIsKey(bool b) {
191 return !m_name.isEmpty() && !m_query.isEmpty();
207 QML_DECLARE_TYPE(QDeclarativeXmlListModel)
208 QML_DECLARE_TYPE(QDeclarativeXmlListModelRole)
212 #endif // QDECLARATIVEXMLLISTMODEL_H