QML appletinterface: add support for actionTrigered(name) method so we don't have to create an action_name method for each contextmenu action.

Signed-off-by: Davide Bettio <bettio@kde.org>
This commit is contained in:
Davide Bettio 2014-04-22 20:08:20 +02:00
parent 1d5c6e5964
commit e1a2555278
No known key found for this signature in database
GPG Key ID: 89B727A19263B1C8
2 changed files with 15 additions and 1 deletions

View File

@ -501,7 +501,13 @@ QStringList AppletInterface::downloadedFiles() const
void AppletInterface::executeAction(const QString &name)
{
if (qmlObject()->rootObject()) {
QMetaObject::invokeMethod(qmlObject()->rootObject(), QString("action_" + name).toLatin1(), Qt::DirectConnection);
const QMetaObject *metaObj = qmlObject()->rootObject()->metaObject();
QString actionMethodName = QString("action_" + name);
if (metaObj->indexOfMethod(QMetaObject::normalizedSignature((actionMethodName + "()").toLatin1())) != -1){
QMetaObject::invokeMethod(qmlObject()->rootObject(), actionMethodName.toLatin1(), Qt::DirectConnection);
}else{
QMetaObject::invokeMethod(qmlObject()->rootObject(), "actionTriggered", Qt::DirectConnection, Q_ARG(QVariant, name));
}
}
}

View File

@ -189,6 +189,14 @@ public:
Q_INVOKABLE void setConfigurationRequired(bool needsConfiguring, const QString &reason = QString());
Q_INVOKABLE void setActionSeparator(const QString &name);
/**
* 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.
* @param: action name
* @text: user visible displayed text
* @icon: user visible optional displayed icon
* @shortcut: shortcut to trigger this action
*/
Q_INVOKABLE void setAction(const QString &name, const QString &text,
const QString &icon = QString(), const QString &shortcut = QString());