support exclusive action groups in the contextual actions

This commit is contained in:
Marco Martin 2020-10-22 18:46:58 +02:00
parent d578c2a154
commit 4ceb47765f
2 changed files with 47 additions and 0 deletions

View File

@ -8,6 +8,7 @@
#include "appletinterface.h"
#include <QAction>
#include <QActionGroup>
#include <QDir>
#include <QFile>
#include <QIcon>
@ -418,6 +419,25 @@ QString AppletInterface::file(const QString &fileType, const QString &filePath)
return appletScript()->filePath(fileType, filePath);
}
QList<QObject *> AppletInterface::contextualActionsObjects() const
{
QList<QObject *> actions;
Plasma::Applet *a = applet();
if (a->failedToLaunch()) {
return actions;
}
for (const QString &name : qAsConst(m_actions)) {
QAction *action = a->actions()->action(name);
if (action) {
actions << action;
}
}
return actions;
}
QList<QAction *> AppletInterface::contextualActions() const
{
QList<QAction *> actions;
@ -449,9 +469,26 @@ void AppletInterface::setActionSeparator(const QString &name)
action->setSeparator(true);
a->actions()->addAction(name, action);
m_actions.append(name);
Q_EMIT contextualActionsChanged();
}
}
void AppletInterface::setActionGroup(const QString &actionName, const QString &group)
{
Plasma::Applet *a = applet();
QAction *action = a->actions()->action(actionName);
if (!action) {
return;
}
if (!m_actionGroups.contains(group)) {
m_actionGroups[group] = new QActionGroup(this);
}
action->setActionGroup(m_actionGroups[group]);
}
void AppletInterface::setAction(const QString &name, const QString &text, const QString &icon, const QString &shortcut)
{
Plasma::Applet *a = applet();
@ -465,6 +502,7 @@ void AppletInterface::setAction(const QString &name, const QString &text, const
Q_ASSERT(!m_actions.contains(name));
m_actions.append(name);
Q_EMIT contextualActionsChanged();
connect(action, &QAction::triggered, this, [this, name] {
executeAction(name);

View File

@ -21,6 +21,7 @@
#include "declarativeappletscript.h"
class QAction;
class QActionGroup;
class QmlAppletScript;
class QSizeF;
@ -250,6 +251,7 @@ class AppletInterface : public PlasmaQuick::AppletQuickItem
*/
Q_PROPERTY(KPluginMetaData metaData READ metaData CONSTANT)
Q_PROPERTY(QList<QObject *> contextualActions READ contextualActionsObjects NOTIFY contextualActionsChanged)
public:
AppletInterface(DeclarativeAppletScript *script, const QVariantList &args = QVariantList(), QQuickItem *parent = nullptr);
~AppletInterface() override;
@ -258,6 +260,9 @@ public:
DeclarativeAppletScript *appletScript() const;
// This is for QML which only supports QList<QObject *>
QList<QObject *> contextualActionsObjects() const;
QList<QAction *> contextualActions() const;
void executeAction(const QString &name);
@ -272,6 +277,8 @@ public:
Q_INVOKABLE void setConfigurationRequired(bool needsConfiguring, const QString &reason = QString());
Q_INVOKABLE void setActionSeparator(const QString &name);
Q_INVOKABLE void setActionGroup(const QString &action, const QString &group);
/**
* Add an action to the Plasmoid contextual menu.
* When the action is triggered a function called action_<name> will be called, if there is no function with that name actionTriggered(name) will be called instead.
@ -466,6 +473,7 @@ Q_SIGNALS:
void availableScreenRegionChanged();
void availableScreenRectChanged();
void constraintHintsChanged();
void contextualActionsChanged();
void userConfiguringChanged();
void globalShortcutChanged();
@ -496,6 +504,7 @@ private Q_SLOTS:
private:
QStringList m_actions;
QHash<QString, QActionGroup *> m_actionGroups;
KDeclarative::ConfigPropertyMap *m_configuration;
DeclarativeAppletScript *m_appletScriptEngine;