Commit 4f9aebf0 authored by Nicolas Fella's avatar Nicolas Fella
Browse files

Adapt to KWeatherCore API change

parent d70d0118
Pipeline #73069 passed with stage
in 17 seconds
......@@ -6,7 +6,7 @@
#include <QProcess>
QVariant HourlyModel::data(const QModelIndex &index, int role) const
{
if (!m_location || index.row() < 0 || index.row() >= rowCount(QModelIndex()))
if (index.row() < 0 || index.row() >= rowCount(QModelIndex()))
return {};
switch (role) {
......@@ -27,17 +27,14 @@ QVariant HourlyModel::data(const QModelIndex &index, int role) const
int HourlyModel::rowCount(const QModelIndex &index) const
{
Q_UNUSED(index)
if (!m_location)
return 0;
else
return 24;
return 24;
}
QHash<int, QByteArray> HourlyModel::roleNames() const
{
return {{Time, "time"}, {Icon, "weatherIcon"}, {Description, "description"},
{Temperature, "temperature"}, {Precipitation, "precipitation"}};
}
void HourlyModel::loadForecast(QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast> forecast)
void HourlyModel::loadForecast(KWeatherCore::WeatherForecast forecast)
{
beginResetModel();
m_location = std::move(forecast);
......@@ -48,7 +45,7 @@ const KWeatherCore::HourlyWeatherForecast &HourlyModel::getNthHour(int index) co
{
auto dayIndex {0};
auto hourIndex {0};
for (const auto &day : m_location->dailyWeatherForecast()) {
for (const auto &day : m_location.dailyWeatherForecast()) {
if ((int)day.hourlyWeatherForecast().size() - 1 < index) {
index -= day.hourlyWeatherForecast().size();
dayIndex++;
......@@ -58,7 +55,7 @@ const KWeatherCore::HourlyWeatherForecast &HourlyModel::getNthHour(int index) co
}
}
return m_location->dailyWeatherForecast().at(dayIndex).hourlyWeatherForecast().at(hourIndex);
return m_location.dailyWeatherForecast().at(dayIndex).hourlyWeatherForecast().at(hourIndex);
}
const QString &HourlyModel::currentIcon() const
......
......@@ -23,12 +23,12 @@ public:
const QString &currentDescription() const;
Q_INVOKABLE void openKWeather();
public Q_SLOTS:
void loadForecast(QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast>);
void loadForecast(KWeatherCore::WeatherForecast);
Q_SIGNALS:
void reseted();
private:
const KWeatherCore::HourlyWeatherForecast &getNthHour(int index) const;
QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast> m_location;
KWeatherCore::WeatherForecast m_location;
};
#endif // HOURLYNMODEL_H
......@@ -79,11 +79,11 @@ void KWeather_1x4::setLocation(const QString &location)
}
bool KWeather_1x4::hasForecast() const
{
return m_forecast && !m_forecast->dailyWeatherForecast().empty() && !m_forecast->dailyWeatherForecast().front().hourlyWeatherForecast().empty();
return !m_forecast.dailyWeatherForecast().empty() && !m_forecast.dailyWeatherForecast().front().hourlyWeatherForecast().empty();
}
const KWeatherCore::HourlyWeatherForecast &KWeather_1x4::getFirst() const
{
return m_forecast->dailyWeatherForecast().front().hourlyWeatherForecast().front();
return m_forecast.dailyWeatherForecast().front().hourlyWeatherForecast().front();
}
QString KWeather_1x4::location() const
{
......
......@@ -49,7 +49,7 @@ private:
bool m_needLocation = true;
QString m_location;
double m_latitude, m_longitude;
QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast> m_forecast;
KWeatherCore::WeatherForecast m_forecast;
HourlyModel *m_hourlyModel;
};
......
......@@ -15,7 +15,7 @@ WeatherDay::WeatherDay(WeatherDayListModel *parent)
connect(parent, &WeatherDayListModel::weatherRefresh, this, &WeatherDay::refreshDaysFromForecasts);
}
WeatherDay::WeatherDay(SharedForecastPtr ptr, int day, WeatherDayListModel *parent)
WeatherDay::WeatherDay(KWeatherCore::WeatherForecast ptr, int day, WeatherDayListModel *parent)
: QObject(parent)
, m_day(day)
, m_forecast(std::move(ptr))
......@@ -45,7 +45,7 @@ void WeatherDay::determineSunrise()
m_moonPhase = "New Moon";
}
}
void WeatherDay::refreshDaysFromForecasts(SharedForecastPtr ptr)
void WeatherDay::refreshDaysFromForecasts(KWeatherCore::WeatherForecast ptr)
{
m_forecast = std::move(ptr);
determineSunrise();
......
......@@ -9,7 +9,6 @@
#include "global.h"
#include <KWeatherCore/WeatherForecast>
#include <QObject>
using SharedForecastPtr = QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast>;
class WeatherDayListModel;
class WeatherDay : public QObject
{
......@@ -30,7 +29,7 @@ class WeatherDay : public QObject
public:
WeatherDay(WeatherDayListModel *parent = nullptr);
explicit WeatherDay(SharedForecastPtr ptr, int day, WeatherDayListModel *parent = nullptr);
explicit WeatherDay(KWeatherCore::WeatherForecast ptr, int day, WeatherDayListModel *parent = nullptr);
const QString &weatherDescription() const
{
return day().weatherDescription();
......@@ -81,7 +80,7 @@ public:
}
public Q_SLOTS:
void refreshDaysFromForecasts(SharedForecastPtr ptr);
void refreshDaysFromForecasts(KWeatherCore::WeatherForecast ptr);
Q_SIGNALS:
void propertyChanged();
......@@ -89,7 +88,7 @@ Q_SIGNALS:
private:
const KWeatherCore::DailyWeatherForecast &day() const
{
return m_forecast->dailyWeatherForecast().at(m_day);
return m_forecast.dailyWeatherForecast().at(m_day);
}
void determineSunrise();
......@@ -98,5 +97,5 @@ private:
QString m_sunset;
QString m_moonPhase;
int m_day = 0;
SharedForecastPtr m_forecast;
KWeatherCore::WeatherForecast m_forecast;
};
......@@ -39,7 +39,7 @@ QHash<int, QByteArray> WeatherDayListModel::roleNames() const
WeatherDay *WeatherDayListModel::get(int index)
{
WeatherDay *ret = nullptr;
if (!m_forecasts || index < 0 || index >= static_cast<int>(m_forecasts->dailyWeatherForecast().size()))
if (index < 0 || index >= static_cast<int>(m_forecasts.dailyWeatherForecast().size()))
return ret;
ret = m_weatherDays.at(index);
......@@ -47,9 +47,9 @@ WeatherDay *WeatherDayListModel::get(int index)
return ret;
}
void WeatherDayListModel::refreshDaysFromForecasts(SharedForecastPtr forecasts)
void WeatherDayListModel::refreshDaysFromForecasts(KWeatherCore::WeatherForecast forecasts)
{
if (forecasts->dailyWeatherForecast().empty())
if (forecasts.dailyWeatherForecast().empty())
return;
m_forecasts = forecasts;
......@@ -57,20 +57,20 @@ void WeatherDayListModel::refreshDaysFromForecasts(SharedForecastPtr forecasts)
// skip outdated data
int skipped = 0;
auto date = QDate::currentDate();
while (forecasts->dailyWeatherForecast().at(skipped).date() < date)
while (forecasts.dailyWeatherForecast().at(skipped).date() < date)
skipped++;
if (forecasts->dailyWeatherForecast().size() - skipped > m_weatherDays.size()) {
beginInsertRows(QModelIndex(), m_weatherDays.size(), forecasts->dailyWeatherForecast().size() - 1 - skipped);
for (auto i = m_weatherDays.size(); i < forecasts->dailyWeatherForecast().size() - skipped; i++) {
if (forecasts.dailyWeatherForecast().size() - skipped > m_weatherDays.size()) {
beginInsertRows(QModelIndex(), m_weatherDays.size(), forecasts.dailyWeatherForecast().size() - 1 - skipped);
for (auto i = m_weatherDays.size(); i < forecasts.dailyWeatherForecast().size() - skipped; i++) {
auto weatherDay = new WeatherDay(forecasts, i, this);
QQmlEngine::setObjectOwnership(weatherDay, QQmlEngine::CppOwnership); // prevent segfaults from js garbage collecting
m_weatherDays.push_back(weatherDay);
}
endInsertRows();
} else if (forecasts->dailyWeatherForecast().size() - skipped < m_weatherDays.size()) {
beginRemoveRows(QModelIndex(), forecasts->dailyWeatherForecast().size() - skipped, m_weatherDays.size() - 1);
for (auto i = m_weatherDays.size() - forecasts->dailyWeatherForecast().size() + skipped; i > 0; i--) {
} else if (forecasts.dailyWeatherForecast().size() - skipped < m_weatherDays.size()) {
beginRemoveRows(QModelIndex(), forecasts.dailyWeatherForecast().size() - skipped, m_weatherDays.size() - 1);
for (auto i = m_weatherDays.size() - forecasts.dailyWeatherForecast().size() + skipped; i > 0; i--) {
m_weatherDays.back()->deleteLater();
m_weatherDays.pop_back();
}
......@@ -82,5 +82,5 @@ void WeatherDayListModel::refreshDaysFromForecasts(SharedForecastPtr forecasts)
void WeatherDayListModel::updateUi()
{
Q_EMIT dataChanged(createIndex(0, 0), createIndex(m_forecasts->dailyWeatherForecast().size() - 1, 0));
Q_EMIT dataChanged(createIndex(0, 0), createIndex(m_forecasts.dailyWeatherForecast().size() - 1, 0));
}
......@@ -10,7 +10,6 @@
#include <KWeatherCore/WeatherForecast>
#include <QAbstractListModel>
#include <QObject>
using SharedForecastPtr = QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast>;
class WeatherLocation;
class WeatherDay;
class WeatherDayListModel : public QAbstractListModel
......@@ -29,11 +28,11 @@ public:
Q_INVOKABLE void updateUi();
public Q_SLOTS:
void refreshDaysFromForecasts(SharedForecastPtr forecast);
void refreshDaysFromForecasts(KWeatherCore::WeatherForecast forecast);
Q_SIGNALS:
void weatherRefresh(SharedForecastPtr forecast);
void weatherRefresh(KWeatherCore::WeatherForecast forecast);
private:
SharedForecastPtr m_forecasts;
KWeatherCore::WeatherForecast m_forecasts;
std::vector<WeatherDay *> m_weatherDays;
};
......@@ -10,7 +10,6 @@
#include "weatherlocationmodel.h"
#include <KConfigCore/KConfigGroup>
#include <QDirIterator>
#include <QExplicitlySharedDataPointer>
#include <QFile>
#include <QStandardPaths>
#include <QTimeZone>
......
......@@ -16,14 +16,14 @@ WeatherHour::WeatherHour(WeatherHourListModel *parent)
connect(parent, &WeatherHourListModel::weatherRefresh, this, &WeatherHour::refreshHoursFromForecasts);
}
WeatherHour::WeatherHour(SharedForecastPtr ptr, int day, int hour, WeatherHourListModel *parent)
WeatherHour::WeatherHour(KWeatherCore::WeatherForecast ptr, int day, int hour, WeatherHourListModel *parent)
: QObject(parent)
, m_forecast(std::move(ptr))
, m_day(day)
, m_hour(hour)
{
}
void WeatherHour::refreshHoursFromForecasts(SharedForecastPtr ptr)
void WeatherHour::refreshHoursFromForecasts(KWeatherCore::WeatherForecast ptr)
{
m_forecast = std::move(ptr);
Q_EMIT propertyChanged();
......@@ -37,5 +37,5 @@ QString WeatherHour::windSpeed() const
}
const KWeatherCore::HourlyWeatherForecast &WeatherHour::hour() const
{
return m_forecast->dailyWeatherForecast().at(m_day).hourlyWeatherForecast().at(m_hour);
return m_forecast.dailyWeatherForecast().at(m_day).hourlyWeatherForecast().at(m_hour);
}
......@@ -10,7 +10,7 @@
#include "kweathersettings.h"
#include <KWeatherCore/WeatherForecast>
#include <QObject>
using SharedForecastPtr = QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast>;
class WeatherHourListModel;
class WeatherHour : public QObject
{
......@@ -29,7 +29,7 @@ class WeatherHour : public QObject
public:
WeatherHour(WeatherHourListModel *parent = nullptr);
explicit WeatherHour(SharedForecastPtr ptr, int day, int hour, WeatherHourListModel *parent = nullptr);
explicit WeatherHour(KWeatherCore::WeatherForecast ptr, int day, int hour, WeatherHourListModel *parent = nullptr);
QString windDirection() const
{
......@@ -73,14 +73,14 @@ public:
return hour().date();
}
public Q_SLOTS:
void refreshHoursFromForecasts(SharedForecastPtr ptr);
void refreshHoursFromForecasts(KWeatherCore::WeatherForecast ptr);
Q_SIGNALS:
void propertyChanged();
private:
const KWeatherCore::HourlyWeatherForecast &hour() const;
SharedForecastPtr m_forecast;
KWeatherCore::WeatherForecast m_forecast;
int m_day = 0;
int m_hour = 0;
};
......@@ -53,24 +53,24 @@ WeatherHour *WeatherHourListModel::get(int index)
return ret;
}
void WeatherHourListModel::refreshHoursFromForecasts(QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast> forecast)
void WeatherHourListModel::refreshHoursFromForecasts(KWeatherCore::WeatherForecast forecast)
{
Q_EMIT layoutAboutToBeChanged();
// sync days
if (forecast->dailyWeatherForecast().size() > m_hours.size()) {
if (forecast.dailyWeatherForecast().size() > m_hours.size()) {
auto i = m_hours.size();
for (; i < forecast->dailyWeatherForecast().size(); i++) {
for (; i < forecast.dailyWeatherForecast().size(); i++) {
m_hours.push_back({});
const auto &hours = forecast->dailyWeatherForecast().at(i).hourlyWeatherForecast();
const auto &hours = forecast.dailyWeatherForecast().at(i).hourlyWeatherForecast();
m_hours.back().reserve(hours.size());
for (auto j = 0; j < static_cast<int>(hours.size()); j++) {
m_hours.back().push_back(new WeatherHour(forecast, i, j, this));
}
}
} else if (forecast->dailyWeatherForecast().size() < m_hours.size()) {
auto i = m_hours.size() - forecast->dailyWeatherForecast().size();
} else if (forecast.dailyWeatherForecast().size() < m_hours.size()) {
auto i = m_hours.size() - forecast.dailyWeatherForecast().size();
for (; i > 0; i--) {
auto &hours = m_hours.back();
for (auto it = hours.begin(); it != hours.end(); it++)
......@@ -82,7 +82,7 @@ void WeatherHourListModel::refreshHoursFromForecasts(QExplicitlySharedDataPointe
// sync hours
const auto &days = forecast->dailyWeatherForecast();
const auto &days = forecast.dailyWeatherForecast();
int dayIndex = 0;
for (const auto &day : days) {
const auto &newHours = day.hourlyWeatherForecast();
......
......@@ -31,9 +31,9 @@ public:
Q_INVOKABLE void updateUi();
WeatherHour *currentForecast() const;
public Q_SLOTS:
void refreshHoursFromForecasts(QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast> forecast);
void refreshHoursFromForecasts(KWeatherCore::WeatherForecast forecast);
Q_SIGNALS:
void weatherRefresh(SharedForecastPtr ptr);
void weatherRefresh(KWeatherCore::WeatherForecast ptr);
void currentForecastChanged();
private:
......
......@@ -32,7 +32,7 @@ WeatherLocation::WeatherLocation()
this->m_timer->start(60 - QDateTime::currentDateTime().currentMSecsSinceEpoch() % 60);
}
WeatherLocation::WeatherLocation(QString locationId, QString locationName, QString timeZone, float latitude, float longitude, SharedForecastPtr forecast)
WeatherLocation::WeatherLocation(QString locationId, QString locationName, QString timeZone, float latitude, float longitude, KWeatherCore::WeatherForecast forecast)
: m_forecast(forecast)
, m_locationName(std::move(locationName))
, m_locationId(std::move(locationId))
......@@ -50,11 +50,9 @@ WeatherLocation::WeatherLocation(QString locationId, QString locationName, QStri
QQmlEngine::setObjectOwnership(m_weatherDayListModel, QQmlEngine::CppOwnership);
QQmlEngine::setObjectOwnership(m_weatherHourListModel, QQmlEngine::CppOwnership);
if (forecast) {
m_lastUpdated = forecast->createdTime();
determineCurrentForecast();
updateSeries();
}
m_lastUpdated = forecast.createdTime();
determineCurrentForecast();
updateSeries();
}
WeatherLocation *WeatherLocation::load(const QString &groupName)
{
......@@ -98,19 +96,19 @@ void WeatherLocation::deleteConfig()
config.deleteGroup(locationId());
config.sync();
}
void WeatherLocation::updateData(QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast> forecasts)
void WeatherLocation::updateData(KWeatherCore::WeatherForecast forecasts)
{
m_forecast = forecasts;
// first time the timezone is empty
if (m_timeZone.isEmpty()) {
m_timeZone = forecasts->timezone();
m_timeZone = forecasts.timezone();
save();
}
determineCurrentForecast();
updateSeries();
m_lastUpdated = forecasts->createdTime();
m_lastUpdated = forecasts.createdTime();
emit weatherRefresh(m_forecast);
emit stopLoadingIndicator();
......@@ -121,10 +119,10 @@ void WeatherLocation::updateData(QExplicitlySharedDataPointer<KWeatherCore::Weat
void WeatherLocation::determineCurrentForecast()
{
if (m_forecast->dailyWeatherForecast().empty())
if (m_forecast.dailyWeatherForecast().empty())
return;
auto currentWeather = m_forecast->dailyWeatherForecast().begin()->hourlyWeatherForecast().begin();
auto currentWeather = m_forecast.dailyWeatherForecast().begin()->hourlyWeatherForecast().begin();
m_backgroundComponent = QStringLiteral("backgrounds/ClearDay.qml");
bool isDayStyle = false; // make sure that if the background is definitively day, the colours match that
......@@ -216,11 +214,11 @@ void WeatherLocation::initSeries(QtCharts::QAbstractSeries *series)
}
void WeatherLocation::updateSeries()
{
if (m_series && !m_forecast->dailyWeatherForecast().empty()) {
if (m_series && !m_forecast.dailyWeatherForecast().empty()) {
m_vector.clear();
double minTemp = std::numeric_limits<double>::max(), maxTemp = std::numeric_limits<double>::min();
for (const auto &d : m_forecast->dailyWeatherForecast()) {
for (const auto &d : m_forecast.dailyWeatherForecast()) {
const auto dayMinTemp = Kweather::convertTemp(d.minTemp()), dayMaxTemp = Kweather::convertTemp(d.maxTemp());
m_vector.append(QPointF(d.date().startOfDay().toMSecsSinceEpoch(), dayMaxTemp));
......@@ -234,7 +232,7 @@ void WeatherLocation::updateSeries()
m_series->replace(m_vector);
if (m_axisX) {
m_axisX->setRange(m_forecast->dailyWeatherForecast().front().date().startOfDay(), m_forecast->dailyWeatherForecast().back().date().startOfDay());
m_axisX->setRange(m_forecast.dailyWeatherForecast().front().date().startOfDay(), m_forecast.dailyWeatherForecast().back().date().startOfDay());
}
}
}
......
......@@ -19,8 +19,6 @@
#include <QTimer>
#include <utility>
using SharedForecastPtr = QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast>;
class WeatherDayListModel;
namespace QtCharts
{
......@@ -59,7 +57,7 @@ public:
QString timeZone,
float latitude,
float longitude,
SharedForecastPtr forecast = SharedForecastPtr(new KWeatherCore::WeatherForecast));
KWeatherCore::WeatherForecast forecast = {});
void save();
static WeatherLocation *load(const QString &groupName);
WeatherHour *currentWeather() const;
......@@ -165,10 +163,10 @@ public:
int index();
void deleteConfig();
public slots:
void updateData(QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast> forecasts);
void updateData(KWeatherCore::WeatherForecast forecasts);
signals:
void weatherRefresh(QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast> forecasts); // sent when weather data is refreshed
void weatherRefresh(KWeatherCore::WeatherForecast forecasts); // sent when weather data is refreshed
void currentForecastChange();
void propertyChanged(); // avoid warning
void stopLoadingIndicator();
......@@ -185,7 +183,7 @@ private:
void updateAxes();
KWeatherCore::WeatherForecastSource m_source;
SharedForecastPtr m_forecast;
KWeatherCore::WeatherForecast m_forecast;
// chart related fields
QtCharts::QSplineSeries *m_series = nullptr;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment