capability to load QML kcms in plasmoid config windows

With the same mechanism as plasma 1, add the possibility to load kcms directly
 in the plasmoids config windows. this works only for qml based ones.
the apply enable and trigger is correctly wired between the config dialog
and kcm apis.

Change-Id: I66b5ab2253dcbb5f223b5dcf5616efc9d5aab274
REVIEW:123673
This commit is contained in:
Marco Martin 2015-05-08 20:35:45 +02:00
parent ec6afe2a8a
commit f1c0a9a06b
4 changed files with 46 additions and 2 deletions

View File

@ -64,6 +64,9 @@ Type=QString
[PropertyDef::X-Plasma-Provides]
Type=QStringList
[PropertyDef::X-Plasma-ConfigPlugins]
Type=QStringList
[PropertyDef::X-Plasma-StandAloneApp]
Type=bool

View File

@ -35,6 +35,7 @@
#include <klocalizedstring.h>
#include <kdeclarative/kdeclarative.h>
#include <KQuickAddons/ConfigModule>
#include <Plasma/Corona>
#include <Plasma/PluginLoader>
@ -53,6 +54,7 @@ public:
ConfigModel *q;
QList<ConfigCategory *> categories;
QWeakPointer<Plasma::Applet> appletInterface;
QHash<QString, KQuickAddons::ConfigModule *> kcms;
void appendCategory(ConfigCategory *c);
void clear();
@ -153,6 +155,7 @@ QVariant ConfigModelPrivate::get(int row) const
value["source"] = categories.at(row)->source();
}
value["visible"] = categories.at(row)->visible();
value["kcm"] = q->data(q->index(row, 0), ConfigModel::KCMRole);
return value;
}
@ -167,6 +170,7 @@ ConfigModel::ConfigModel(QObject *parent)
roleNames[SourceRole] = "source";
roleNames[PluginNameRole] = "pluginName";
roleNames[VisibleRole] = "visible";
roleNames[KCMRole] = "kcm";
setRoleNames(roleNames);
}
@ -204,6 +208,25 @@ QVariant ConfigModel::data(const QModelIndex &index, int role) const
return d->categories.at(index.row())->pluginName();
case VisibleRole:
return d->categories.at(index.row())->visible();
case KCMRole: {
const QString pluginName = d->categories.at(index.row())->pluginName();
if (d->kcms.contains(pluginName)) {
return QVariant::fromValue(d->kcms.value(pluginName));
}
KPluginLoader loader(KPluginLoader::findPlugin(pluginName));
KPluginFactory* factory = loader.factory();
if (!factory) {
qWarning() << "Error loading KCM:" << loader.errorString();
} else {
KQuickAddons::ConfigModule *cm = factory->create<KQuickAddons::ConfigModule >(const_cast<ConfigModel *>(this));
if (!cm) {
qWarning() << "Error creating KCM object from plugin" << loader.fileName();
}
d->kcms[pluginName] = cm;
return QVariant::fromValue(cm);
}
}
default:
return QVariant();
}

View File

@ -70,7 +70,8 @@ public:
IconRole,
SourceRole,
PluginNameRole,
VisibleRole
VisibleRole,
KCMRole
};
ConfigModel(QObject *parent = 0);
~ConfigModel();

View File

@ -35,6 +35,7 @@
#include <klocalizedstring.h>
#include <kdeclarative/kdeclarative.h>
#include <packageurlinterceptor.h>
#include <KQuickAddons/ConfigModule>
#include <Plasma/Corona>
#include <Plasma/PluginLoader>
@ -64,6 +65,7 @@ public:
ConfigView *q;
QWeakPointer <Plasma::Applet> applet;
ConfigModel *configModel;
ConfigModel *kcmConfigModel;
Plasma::Corona *corona;
//Attached Layout property of mainItem, if any
@ -126,12 +128,27 @@ void ConfigViewPrivate::init()
QQmlComponent *component = new QQmlComponent(q->engine(), QUrl::fromLocalFile(applet.data()->package().filePath("configmodel")), q);
QObject *object = component->beginCreate(q->engine()->rootContext());
configModel = qobject_cast<ConfigModel *>(object);
if (configModel) {
configModel->setApplet(applet.data());
} else {
delete object;
}
const QStringList kcms = applet.data()->pluginInfo().property("X-Plasma-ConfigPlugins").value<QStringList>();
if (!kcms.isEmpty()) {
if (!configModel) {
configModel = new ConfigModel(q);
}
foreach (const QString &kcm, kcms) {
KPluginLoader loader(KPluginLoader::findPlugin(QLatin1String("kcms/") + kcm));
KPluginMetaData md(loader.fileName());
configModel->appendCategory(md.iconName(), md.name(), QString(), loader.fileName());
}
}
q->engine()->rootContext()->setContextProperty("plasmoid", applet.data()->property("_plasma_graphicObject").value<QObject *>());
q->engine()->rootContext()->setContextProperty("configDialog", q);
component->completeCreate();