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

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef TIMELINEMODEL_H
0008 #define TIMELINEMODEL_H
0009 
0010 #include "timelineelement.h"
0011 #include "transfer.h"
0012 
0013 #include <QAbstractListModel>
0014 #include <QDateTime>
0015 #include <QTimer>
0016 
0017 class ReservationManager;
0018 class WeatherForecastManager;
0019 class TripGroupManager;
0020 class TransferManager;
0021 
0022 class TimelineModel : public QAbstractListModel
0023 {
0024     Q_OBJECT
0025     Q_PROPERTY(int todayRow READ todayRow NOTIFY todayRowChanged)
0026     Q_PROPERTY(QString currentBatchId READ currentBatchId NOTIFY currentBatchChanged)
0027 
0028 public:
0029     enum Role {
0030         SectionHeaderRole = Qt::UserRole + 1,
0031         BatchIdRole,
0032         ElementTypeRole,
0033         TodayEmptyRole,
0034         IsTodayRole,
0035         ElementRangeRole,
0036         LocationInformationRole,
0037         WeatherForecastRole,
0038         ReservationsRole, // for unit testing
0039         TripGroupIdRole,
0040         TripGroupRole,
0041         TransferRole,
0042         StartDateTimeRole,
0043         EndDateTimeRole,
0044         IsTimeboxedRole,
0045         IsCanceledRole,
0046     };
0047     Q_ENUM(Role)
0048 
0049     explicit TimelineModel(QObject *parent = nullptr);
0050     ~TimelineModel() override;
0051 
0052     void setReservationManager(ReservationManager *mgr);
0053     void setWeatherForecastManager(WeatherForecastManager *mgr);
0054     TripGroupManager* tripGroupManager() const;
0055     void setTripGroupManager(TripGroupManager *mgr);
0056     void setHomeCountryIsoCode(const QString &isoCode);
0057     void setTransferManager(TransferManager *mgr);
0058 
0059     QVariant data(const QModelIndex& index, int role) const override;
0060     int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0061     QHash<int, QByteArray> roleNames() const override;
0062 
0063     int todayRow() const;
0064 
0065     /** The most "current" batch to show with the "ticket check" action. */
0066     QString currentBatchId() const;
0067 
0068     /** The location we are in at the given date/time. */
0069     Q_INVOKABLE QVariant locationAtTime(const QDateTime &dt) const;
0070 
0071     // for unit testing
0072     void setCurrentDateTime(const QDateTime &dt);
0073     QDateTime now() const;
0074     QDate today() const;
0075 
0076 Q_SIGNALS:
0077     void todayRowChanged();
0078     void currentBatchChanged();
0079 
0080 private:
0081     void batchAdded(const QString &resId);
0082     void insertElement(TimelineElement &&elem);
0083     std::vector<TimelineElement>::iterator insertOrUpdate(std::vector<TimelineElement>::iterator it, TimelineElement &&elem);
0084     void batchChanged(const QString &resId);
0085     void updateElement(const QString &resId, const QVariant &res, TimelineElement::RangeType rangeType);
0086     void batchRenamed(const QString &oldBatchId, const QString &newBatchId);
0087     void batchRemoved(const QString &resId);
0088 
0089     void tripGroupAdded(const QString &groupId);
0090     void tripGroupChanged(const QString &groupId);
0091     void tripGroupRemoved(const QString &groupId);
0092 
0093     void transferChanged(const Transfer &transfer);
0094     void transferRemoved(const QString &resId, Transfer::Alignment alignment);
0095 
0096     void dayChanged();
0097     void updateTodayMarker();
0098     void updateInformationElements();
0099     void updateWeatherElements();
0100     void updateTransfersForBatch(const QString &batchId);
0101 
0102     void scheduleCurrentBatchTimer();
0103     bool isDateEmpty(const QDate &date) const;
0104 
0105     friend class TimelineElement;
0106     ReservationManager *m_resMgr = nullptr;
0107     WeatherForecastManager *m_weatherMgr = nullptr;
0108     TripGroupManager *m_tripGroupManager = nullptr;
0109     TransferManager *m_transferManager = nullptr;
0110     std::vector<TimelineElement> m_elements;
0111     QString m_homeCountry;
0112     QDateTime m_unitTestTime;
0113     QTimer m_dayUpdateTimer;
0114     QTimer m_currentBatchTimer;
0115     bool m_todayEmpty = true;
0116 };
0117 
0118 #endif // TIMELINEMODEL_H