diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-01-09 09:35:59 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-01-10 12:42:13 +0100 |
commit | 6d726bb10386b3d7f5481d41b735ec06cb2163ad (patch) | |
tree | 4d591b67b54c5a83f9f1d718a4576c8ccf05859b /framework/src/extensionmodel.cpp | |
parent | 2d9944bd0b5cd1dd202d9dc6318d612e1aca4241 (diff) | |
download | kube-6d726bb10386b3d7f5481d41b735ec06cb2163ad.tar.gz kube-6d726bb10386b3d7f5481d41b735ec06cb2163ad.zip |
Install composer/converations/people as separate views and load them
dynamically.
Diffstat (limited to 'framework/src/extensionmodel.cpp')
-rw-r--r-- | framework/src/extensionmodel.cpp | 63 |
1 files changed, 55 insertions, 8 deletions
diff --git a/framework/src/extensionmodel.cpp b/framework/src/extensionmodel.cpp index f64a0e17..e3fab7d8 100644 --- a/framework/src/extensionmodel.cpp +++ b/framework/src/extensionmodel.cpp | |||
@@ -22,12 +22,17 @@ | |||
22 | #include <QDir> | 22 | #include <QDir> |
23 | #include <QDebug> | 23 | #include <QDebug> |
24 | #include <QTimer> | 24 | #include <QTimer> |
25 | #include <QJsonDocument> | ||
26 | #include <QJsonObject> | ||
25 | 27 | ||
26 | using namespace Kube; | 28 | using namespace Kube; |
27 | 29 | ||
28 | ExtensionModel::ExtensionModel(QObject *parent) | 30 | ExtensionModel::ExtensionModel(QObject *parent) |
29 | : QSortFilterProxyModel(parent) | 31 | : QSortFilterProxyModel(parent) |
30 | { | 32 | { |
33 | setDynamicSortFilter(true); | ||
34 | sort(0, Qt::DescendingOrder); | ||
35 | setFilterCaseSensitivity(Qt::CaseInsensitive); | ||
31 | QTimer::singleShot(0, this, &ExtensionModel::load); | 36 | QTimer::singleShot(0, this, &ExtensionModel::load); |
32 | } | 37 | } |
33 | 38 | ||
@@ -36,8 +41,7 @@ QHash<int, QByteArray> ExtensionModel::roleNames() const | |||
36 | return { | 41 | return { |
37 | {Name, "name"}, | 42 | {Name, "name"}, |
38 | {Tooltip, "tooltip"}, | 43 | {Tooltip, "tooltip"}, |
39 | {Icon, "icon"}, | 44 | {Icon, "icon"} |
40 | {Source, "source"} | ||
41 | }; | 45 | }; |
42 | } | 46 | } |
43 | 47 | ||
@@ -48,21 +52,55 @@ void ExtensionModel::load() | |||
48 | auto engine = qmlEngine(this); | 52 | auto engine = qmlEngine(this); |
49 | Q_ASSERT(engine); | 53 | Q_ASSERT(engine); |
50 | for (const auto &path : engine->importPathList()) { | 54 | for (const auto &path : engine->importPathList()) { |
51 | QDir dir{path + "/org/kube/viewextensions"}; | 55 | QDir dir{path + "/org/kube/views"}; |
52 | for (const auto &pluginName : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { | 56 | for (const auto &pluginName : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { |
53 | auto viewPath = dir.path() + pluginName + "/View.qml"; | 57 | const auto pluginPath = dir.path() + "/" + pluginName; |
54 | qWarning() << "Plugin path: " << dir.path() + pluginName + "/View.qml"; | 58 | mPaths.insert(pluginName, pluginPath); |
55 | auto item = new QStandardItem; | 59 | auto item = new QStandardItem; |
56 | item->setData(viewPath, Source); | ||
57 | item->setData(pluginName, Name); | 60 | item->setData(pluginName, Name); |
58 | item->setData(pluginName, Tooltip); | 61 | item->setData(pluginName, Tooltip); |
59 | item->setData("document-decrypt", Icon); | 62 | item->setData("kdocumentinfo-inverted", Icon); |
63 | |||
64 | if (QFileInfo::exists(pluginPath + "/metadata.json")) { | ||
65 | QFile file{pluginPath + "/metadata.json"}; | ||
66 | file.open(QIODevice::ReadOnly); | ||
67 | auto json = QJsonDocument::fromJson(file.readAll()); | ||
68 | auto map = json.object().toVariantMap(); | ||
69 | item->setData(map.value("icon").toString(), Icon); | ||
70 | item->setData(map.value("tooltip").toString(), Tooltip); | ||
71 | if (map.value("hidden", false).toBool()) { | ||
72 | delete item; | ||
73 | continue; | ||
74 | } | ||
75 | } | ||
76 | |||
60 | model->appendRow(item); | 77 | model->appendRow(item); |
61 | } | 78 | } |
62 | } | 79 | } |
63 | setSourceModel(model); | 80 | setSourceModel(model); |
64 | } | 81 | } |
65 | 82 | ||
83 | QString ExtensionModel::findSource(const QString &extensionName, const QString &sourceName) | ||
84 | { | ||
85 | if (mPaths.isEmpty()) { | ||
86 | load(); | ||
87 | } | ||
88 | return mPaths.value(extensionName) + "/" + sourceName; | ||
89 | } | ||
90 | |||
91 | void ExtensionModel::setSortOrder(const QVariantList &order) | ||
92 | { | ||
93 | mSortOrder.clear(); | ||
94 | for (const auto &e : order) { | ||
95 | mSortOrder << e.toString(); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | QVariantList ExtensionModel::sortOrder() const | ||
100 | { | ||
101 | return {}; | ||
102 | } | ||
103 | |||
66 | QVariant ExtensionModel::data(const QModelIndex &idx, int role) const | 104 | QVariant ExtensionModel::data(const QModelIndex &idx, int role) const |
67 | { | 105 | { |
68 | return QSortFilterProxyModel::data(idx, role); | 106 | return QSortFilterProxyModel::data(idx, role); |
@@ -70,5 +108,14 @@ QVariant ExtensionModel::data(const QModelIndex &idx, int role) const | |||
70 | 108 | ||
71 | bool ExtensionModel::lessThan(const QModelIndex &left, const QModelIndex &right) const | 109 | bool ExtensionModel::lessThan(const QModelIndex &left, const QModelIndex &right) const |
72 | { | 110 | { |
73 | return QSortFilterProxyModel::lessThan(left, right); | 111 | auto leftIndex = mSortOrder.indexOf(left.data(Name).toString()); |
112 | auto rightIndex = mSortOrder.indexOf(right.data(Name).toString()); | ||
113 | if (leftIndex >= 0 && rightIndex >= 0) { | ||
114 | //Higher index is less than | ||
115 | return leftIndex > rightIndex; | ||
116 | } | ||
117 | if (leftIndex < 0 && rightIndex < 0) { | ||
118 | return QSortFilterProxyModel::lessThan(left, right); | ||
119 | } | ||
120 | return leftIndex < rightIndex; | ||
74 | } | 121 | } |