Commit 5a2c23fc authored by Volker Krause's avatar Volker Krause

Show weather forecast for the entire time range

parent b495de76
......@@ -47,7 +47,7 @@ private slots:
WeatherForecastManager mgr;
mgr.setAllowNetworkAccess(true);
const auto now = QDateTime::currentDateTimeUtc().addSecs(1800);
auto fc = mgr.forecast(46.1, 7.78, now).value<WeatherForecast>();
auto fc = mgr.forecast(46.1, 7.78, now);
QVERIFY(!fc.isValid());
QSignalSpy updateSpy(&mgr, &WeatherForecastManager::forecastUpdated);
......@@ -56,7 +56,7 @@ private slots:
mgr.monitorLocation(46.1, 7.78);
QVERIFY(updateSpy.wait());
fc = mgr.forecast(46.1, 7.78, now).value<WeatherForecast>();
fc = mgr.forecast(46.1, 7.78, now);
qDebug() << fc.dateTime() << fc.minimumTemperature() << fc.maximumTemperature() << fc.symbolType() << fc.symbolIconName();
QVERIFY(fc.isValid());
QVERIFY(fc.dateTime().isValid());
......
......@@ -20,6 +20,7 @@
#include "pkpassmanager.h"
#include "reservationmanager.h"
#include <weatherforecast.h>
#include <weatherforecastmanager.h>
#include <KItinerary/BusTrip>
......@@ -384,11 +385,11 @@ void TimelineModel::insertWeatherElements()
const auto geo = geoCoordinate(it);
if (geo.isValid()) {
m_weatherMgr->monitorLocation(geo.latitude(), geo.longitude());
const auto fc = m_weatherMgr->forecast(geo.latitude(), geo.longitude(), QDateTime(date, QTime(12, 0)));
const auto fc = m_weatherMgr->forecast(geo.latitude(), geo.longitude(), QDateTime(date, QTime(0, 0)), QDateTime(date, QTime(23, 59)));
if (fc.isValid()) {
const auto row = std::distance(m_elements.begin(), it);
beginInsertRows({}, row, row);
it = m_elements.insert(it, Element{{}, fc, QDateTime(date, QTime()), WeatherForecast, SelfContained});
it = m_elements.insert(it, Element{{}, QVariant::fromValue(fc), QDateTime(date, QTime()), WeatherForecast, SelfContained});
endInsertRows();
date = date.addDays(1);
continue;
......@@ -404,7 +405,7 @@ void TimelineModel::updateWeatherElements()
for (auto it = m_elements.begin(); it != m_elements.end(); ++it) {
if ((*it).elementType == WeatherForecast) {
// TODO see above
(*it).content = m_weatherMgr->forecast(52, 13.5, QDateTime((*it).dt.date(), QTime(12, 0)));
(*it).content = QVariant::fromValue(m_weatherMgr->forecast(52, 13.5, QDateTime((*it).dt.date(), QTime(0, 0)), QDateTime((*it).dt.date(), QTime(23, 59))));
const auto idx = index(std::distance(m_elements.begin(), it), 0);
emit dataChanged(idx, idx);
}
......
......@@ -32,6 +32,11 @@
#include <cmath>
static void alignToHour(QDateTime &dt)
{
dt.setTime(QTime(dt.time().hour(), 0, 0, 0));
}
/*
* ATTENTION!
* Before touching anything in here, especially regarding the network operations
......@@ -65,24 +70,42 @@ void WeatherForecastManager::monitorLocation(float latitude, float longitude)
fetchTile(t);
}
QVariant WeatherForecastManager::forecast(float latitude, float longitude, const QDateTime& dt) const
WeatherForecast WeatherForecastManager::forecast(float latitude, float longitude, const QDateTime &dt) const
{
return forecast(latitude, longitude, dt, dt.addSecs(3600));
}
WeatherForecast WeatherForecastManager::forecast(float latitude, float longitude, const QDateTime &begin, const QDateTime &end) const
{
auto beginDt = std::max(begin, QDateTime::currentDateTimeUtc());
alignToHour(beginDt);
auto endDt = std::max(end, QDateTime::currentDateTimeUtc());
alignToHour(endDt);
if (!beginDt.isValid() || !endDt.isValid() || beginDt > endDt) {
return {};
}
WeatherTile tile{latitude, longitude};
if (!loadForecastData(tile)) {
return {};
}
const auto &forecasts = m_forecastData[tile];
auto it = std::lower_bound(forecasts.begin(), forecasts.end(), dt, [](const WeatherForecast &lhs, const QDateTime &rhs) {
const auto beginIt = std::lower_bound(forecasts.begin(), forecasts.end(), beginDt, [](const WeatherForecast &lhs, const QDateTime &rhs) {
return lhs.dateTime() < rhs;
});
if (it != forecasts.begin()) {
--it;
if (beginIt == forecasts.end()) {
return {};
}
if (it != forecasts.end()) {
return QVariant::fromValue(*it);
const auto endIt = std::lower_bound(forecasts.begin(), forecasts.end(), endDt, [](const WeatherForecast &lhs, const QDateTime &rhs) {
return lhs.dateTime() < rhs;
});
WeatherForecast fc(*beginIt);
for (auto it = beginIt; it != endIt; ++it) {
fc.merge(*it);
}
return {};
return fc;
}
void WeatherForecastManager::fetchTile(WeatherTile tile)
......@@ -263,11 +286,6 @@ void WeatherForecastManager::mergeForecasts(std::vector<WeatherForecast>& foreca
forecasts.erase(storeIt, forecasts.end());
}
static void alignToHour(QDateTime &dt)
{
dt.setTime(QTime(dt.time().hour(), 0, 0, 0));
}
std::vector<WeatherForecast> WeatherForecastManager::parseForecast(QXmlStreamReader &reader) const
{
std::vector<WeatherForecast> result;
......
......@@ -47,7 +47,9 @@ public:
void monitorLocation(float latitude, float longitude);
/** Get the forecast for the given time and location. */
Q_INVOKABLE QVariant forecast(float latitude, float longitude, const QDateTime &dt) const;
WeatherForecast forecast(float latitude, float longitude, const QDateTime &dt) const;
/** Get the forecast for the give time range and location. */
WeatherForecast forecast(float latitude, float longitude, const QDateTime &begin, const QDateTime &end) const;
signals:
/** Updated when new forecast data has been retrieved. */
......
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