1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
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 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #ifndef APPMODEL_H |
52 | #define APPMODEL_H |
53 | |
54 | #include <QtCore/QObject> |
55 | #include <QtCore/QString> |
56 | #include <QtNetwork/QNetworkReply> |
57 | #include <QtQml/QQmlListProperty> |
58 | |
59 | #include <QtPositioning/QGeoPositionInfoSource> |
60 | |
61 | //! [0] |
62 | class WeatherData : public QObject { |
63 | Q_OBJECT |
64 | Q_PROPERTY(QString dayOfWeek |
65 | READ dayOfWeek WRITE setDayOfWeek |
66 | NOTIFY dataChanged) |
67 | Q_PROPERTY(QString weatherIcon |
68 | READ weatherIcon WRITE setWeatherIcon |
69 | NOTIFY dataChanged) |
70 | Q_PROPERTY(QString weatherDescription |
71 | READ weatherDescription WRITE setWeatherDescription |
72 | NOTIFY dataChanged) |
73 | Q_PROPERTY(QString temperature |
74 | READ temperature WRITE setTemperature |
75 | NOTIFY dataChanged) |
76 | |
77 | public: |
78 | explicit WeatherData(QObject *parent = 0); |
79 | WeatherData(const WeatherData &other); |
80 | |
81 | QString dayOfWeek() const; |
82 | QString weatherIcon() const; |
83 | QString weatherDescription() const; |
84 | QString temperature() const; |
85 | |
86 | void setDayOfWeek(const QString &value); |
87 | void setWeatherIcon(const QString &value); |
88 | void setWeatherDescription(const QString &value); |
89 | void setTemperature(const QString &value); |
90 | |
91 | signals: |
92 | void dataChanged(); |
93 | //! [0] |
94 | private: |
95 | QString m_dayOfWeek; |
96 | QString m_weather; |
97 | QString m_weatherDescription; |
98 | QString m_temperature; |
99 | //! [1] |
100 | }; |
101 | //! [1] |
102 | |
103 | Q_DECLARE_METATYPE(WeatherData) |
104 | |
105 | class AppModelPrivate; |
106 | //! [2] |
107 | class AppModel : public QObject |
108 | { |
109 | Q_OBJECT |
110 | Q_PROPERTY(bool ready |
111 | READ ready |
112 | NOTIFY readyChanged) |
113 | Q_PROPERTY(bool hasSource |
114 | READ hasSource |
115 | NOTIFY readyChanged) |
116 | Q_PROPERTY(bool hasValidCity |
117 | READ hasValidCity |
118 | NOTIFY cityChanged) |
119 | Q_PROPERTY(bool hasValidWeather |
120 | READ hasValidWeather |
121 | NOTIFY weatherChanged) |
122 | Q_PROPERTY(bool useGps |
123 | READ useGps WRITE setUseGps |
124 | NOTIFY useGpsChanged) |
125 | Q_PROPERTY(QString city |
126 | READ city WRITE setCity |
127 | NOTIFY cityChanged) |
128 | Q_PROPERTY(WeatherData *weather |
129 | READ weather |
130 | NOTIFY weatherChanged) |
131 | Q_PROPERTY(QQmlListProperty<WeatherData> forecast |
132 | READ forecast |
133 | NOTIFY weatherChanged) |
134 | |
135 | public: |
136 | explicit AppModel(QObject *parent = 0); |
137 | ~AppModel(); |
138 | |
139 | bool ready() const; |
140 | bool hasSource() const; |
141 | bool useGps() const; |
142 | bool hasValidCity() const; |
143 | bool hasValidWeather() const; |
144 | void setUseGps(bool value); |
145 | void hadError(bool tryAgain); |
146 | |
147 | QString city() const; |
148 | void setCity(const QString &value); |
149 | |
150 | WeatherData *weather() const; |
151 | QQmlListProperty<WeatherData> forecast() const; |
152 | |
153 | public slots: |
154 | Q_INVOKABLE void refreshWeather(); |
155 | |
156 | //! [2] |
157 | private slots: |
158 | void queryCity(); |
159 | void positionUpdated(QGeoPositionInfo gpsPos); |
160 | void positionError(QGeoPositionInfoSource::Error e); |
161 | void handleGeoNetworkData(QNetworkReply *networkReply); |
162 | void handleWeatherNetworkData(QNetworkReply *networkReply); |
163 | void handleForecastNetworkData(QNetworkReply *networkReply); |
164 | |
165 | //! [3] |
166 | signals: |
167 | void readyChanged(); |
168 | void useGpsChanged(); |
169 | void cityChanged(); |
170 | void weatherChanged(); |
171 | |
172 | //! [3] |
173 | |
174 | private: |
175 | AppModelPrivate *d; |
176 | |
177 | //! [4] |
178 | }; |
179 | //! [4] |
180 | |
181 | #endif // APPMODEL_H |
182 | |