diff --git a/src/Gui/Command.cpp b/src/Gui/Command.cpp index a153a0a2b..483041644 100644 --- a/src/Gui/Command.cpp +++ b/src/Gui/Command.cpp @@ -848,7 +848,12 @@ void PythonCommand::activated(int iMsg) { if (Activation.empty()) { try { - Interpreter().runMethodVoid(_pcPyCommand, "Activated"); + if (isCheckable()) { + Interpreter().runMethod(_pcPyCommand, "Activated", "", 0, "(i)", iMsg); + } + else { + Interpreter().runMethodVoid(_pcPyCommand, "Activated"); + } } catch (const Base::PyException& e) { Base::Console().Error("Running the Python command '%s' failed:\n%s\n%s", @@ -906,14 +911,28 @@ const char* PythonCommand::getHelpUrl(void) const Action * PythonCommand::createAction(void) { + QAction* qtAction = new QAction(0); Action *pcAction; - pcAction = new Action(this,getMainWindow()); + pcAction = new Action(this, qtAction, getMainWindow()); pcAction->setShortcut(QString::fromAscii(getAccel())); applyCommandData(this->getName(), pcAction); if (strcmp(getResource("Pixmap"),"") != 0) pcAction->setIcon(Gui::BitmapFactory().iconFromTheme(getResource("Pixmap"))); + try { + if (isCheckable()) { + pcAction->setCheckable(true); + // Here the QAction must be tmp. blocked to avoid to call the 'activated' method + qtAction->blockSignals(true); + pcAction->setChecked(isChecked()); + qtAction->blockSignals(false); + } + } + catch (const Base::Exception& e) { + Base::Console().Error("%s\n", e.what()); + } + return pcAction; } @@ -951,6 +970,29 @@ const char* PythonCommand::getAccel() const return getResource("Accel"); } +bool PythonCommand::isCheckable() const +{ + PyObject* item = PyDict_GetItemString(_pcPyResourceDict,"Checkable"); + return item ? true : false; +} + +bool PythonCommand::isChecked() const +{ + PyObject* item = PyDict_GetItemString(_pcPyResourceDict,"Checkable"); + if (!item) { + throw Base::Exception("PythonCommand::isChecked(): Method GetResources() of the Python " + "command object doesn't contain the key 'Checkable'"); + } + + if (PyBool_Check(item)) { + return PyObject_IsTrue(item) ? true : false; + } + else { + throw Base::Exception("PythonCommand::isChecked(): Method GetResources() of the Python " + "command object contains the key 'Checkable' which is not a boolean"); + } +} + //=========================================================================== // PythonGroupCommand //=========================================================================== diff --git a/src/Gui/Command.h b/src/Gui/Command.h index e02982428..22dde1b8d 100644 --- a/src/Gui/Command.h +++ b/src/Gui/Command.h @@ -351,6 +351,8 @@ public: const char* getStatusTip () const; const char* getPixmap () const; const char* getAccel () const; + bool isCheckable () const; + bool isChecked () const; //@} protected: diff --git a/src/Mod/TemplatePyMod/Commands.py b/src/Mod/TemplatePyMod/Commands.py index 432ff0756..774665364 100644 --- a/src/Mod/TemplatePyMod/Commands.py +++ b/src/Mod/TemplatePyMod/Commands.py @@ -231,6 +231,17 @@ class TemplatePyGroup: def GetResources(self): return {'Pixmap' : 'python', 'MenuText': 'Group command', 'ToolTip': 'Example group command'} +class TemplatePyCheckable: + "Example toggle command class" + def Activated(self, index): + if index == 0: + print "Toggle is off" + else: + print "Toggle is on" + + def GetResources(self): + return {'Pixmap' : 'python', 'MenuText': 'Toggle command', 'ToolTip': 'Example toggle command', 'Checkable': True} + #--------------------------------------------------------------------------- # Adds the commands to the FreeCAD command manager #--------------------------------------------------------------------------- @@ -244,3 +255,4 @@ FreeCADGui.addCommand('TemplatePyGrp_1',TemplatePyGrp_1()) FreeCADGui.addCommand('TemplatePyGrp_2',TemplatePyGrp_2()) FreeCADGui.addCommand('TemplatePyGrp_3',TemplatePyGrp_3()) FreeCADGui.addCommand('TemplatePyGroup',TemplatePyGroup()) +FreeCADGui.addCommand('TemplatePyCheckable',TemplatePyCheckable())