1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36 
37 #ifndef QDECLARATIVESUPPORTEDCATEGORIESMODEL_H
38 #define QDECLARATIVESUPPORTEDCATEGORIESMODEL_H
39 
40 //
41 //  W A R N I N G
42 //  -------------
43 //
44 // This file is not part of the Qt API.  It exists purely as an
45 // implementation detail.  This header file may change from version to
46 // version without notice, or even be removed.
47 //
48 // We mean it.
49 //
50 
51 #include <QtLocation/private/qlocationglobal_p.h>
52 #include <QtLocation/private/qdeclarativegeoserviceprovider_p.h>
53 
54 #include <QObject>
55 #include <QtCore/QStringList>
56 #include <QtCore/QSharedPointer>
57 #include <QAbstractListModel>
58 #include <QQmlListProperty>
59 #include <QtQml/QQmlParserStatus>
60 
61 #include <QtLocation/QPlaceCategory>
62 
63 #include <QtLocation/private/qdeclarativecategory_p.h>
64 
65 QT_BEGIN_NAMESPACE
66 
67 class QGeoServiceProvider;
68 class QPlaceManager;
69 class QPlaceReply;
70 
71 class PlaceCategoryNode
72 {
73 public:
74     QString parentId;
75     QStringList childIds;
76     QSharedPointer<QDeclarativeCategory> declCategory;
77 };
78 
79 class Q_LOCATION_PRIVATE_EXPORT QDeclarativeSupportedCategoriesModel : public QAbstractItemModel, public QQmlParserStatus
80 {
81     Q_OBJECT
82 
83     Q_ENUMS(Status)
84 
85     Q_PROPERTY(QDeclarativeGeoServiceProvider *plugin READ plugin WRITE setPlugin NOTIFY pluginChanged)
86     Q_PROPERTY(bool hierarchical READ hierarchical WRITE setHierarchical NOTIFY hierarchicalChanged)
87     Q_PROPERTY(Status status READ status NOTIFY statusChanged)
88 
89     Q_INTERFACES(QQmlParserStatus)
90     Q_ENUMS(Roles) //The Roles enum is for internal usage only.
91 
92 public:
93     explicit QDeclarativeSupportedCategoriesModel(QObject *parent = 0);
94     virtual ~QDeclarativeSupportedCategoriesModel();
95 
96     // From QQmlParserStatus
classBegin()97     virtual void classBegin() {}
98     virtual void componentComplete();
99 
100     // From QAbstractItemModel
101     int rowCount(const QModelIndex &parent) const;
102     int columnCount(const QModelIndex &parent) const;
103 
104     QModelIndex index(int row, int column, const QModelIndex &parent) const;
105     QModelIndex parent(const QModelIndex &child) const;
106 
107     Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const;
108     QHash<int, QByteArray> roleNames() const;
109 
110     enum Roles {
111         CategoryRole = Qt::UserRole,
112         ParentCategoryRole
113     };  //for internal usage only
114 
115     enum Status {Null, Ready, Loading, Error};
116 
117     void setPlugin(QDeclarativeGeoServiceProvider *plugin);
118     QDeclarativeGeoServiceProvider *plugin() const;
119 
120     void setHierarchical(bool hierarchical);
121     bool hierarchical() const;
122 
123     Q_INVOKABLE QString errorString() const;
124 
125     Status status() const;
126     void setStatus(Status status, const QString &errorString = QString());
127 
128     using QAbstractItemModel::dataChanged;
129 Q_SIGNALS:
130     void pluginChanged();
131     void hierarchicalChanged();
132     void statusChanged();
133     void dataChanged();
134 
135 public Q_SLOTS:
136     void update();
137 
138 private Q_SLOTS:
139     void replyFinished();
140     void addedCategory(const QPlaceCategory &category, const QString &parentId);
141     void updatedCategory(const QPlaceCategory &category, const QString &parentId);
142     void removedCategory(const QString &categoryId, const QString &parentId);
143     void connectNotificationSignals();
144 
145 private:
146     QStringList populateCategories(QPlaceManager *, const QPlaceCategory &parent);
147     QModelIndex index(const QString &categoryId) const;
148     int rowToAddChild(PlaceCategoryNode *, const QPlaceCategory &category);
149     void updateLayout();
150 
151     QPlaceReply *m_response;
152 
153     QDeclarativeGeoServiceProvider *m_plugin;
154     bool m_hierarchical;
155     bool m_complete;
156     Status m_status;
157     QString m_errorString;
158 
159     QHash<QString, PlaceCategoryNode *> m_categoriesTree;
160 };
161 
162 QT_END_NAMESPACE
163 
164 QML_DECLARE_TYPE(QDeclarativeSupportedCategoriesModel)
165 
166 #endif // QDECLARATIVESUPPORTEDCATEGORIESMODEL_H
167