/usr/share/qt5/doc/qtpositioning/qtpositioning-weatherinfo-appmodel-cpp.html is in qtlocation5-doc-html 5.2.1-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>appmodel.cpp Example File (weatherinfo/appmodel.cpp) | QtPositioning 5.2</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li>Qt 5.2</li>
<li><a href="qtpositioning-index.html">Qt Positioning</a></li>
<li>appmodel.cpp Example File (weatherinfo/appmodel.cpp)</li>
<li id="buildversion">
Qt 5.2.1 Reference Documentation</li>
</ul>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<h1 class="title">appmodel.cpp Example File</h1>
<span class="small-subtitle">weatherinfo/appmodel.cpp</span>
<!-- $$$weatherinfo/appmodel.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp"><span class="comment">/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/</span>
<span class="preprocessor">#include "appmodel.h"</span>
<span class="preprocessor">#include <qgeopositioninfosource.h></span>
<span class="preprocessor">#include <qgeosatelliteinfosource.h></span>
<span class="preprocessor">#include <qnmeapositioninfosource.h></span>
<span class="preprocessor">#include <qgeopositioninfo.h></span>
<span class="preprocessor">#include <qnetworkconfigmanager.h></span>
<span class="preprocessor">#include <qnetworksession.h></span>
<span class="preprocessor">#include <QSignalMapper></span>
<span class="preprocessor">#include <QJsonDocument></span>
<span class="preprocessor">#include <QJsonObject></span>
<span class="preprocessor">#include <QJsonArray></span>
<span class="preprocessor">#include <QStringList></span>
<span class="preprocessor">#include <QUrlQuery></span>
<span class="preprocessor">#include <QElapsedTimer></span>
<span class="comment">/*
*This application uses http://openweathermap.org/api
**/</span>
<span class="preprocessor">#define ZERO_KELVIN 273.15</span>
WeatherData<span class="operator">::</span>WeatherData(<span class="type">QObject</span> <span class="operator">*</span>parent) :
<span class="type">QObject</span>(parent)
{
}
WeatherData<span class="operator">::</span>WeatherData(<span class="keyword">const</span> WeatherData <span class="operator">&</span>other) :
<span class="type">QObject</span>(<span class="number">0</span>)<span class="operator">,</span>
m_dayOfWeek(other<span class="operator">.</span>m_dayOfWeek)<span class="operator">,</span>
m_weather(other<span class="operator">.</span>m_weather)<span class="operator">,</span>
m_weatherDescription(other<span class="operator">.</span>m_weatherDescription)<span class="operator">,</span>
m_temperature(other<span class="operator">.</span>m_temperature)
{
}
<span class="type">QString</span> WeatherData<span class="operator">::</span>dayOfWeek() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_dayOfWeek;
}
<span class="comment">/*!
* The icon value is based on OpenWeatherMap.org icon set. For details
* see http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes
*
* e.g. 01d ->sunny day
*
* The icon string will be translated to
* http://openweathermap.org/img/w/01d.png
*/</span>
<span class="type">QString</span> WeatherData<span class="operator">::</span>weatherIcon() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_weather;
}
<span class="type">QString</span> WeatherData<span class="operator">::</span>weatherDescription() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_weatherDescription;
}
<span class="type">QString</span> WeatherData<span class="operator">::</span>temperature() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_temperature;
}
<span class="type">void</span> WeatherData<span class="operator">::</span>setDayOfWeek(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>value)
{
m_dayOfWeek <span class="operator">=</span> value;
<span class="keyword">emit</span> dataChanged();
}
<span class="type">void</span> WeatherData<span class="operator">::</span>setWeatherIcon(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>value)
{
m_weather <span class="operator">=</span> value;
<span class="keyword">emit</span> dataChanged();
}
<span class="type">void</span> WeatherData<span class="operator">::</span>setWeatherDescription(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>value)
{
m_weatherDescription <span class="operator">=</span> value;
<span class="keyword">emit</span> dataChanged();
}
<span class="type">void</span> WeatherData<span class="operator">::</span>setTemperature(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>value)
{
m_temperature <span class="operator">=</span> value;
<span class="keyword">emit</span> dataChanged();
}
<span class="keyword">class</span> AppModelPrivate
{
<span class="keyword">public</span>:
<span class="type"><a href="qgeopositioninfosource.html">QGeoPositionInfoSource</a></span> <span class="operator">*</span>src;
<span class="type"><a href="qgeocoordinate.html">QGeoCoordinate</a></span> coord;
<span class="type">QString</span> city;
<span class="type">QNetworkAccessManager</span> <span class="operator">*</span>nam;
<span class="type">QNetworkSession</span> <span class="operator">*</span>ns;
WeatherData now;
<span class="type">QList</span><span class="operator"><</span>WeatherData<span class="operator">*</span><span class="operator">></span> forecast;
<span class="type">QQmlListProperty</span><span class="operator"><</span>WeatherData<span class="operator">></span> <span class="operator">*</span>fcProp;
<span class="type">QSignalMapper</span> <span class="operator">*</span>geoReplyMapper;
<span class="type">QSignalMapper</span> <span class="operator">*</span>weatherReplyMapper<span class="operator">,</span> <span class="operator">*</span>forecastReplyMapper;
bool ready;
bool useGps;
<span class="type">QElapsedTimer</span> throttle;
AppModelPrivate() :
src(NULL)<span class="operator">,</span>
nam(NULL)<span class="operator">,</span>
ns(NULL)<span class="operator">,</span>
fcProp(NULL)<span class="operator">,</span>
ready(<span class="keyword">false</span>)<span class="operator">,</span>
useGps(<span class="keyword">true</span>)
{}
};
<span class="keyword">static</span> <span class="type">void</span> forecastAppend(<span class="type">QQmlListProperty</span><span class="operator"><</span>WeatherData<span class="operator">></span> <span class="operator">*</span>prop<span class="operator">,</span> WeatherData <span class="operator">*</span>val)
{
Q_UNUSED(val);
Q_UNUSED(prop);
}
<span class="keyword">static</span> WeatherData <span class="operator">*</span>forecastAt(<span class="type">QQmlListProperty</span><span class="operator"><</span>WeatherData<span class="operator">></span> <span class="operator">*</span>prop<span class="operator">,</span> <span class="type">int</span> index)
{
AppModelPrivate <span class="operator">*</span>d <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator"><</span>AppModelPrivate<span class="operator">*</span><span class="operator">></span>(prop<span class="operator">-</span><span class="operator">></span>data);
<span class="keyword">return</span> d<span class="operator">-</span><span class="operator">></span>forecast<span class="operator">.</span>at(index);
}
<span class="keyword">static</span> <span class="type">int</span> forecastCount(<span class="type">QQmlListProperty</span><span class="operator"><</span>WeatherData<span class="operator">></span> <span class="operator">*</span>prop)
{
AppModelPrivate <span class="operator">*</span>d <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator"><</span>AppModelPrivate<span class="operator">*</span><span class="operator">></span>(prop<span class="operator">-</span><span class="operator">></span>data);
<span class="keyword">return</span> d<span class="operator">-</span><span class="operator">></span>forecast<span class="operator">.</span>size();
}
<span class="keyword">static</span> <span class="type">void</span> forecastClear(<span class="type">QQmlListProperty</span><span class="operator"><</span>WeatherData<span class="operator">></span> <span class="operator">*</span>prop)
{
<span class="keyword">static_cast</span><span class="operator"><</span>AppModelPrivate<span class="operator">*</span><span class="operator">></span>(prop<span class="operator">-</span><span class="operator">></span>data)<span class="operator">-</span><span class="operator">></span>forecast<span class="operator">.</span>clear();
}
AppModel<span class="operator">::</span>AppModel(<span class="type">QObject</span> <span class="operator">*</span>parent) :
<span class="type">QObject</span>(parent)<span class="operator">,</span>
d(<span class="keyword">new</span> AppModelPrivate)
{
d<span class="operator">-</span><span class="operator">></span>fcProp <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QQmlListProperty</span><span class="operator"><</span>WeatherData<span class="operator">></span>(<span class="keyword">this</span><span class="operator">,</span> d<span class="operator">,</span>
forecastAppend<span class="operator">,</span>
forecastCount<span class="operator">,</span>
forecastAt<span class="operator">,</span>
forecastClear);
d<span class="operator">-</span><span class="operator">></span>geoReplyMapper <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QSignalMapper</span>(<span class="keyword">this</span>);
d<span class="operator">-</span><span class="operator">></span>weatherReplyMapper <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QSignalMapper</span>(<span class="keyword">this</span>);
d<span class="operator">-</span><span class="operator">></span>forecastReplyMapper <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QSignalMapper</span>(<span class="keyword">this</span>);
connect(d<span class="operator">-</span><span class="operator">></span>geoReplyMapper<span class="operator">,</span> SIGNAL(mapped(<span class="type">QObject</span><span class="operator">*</span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(handleGeoNetworkData(<span class="type">QObject</span><span class="operator">*</span>)));
connect(d<span class="operator">-</span><span class="operator">></span>weatherReplyMapper<span class="operator">,</span> SIGNAL(mapped(<span class="type">QObject</span><span class="operator">*</span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(handleWeatherNetworkData(<span class="type">QObject</span><span class="operator">*</span>)));
connect(d<span class="operator">-</span><span class="operator">></span>forecastReplyMapper<span class="operator">,</span> SIGNAL(mapped(<span class="type">QObject</span><span class="operator">*</span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(handleForecastNetworkData(<span class="type">QObject</span><span class="operator">*</span>)));
<span class="comment">// make sure we have an active network session</span>
d<span class="operator">-</span><span class="operator">></span>nam <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QNetworkAccessManager</span>(<span class="keyword">this</span>);
<span class="type">QNetworkConfigurationManager</span> ncm;
d<span class="operator">-</span><span class="operator">></span>ns <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QNetworkSession</span>(ncm<span class="operator">.</span>defaultConfiguration()<span class="operator">,</span> <span class="keyword">this</span>);
connect(d<span class="operator">-</span><span class="operator">></span>ns<span class="operator">,</span> SIGNAL(opened())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(networkSessionOpened()));
<span class="comment">// the session may be already open. if it is, run the slot directly</span>
<span class="keyword">if</span> (d<span class="operator">-</span><span class="operator">></span>ns<span class="operator">-</span><span class="operator">></span>isOpen())
<span class="keyword">this</span><span class="operator">-</span><span class="operator">></span>networkSessionOpened();
<span class="comment">// tell the system we want network</span>
d<span class="operator">-</span><span class="operator">></span>ns<span class="operator">-</span><span class="operator">></span>open();
}
AppModel<span class="operator">::</span><span class="operator">~</span>AppModel()
{
d<span class="operator">-</span><span class="operator">></span>ns<span class="operator">-</span><span class="operator">></span>close();
<span class="keyword">if</span> (d<span class="operator">-</span><span class="operator">></span>src)
d<span class="operator">-</span><span class="operator">></span>src<span class="operator">-</span><span class="operator">></span>stopUpdates();
<span class="keyword">delete</span> d;
}
<span class="type">void</span> AppModel<span class="operator">::</span>networkSessionOpened()
{
d<span class="operator">-</span><span class="operator">></span>src <span class="operator">=</span> <span class="type"><a href="qgeopositioninfosource.html">QGeoPositionInfoSource</a></span><span class="operator">::</span>createDefaultSource(<span class="keyword">this</span>);
<span class="keyword">if</span> (d<span class="operator">-</span><span class="operator">></span>src) {
d<span class="operator">-</span><span class="operator">></span>useGps <span class="operator">=</span> <span class="keyword">true</span>;
connect(d<span class="operator">-</span><span class="operator">></span>src<span class="operator">,</span> SIGNAL(positionUpdated(<span class="type"><a href="qgeopositioninfo.html">QGeoPositionInfo</a></span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(positionUpdated(<span class="type"><a href="qgeopositioninfo.html">QGeoPositionInfo</a></span>)));
d<span class="operator">-</span><span class="operator">></span>src<span class="operator">-</span><span class="operator">></span>startUpdates();
} <span class="keyword">else</span> {
d<span class="operator">-</span><span class="operator">></span>useGps <span class="operator">=</span> <span class="keyword">false</span>;
d<span class="operator">-</span><span class="operator">></span>city <span class="operator">=</span> <span class="string">"Brisbane"</span>;
<span class="keyword">emit</span> cityChanged();
<span class="keyword">this</span><span class="operator">-</span><span class="operator">></span>refreshWeather();
}
}
<span class="type">void</span> AppModel<span class="operator">::</span>positionUpdated(<span class="type"><a href="qgeopositioninfo.html">QGeoPositionInfo</a></span> gpsPos)
{
d<span class="operator">-</span><span class="operator">></span>coord <span class="operator">=</span> gpsPos<span class="operator">.</span>coordinate();
<span class="keyword">if</span> (<span class="operator">!</span>(d<span class="operator">-</span><span class="operator">></span>useGps))
<span class="keyword">return</span>;
<span class="type">QString</span> latitude<span class="operator">,</span> longitude;
longitude<span class="operator">.</span>setNum(d<span class="operator">-</span><span class="operator">></span>coord<span class="operator">.</span>longitude());
latitude<span class="operator">.</span>setNum(d<span class="operator">-</span><span class="operator">></span>coord<span class="operator">.</span>latitude());
<span class="comment">//don't update more often then once a minute</span>
<span class="comment">//to keep load on server low</span>
<span class="keyword">if</span> (d<span class="operator">-</span><span class="operator">></span>throttle<span class="operator">.</span>isValid() <span class="operator">&</span><span class="operator">&</span> d<span class="operator">-</span><span class="operator">></span>throttle<span class="operator">.</span>elapsed() <span class="operator"><</span> <span class="number">1000</span><span class="operator">*</span><span class="number">10</span> ) {
<span class="keyword">return</span>;
}
d<span class="operator">-</span><span class="operator">></span>throttle<span class="operator">.</span>restart();
<span class="type">QUrl</span> url(<span class="string">"http://api.openweathermap.org/data/2.5/weather"</span>);
<span class="type">QUrlQuery</span> query;
query<span class="operator">.</span>addQueryItem(<span class="string">"lat"</span><span class="operator">,</span> latitude);
query<span class="operator">.</span>addQueryItem(<span class="string">"lon"</span><span class="operator">,</span> longitude);
query<span class="operator">.</span>addQueryItem(<span class="string">"mode"</span><span class="operator">,</span> <span class="string">"json"</span>);
url<span class="operator">.</span>setQuery(query);
<span class="type">QNetworkReply</span> <span class="operator">*</span>rep <span class="operator">=</span> d<span class="operator">-</span><span class="operator">></span>nam<span class="operator">-</span><span class="operator">></span>get(<span class="type">QNetworkRequest</span>(url));
<span class="comment">// connect up the signal right away</span>
d<span class="operator">-</span><span class="operator">></span>geoReplyMapper<span class="operator">-</span><span class="operator">></span>setMapping(rep<span class="operator">,</span> rep);
connect(rep<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span>
d<span class="operator">-</span><span class="operator">></span>geoReplyMapper<span class="operator">,</span> SLOT(map()));
}
<span class="type">void</span> AppModel<span class="operator">::</span>handleGeoNetworkData(<span class="type">QObject</span> <span class="operator">*</span>replyObj)
{
<span class="type">QNetworkReply</span> <span class="operator">*</span>networkReply <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type">QNetworkReply</span><span class="operator">*</span><span class="operator">></span>(replyObj);
<span class="keyword">if</span> (<span class="operator">!</span>networkReply)
<span class="keyword">return</span>;
<span class="keyword">if</span> (<span class="operator">!</span>networkReply<span class="operator">-</span><span class="operator">></span>error()) {
<span class="comment">//convert coordinates to city name</span>
<span class="type">QJsonDocument</span> document <span class="operator">=</span> <span class="type">QJsonDocument</span><span class="operator">::</span>fromJson(networkReply<span class="operator">-</span><span class="operator">></span>readAll());
<span class="type">QJsonObject</span> jo <span class="operator">=</span> document<span class="operator">.</span>object();
<span class="type">QJsonValue</span> jv <span class="operator">=</span> jo<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"name"</span>));
<span class="keyword">const</span> <span class="type">QString</span> city <span class="operator">=</span> jv<span class="operator">.</span>toString();
<span class="keyword">if</span> (city <span class="operator">!</span><span class="operator">=</span> d<span class="operator">-</span><span class="operator">></span>city) {
<span class="keyword">if</span> (<span class="operator">!</span>d<span class="operator">-</span><span class="operator">></span>throttle<span class="operator">.</span>isValid())
d<span class="operator">-</span><span class="operator">></span>throttle<span class="operator">.</span>start();
d<span class="operator">-</span><span class="operator">></span>city <span class="operator">=</span> city;
<span class="keyword">emit</span> cityChanged();
refreshWeather();
}
}
networkReply<span class="operator">-</span><span class="operator">></span>deleteLater();
}
<span class="type">void</span> AppModel<span class="operator">::</span>refreshWeather()
{
<span class="type">QUrl</span> url(<span class="string">"http://api.openweathermap.org/data/2.5/weather"</span>);
<span class="type">QUrlQuery</span> query;
query<span class="operator">.</span>addQueryItem(<span class="string">"q"</span><span class="operator">,</span> d<span class="operator">-</span><span class="operator">></span>city);
query<span class="operator">.</span>addQueryItem(<span class="string">"mode"</span><span class="operator">,</span> <span class="string">"json"</span>);
url<span class="operator">.</span>setQuery(query);
<span class="type">QNetworkReply</span> <span class="operator">*</span>rep <span class="operator">=</span> d<span class="operator">-</span><span class="operator">></span>nam<span class="operator">-</span><span class="operator">></span>get(<span class="type">QNetworkRequest</span>(url));
<span class="comment">// connect up the signal right away</span>
d<span class="operator">-</span><span class="operator">></span>weatherReplyMapper<span class="operator">-</span><span class="operator">></span>setMapping(rep<span class="operator">,</span> rep);
connect(rep<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span>
d<span class="operator">-</span><span class="operator">></span>weatherReplyMapper<span class="operator">,</span> SLOT(map()));
}
<span class="keyword">static</span> <span class="type">QString</span> niceTemperatureString(<span class="type">double</span> t)
{
<span class="keyword">return</span> <span class="type">QString</span><span class="operator">::</span>number(qRound(t<span class="operator">-</span>ZERO_KELVIN)) <span class="operator">+</span> <span class="type">QChar</span>(<span class="number">0xB0</span>);
}
<span class="type">void</span> AppModel<span class="operator">::</span>handleWeatherNetworkData(<span class="type">QObject</span> <span class="operator">*</span>replyObj)
{
<span class="type">QNetworkReply</span> <span class="operator">*</span>networkReply <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type">QNetworkReply</span><span class="operator">*</span><span class="operator">></span>(replyObj);
<span class="keyword">if</span> (<span class="operator">!</span>networkReply)
<span class="keyword">return</span>;
<span class="keyword">if</span> (<span class="operator">!</span>networkReply<span class="operator">-</span><span class="operator">></span>error()) {
foreach (WeatherData <span class="operator">*</span>inf<span class="operator">,</span> d<span class="operator">-</span><span class="operator">></span>forecast)
<span class="keyword">delete</span> inf;
d<span class="operator">-</span><span class="operator">></span>forecast<span class="operator">.</span>clear();
<span class="type">QJsonDocument</span> document <span class="operator">=</span> <span class="type">QJsonDocument</span><span class="operator">::</span>fromJson(networkReply<span class="operator">-</span><span class="operator">></span>readAll());
<span class="keyword">if</span> (document<span class="operator">.</span>isObject()) {
<span class="type">QJsonObject</span> obj <span class="operator">=</span> document<span class="operator">.</span>object();
<span class="type">QJsonObject</span> tempObject;
<span class="type">QJsonValue</span> val;
<span class="keyword">if</span> (obj<span class="operator">.</span>contains(<span class="type">QStringLiteral</span>(<span class="string">"weather"</span>))) {
val <span class="operator">=</span> obj<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"weather"</span>));
<span class="type">QJsonArray</span> weatherArray <span class="operator">=</span> val<span class="operator">.</span>toArray();
val <span class="operator">=</span> weatherArray<span class="operator">.</span>at(<span class="number">0</span>);
tempObject <span class="operator">=</span> val<span class="operator">.</span>toObject();
d<span class="operator">-</span><span class="operator">></span>now<span class="operator">.</span>setWeatherDescription(tempObject<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"description"</span>))<span class="operator">.</span>toString());
d<span class="operator">-</span><span class="operator">></span>now<span class="operator">.</span>setWeatherIcon(tempObject<span class="operator">.</span>value(<span class="string">"icon"</span>)<span class="operator">.</span>toString());
}
<span class="keyword">if</span> (obj<span class="operator">.</span>contains(<span class="type">QStringLiteral</span>(<span class="string">"main"</span>))) {
val <span class="operator">=</span> obj<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"main"</span>));
tempObject <span class="operator">=</span> val<span class="operator">.</span>toObject();
val <span class="operator">=</span> tempObject<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"temp"</span>));
d<span class="operator">-</span><span class="operator">></span>now<span class="operator">.</span>setTemperature(niceTemperatureString(val<span class="operator">.</span>toDouble()));
}
}
}
networkReply<span class="operator">-</span><span class="operator">></span>deleteLater();
<span class="comment">//retrieve the forecast</span>
<span class="type">QUrl</span> url(<span class="string">"http://api.openweathermap.org/data/2.5/forecast/daily"</span>);
<span class="type">QUrlQuery</span> query;
query<span class="operator">.</span>addQueryItem(<span class="string">"q"</span><span class="operator">,</span> d<span class="operator">-</span><span class="operator">></span>city);
query<span class="operator">.</span>addQueryItem(<span class="string">"mode"</span><span class="operator">,</span> <span class="string">"json"</span>);
query<span class="operator">.</span>addQueryItem(<span class="string">"cnt"</span><span class="operator">,</span> <span class="string">"5"</span>);
url<span class="operator">.</span>setQuery(query);
<span class="type">QNetworkReply</span> <span class="operator">*</span>rep <span class="operator">=</span> d<span class="operator">-</span><span class="operator">></span>nam<span class="operator">-</span><span class="operator">></span>get(<span class="type">QNetworkRequest</span>(url));
<span class="comment">// connect up the signal right away</span>
d<span class="operator">-</span><span class="operator">></span>forecastReplyMapper<span class="operator">-</span><span class="operator">></span>setMapping(rep<span class="operator">,</span> rep);
connect(rep<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> d<span class="operator">-</span><span class="operator">></span>forecastReplyMapper<span class="operator">,</span> SLOT(map()));
}
<span class="type">void</span> AppModel<span class="operator">::</span>handleForecastNetworkData(<span class="type">QObject</span> <span class="operator">*</span>replyObj)
{
<span class="type">QNetworkReply</span> <span class="operator">*</span>networkReply <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type">QNetworkReply</span><span class="operator">*</span><span class="operator">></span>(replyObj);
<span class="keyword">if</span> (<span class="operator">!</span>networkReply)
<span class="keyword">return</span>;
<span class="keyword">if</span> (<span class="operator">!</span>networkReply<span class="operator">-</span><span class="operator">></span>error()) {
<span class="type">QJsonDocument</span> document <span class="operator">=</span> <span class="type">QJsonDocument</span><span class="operator">::</span>fromJson(networkReply<span class="operator">-</span><span class="operator">></span>readAll());
<span class="type">QJsonObject</span> jo;
<span class="type">QJsonValue</span> jv;
<span class="type">QJsonObject</span> root <span class="operator">=</span> document<span class="operator">.</span>object();
jv <span class="operator">=</span> root<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"list"</span>));
<span class="keyword">if</span> (<span class="operator">!</span>jv<span class="operator">.</span>isArray())
qWarning() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Invalid forecast object"</span>;
<span class="type">QJsonArray</span> ja <span class="operator">=</span> jv<span class="operator">.</span>toArray();
<span class="comment">//we need 4 days of forecast -> first entry is today</span>
<span class="keyword">if</span> (ja<span class="operator">.</span>count() <span class="operator">!</span><span class="operator">=</span> <span class="number">5</span>)
qWarning() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Invalid forecast object"</span>;
<span class="type">QString</span> data;
<span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">1</span>; i<span class="operator"><</span>ja<span class="operator">.</span>count(); i<span class="operator">+</span><span class="operator">+</span>) {
WeatherData <span class="operator">*</span>forecastEntry <span class="operator">=</span> <span class="keyword">new</span> WeatherData();
<span class="comment">//min/max temperature</span>
<span class="type">QJsonObject</span> subtree <span class="operator">=</span> ja<span class="operator">.</span>at(i)<span class="operator">.</span>toObject();
jo <span class="operator">=</span> subtree<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"temp"</span>))<span class="operator">.</span>toObject();
jv <span class="operator">=</span> jo<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"min"</span>));
data<span class="operator">.</span>clear();
data <span class="operator">+</span><span class="operator">=</span> niceTemperatureString(jv<span class="operator">.</span>toDouble());
data <span class="operator">+</span><span class="operator">=</span> <span class="type">QChar</span>(<span class="char">'/'</span>);
jv <span class="operator">=</span> jo<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"max"</span>));
data <span class="operator">+</span><span class="operator">=</span> niceTemperatureString(jv<span class="operator">.</span>toDouble());
forecastEntry<span class="operator">-</span><span class="operator">></span>setTemperature(data);
<span class="comment">//get date</span>
jv <span class="operator">=</span> subtree<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"dt"</span>));
<span class="type">QDateTime</span> dt <span class="operator">=</span> <span class="type">QDateTime</span><span class="operator">::</span>fromMSecsSinceEpoch((<span class="type">qint64</span>)jv<span class="operator">.</span>toDouble()<span class="operator">*</span><span class="number">1000</span>);
forecastEntry<span class="operator">-</span><span class="operator">></span>setDayOfWeek(dt<span class="operator">.</span>date()<span class="operator">.</span>toString(<span class="type">QStringLiteral</span>(<span class="string">"ddd"</span>)));
<span class="comment">//get icon</span>
<span class="type">QJsonArray</span> weatherArray <span class="operator">=</span> subtree<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"weather"</span>))<span class="operator">.</span>toArray();
jo <span class="operator">=</span> weatherArray<span class="operator">.</span>at(<span class="number">0</span>)<span class="operator">.</span>toObject();
forecastEntry<span class="operator">-</span><span class="operator">></span>setWeatherIcon(jo<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"icon"</span>))<span class="operator">.</span>toString());
<span class="comment">//get description</span>
forecastEntry<span class="operator">-</span><span class="operator">></span>setWeatherDescription(jo<span class="operator">.</span>value(<span class="type">QStringLiteral</span>(<span class="string">"description"</span>))<span class="operator">.</span>toString());
d<span class="operator">-</span><span class="operator">></span>forecast<span class="operator">.</span>append(forecastEntry);
}
<span class="keyword">if</span> (<span class="operator">!</span>(d<span class="operator">-</span><span class="operator">></span>ready)) {
d<span class="operator">-</span><span class="operator">></span>ready <span class="operator">=</span> <span class="keyword">true</span>;
<span class="keyword">emit</span> readyChanged();
}
<span class="keyword">emit</span> weatherChanged();
}
networkReply<span class="operator">-</span><span class="operator">></span>deleteLater();
}
bool AppModel<span class="operator">::</span>hasValidCity() <span class="keyword">const</span>
{
<span class="keyword">return</span> (<span class="operator">!</span>(d<span class="operator">-</span><span class="operator">></span>city<span class="operator">.</span>isEmpty()) <span class="operator">&</span><span class="operator">&</span> d<span class="operator">-</span><span class="operator">></span>city<span class="operator">.</span>size() <span class="operator">></span> <span class="number">1</span> <span class="operator">&</span><span class="operator">&</span> d<span class="operator">-</span><span class="operator">></span>city <span class="operator">!</span><span class="operator">=</span> <span class="string">""</span>);
}
bool AppModel<span class="operator">::</span>hasValidWeather() <span class="keyword">const</span>
{
<span class="keyword">return</span> hasValidCity() <span class="operator">&</span><span class="operator">&</span> (<span class="operator">!</span>(d<span class="operator">-</span><span class="operator">></span>now<span class="operator">.</span>weatherIcon()<span class="operator">.</span>isEmpty()) <span class="operator">&</span><span class="operator">&</span>
(d<span class="operator">-</span><span class="operator">></span>now<span class="operator">.</span>weatherIcon()<span class="operator">.</span>size() <span class="operator">></span> <span class="number">1</span>) <span class="operator">&</span><span class="operator">&</span>
d<span class="operator">-</span><span class="operator">></span>now<span class="operator">.</span>weatherIcon() <span class="operator">!</span><span class="operator">=</span> <span class="string">""</span>);
}
WeatherData <span class="operator">*</span>AppModel<span class="operator">::</span>weather() <span class="keyword">const</span>
{
<span class="keyword">return</span> <span class="operator">&</span>(d<span class="operator">-</span><span class="operator">></span>now);
}
<span class="type">QQmlListProperty</span><span class="operator"><</span>WeatherData<span class="operator">></span> AppModel<span class="operator">::</span>forecast() <span class="keyword">const</span>
{
<span class="keyword">return</span> <span class="operator">*</span>(d<span class="operator">-</span><span class="operator">></span>fcProp);
}
bool AppModel<span class="operator">::</span>ready() <span class="keyword">const</span>
{
<span class="keyword">return</span> d<span class="operator">-</span><span class="operator">></span>ready;
}
bool AppModel<span class="operator">::</span>hasSource() <span class="keyword">const</span>
{
<span class="keyword">return</span> (d<span class="operator">-</span><span class="operator">></span>src <span class="operator">!</span><span class="operator">=</span> NULL);
}
bool AppModel<span class="operator">::</span>useGps() <span class="keyword">const</span>
{
<span class="keyword">return</span> d<span class="operator">-</span><span class="operator">></span>useGps;
}
<span class="type">void</span> AppModel<span class="operator">::</span>setUseGps(bool value)
{
d<span class="operator">-</span><span class="operator">></span>useGps <span class="operator">=</span> value;
<span class="keyword">if</span> (value) {
d<span class="operator">-</span><span class="operator">></span>city <span class="operator">=</span> <span class="string">""</span>;
d<span class="operator">-</span><span class="operator">></span>throttle<span class="operator">.</span>invalidate();
<span class="keyword">emit</span> cityChanged();
<span class="keyword">emit</span> weatherChanged();
}
<span class="keyword">emit</span> useGpsChanged();
}
<span class="type">QString</span> AppModel<span class="operator">::</span>city() <span class="keyword">const</span>
{
<span class="keyword">return</span> d<span class="operator">-</span><span class="operator">></span>city;
}
<span class="type">void</span> AppModel<span class="operator">::</span>setCity(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>value)
{
d<span class="operator">-</span><span class="operator">></span>city <span class="operator">=</span> value;
<span class="keyword">emit</span> cityChanged();
refreshWeather();
}</pre>
</div>
<!-- @@@weatherinfo/appmodel.cpp -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2013 Digia Plc and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|