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

0001 // Copyright (c) 2018 Michael Bohlender <michael.bohlender@kdemail.net>
0002 // Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
0003 // Copyright (c) 2018 RĂ©mi Nicole <minijackson@riseup.net>
0004 // Copyright (c) 2021 Carl Schwan <carlschwan@kde.org>
0005 // SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
0006 // SPDX-License-Identifier: LGPL-2.0-or-later
0007 
0008 #pragma once
0009 
0010 #include "incidenceoccurrencemodel.h"
0011 #include <QAbstractItemModel>
0012 #include <QList>
0013 #include <QQmlParserStatus>
0014 #include <QSharedPointer>
0015 #include <QTimer>
0016 
0017 namespace KCalendarCore
0018 {
0019 class Incidence;
0020 }
0021 
0022 /**
0023  * Each toplevel index represents a week.
0024  * The "incidences" roles provides a list of lists, where each list represents a visual line,
0025  * containing a number of events to display.
0026  */
0027 class MultiDayIncidenceModel : public QAbstractListModel, public QQmlParserStatus
0028 {
0029     Q_OBJECT
0030     Q_INTERFACES(QQmlParserStatus)
0031     Q_PROPERTY(int periodLength READ periodLength WRITE setPeriodLength NOTIFY periodLengthChanged)
0032     Q_PROPERTY(MultiDayIncidenceModel::Filters filters READ filters WRITE setFilters NOTIFY filtersChanged)
0033     Q_PROPERTY(int incidenceCount READ incidenceCount NOTIFY incidenceCountChanged)
0034     Q_PROPERTY(IncidenceOccurrenceModel *model READ model WRITE setModel NOTIFY modelChanged)
0035     Q_PROPERTY(bool showTodos READ showTodos WRITE setShowTodos NOTIFY showTodosChanged)
0036     Q_PROPERTY(bool showSubTodos READ showSubTodos WRITE setShowSubTodos NOTIFY showSubTodosChanged)
0037     Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)
0038 
0039 public:
0040     enum Filter {
0041         AllDayOnly = 0x1,
0042         NoStartDateOnly = 0x2,
0043         MultiDayOnly = 0x3,
0044     };
0045     Q_DECLARE_FLAGS(Filters, Filter)
0046     Q_FLAGS(Filters)
0047     Q_ENUM(Filter)
0048 
0049     enum Roles {
0050         IncidencesRole = IncidenceOccurrenceModel::LastRole,
0051         PeriodStartDateRole,
0052     };
0053 
0054     explicit MultiDayIncidenceModel(QObject *parent = nullptr);
0055     ~MultiDayIncidenceModel() override = default;
0056 
0057     int rowCount(const QModelIndex &parent = {}) const override;
0058     QVariant data(const QModelIndex &index, int role) const override;
0059     QHash<int, QByteArray> roleNames() const override;
0060 
0061     IncidenceOccurrenceModel *model() const;
0062     int periodLength() const;
0063     MultiDayIncidenceModel::Filters filters() const;
0064     bool showTodos() const;
0065     bool showSubTodos() const;
0066     int incidenceCount() const;
0067     bool incidencePassesFilter(const QModelIndex &idx) const;
0068     bool active() const;
0069     void setActive(const bool active);
0070 
0071     void classBegin() override;
0072     void componentComplete() override;
0073 
0074 Q_SIGNALS:
0075     void periodLengthChanged();
0076     void filtersChanged();
0077     void incidenceCountChanged();
0078     void modelChanged();
0079     void showTodosChanged();
0080     void showSubTodosChanged();
0081     void activeChanged();
0082 
0083 public Q_SLOTS:
0084     void setModel(IncidenceOccurrenceModel *model);
0085     void setPeriodLength(int periodLength);
0086     void setFilters(MultiDayIncidenceModel::Filters filters);
0087     void setShowTodos(const bool showTodos);
0088     void setShowSubTodos(const bool showSubTodos);
0089 
0090 private Q_SLOTS:
0091     void slotSourceDataChanged(const QModelIndex &upperLeft, const QModelIndex &bottomRight);
0092 
0093 private:
0094     QList<QModelIndex> sortedIncidencesFromSourceModel(const QDate &rowStart) const;
0095     QVariantList layoutLines(const QDate &rowStart) const;
0096     void scheduleReset();
0097 
0098     QSet<int> m_linesToUpdate;
0099     QTimer m_refreshTimer;
0100     IncidenceOccurrenceModel *mSourceModel{nullptr};
0101     int mPeriodLength = 7;
0102     MultiDayIncidenceModel::Filters m_filters;
0103     bool m_showTodos = true;
0104     bool m_showSubTodos = true;
0105     bool m_active = true;
0106     bool m_initialized = false;
0107 };
0108 
0109 Q_DECLARE_OPERATORS_FOR_FLAGS(MultiDayIncidenceModel::Filters)