From 52c3a27e183445bccf488c39117c6bdf8cd5f214 Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Tue, 10 Sep 2013 15:51:22 +0200 Subject: [PATCH] support for wallpaper actions --- .../qml/plasmoid/wallpaperinterface.cpp | 75 ++++++++++++++++++- .../qml/plasmoid/wallpaperinterface.h | 16 ++++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/scriptengines/qml/plasmoid/wallpaperinterface.cpp b/src/scriptengines/qml/plasmoid/wallpaperinterface.cpp index b64e16710..b8dc871be 100644 --- a/src/scriptengines/qml/plasmoid/wallpaperinterface.cpp +++ b/src/scriptengines/qml/plasmoid/wallpaperinterface.cpp @@ -23,10 +23,13 @@ #include #include +#include + #include #include #include #include +#include #include #include @@ -36,8 +39,11 @@ WallpaperInterface::WallpaperInterface(ContainmentInterface *parent) m_containmentInterface(parent), m_qmlObject(0), m_configLoader(0), - m_configuration(0) + m_configuration(0), + m_actionSignals(0) { + m_actions = new KActionCollection(this); + if (!m_containmentInterface->containment()->wallpaper().isEmpty()) { syncWallpaperPackage(); } @@ -93,6 +99,7 @@ void WallpaperInterface::syncWallpaperPackage() m_qmlObject->setInitializationDelayed(true); } + m_actions->clear(); m_pkg = Plasma::PluginLoader::self()->loadPackage("Plasma/QmlWallpaper"); m_pkg.setPath(m_wallpaperPlugin); @@ -104,6 +111,7 @@ void WallpaperInterface::syncWallpaperPackage() m_configuration = new ConfigPropertyMap(configScheme(), this); } + m_qmlObject->setSource(QUrl::fromLocalFile(m_pkg.filePath("mainscript"))); m_qmlObject->engine()->rootContext()->setContextProperty("wallpaper", this); m_qmlObject->completeInitialization(); @@ -132,4 +140,69 @@ void WallpaperInterface::syncWallpaperPackage() emit configurationChanged(); } +QList WallpaperInterface::contextualActions() const +{ + return m_actions->actions(); +} + +void WallpaperInterface::setAction(const QString &name, const QString &text, const QString &icon, const QString &shortcut) +{ + QAction *action = m_actions->action(name); + + if (action) { + action->setText(text); + } else { + action = new QAction(text, this); + m_actions->addAction(name, action); + + Q_ASSERT(!m_actions->actions().contains(name)); + m_actions->addAction(name, action); + + if (!m_actionSignals) { + m_actionSignals = new QSignalMapper(this); + connect(m_actionSignals, SIGNAL(mapped(QString)), + this, SLOT(executeAction(QString))); + } + + connect(action, SIGNAL(triggered()), m_actionSignals, SLOT(map())); + m_actionSignals->setMapping(action, name); + } + + if (!icon.isEmpty()) { + action->setIcon(QIcon::fromTheme(icon)); + } + + if (!shortcut.isEmpty()) { + action->setShortcut(shortcut); + } + + action->setObjectName(name); +} + +void WallpaperInterface::removeAction(const QString &name) +{ + QAction *action = m_actions->action(name); + + if (action) { + if (m_actionSignals) { + m_actionSignals->removeMappings(action); + } + m_actions->removeAction(action); + + delete action; + } +} + +QAction *WallpaperInterface::action(QString name) const +{ + return m_actions->action(name); +} + +void WallpaperInterface::executeAction(const QString &name) +{ + if (m_qmlObject->rootObject()) { + QMetaObject::invokeMethod(m_qmlObject->rootObject(), QString("action_" + name).toLatin1(), Qt::DirectConnection); + } +} + #include "moc_wallpaperinterface.cpp" diff --git a/src/scriptengines/qml/plasmoid/wallpaperinterface.h b/src/scriptengines/qml/plasmoid/wallpaperinterface.h index 53f2cc222..9aaa96de3 100644 --- a/src/scriptengines/qml/plasmoid/wallpaperinterface.h +++ b/src/scriptengines/qml/plasmoid/wallpaperinterface.h @@ -28,9 +28,12 @@ namespace Plasma { class ConfigLoader; } +class KActionCollection; + class ContainmentInterface; class ConfigPropertyMap; class QmlObject; +class QSignalMapper; class WallpaperInterface : public QQuickItem { @@ -38,6 +41,7 @@ class WallpaperInterface : public QQuickItem //Q_PROPERTY(QString plugin READ plugin WRITE setPlugin NOTIFY pluginChanged) Q_PROPERTY(ConfigPropertyMap *configuration READ configuration NOTIFY configurationChanged) + Q_PROPERTY(QList contextualActions READ contextualActions) public: WallpaperInterface(ContainmentInterface *parent = 0); @@ -49,12 +53,22 @@ public: Plasma::ConfigLoader *configScheme(); + QList contextualActions() const; + + Q_INVOKABLE void setAction(const QString &name, const QString &text, + const QString &icon = QString(), const QString &shortcut = QString()); + + Q_INVOKABLE void removeAction(const QString &name); + + Q_INVOKABLE QAction *action(QString name) const; + Q_SIGNALS: void packageChanged(); void configurationChanged(); private Q_SLOTS: void syncWallpaperPackage(); + void executeAction(const QString &name); private: QString m_wallpaperPlugin; @@ -63,6 +77,8 @@ private: Plasma::Package m_pkg; ConfigPropertyMap *m_configuration; Plasma::ConfigLoader *m_configLoader; + KActionCollection *m_actions; + QSignalMapper *m_actionSignals; }; #endif