File indexing completed on 2024-11-24 04:50:36

0001 // SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include "incidenceoccurrencemodel.h"
0007 #include <QAbstractItemModel>
0008 #include <QDateTime>
0009 #include <QList>
0010 #include <QSharedPointer>
0011 #include <QTimer>
0012 
0013 namespace KCalendarCore
0014 {
0015 class Incidence;
0016 }
0017 
0018 /**
0019  * Each toplevel index represents a day.
0020  * The "incidences" roles provides a list of lists, where each list represents a visual line,
0021  * containing a number of events to display.
0022  */
0023 class HourlyIncidenceModel : public QAbstractListModel
0024 {
0025     Q_OBJECT
0026     Q_PROPERTY(int periodLength READ periodLength WRITE setPeriodLength NOTIFY periodLengthChanged)
0027     Q_PROPERTY(HourlyIncidenceModel::Filters filters READ filters WRITE setFilters NOTIFY filtersChanged)
0028     Q_PROPERTY(IncidenceOccurrenceModel *model READ model WRITE setModel NOTIFY modelChanged)
0029     Q_PROPERTY(bool showTodos READ showTodos WRITE setShowTodos NOTIFY showTodosChanged)
0030     Q_PROPERTY(bool showSubTodos READ showSubTodos WRITE setShowSubTodos NOTIFY showSubTodosChanged)
0031     Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)
0032 
0033 public:
0034     enum Filter {
0035         NoAllDay = 0x1,
0036         NoMultiDay = 0x2,
0037     };
0038     Q_DECLARE_FLAGS(Filters, Filter)
0039     Q_FLAGS(Filters)
0040     Q_ENUM(Filter)
0041 
0042     enum Roles {
0043         IncidencesRole = IncidenceOccurrenceModel::LastRole,
0044         PeriodStartDateTimeRole,
0045     };
0046 
0047     explicit HourlyIncidenceModel(QObject *parent = nullptr);
0048     ~HourlyIncidenceModel() override = default;
0049 
0050     int rowCount(const QModelIndex &parent = {}) const override;
0051     QVariant data(const QModelIndex &index, int role) const override;
0052     QHash<int, QByteArray> roleNames() const override;
0053 
0054     IncidenceOccurrenceModel *model() const;
0055     int periodLength() const;
0056     HourlyIncidenceModel::Filters filters() const;
0057     bool showTodos() const;
0058     bool showSubTodos() const;
0059     bool active() const;
0060     void setActive(const bool active);
0061 
0062 Q_SIGNALS:
0063     void periodLengthChanged();
0064     void filtersChanged();
0065     void modelChanged();
0066     void showTodosChanged();
0067     void showSubTodosChanged();
0068     void activeChanged();
0069 
0070 public Q_SLOTS:
0071     void setModel(IncidenceOccurrenceModel *model);
0072     void setPeriodLength(int periodLength);
0073     void setFilters(HourlyIncidenceModel::Filters filters);
0074     void setShowTodos(const bool showTodos);
0075     void setShowSubTodos(const bool showSubTodos);
0076 
0077 private Q_SLOTS:
0078     void scheduleReset();
0079 
0080 private:
0081     QList<QModelIndex> sortedIncidencesFromSourceModel(const QDateTime &rowStart) const;
0082     QVariantList layoutLines(const QDateTime &rowStart) const;
0083 
0084     QTimer mRefreshTimer;
0085     IncidenceOccurrenceModel *mSourceModel{nullptr};
0086     QList<QVariantList> m_laidOutLines;
0087     int mPeriodLength{15}; // In minutes
0088     HourlyIncidenceModel::Filters m_filters;
0089     bool m_showTodos = true;
0090     bool m_showSubTodos = true;
0091     bool m_active = true;
0092 };
0093 
0094 Q_DECLARE_OPERATORS_FOR_FLAGS(HourlyIncidenceModel::Filters)