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
51import QtQuick 2.0
52import "components"
53//! [0]
54import WeatherInfo 1.0
55
56Item {
57    id: window
58//! [0]
59    width: 360
60    height: 640
61
62    state: "loading"
63
64    states: [
65        State {
66            name: "loading"
67            PropertyChanges { target: main; opacity: 0 }
68            PropertyChanges { target: wait; opacity: 1 }
69        },
70        State {
71            name: "ready"
72            PropertyChanges { target: main; opacity: 1 }
73            PropertyChanges { target: wait; opacity: 0 }
74        }
75    ]
76//! [1]
77    AppModel {
78        id: model
79        onReadyChanged: {
80            if (model.ready)
81                window.state = "ready"
82            else
83                window.state = "loading"
84        }
85    }
86//! [1]
87    Item {
88        id: wait
89        anchors.fill: parent
90
91        Text {
92            text: "Loading weather data..."
93            anchors.centerIn: parent
94            font.pointSize: 18
95        }
96    }
97
98    Item {
99        id: main
100        anchors.fill: parent
101
102        Column {
103            spacing: 6
104
105            anchors {
106                fill: parent
107                topMargin: 6; bottomMargin: 6; leftMargin: 6; rightMargin: 6
108            }
109
110            Rectangle {
111                width: parent.width
112                height: 25
113                color: "lightgrey"
114
115                Text {
116                    text: (model.hasValidCity ? model.city : "Unknown location") + (model.useGps ? " (GPS)" : "")
117                    anchors.fill: parent
118                    horizontalAlignment: Text.AlignHCenter
119                    verticalAlignment: Text.AlignVCenter
120                }
121
122                MouseArea {
123                    anchors.fill: parent
124                    onClicked: {
125                        if (model.useGps) {
126                            model.useGps = false
127                            model.city = "Brisbane"
128                        } else {
129                            switch (model.city) {
130                            case "Brisbane":
131                                model.city = "Oslo"
132                                break
133                            case "Oslo":
134                                model.city = "Helsinki"
135                                break
136                            case "Helsinki":
137                                model.city = "New York"
138                                break
139                            case "New York":
140                                model.useGps = true
141                                break
142                            }
143                        }
144                    }
145                }
146            }
147
148//! [3]
149            BigForecastIcon {
150                id: current
151
152                width: main.width -12
153                height: 2 * (main.height - 25 - 12) / 3
154
155                weatherIcon: (model.hasValidWeather
156                          ? model.weather.weatherIcon
157                          : "01d")
158//! [3]
159                topText: (model.hasValidWeather
160                          ? model.weather.temperature
161                          : "??")
162                bottomText: (model.hasValidWeather
163                             ? model.weather.weatherDescription
164                             : "No weather data")
165
166                MouseArea {
167                    anchors.fill: parent
168                    onClicked: {
169                        model.refreshWeather()
170                    }
171                }
172//! [4]
173            }
174//! [4]
175
176            Row {
177                id: iconRow
178                spacing: 6
179
180                width: main.width - 12
181                height: (main.height - 25 - 24) / 3
182
183                property real iconWidth: iconRow.width / 4 - 10
184                property real iconHeight: iconRow.height
185
186                ForecastIcon {
187                    id: forecast1
188                    width: iconRow.iconWidth
189                    height: iconRow.iconHeight
190
191                    topText: (model.hasValidWeather ?
192                              model.forecast[0].dayOfWeek : "??")
193                    bottomText: (model.hasValidWeather ?
194                                 model.forecast[0].temperature : "??/??")
195                    weatherIcon: (model.hasValidWeather ?
196                              model.forecast[0].weatherIcon : "01d")
197                }
198                ForecastIcon {
199                    id: forecast2
200                    width: iconRow.iconWidth
201                    height: iconRow.iconHeight
202
203                    topText: (model.hasValidWeather ?
204                              model.forecast[1].dayOfWeek : "??")
205                    bottomText: (model.hasValidWeather ?
206                                 model.forecast[1].temperature : "??/??")
207                    weatherIcon: (model.hasValidWeather ?
208                              model.forecast[1].weatherIcon : "01d")
209                }
210                ForecastIcon {
211                    id: forecast3
212                    width: iconRow.iconWidth
213                    height: iconRow.iconHeight
214
215                    topText: (model.hasValidWeather ?
216                              model.forecast[2].dayOfWeek : "??")
217                    bottomText: (model.hasValidWeather ?
218                                 model.forecast[2].temperature : "??/??")
219                    weatherIcon: (model.hasValidWeather ?
220                              model.forecast[2].weatherIcon : "01d")
221                }
222                ForecastIcon {
223                    id: forecast4
224                    width: iconRow.iconWidth
225                    height: iconRow.iconHeight
226
227                    topText: (model.hasValidWeather ?
228                              model.forecast[3].dayOfWeek : "??")
229                    bottomText: (model.hasValidWeather ?
230                                 model.forecast[3].temperature : "??/??")
231                    weatherIcon: (model.hasValidWeather ?
232                              model.forecast[3].weatherIcon : "01d")
233                }
234
235            }
236        }
237    }
238//! [2]
239}
240//! [2]
241