Extend Action/Command framework to add arbitrary widgets

This commit is contained in:
wmayer 2012-11-22 15:17:32 +01:00
parent 5082ae2239
commit 0473003fc9
2 changed files with 32 additions and 1 deletions

View File

@ -31,6 +31,7 @@
# include <QDesktopWidget>
# include <QEvent>
# include <QMessageBox>
# include <QTimer>
# include <QToolBar>
# include <QToolButton>
#endif
@ -55,13 +56,21 @@ using namespace Gui::Dialog;
* Constructs an action called \a name with parent \a parent. It also stores a pointer
* to the command object.
*/
Action::Action (Command* pcCmd,QObject * parent)
Action::Action (Command* pcCmd, QObject * parent)
: QObject(parent), _action(new QAction( this )), _pcCmd(pcCmd)
{
_action->setObjectName(QString::fromAscii(_pcCmd->getName()));
connect(_action, SIGNAL(triggered(bool)), this, SLOT(onActivated()));
}
Action::Action (Command* pcCmd, QAction* action, QObject * parent)
: QObject(parent), _action(action), _pcCmd(pcCmd)
{
_action->setParent(this);
_action->setObjectName(QString::fromAscii(_pcCmd->getName()));
connect(_action, SIGNAL(triggered(bool)), this, SLOT(onActivated()));
}
Action::~Action()
{
delete _action;
@ -257,6 +266,14 @@ void ActionGroup::setVisible( bool b )
_group->setVisible(b);
}
QAction* ActionGroup::addAction(QAction* action)
{
int index = _group->actions().size();
action = _group->addAction(action);
action->setData(QVariant(index));
return action;
}
QAction* ActionGroup::addAction(const QString& text)
{
int index = _group->actions().size();
@ -289,6 +306,14 @@ void ActionGroup::onActivated ()
_pcCmd->invoke(this->property("defaultAction").toInt());
}
/**
* Activates the command.
*/
void ActionGroup::onActivated (int index)
{
_pcCmd->invoke(index);
}
/**
* Activates the command.
*/
@ -410,6 +435,8 @@ void WorkbenchComboBox::onActivated(int i)
int index = itemData(i).toInt();
WorkbenchActionEvent* ev = new WorkbenchActionEvent(this->actions()[index]);
QApplication::postEvent(this->group, ev);
// TODO: Test if we can use this instead
//QTimer::singleShot(20, this->actions()[i], SLOT(trigger()));
}
void WorkbenchComboBox::onActivated(QAction* action)

View File

@ -45,6 +45,8 @@ class GuiExport Action : public QObject
public:
Action (Command* pcCmd, QObject * parent = 0);
/// Action takes ownership of the 'action' object.
Action (Command* pcCmd, QAction* action, QObject * parent);
virtual ~Action();
virtual void addTo (QWidget * w);
@ -100,6 +102,7 @@ public:
void setVisible (bool);
void setDropDownMenu(bool b) { _dropDown = b; }
QAction* addAction(QAction*);
QAction* addAction(const QString&);
QList<QAction*> actions() const;
int checkedAction() const;
@ -107,6 +110,7 @@ public:
public Q_SLOTS:
void onActivated ();
void onActivated (int);
void onActivated (QAction*);
protected: