diff --git a/framework/src/domain/controller.cpp b/framework/src/domain/controller.cpp index 7a2996b8..5e9c43c7 100644 --- a/framework/src/domain/controller.cpp +++ b/framework/src/domain/controller.cpp @@ -1,179 +1,192 @@ /* Copyright (c) 2016 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 "controller.h" #include #include #include #include #include #include using namespace Kube; ControllerState::ControllerState() : QObject() { QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); } ControllerAction::ControllerAction() : ControllerState() { } void ControllerAction::execute() { emit triggered(); } +void Controller::classBegin() +{ +} + +void Controller::componentComplete() +{ + init(); +} + +void Controller::init() +{ +} + void Controller::clear() { auto meta = metaObject(); //We want to get the offset for this class, but clear the properties of all subclasses (thus staticMetaObject for the offset) for (auto i = staticMetaObject.propertyOffset(); i < meta->propertyCount(); i++) { auto property = meta->property(i); setProperty(property.name(), QVariant()); } for (const auto &p : dynamicPropertyNames()) { setProperty(p, QVariant()); } emit cleared(); } void Controller::run(const KAsync::Job &job) { auto jobToExec = job; jobToExec.onError([] (const KAsync::Error &error) { SinkWarningCtx(Sink::Log::Context{"controller"}) << "Error while executing job: " << error.errorMessage; }); //TODO handle error //TODO attach a log context to the execution that we can gather from the job? jobToExec.exec(); } static void traverse(const QStandardItemModel *model, const std::function &f) { auto root = model->invisibleRootItem(); for (int row = 0; row < root->rowCount(); row++) { if (!f(root->child(row, 0))) { return; } } } ListPropertyController::ListPropertyController(const QStringList &roles) : QObject(), mModel(new QStandardItemModel) { //Generate a set of roles for the names. We're not using any enum, so the actual role value doesn't matter. int role = Qt::UserRole + 1; mRoles.insert("id", role); role++; for (const auto &r : roles) { mRoles.insert(r, role); role++; } QHash roleNames; for (const auto r : mRoles.keys()) { roleNames.insert(mRoles[r], r.toLatin1()); } mModel->setItemRoleNames(roleNames); } void ListPropertyController::add(const QVariantMap &value) { auto item = new QStandardItem; auto id = QUuid::createUuid().toByteArray(); item->setData(id, mRoles["id"]); for (const auto &k : value.keys()) { item->setData(value.value(k), mRoles[k]); } mModel->appendRow(QList() << item); if (mModel->rowCount() <= 1) { emit emptyChanged(); } emit added(id, value); } void ListPropertyController::remove(const QByteArray &id) { auto root = mModel->invisibleRootItem(); const auto idRole = mRoles["id"]; for (int row = 0; row < root->rowCount(); row++) { if (root->child(row, 0)->data(idRole).toByteArray() == id) { root->removeRow(row); break; } } emit removed(id); if (mModel->rowCount() <= 0) { emit emptyChanged(); } } bool ListPropertyController::empty() const { return mModel->rowCount() == 0; } void ListPropertyController::clear() { mModel->clear(); } QAbstractItemModel *ListPropertyController::model() { QQmlEngine::setObjectOwnership(mModel.data(), QQmlEngine::CppOwnership); return mModel.data(); } void ListPropertyController::setValue(const QByteArray &id, const QString &key, const QVariant &value) { setValues(id, {{key, value}}); } void ListPropertyController::setValues(const QByteArray &id, const QVariantMap &values) { const auto idRole = mRoles["id"]; ::traverse(mModel.data(), [&] (QStandardItem *item) { if (item->data(idRole).toByteArray() == id) { for (const auto &key : values.keys()) { item->setData(values.value(key), mRoles[key]); } return false; } return true; }); } void ListPropertyController::traverse(const std::function &f) { ::traverse(mModel.data(), [&] (QStandardItem *item) { QVariantMap map; for (const auto &key : mRoles.keys()) { map.insert(key, item->data(mRoles[key])); } f(map); return true; }); } diff --git a/framework/src/domain/controller.h b/framework/src/domain/controller.h index f0bdbba8..062a6a1d 100644 --- a/framework/src/domain/controller.h +++ b/framework/src/domain/controller.h @@ -1,151 +1,159 @@ /* Copyright (c) 2016 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 +#include #include #define KUBE_CONTROLLER_PROPERTY(TYPE, NAME, LOWERCASENAME) \ public: Q_PROPERTY(TYPE LOWERCASENAME MEMBER m##NAME NOTIFY LOWERCASENAME##Changed) \ Q_SIGNALS: void LOWERCASENAME##Changed(); \ private: TYPE m##NAME; \ public: \ struct NAME { \ static constexpr const char *name = #LOWERCASENAME; \ typedef TYPE Type; \ }; \ void set##NAME(const TYPE &value) { setProperty(NAME::name, QVariant::fromValue(value)); } \ void clear##NAME() { setProperty(NAME::name, QVariant{}); } \ TYPE get##NAME() const { return m##NAME; } \ #define KUBE_CONTROLLER_ACTION(NAME) \ Q_PROPERTY (Kube::ControllerAction* NAME##Action READ NAME##Action CONSTANT) \ private: QScopedPointer action_##NAME; \ public: Kube::ControllerAction* NAME##Action() const { Q_ASSERT(action_##NAME); return action_##NAME.data(); } \ private slots: void NAME(); \ #define KUBE_CONTROLLER_LISTCONTROLLER(NAME) \ Q_PROPERTY (Kube::ListPropertyController* NAME READ NAME##Controller CONSTANT) \ private: QScopedPointer controller_##NAME; \ public: Kube::ListPropertyController* NAME##Controller() const { Q_ASSERT(controller_##NAME); return controller_##NAME.data(); } \ namespace Kube { class ControllerState : public QObject { Q_OBJECT Q_PROPERTY(bool enabled MEMBER mEnabled NOTIFY enabledChanged) public: ControllerState(); ~ControllerState() = default; void setEnabled(bool enabled) { setProperty("enabled", enabled); } signals: void enabledChanged(); private: bool mEnabled = false; }; class KUBE_EXPORT ControllerAction : public ControllerState { Q_OBJECT public: ControllerAction(); template ControllerAction(const typename QtPrivate::FunctionPointer::Object *obj, Func slot) : ControllerAction() { QObject::connect(this, &ControllerAction::triggered, obj, slot); } ~ControllerAction() = default; Q_INVOKABLE void execute(); signals: void triggered(); }; -class Controller : public QObject { +class Controller : public QObject, public QQmlParserStatus { Q_OBJECT + Q_INTERFACES(QQmlParserStatus) + public: Controller() = default; virtual ~Controller() = default; + virtual void init(); + + void classBegin() override; + void componentComplete() override; + public slots: virtual void clear(); signals: void done(); void error(); void cleared(); protected: void run(const KAsync::Job &job); }; class KUBE_EXPORT ListPropertyController : public QObject { Q_OBJECT Q_PROPERTY(QAbstractItemModel* model READ model CONSTANT) Q_PROPERTY(bool empty READ empty NOTIFY emptyChanged) public: ListPropertyController(const QStringList &roles); Q_INVOKABLE virtual void add(const QVariantMap &value); Q_INVOKABLE virtual void remove(const QByteArray &id); Q_INVOKABLE void clear(); QAbstractItemModel *model(); void setValue(const QByteArray &id, const QString &key, const QVariant &); void setValues(const QByteArray &id, const QVariantMap &values); void traverse(const std::function &f); template QList getList(const QString &property) { QList list; traverse([&] (const QVariantMap &map) { list << map[property].value(); }); return list; } bool empty() const; Q_SIGNALS: void added(QByteArray, QVariantMap); void removed(QByteArray); void emptyChanged(); protected: QScopedPointer mModel; private: QHash mRoles; }; }