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

0001 // SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #pragma once
0005 
0006 #include <KCalendarCore/Calendar>
0007 #include <QAbstractItemModel>
0008 
0009 /**
0010  * This class provides a QAbstractItemModel for an incidences' reminders/alarms.
0011  * This can be useful for letting users add, modify, or delete incidences on new or pre-existing incidences.
0012  * It treats the incidence's list of alarms as the single source of truth (and it should be kept this way!)
0013  *
0014  * The data for the model comes from m_incidence, which is set in the constructor. This is a pointer to the
0015  * incidence this model is getting the alarm info from. All alarm pointers are then added to m_alarms, which
0016  * is a list. Elements in this model are therefore accessed through row numbers, as the list is a one-
0017  * dimensional data structure.
0018  */
0019 
0020 class RemindersModel : public QAbstractListModel
0021 {
0022     Q_OBJECT
0023     Q_PROPERTY(KCalendarCore::Incidence::Ptr incidence READ incidence WRITE setIncidence NOTIFY incidenceChanged)
0024     Q_PROPERTY(KCalendarCore::Alarm::List alarms READ alarms NOTIFY alarmsChanged)
0025 
0026 public:
0027     enum Roles {
0028         TypeRole = Qt::UserRole + 1,
0029         SummaryRole,
0030         TimeRole,
0031         StartOffsetRole,
0032         EndOffsetRole,
0033     };
0034     Q_ENUM(Roles)
0035 
0036     explicit RemindersModel(QObject *parent = nullptr);
0037     ~RemindersModel() override = default;
0038 
0039     KCalendarCore::Incidence::Ptr incidence() const;
0040     void setIncidence(KCalendarCore::Incidence::Ptr incidence);
0041     KCalendarCore::Alarm::List alarms() const;
0042 
0043     QVariant data(const QModelIndex &idx, int role) const override;
0044     bool setData(const QModelIndex &idx, const QVariant &value, int role) override;
0045     QHash<int, QByteArray> roleNames() const override;
0046     int rowCount(const QModelIndex &parent = {}) const override;
0047 
0048     Q_INVOKABLE void addAlarm();
0049     Q_INVOKABLE void deleteAlarm(const int row);
0050 
0051 Q_SIGNALS:
0052     void incidenceChanged();
0053     void alarmsChanged();
0054 
0055 private:
0056     KCalendarCore::Incidence::Ptr m_incidence;
0057     QVariantMap m_dataRoles;
0058 };