summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2018-08-03 13:49:56 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2018-08-03 13:56:58 +0200
commit2085b83960cbb8e4693cf48a5bd265aa946256de (patch)
tree58f5d6f7ff18cd63ac0aa8827dadfbb66e269083
parent064367aec304708591f5cd4d010ed462119a0323 (diff)
downloadkube-2085b83960cbb8e4693cf48a5bd265aa946256de.tar.gz
kube-2085b83960cbb8e4693cf48a5bd265aa946256de.zip
Weekview with calendar colors
-rw-r--r--framework/src/domain/daylongeventmodel.cpp13
-rw-r--r--framework/src/domain/daylongeventmodel.h6
-rw-r--r--framework/src/domain/perioddayeventmodel.cpp15
-rw-r--r--framework/src/domain/perioddayeventmodel.h6
-rw-r--r--framework/src/entitycache.h77
-rw-r--r--views/calendar/qml/DaylongEvents.qml6
-rw-r--r--views/calendar/qml/WeekEvents.qml6
-rw-r--r--views/calendar/qml/WeekView.qml10
8 files changed, 120 insertions, 19 deletions
diff --git a/framework/src/domain/daylongeventmodel.cpp b/framework/src/domain/daylongeventmodel.cpp
index 0ea73709..1b47edda 100644
--- a/framework/src/domain/daylongeventmodel.cpp
+++ b/framework/src/domain/daylongeventmodel.cpp
@@ -25,6 +25,8 @@
25#include <sink/query.h> 25#include <sink/query.h>
26#include <sink/store.h> 26#include <sink/store.h>
27 27
28#include "entitycache.h"
29
28DayLongEventModel::DayLongEventModel(QObject *parent) : QSortFilterProxyModel(parent) 30DayLongEventModel::DayLongEventModel(QObject *parent) : QSortFilterProxyModel(parent)
29{ 31{
30 Sink::Query query; 32 Sink::Query query;
@@ -33,11 +35,14 @@ DayLongEventModel::DayLongEventModel(QObject *parent) : QSortFilterProxyModel(pa
33 query.request<Event::Description>(); 35 query.request<Event::Description>();
34 query.request<Event::StartTime>(); 36 query.request<Event::StartTime>();
35 query.request<Event::EndTime>(); 37 query.request<Event::EndTime>();
38 query.request<Event::Calendar>();
36 39
37 query.filter<Event::AllDay>(true); 40 query.filter<Event::AllDay>(true);
38 41
39 mModel = Sink::Store::loadModel<Event>(query); 42 mModel = Sink::Store::loadModel<Event>(query);
40 43
44 mCalendarCache = EntityCache<Calendar, Calendar::Color>::Ptr::create();
45
41 setSourceModel(mModel.data()); 46 setSourceModel(mModel.data());
42} 47}
43 48
@@ -48,9 +53,15 @@ QHash<int, QByteArray> DayLongEventModel::roleNames() const
48 {Description, "description"}, 53 {Description, "description"},
49 {StartDate, "starts"}, 54 {StartDate, "starts"},
50 {Duration, "duration"}, 55 {Duration, "duration"},
56 {Color, "color"},
51 }; 57 };
52} 58}
53 59
60QByteArray DayLongEventModel::getColor(const QByteArray &calendar) const
61{
62 return mCalendarCache->getProperty(calendar, "color").toByteArray();
63}
64
54QVariant DayLongEventModel::data(const QModelIndex &idx, int role) const 65QVariant DayLongEventModel::data(const QModelIndex &idx, int role) const
55{ 66{
56 auto srcIdx = mapToSource(idx); 67 auto srcIdx = mapToSource(idx);
@@ -71,6 +82,8 @@ QVariant DayLongEventModel::data(const QModelIndex &idx, int role) const
71 } 82 }
72 case Duration: 83 case Duration:
73 return event->getStartTime().date().daysTo(event->getEndTime().date()); 84 return event->getStartTime().date().daysTo(event->getEndTime().date());
85 case Color:
86 return getColor(event->getCalendar());
74 } 87 }
75 88
76 return QSortFilterProxyModel::data(idx, role); 89 return QSortFilterProxyModel::data(idx, role);
diff --git a/framework/src/domain/daylongeventmodel.h b/framework/src/domain/daylongeventmodel.h
index 21bcbfba..12827bef 100644
--- a/framework/src/domain/daylongeventmodel.h
+++ b/framework/src/domain/daylongeventmodel.h
@@ -28,6 +28,7 @@
28#include <QSortFilterProxyModel> 28#include <QSortFilterProxyModel>
29#include <QVector> 29#include <QVector>
30 30
31class EntityCacheInterface;
31class KUBE_EXPORT DayLongEventModel : public QSortFilterProxyModel 32class KUBE_EXPORT DayLongEventModel : public QSortFilterProxyModel
32{ 33{
33 Q_OBJECT 34 Q_OBJECT
@@ -37,6 +38,7 @@ class KUBE_EXPORT DayLongEventModel : public QSortFilterProxyModel
37 38
38public: 39public:
39 using Event = Sink::ApplicationDomain::Event; 40 using Event = Sink::ApplicationDomain::Event;
41 using Calendar = Sink::ApplicationDomain::Calendar;
40 42
41 enum Roles 43 enum Roles
42 { 44 {
@@ -44,6 +46,7 @@ public:
44 Description, 46 Description,
45 StartDate, 47 StartDate,
46 Duration, 48 Duration,
49 Color
47 }; 50 };
48 Q_ENUM(Roles); 51 Q_ENUM(Roles);
49 52
@@ -62,7 +65,10 @@ public:
62 void setPeriodLength(int); 65 void setPeriodLength(int);
63 66
64private: 67private:
68 QByteArray getColor(const QByteArray &calendar) const;
69
65 QSharedPointer<QAbstractItemModel> mModel; 70 QSharedPointer<QAbstractItemModel> mModel;
71 QSharedPointer<EntityCacheInterface> mCalendarCache;
66 72
67 QDate mPeriodStart; 73 QDate mPeriodStart;
68 int mPeriodLength = 7; 74 int mPeriodLength = 7;
diff --git a/framework/src/domain/perioddayeventmodel.cpp b/framework/src/domain/perioddayeventmodel.cpp
index c5f2c197..4463e252 100644
--- a/framework/src/domain/perioddayeventmodel.cpp
+++ b/framework/src/domain/perioddayeventmodel.cpp
@@ -29,6 +29,8 @@
29#include <QJsonObject> 29#include <QJsonObject>
30#include <QMetaEnum> 30#include <QMetaEnum>
31 31
32#include <entitycache.h>
33
32PeriodDayEventModel::PeriodDayEventModel(QObject *parent) 34PeriodDayEventModel::PeriodDayEventModel(QObject *parent)
33 : QAbstractItemModel(parent), partitionedEvents(7) 35 : QAbstractItemModel(parent), partitionedEvents(7)
34{ 36{
@@ -43,6 +45,7 @@ void PeriodDayEventModel::updateQuery()
43 query.request<Event::Description>(); 45 query.request<Event::Description>();
44 query.request<Event::StartTime>(); 46 query.request<Event::StartTime>();
45 query.request<Event::EndTime>(); 47 query.request<Event::EndTime>();
48 query.request<Event::Calendar>();
46 49
47 auto periodEnd = mPeriodStart.addDays(mPeriodLength); 50 auto periodEnd = mPeriodStart.addDays(mPeriodLength);
48 51
@@ -59,6 +62,8 @@ void PeriodDayEventModel::updateQuery()
59 QObject::connect(eventModel.data(), &QAbstractItemModel::rowsMoved, this, &PeriodDayEventModel::partitionData); 62 QObject::connect(eventModel.data(), &QAbstractItemModel::rowsMoved, this, &PeriodDayEventModel::partitionData);
60 QObject::connect(eventModel.data(), &QAbstractItemModel::rowsRemoved, this, &PeriodDayEventModel::partitionData); 63 QObject::connect(eventModel.data(), &QAbstractItemModel::rowsRemoved, this, &PeriodDayEventModel::partitionData);
61 64
65 mCalendarCache = EntityCache<Calendar, Calendar::Color>::Ptr::create();
66
62 partitionData(); 67 partitionData();
63} 68}
64 69
@@ -157,6 +162,11 @@ int PeriodDayEventModel::columnCount(const QModelIndex &parent) const
157 return eventModel->columnCount(); 162 return eventModel->columnCount();
158} 163}
159 164
165QByteArray PeriodDayEventModel::getColor(const QByteArray &calendar) const
166{
167 return mCalendarCache->getProperty(calendar, "color").toByteArray();
168}
169
160QVariant PeriodDayEventModel::data(const QModelIndex &id, int role) const 170QVariant PeriodDayEventModel::data(const QModelIndex &id, int role) const
161{ 171{
162 if (id.internalId() == DAY_ID) { 172 if (id.internalId() == DAY_ID) {
@@ -184,7 +194,7 @@ QVariant PeriodDayEventModel::data(const QModelIndex &id, int role) const
184 {"description", data(eventId, Description)}, 194 {"description", data(eventId, Description)},
185 {"starts", startTime.hour() + startTime.minute() / 60.}, 195 {"starts", startTime.hour() + startTime.minute() / 60.},
186 {"duration", data(eventId, Duration)}, 196 {"duration", data(eventId, Duration)},
187 {"color", "#134bab"}, 197 {"color", data(eventId, Color)},
188 {"indention", 0}, 198 {"indention", 0},
189 }); 199 });
190 } 200 }
@@ -213,6 +223,8 @@ QVariant PeriodDayEventModel::data(const QModelIndex &id, int role) const
213 auto end = event->getEndTime(); 223 auto end = event->getEndTime();
214 return start.secsTo(end) / 3600; 224 return start.secsTo(end) / 3600;
215 } 225 }
226 case Color:
227 return getColor(event->getCalendar());
216 default: 228 default:
217 SinkWarning() << "Unknown role for event:" << QMetaEnum::fromType<Roles>().valueToKey(role); 229 SinkWarning() << "Unknown role for event:" << QMetaEnum::fromType<Roles>().valueToKey(role);
218 return {}; 230 return {};
@@ -229,6 +241,7 @@ QHash<int, QByteArray> PeriodDayEventModel::roleNames() const
229 {Description, "description"}, 241 {Description, "description"},
230 {StartTime, "starts"}, 242 {StartTime, "starts"},
231 {Duration, "duration"}, 243 {Duration, "duration"},
244 {Color, "color"}
232 }; 245 };
233} 246}
234 247
diff --git a/framework/src/domain/perioddayeventmodel.h b/framework/src/domain/perioddayeventmodel.h
index a7d9cea8..a0410e2e 100644
--- a/framework/src/domain/perioddayeventmodel.h
+++ b/framework/src/domain/perioddayeventmodel.h
@@ -78,6 +78,8 @@
78// | '--- List of event pointers for that day 78// | '--- List of event pointers for that day
79// '--- Partition / day 79// '--- Partition / day
80// 80//
81
82class EntityCacheInterface;
81class KUBE_EXPORT PeriodDayEventModel : public QAbstractItemModel 83class KUBE_EXPORT PeriodDayEventModel : public QAbstractItemModel
82{ 84{
83 Q_OBJECT 85 Q_OBJECT
@@ -87,6 +89,7 @@ class KUBE_EXPORT PeriodDayEventModel : public QAbstractItemModel
87 89
88public: 90public:
89 using Event = Sink::ApplicationDomain::Event; 91 using Event = Sink::ApplicationDomain::Event;
92 using Calendar = Sink::ApplicationDomain::Calendar;
90 93
91 enum Roles 94 enum Roles
92 { 95 {
@@ -96,6 +99,7 @@ public:
96 Description, 99 Description,
97 StartTime, 100 StartTime,
98 Duration, 101 Duration,
102 Color
99 }; 103 };
100 Q_ENUM(Roles); 104 Q_ENUM(Roles);
101 PeriodDayEventModel(QObject *parent = nullptr); 105 PeriodDayEventModel(QObject *parent = nullptr);
@@ -120,6 +124,7 @@ public:
120private: 124private:
121 void updateQuery(); 125 void updateQuery();
122 void partitionData(); 126 void partitionData();
127 QByteArray getColor(const QByteArray &calendar) const;
123 128
124 int bucketOf(const QDate &candidate) const; 129 int bucketOf(const QDate &candidate) const;
125 130
@@ -128,6 +133,7 @@ private:
128 133
129 QSharedPointer<QAbstractItemModel> eventModel; 134 QSharedPointer<QAbstractItemModel> eventModel;
130 QVector<QList<QSharedPointer<Event>>> partitionedEvents; 135 QVector<QList<QSharedPointer<Event>>> partitionedEvents;
136 QSharedPointer<EntityCacheInterface> mCalendarCache;
131 137
132 static const constexpr quintptr DAY_ID = std::numeric_limits<quintptr>::max(); 138 static const constexpr quintptr DAY_ID = std::numeric_limits<quintptr>::max();
133}; 139};
diff --git a/framework/src/entitycache.h b/framework/src/entitycache.h
new file mode 100644
index 00000000..485bcbdd
--- /dev/null
+++ b/framework/src/entitycache.h
@@ -0,0 +1,77 @@
1/*
2 Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#pragma once
20
21#include "kube_export.h"
22#include <sink/query.h>
23#include <sink/store.h>
24#include <QSharedPointer>
25#include <QAbstractItemModel>
26
27class KUBE_EXPORT EntityCacheInterface
28{
29public:
30 typedef QSharedPointer<EntityCacheInterface> Ptr;
31 EntityCacheInterface() = default;
32 virtual ~EntityCacheInterface() = default;
33
34 virtual QVariant getProperty(const QByteArray &identifier, const QByteArray &property) const = 0;
35};
36
37template<typename DomainType, typename Property>
38class KUBE_EXPORT EntityCache : public EntityCacheInterface
39{
40public:
41 typedef QSharedPointer<EntityCache> Ptr;
42
43 EntityCache();
44 virtual ~EntityCache() = default;
45
46 virtual QVariant getProperty(const QByteArray &, const QByteArray &) const override;
47
48private:
49 QHash<QByteArray, typename DomainType::Ptr> mCache;
50 QSharedPointer<QAbstractItemModel> mModel;
51};
52
53template<typename DomainType, typename Property>
54EntityCache<DomainType, Property>::EntityCache()
55 : EntityCacheInterface()
56{
57 Sink::Query query;
58 query.request<Property>();
59 query.setFlags(Sink::Query::LiveQuery);
60 mModel = Sink::Store::loadModel<DomainType>(query);
61 QObject::connect(mModel.data(), &QAbstractItemModel::rowsInserted, mModel.data(), [this] (const QModelIndex &, int start, int end) {
62 for (int row = start; row <= end; row++) {
63 auto entity = mModel->index(row, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).template value<typename DomainType::Ptr>();
64 mCache.insert(entity->identifier(), entity);
65 }
66 });
67}
68
69template<typename DomainType, typename Property>
70QVariant EntityCache<DomainType, Property>::getProperty(const QByteArray &identifier, const QByteArray &property) const
71{
72 if (auto entity = mCache.value(identifier)) {
73 return entity->getProperty(property);
74 }
75 return {};
76}
77
diff --git a/views/calendar/qml/DaylongEvents.qml b/views/calendar/qml/DaylongEvents.qml
deleted file mode 100644
index a338cf3b..00000000
--- a/views/calendar/qml/DaylongEvents.qml
+++ /dev/null
@@ -1,6 +0,0 @@
1import QtQuick 2.7
2
3import org.kube.framework 1.0 as Kube
4
5Kube.DayLongEventModel {
6}
diff --git a/views/calendar/qml/WeekEvents.qml b/views/calendar/qml/WeekEvents.qml
deleted file mode 100644
index 73774b27..00000000
--- a/views/calendar/qml/WeekEvents.qml
+++ /dev/null
@@ -1,6 +0,0 @@
1import QtQuick 2.7
2
3import org.kube.framework 1.0 as Kube
4
5Kube.PeriodDayEventModel {
6}
diff --git a/views/calendar/qml/WeekView.qml b/views/calendar/qml/WeekView.qml
index fb49e8e0..08c56e5a 100644
--- a/views/calendar/qml/WeekView.qml
+++ b/views/calendar/qml/WeekView.qml
@@ -111,7 +111,7 @@ FocusScope {
111 111
112 clip: true 112 clip: true
113 113
114 model: DaylongEvents { 114 model: Kube.DayLongEventModel {
115 start: root.startDate 115 start: root.startDate
116 length: root.daysToShow 116 length: root.daysToShow
117 } 117 }
@@ -124,8 +124,7 @@ FocusScope {
124 width: root.dayWidth * model.duration 124 width: root.dayWidth * model.duration
125 height: parent.height 125 height: parent.height
126 x: root.dayWidth * model.starts 126 x: root.dayWidth * model.starts
127 //color: model.color 127 color: model.color
128 color: Kube.Colors.jazzberryJam
129 border.width: 1 128 border.width: 1
130 border.color: Kube.Colors.viewBackgroundColor 129 border.color: Kube.Colors.viewBackgroundColor
131 130
@@ -204,7 +203,7 @@ FocusScope {
204 //END time labels 203 //END time labels
205 204
206 Repeater { 205 Repeater {
207 model: WeekEvents { 206 model: Kube.PeriodDayEventModel {
208 start: root.startDate 207 start: root.startDate
209 length: root.daysToShow 208 length: root.daysToShow
210 } 209 }
@@ -266,8 +265,7 @@ FocusScope {
266 y: root.hourHeight * model.modelData.starts 265 y: root.hourHeight * model.modelData.starts
267 x: Kube.Units.gridUnit * model.modelData.indention 266 x: Kube.Units.gridUnit * model.modelData.indention
268 267
269 //color: model.modelData.color 268 color: model.modelData.color
270 color: Kube.Colors.bewareOrange
271 opacity: 0.8 269 opacity: 0.8
272 border.width: 1 270 border.width: 1
273 border.color: Kube.Colors.viewBackgroundColor 271 border.color: Kube.Colors.viewBackgroundColor