weatherlocation.h 5.76 KB
Newer Older
Han Young's avatar
Han Young committed
1
/*
2
3
 * SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com>
 * SPDX-FileCopyrightText: 2020 Devin Lin <espidev@gmail.com>
4
 * SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de>
Han Young's avatar
Han Young committed
5
6
7
8
9
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#pragma once
Han Young's avatar
Han Young committed
10
#include <KWeatherCore/WeatherForecastSource>
Han Young's avatar
Han Young committed
11
#include <QAbstractListModel>
Nicolas Fella's avatar
Nicolas Fella committed
12
#include <QColor>
13
#include <QDateTime>
14
#include <QDebug>
Han Young's avatar
Han Young committed
15
16
17
#include <QJsonDocument>
#include <QJsonObject>
#include <QObject>
18
19
20
#include <QTimeZone>
#include <QTimer>
#include <utility>
Han Young's avatar
Han Young committed
21
22
23
24
25
26
27
28
29

namespace QtCharts
{
class QAbstractSeries;
class QSplineSeries;
class QDateTimeAxis;
class QValueAxis;
}

Han Young's avatar
Han Young committed
30
31
32
33
34
class WeatherLocation : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ locationName NOTIFY propertyChanged)
    Q_PROPERTY(QString lastUpdated READ lastUpdatedFormatted NOTIFY propertyChanged)
35
36
    Q_PROPERTY(QString currentTime READ currentTimeFormatted NOTIFY currentTimeChanged)
    Q_PROPERTY(QString currentDate READ currentDateFormatted NOTIFY currentDateChanged)
Nicolas Fella's avatar
Nicolas Fella committed
37
    Q_PROPERTY(QVariantList dayForecasts READ dayForecasts NOTIFY dayForecastsChanged)
Nicolas Fella's avatar
Nicolas Fella committed
38
39
    Q_PROPERTY(QVariantList hourForecasts READ hourForecasts NOTIFY hourForecastsChanged)
    Q_PROPERTY(int selectedDay READ selectedDay WRITE setSelectedDay NOTIFY selectedDayChanged)
Nicolas Fella's avatar
Nicolas Fella committed
40
    Q_PROPERTY(KWeatherCore::DailyWeatherForecast todayForecast READ todayForecast NOTIFY dayForecastsChanged)
Han Young's avatar
Han Young committed
41
42

    Q_PROPERTY(QString backgroundComponent READ backgroundComponent NOTIFY currentForecastChange)
43
44
45
46
47
    Q_PROPERTY(QColor backgroundColor READ backgroundColor NOTIFY currentForecastChange)
    Q_PROPERTY(QColor textColor READ textColor NOTIFY currentForecastChange)
    Q_PROPERTY(QColor cardBackgroundColor READ cardBackgroundColor NOTIFY currentForecastChange)
    Q_PROPERTY(QColor cardTextColor READ cardTextColor NOTIFY currentForecastChange)
    Q_PROPERTY(QColor iconColor READ iconColor NOTIFY currentForecastChange)
Han Young's avatar
Han Young committed
48
49
    Q_PROPERTY(bool darkTheme READ darkTheme NOTIFY currentForecastChange)

Han Young's avatar
Han Young committed
50
public:
51
52
53
54
55
    explicit WeatherLocation(QString locationId,
                             QString locationName,
                             QString timeZone,
                             float latitude,
                             float longitude,
56
                             KWeatherCore::WeatherForecast forecast = {});
Han Young's avatar
Han Young committed
57
    void save();
Han Young's avatar
Han Young committed
58
    static WeatherLocation *load(const QString &groupName);
59
    Q_INVOKABLE void update();
Han Young's avatar
Han Young committed
60

61
62
63
64
65
66
67
68
69
70
71
72
    const QString &locationId() const
    {
        return m_locationId;
    }
    const QString &locationName() const
    {
        return m_locationName;
    }
    const QString &timeZone() const
    {
        return m_timeZone;
    };
73
    float latitude() const
74
75
76
    {
        return m_latitude;
    }
77
    float longitude() const
78
79
80
81
82
83
84
85
86
87
88
89
90
    {
        return m_longitude;
    }
    QString lastUpdatedFormatted() const
    {
        return lastUpdated().toString("hh:mm ap");
    }
    const QDateTime &lastUpdated() const
    {
        return m_lastUpdated;
    }
    QString currentTimeFormatted() const
    {
Han Young's avatar
Han Young committed
91
        return currentDateTime().toString("hh:mm ap");
92
93
94
    }
    QString currentDateFormatted() const
    {
Han Young's avatar
Han Young committed
95
        return currentDateTime().toString("dd MMM yyyy");
96
    }
Han Young's avatar
Han Young committed
97
    QDateTime currentDateTime() const
98
    {
Han Young's avatar
Han Young committed
99
        return QDateTime::currentDateTime().toTimeZone(QTimeZone(m_timeZone.toUtf8()));
100
101
102
103
104
105
106
107
108
109
    }
    void setLastUpdated(QDateTime lastUpdated)
    {
        m_lastUpdated = std::move(lastUpdated);
        emit propertyChanged();
    }
    const QString &backgroundComponent() const
    {
        return m_backgroundComponent;
    };
110
    const QColor &backgroundColor() const
111
112
113
    {
        return m_backgroundColor;
    };
114
    const QColor &textColor() const
115
116
117
    {
        return m_textColor;
    };
118
    const QColor &cardBackgroundColor() const
119
120
121
    {
        return m_cardBackgroundColor;
    };
122
    const QColor &cardTextColor() const
123
124
125
126
    {
        return m_cardTextColor;
    };

127
    const QColor &iconColor() const
128
129
130
    {
        return m_iconColor;
    }
Han Young's avatar
Han Young committed
131
132
133
134
    bool darkTheme() const
    {
        return m_isDarkTheme;
    }
Nicolas Fella's avatar
Nicolas Fella committed
135
136
137
138
    QVariantList dayForecasts() const
    {
        return m_dayForecasts;
    }
Nicolas Fella's avatar
Nicolas Fella committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    QVariantList hourForecasts() const
    {
        return m_hourForecasts;
    }
    int selectedDay() const
    {
        return m_selectedDay;
    }
    void setSelectedDay(int selectedDay)
    {
        if (selectedDay != m_selectedDay) {
            m_selectedDay = selectedDay;
            Q_EMIT selectedDayChanged();
        }
    }
Nicolas Fella's avatar
Nicolas Fella committed
154
    KWeatherCore::DailyWeatherForecast todayForecast() const;
Han Young's avatar
Han Young committed
155

Han Young's avatar
Han Young committed
156
157
158
    // for restore order of locations
    void saveOrder(int index);
    int index();
159
    void deleteConfig();
160
public slots:
161
    void updateData(KWeatherCore::WeatherForecast forecasts);
162
163

signals:
164
    void weatherRefresh(KWeatherCore::WeatherForecast forecasts); // sent when weather data is refreshed
Han Young's avatar
Han Young committed
165
166
167
    void currentForecastChange();
    void propertyChanged(); // avoid warning
    void stopLoadingIndicator();
168
169
    void currentTimeChanged();
    void currentDateChanged();
Nicolas Fella's avatar
Nicolas Fella committed
170
    void dayForecastsChanged();
Nicolas Fella's avatar
Nicolas Fella committed
171
172
    void hourForecastsChanged();
    void selectedDayChanged();
Han Young's avatar
Han Young committed
173

174
175
176
    void chartListChanged();
private slots:
    void updateCurrentDateTime();
177

Han Young's avatar
Han Young committed
178
private:
179
180
181
    void determineCurrentForecast();

    KWeatherCore::WeatherForecastSource m_source;
182
    KWeatherCore::WeatherForecast m_forecast;
183

Han Young's avatar
Han Young committed
184
    // background related fields
185
186
187
188
189
    QColor m_backgroundColor;
    QColor m_textColor;
    QColor m_cardBackgroundColor;
    QColor m_cardTextColor;
    QColor m_iconColor;
Han Young's avatar
Han Young committed
190
    QString m_backgroundComponent = QStringLiteral("backgrounds/ClearDay.qml");
Han Young's avatar
Han Young committed
191
    bool m_isDarkTheme = false;
Han Young's avatar
Han Young committed
192

Han Young's avatar
Han Young committed
193
194
195
    QString m_locationName, m_locationId;
    QString m_timeZone;
    QDateTime m_lastUpdated;
196
197
    QTimer *m_timer;
    float m_latitude, m_longitude;
Han Young's avatar
Han Young committed
198

Nicolas Fella's avatar
Nicolas Fella committed
199
    QVariantList m_dayForecasts;
Nicolas Fella's avatar
Nicolas Fella committed
200
    QVariantList m_hourForecasts;
201
    int m_selectedDay = 0;
Han Young's avatar
Han Young committed
202
};