source: trunk/demos/embedded/weatherinfo/weatherinfo.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 19.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the demonstration applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtCore>
43#include <QtGui>
44#include <QtNetwork>
45#include <QtSvg>
46
47#if defined (Q_OS_SYMBIAN)
48#include "sym_iap_util.h"
49#endif
50
51class WeatherInfo: public QMainWindow
52{
53 Q_OBJECT
54
55private:
56
57 QGraphicsView *m_view;
58 QGraphicsScene m_scene;
59 QString city;
60 QGraphicsRectItem *m_statusItem;
61 QGraphicsTextItem *m_temperatureItem;
62 QGraphicsTextItem *m_conditionItem;
63 QGraphicsSvgItem *m_iconItem;
64 QList<QGraphicsRectItem*> m_forecastItems;
65 QList<QGraphicsTextItem*> m_dayItems;
66 QList<QGraphicsSvgItem*> m_conditionItems;
67 QList<QGraphicsTextItem*> m_rangeItems;
68 QTimeLine m_timeLine;
69 QHash<QString, QString> m_icons;
70
71public:
72 WeatherInfo(QWidget *parent = 0): QMainWindow(parent) {
73
74 m_view = new QGraphicsView(this);
75 setCentralWidget(m_view);
76
77 setupScene();
78 m_view->setScene(&m_scene);
79 m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
80 m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
81
82 m_view->setFrameShape(QFrame::NoFrame);
83 setWindowTitle("Weather Info");
84
85 QStringList cities;
86 cities << "Oslo";
87 cities << "Berlin";
88 cities << "Brisbane";
89 cities << "Helsinki";
90 cities << "San Diego";
91 for (int i = 0; i < cities.count(); ++i) {
92 QAction *action = new QAction(cities[i], this);
93 connect(action, SIGNAL(triggered()), SLOT(chooseCity()));
94 addAction(action);
95#if defined(Q_OS_SYMBIAN)
96 menuBar()->addAction(action);
97#endif
98 }
99 setContextMenuPolicy(Qt::ActionsContextMenu);
100
101 QTimer::singleShot(0, this, SLOT(delayedInit()));
102 }
103
104private slots:
105 void delayedInit() {
106#if defined(Q_OS_SYMBIAN)
107 qt_SetDefaultIap();
108#endif
109 request("Oslo");
110 }
111
112private slots:
113
114 void chooseCity() {
115 QAction *action = qobject_cast<QAction*>(sender());
116 if (action)
117 request(action->text());
118 }
119
120 void handleNetworkData(QNetworkReply *networkReply) {
121 QUrl url = networkReply->url();
122 if (!networkReply->error())
123 digest(QString::fromUtf8(networkReply->readAll()));
124 networkReply->deleteLater();
125 networkReply->manager()->deleteLater();
126 }
127
128 void animate(int frame) {
129 qreal progress = static_cast<qreal>(frame) / 100;
130#if QT_VERSION >= 0x040500
131 m_iconItem->setOpacity(progress);
132#endif
133 qreal hw = width() / 2.0;
134 m_statusItem->setPos(-hw + hw * progress, 0);
135 for (int i = 0; i < m_forecastItems.count(); ++i) {
136 qreal ofs = i * 0.5 / m_forecastItems.count();
137 qreal alpha = qBound(qreal(0), 2 * (progress - ofs), qreal(1));
138#if QT_VERSION >= 0x040500
139 m_conditionItems[i]->setOpacity(alpha);
140#endif
141 QPointF pos = m_forecastItems[i]->pos();
142 if (width() > height()) {
143 qreal fx = width() - width() * 0.4 * alpha;
144 m_forecastItems[i]->setPos(fx, pos.y());
145 } else {
146 qreal fx = height() - height() * 0.5 * alpha;
147 m_forecastItems[i]->setPos(pos.x(), fx);
148 }
149 }
150 }
151
152private:
153
154 void setupScene() {
155
156 QColor textColor = palette().color(QPalette::WindowText);
157 QFont textFont = font();
158 textFont.setBold(true);
159 textFont.setPointSize(textFont.pointSize() * 2);
160
161 m_temperatureItem = m_scene.addText(QString(), textFont);
162 m_temperatureItem->setDefaultTextColor(textColor);
163
164 m_conditionItem = m_scene.addText(QString(), textFont);
165 m_conditionItem->setDefaultTextColor(textColor);
166
167 m_iconItem = new QGraphicsSvgItem;
168 m_scene.addItem(m_iconItem);
169
170 m_statusItem = m_scene.addRect(0, 0, 10, 10);
171 m_statusItem->setPen(Qt::NoPen);
172 m_statusItem->setBrush(Qt::NoBrush);
173 m_temperatureItem->setParentItem(m_statusItem);
174 m_conditionItem->setParentItem(m_statusItem);
175 m_iconItem->setParentItem(m_statusItem);
176
177 connect(&m_timeLine, SIGNAL(frameChanged(int)), SLOT(animate(int)));
178 m_timeLine.setDuration(1100);
179 m_timeLine.setFrameRange(0, 100);
180 m_timeLine.setCurveShape(QTimeLine::EaseInCurve);
181 }
182
183 void request(const QString &location) {
184 QUrl url("http://www.google.com/ig/api");
185 url.addEncodedQueryItem("hl", "en");
186 url.addEncodedQueryItem("weather", QUrl::toPercentEncoding(location));
187
188 QNetworkAccessManager *manager = new QNetworkAccessManager(this);
189 connect(manager, SIGNAL(finished(QNetworkReply*)),
190 this, SLOT(handleNetworkData(QNetworkReply*)));
191 manager->get(QNetworkRequest(url));
192
193 city = QString();
194 setWindowTitle("Loading...");
195 }
196
197 QString extractIcon(const QString &data) {
198 if (m_icons.isEmpty()) {
199 m_icons["mostly_cloudy"] = "weather-few-clouds";
200 m_icons["cloudy"] = "weather-overcast";
201 m_icons["mostly_sunny"] = "weather-sunny-very-few-clouds";
202 m_icons["partly_cloudy"] = "weather-sunny-very-few-clouds";
203 m_icons["sunny"] = "weather-sunny";
204 m_icons["flurries"] = "weather-snow";
205 m_icons["fog"] = "weather-fog";
206 m_icons["haze"] = "weather-haze";
207 m_icons["icy"] = "weather-icy";
208 m_icons["sleet"] = "weather-sleet";
209 m_icons["chance_of_sleet"] = "weather-sleet";
210 m_icons["snow"] = "weather-snow";
211 m_icons["chance_of_snow"] = "weather-snow";
212 m_icons["mist"] = "weather-showers";
213 m_icons["rain"] = "weather-showers";
214 m_icons["chance_of_rain"] = "weather-showers";
215 m_icons["storm"] = "weather-storm";
216 m_icons["chance_of_storm"] = "weather-storm";
217 m_icons["thunderstorm"] = "weather-thundershower";
218 m_icons["chance_of_tstorm"] = "weather-thundershower";
219 }
220 QRegExp regex("([\\w]+).gif$");
221 if (regex.indexIn(data) != -1) {
222 QString i = regex.cap();
223 i = i.left(i.length() - 4);
224 QString name = m_icons.value(i);
225 if (!name.isEmpty()) {
226 name.prepend(":/icons/");
227 name.append(".svg");
228 return name;
229 }
230 }
231 return QString();
232 }
233
234 static QString toCelcius(QString t, QString unit) {
235 bool ok = false;
236 int degree = t.toInt(&ok);
237 if (!ok)
238 return QString();
239 if (unit != "SI")
240 degree = ((degree - 32) * 5 + 8)/ 9;
241 return QString::number(degree) + QChar(176);
242 }
243
244
245#define GET_DATA_ATTR xml.attributes().value("data").toString()
246
247 void digest(const QString &data) {
248
249 QColor textColor = palette().color(QPalette::WindowText);
250 QString unitSystem;
251
252 delete m_iconItem;
253 m_iconItem = new QGraphicsSvgItem();
254 m_scene.addItem(m_iconItem);
255 m_iconItem->setParentItem(m_statusItem);
256 qDeleteAll(m_dayItems);
257 qDeleteAll(m_conditionItems);
258 qDeleteAll(m_rangeItems);
259 qDeleteAll(m_forecastItems);
260 m_dayItems.clear();
261 m_conditionItems.clear();
262 m_rangeItems.clear();
263 m_forecastItems.clear();
264
265 QXmlStreamReader xml(data);
266 while (!xml.atEnd()) {
267 xml.readNext();
268 if (xml.tokenType() == QXmlStreamReader::StartElement) {
269 if (xml.name() == "city") {
270 city = GET_DATA_ATTR;
271 setWindowTitle(city);
272 }
273 if (xml.name() == "unit_system")
274 unitSystem = xml.attributes().value("data").toString();
275 // Parse current weather conditions
276 if (xml.name() == "current_conditions") {
277 while (!xml.atEnd()) {
278 xml.readNext();
279 if (xml.name() == "current_conditions")
280 break;
281 if (xml.tokenType() == QXmlStreamReader::StartElement) {
282 if (xml.name() == "condition") {
283 m_conditionItem->setPlainText(GET_DATA_ATTR);
284 }
285 if (xml.name() == "icon") {
286 QString name = extractIcon(GET_DATA_ATTR);
287 if (!name.isEmpty()) {
288 delete m_iconItem;
289 m_iconItem = new QGraphicsSvgItem(name);
290 m_scene.addItem(m_iconItem);
291 m_iconItem->setParentItem(m_statusItem);
292 }
293 }
294 if (xml.name() == "temp_c") {
295 QString s = GET_DATA_ATTR + QChar(176);
296 m_temperatureItem->setPlainText(s);
297 }
298 }
299 }
300 }
301 // Parse and collect the forecast conditions
302 if (xml.name() == "forecast_conditions") {
303 QGraphicsTextItem *dayItem = 0;
304 QGraphicsSvgItem *statusItem = 0;
305 QString lowT, highT;
306 while (!xml.atEnd()) {
307 xml.readNext();
308 if (xml.name() == "forecast_conditions") {
309 if (dayItem && statusItem &&
310 !lowT.isEmpty() && !highT.isEmpty()) {
311 m_dayItems << dayItem;
312 m_conditionItems << statusItem;
313 QString txt = highT + '/' + lowT;
314 QGraphicsTextItem* rangeItem;
315 rangeItem = m_scene.addText(txt);
316 rangeItem->setDefaultTextColor(textColor);
317 m_rangeItems << rangeItem;
318 QGraphicsRectItem *box;
319 box = m_scene.addRect(0, 0, 10, 10);
320 box->setPen(Qt::NoPen);
321 box->setBrush(Qt::NoBrush);
322 m_forecastItems << box;
323 dayItem->setParentItem(box);
324 statusItem->setParentItem(box);
325 rangeItem->setParentItem(box);
326 } else {
327 delete dayItem;
328 delete statusItem;
329 }
330 break;
331 }
332 if (xml.tokenType() == QXmlStreamReader::StartElement) {
333 if (xml.name() == "day_of_week") {
334 QString s = GET_DATA_ATTR;
335 dayItem = m_scene.addText(s.left(3));
336 dayItem->setDefaultTextColor(textColor);
337 }
338 if (xml.name() == "icon") {
339 QString name = extractIcon(GET_DATA_ATTR);
340 if (!name.isEmpty()) {
341 statusItem = new QGraphicsSvgItem(name);
342 m_scene.addItem(statusItem);
343 }
344 }
345 if (xml.name() == "low")
346 lowT = toCelcius(GET_DATA_ATTR, unitSystem);
347 if (xml.name() == "high")
348 highT = toCelcius(GET_DATA_ATTR, unitSystem);
349 }
350 }
351 }
352
353 }
354 }
355
356 m_timeLine.stop();
357 layoutItems();
358 animate(0);
359 m_timeLine.start();
360 }
361
362 void layoutItems() {
363 m_scene.setSceneRect(0, 0, width() - 1, height() - 1);
364 m_view->centerOn(width() / 2, height() / 2);
365 if (width() > height())
366 layoutItemsLandscape();
367 else
368 layoutItemsPortrait();
369 }
370
371 void layoutItemsLandscape() {
372 m_statusItem->setRect(0, 0, width() / 2 - 1, height() - 1);
373
374 if (!m_iconItem->boundingRect().isEmpty()) {
375 qreal dim = qMin(width() * 0.6, height() * 0.8);
376 qreal pad = (height() - dim) / 2;
377 qreal sw = dim / m_iconItem->boundingRect().width();
378 qreal sh = dim / m_iconItem->boundingRect().height();
379 m_iconItem->setTransform(QTransform().scale(sw, sh));
380 m_iconItem->setPos(1, pad);
381 }
382
383 m_temperatureItem->setPos(2, 2);
384 qreal h = m_conditionItem->boundingRect().height();
385 m_conditionItem->setPos(10, height() - h);
386
387 if (m_dayItems.count()) {
388 qreal left = width() * 0.6;
389 qreal h = height() / m_dayItems.count();
390 QFont textFont = font();
391 textFont.setPixelSize(static_cast<int>(h * 0.3));
392 qreal statusWidth = 0;
393 qreal rangeWidth = 0;
394 for (int i = 0; i < m_dayItems.count(); ++i) {
395 m_dayItems[i]->setFont(textFont);
396 QRectF brect = m_dayItems[i]->boundingRect();
397 statusWidth = qMax(statusWidth, brect.width());
398 brect = m_rangeItems[i]->boundingRect();
399 rangeWidth = qMax(rangeWidth, brect.width());
400 }
401 qreal space = width() - left - statusWidth - rangeWidth;
402 qreal dim = qMin(h, space);
403 qreal pad = statusWidth + (space - dim) / 2;
404 for (int i = 0; i < m_dayItems.count(); ++i) {
405 qreal base = h * i;
406 m_forecastItems[i]->setPos(left, base);
407 m_forecastItems[i]->setRect(0, 0, width() - left, h);
408 QRectF brect = m_dayItems[i]->boundingRect();
409 qreal ofs = (h - brect.height()) / 2;
410 m_dayItems[i]->setPos(0, ofs);
411 brect = m_rangeItems[i]->boundingRect();
412 ofs = (h - brect.height()) / 2;
413 m_rangeItems[i]->setPos(width() - rangeWidth - left, ofs);
414 brect = m_conditionItems[i]->boundingRect();
415 ofs = (h - dim) / 2;
416 m_conditionItems[i]->setPos(pad, ofs);
417 if (brect.isEmpty())
418 continue;
419 qreal sw = dim / brect.width();
420 qreal sh = dim / brect.height();
421 m_conditionItems[i]->setTransform(QTransform().scale(sw, sh));
422 }
423 }
424 }
425
426 void layoutItemsPortrait() {
427
428 m_statusItem->setRect(0, 0, width() - 1, height() / 2 - 1);
429
430 if (!m_iconItem->boundingRect().isEmpty()) {
431 qreal dim = qMin(width() * 0.8, height() * 0.4);
432 qreal ofsy = (height() / 2 - dim) / 2;
433 qreal ofsx = (width() - dim) / 3;
434 qreal sw = dim / m_iconItem->boundingRect().width();
435 qreal sh = dim / m_iconItem->boundingRect().height();
436 m_iconItem->setTransform(QTransform().scale(sw, sh));
437 m_iconItem->setPos(ofsx, ofsy);
438 }
439
440 m_temperatureItem->setPos(2, 2);
441 qreal ch = m_conditionItem->boundingRect().height();
442 qreal cw = m_conditionItem->boundingRect().width();
443 m_conditionItem->setPos(width() - cw , height() / 2 - ch - 20);
444
445 if (m_dayItems.count()) {
446 qreal top = height() * 0.5;
447 qreal w = width() / m_dayItems.count();
448 qreal statusHeight = 0;
449 qreal rangeHeight = 0;
450 for (int i = 0; i < m_dayItems.count(); ++i) {
451 m_dayItems[i]->setFont(font());
452 QRectF brect = m_dayItems[i]->boundingRect();
453 statusHeight = qMax(statusHeight, brect.height());
454 brect = m_rangeItems[i]->boundingRect();
455 rangeHeight = qMax(rangeHeight, brect.height());
456 }
457 qreal space = height() - top - statusHeight - rangeHeight;
458 qreal dim = qMin(w, space);
459
460 qreal boxh = statusHeight + rangeHeight + dim;
461 qreal pad = (height() - top - boxh) / 2;
462
463 for (int i = 0; i < m_dayItems.count(); ++i) {
464 qreal base = w * i;
465 m_forecastItems[i]->setPos(base, top);
466 m_forecastItems[i]->setRect(0, 0, w, boxh);
467 QRectF brect = m_dayItems[i]->boundingRect();
468 qreal ofs = (w - brect.width()) / 2;
469 m_dayItems[i]->setPos(ofs, pad);
470
471 brect = m_rangeItems[i]->boundingRect();
472 ofs = (w - brect.width()) / 2;
473 m_rangeItems[i]->setPos(ofs, pad + statusHeight + dim);
474
475 brect = m_conditionItems[i]->boundingRect();
476 ofs = (w - dim) / 2;
477 m_conditionItems[i]->setPos(ofs, pad + statusHeight);
478 if (brect.isEmpty())
479 continue;
480 qreal sw = dim / brect.width();
481 qreal sh = dim / brect.height();
482 m_conditionItems[i]->setTransform(QTransform().scale(sw, sh));
483 }
484 }
485 }
486
487
488 void resizeEvent(QResizeEvent *event) {
489 Q_UNUSED(event);
490 layoutItems();
491 }
492
493};
494
495#include "weatherinfo.moc"
496
497int main(int argc, char *argv[])
498{
499 QApplication app(argc, argv);
500
501 WeatherInfo w;
502#if defined(Q_OS_SYMBIAN)
503 w.showMaximized();
504#else
505 w.resize(520, 288);
506 w.show();
507#endif
508
509 return app.exec();
510}
Note: See TracBrowser for help on using the repository browser.