From 2085b83960cbb8e4693cf48a5bd265aa946256de Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 3 Aug 2018 13:49:56 +0200 Subject: Weekview with calendar colors --- framework/src/domain/daylongeventmodel.cpp | 13 +++++ framework/src/domain/daylongeventmodel.h | 6 +++ framework/src/domain/perioddayeventmodel.cpp | 15 +++++- framework/src/domain/perioddayeventmodel.h | 6 +++ framework/src/entitycache.h | 77 ++++++++++++++++++++++++++++ views/calendar/qml/DaylongEvents.qml | 6 --- views/calendar/qml/WeekEvents.qml | 6 --- views/calendar/qml/WeekView.qml | 10 ++-- 8 files changed, 120 insertions(+), 19 deletions(-) create mode 100644 framework/src/entitycache.h delete mode 100644 views/calendar/qml/DaylongEvents.qml delete mode 100644 views/calendar/qml/WeekEvents.qml 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 @@ #include #include +#include "entitycache.h" + DayLongEventModel::DayLongEventModel(QObject *parent) : QSortFilterProxyModel(parent) { Sink::Query query; @@ -33,11 +35,14 @@ DayLongEventModel::DayLongEventModel(QObject *parent) : QSortFilterProxyModel(pa query.request(); query.request(); query.request(); + query.request(); query.filter(true); mModel = Sink::Store::loadModel(query); + mCalendarCache = EntityCache::Ptr::create(); + setSourceModel(mModel.data()); } @@ -48,9 +53,15 @@ QHash DayLongEventModel::roleNames() const {Description, "description"}, {StartDate, "starts"}, {Duration, "duration"}, + {Color, "color"}, }; } +QByteArray DayLongEventModel::getColor(const QByteArray &calendar) const +{ + return mCalendarCache->getProperty(calendar, "color").toByteArray(); +} + QVariant DayLongEventModel::data(const QModelIndex &idx, int role) const { auto srcIdx = mapToSource(idx); @@ -71,6 +82,8 @@ QVariant DayLongEventModel::data(const QModelIndex &idx, int role) const } case Duration: return event->getStartTime().date().daysTo(event->getEndTime().date()); + case Color: + return getColor(event->getCalendar()); } 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 @@ #include #include +class EntityCacheInterface; class KUBE_EXPORT DayLongEventModel : public QSortFilterProxyModel { Q_OBJECT @@ -37,6 +38,7 @@ class KUBE_EXPORT DayLongEventModel : public QSortFilterProxyModel public: using Event = Sink::ApplicationDomain::Event; + using Calendar = Sink::ApplicationDomain::Calendar; enum Roles { @@ -44,6 +46,7 @@ public: Description, StartDate, Duration, + Color }; Q_ENUM(Roles); @@ -62,7 +65,10 @@ public: void setPeriodLength(int); private: + QByteArray getColor(const QByteArray &calendar) const; + QSharedPointer mModel; + QSharedPointer mCalendarCache; QDate mPeriodStart; 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 @@ #include #include +#include + PeriodDayEventModel::PeriodDayEventModel(QObject *parent) : QAbstractItemModel(parent), partitionedEvents(7) { @@ -43,6 +45,7 @@ void PeriodDayEventModel::updateQuery() query.request(); query.request(); query.request(); + query.request(); auto periodEnd = mPeriodStart.addDays(mPeriodLength); @@ -59,6 +62,8 @@ void PeriodDayEventModel::updateQuery() QObject::connect(eventModel.data(), &QAbstractItemModel::rowsMoved, this, &PeriodDayEventModel::partitionData); QObject::connect(eventModel.data(), &QAbstractItemModel::rowsRemoved, this, &PeriodDayEventModel::partitionData); + mCalendarCache = EntityCache::Ptr::create(); + partitionData(); } @@ -157,6 +162,11 @@ int PeriodDayEventModel::columnCount(const QModelIndex &parent) const return eventModel->columnCount(); } +QByteArray PeriodDayEventModel::getColor(const QByteArray &calendar) const +{ + return mCalendarCache->getProperty(calendar, "color").toByteArray(); +} + QVariant PeriodDayEventModel::data(const QModelIndex &id, int role) const { if (id.internalId() == DAY_ID) { @@ -184,7 +194,7 @@ QVariant PeriodDayEventModel::data(const QModelIndex &id, int role) const {"description", data(eventId, Description)}, {"starts", startTime.hour() + startTime.minute() / 60.}, {"duration", data(eventId, Duration)}, - {"color", "#134bab"}, + {"color", data(eventId, Color)}, {"indention", 0}, }); } @@ -213,6 +223,8 @@ QVariant PeriodDayEventModel::data(const QModelIndex &id, int role) const auto end = event->getEndTime(); return start.secsTo(end) / 3600; } + case Color: + return getColor(event->getCalendar()); default: SinkWarning() << "Unknown role for event:" << QMetaEnum::fromType().valueToKey(role); return {}; @@ -229,6 +241,7 @@ QHash PeriodDayEventModel::roleNames() const {Description, "description"}, {StartTime, "starts"}, {Duration, "duration"}, + {Color, "color"} }; } 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 @@ // | '--- List of event pointers for that day // '--- Partition / day // + +class EntityCacheInterface; class KUBE_EXPORT PeriodDayEventModel : public QAbstractItemModel { Q_OBJECT @@ -87,6 +89,7 @@ class KUBE_EXPORT PeriodDayEventModel : public QAbstractItemModel public: using Event = Sink::ApplicationDomain::Event; + using Calendar = Sink::ApplicationDomain::Calendar; enum Roles { @@ -96,6 +99,7 @@ public: Description, StartTime, Duration, + Color }; Q_ENUM(Roles); PeriodDayEventModel(QObject *parent = nullptr); @@ -120,6 +124,7 @@ public: private: void updateQuery(); void partitionData(); + QByteArray getColor(const QByteArray &calendar) const; int bucketOf(const QDate &candidate) const; @@ -128,6 +133,7 @@ private: QSharedPointer eventModel; QVector>> partitionedEvents; + QSharedPointer mCalendarCache; static const constexpr quintptr DAY_ID = std::numeric_limits::max(); }; 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 @@ +/* + Copyright (c) 2018 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +#pragma once + +#include "kube_export.h" +#include +#include +#include +#include + +class KUBE_EXPORT EntityCacheInterface +{ +public: + typedef QSharedPointer Ptr; + EntityCacheInterface() = default; + virtual ~EntityCacheInterface() = default; + + virtual QVariant getProperty(const QByteArray &identifier, const QByteArray &property) const = 0; +}; + +template +class KUBE_EXPORT EntityCache : public EntityCacheInterface +{ +public: + typedef QSharedPointer Ptr; + + EntityCache(); + virtual ~EntityCache() = default; + + virtual QVariant getProperty(const QByteArray &, const QByteArray &) const override; + +private: + QHash mCache; + QSharedPointer mModel; +}; + +template +EntityCache::EntityCache() + : EntityCacheInterface() +{ + Sink::Query query; + query.request(); + query.setFlags(Sink::Query::LiveQuery); + mModel = Sink::Store::loadModel(query); + QObject::connect(mModel.data(), &QAbstractItemModel::rowsInserted, mModel.data(), [this] (const QModelIndex &, int start, int end) { + for (int row = start; row <= end; row++) { + auto entity = mModel->index(row, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).template value(); + mCache.insert(entity->identifier(), entity); + } + }); +} + +template +QVariant EntityCache::getProperty(const QByteArray &identifier, const QByteArray &property) const +{ + if (auto entity = mCache.value(identifier)) { + return entity->getProperty(property); + } + return {}; +} + 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 @@ -import QtQuick 2.7 - -import org.kube.framework 1.0 as Kube - -Kube.DayLongEventModel { -} 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 @@ -import QtQuick 2.7 - -import org.kube.framework 1.0 as Kube - -Kube.PeriodDayEventModel { -} 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 { clip: true - model: DaylongEvents { + model: Kube.DayLongEventModel { start: root.startDate length: root.daysToShow } @@ -124,8 +124,7 @@ FocusScope { width: root.dayWidth * model.duration height: parent.height x: root.dayWidth * model.starts - //color: model.color - color: Kube.Colors.jazzberryJam + color: model.color border.width: 1 border.color: Kube.Colors.viewBackgroundColor @@ -204,7 +203,7 @@ FocusScope { //END time labels Repeater { - model: WeekEvents { + model: Kube.PeriodDayEventModel { start: root.startDate length: root.daysToShow } @@ -266,8 +265,7 @@ FocusScope { y: root.hourHeight * model.modelData.starts x: Kube.Units.gridUnit * model.modelData.indention - //color: model.modelData.color - color: Kube.Colors.bewareOrange + color: model.modelData.color opacity: 0.8 border.width: 1 border.color: Kube.Colors.viewBackgroundColor -- cgit v1.2.3