diff options
Diffstat (limited to 'framework/src/domain/eventtreemodel.cpp')
-rw-r--r-- | framework/src/domain/eventtreemodel.cpp | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/framework/src/domain/eventtreemodel.cpp b/framework/src/domain/eventtreemodel.cpp new file mode 100644 index 00000000..784a40f9 --- /dev/null +++ b/framework/src/domain/eventtreemodel.cpp | |||
@@ -0,0 +1,249 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Michael Bohlender <michael.bohlender@kdemail.net> | ||
3 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
4 | |||
5 | This library is free software; you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Library General Public License as published by | ||
7 | the Free Software Foundation; either version 2 of the License, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
13 | License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to the | ||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
18 | 02110-1301, USA. | ||
19 | */ | ||
20 | |||
21 | #include "eventtreemodel.h" | ||
22 | |||
23 | #include <sink/log.h> | ||
24 | #include <sink/query.h> | ||
25 | #include <sink/store.h> | ||
26 | |||
27 | #include <QJsonArray> | ||
28 | #include <QJsonObject> | ||
29 | #include <QMetaEnum> | ||
30 | |||
31 | EventTreeModel::EventTreeModel(QObject *parent) : QAbstractItemModel(parent), partitionedEvents(7) | ||
32 | { | ||
33 | Sink::Query query; | ||
34 | query.request<Event::Summary>(); | ||
35 | query.request<Event::Description>(); | ||
36 | query.request<Event::StartTime>(); | ||
37 | query.request<Event::EndTime>(); | ||
38 | |||
39 | eventModel = Sink::Store::loadModel<Event>(query); | ||
40 | |||
41 | QObject::connect(eventModel.data(), &QAbstractItemModel::dataChanged, this, &EventTreeModel::partitionData); | ||
42 | |||
43 | Sink::Store::synchronize(Sink::Query()); | ||
44 | |||
45 | partitionData(); | ||
46 | } | ||
47 | |||
48 | void EventTreeModel::partitionData() | ||
49 | { | ||
50 | SinkLog() << "Partitioning event data"; | ||
51 | |||
52 | for (int i = 0; i < 7; ++i) { | ||
53 | SinkLog() << "Clearing #" << i; | ||
54 | partitionedEvents[i].clear(); | ||
55 | } | ||
56 | |||
57 | for (int i = 0; i < eventModel->rowCount(); ++i) { | ||
58 | auto event = eventModel->index(i, 0).data(Sink::Store::DomainObjectRole).value<Event::Ptr>(); | ||
59 | int dayOfWeek = event->getStartTime().date().dayOfWeek(); | ||
60 | |||
61 | Q_ASSERT(dayOfWeek != 0); | ||
62 | |||
63 | SinkLog() << "Adding event in bucket #" << dayOfWeek; | ||
64 | |||
65 | partitionedEvents[dayOfWeek - 1].append(event); | ||
66 | } | ||
67 | |||
68 | // auto bottomRight = index(partitionedEvents[6].size(), eventModel->columnCount() - 1, index(6, | ||
69 | // 0)); | ||
70 | emit dataChanged({}, {}); | ||
71 | } | ||
72 | |||
73 | QModelIndex EventTreeModel::index(int row, int column, const QModelIndex &parent) const | ||
74 | { | ||
75 | SinkLog() << "Calling EventTreeModel::index with" << row << column << parent.isValid(); | ||
76 | |||
77 | if (!hasIndex(row, column, parent)) { | ||
78 | return {}; | ||
79 | } | ||
80 | |||
81 | if (!parent.isValid()) { | ||
82 | // Asking for a LocalEventListModel | ||
83 | |||
84 | // TODO: Assuming week view | ||
85 | if (!(0 <= row && row <= 6)) { | ||
86 | return {}; | ||
87 | } | ||
88 | |||
89 | return createIndex(row, column, DAY_ID); | ||
90 | } | ||
91 | |||
92 | // Asking for an Event | ||
93 | auto day = static_cast<int>(parent.internalId()); | ||
94 | |||
95 | Q_ASSERT(0 <= day && day <= 6); | ||
96 | if (row >= partitionedEvents[day].size()) { | ||
97 | return {}; | ||
98 | } | ||
99 | |||
100 | return createIndex(row, column, day); | ||
101 | } | ||
102 | |||
103 | QModelIndex EventTreeModel::parent(const QModelIndex &index) const | ||
104 | { | ||
105 | SinkLog() << "Calling GlobalEventListModel::parent with index:" << index.row() << index.column(); | ||
106 | |||
107 | if (!index.isValid()) { | ||
108 | return {}; | ||
109 | } | ||
110 | |||
111 | if (index.internalId() == DAY_ID) { | ||
112 | return {}; | ||
113 | } | ||
114 | |||
115 | auto day = index.internalId(); | ||
116 | |||
117 | return this->index(day, 0); | ||
118 | } | ||
119 | |||
120 | int EventTreeModel::rowCount(const QModelIndex &parent) const | ||
121 | { | ||
122 | if (!parent.isValid()) { | ||
123 | return 7; | ||
124 | } | ||
125 | |||
126 | auto day = parent.internalId(); | ||
127 | |||
128 | return partitionedEvents[day].size(); | ||
129 | } | ||
130 | |||
131 | int EventTreeModel::columnCount(const QModelIndex &parent) const | ||
132 | { | ||
133 | if (!parent.isValid()) { | ||
134 | return 1; | ||
135 | } | ||
136 | |||
137 | return eventModel->columnCount(); | ||
138 | } | ||
139 | |||
140 | bool EventTreeModel::hasChildren(const QModelIndex &parent) const | ||
141 | { | ||
142 | return parent.internalId() == DAY_ID; | ||
143 | } | ||
144 | |||
145 | QVariant EventTreeModel::data(const QModelIndex &id, int role) const | ||
146 | { | ||
147 | if (hasChildren(id)) { | ||
148 | auto day = id.row(); | ||
149 | |||
150 | switch (role) { | ||
151 | case Qt::DisplayRole: | ||
152 | switch (day) { | ||
153 | case 0: | ||
154 | return "Monday"; | ||
155 | case 1: | ||
156 | return "Tuesday"; | ||
157 | case 2: | ||
158 | return "Wednesday"; | ||
159 | case 3: | ||
160 | return "Thursday"; | ||
161 | case 4: | ||
162 | return "Friday"; | ||
163 | case 5: | ||
164 | return "Saturday"; | ||
165 | case 6: | ||
166 | return "Sunday"; | ||
167 | default: | ||
168 | SinkWarning() << "Unknown day"; | ||
169 | return {}; | ||
170 | } | ||
171 | case Events: | ||
172 | //{ | ||
173 | // auto result = QVariantList{}; | ||
174 | |||
175 | // for (int i = 0; i < partitionedEvents[day].size(); ++i) { | ||
176 | // auto eventId = index(i, 0, id); | ||
177 | // SinkLog() << "Appending event:" << data(eventId, Summary); | ||
178 | // result.append(QVariantMap{ | ||
179 | // {"text", data(eventId, Summary)}, | ||
180 | // {"description", data(eventId, Description)}, | ||
181 | // {"starts", data(eventId, StartTime)}, | ||
182 | // {"duration", data(eventId, Duration)}, | ||
183 | // {"color", "#123"}, | ||
184 | // {"indention", 0}, | ||
185 | // }); | ||
186 | // } | ||
187 | |||
188 | // return result; | ||
189 | //} | ||
190 | return QVariantList{ | ||
191 | QVariantMap{ | ||
192 | {"summary", "Hello, World!"}, | ||
193 | {"text", "Hello, World!"}, | ||
194 | {"description", "This is Hello, world!"}, | ||
195 | {"starts", 4}, | ||
196 | {"duration", 3}, | ||
197 | {"color", "#123"}, | ||
198 | {"indention", 0}, | ||
199 | }, | ||
200 | QVariantMap{ | ||
201 | {"summary", "Hello, World!"}, | ||
202 | {"text", "Hello, World!"}, | ||
203 | {"description", "This is Hello, world!"}, | ||
204 | {"starts", 8}, | ||
205 | {"duration", 3}, | ||
206 | {"color", "#456"}, | ||
207 | {"indention", 1}, | ||
208 | }, | ||
209 | }; | ||
210 | default: | ||
211 | SinkWarning() << "Unknown role for day:" << QMetaEnum::fromType<Roles>().valueToKey(role); | ||
212 | return {}; | ||
213 | } | ||
214 | } | ||
215 | |||
216 | auto day = static_cast<int>(id.internalId()); | ||
217 | SinkLog() << "Fetching data for day" << day; | ||
218 | auto event = partitionedEvents[day].at(id.row()); | ||
219 | |||
220 | switch (role) { | ||
221 | case Summary: | ||
222 | return event->getSummary(); | ||
223 | case Description: | ||
224 | return event->getDescription(); | ||
225 | case StartTime: | ||
226 | return event->getStartTime(); | ||
227 | case Duration: { | ||
228 | auto start = event->getStartTime(); | ||
229 | auto end = event->getEndTime(); | ||
230 | return start.secsTo(end) / 3600; | ||
231 | } | ||
232 | default: | ||
233 | SinkWarning() << "Unknown role for event:" << QMetaEnum::fromType<Roles>().valueToKey(role); | ||
234 | return {}; | ||
235 | } | ||
236 | } | ||
237 | |||
238 | QHash<int, QByteArray> EventTreeModel::roleNames() const | ||
239 | { | ||
240 | QHash<int, QByteArray> roles; | ||
241 | |||
242 | roles[Events] = "events"; | ||
243 | roles[Summary] = "summary"; | ||
244 | roles[Description] = "description"; | ||
245 | roles[StartTime] = "starts"; | ||
246 | roles[Duration] = "duration"; | ||
247 | |||
248 | return roles; | ||
249 | } | ||