diff options
Diffstat (limited to 'framework/src/domain/daylongeventmodel.cpp')
-rw-r--r-- | framework/src/domain/daylongeventmodel.cpp | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/framework/src/domain/daylongeventmodel.cpp b/framework/src/domain/daylongeventmodel.cpp new file mode 100644 index 00000000..0ea73709 --- /dev/null +++ b/framework/src/domain/daylongeventmodel.cpp | |||
@@ -0,0 +1,123 @@ | |||
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 | #include "daylongeventmodel.h" | ||
23 | |||
24 | #include <sink/log.h> | ||
25 | #include <sink/query.h> | ||
26 | #include <sink/store.h> | ||
27 | |||
28 | DayLongEventModel::DayLongEventModel(QObject *parent) : QSortFilterProxyModel(parent) | ||
29 | { | ||
30 | Sink::Query query; | ||
31 | query.setFlags(Sink::Query::LiveQuery); | ||
32 | query.request<Event::Summary>(); | ||
33 | query.request<Event::Description>(); | ||
34 | query.request<Event::StartTime>(); | ||
35 | query.request<Event::EndTime>(); | ||
36 | |||
37 | query.filter<Event::AllDay>(true); | ||
38 | |||
39 | mModel = Sink::Store::loadModel<Event>(query); | ||
40 | |||
41 | setSourceModel(mModel.data()); | ||
42 | } | ||
43 | |||
44 | QHash<int, QByteArray> DayLongEventModel::roleNames() const | ||
45 | { | ||
46 | return { | ||
47 | {Summary, "summary"}, | ||
48 | {Description, "description"}, | ||
49 | {StartDate, "starts"}, | ||
50 | {Duration, "duration"}, | ||
51 | }; | ||
52 | } | ||
53 | |||
54 | QVariant DayLongEventModel::data(const QModelIndex &idx, int role) const | ||
55 | { | ||
56 | auto srcIdx = mapToSource(idx); | ||
57 | auto event = srcIdx.data(Sink::Store::DomainObjectRole).value<Event::Ptr>(); | ||
58 | |||
59 | switch (role) { | ||
60 | case Summary: | ||
61 | return event->getSummary(); | ||
62 | case Description: | ||
63 | return event->getDescription(); | ||
64 | case StartDate: { | ||
65 | auto dayIndex = mPeriodStart.daysTo(event->getStartTime().date()); | ||
66 | if (dayIndex < 0) { | ||
67 | return 0; | ||
68 | } | ||
69 | |||
70 | return dayIndex; | ||
71 | } | ||
72 | case Duration: | ||
73 | return event->getStartTime().date().daysTo(event->getEndTime().date()); | ||
74 | } | ||
75 | |||
76 | return QSortFilterProxyModel::data(idx, role); | ||
77 | } | ||
78 | |||
79 | bool DayLongEventModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const | ||
80 | { | ||
81 | auto idx = sourceModel()->index(sourceRow, 0, sourceParent); | ||
82 | auto event = idx.data(Sink::Store::DomainObjectRole).value<Event::Ptr>(); | ||
83 | |||
84 | auto eventStart = event->getStartTime().date(); | ||
85 | auto eventEnd = event->getEndTime().date(); | ||
86 | |||
87 | if (!eventStart.isValid() || !eventEnd.isValid()) { | ||
88 | SinkWarning() << "Invalid date in the event model, ignoring..."; | ||
89 | return false; | ||
90 | } | ||
91 | |||
92 | return eventStart < mPeriodStart.addDays(mPeriodLength) && eventEnd >= mPeriodStart; | ||
93 | } | ||
94 | |||
95 | QDate DayLongEventModel::periodStart() const | ||
96 | { | ||
97 | return mPeriodStart; | ||
98 | } | ||
99 | |||
100 | void DayLongEventModel::setPeriodStart(const QDate &start) | ||
101 | { | ||
102 | if (!start.isValid()) { | ||
103 | SinkWarning() << "Passed an invalid starting date in setPeriodStart, ignoring..."; | ||
104 | return; | ||
105 | } | ||
106 | |||
107 | mPeriodStart = start; | ||
108 | } | ||
109 | |||
110 | void DayLongEventModel::setPeriodStart(const QVariant &start) | ||
111 | { | ||
112 | setPeriodStart(start.toDate()); | ||
113 | } | ||
114 | |||
115 | int DayLongEventModel::periodLength() const | ||
116 | { | ||
117 | return mPeriodLength; | ||
118 | } | ||
119 | |||
120 | void DayLongEventModel::setPeriodLength(int length) | ||
121 | { | ||
122 | mPeriodLength = length; | ||
123 | } | ||