diff options
Diffstat (limited to 'examples/positioning/weatherinfo')
33 files changed, 0 insertions, 2403 deletions
diff --git a/examples/positioning/weatherinfo/CMakeLists.txt b/examples/positioning/weatherinfo/CMakeLists.txt deleted file mode 100644 index 63e13830..00000000 --- a/examples/positioning/weatherinfo/CMakeLists.txt +++ /dev/null @@ -1,74 +0,0 @@ -# Generated from weatherinfo.pro. - -cmake_minimum_required(VERSION 3.16) -project(weatherinfo LANGUAGES CXX) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/positioning/weatherinfo") - -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Network) -find_package(Qt6 COMPONENTS Positioning) -find_package(Qt6 COMPONENTS Qml) -find_package(Qt6 COMPONENTS Quick) - -qt_add_executable(weatherinfo - appmodel.cpp appmodel.h - providerbackend.cpp providerbackend.h - openweathermapbackend.cpp openweathermapbackend.h - weatherapibackend.cpp weatherapibackend.h - main.cpp -) -set_target_properties(weatherinfo PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) -target_link_libraries(weatherinfo PRIVATE - Qt::Core - Qt::Gui - Qt::Network - Qt::Positioning - Qt::Qml - Qt::Quick -) - -qt_add_qml_module(weatherinfo - URI WeatherInfo - VERSION 1.0 - QML_FILES - components/BigForecastIcon.qml - components/ForecastIcon.qml - components/WeatherIcon.qml - weatherinfo.qml - RESOURCES - icons/weather-few-clouds.png - icons/weather-fog.png - icons/weather-haze.png - icons/weather-icy.png - icons/weather-overcast.png - icons/weather-showers.png - icons/weather-sleet.png - icons/weather-snow.png - icons/weather-storm.png - icons/weather-sunny-very-few-clouds.png - icons/weather-sunny.png - icons/weather-thundershower.png - icons/weather-showers-scattered.png - NO_RESOURCE_TARGET_PATH -) - -install(TARGETS weatherinfo - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/positioning/weatherinfo/appmodel.cpp b/examples/positioning/weatherinfo/appmodel.cpp deleted file mode 100644 index a5f207f1..00000000 --- a/examples/positioning/weatherinfo/appmodel.cpp +++ /dev/null @@ -1,514 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -#include "appmodel.h" -#include "openweathermapbackend.h" -#include "weatherapibackend.h" - -#include <QGeoPositionInfoSource> -#include <QGeoPositionInfo> -#include <QGeoCircle> -#include <QLoggingCategory> - -Q_LOGGING_CATEGORY(requestsLog, "wapp.requests") - -WeatherData::WeatherData(QObject *parent) : - QObject(parent) -{ -} - -WeatherData::WeatherData(const WeatherData &other) : - QObject(nullptr), - m_dayOfWeek(other.m_dayOfWeek), - m_weather(other.m_weather), - m_weatherDescription(other.m_weatherDescription), - m_temperature(other.m_temperature) -{ -} - -WeatherData::WeatherData(const WeatherInfo &other) - : QObject(nullptr), - m_dayOfWeek(other.m_dayOfWeek), - m_weather(other.m_weatherIconId), - m_weatherDescription(other.m_weatherDescription), - m_temperature(other.m_temperature) -{ -} - -QString WeatherData::dayOfWeek() const -{ - return m_dayOfWeek; -} - -/*! - * 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 - */ -QString WeatherData::weatherIcon() const -{ - return m_weather; -} - -QString WeatherData::weatherDescription() const -{ - return m_weatherDescription; -} - -QString WeatherData::temperature() const -{ - return m_temperature; -} - -void WeatherData::setDayOfWeek(const QString &value) -{ - m_dayOfWeek = value; - emit dataChanged(); -} - -void WeatherData::setWeatherIcon(const QString &value) -{ - m_weather = value; - emit dataChanged(); -} - -void WeatherData::setWeatherDescription(const QString &value) -{ - m_weatherDescription = value; - emit dataChanged(); -} - -void WeatherData::setTemperature(const QString &value) -{ - m_temperature = value; - emit dataChanged(); -} - -/* - The class is used as a cache for the weather information. - It contains a map to cache weather for cities. - The gps location is cached separately. - - For the coordiante search we do not compare the coordinate directly, but - check if it's within a circle of 3 km radius (we assume that the weather - does not really change within that radius). - - The cache returns a pair with empty location and weather data if no data - is found, or if the data is outdated. -*/ -class WeatherDataCache -{ -public: - WeatherDataCache() = default; - - using WeatherDataPair = QPair<QString, QList<WeatherInfo>>; - - WeatherDataPair getWeatherData(const QString &name) const; - WeatherDataPair getWeatherData(const QGeoCoordinate &coordinate) const; - - void addCacheElement(const LocationInfo &location, const QList<WeatherInfo> &info); - - static bool isCacheResultValid(const WeatherDataPair &result); - -private: - struct CacheItem - { - qint64 m_cacheTime; - QList<WeatherInfo> m_weatherData; - }; - - QMap<QString, CacheItem> m_cityCache; - - QGeoCoordinate m_gpsLocation; - QString m_gpsName; - CacheItem m_gpsData; - - static const qint64 kCacheTimeoutInterval = 3600; // 1 hour - static const int kCircleRadius = 3000; // 3 km -}; - -WeatherDataCache::WeatherDataPair WeatherDataCache::getWeatherData(const QString &name) const -{ - if (m_cityCache.contains(name)) { - const qint64 currentTime = QDateTime::currentSecsSinceEpoch(); - const auto &item = m_cityCache.value(name); - if (currentTime - item.m_cacheTime < kCacheTimeoutInterval) - return qMakePair(name, item.m_weatherData); - } - return qMakePair(QString(), QList<WeatherInfo>()); -} - -WeatherDataCache::WeatherDataPair WeatherDataCache::getWeatherData(const QGeoCoordinate &coordinate) const -{ - if (m_gpsLocation.isValid() && !m_gpsName.isEmpty()) { - const QGeoCircle area(m_gpsLocation, kCircleRadius); - if (area.contains(coordinate)) { - const qint64 currentTime = QDateTime::currentSecsSinceEpoch(); - if (currentTime - m_gpsData.m_cacheTime < kCacheTimeoutInterval) - return qMakePair(m_gpsName, m_gpsData.m_weatherData); - } - } - return qMakePair(QString(), QList<WeatherInfo>()); -} - -void WeatherDataCache::addCacheElement(const LocationInfo &location, const QList<WeatherInfo> &info) -{ - // It it expected that we have valid QGeoCoordinate only when the weather - // is received based on coordinates. - const qint64 currentTime = QDateTime::currentSecsSinceEpoch(); - if (location.m_coordinate.isValid()) { - m_gpsLocation = location.m_coordinate; - m_gpsName = location.m_name; - m_gpsData = { currentTime, info }; - } else { - m_cityCache[location.m_name] = { currentTime, info }; - } -} - -bool WeatherDataCache::isCacheResultValid(const WeatherDataCache::WeatherDataPair &result) -{ - return !result.first.isEmpty() && !result.second.isEmpty(); -} - -class AppModelPrivate -{ -public: - QGeoPositionInfoSource *src = nullptr; - QGeoCoordinate coord; - QString city; - WeatherData now; - QList<WeatherData*> forecast; - QQmlListProperty<WeatherData> *fcProp = nullptr; - bool ready = false; - bool useGps = true; - WeatherDataCache m_dataCache; - ProviderBackend *m_currentBackend = nullptr; - QList<ProviderBackend*> m_supportedBackends; - qsizetype m_currentBackendIndex = 0; -}; - -static void forecastAppend(QQmlListProperty<WeatherData> *prop, WeatherData *val) -{ - Q_UNUSED(val); - Q_UNUSED(prop); -} - -static WeatherData *forecastAt(QQmlListProperty<WeatherData> *prop, qsizetype index) -{ - AppModelPrivate *d = static_cast<AppModelPrivate*>(prop->data); - return d->forecast.at(index); -} - -static qsizetype forecastCount(QQmlListProperty<WeatherData> *prop) -{ - AppModelPrivate *d = static_cast<AppModelPrivate*>(prop->data); - return d->forecast.size(); -} - -static void forecastClear(QQmlListProperty<WeatherData> *prop) -{ - static_cast<AppModelPrivate*>(prop->data)->forecast.clear(); -} - -//! [0] -AppModel::AppModel(QObject *parent) : - QObject(parent), - d(new AppModelPrivate) -{ -//! [0] - d->fcProp = new QQmlListProperty<WeatherData>(this, d, forecastAppend, - forecastCount, - forecastAt, - forecastClear); - - d->m_supportedBackends.push_back(new OpenWeatherMapBackend(this)); - d->m_supportedBackends.push_back(new WeatherApiBackend(this)); - registerBackend(0); - -//! [1] - d->src = QGeoPositionInfoSource::createDefaultSource(this); - - if (d->src) { - d->useGps = true; - connect(d->src, SIGNAL(positionUpdated(QGeoPositionInfo)), - this, SLOT(positionUpdated(QGeoPositionInfo))); - connect(d->src, SIGNAL(errorOccurred(QGeoPositionInfoSource::Error)), - this, SLOT(positionError(QGeoPositionInfoSource::Error))); - d->src->startUpdates(); - } else { - d->useGps = false; - d->city = "Brisbane"; - emit cityChanged(); - requestWeatherByCity(); - } -} -//! [1] - -AppModel::~AppModel() -{ - if (d->src) - d->src->stopUpdates(); - if (d->fcProp) - delete d->fcProp; - foreach (WeatherData *inf, d->forecast) - delete inf; - d->forecast.clear(); - delete d; -} - -//! [2] -void AppModel::positionUpdated(QGeoPositionInfo gpsPos) -{ - d->coord = gpsPos.coordinate(); - - if (!d->useGps) - return; - - requestWeatherByCoordinates(); -} -//! [2] - -void AppModel::positionError(QGeoPositionInfoSource::Error e) -{ - Q_UNUSED(e); - qWarning() << "Position source error. Falling back to simulation mode."; - - // activate simulation mode - if (d->useGps) { - d->useGps = false; - d->city = "Brisbane"; - emit cityChanged(); - requestWeatherByCity(); - } -} - -void AppModel::refreshWeather() -{ - if (d->city.isEmpty()) { - qCDebug(requestsLog) << "refreshing weather skipped (no city)"; - return; - } - qCDebug(requestsLog) << "refreshing weather"; - requestWeatherByCity(); -} - -void AppModel::handleWeatherData(const LocationInfo &location, - const QList<WeatherInfo> &weatherDetails) -{ - if (applyWeatherData(location.m_name, weatherDetails)) - d->m_dataCache.addCacheElement(location, weatherDetails); -} - -void AppModel::switchToNextBackend() -{ - deregisterCurrentBackend(); - registerBackend(d->m_currentBackendIndex + 1); - if (d->m_currentBackend) { - // repeat the query - if (d->useGps) - requestWeatherByCoordinates(); - else - requestWeatherByCity(); - } else { - qWarning("The application has iterated through all of the weather backends, " - "and none of them seems to respond now. Please wait until any of the " - "backends becomes available again."); - } -} - -bool AppModel::applyWeatherData(const QString &city, const QList<WeatherInfo> &weatherDetails) -{ - // Check that we didn't get outdated weather data. The city should match, - // if only we do not use GPS. - if (city != d->city && !d->useGps) - return false; - - if (city != d->city && d->useGps) { - d->city = city; - emit cityChanged(); - } - - // delete previous forecast - foreach (WeatherData *inf, d->forecast) - delete inf; - d->forecast.clear(); - - // The first item in the list represents current weather. - if (!weatherDetails.isEmpty()) { - d->now.setTemperature(weatherDetails.first().m_temperature); - d->now.setWeatherIcon(weatherDetails.first().m_weatherIconId); - d->now.setWeatherDescription(weatherDetails.first().m_weatherDescription); - } - - // The other items represent weather forecast. The amount of items depends - // on the provider backend. - for (qsizetype i = 1; i < weatherDetails.size(); ++i) { - WeatherData *forecastEntry = new WeatherData(weatherDetails.at(i)); - d->forecast.append(forecastEntry); - } - - if (!d->ready) { - d->ready = true; - emit readyChanged(); - } - - emit weatherChanged(); - - return true; -} - -void AppModel::requestWeatherByCoordinates() -{ - const auto cacheResult = d->m_dataCache.getWeatherData(d->coord); - if (WeatherDataCache::isCacheResultValid(cacheResult)) - applyWeatherData(cacheResult.first, cacheResult.second); - else if (d->m_currentBackend) - d->m_currentBackend->requestWeatherInfo(d->coord); -} - -void AppModel::requestWeatherByCity() -{ - const auto cacheResult = d->m_dataCache.getWeatherData(d->city); - if (WeatherDataCache::isCacheResultValid(cacheResult)) - applyWeatherData(cacheResult.first, cacheResult.second); - else if (d->m_currentBackend) - d->m_currentBackend->requestWeatherInfo(d->city); -} - -void AppModel::registerBackend(qsizetype index) -{ - if (index >= 0 && index < d->m_supportedBackends.size()) { - d->m_currentBackend = d->m_supportedBackends.at(index); - d->m_currentBackendIndex = index; - connect(d->m_currentBackend, &ProviderBackend::weatherInformation, - this, &AppModel::handleWeatherData); - connect(d->m_currentBackend, &ProviderBackend::errorOccurred, - this, &AppModel::switchToNextBackend); - } -} - -void AppModel::deregisterCurrentBackend() -{ - if (d->m_currentBackend) { - disconnect(d->m_currentBackend, &ProviderBackend::weatherInformation, - this, &AppModel::handleWeatherData); - disconnect(d->m_currentBackend, &ProviderBackend::errorOccurred, - this, &AppModel::switchToNextBackend); - d->m_currentBackend = nullptr; - } -} - -bool AppModel::hasValidCity() const -{ - return (!(d->city.isEmpty()) && d->city.size() > 1 && d->city != ""); -} - -bool AppModel::hasValidWeather() const -{ - return hasValidCity() && (!(d->now.weatherIcon().isEmpty()) && - (d->now.weatherIcon().size() > 1) && - d->now.weatherIcon() != ""); -} - -WeatherData *AppModel::weather() const -{ - return &(d->now); -} - -QQmlListProperty<WeatherData> AppModel::forecast() const -{ - return *(d->fcProp); -} - -bool AppModel::ready() const -{ - return d->ready; -} - -bool AppModel::hasSource() const -{ - return (d->src != NULL); -} - -bool AppModel::useGps() const -{ - return d->useGps; -} - -void AppModel::setUseGps(bool value) -{ - d->useGps = value; - if (value) { - d->city = ""; - emit cityChanged(); - emit weatherChanged(); - // if we already have a valid GPS position, do not wait until it - // updates, but query the city immediately - if (d->coord.isValid()) - requestWeatherByCoordinates(); - } - emit useGpsChanged(); -} - -QString AppModel::city() const -{ - return d->city; -} - -void AppModel::setCity(const QString &value) -{ - d->city = value; - emit cityChanged(); - requestWeatherByCity(); -} diff --git a/examples/positioning/weatherinfo/appmodel.h b/examples/positioning/weatherinfo/appmodel.h deleted file mode 100644 index 73a9d06a..00000000 --- a/examples/positioning/weatherinfo/appmodel.h +++ /dev/null @@ -1,190 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -#ifndef APPMODEL_H -#define APPMODEL_H - -#include <QtCore/QObject> -#include <QtCore/QString> -#include <QtNetwork/QNetworkReply> -#include <QtQml/QQmlListProperty> -#include <QtQml/qqml.h> - -#include <QtPositioning/QGeoPositionInfoSource> - -#include "providerbackend.h" - -//! [0] -class WeatherData : public QObject { - Q_OBJECT - Q_PROPERTY(QString dayOfWeek - READ dayOfWeek WRITE setDayOfWeek - NOTIFY dataChanged) - Q_PROPERTY(QString weatherIcon - READ weatherIcon WRITE setWeatherIcon - NOTIFY dataChanged) - Q_PROPERTY(QString weatherDescription - READ weatherDescription WRITE setWeatherDescription - NOTIFY dataChanged) - Q_PROPERTY(QString temperature - READ temperature WRITE setTemperature - NOTIFY dataChanged) - QML_ANONYMOUS - -public: - explicit WeatherData(QObject *parent = 0); - WeatherData(const WeatherData &other); - WeatherData(const WeatherInfo &other); - - QString dayOfWeek() const; - QString weatherIcon() const; - QString weatherDescription() const; - QString temperature() const; - - void setDayOfWeek(const QString &value); - void setWeatherIcon(const QString &value); - void setWeatherDescription(const QString &value); - void setTemperature(const QString &value); - -signals: - void dataChanged(); -//! [0] -private: - QString m_dayOfWeek; - QString m_weather; - QString m_weatherDescription; - QString m_temperature; -//! [1] -}; -//! [1] - -Q_DECLARE_METATYPE(WeatherData) - -class AppModelPrivate; -//! [2] -class AppModel : public QObject -{ - Q_OBJECT - Q_PROPERTY(bool ready - READ ready - NOTIFY readyChanged) - Q_PROPERTY(bool hasSource - READ hasSource - NOTIFY readyChanged) - Q_PROPERTY(bool hasValidCity - READ hasValidCity - NOTIFY cityChanged) - Q_PROPERTY(bool hasValidWeather - READ hasValidWeather - NOTIFY weatherChanged) - Q_PROPERTY(bool useGps - READ useGps WRITE setUseGps - NOTIFY useGpsChanged) - Q_PROPERTY(QString city - READ city WRITE setCity - NOTIFY cityChanged) - Q_PROPERTY(WeatherData *weather - READ weather - NOTIFY weatherChanged) - Q_PROPERTY(QQmlListProperty<WeatherData> forecast - READ forecast - NOTIFY weatherChanged) - QML_ELEMENT - -public: - explicit AppModel(QObject *parent = 0); - ~AppModel(); - - bool ready() const; - bool hasSource() const; - bool useGps() const; - bool hasValidCity() const; - bool hasValidWeather() const; - void setUseGps(bool value); - - QString city() const; - void setCity(const QString &value); - - WeatherData *weather() const; - QQmlListProperty<WeatherData> forecast() const; - -public slots: - Q_INVOKABLE void refreshWeather(); - -//! [2] -private slots: - void positionUpdated(QGeoPositionInfo gpsPos); - void positionError(QGeoPositionInfoSource::Error e); - void handleWeatherData(const LocationInfo &location, const QList<WeatherInfo> &weatherDetails); - void switchToNextBackend(); - -//! [3] -signals: - void readyChanged(); - void useGpsChanged(); - void cityChanged(); - void weatherChanged(); -//! [3] - -private: - bool applyWeatherData(const QString &city, const QList<WeatherInfo> &weatherDetails); - void requestWeatherByCoordinates(); - void requestWeatherByCity(); - void registerBackend(qsizetype index); - void deregisterCurrentBackend(); - - - AppModelPrivate *d; - -//! [4] -}; -//! [4] - -#endif // APPMODEL_H diff --git a/examples/positioning/weatherinfo/components/BigForecastIcon.qml b/examples/positioning/weatherinfo/components/BigForecastIcon.qml deleted file mode 100644 index 9fa854a0..00000000 --- a/examples/positioning/weatherinfo/components/BigForecastIcon.qml +++ /dev/null @@ -1,92 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -import QtQuick 2.0 - -Item { - id: current - - property string topText: "20*" - property string bottomText: "Mostly cloudy" - property string weatherIcon: "sunny" - property real smallSide: (current.width < current.height ? current.width : current.height) - - Text { - text: current.topText - font.pointSize: 28 - anchors { - top: current.top - left: current.left - topMargin: 5 - leftMargin: 5 - } - } - - WeatherIcon { - weatherIcon: current.weatherIcon - anchors.centerIn: parent - anchors.verticalCenterOffset: -15 - width: current.smallSide - height: current.smallSide - } - - Text { - text: current.bottomText - font.pointSize: 23 - wrapMode: Text.WordWrap - width: parent.width - horizontalAlignment: Text.AlignRight - anchors { - bottom: current.bottom - right: current.right - rightMargin: 5 - } - } -} diff --git a/examples/positioning/weatherinfo/components/ForecastIcon.qml b/examples/positioning/weatherinfo/components/ForecastIcon.qml deleted file mode 100644 index ecf9a463..00000000 --- a/examples/positioning/weatherinfo/components/ForecastIcon.qml +++ /dev/null @@ -1,99 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -import QtQuick 2.0 - -Item { - id: top - - property string topText: "Mon" - property string weatherIcon: "sunny" - property string bottomText: "22*/23*" - - Text { - id: dayText - horizontalAlignment: Text.AlignHCenter - width: top.width - text: top.topText - - anchors.top: parent.top - anchors.topMargin: top.height / 5 - dayText.paintedHeight - anchors.horizontalCenter: parent.horizontalCenter - } - - WeatherIcon { - id: icon - weatherIcon: top.weatherIcon - - property real side: { - var h = 3 * top.height / 5 - if (top.width < h) - top.width; - else - h; - } - - width: icon.side - height: icon.side - - anchors.centerIn: parent - } - - Text { - id: tempText - horizontalAlignment: Text.AlignHCenter - width: top.width - text: top.bottomText - - anchors.bottom: parent.bottom - anchors.bottomMargin: top.height / 5 - tempText.paintedHeight - anchors.horizontalCenter: parent.horizontalCenter - } -} diff --git a/examples/positioning/weatherinfo/components/WeatherIcon.qml b/examples/positioning/weatherinfo/components/WeatherIcon.qml deleted file mode 100644 index 47832c0e..00000000 --- a/examples/positioning/weatherinfo/components/WeatherIcon.qml +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -import QtQuick 2.0 - -Item { - id: container - - property string weatherIcon: "sunny" - - Image { - id: img - source: "../icons/weather-" + container.weatherIcon + ".png" - smooth: true - anchors.fill: parent - } -} diff --git a/examples/positioning/weatherinfo/doc/images/example-weatherinfo.png b/examples/positioning/weatherinfo/doc/images/example-weatherinfo.png Binary files differdeleted file mode 100644 index 3a1c6c24..00000000 --- a/examples/positioning/weatherinfo/doc/images/example-weatherinfo.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/doc/src/weatherinfo.qdoc b/examples/positioning/weatherinfo/doc/src/weatherinfo.qdoc deleted file mode 100644 index 7d9cab6f..00000000 --- a/examples/positioning/weatherinfo/doc/src/weatherinfo.qdoc +++ /dev/null @@ -1,164 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:FDL$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Free Documentation License Usage -** Alternatively, this file may be used under the terms of the GNU Free -** Documentation License version 1.3 as published by the Free Software -** Foundation and appearing in the file included in the packaging of -** this file. Please review the following information to ensure -** the GNU Free Documentation License version 1.3 requirements -** will be met: https://www.gnu.org/licenses/fdl-1.3.html. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -/*! - \example weatherinfo - \title Weather Info (C++/QML) - - \brief The Weather Info example shows how to use the user's current position - to retrieve local content from a web service in a C++ plugin for QML. - - \ingroup qtpositioning-examples - - Key \l{Qt Positioning} classes used in this example: - - \list - \li \l{QGeoPositionInfo} - \li \l{QGeoPositionInfoSource} - \endlist - - \image ../images/example-weatherinfo.png - - \include examples-run.qdocinc - - \section1 Weather Data Providers - - The example uses two unrelated weather data providers: - - \list - \li \l {http://www.openweathermap.org}{OpenWeather} - \li \l {https://www.weatherapi.com/}{WeatherAPI.com} - \endlist - - The provider to be used is selected automatically at runtime and can be - changed if the selected provider is not available. However, it can't be - specified manually. - - \note Free plans are used for both providers, which implies certain - limitations on the amount of weather requests. If the limits are exceeded, - the providers become temporary unavailable. When both providers are - unavailable, the application would not be able to show any weather - information. In this case it is required to wait until at least one of the - providers becomes available again. - - \section1 Application Data Models - - The key part of this example is the application's data model, contained - in the \c WeatherData and \c AppModel classes. \c WeatherData represents - the weather information taken from the HTTP service. It is a simple data - class, but we use \l Q_PROPERTY to expose it nicely to QML later. - It also uses \l QML_ANONYMOUS macro, which makes it recognized in QML. - - \snippet weatherinfo/appmodel.h 0 - \snippet weatherinfo/appmodel.h 1 - - \c AppModel models the state of the entire application. At startup, we - get the platform's default position source using - \l QGeoPositionInfoSource::createDefaultSource(). - - \snippet weatherinfo/appmodel.cpp 0 - \snippet weatherinfo/appmodel.cpp 1 - - If no default source is available, we take a static position and fetch - weather for that. If, however, we do have a position source, we connect - its \l {QGeoPositionInfoSource::}{positionUpdated()} signal to a slot on the - \c AppModel and call \l {QGeoPositionInfoSource::}{startUpdates()}, which - begins regular updates of device position. - - When a position update is received, we use the longitude and latitude - of the returned coordinate to retrieve weather data for the specified - location. - - \snippet weatherinfo/appmodel.cpp 2 - - To inform the UI about this process, the \c cityChanged() signal is emitted - when a new city is used, and the \c weatherChanged() signal whenever a - weather update occurs. - - The model also uses \l QML_ELEMENT macro, which makes it available in QML. - - \snippet weatherinfo/appmodel.h 2 - \snippet weatherinfo/appmodel.h 3 - \snippet weatherinfo/appmodel.h 4 - - We use a \l QQmlListProperty for the weather forecast information, - which contains the weather forecast for the next days (the number of days - is provider-specific). This makes it easy to access the forecast from QML. - - \section1 Expose Custom Models to QML - - To expose the models to the QML UI layer, we use the \l QML_ELEMENT and - \l QML_ANONYMOUS macros. See the \l QQmlEngine class description for more - details on these macros. - - To make the types available in QML, we need to update our build accordingly. - - \section2 CMake Build - - For a CMake-based build, we need to add the following to the - \c {CMakeLists.txt}: - - \quotefromfile weatherinfo/CMakeLists.txt - \skipto qt_add_qml_module(weatherinfo - \printuntil ) - - \section2 qmake Build - - For a qmake build, we need to modify the \c {weatherinfo.pro} file in the - following way: - - \quotefromfile weatherinfo/weatherinfo.pro - \skipto CONFIG - \printuntil QML_IMPORT_MAJOR_VERSION - - \section1 Instantiate the Models in QML - - Finally, in the actual QML, we instantiate the \c {AppModel}: - - \snippet weatherinfo/weatherinfo.qml 0 - \snippet weatherinfo/weatherinfo.qml 1 - \snippet weatherinfo/weatherinfo.qml 2 - - Once the model is instantiated like this, we can use its properties - elsewhere in the QML document: - - \snippet weatherinfo/weatherinfo.qml 3 - \snippet weatherinfo/weatherinfo.qml 4 - - \section1 Files and Attributions - - The example bundles the following images from Third-Party sources: - - \table - \row - \li \l{weatherinfo-tango-icons}{Tango Icons} - \li Public Domain - \row - \li \l{weatherinfo-tango-weather-pack}{Tango Weather Icon Pack by Darkobra} - \li Public Domain - \endtable -*/ diff --git a/examples/positioning/weatherinfo/icons/README.txt b/examples/positioning/weatherinfo/icons/README.txt deleted file mode 100644 index d3841532..00000000 --- a/examples/positioning/weatherinfo/icons/README.txt +++ /dev/null @@ -1,5 +0,0 @@ -The scalable icons are from: - -http://tango.freedesktop.org/Tango_Icon_Library -http://darkobra.deviantart.com/art/Tango-Weather-Icon-Pack-98024429 - diff --git a/examples/positioning/weatherinfo/icons/qt_attribution.json b/examples/positioning/weatherinfo/icons/qt_attribution.json deleted file mode 100644 index 23bbe386..00000000 --- a/examples/positioning/weatherinfo/icons/qt_attribution.json +++ /dev/null @@ -1,28 +0,0 @@ -[ - { - "Id": "weatherinfo-tango-weather-pack", - "Name": "Tango Weather Icon Pack by Darkobra", - "QDocModule": "qtpositioning", - "QtUsage": "Used in the \"Weather Info (C++/QML)\" example", - "QtParts": [ "examples" ], - "Description": "74 brand spankin' new Tango icons for your viewing pleasure.", - "Homepage": "https://www.deviantart.com/darkobra/art/Tango-Weather-Icon-Pack-98024429", - - "License": "Public Domain", - "Copyright": "Copyright DarKobra (C Stark)", - "Files": "weather-fog.png weather-haze.png weather-icy.png weather-sleet.png weather-sunny.png weather-sunny-very-few-clouds.png weather-sunny-few-clouds.png weather-thunder-shower.png" - }, - { - "Id": "weatherinfo-tango-icons", - "Name": "Tango Icons", - "QDocModule": "qtpositioning", - "QtUsage": "Used in the \"Weather Info (C++/QML)\" example", - "QtParts": [ "examples" ], - "Description": "The Tango Desktop Project exists to help create a consistent graphical user interface experience for free and Open Source software. ", - "Homepage": "http://tango.freedesktop.org/Tango_Desktop_Project", - - "License": "Public Domain", - "Copyright": "Copyright Tango Project contributors", - "Files": "weather-few-clouds.png weather-overcast.png weather-showers.png weather-storm.png weather-snow.png weather-showers-scattered.png" - } -] diff --git a/examples/positioning/weatherinfo/icons/weather-few-clouds.png b/examples/positioning/weatherinfo/icons/weather-few-clouds.png Binary files differdeleted file mode 100644 index a8000ef0..00000000 --- a/examples/positioning/weatherinfo/icons/weather-few-clouds.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/icons/weather-fog.png b/examples/positioning/weatherinfo/icons/weather-fog.png Binary files differdeleted file mode 100644 index 9ffe9c4a..00000000 --- a/examples/positioning/weatherinfo/icons/weather-fog.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/icons/weather-haze.png b/examples/positioning/weatherinfo/icons/weather-haze.png Binary files differdeleted file mode 100644 index ae6f13eb..00000000 --- a/examples/positioning/weatherinfo/icons/weather-haze.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/icons/weather-icy.png b/examples/positioning/weatherinfo/icons/weather-icy.png Binary files differdeleted file mode 100644 index 18d20384..00000000 --- a/examples/positioning/weatherinfo/icons/weather-icy.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/icons/weather-overcast.png b/examples/positioning/weatherinfo/icons/weather-overcast.png Binary files differdeleted file mode 100644 index 9d50f77f..00000000 --- a/examples/positioning/weatherinfo/icons/weather-overcast.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/icons/weather-showers-scattered.png b/examples/positioning/weatherinfo/icons/weather-showers-scattered.png Binary files differdeleted file mode 100644 index d3e5b166..00000000 --- a/examples/positioning/weatherinfo/icons/weather-showers-scattered.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/icons/weather-showers.png b/examples/positioning/weatherinfo/icons/weather-showers.png Binary files differdeleted file mode 100644 index 07489aaa..00000000 --- a/examples/positioning/weatherinfo/icons/weather-showers.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/icons/weather-sleet.png b/examples/positioning/weatherinfo/icons/weather-sleet.png Binary files differdeleted file mode 100644 index 3d225895..00000000 --- a/examples/positioning/weatherinfo/icons/weather-sleet.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/icons/weather-snow.png b/examples/positioning/weatherinfo/icons/weather-snow.png Binary files differdeleted file mode 100644 index e7426e3c..00000000 --- a/examples/positioning/weatherinfo/icons/weather-snow.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/icons/weather-storm.png b/examples/positioning/weatherinfo/icons/weather-storm.png Binary files differdeleted file mode 100644 index 6b6b366a..00000000 --- a/examples/positioning/weatherinfo/icons/weather-storm.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/icons/weather-sunny-very-few-clouds.png b/examples/positioning/weatherinfo/icons/weather-sunny-very-few-clouds.png Binary files differdeleted file mode 100644 index 133bcb29..00000000 --- a/examples/positioning/weatherinfo/icons/weather-sunny-very-few-clouds.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/icons/weather-sunny.png b/examples/positioning/weatherinfo/icons/weather-sunny.png Binary files differdeleted file mode 100644 index 0fac921d..00000000 --- a/examples/positioning/weatherinfo/icons/weather-sunny.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/icons/weather-thundershower.png b/examples/positioning/weatherinfo/icons/weather-thundershower.png Binary files differdeleted file mode 100644 index 1aff1639..00000000 --- a/examples/positioning/weatherinfo/icons/weather-thundershower.png +++ /dev/null diff --git a/examples/positioning/weatherinfo/main.cpp b/examples/positioning/weatherinfo/main.cpp deleted file mode 100644 index a002a244..00000000 --- a/examples/positioning/weatherinfo/main.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -#include <QtGui/QGuiApplication> -#include <QtQuick/QQuickView> -#include <QtQml/QQmlEngine> -#include <QtQml/QQmlContext> -#include <QtQuick/QQuickItem> -#include <QLoggingCategory> - -#include "appmodel.h" - -int main(int argc, char *argv[]) -{ - QLoggingCategory::setFilterRules("wapp.*.debug=false"); - QGuiApplication application(argc, argv); - - const QString mainQmlApp = QStringLiteral("qrc:///weatherinfo.qml"); - QQuickView view; - view.setSource(QUrl(mainQmlApp)); - view.setResizeMode(QQuickView::SizeRootObjectToView); - - QObject::connect(view.engine(), SIGNAL(quit()), qApp, SLOT(quit())); - view.setGeometry(QRect(100, 100, 360, 640)); - view.show(); - return application.exec(); -} diff --git a/examples/positioning/weatherinfo/openweathermapbackend.cpp b/examples/positioning/weatherinfo/openweathermapbackend.cpp deleted file mode 100644 index 0a77579b..00000000 --- a/examples/positioning/weatherinfo/openweathermapbackend.cpp +++ /dev/null @@ -1,265 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -#include "openweathermapbackend.h" - -#include <QNetworkAccessManager> -#include <QNetworkReply> -#include <QUrlQuery> -#include <QJsonDocument> -#include <QJsonObject> -#include <QJsonValue> -#include <QJsonArray> -#include <QGeoCoordinate> -#include <QLoggingCategory> - -Q_DECLARE_LOGGING_CATEGORY(requestsLog) - -static constexpr auto kZeroKelvin = 273.15; - -static QString niceTemperatureString(double t) -{ - return QString::number(qRound(t - kZeroKelvin)) + QChar(0xB0); -} - -/* - Converts weather code to a string that will be used to show the icon. - The possible strings are based on the icon names. The icon name is built up - as follows: - weather-[mystring].png - where [mystring] is the value returned by this method. - Check resources for the full list of available icons. -*/ -static QString weatherCodeToString(const QString &code) -{ - if (code == u"01d" || code == u"01n") - return "sunny"; - else if (code == u"02d" || code == u"02n") - return "sunny-very-few-clouds"; - else if (code == u"03d" || code == u"03n") - return "few-clouds"; - else if (code == u"04d" || code == u"04n") - return "overcast"; - else if (code == u"09d" || code == u"09n" || code == u"10d" || code == u"10n") - return "showers"; - else if (code == u"11d" || code == u"11n") - return "thundershower"; - else if (code == u"13d" || code == u"13n") - return "snow"; - else if (code == u"50d" || code == u"50n") - return "fog"; - - return "sunny"; // default choice -} - -static void parseWeatherDescription(const QJsonObject &object, WeatherInfo &info) -{ - const QJsonArray weatherArray = object.value(u"weather").toArray(); - if (!weatherArray.isEmpty()) { - const QJsonObject obj = weatherArray.first().toObject(); - info.m_weatherDescription = obj.value(u"description").toString(); - info.m_weatherIconId = weatherCodeToString(obj.value(u"icon").toString()); - } else { - qCDebug(requestsLog, "An empty weather array is returned."); - } -} - -OpenWeatherMapBackend::OpenWeatherMapBackend(QObject *parent) - : ProviderBackend(parent), - m_networkManager(new QNetworkAccessManager(this)), - m_appId(QStringLiteral("36496bad1955bf3365448965a42b9eac")) -{ -} - -void OpenWeatherMapBackend::requestWeatherInfo(const QString &city) -{ - QUrlQuery query; - query.addQueryItem(QStringLiteral("q"), city); - - requestCurrentWeather(query, QGeoCoordinate()); -} - -void OpenWeatherMapBackend::requestWeatherInfo(const QGeoCoordinate &coordinate) -{ - QUrlQuery query; - query.addQueryItem(QStringLiteral("lat"), QString::number(coordinate.latitude())); - query.addQueryItem(QStringLiteral("lon"), QString::number(coordinate.longitude())); - - requestCurrentWeather(query, coordinate); -} - -void OpenWeatherMapBackend::handleCurrentWeatherReply(QNetworkReply *reply, - const QGeoCoordinate &coordinate) -{ - if (!reply) { - emit errorOccurred(); - return; - } - bool parsed = false; - if (!reply->error()) { - // extract info about current weather - const QJsonDocument document = QJsonDocument::fromJson(reply->readAll()); - const QJsonObject documentObject = document.object(); - - LocationInfo currentLocation; - currentLocation.m_name = documentObject.value(u"name").toString(); - if (coordinate.isValid()) - currentLocation.m_coordinate = coordinate; - qCDebug(requestsLog) << "Got current weather for" << currentLocation.m_name; - - WeatherInfo currentWeather; - - parseWeatherDescription(documentObject, currentWeather); - - const QJsonObject mainObject = documentObject.value(u"main").toObject(); - const QJsonValue tempValue = mainObject.value(u"temp"); - if (tempValue.isDouble()) - currentWeather.m_temperature = niceTemperatureString(tempValue.toDouble()); - else - qCDebug(requestsLog, "Failed to parse current temperature."); - - parsed = !currentLocation.m_name.isEmpty() && !currentWeather.m_temperature.isEmpty(); - - if (parsed) { - // request forecast - requestWeatherForecast(currentLocation, currentWeather); - } - } - if (!parsed) { - emit errorOccurred(); - if (reply->error()) - qCDebug(requestsLog) << reply->errorString(); - else - qCDebug(requestsLog, "Failed to parse current weather JSON."); - } - - reply->deleteLater(); -} - -void OpenWeatherMapBackend::handleWeatherForecastReply(QNetworkReply *reply, - const LocationInfo &location, - const WeatherInfo ¤tWeather) -{ - if (!reply) { - emit errorOccurred(); - return; - } - if (!reply->error()) { - QJsonDocument document = QJsonDocument::fromJson(reply->readAll()); - const QJsonObject documentObject = document.object(); - - QList<WeatherInfo> weatherDetails; - // current weather will be the first in the list - weatherDetails << currentWeather; - - QJsonArray daysList = documentObject.value(u"list").toArray(); - // include current day as well - for (qsizetype i = 0; i < daysList.size(); ++i) { - QJsonObject dayObject = daysList.at(i).toObject(); - WeatherInfo info; - - const QDateTime dt = QDateTime::fromSecsSinceEpoch(dayObject.value(u"dt").toInteger()); - info.m_dayOfWeek = dt.toString(u"ddd"); - - const QJsonObject tempObject = dayObject.value(u"temp").toObject(); - - const QJsonValue minTemp = tempObject.value(u"min"); - const QJsonValue maxTemp = tempObject.value(u"max"); - if (minTemp.isDouble() && maxTemp.isDouble()) { - info.m_temperature = niceTemperatureString(minTemp.toDouble()) + QChar('/') - + niceTemperatureString(maxTemp.toDouble()); - } else { - qCDebug(requestsLog, "Failed to parse min or max temperature."); - } - - parseWeatherDescription(dayObject, info); - - if (!info.m_temperature.isEmpty() && !info.m_weatherIconId.isEmpty()) - weatherDetails.push_back(info); - } - - emit weatherInformation(location, weatherDetails); - } else { - emit errorOccurred(); - qCDebug(requestsLog) << reply->errorString(); - } - - reply->deleteLater(); -} - -void OpenWeatherMapBackend::requestCurrentWeather(QUrlQuery &query, - const QGeoCoordinate &coordinate) -{ - QUrl url("http://api.openweathermap.org/data/2.5/weather"); - query.addQueryItem(QStringLiteral("mode"), QStringLiteral("json")); - query.addQueryItem(QStringLiteral("APPID"), m_appId); - url.setQuery(query); - - QNetworkReply *reply = m_networkManager->get(QNetworkRequest(url)); - connect(reply, &QNetworkReply::finished, this, - [this, reply, coordinate]() { handleCurrentWeatherReply(reply, coordinate); }); -} - -void OpenWeatherMapBackend::requestWeatherForecast(const LocationInfo &location, - const WeatherInfo ¤tWeather) -{ - QUrl url("http://api.openweathermap.org/data/2.5/forecast/daily"); - QUrlQuery query; - query.addQueryItem(QStringLiteral("q"), location.m_name); - query.addQueryItem(QStringLiteral("mode"), QStringLiteral("json")); - query.addQueryItem(QStringLiteral("cnt"), QStringLiteral("4")); - query.addQueryItem(QStringLiteral("APPID"), m_appId); - url.setQuery(query); - - QNetworkReply *reply = m_networkManager->get(QNetworkRequest(url)); - connect(reply, &QNetworkReply::finished, this, [this, reply, location, currentWeather]() { - handleWeatherForecastReply(reply, location, currentWeather); - }); -} diff --git a/examples/positioning/weatherinfo/openweathermapbackend.h b/examples/positioning/weatherinfo/openweathermapbackend.h deleted file mode 100644 index 7f4e5f97..00000000 --- a/examples/positioning/weatherinfo/openweathermapbackend.h +++ /dev/null @@ -1,85 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -#ifndef OPENWEATHERMAPBACKEND_H -#define OPENWEATHERMAPBACKEND_H - -#include "providerbackend.h" - -QT_BEGIN_NAMESPACE -class QNetworkAccessManager; -class QNetworkReply; -class QUrlQuery; -QT_END_NAMESPACE - -class OpenWeatherMapBackend : public ProviderBackend -{ - Q_OBJECT -public: - explicit OpenWeatherMapBackend(QObject *parent = nullptr); - ~OpenWeatherMapBackend() = default; - - void requestWeatherInfo(const QString &city) override; - void requestWeatherInfo(const QGeoCoordinate &coordinate) override; - -private slots: - void handleCurrentWeatherReply(QNetworkReply *reply, const QGeoCoordinate &coordinate); - void handleWeatherForecastReply(QNetworkReply *reply, const LocationInfo &location, - const WeatherInfo ¤tWeather); - -private: - void requestCurrentWeather(QUrlQuery &query, const QGeoCoordinate &coordinate); - void requestWeatherForecast(const LocationInfo &location, const WeatherInfo ¤tWeather); - - QNetworkAccessManager *m_networkManager; - const QString m_appId; -}; - -#endif // OPENWEATHERMAPBACKEND_H diff --git a/examples/positioning/weatherinfo/providerbackend.cpp b/examples/positioning/weatherinfo/providerbackend.cpp deleted file mode 100644 index 30c45209..00000000 --- a/examples/positioning/weatherinfo/providerbackend.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -#include "providerbackend.h" - -ProviderBackend::ProviderBackend(QObject *parent) : QObject(parent) { } diff --git a/examples/positioning/weatherinfo/providerbackend.h b/examples/positioning/weatherinfo/providerbackend.h deleted file mode 100644 index c59f6c49..00000000 --- a/examples/positioning/weatherinfo/providerbackend.h +++ /dev/null @@ -1,90 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -#ifndef PROVIDERBACKEND_H -#define PROVIDERBACKEND_H - -#include <QObject> -#include <QGeoCoordinate> - -struct WeatherInfo -{ - QString m_dayOfWeek; - QString m_weatherIconId; - QString m_weatherDescription; - QString m_temperature; -}; - -struct LocationInfo -{ - QString m_name; - QGeoCoordinate m_coordinate; -}; - -class ProviderBackend : public QObject -{ - Q_OBJECT -public: - explicit ProviderBackend(QObject *parent = nullptr); - - virtual void requestWeatherInfo(const QString &city) = 0; - virtual void requestWeatherInfo(const QGeoCoordinate &coordinate) = 0; - -signals: - // The first element in weatherDetails represents current weather. - // Next are the weather forecast, including the current day. - // The LocationInfo object should contain valid coordinate only when it was - // initially used to request the weather. If the city name was used, an - // empty coordinate is expected to be transferred. - void weatherInformation(const LocationInfo &location, const QList<WeatherInfo> &weatherDetails); - void errorOccurred(); -}; - -#endif // PROVIDERBACKEND_H diff --git a/examples/positioning/weatherinfo/weatherapibackend.cpp b/examples/positioning/weatherinfo/weatherapibackend.cpp deleted file mode 100644 index a98f7dbb..00000000 --- a/examples/positioning/weatherinfo/weatherapibackend.cpp +++ /dev/null @@ -1,266 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -#include "weatherapibackend.h" - -#include <QNetworkAccessManager> -#include <QNetworkReply> -#include <QUrlQuery> -#include <QJsonDocument> -#include <QJsonObject> -#include <QJsonValue> -#include <QJsonArray> -#include <QLoggingCategory> - -Q_DECLARE_LOGGING_CATEGORY(requestsLog) - -static QString niceTemperatureString(double t) -{ - return QString::number(qRound(t)) + QChar(0xB0); -} - -/* - Converts weather code to a string that will be used to show the icon. - The possible strings are based on the icon names. The icon name is built up - as follows: - weather-[mystring].png - where [mystring] is the value returned by this method. - Check resources for the full list of available icons. -*/ -static QString weatherCodeToString(int code) -{ - switch (code) { - case 1000: - return "sunny"; - case 1003: - return "sunny-very-few-clouds"; - case 1006: - return "few-clouds"; - case 1009: - return "overcast"; - case 1030: - case 1135: - case 1147: - return "fog"; - case 1063: - case 1072: - case 1150: - case 1153: - case 1168: - case 1171: - case 1180: - case 1183: - case 1186: - case 1189: - case 1198: - return "showers-scattered"; - case 1066: - case 1069: - case 1114: - case 1117: - case 1210: - case 1213: - case 1216: - case 1219: - case 1222: - case 1225: - case 1237: - case 1255: - case 1258: - case 1261: - case 1264: - case 1279: - case 1282: - return "snow"; - case 1087: - return "storm"; - case 1192: - case 1195: - case 1201: - case 1240: - case 1243: - case 1246: - return "showers"; - case 1204: - case 1207: - case 1249: - case 1252: - return "sleet"; - case 1273: - case 1276: - return "thundershower"; - default: - return "sunny"; - } - - return "sunny"; // default choice -} - -static void parseWeatherDescription(const QJsonObject &object, WeatherInfo &info) -{ - const QJsonObject conditionObject = object.value(u"condition").toObject(); - info.m_weatherDescription = conditionObject.value(u"text").toString(); - info.m_weatherIconId = weatherCodeToString(conditionObject.value(u"code").toInt()); -} - -WeatherApiBackend::WeatherApiBackend(QObject *parent) - : ProviderBackend(parent), - m_networkManager(new QNetworkAccessManager(this)), - m_apiKey(QStringLiteral("8edde160c63c4be6b77101828211208")) -{ -} - -void WeatherApiBackend::requestWeatherInfo(const QString &city) -{ - generateWeatherRequest(city, QGeoCoordinate()); -} - -void WeatherApiBackend::requestWeatherInfo(const QGeoCoordinate &coordinate) -{ - const QString coordinateStr = - QString("%1,%2").arg(coordinate.latitude()).arg(coordinate.longitude()); - generateWeatherRequest(coordinateStr, coordinate); -} - -void WeatherApiBackend::handleWeatherForecastReply(QNetworkReply *reply, - const QGeoCoordinate &coordinate) -{ - if (!reply) { - emit errorOccurred(); - return; - } - bool parsed = false; - if (!reply->error()) { - QJsonDocument document = QJsonDocument::fromJson(reply->readAll()); - const QJsonObject documentObject = document.object(); - const QJsonObject locationObject = documentObject.value(u"location").toObject(); - - // extract location name - LocationInfo location; - location.m_name = locationObject.value(u"name").toString(); - if (coordinate.isValid()) - location.m_coordinate = coordinate; - qCDebug(requestsLog) << "Got weather for" << location.m_name; - - // extract current weather - WeatherInfo currentWeather; - - const QJsonObject currentWeatherObject = documentObject.value(u"current").toObject(); - const QJsonValue temperature = currentWeatherObject.value(u"temp_c"); - if (temperature.isDouble()) - currentWeather.m_temperature = niceTemperatureString(temperature.toDouble()); - - parseWeatherDescription(currentWeatherObject, currentWeather); - - parsed = !location.m_name.isEmpty() && !currentWeather.m_temperature.isEmpty(); - - if (parsed) { - QList<WeatherInfo> weatherDetails; - weatherDetails << currentWeather; - - // extract forecast details - const QJsonObject forecastObject = documentObject.value(u"forecast").toObject(); - const QJsonArray forecastDays = forecastObject.value(u"forecastday").toArray(); - for (qsizetype i = 0; i < forecastDays.size(); ++i) { - const QJsonObject dayInfo = forecastDays.at(i).toObject(); - WeatherInfo info; - - const QDateTime dt = - QDateTime::fromSecsSinceEpoch(dayInfo.value(u"date_epoch").toInteger()); - info.m_dayOfWeek = dt.toString(u"ddd"); - - const QJsonObject dayObject = dayInfo.value(u"day").toObject(); - const QJsonValue minTemp = dayObject.value(u"mintemp_c"); - const QJsonValue maxTemp = dayObject.value(u"maxtemp_c"); - if (minTemp.isDouble() && maxTemp.isDouble()) { - info.m_temperature = niceTemperatureString(minTemp.toDouble()) + QChar('/') - + niceTemperatureString(maxTemp.toDouble()); - } else { - qCDebug(requestsLog, "Failed to parse min or max temperature."); - } - - parseWeatherDescription(dayObject, info); - - if (!info.m_temperature.isEmpty() && !info.m_weatherIconId.isEmpty()) - weatherDetails.push_back(info); - } - - emit weatherInformation(location, weatherDetails); - } - } - if (!parsed) { - emit errorOccurred(); - if (reply->error()) - qCDebug(requestsLog) << reply->errorString(); - else - qCDebug(requestsLog, "Failed to parse weather JSON."); - } - - reply->deleteLater(); -} - -void WeatherApiBackend::generateWeatherRequest(const QString &locationString, - const QGeoCoordinate &coordinate) -{ - QUrl url("https://api.weatherapi.com/v1/forecast.json"); - - QUrlQuery query; - query.addQueryItem(QStringLiteral("key"), m_apiKey); - query.addQueryItem(QStringLiteral("q"), locationString); - query.addQueryItem(QStringLiteral("days"), QStringLiteral("4")); - query.addQueryItem(QStringLiteral("aqi"), QStringLiteral("no")); - query.addQueryItem(QStringLiteral("alerts"), QStringLiteral("no")); - - url.setQuery(query); - - QNetworkReply *reply = m_networkManager->get(QNetworkRequest(url)); - connect(reply, &QNetworkReply::finished, this, - [this, reply, coordinate]() { handleWeatherForecastReply(reply, coordinate); }); -} diff --git a/examples/positioning/weatherinfo/weatherapibackend.h b/examples/positioning/weatherinfo/weatherapibackend.h deleted file mode 100644 index dce078a1..00000000 --- a/examples/positioning/weatherinfo/weatherapibackend.h +++ /dev/null @@ -1,80 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -#ifndef WEATHERAPIBACKEND_H -#define WEATHERAPIBACKEND_H - -#include "providerbackend.h" - -QT_BEGIN_NAMESPACE -class QNetworkAccessManager; -class QNetworkReply; -QT_END_NAMESPACE - -class WeatherApiBackend : public ProviderBackend -{ - Q_OBJECT -public: - explicit WeatherApiBackend(QObject *parent = nullptr); - - void requestWeatherInfo(const QString &city) override; - void requestWeatherInfo(const QGeoCoordinate &coordinate) override; - -private slots: - void handleWeatherForecastReply(QNetworkReply *reply, const QGeoCoordinate &coordinate); - -private: - void generateWeatherRequest(const QString &locationString, const QGeoCoordinate &coordinate); - - QNetworkAccessManager *m_networkManager; - const QString m_apiKey; -}; - -#endif // WEATHERAPIBACKEND_H diff --git a/examples/positioning/weatherinfo/weatherinfo.pro b/examples/positioning/weatherinfo/weatherinfo.pro deleted file mode 100644 index eb88ca42..00000000 --- a/examples/positioning/weatherinfo/weatherinfo.pro +++ /dev/null @@ -1,31 +0,0 @@ -TEMPLATE = app -TARGET = weatherinfo - -QT += core network positioning qml quick - -CONFIG += qmltypes -QML_IMPORT_NAME = WeatherInfo -QML_IMPORT_MAJOR_VERSION = 1 - -SOURCES += main.cpp \ - appmodel.cpp \ - openweathermapbackend.cpp \ - providerbackend.cpp \ - weatherapibackend.cpp - -OTHER_FILES += weatherinfo.qml \ - components/WeatherIcon.qml \ - components/ForecastIcon.qml \ - components/BigForecastIcon.qml \ - icons/* - - -RESOURCES += weatherinfo.qrc - -HEADERS += appmodel.h \ - openweathermapbackend.h \ - providerbackend.h \ - weatherapibackend.h - -target.path = $$[QT_INSTALL_EXAMPLES]/positioning/weatherinfo -INSTALLS += target diff --git a/examples/positioning/weatherinfo/weatherinfo.qml b/examples/positioning/weatherinfo/weatherinfo.qml deleted file mode 100644 index 62bbe7d7..00000000 --- a/examples/positioning/weatherinfo/weatherinfo.qml +++ /dev/null @@ -1,208 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, 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 The Qt Company Ltd 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$ -** -****************************************************************************/ - -import QtQuick 2.0 -import "components" -//! [0] -import WeatherInfo 1.0 - -Item { - id: window -//! [0] - width: 360 - height: 640 - - state: "loading" - - states: [ - State { - name: "loading" - PropertyChanges { target: main; opacity: 0 } - PropertyChanges { target: wait; opacity: 1 } - }, - State { - name: "ready" - PropertyChanges { target: main; opacity: 1 } - PropertyChanges { target: wait; opacity: 0 } - } - ] -//! [1] - AppModel { - id: appModel - onReadyChanged: { - if (appModel.ready) - window.state = "ready" - else - window.state = "loading" - } - } -//! [1] - Item { - id: wait - anchors.fill: parent - - Text { - text: "Loading weather data..." - anchors.centerIn: parent - font.pointSize: 18 - } - } - - Item { - id: main - anchors.fill: parent - - Column { - spacing: 6 - - anchors { - fill: parent - topMargin: 6; bottomMargin: 6; leftMargin: 6; rightMargin: 6 - } - - Rectangle { - width: parent.width - height: 25 - color: "lightgrey" - - Text { - text: (appModel.hasValidCity ? appModel.city : "Unknown location") + (appModel.useGps ? " (GPS)" : "") - anchors.fill: parent - horizontalAlignment: Text.AlignHCenter - verticalAlignment: Text.AlignVCenter - } - - MouseArea { - anchors.fill: parent - onClicked: { - if (appModel.useGps) { - appModel.useGps = false - appModel.city = "Brisbane" - } else { - switch (appModel.city) { - case "Brisbane": - appModel.city = "Oslo" - break - case "Oslo": - appModel.city = "Helsinki" - break - case "Helsinki": - appModel.city = "New York" - break - case "New York": - appModel.useGps = true - break - } - } - } - } - } - -//! [3] - BigForecastIcon { - id: current - - width: main.width -12 - height: 2 * (main.height - 25 - 12) / 3 - - weatherIcon: (appModel.hasValidWeather - ? appModel.weather.weatherIcon - : "sunny") -//! [3] - topText: (appModel.hasValidWeather - ? appModel.weather.temperature - : "??") - bottomText: (appModel.hasValidWeather - ? appModel.weather.weatherDescription - : "No weather data") - - MouseArea { - anchors.fill: parent - onClicked: { - appModel.refreshWeather() - } - } -//! [4] - } -//! [4] - - Row { - id: iconRow - spacing: 6 - - width: main.width - 12 - height: (main.height - 25 - 24) / 3 - - property int daysCount: appModel.forecast.length - property real iconWidth: (daysCount > 0) ? (iconRow.width / daysCount) - 10 - : iconRow.width - property real iconHeight: iconRow.height - - Repeater { - model: appModel.forecast - ForecastIcon { - id: forecast1 - width: iconRow.iconWidth - height: iconRow.iconHeight - - topText: (appModel.hasValidWeather ? - modelData.dayOfWeek : "??") - bottomText: (appModel.hasValidWeather ? - modelData.temperature : "??/??") - weatherIcon: (appModel.hasValidWeather ? - modelData.weatherIcon : "sunny") - } - } - } - } - } -//! [2] -} -//! [2] diff --git a/examples/positioning/weatherinfo/weatherinfo.qrc b/examples/positioning/weatherinfo/weatherinfo.qrc deleted file mode 100644 index 1578fa9d..00000000 --- a/examples/positioning/weatherinfo/weatherinfo.qrc +++ /dev/null @@ -1,21 +0,0 @@ -<RCC> - <qresource prefix="/"> - <file>weatherinfo.qml</file> - <file>components/BigForecastIcon.qml</file> - <file>components/ForecastIcon.qml</file> - <file>components/WeatherIcon.qml</file> - <file>icons/weather-few-clouds.png</file> - <file>icons/weather-fog.png</file> - <file>icons/weather-haze.png</file> - <file>icons/weather-icy.png</file> - <file>icons/weather-overcast.png</file> - <file>icons/weather-showers.png</file> - <file>icons/weather-sleet.png</file> - <file>icons/weather-snow.png</file> - <file>icons/weather-storm.png</file> - <file>icons/weather-sunny-very-few-clouds.png</file> - <file>icons/weather-sunny.png</file> - <file>icons/weather-thundershower.png</file> - <file>icons/weather-showers-scattered.png</file> - </qresource> -</RCC> |