MauiKit Calendar

incidenceoccurrencemodel.h
1// Copyright (c) 2018 Michael Bohlender <michael.bohlender@kdemail.net>
2// Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
3// Copyright (c) 2018 RĂ©mi Nicole <minijackson@riseup.net>
4// Copyright (c) 2021 Carl Schwan <carlschwan@kde.org>
5// Copyright (c) 2021 Claudio Cambra <claudio.cambra@gmail.com>
6// SPDX-License-Identifier: LGPL-2.0-or-later
7
8#pragma once
9
10#include <QObject>
11#include <QQmlEngine>
12#include <Akonadi/ETMCalendar>
13
14#include <KConfigWatcher>
15#include <KFormat>
16#include <QAbstractItemModel>
17#include <QColor>
18#include <QDateTime>
19#include <QList>
20#include <QSharedPointer>
21#include <QTimer>
22
23class Filter;
24namespace KCalendarCore
25{
26class Incidence;
27}
28namespace Akonadi
29{
30class ETMCalendar;
31}
32
33using namespace KCalendarCore;
34
35/**
36 * Loads all event occurrences within the given period and matching the given filter.
37 *
38 * Recurrences are expanded
39 */
41{
43 QML_ELEMENT
44 Q_PROPERTY(QDate start READ start WRITE setStart NOTIFY startChanged)
45 Q_PROPERTY(int length READ length WRITE setLength NOTIFY lengthChanged)
46 Q_PROPERTY(Filter *filter READ filter WRITE setFilter NOTIFY filterChanged)
47 Q_PROPERTY(Akonadi::ETMCalendar::Ptr calendar READ calendar WRITE setCalendar NOTIFY calendarChanged)
48 Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
49 Q_PROPERTY(int resetThrottleInterval READ resetThrottleInterval WRITE setResetThrottleInterval NOTIFY resetThrottleIntervalChanged)
50
51public:
52 enum Roles {
53 Summary = Qt::UserRole + 1,
54 Description,
55 Location,
56 StartTime,
57 EndTime,
59 DurationString,
60 Recurs,
61 HasReminders,
62 Priority,
63 Color,
64 CollectionId,
65 AllDay,
66 TodoCompleted,
67 IsOverdue,
68 IsReadOnly,
69 IncidenceId,
70 IncidenceType,
71 IncidenceTypeStr,
72 IncidenceTypeIcon,
73 IncidencePtr,
74 IncidenceOccurrence,
75 LastRole
76 };
77 Q_ENUM(Roles)
78 explicit IncidenceOccurrenceModel(QObject *parent = nullptr);
79 ~IncidenceOccurrenceModel() override = default;
80
81 int rowCount(const QModelIndex &parent = {}) const override;
82 QVariant data(const QModelIndex &index, int role) const override;
83 QHash<int, QByteArray> roleNames() const override;
84
85 Akonadi::ETMCalendar::Ptr calendar() const;
86 QDate start() const;
87 int length() const;
88 Filter *filter() const;
89 bool loading() const;
90 int resetThrottleInterval() const;
91
92 struct Occurrence {
94 QDateTime end;
96 QColor color;
97 qint64 collectionId;
98 bool allDay;
99 };
100
102 void startChanged();
103 void lengthChanged();
104 void filterChanged();
105 void calendarChanged();
106 void loadingChanged();
107 void resetThrottleIntervalChanged();
108
109public Q_SLOTS:
110 void setStart(const QDate &start);
111 void setLength(int length);
112 void setFilter(Filter *filter);
113 void setCalendar(Akonadi::ETMCalendar::Ptr calendar);
114 void setResetThrottleInterval(const int resetThrottleInterval);
115
116private Q_SLOTS:
117 void loadColors();
118 void scheduleReset();
119 void resetFromSource();
120
121 void slotSourceDataChanged(const QModelIndex &upperLeft, const QModelIndex &bottomRight);
122 void slotSourceRowsInserted(const QModelIndex &parent, const int first, const int last);
123
124 void setLoading(const bool loading);
125
126private:
127 static std::pair<QDateTime, QDateTime> incidenceOccurrenceStartEnd(const QDateTime &ocStart, const KCalendarCore::Incidence::Ptr &incidence);
128 static uint incidenceOccurrenceHash(const QDateTime &ocStart, const QDateTime &ocEnd, const QString &incidenceUid);
129 bool incidencePassesFilter(const KCalendarCore::Incidence::Ptr &incidence);
130
131 QColor getColor(const KCalendarCore::Incidence::Ptr &incidence);
132 qint64 getCollectionId(const KCalendarCore::Incidence::Ptr &incidence);
133
135 QDate mStart;
136 QDate mEnd;
137 int mLength{0};
138 Akonadi::ETMCalendar::Ptr m_coreCalendar;
139
140 QTimer m_resetThrottlingTimer;
141 int m_resetThrottleInterval = 100;
142
143 bool m_loading = false;
144 QVector<Occurrence> m_incidences; // We need incidences to be in a preditable order for the model
145 QHash<uint, QPersistentModelIndex> m_occurrenceIndexHash;
146 QHash<QString, QColor> m_colors;
147 KConfigWatcher::Ptr m_colorWatcher;
148 Filter *mFilter = nullptr;
149 KFormat m_format;
150};
151
152Q_DECLARE_METATYPE(IncidenceOccurrenceModel::Occurrence)
153
154#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
155Q_DECLARE_METATYPE(KCalendarCore::Incidence::Ptr)
156#endif
Loads all event occurrences within the given period and matching the given filter.
Q_SCRIPTABLE Q_NOREPLY void start()
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
Q_ENUM(...)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
T qobject_cast(QObject *object)
UserRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:50:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.