diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-01-08 19:34:26 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-01-08 19:34:26 +0100 |
commit | 2d9944bd0b5cd1dd202d9dc6318d612e1aca4241 (patch) | |
tree | d9fbc3d48937f18c35cbb1000b4310a9d6162800 /framework/src/extensionmodel.cpp | |
parent | d3e1aa3fa1d64360c9dc31edb17072f456ddfba4 (diff) | |
download | kube-2d9944bd0b5cd1dd202d9dc6318d612e1aca4241.tar.gz kube-2d9944bd0b5cd1dd202d9dc6318d612e1aca4241.zip |
Load extensions with a model
Diffstat (limited to 'framework/src/extensionmodel.cpp')
-rw-r--r-- | framework/src/extensionmodel.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
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 @@ | |||
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 | #include "extensionmodel.h" | ||
20 | #include <QStandardItemModel> | ||
21 | #include <QQmlEngine> | ||
22 | #include <QDir> | ||
23 | #include <QDebug> | ||
24 | #include <QTimer> | ||
25 | |||
26 | using namespace Kube; | ||
27 | |||
28 | ExtensionModel::ExtensionModel(QObject *parent) | ||
29 | : QSortFilterProxyModel(parent) | ||
30 | { | ||
31 | QTimer::singleShot(0, this, &ExtensionModel::load); | ||
32 | } | ||
33 | |||
34 | QHash<int, QByteArray> ExtensionModel::roleNames() const | ||
35 | { | ||
36 | return { | ||
37 | {Name, "name"}, | ||
38 | {Tooltip, "tooltip"}, | ||
39 | {Icon, "icon"}, | ||
40 | {Source, "source"} | ||
41 | }; | ||
42 | } | ||
43 | |||
44 | void ExtensionModel::load() | ||
45 | { | ||
46 | auto model = new QStandardItemModel(this); | ||
47 | |||
48 | auto engine = qmlEngine(this); | ||
49 | Q_ASSERT(engine); | ||
50 | for (const auto &path : engine->importPathList()) { | ||
51 | QDir dir{path + "/org/kube/viewextensions"}; | ||
52 | for (const auto &pluginName : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { | ||
53 | auto viewPath = dir.path() + pluginName + "/View.qml"; | ||
54 | qWarning() << "Plugin path: " << dir.path() + pluginName + "/View.qml"; | ||
55 | auto item = new QStandardItem; | ||
56 | item->setData(viewPath, Source); | ||
57 | item->setData(pluginName, Name); | ||
58 | item->setData(pluginName, Tooltip); | ||
59 | item->setData("document-decrypt", Icon); | ||
60 | model->appendRow(item); | ||
61 | } | ||
62 | } | ||
63 | setSourceModel(model); | ||
64 | } | ||
65 | |||
66 | QVariant ExtensionModel::data(const QModelIndex &idx, int role) const | ||
67 | { | ||
68 | return QSortFilterProxyModel::data(idx, role); | ||
69 | } | ||
70 | |||
71 | bool ExtensionModel::lessThan(const QModelIndex &left, const QModelIndex &right) const | ||
72 | { | ||
73 | return QSortFilterProxyModel::lessThan(left, right); | ||
74 | } | ||