summaryrefslogtreecommitdiffstats
path: root/framework
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2018-04-13 10:25:42 +0200
committerMinijackson <minijackson@riseup.net>2018-04-16 10:18:33 +0200
commitcabc867b3e7cbf56ac2d8f646c52deb59a3197f4 (patch)
tree2d9a193601d40b34a99af4949b0ff08f435bb1ad /framework
parent5f425961620431040f1fabe77f70427b49d230fc (diff)
downloadkube-cabc867b3e7cbf56ac2d8f646c52deb59a3197f4.tar.gz
kube-cabc867b3e7cbf56ac2d8f646c52deb59a3197f4.zip
Make the event model size-generic and filter dates
Diffstat (limited to 'framework')
-rw-r--r--framework/src/domain/eventtreemodel.cpp98
-rw-r--r--framework/src/domain/eventtreemodel.h25
2 files changed, 80 insertions, 43 deletions
diff --git a/framework/src/domain/eventtreemodel.cpp b/framework/src/domain/eventtreemodel.cpp
index 9f9bbdca..4e25cffe 100644
--- a/framework/src/domain/eventtreemodel.cpp
+++ b/framework/src/domain/eventtreemodel.cpp
@@ -52,31 +52,40 @@ EventTreeModel::EventTreeModel(QObject *parent) : QAbstractItemModel(parent), pa
52 52
53void EventTreeModel::partitionData() 53void EventTreeModel::partitionData()
54{ 54{
55 partitioningMutex.lock();
56
57 SinkLog() << "Partitioning event data"; 55 SinkLog() << "Partitioning event data";
58 56
59 beginResetModel(); 57 beginResetModel();
60 58
61 for (int i = 0; i < 7; ++i) { 59 partitionedEvents = QVector<QList<QSharedPointer<Event>>>(mViewLength);
62 partitionedEvents[i].clear();
63 }
64 60
65 for (int i = 0; i < eventModel->rowCount(); ++i) { 61 for (int i = 0; i < eventModel->rowCount(); ++i) {
66 auto event = eventModel->index(i, 0).data(Sink::Store::DomainObjectRole).value<Event::Ptr>(); 62 auto event = eventModel->index(i, 0).data(Sink::Store::DomainObjectRole).value<Event::Ptr>();
67 int dayOfWeek = event->getStartTime().date().dayOfWeek(); 63 QDate eventDate = event->getStartTime().date();
68 64
69 Q_ASSERT(dayOfWeek != 0); 65 if(!eventDate.isValid()) {
70 dayOfWeek -= 1; 66 SinkWarning() << "Invalid date in the eventModel, ignoring...";
67 continue;
68 }
71 69
72 SinkTrace() << "Adding event:" << event->getSummary() << "in bucket #" << dayOfWeek; 70 int bucket = bucketOf(eventDate);
73 71
74 partitionedEvents[dayOfWeek].append(event); 72 if(bucket >= 0) {
73 SinkTrace() << "Adding event:" << event->getSummary() << "in bucket #" << bucket;
74 partitionedEvents[bucket].append(event);
75 }
75 } 76 }
76 77
77 endResetModel(); 78 endResetModel();
79}
80
81int EventTreeModel::bucketOf(QDate const &candidate) const
82{
83 int bucket = mViewStart.daysTo(candidate);
84 if(bucket >= mViewLength || bucket < 0) {
85 return -1;
86 }
78 87
79 partitioningMutex.unlock(); 88 return bucket;
80} 89}
81 90
82QModelIndex EventTreeModel::index(int row, int column, const QModelIndex &parent) const 91QModelIndex EventTreeModel::index(int row, int column, const QModelIndex &parent) const
@@ -86,10 +95,9 @@ QModelIndex EventTreeModel::index(int row, int column, const QModelIndex &parent
86 } 95 }
87 96
88 if (!parent.isValid()) { 97 if (!parent.isValid()) {
89 // Asking for a LocalEventListModel 98 // Asking for a day
90 99
91 // TODO: Assuming week view 100 if (!(0 <= row && row < mViewLength)) {
92 if (!(0 <= row && row <= 6)) {
93 return {}; 101 return {};
94 } 102 }
95 103
@@ -99,7 +107,7 @@ QModelIndex EventTreeModel::index(int row, int column, const QModelIndex &parent
99 // Asking for an Event 107 // Asking for an Event
100 auto day = static_cast<int>(parent.row()); 108 auto day = static_cast<int>(parent.row());
101 109
102 Q_ASSERT(0 <= day && day <= 6); 110 Q_ASSERT(0 <= day && day <= mViewLength);
103 if (row >= partitionedEvents[day].size()) { 111 if (row >= partitionedEvents[day].size()) {
104 return {}; 112 return {};
105 } 113 }
@@ -125,7 +133,7 @@ QModelIndex EventTreeModel::parent(const QModelIndex &index) const
125int EventTreeModel::rowCount(const QModelIndex &parent) const 133int EventTreeModel::rowCount(const QModelIndex &parent) const
126{ 134{
127 if (!parent.isValid()) { 135 if (!parent.isValid()) {
128 return 7; 136 return mViewLength;
129 } 137 }
130 138
131 auto day = parent.row(); 139 auto day = parent.row();
@@ -147,29 +155,12 @@ QVariant EventTreeModel::data(const QModelIndex &id, int role) const
147 if (id.internalId() == DAY_ID) { 155 if (id.internalId() == DAY_ID) {
148 auto day = id.row(); 156 auto day = id.row();
149 157
150 SinkTrace() << "Fetching data for day" << day << "with role" << QMetaEnum::fromType<Roles>().valueToKey(role); 158 SinkTrace() << "Fetching data for day" << day << "with role"
159 << QMetaEnum::fromType<Roles>().valueToKey(role);
151 160
152 switch (role) { 161 switch (role) {
153 case Qt::DisplayRole: 162 case Qt::DisplayRole:
154 switch (day) { 163 return mViewStart.addDays(day).toString();
155 case 0:
156 return "Monday";
157 case 1:
158 return "Tuesday";
159 case 2:
160 return "Wednesday";
161 case 3:
162 return "Thursday";
163 case 4:
164 return "Friday";
165 case 5:
166 return "Saturday";
167 case 6:
168 return "Sunday";
169 default:
170 SinkWarning() << "Unknown day";
171 return {};
172 }
173 case Events: { 164 case Events: {
174 auto result = QVariantList{}; 165 auto result = QVariantList{};
175 166
@@ -197,7 +188,8 @@ QVariant EventTreeModel::data(const QModelIndex &id, int role) const
197 } 188 }
198 } else { 189 } else {
199 auto day = id.internalId(); 190 auto day = id.internalId();
200 SinkTrace() << "Fetching data for event on day" << day << "with role" << QMetaEnum::fromType<Roles>().valueToKey(role); 191 SinkTrace() << "Fetching data for event on day" << day << "with role"
192 << QMetaEnum::fromType<Roles>().valueToKey(role);
201 auto event = partitionedEvents[day].at(id.row()); 193 auto event = partitionedEvents[day].at(id.row());
202 194
203 switch (role) { 195 switch (role) {
@@ -231,3 +223,35 @@ QHash<int, QByteArray> EventTreeModel::roleNames() const
231 223
232 return roles; 224 return roles;
233} 225}
226
227QDate EventTreeModel::viewStart() const
228{
229 return mViewStart;
230}
231
232void EventTreeModel::setViewStart(QDate start)
233{
234 if (!start.isValid()) {
235 SinkWarning() << "Passed an invalid starting date in setViewStart, ignoring...";
236 return;
237 }
238
239 mViewStart = std::move(start);
240 partitionData();
241}
242
243void EventTreeModel::setViewStart(QVariant start)
244{
245 setViewStart(start.toDate());
246}
247
248int EventTreeModel::viewLength() const
249{
250 return mViewLength;
251}
252
253void EventTreeModel::setViewLength(int length)
254{
255 mViewLength = std::move(length);
256 partitionData();
257}
diff --git a/framework/src/domain/eventtreemodel.h b/framework/src/domain/eventtreemodel.h
index 77189073..516410ee 100644
--- a/framework/src/domain/eventtreemodel.h
+++ b/framework/src/domain/eventtreemodel.h
@@ -25,10 +25,11 @@
25 25
26#include <QAbstractItemModel> 26#include <QAbstractItemModel>
27#include <QList> 27#include <QList>
28#include <QMutex>
29#include <QSharedPointer> 28#include <QSharedPointer>
30#include <QVector> 29#include <QVector>
31 30
31#include <limits>
32
32// Implementation notes 33// Implementation notes
33// ==================== 34// ====================
34// 35//
@@ -38,8 +39,8 @@
38// Columns are never used. 39// Columns are never used.
39// 40//
40// Top-level items just contains the ".events" attribute, and their rows 41// Top-level items just contains the ".events" attribute, and their rows
41// correspond to their day of week. In that case the internalId contains 42// correspond to their offset compared to the start of the view (in number of
42// DAY_ID. 43// days). In that case the internalId contains DAY_ID.
43// 44//
44// Direct children are events, and their rows corresponds to their index in 45// Direct children are events, and their rows corresponds to their index in
45// their partition. In that case no internalId / internalPointer is used. 46// their partition. In that case no internalId / internalPointer is used.
@@ -60,6 +61,9 @@ class EventTreeModel : public QAbstractItemModel
60{ 61{
61 Q_OBJECT 62 Q_OBJECT
62 63
64 Q_PROPERTY(QVariant viewStart READ viewStart WRITE setViewStart)
65 Q_PROPERTY(int viewLength READ viewLength WRITE setViewLength)
66
63public: 67public:
64 using Event = Sink::ApplicationDomain::Event; 68 using Event = Sink::ApplicationDomain::Event;
65 69
@@ -85,13 +89,22 @@ public:
85 89
86 QHash<int, QByteArray> roleNames() const override; 90 QHash<int, QByteArray> roleNames() const override;
87 91
92 QDate viewStart() const;
93 void setViewStart(QDate);
94 void setViewStart(QVariant);
95 int viewLength() const;
96 void setViewLength(int);
97
88private: 98private:
89 void partitionData(); 99 void partitionData();
90 100
101 int bucketOf(QDate const &candidate) const;
102
103 QDate mViewStart;
104 int mViewLength = 7;
105
91 QSharedPointer<QAbstractItemModel> eventModel; 106 QSharedPointer<QAbstractItemModel> eventModel;
92 QVector<QList<QSharedPointer<Event>>> partitionedEvents; 107 QVector<QList<QSharedPointer<Event>>> partitionedEvents;
93 108
94 QMutex partitioningMutex; 109 static const constexpr quintptr DAY_ID = std::numeric_limits<quintptr>::max();
95
96 static const constexpr quintptr DAY_ID = 7;
97}; 110};