File indexing completed on 2025-02-02 05:02:35
0001 /* 0002 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "statisticstimerangemodel.h" 0008 #include "reservationmanager.h" 0009 0010 #include <KItinerary/SortUtil> 0011 0012 #include <KLocalizedString> 0013 0014 #include <QDate> 0015 #include <QDebug> 0016 #include <QLocale> 0017 0018 using namespace KItinerary; 0019 0020 StatisticsTimeRangeModel::StatisticsTimeRangeModel(QObject *parent) 0021 : QAbstractListModel(parent) 0022 { 0023 } 0024 0025 StatisticsTimeRangeModel::~StatisticsTimeRangeModel() = default; 0026 0027 ReservationManager* StatisticsTimeRangeModel::reservationManager() const 0028 { 0029 return m_resMgr; 0030 } 0031 0032 void StatisticsTimeRangeModel::setReservationManager(ReservationManager *resMgr) 0033 { 0034 if (m_resMgr == resMgr) { 0035 return; 0036 } 0037 m_resMgr = resMgr; 0038 Q_EMIT setupChanged(); 0039 0040 beginResetModel(); 0041 int y = 0; 0042 0043 const auto &batches = m_resMgr->batches(); 0044 for (const auto &batchId : batches) { 0045 const auto res = m_resMgr->reservation(batchId); 0046 const auto dt = SortUtil::startDateTime(res); 0047 if (dt.date().year() != y) { 0048 m_years.push_back(dt.date().year()); 0049 y = dt.date().year(); 0050 } 0051 } 0052 std::reverse(m_years.begin(), m_years.end()); 0053 endResetModel(); 0054 } 0055 0056 int StatisticsTimeRangeModel::rowCount(const QModelIndex &parent) const 0057 { 0058 if (parent.isValid()) { 0059 return 0; 0060 } 0061 return 1 + m_years.size(); 0062 } 0063 0064 QVariant StatisticsTimeRangeModel::data(const QModelIndex &index, int role) const 0065 { 0066 switch (role) { 0067 case Qt::DisplayRole: 0068 if (index.row() == 0) { 0069 return i18n("Total"); 0070 } 0071 return QLocale().toString(QDate(m_years[index.row() - 1], 1, 1), QStringLiteral("yyyy")); 0072 case BeginRole: 0073 if (index.row() == 0 || index.row() == (int)m_years.size()) { // first range is open ended here, to skip trend computation 0074 return QDate(); 0075 } 0076 return QDate(m_years[index.row() - 1], 1, 1); 0077 case EndRole: 0078 if (index.row() == 0) { 0079 return QDate(); 0080 } 0081 return QDate(m_years[index.row() - 1], 12, 31); 0082 } 0083 0084 return {}; 0085 } 0086 0087 QHash<int, QByteArray> StatisticsTimeRangeModel::roleNames() const 0088 { 0089 auto r = QAbstractListModel::roleNames(); 0090 r.insert(BeginRole, "begin"); 0091 r.insert(EndRole, "end"); 0092 return r; 0093 } 0094 0095 #include "moc_statisticstimerangemodel.cpp"