1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml module of the Qt Toolkit. |
7 | ** |
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 The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
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 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QQUICKXMLLISTMODEL_H |
41 | #define QQUICKXMLLISTMODEL_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <qqml.h> |
55 | #include <qqmlinfo.h> |
56 | #include <qjsvalue.h> |
57 | |
58 | #include <QtCore/qurl.h> |
59 | #include <QtCore/qstringlist.h> |
60 | #include <QtCore/qabstractitemmodel.h> |
61 | |
62 | QT_BEGIN_NAMESPACE |
63 | |
64 | |
65 | class QQmlContext; |
66 | class QQuickXmlListModelRole; |
67 | class QQuickXmlListModelPrivate; |
68 | |
69 | struct QQuickXmlQueryResult { |
70 | int queryId; |
71 | int size; |
72 | QList<QList<QVariant> > data; |
73 | QList<QPair<int, int> > inserted; |
74 | QList<QPair<int, int> > removed; |
75 | QStringList keyRoleResultsCache; |
76 | }; |
77 | |
78 | class QQuickXmlListModel : public QAbstractListModel, public QQmlParserStatus |
79 | { |
80 | Q_OBJECT |
81 | Q_INTERFACES(QQmlParserStatus) |
82 | |
83 | Q_PROPERTY(Status status READ status NOTIFY statusChanged) |
84 | Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged) |
85 | Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) |
86 | Q_PROPERTY(QString xml READ xml WRITE setXml NOTIFY xmlChanged) |
87 | Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged) |
88 | Q_PROPERTY(QString namespaceDeclarations READ namespaceDeclarations WRITE setNamespaceDeclarations NOTIFY namespaceDeclarationsChanged) |
89 | Q_PROPERTY(QQmlListProperty<QQuickXmlListModelRole> roles READ roleObjects) |
90 | Q_PROPERTY(int count READ count NOTIFY countChanged) |
91 | Q_CLASSINFO("DefaultProperty" , "roles" ) |
92 | |
93 | public: |
94 | QQuickXmlListModel(QObject *parent = 0); |
95 | ~QQuickXmlListModel(); |
96 | |
97 | QModelIndex index(int row, int column, const QModelIndex &parent) const override; |
98 | int rowCount(const QModelIndex &parent) const override; |
99 | QVariant data(const QModelIndex &index, int role) const override; |
100 | QHash<int, QByteArray> roleNames() const override; |
101 | |
102 | int count() const; |
103 | |
104 | QQmlListProperty<QQuickXmlListModelRole> roleObjects(); |
105 | |
106 | QUrl source() const; |
107 | void setSource(const QUrl&); |
108 | |
109 | QString xml() const; |
110 | void setXml(const QString&); |
111 | |
112 | QString query() const; |
113 | void setQuery(const QString&); |
114 | |
115 | QString namespaceDeclarations() const; |
116 | void setNamespaceDeclarations(const QString&); |
117 | |
118 | Q_INVOKABLE QJSValue get(int index) const; |
119 | |
120 | enum Status { Null, Ready, Loading, Error }; |
121 | Q_ENUM(Status) |
122 | Status status() const; |
123 | qreal progress() const; |
124 | |
125 | Q_INVOKABLE QString errorString() const; |
126 | |
127 | void classBegin() override; |
128 | void componentComplete() override; |
129 | |
130 | Q_SIGNALS: |
131 | void statusChanged(QQuickXmlListModel::Status); |
132 | void progressChanged(qreal progress); |
133 | void countChanged(); |
134 | void sourceChanged(); |
135 | void xmlChanged(); |
136 | void queryChanged(); |
137 | void namespaceDeclarationsChanged(); |
138 | |
139 | public Q_SLOTS: |
140 | // ### need to use/expose Expiry to guess when to call this? |
141 | // ### property to auto-call this on reasonable Expiry? |
142 | // ### LastModified/Age also useful to guess. |
143 | // ### Probably also applies to other network-requesting types. |
144 | void reload(); |
145 | |
146 | private Q_SLOTS: |
147 | #if QT_CONFIG(qml_network) |
148 | void requestFinished(); |
149 | #endif |
150 | void requestProgress(qint64,qint64); |
151 | void dataCleared(); |
152 | void queryCompleted(const QQuickXmlQueryResult &); |
153 | void queryError(void* object, const QString& error); |
154 | |
155 | private: |
156 | Q_DECLARE_PRIVATE(QQuickXmlListModel) |
157 | Q_DISABLE_COPY(QQuickXmlListModel) |
158 | }; |
159 | |
160 | class QQuickXmlListModelRole : public QObject |
161 | { |
162 | Q_OBJECT |
163 | Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) |
164 | Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged) |
165 | Q_PROPERTY(bool isKey READ isKey WRITE setIsKey NOTIFY isKeyChanged) |
166 | public: |
167 | QQuickXmlListModelRole() : m_isKey(false) {} |
168 | ~QQuickXmlListModelRole() {} |
169 | |
170 | QString name() const { return m_name; } |
171 | void setName(const QString &name) { |
172 | if (name == m_name) |
173 | return; |
174 | m_name = name; |
175 | Q_EMIT nameChanged(); |
176 | } |
177 | |
178 | QString query() const { return m_query; } |
179 | void setQuery(const QString &query) |
180 | { |
181 | if (query.startsWith(c: QLatin1Char('/'))) { |
182 | qmlWarning(me: this) << tr(s: "An XmlRole query must not start with '/'" ); |
183 | return; |
184 | } |
185 | if (m_query == query) |
186 | return; |
187 | m_query = query; |
188 | Q_EMIT queryChanged(); |
189 | } |
190 | |
191 | bool isKey() const { return m_isKey; } |
192 | void setIsKey(bool b) { |
193 | if (m_isKey == b) |
194 | return; |
195 | m_isKey = b; |
196 | Q_EMIT isKeyChanged(); |
197 | } |
198 | |
199 | bool isValid() const { |
200 | return !m_name.isEmpty() && !m_query.isEmpty(); |
201 | } |
202 | |
203 | Q_SIGNALS: |
204 | void nameChanged(); |
205 | void queryChanged(); |
206 | void isKeyChanged(); |
207 | |
208 | private: |
209 | QString m_name; |
210 | QString m_query; |
211 | bool m_isKey; |
212 | }; |
213 | |
214 | QT_END_NAMESPACE |
215 | |
216 | QML_DECLARE_TYPE(QQuickXmlListModel) |
217 | QML_DECLARE_TYPE(QQuickXmlListModelRole) |
218 | |
219 | #endif // QQUICKXMLLISTMODEL_H |
220 | |