diff options
Diffstat (limited to 'framework/src/domain/eventtreemodel.cpp')
-rw-r--r-- | framework/src/domain/eventtreemodel.cpp | 98 |
1 files changed, 61 insertions, 37 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 | ||
53 | void EventTreeModel::partitionData() | 53 | void 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 | |||
81 | int 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 | ||
82 | QModelIndex EventTreeModel::index(int row, int column, const QModelIndex &parent) const | 91 | QModelIndex 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 | |||
125 | int EventTreeModel::rowCount(const QModelIndex &parent) const | 133 | int 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 | |||
227 | QDate EventTreeModel::viewStart() const | ||
228 | { | ||
229 | return mViewStart; | ||
230 | } | ||
231 | |||
232 | void 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 | |||
243 | void EventTreeModel::setViewStart(QVariant start) | ||
244 | { | ||
245 | setViewStart(start.toDate()); | ||
246 | } | ||
247 | |||
248 | int EventTreeModel::viewLength() const | ||
249 | { | ||
250 | return mViewLength; | ||
251 | } | ||
252 | |||
253 | void EventTreeModel::setViewLength(int length) | ||
254 | { | ||
255 | mViewLength = std::move(length); | ||
256 | partitionData(); | ||
257 | } | ||