KCalendarCore

calendarlistmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "calendarlistmodel.h"
7
8#include <KCalendarCore/CalendarPlugin>
9#include <KCalendarCore/CalendarPluginLoader>
10
11using namespace KCalendarCore;
12
13namespace KCalendarCore
14{
15class CalendarListModelPrivate
16{
17public:
18 QList<Calendar::Ptr> calendars;
19};
20}
21
22CalendarListModel::CalendarListModel(QObject *parent)
23 : QAbstractListModel(parent)
24{
25 if (CalendarPluginLoader::hasPlugin()) {
26 d = std::make_unique<CalendarListModelPrivate>();
27 d->calendars = CalendarPluginLoader::plugin()->calendars();
28 connect(CalendarPluginLoader::plugin(), &CalendarPlugin::calendarsChanged, this, [this]() {
29 beginResetModel();
30 d->calendars = CalendarPluginLoader::plugin()->calendars();
31 endResetModel();
32 });
33 }
34}
35
36CalendarListModel::~CalendarListModel() = default;
37
38int CalendarListModel::rowCount(const QModelIndex &parent) const
39{
40 if (parent.isValid() || !d) {
41 return 0;
42 }
43
44 return d->calendars.size();
45}
46
47QVariant CalendarListModel::data(const QModelIndex &index, int role) const
48{
49 if (!checkIndex(index) || !d) {
50 return {};
51 }
52
53 const auto &cal = d->calendars.at(index.row());
54 switch (role) {
55 case NameRole:
56 return cal->name();
57 case IconRole:
58 return cal->icon();
59 case CalendarRole:
60 return QVariant::fromValue(cal.get());
61 case AccessModeRole:
62 return cal->accessMode();
63 case IdRole:
64 return cal->id();
65 }
66
67 return {};
68}
69
70QHash<int, QByteArray> CalendarListModel::roleNames() const
71{
73 n.insert(NameRole, "name");
74 n.insert(IconRole, "icon");
75 n.insert(CalendarRole, "calendar");
76 n.insert(AccessModeRole, "accessMode");
77 n.insert(IdRole, "id");
78 return n;
79}
80
81#include "moc_calendarlistmodel.cpp"
@ IconRole
the calendar icon, when available
@ CalendarRole
the KCalendarCore::Calendar calendar
@ IdRole
the internal calendar id
@ AccessModeRole
the access mode of the calendar (see KCalendarCore::AccessMode)
@ NameRole
display name of the calendar
void calendarsChanged()
Emitted when the set of calendars changed.
Namespace for all KCalendarCore types.
Definition alarm.h:37
bool checkIndex(const QModelIndex &index, CheckIndexOptions options) const const
virtual QHash< int, QByteArray > roleNames() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
int row() const const
QObject * parent() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:56:45 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.