File indexing completed on 2023-05-30 11:24:54
0001 /* 0002 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #ifndef KOPENINGHOURS_INTERVALMODEL_H 0008 #define KOPENINGHOURS_INTERVALMODEL_H 0009 0010 #include "kopeninghours_export.h" 0011 0012 #include <KOpeningHours/OpeningHours> 0013 0014 #include <QAbstractListModel> 0015 #include <QDate> 0016 0017 #include <memory> 0018 0019 namespace KOpeningHours { 0020 0021 class IntervalModelPrivate; 0022 0023 /** Model for showing opening intervals per day. */ 0024 class KOPENINGHOURS_EXPORT IntervalModel : public QAbstractListModel 0025 { 0026 Q_OBJECT 0027 /** The opening hours expression shown in this model. */ 0028 Q_PROPERTY(KOpeningHours::OpeningHours openingHours READ openingHours WRITE setOpeningHours NOTIFY openingHoursChanged) 0029 /** Begin of the date range to show in this model. */ 0030 Q_PROPERTY(QDate beginDate READ beginDate WRITE setBeginDate NOTIFY beginDateChanged) 0031 /** End of the date range to show in this model. */ 0032 Q_PROPERTY(QDate endDate READ endDate WRITE setEndDate NOTIFY endDateChanged) 0033 0034 /** Description of the current status as a translated human-readable string. 0035 * See Display::currentState. 0036 */ 0037 Q_PROPERTY(QString currentState READ currentState NOTIFY openingHoursChanged) 0038 0039 public: 0040 explicit IntervalModel(QObject *parent = nullptr); 0041 ~IntervalModel() override; 0042 0043 OpeningHours openingHours() const; 0044 void setOpeningHours(const OpeningHours &oh); 0045 0046 QDate beginDate() const; 0047 void setBeginDate(QDate beginDate); 0048 QDate endDate() const; 0049 void setEndDate(QDate endDate); 0050 0051 enum Roles { 0052 IntervalsRole = Qt::UserRole, ///< All intervals in the current row. 0053 DateRole, ///< The date represented by the current row. 0054 DayBeginTimeRole, ///< Same as @c DateRole, but as a date/time object. 0055 ShortDayNameRole, ///< Localized short day name for the current row. 0056 IsTodayRole, ///< @c true if the row represents the current day. 0057 }; 0058 0059 int rowCount(const QModelIndex& parent = {}) const override; 0060 QVariant data(const QModelIndex &index, int role) const override; 0061 QHash<int, QByteArray> roleNames() const override; 0062 0063 /** Returns the day the week containing @p dt begins, based on the current locale. 0064 * This is useful to align the content of this model to a week. 0065 */ 0066 Q_INVOKABLE QDate beginOfWeek(const QDateTime &dt) const; 0067 0068 /** Localized formatting for time column headers. */ 0069 Q_INVOKABLE QString formatTimeColumnHeader(int hour, int minute) const; 0070 0071 Q_SIGNALS: 0072 void openingHoursChanged(); 0073 void beginDateChanged(); 0074 void endDateChanged(); 0075 0076 private: 0077 QString currentState() const; 0078 std::unique_ptr<IntervalModelPrivate> d; 0079 }; 0080 0081 } 0082 0083 #endif // KOPENINGHOURS_INTERVALMODEL_H