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 #include "remindersmodel.h" 0005 #include "merkuro_calendar_debug.h" 0006 #include <QMetaEnum> 0007 0008 RemindersModel::RemindersModel(QObject *parent) 0009 : QAbstractListModel(parent) 0010 { 0011 } 0012 0013 KCalendarCore::Incidence::Ptr RemindersModel::incidence() const 0014 { 0015 return m_incidence; 0016 } 0017 0018 void RemindersModel::setIncidence(KCalendarCore::Incidence::Ptr incidence) 0019 { 0020 if (m_incidence == incidence) { 0021 return; 0022 } 0023 m_incidence = incidence; 0024 Q_EMIT incidenceChanged(); 0025 Q_EMIT alarmsChanged(); 0026 Q_EMIT layoutChanged(); 0027 } 0028 0029 KCalendarCore::Alarm::List RemindersModel::alarms() const 0030 { 0031 if (!m_incidence) { 0032 return {}; 0033 } 0034 return m_incidence->alarms(); 0035 } 0036 0037 QVariant RemindersModel::data(const QModelIndex &idx, int role) const 0038 { 0039 Q_ASSERT(m_incidence); 0040 Q_ASSERT(checkIndex(idx, QAbstractItemModel::CheckIndexOption::IndexIsValid)); 0041 0042 auto alarm = m_incidence->alarms()[idx.row()]; 0043 switch (role) { 0044 case TypeRole: 0045 return alarm->type(); 0046 case TimeRole: 0047 return alarm->time(); 0048 case SummaryRole: 0049 return alarm->text(); 0050 case StartOffsetRole: 0051 return alarm->startOffset().asSeconds(); 0052 case EndOffsetRole: 0053 return alarm->endOffset().asSeconds(); 0054 default: 0055 qCWarning(MERKURO_CALENDAR_LOG) << "Unknown role for incidence:" << QMetaEnum::fromType<Roles>().valueToKey(role); 0056 return {}; 0057 } 0058 } 0059 0060 bool RemindersModel::setData(const QModelIndex &idx, const QVariant &value, int role) 0061 { 0062 Q_ASSERT(m_incidence); 0063 Q_ASSERT(checkIndex(idx, QAbstractItemModel::CheckIndexOption::IndexIsValid)); 0064 0065 if (!idx.isValid()) { 0066 return false; 0067 } 0068 0069 switch (role) { 0070 case TypeRole: { 0071 auto type = static_cast<KCalendarCore::Alarm::Type>(value.toInt()); 0072 m_incidence->alarms()[idx.row()]->setType(type); 0073 break; 0074 } 0075 case TimeRole: { 0076 QDateTime time = value.toDateTime(); 0077 m_incidence->alarms()[idx.row()]->setTime(time); 0078 break; 0079 } 0080 case StartOffsetRole: { 0081 // offset can be set in seconds or days, if we want it to be before the incidence, 0082 // it has to be set to a negative value. 0083 KCalendarCore::Duration offset(value.toInt()); 0084 m_incidence->alarms()[idx.row()]->setStartOffset(offset); 0085 break; 0086 } 0087 case EndOffsetRole: { 0088 KCalendarCore::Duration offset(value.toInt()); 0089 m_incidence->alarms()[idx.row()]->setEndOffset(offset); 0090 break; 0091 } 0092 default: 0093 qCWarning(MERKURO_CALENDAR_LOG) << "Unknown role for incidence:" << QMetaEnum::fromType<Roles>().valueToKey(role); 0094 return false; 0095 } 0096 Q_EMIT dataChanged(idx, idx); 0097 return true; 0098 } 0099 0100 QHash<int, QByteArray> RemindersModel::roleNames() const 0101 { 0102 return { 0103 {TypeRole, QByteArrayLiteral("type")}, 0104 {TimeRole, QByteArrayLiteral("time")}, 0105 {StartOffsetRole, QByteArrayLiteral("startOffset")}, 0106 {EndOffsetRole, QByteArrayLiteral("endOffset")}, 0107 }; 0108 } 0109 0110 int RemindersModel::rowCount(const QModelIndex &) const 0111 { 0112 if (!m_incidence) { 0113 return 0; 0114 } 0115 return m_incidence->alarms().size(); 0116 } 0117 0118 void RemindersModel::addAlarm() 0119 { 0120 Q_ASSERT(m_incidence); 0121 0122 KCalendarCore::Alarm::Ptr alarm(new KCalendarCore::Alarm(m_incidence.get())); 0123 alarm->setEnabled(true); 0124 alarm->setType(KCalendarCore::Alarm::Display); 0125 alarm->setText(m_incidence->summary()); 0126 alarm->setStartOffset(0); 0127 0128 qCDebug(MERKURO_CALENDAR_LOG) << alarm->parentUid(); 0129 0130 m_incidence->addAlarm(alarm); 0131 Q_EMIT alarmsChanged(); 0132 Q_EMIT layoutChanged(); 0133 } 0134 0135 void RemindersModel::deleteAlarm(const int row) 0136 { 0137 Q_ASSERT(m_incidence); 0138 0139 if (!hasIndex(row, 0)) { 0140 return; 0141 } 0142 0143 m_incidence->removeAlarm(m_incidence->alarms()[row]); 0144 Q_EMIT alarmsChanged(); 0145 Q_EMIT layoutChanged(); 0146 } 0147 0148 #include "moc_remindersmodel.cpp"