MauiKit Calendar

remindersmodel.cpp
1// SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#include "kalendar_debug.h"
5#include <QMetaEnum>
6#include <models/remindersmodel.h>
7
8RemindersModel::RemindersModel(QObject *parent, KCalendarCore::Incidence::Ptr incidencePtr)
9 : QAbstractListModel(parent)
10 , m_incidence(incidencePtr)
11{
12 for (int i = 0; i < QMetaEnum::fromType<RemindersModel::Roles>().keyCount(); i++) {
14 const QString key = QLatin1String(roleNames().value(value));
15 m_dataRoles[key] = value;
16 }
17}
18
19KCalendarCore::Incidence::Ptr RemindersModel::incidencePtr()
20{
21 return m_incidence;
22}
23
24void RemindersModel::setIncidencePtr(KCalendarCore::Incidence::Ptr incidence)
25{
26 if (m_incidence == incidence) {
27 return;
28 }
29 m_incidence = incidence;
30 Q_EMIT incidencePtrChanged();
31 Q_EMIT alarmsChanged();
33}
34
35KCalendarCore::Alarm::List RemindersModel::alarms()
36{
37 return m_incidence->alarms();
38}
39
40QVariantMap RemindersModel::dataroles()
41{
42 return m_dataRoles;
43}
44
45QVariant RemindersModel::data(const QModelIndex &idx, int role) const
46{
47 if (!hasIndex(idx.row(), idx.column())) {
48 return {};
49 }
50 auto alarm = m_incidence->alarms()[idx.row()];
51 switch (role) {
52 case TypeRole:
53 return alarm->type();
54 case TimeRole:
55 return alarm->time();
56 case StartOffsetRole:
57 return alarm->startOffset().asSeconds();
58 case EndOffsetRole:
59 return alarm->endOffset().asSeconds();
60 default:
61 qCWarning(KALENDAR_LOG) << "Unknown role for incidence:" << QMetaEnum::fromType<Roles>().valueToKey(role);
62 return {};
63 }
64}
65
66bool RemindersModel::setData(const QModelIndex &idx, const QVariant &value, int role)
67{
68 if (!idx.isValid()) {
69 return false;
70 }
71
72 switch (role) {
73 case TypeRole: {
74 auto type = static_cast<KCalendarCore::Alarm::Type>(value.toInt());
75 m_incidence->alarms()[idx.row()]->setType(type);
76 break;
77 }
78 case TimeRole: {
79 QDateTime time = value.toDateTime();
80 m_incidence->alarms()[idx.row()]->setTime(time);
81 break;
82 }
83 case StartOffsetRole: {
84 // offset can be set in seconds or days, if we want it to be before the incidence,
85 // it has to be set to a negative value.
86 KCalendarCore::Duration offset(value.toInt());
87 m_incidence->alarms()[idx.row()]->setStartOffset(offset);
88 break;
89 }
90 case EndOffsetRole: {
91 KCalendarCore::Duration offset(value.toInt());
92 m_incidence->alarms()[idx.row()]->setEndOffset(offset);
93 break;
94 }
95 default:
96 qCWarning(KALENDAR_LOG) << "Unknown role for incidence:" << QMetaEnum::fromType<Roles>().valueToKey(role);
97 return false;
98 }
99 Q_EMIT dataChanged(idx, idx);
100 return true;
101}
102
103QHash<int, QByteArray> RemindersModel::roleNames() const
104{
105 return {
106 {TypeRole, QByteArrayLiteral("type")},
107 {TimeRole, QByteArrayLiteral("time")},
108 {StartOffsetRole, QByteArrayLiteral("startOffset")},
109 {EndOffsetRole, QByteArrayLiteral("endOffset")},
110 };
111}
112
113int RemindersModel::rowCount(const QModelIndex &) const
114{
115 return m_incidence->alarms().size();
116}
117
118void RemindersModel::addAlarm()
119{
120 KCalendarCore::Alarm::Ptr alarm(new KCalendarCore::Alarm(m_incidence.get()));
121 alarm->setEnabled(true);
122 alarm->setType(KCalendarCore::Alarm::Display);
123 alarm->setText(m_incidence->summary());
124 alarm->setStartOffset(0);
125
126 qCDebug(KALENDAR_LOG) << alarm->parentUid();
127
128 m_incidence->addAlarm(alarm);
129 Q_EMIT alarmsChanged();
131}
132
133void RemindersModel::deleteAlarm(int row)
134{
135 if (!hasIndex(row, 0)) {
136 return;
137 }
138
139 m_incidence->removeAlarm(m_incidence->alarms()[row]);
140 Q_EMIT alarmsChanged();
142}
AKONADI_CALENDAR_EXPORT KCalendarCore::Incidence::Ptr incidence(const Akonadi::Item &item)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
bool hasIndex(int row, int column, const QModelIndex &parent) const const
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
QMetaEnum fromType()
int value(int index) const const
const char * valueToKey(int value) const const
int column() const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
T * get() const const
QDateTime toDateTime() const const
int toInt(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:35:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.