From 3a4adb790b4d3bef746f5ad88ef073ef821053fa Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 1 Aug 2018 17:33:03 +0200 Subject: Query for the list of calendars --- framework/src/CMakeLists.txt | 1 + framework/src/entitymodel.cpp | 142 ++++++++++++++++++++++++++++++++++++++ framework/src/entitymodel.h | 74 ++++++++++++++++++++ framework/src/frameworkplugin.cpp | 2 + views/calendar/qml/View.qml | 7 +- 5 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 framework/src/entitymodel.cpp create mode 100644 framework/src/entitymodel.h diff --git a/framework/src/CMakeLists.txt b/framework/src/CMakeLists.txt index 46ec3105..86bbf598 100644 --- a/framework/src/CMakeLists.txt +++ b/framework/src/CMakeLists.txt @@ -54,6 +54,7 @@ add_library(kubeframework SHARED viewhighlighter.cpp file.cpp logmodel.cpp + entitymodel.cpp ) generate_export_header(kubeframework BASE_NAME Kube EXPORT_FILE_NAME kube_export.h) set_target_properties(kubeframework PROPERTIES diff --git a/framework/src/entitymodel.cpp b/framework/src/entitymodel.cpp new file mode 100644 index 00000000..5d8ac83e --- /dev/null +++ b/framework/src/entitymodel.cpp @@ -0,0 +1,142 @@ +/* + 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. +*/ + +#include "entitymodel.h" + +#include +#include + +using namespace Sink; +using namespace Sink::ApplicationDomain; + +EntityModel::EntityModel(QObject *parent) : QSortFilterProxyModel(parent) +{ + setDynamicSortFilter(true); + sort(0, Qt::AscendingOrder); +} + +EntityModel::~EntityModel() +{ + +} + +QHash< int, QByteArray > EntityModel::roleNames() const +{ + return mRoleNames; +} + +QVariant EntityModel::data(const QModelIndex &idx, int role) const +{ + auto srcIdx = mapToSource(idx); + auto entity = srcIdx.data(Sink::Store::DomainObjectBaseRole).value(); + + const auto roleName = mRoleNames.value(role); + qWarning() << "Fetch data" << idx << role << roleName; + if (roleName == "identifier") { + return entity->identifier(); + } else if (roleName == "object") { + return QVariant::fromValue(entity); + } else { + return entity->getProperty(roleName); + } +} + +void EntityModel::runQuery(const Query &query) +{ + if (mType == "calendar") { + mModel = Store::loadModel(query); + } else { + qWarning() << "Type not supported " << mType; + } + setSourceModel(mModel.data()); +} + +void EntityModel::updateQuery() +{ + if (mType.isEmpty() || mRoles.isEmpty()) { + return; + } + + Query query; + if (!mAccountId.isEmpty()) { + query.resourceFilter(mAccountId.toUtf8()); + } + query.setFlags(Sink::Query::LiveQuery | Sink::Query::UpdateStatus); + + for (const auto &property: mRoles.keys()) { + query.requestedProperties << property; + } + runQuery(query); +} + +void EntityModel::setAccountId(const QString &accountId) +{ + + //Get all folders of an account + mAccountId = accountId; + updateQuery(); +} + +QString EntityModel::accountId() const +{ + return {}; +} + +void EntityModel::setType(const QString &type) +{ + mType = type; + updateQuery(); +} + +QString EntityModel::type() const +{ + return {}; +} + +void EntityModel::setRoles(const QStringList &roles) +{ + mRoleNames.clear(); + int role = Qt::UserRole + 1; + mRoleNames.insert(role++, "identifier"); + mRoleNames.insert(role++, "object"); + for (int i = 0; i < roles.size(); i++) { + mRoleNames.insert(role++, roles.at(i).toLatin1()); + } + mRoles.clear(); + for (const auto &r : mRoleNames.keys()) { + mRoles.insert(mRoleNames.value(r), r); + } + updateQuery(); +} + +QStringList EntityModel::roles() const +{ + // return mRoleNames.values(); + return {}; +} + +void EntityModel::setFilter(const QVariantMap &) +{ + //TODO +} + +QVariantMap EntityModel::filter() const +{ + return {}; +} diff --git a/framework/src/entitymodel.h b/framework/src/entitymodel.h new file mode 100644 index 00000000..b8a0417e --- /dev/null +++ b/framework/src/entitymodel.h @@ -0,0 +1,74 @@ +/* + 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 + +namespace Sink { + class Query; +} + +class KUBE_EXPORT EntityModel : public QSortFilterProxyModel +{ + Q_OBJECT + + Q_PROPERTY (QString accountId READ accountId WRITE setAccountId) + Q_PROPERTY (QString type READ type WRITE setType) + Q_PROPERTY (QStringList roles READ roles WRITE setRoles) + Q_PROPERTY (QVariantMap filter READ filter WRITE setFilter) + +public: + enum Status { + NoStatus, + InProgressStatus, + ErrorStatus, + SuccessStatus, + }; + Q_ENUMS(Status) + + EntityModel(QObject *parent = Q_NULLPTR); + ~EntityModel(); + + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; + + QHash roleNames() const Q_DECL_OVERRIDE; + + void setAccountId(const QString &); + QString accountId() const; + + void setType(const QString &); + QString type() const; + + void setRoles(const QStringList &); + QStringList roles() const; + + void setFilter(const QVariantMap &); + QVariantMap filter() const; + +private: + void runQuery(const Sink::Query &query); + void updateQuery(); + QSharedPointer mModel; + QHash mRoleNames; + QHash mRoles; + QString mAccountId; + QString mType; +}; diff --git a/framework/src/frameworkplugin.cpp b/framework/src/frameworkplugin.cpp index 72e9730b..5fc4eb27 100644 --- a/framework/src/frameworkplugin.cpp +++ b/framework/src/frameworkplugin.cpp @@ -46,6 +46,7 @@ #include "viewhighlighter.h" #include "file.h" #include "logmodel.h" +#include "entitymodel.h" #include #include @@ -172,6 +173,7 @@ void FrameworkPlugin::registerTypes (const char *uri) qmlRegisterType(uri, 1, 0,"PeopleModel"); qmlRegisterType(uri, 1, 0, "TextDocumentHandler"); qmlRegisterType(uri, 1, 0, "LogModel"); + qmlRegisterType(uri, 1, 0, "EntityModel"); qmlRegisterType(uri, 1, 0, "AccountFactory"); qmlRegisterType(uri, 1, 0, "AccountsModel"); diff --git a/views/calendar/qml/View.qml b/views/calendar/qml/View.qml index 8a166c47..505c6754 100644 --- a/views/calendar/qml/View.qml +++ b/views/calendar/qml/View.qml @@ -95,14 +95,17 @@ RowLayout { spacing: Kube.Units.smallSpacing Repeater { - model: ["calendar_1","calendar_2","calendar_3"] + model: Kube.EntityModel { + type: "calendar" + roles: ["name"] + } delegate: Row { spacing: Kube.Units.smallSpacing Kube.CheckBox { opacity: 0.9 } Kube.Label { - text: modelData + text: model.name color: Kube.Colors.highlightedTextColor } } -- cgit v1.2.3