1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #ifndef QQUICKXMLLISTMODEL_H
43 #define QQUICKXMLLISTMODEL_H
48 #include <QtCore/qurl.h>
49 #include <QtCore/qstringlist.h>
50 #include <QtCore/qabstractitemmodel.h>
51 #include <private/qv8engine_p.h>
59 class QQuickXmlListModelRole;
60 class QQuickXmlListModelPrivate;
62 struct QQuickXmlQueryResult {
65 QList<QList<QVariant> > data;
66 QList<QPair<int, int> > inserted;
67 QList<QPair<int, int> > removed;
68 QStringList keyRoleResultsCache;
71 class QQuickXmlListModel : public QAbstractListModel, public QQmlParserStatus
74 Q_INTERFACES(QQmlParserStatus)
77 Q_PROPERTY(Status status READ status NOTIFY statusChanged)
78 Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged)
79 Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
80 Q_PROPERTY(QString xml READ xml WRITE setXml NOTIFY xmlChanged)
81 Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged)
82 Q_PROPERTY(QString namespaceDeclarations READ namespaceDeclarations WRITE setNamespaceDeclarations NOTIFY namespaceDeclarationsChanged)
83 Q_PROPERTY(QQmlListProperty<QQuickXmlListModelRole> roles READ roleObjects)
84 Q_PROPERTY(int count READ count NOTIFY countChanged)
85 Q_CLASSINFO("DefaultProperty", "roles")
88 QQuickXmlListModel(QObject *parent = 0);
89 ~QQuickXmlListModel();
91 QModelIndex index(int row, int column, const QModelIndex &parent) const;
92 int rowCount(const QModelIndex &parent) const;
93 QVariant data(const QModelIndex &index, int role) const;
94 QHash<int, QByteArray> roleNames() const;
98 QQmlListProperty<QQuickXmlListModelRole> 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 QQmlV8Handle 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(QQuickXmlListModel::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 QQuickXmlQueryResult &);
144 void queryError(void* object, const QString& error);
147 Q_DECLARE_PRIVATE(QQuickXmlListModel)
148 Q_DISABLE_COPY(QQuickXmlListModel)
151 class QQuickXmlListModelRole : 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 QQuickXmlListModelRole() : m_isKey(false) {}
159 ~QQuickXmlListModelRole() {}
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(QQuickXmlListModel)
208 QML_DECLARE_TYPE(QQuickXmlListModelRole)
212 #endif // QQUICKXMLLISTMODEL_H