File indexing completed on 2024-06-23 04:42:36

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