From 284bac43103f1ff20b991297bca025e3f5bc7fae Mon Sep 17 00:00:00 2001 From: "Ryan P. Bitanga" Date: Sat, 25 Jul 2009 04:42:52 +0000 Subject: [PATCH] Add the convenience method for extracting actions from QMenus to AbstractRunner svn path=/trunk/KDE/kdelibs/; revision=1002128 --- abstractrunner.cpp | 32 ++++++++++++++++++++++++++++++++ abstractrunner.h | 12 ++++++++++++ 2 files changed, 44 insertions(+) diff --git a/abstractrunner.cpp b/abstractrunner.cpp index 46c2cf96b..bf6ff7734 100644 --- a/abstractrunner.cpp +++ b/abstractrunner.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -224,6 +225,37 @@ void AbstractRunner::clearActions() d->actions.clear(); } +QList AbstractRunner::actionsFromMenu(QMenu *menu, const QString &prefix) +{ + QList ret; + foreach (QAction *action, menu->actions()) { + if (QMenu *submenu = action->menu()) { + //Flatten hierarchy and prefix submenu text to all actions in submenu + ret << actionsFromMenu(submenu, action->text()); + } else if (!action->isSeparator() && action->isEnabled()) { + QString text = action->text(); + if (action->isCheckable()) { + if (action->isChecked()) { + text = QString("(%1) %2").arg(QChar(0x2613)).arg(text); + } else { + text = QString("( ) %1").arg(text); + } + } + + if (!prefix.isEmpty()) { + text = QString("%1: %2").arg(prefix).arg(text); + } + text = text.replace(QRegExp("&([\\S])"), "\\1"); + + QAction *a = new QAction(action->icon(), text, this); + + connect(a, SIGNAL(triggered(bool)), action, SIGNAL(triggered(bool))); + ret << a; + } + } + return ret; +} + bool AbstractRunner::hasRunOptions() { return d->hasRunOptions; diff --git a/abstractrunner.h b/abstractrunner.h index a898c5e77..b231ef86e 100644 --- a/abstractrunner.h +++ b/abstractrunner.h @@ -352,6 +352,18 @@ class PLASMA_EXPORT AbstractRunner : public QObject */ void clearActions(); + /** + * Returns a list of all actions in the given QMenu + * This method flattens the hierarchy of the menu by prefixing the + * text of all actions in a submenu with the submenu title. + * + * @param menu the QMenu storing the actions + * @param prefix text to display before the text of all actions in the menu + * + * @since 4.4 + */ + QList actionsFromMenu(QMenu *menu, const QString &prefix = QString()); + /** * Adds a registed syntax that this runner understands. This is used to * display to the user what this runner can understand and how it can be