Skip to content

Commit

Permalink
Gui: Command API changes
Browse files Browse the repository at this point in the history
* invoke(), distinguish between command triggering source. Also add
  support of auto transaction using App::AutoTransaction. Now all
  command will support undo/redo by default.

* setupCheckable(), a helper function to Improve support of
  PythonGroupCommand

* getObjectCmd(), helper function to output Python command to refer to
  an object without ambiguity. Because with introduction of external
  linking, an object can no longer be safely referred through the
  current active document.

* Support auto MacroManager command logger. For commands that does not
  log any output to MacroManager, a log entry of 'Gui.runCommand()' will
  be auto generated.

* Support linked object in copyVisual()

* Modified do/runCommand() to print calling file and line number.

* Add various helper macros for run command involving a document or
  object.
  • Loading branch information
realthunder authored and wwmayer committed Aug 17, 2019
1 parent f5d92fd commit b1c0de8
Show file tree
Hide file tree
Showing 8 changed files with 678 additions and 93 deletions.
44 changes: 31 additions & 13 deletions src/Gui/Action.cpp
Expand Up @@ -37,6 +37,7 @@
# include <QToolButton>
#endif

#include <Base/Tools.h>
#include "Action.h"
#include "Application.h"
#include "Command.h"
Expand Down Expand Up @@ -92,19 +93,21 @@ void Action::addTo(QWidget *w)
*/
void Action::onActivated ()
{
_pcCmd->invoke(0);
_pcCmd->invoke(0,Command::TriggerAction);
}

/**
* Sets whether the command is toggled.
*/
void Action::onToggled(bool b)
{
_pcCmd->invoke( b ? 1 : 0 );
_pcCmd->invoke( b ? 1 : 0 , Command::TriggerAction);
}

void Action::setCheckable(bool b)
{
if(b == _action->isCheckable())
return;
_action->setCheckable(b);
if (b) {
disconnect(_action, SIGNAL(triggered(bool)), this, SLOT(onActivated()));
Expand All @@ -116,9 +119,14 @@ void Action::setCheckable(bool b)
}
}

void Action::setChecked(bool b)
void Action::setChecked(bool b, bool no_signal)
{
bool blocked;
if(no_signal)
blocked = _action->blockSignals(true);
_action->setChecked(b);
if(no_signal)
_action->blockSignals(blocked);
}

bool Action::isChecked() const
Expand Down Expand Up @@ -154,6 +162,11 @@ void Action::setIcon (const QIcon & icon)
_action->setIcon(icon);
}

QIcon Action::icon () const
{
return _action->icon();
}

void Action::setStatusTip(const QString & s)
{
_action->setStatusTip(s);
Expand Down Expand Up @@ -206,7 +219,7 @@ void Action::setMenuRole(QAction::MenuRole menuRole)
* to the command object.
*/
ActionGroup::ActionGroup ( Command* pcCmd,QObject * parent)
: Action(pcCmd, parent), _group(0), _dropDown(false)
: Action(pcCmd, parent), _group(0), _dropDown(false),_external(false),_toggle(false)
{
_group = new QActionGroup(this);
connect(_group, SIGNAL(triggered(QAction*)), this, SLOT(onActivated (QAction*)));
Expand Down Expand Up @@ -319,16 +332,13 @@ void ActionGroup::setCheckedAction(int i)
*/
void ActionGroup::onActivated ()
{
_pcCmd->invoke(this->property("defaultAction").toInt());
_pcCmd->invoke(this->property("defaultAction").toInt(), Command::TriggerAction);
}

/**
* Activates the command.
*/
void ActionGroup::onActivated (int index)
void ActionGroup::onToggled(bool)
{
_pcCmd->invoke(index);
}
onActivated();
}

/**
* Activates the command.
Expand All @@ -337,19 +347,27 @@ void ActionGroup::onActivated (QAction* a)
{
int index = _group->actions().indexOf(a);

// Calling QToolButton::setIcon() etc. has no effect if it has QAction set.
// We have to change the QAction icon instead
#if 0
QList<QWidget*> widgets = a->associatedWidgets();
for (QList<QWidget*>::iterator it = widgets.begin(); it != widgets.end(); ++it) {
QMenu* menu = qobject_cast<QMenu*>(*it);
if (menu) {
QToolButton* button = qobject_cast<QToolButton*>(menu->parent());
if (button) {
button->setIcon(a->icon());
button->setText(a->text());
button->setToolTip(a->toolTip());
this->setProperty("defaultAction", QVariant(index));
}
}
}

_pcCmd->invoke(index);
#endif
this->setIcon(a->icon());
this->setToolTip(a->toolTip());
this->setProperty("defaultAction", QVariant(index));
_pcCmd->invoke(index, Command::TriggerChildAction);
}

void ActionGroup::onHovered (QAction *a)
Expand Down
8 changes: 6 additions & 2 deletions src/Gui/Action.h
Expand Up @@ -54,12 +54,13 @@ class GuiExport Action : public QObject
virtual void setVisible(bool);

void setCheckable(bool);
void setChecked (bool);
void setChecked (bool, bool no_signal=false);
bool isChecked() const;

void setShortcut (const QString &);
QKeySequence shortcut() const;
void setIcon (const QIcon &);
QIcon icon() const;
void setStatusTip (const QString &);
QString statusTip() const;
void setText (const QString &);
Expand All @@ -69,6 +70,7 @@ class GuiExport Action : public QObject
void setWhatsThis (const QString &);
QString whatsThis() const;
void setMenuRole(QAction::MenuRole menuRole);
QAction *action() {return _action;};

public Q_SLOTS:
virtual void onActivated ();
Expand Down Expand Up @@ -112,13 +114,15 @@ class GuiExport ActionGroup : public Action

public Q_SLOTS:
void onActivated ();
void onActivated (int);
void onToggled(bool);
void onActivated (QAction*);
void onHovered (QAction*);

protected:
QActionGroup* _group;
bool _dropDown;
bool _external;
bool _toggle;
};

// --------------------------------------------------------------------
Expand Down

0 comments on commit b1c0de8

Please sign in to comment.