diff options
Diffstat (limited to 'src/applicationinfo.cpp')
-rw-r--r-- | src/applicationinfo.cpp | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/src/applicationinfo.cpp b/src/applicationinfo.cpp new file mode 100644 index 0000000..dcbc54f --- /dev/null +++ b/src/applicationinfo.cpp @@ -0,0 +1,193 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the FOO module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore/QFile> +#include <QtCore/qmath.h> +#include <QtCore/QRegExp> +#include <QtCore/QUrl> +#include <QtCore/QUrlQuery> +#include <QtGui/QGuiApplication> +#include <QtGui/QScreen> +#include <QtNetwork/QNetworkReply> +#include <QtNetwork/QNetworkRequest> + +#include "applicationinfo.h" +#include "weatherimageprovider.h" +#include <QDebug> + +ApplicationInfo::ApplicationInfo(WeatherImageProvider *provider) + : imageProvider(provider) +{ + + m_isMobile = false; +#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_BLACKBERRY) + m_isMobile = true; +#endif + + m_colors = new QQmlPropertyMap(this); + m_colors->insert(QLatin1String("white"), QVariant("#ffffff")); + m_colors->insert(QLatin1String("smokeGray"), QVariant("#eeeeee")); + m_colors->insert(QLatin1String("paleGray"), QVariant("#d7d6d5")); + m_colors->insert(QLatin1String("lightGray"), QVariant("#aeadac")); + m_colors->insert(QLatin1String("darkGray"), QVariant("#35322f")); + m_colors->insert(QLatin1String("mediumGray"), QVariant("#5d5b59")); + m_colors->insert(QLatin1String("doubleDarkGray"), QVariant("#1e1b18")); + m_colors->insert(QLatin1String("blue"), QVariant("#14aaff")); + m_colors->insert(QLatin1String("darkBlue"), QVariant("#14148c")); + + m_constants = new QQmlPropertyMap(this); + m_constants->insert(QLatin1String("isMobile"), QVariant(m_isMobile)); + m_constants->insert(QLatin1String("errorLoadingImage"), QVariant(tr("Error loading image - Host not found or unreachable"))); + + manager = new QNetworkAccessManager(this); + connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); + m_citiesFound = new CitiesListModel(this); + + QRect rect = qApp->primaryScreen()->geometry(); + m_ratio = m_isMobile ? qMin(qMax(rect.width(), rect.height())/1136. , qMin(rect.width(), rect.height())/640.) : 1; + m_sliderHandleWidth = getSizeWithRatio(70); + m_sliderHandleHeight = getSizeWithRatio(87); + m_sliderGapWidth = getSizeWithRatio(100); + m_isPortraitMode = m_isMobile ? rect.height() > rect.width() : false; + m_hMargin = m_isPortraitMode ? 20 * ratio() : 50 * ratio(); + m_applicationWidth = m_isMobile ? rect.width() : 1120; + + m_currentIndexDay = -1; + + if (m_isMobile) + connect(qApp->primaryScreen(), SIGNAL(physicalSizeChanged(QSizeF)), this, SLOT(notifyPortraitMode())); + + // Search in English + // In order to use yr.no weather data service, refer to their terms + // and conditions of use. http://om.yr.no/verdata/free-weather-data/ + QUrl searchUrl2("http://www.yr.no/_/settspr.aspx"); + QUrlQuery query2; + query2.addQueryItem("spr", "eng"); + query2.addQueryItem("redir", "/"); + searchUrl2.setQuery(query2); + manager->get(QNetworkRequest(searchUrl2)); +} + +void ApplicationInfo::setCurrentCityModel(CityModel *model) +{ + if (model != m_currentCityModel) + { + m_currentCityModel = model; + emit currentCityModelChanged(); + } +} + +void ApplicationInfo::setCurrentIndexDay(const int index) +{ + if (index != m_currentIndexDay) { + m_currentIndexDay = index; + emit currentIndexDayChanged(); + } +} + +void ApplicationInfo::setApplicationWidth(const int newWidth) +{ + if (newWidth != m_applicationWidth) { + m_applicationWidth = newWidth; + emit applicationWidthChanged(); + } +} + +QString ApplicationInfo::getImagePath(const QString image) +{ + return QString("qrc:/weatherapp/qml/touch/images/%1").arg(image); +} + +void ApplicationInfo::notifyPortraitMode() +{ + int width = qApp->primaryScreen()->geometry().width(); + int height = qApp->primaryScreen()->geometry().height(); + setIsPortraitMode(height > width); +} + +void ApplicationInfo::setIsPortraitMode(const bool newMode) +{ + if (m_isPortraitMode != newMode) { + m_isPortraitMode = newMode; + m_hMargin = m_isPortraitMode ? 20 * ratio() : 50 * ratio(); + emit portraitModeChanged(); + emit hMarginChanged(); + } +} + +void ApplicationInfo::queryCities(const QString input) +{ + if (input.isEmpty()) + return; + // In order to use yr.no weather data service, refer to their terms + // and conditions of use. http://om.yr.no/verdata/free-weather-data/ + QUrl searchUrl("http://www.yr.no/_/websvc/jsonforslagsboks.aspx"); + QUrlQuery query; + query.addQueryItem("s", input); + searchUrl.setQuery(query); + manager->get(QNetworkRequest(searchUrl)); + m_citiesFound->clear(); + waitForCitiesQueryReply(tr("Waiting for cities, network may be slow...")); +} + +void ApplicationInfo::replyFinished(QNetworkReply *reply) +{ + waitForCitiesQueryReply(""); + if (reply->request().url().query() != "spr=eng&redir=/" ) { + if (reply->error() != QNetworkReply::NoError) { + emit(errorOnQueryCities(tr("Network error: %1").arg(reply->errorString()))); + m_citiesFound->addCities(); + } else { + QString data = reply->readAll(); + QRegExp regExp; + regExp.setPattern("^\\[\\[.*\\],\\[\\[(.*)\\]\\]\\]$"); + regExp.exactMatch(data); + QString foundCities = regExp.capturedTexts().at(1); + QStringList citiesFound = foundCities.split(QRegExp("\\],\\["), QString::SkipEmptyParts); + m_citiesFound->addCities(citiesFound); + } + } + if (reply) { + reply->deleteLater(); + reply = 0; + } +} + |