File indexing completed on 2024-05-19 05:55:49

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com>
0003  * SPDX-FileCopyrightText: 2020 Devin Lin <espidev@gmail.com>
0004  * SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de>
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include <QAbstractListModel>
0012 #include <QColor>
0013 #include <QDateTime>
0014 #include <QDebug>
0015 #include <QJsonDocument>
0016 #include <QJsonObject>
0017 #include <QObject>
0018 #include <QString>
0019 #include <QTimeZone>
0020 #include <QTimer>
0021 #include <qqmlregistration.h>
0022 
0023 #include <KWeatherCore/WeatherForecastSource>
0024 
0025 #include <utility>
0026 
0027 class WeatherLocation : public QObject
0028 {
0029     Q_OBJECT
0030     QML_ELEMENT
0031     Q_PROPERTY(QString name READ locationName CONSTANT)
0032     Q_PROPERTY(float latitude READ latitude CONSTANT)
0033     Q_PROPERTY(float longitude READ longitude CONSTANT)
0034     Q_PROPERTY(QString lastUpdated READ lastUpdatedFormatted NOTIFY lastUpdatedChanged)
0035     Q_PROPERTY(QString currentTime READ currentTimeFormatted NOTIFY currentTimeChanged)
0036     Q_PROPERTY(QString currentDate READ currentDateFormatted NOTIFY currentDateChanged)
0037     Q_PROPERTY(QString timeZone READ timeZone NOTIFY timeZoneChanged)
0038     Q_PROPERTY(QVariant currentHourForecast READ currentHourForecast NOTIFY currentForecastChanged)
0039 
0040     Q_PROPERTY(QVariantList dayForecasts READ dayForecasts NOTIFY dayForecastsChanged)
0041     Q_PROPERTY(QVariantList hourForecasts READ hourForecasts NOTIFY hourForecastsChanged)
0042     Q_PROPERTY(int selectedDay READ selectedDay WRITE setSelectedDay NOTIFY selectedDayChanged)
0043 
0044     Q_PROPERTY(QString backgroundComponent READ backgroundComponent NOTIFY currentForecastChanged)
0045     Q_PROPERTY(QColor backgroundColor READ backgroundColor NOTIFY currentForecastChanged)
0046     Q_PROPERTY(QColor textColor READ textColor NOTIFY currentForecastChanged)
0047     Q_PROPERTY(QColor cardBackgroundColor READ cardBackgroundColor NOTIFY currentForecastChanged)
0048     Q_PROPERTY(QColor cardTextColor READ cardTextColor NOTIFY currentForecastChanged)
0049     Q_PROPERTY(QColor cardSecondaryTextColor READ cardSecondaryTextColor NOTIFY currentForecastChanged)
0050     Q_PROPERTY(QColor iconColor READ iconColor NOTIFY currentForecastChanged)
0051     Q_PROPERTY(bool darkTheme READ darkTheme NOTIFY currentForecastChanged)
0052 
0053     Q_PROPERTY(bool cloud READ cloud NOTIFY currentForecastChanged)
0054     Q_PROPERTY(bool sun READ sun NOTIFY currentForecastChanged)
0055     Q_PROPERTY(bool rain READ rain NOTIFY currentForecastChanged)
0056     Q_PROPERTY(bool star READ star NOTIFY currentForecastChanged)
0057     Q_PROPERTY(bool snow READ snow NOTIFY currentForecastChanged)
0058     Q_PROPERTY(QColor topColor READ topColor NOTIFY currentForecastChanged)
0059     Q_PROPERTY(QColor bottomColor READ bottomColor NOTIFY currentForecastChanged)
0060     Q_PROPERTY(QColor cloudColor READ cloudColor NOTIFY currentForecastChanged)
0061 
0062 public:
0063     explicit WeatherLocation(QString locationId,
0064                              QString locationName,
0065                              QString timeZone,
0066                              float latitude = 0,
0067                              float longitude = 0,
0068                              KWeatherCore::WeatherForecast forecast = {});
0069     explicit WeatherLocation(); // when creating empty locations, don't persist
0070 
0071     static WeatherLocation *load(const QString &groupName);
0072 
0073     void save();
0074     Q_INVOKABLE void update();
0075     void saveOrder(int index); // for restoring order of locations
0076     int index();
0077     void deleteConfig();
0078 
0079     const QString &locationId() const;
0080     const QString &locationName() const;
0081     const QString &timeZone() const;
0082     float latitude() const;
0083     float longitude() const;
0084     QString lastUpdatedFormatted() const;
0085     const QDateTime &lastUpdated() const;
0086     QString currentTimeFormatted() const;
0087     QString currentDateFormatted() const;
0088     QDateTime currentDateTime() const;
0089     const QString &backgroundComponent() const;
0090     const QColor &backgroundColor() const;
0091     const QColor &textColor() const;
0092     const QColor &cardBackgroundColor() const;
0093     const QColor &cardTextColor() const;
0094     const QColor &cardSecondaryTextColor() const;
0095     const QColor &topColor() const;
0096     const QColor &bottomColor() const;
0097     const QColor &cloudColor() const;
0098     bool rain() const;
0099     bool cloud() const;
0100     bool star() const;
0101     bool sun() const;
0102     bool snow() const;
0103 
0104     const QColor &iconColor() const;
0105     bool darkTheme() const;
0106     QVariantList dayForecasts() const;
0107     QVariantList hourForecasts() const;
0108     int selectedDay() const;
0109     void setSelectedDay(int selectedDay);
0110     QVariant currentHourForecast();
0111 
0112 public Q_SLOTS:
0113     void updateData(KWeatherCore::WeatherForecast forecasts);
0114 
0115 Q_SIGNALS:
0116     void weatherRefresh(KWeatherCore::WeatherForecast forecasts); // sent when weather data is refreshed
0117     void currentForecastChanged();
0118     void stopLoadingIndicator();
0119     void currentTimeChanged();
0120     void currentDateChanged();
0121     void timeZoneChanged();
0122     void dayForecastsChanged();
0123     void hourForecastsChanged();
0124     void selectedDayChanged();
0125     void lastUpdatedChanged();
0126 
0127     void chartListChanged();
0128 
0129 private Q_SLOTS:
0130     void updateCurrentDateTime();
0131 
0132 private:
0133     void determineCurrentForecast();
0134 
0135     KWeatherCore::WeatherForecastSource m_source;
0136     KWeatherCore::WeatherForecast m_forecast;
0137 
0138     // background related fields
0139     QColor m_backgroundColor;
0140     QColor m_textColor;
0141     QColor m_cardBackgroundColor;
0142     QColor m_cardTextColor;
0143     QColor m_cardSecondaryTextColor;
0144     QColor m_iconColor;
0145     QString m_backgroundComponent = QStringLiteral("qrc:/qt/qml/org/kde/kweather/backgrounds/qml/backgrounds/ClearDay.qml");
0146     bool m_isDarkTheme = false;
0147     bool m_rain = false;
0148     bool m_snow = false;
0149     bool m_sun = false;
0150     bool m_star = false;
0151     bool m_cloud = false;
0152     QColor m_cloudColor;
0153     QColor m_topColor;
0154     QColor m_bottomColor;
0155 
0156     QString m_locationName, m_locationId;
0157     QString m_timeZone;
0158     QDateTime m_lastUpdated;
0159     QTimer *m_timer = nullptr;
0160     float m_latitude, m_longitude;
0161 
0162     QVariantList m_dayForecasts;
0163     QVariantList m_hourForecasts;
0164     int m_selectedDay = 0;
0165 };