Add the convenience method for extracting actions from QMenus to AbstractRunner

svn path=/trunk/KDE/kdelibs/; revision=1002128
This commit is contained in:
Ryan P. Bitanga 2009-07-25 04:42:52 +00:00
parent ab6124c2a6
commit 284bac4310
2 changed files with 44 additions and 0 deletions

View File

@ -21,6 +21,7 @@
#include <QAction> #include <QAction>
#include <QHash> #include <QHash>
#include <QMenu>
#include <QMutex> #include <QMutex>
#include <QMutexLocker> #include <QMutexLocker>
#include <QTimer> #include <QTimer>
@ -224,6 +225,37 @@ void AbstractRunner::clearActions()
d->actions.clear(); d->actions.clear();
} }
QList<QAction*> AbstractRunner::actionsFromMenu(QMenu *menu, const QString &prefix)
{
QList<QAction*> 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() bool AbstractRunner::hasRunOptions()
{ {
return d->hasRunOptions; return d->hasRunOptions;

View File

@ -352,6 +352,18 @@ class PLASMA_EXPORT AbstractRunner : public QObject
*/ */
void clearActions(); 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<QAction*> actionsFromMenu(QMenu *menu, const QString &prefix = QString());
/** /**
* Adds a registed syntax that this runner understands. This is used to * 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 * display to the user what this runner can understand and how it can be