From 2d9944bd0b5cd1dd202d9dc6318d612e1aca4241 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 8 Jan 2018 19:34:26 +0100 Subject: Load extensions with a model --- framework/src/CMakeLists.txt | 1 + framework/src/extensionmodel.cpp | 74 +++++++++++++++++++++++++++++++++++++++ framework/src/extensionmodel.h | 52 +++++++++++++++++++++++++++ framework/src/frameworkplugin.cpp | 2 ++ 4 files changed, 129 insertions(+) create mode 100644 framework/src/extensionmodel.cpp create mode 100644 framework/src/extensionmodel.h (limited to 'framework') diff --git a/framework/src/CMakeLists.txt b/framework/src/CMakeLists.txt index 526fbefe..97cb453b 100644 --- a/framework/src/CMakeLists.txt +++ b/framework/src/CMakeLists.txt @@ -47,6 +47,7 @@ add_library(kubeframework SHARED startupcheck.cpp keyring.cpp domainobjectcontroller.cpp + extensionmodel.cpp ) target_link_libraries(kubeframework sink diff --git a/framework/src/extensionmodel.cpp b/framework/src/extensionmodel.cpp new file mode 100644 index 00000000..f64a0e17 --- /dev/null +++ b/framework/src/extensionmodel.cpp @@ -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. +*/ +#include "extensionmodel.h" +#include +#include +#include +#include +#include + +using namespace Kube; + +ExtensionModel::ExtensionModel(QObject *parent) + : QSortFilterProxyModel(parent) +{ + QTimer::singleShot(0, this, &ExtensionModel::load); +} + +QHash ExtensionModel::roleNames() const +{ + return { + {Name, "name"}, + {Tooltip, "tooltip"}, + {Icon, "icon"}, + {Source, "source"} + }; +} + +void ExtensionModel::load() +{ + auto model = new QStandardItemModel(this); + + auto engine = qmlEngine(this); + Q_ASSERT(engine); + for (const auto &path : engine->importPathList()) { + QDir dir{path + "/org/kube/viewextensions"}; + for (const auto &pluginName : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { + auto viewPath = dir.path() + pluginName + "/View.qml"; + qWarning() << "Plugin path: " << dir.path() + pluginName + "/View.qml"; + auto item = new QStandardItem; + item->setData(viewPath, Source); + item->setData(pluginName, Name); + item->setData(pluginName, Tooltip); + item->setData("document-decrypt", Icon); + model->appendRow(item); + } + } + setSourceModel(model); +} + +QVariant ExtensionModel::data(const QModelIndex &idx, int role) const +{ + return QSortFilterProxyModel::data(idx, role); +} + +bool ExtensionModel::lessThan(const QModelIndex &left, const QModelIndex &right) const +{ + return QSortFilterProxyModel::lessThan(left, right); +} diff --git a/framework/src/extensionmodel.h b/framework/src/extensionmodel.h new file mode 100644 index 00000000..6e984005 --- /dev/null +++ b/framework/src/extensionmodel.h @@ -0,0 +1,52 @@ +/* + 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 +#include + +namespace Kube { + +class ExtensionModel : public QSortFilterProxyModel +{ + Q_OBJECT +public: + + ExtensionModel(QObject *parent = Q_NULLPTR); + ~ExtensionModel() = default; + + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; + + bool lessThan(const QModelIndex &left, const QModelIndex &right) const Q_DECL_OVERRIDE; + + enum Roles { + Name = Qt::UserRole + 1, + Tooltip, + Icon, + Source + }; + + QHash roleNames() const Q_DECL_OVERRIDE; + +private slots: + void load(); +}; + +} diff --git a/framework/src/frameworkplugin.cpp b/framework/src/frameworkplugin.cpp index f768be1b..b0e2f9c9 100644 --- a/framework/src/frameworkplugin.cpp +++ b/framework/src/frameworkplugin.cpp @@ -41,6 +41,7 @@ #include "keyring.h" #include "controller.h" #include "domainobjectcontroller.h" +#include "extensionmodel.h" #include #include @@ -130,6 +131,7 @@ void FrameworkPlugin::registerTypes (const char *uri) qmlRegisterType(uri, 1, 0, "AccountFactory"); qmlRegisterType(uri, 1, 0, "AccountsModel"); + qmlRegisterType(uri, 1, 0, "ExtensionModel"); qmlRegisterType(uri, 1, 0, "Settings"); -- cgit v1.2.3