File indexing completed on 2025-02-02 05:02:39

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "weatherforecastmodel.h"
0008 
0009 #include "weatherforecastmanager.h"
0010 
0011 #include <QDateTime>
0012 #include <QDebug>
0013 
0014 WeatherForecastModel::WeatherForecastModel(QObject* parent)
0015     : QAbstractListModel(parent)
0016 {
0017 }
0018 
0019 WeatherForecastModel::~WeatherForecastModel() = default;
0020 
0021 QObject* WeatherForecastModel::weatherForecastManager() const
0022 {
0023     return m_mgr;
0024 }
0025 
0026 void WeatherForecastModel::setWeatherForecastManager(QObject* mgr)
0027 {
0028     beginResetModel();
0029     m_mgr = qobject_cast<WeatherForecastManager*>(mgr);
0030     endResetModel();
0031 }
0032 
0033 QVariant WeatherForecastModel::weatherForecast() const
0034 {
0035     return QVariant::fromValue(m_fc);
0036 }
0037 
0038 void WeatherForecastModel::setWeatherForecast(const QVariant& fc)
0039 {
0040     beginResetModel();
0041     m_fc = fc.value<WeatherForecast>();
0042     endResetModel();
0043 }
0044 
0045 int WeatherForecastModel::rowCount(const QModelIndex& parent) const
0046 {
0047     if (parent.isValid() || !m_mgr)
0048         return 0;
0049     return m_fc.range();
0050 }
0051 
0052 QVariant WeatherForecastModel::data(const QModelIndex& index, int role) const
0053 {
0054     if (!index.isValid() || !m_mgr || !m_fc.isValid())
0055         return {};
0056 
0057     switch (role) {
0058         case WeatherForecastRole:
0059         {
0060             const auto fc = m_mgr->forecast(m_fc.tile().latitude(), m_fc.tile().longitude(), m_fc.dateTime().addSecs(index.row() * 3600));
0061             return QVariant::fromValue(fc);
0062         }
0063         case LocalizedTimeRole:
0064             return QLocale().toString(m_fc.dateTime().addSecs(index.row() * 3600).toLocalTime().time(), QLocale::ShortFormat);
0065     }
0066 
0067     return {};
0068 }
0069 
0070 QHash<int, QByteArray> WeatherForecastModel::roleNames() const
0071 {
0072     auto names = QAbstractListModel::roleNames();
0073     names.insert(WeatherForecastRole, "weatherForecast");
0074     names.insert(LocalizedTimeRole, "localizedTime");
0075     return names;
0076 }
0077 
0078 #include "moc_weatherforecastmodel.cpp"