diff options
Diffstat (limited to 'framework/src/domain/perioddayeventmodel.h')
-rw-r--r-- | framework/src/domain/perioddayeventmodel.h | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/framework/src/domain/perioddayeventmodel.h b/framework/src/domain/perioddayeventmodel.h new file mode 100644 index 00000000..e116d9a2 --- /dev/null +++ b/framework/src/domain/perioddayeventmodel.h | |||
@@ -0,0 +1,129 @@ | |||
1 | /* | ||
2 | Copyright (c) 2018 Michael Bohlender <michael.bohlender@kdemail.net> | ||
3 | Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com> | ||
4 | Copyright (c) 2018 Rémi Nicole <minijackson@riseup.net> | ||
5 | |||
6 | This library is free software; you can redistribute it and/or modify it | ||
7 | under the terms of the GNU Library General Public License as published by | ||
8 | the Free Software Foundation; either version 2 of the License, or (at your | ||
9 | option) any later version. | ||
10 | |||
11 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
12 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
14 | License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU Library General Public License | ||
17 | along with this library; see the file COPYING.LIB. If not, write to the | ||
18 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
19 | 02110-1301, USA. | ||
20 | */ | ||
21 | |||
22 | #pragma once | ||
23 | |||
24 | #include <sink/applicationdomaintype.h> | ||
25 | |||
26 | #include <QAbstractItemModel> | ||
27 | #include <QList> | ||
28 | #include <QSharedPointer> | ||
29 | #include <QVector> | ||
30 | |||
31 | #include <limits> | ||
32 | |||
33 | // Facility used to get a restricted period into a Sink model comprised of | ||
34 | // events, partitioned according to the day the events take place. | ||
35 | // | ||
36 | // Model Format | ||
37 | // ============ | ||
38 | // | ||
39 | // Day 0 | ||
40 | // |--- Event 0 starting at `periodStart + 0d` | ||
41 | // |--- Event 1 starting at `periodStart + 0d` | ||
42 | // '--- Event 2 starting at `periodStart + 0d` | ||
43 | // Day 1 | ||
44 | // '--- Event 0 starting at `periodStart + 1d` | ||
45 | // Day 2 | ||
46 | // Day 3 | ||
47 | // |--- Event 0 starting at `periodStart + 3d` | ||
48 | // '--- Event 1 starting at `periodStart + 3d` | ||
49 | // Day 4 | ||
50 | // ⋮ | ||
51 | // | ||
52 | // Implementation notes | ||
53 | // ==================== | ||
54 | // | ||
55 | // On the model side | ||
56 | // ----------------- | ||
57 | // | ||
58 | // Columns are never used. | ||
59 | // | ||
60 | // Top-level items just contains the ".events" attribute, and their rows | ||
61 | // correspond to their offset compared to the start of the period (in number of | ||
62 | // days). In that case the internalId contains DAY_ID. | ||
63 | // | ||
64 | // Direct children are events, and their rows corresponds to their index in | ||
65 | // their partition. In that case no internalId / internalPointer is used. | ||
66 | // | ||
67 | // Internally: | ||
68 | // ----------- | ||
69 | // | ||
70 | // On construction and on dataChanged, all events are processed and partitioned | ||
71 | // in partitionedEvents: | ||
72 | // | ||
73 | // QVector< QList<QSharedPointer<Event> > | ||
74 | // ~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
75 | // | | | ||
76 | // | '--- List of event pointers for that day | ||
77 | // '--- Partition / day | ||
78 | // | ||
79 | class PeriodDayEventModel : public QAbstractItemModel | ||
80 | { | ||
81 | Q_OBJECT | ||
82 | |||
83 | Q_PROPERTY(QVariant start READ periodStart WRITE setPeriodStart) | ||
84 | Q_PROPERTY(int length READ periodLength WRITE setPeriodLength) | ||
85 | |||
86 | public: | ||
87 | using Event = Sink::ApplicationDomain::Event; | ||
88 | |||
89 | enum Roles | ||
90 | { | ||
91 | Events = Qt::UserRole + 1, | ||
92 | Summary, | ||
93 | Description, | ||
94 | StartTime, | ||
95 | Duration, | ||
96 | }; | ||
97 | Q_ENUM(Roles); | ||
98 | PeriodDayEventModel(QObject *parent = nullptr); | ||
99 | ~PeriodDayEventModel() = default; | ||
100 | |||
101 | QModelIndex index(int row, int column, const QModelIndex &parent = {}) const override; | ||
102 | QModelIndex parent(const QModelIndex &index) const override; | ||
103 | |||
104 | int rowCount(const QModelIndex &parent) const override; | ||
105 | int columnCount(const QModelIndex &parent) const override; | ||
106 | |||
107 | QVariant data(const QModelIndex &index, int role) const override; | ||
108 | |||
109 | QHash<int, QByteArray> roleNames() const override; | ||
110 | |||
111 | QDate periodStart() const; | ||
112 | void setPeriodStart(const QDate &); | ||
113 | void setPeriodStart(const QVariant &); | ||
114 | int periodLength() const; | ||
115 | void setPeriodLength(int); | ||
116 | |||
117 | private: | ||
118 | void partitionData(); | ||
119 | |||
120 | int bucketOf(const QDate &candidate) const; | ||
121 | |||
122 | QDate mPeriodStart; | ||
123 | int mPeriodLength = 7; | ||
124 | |||
125 | QSharedPointer<QAbstractItemModel> eventModel; | ||
126 | QVector<QList<QSharedPointer<Event>>> partitionedEvents; | ||
127 | |||
128 | static const constexpr quintptr DAY_ID = std::numeric_limits<quintptr>::max(); | ||
129 | }; | ||