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