Skip to content

Commit

Permalink
+ support of checkable commands
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Aug 25, 2015
1 parent 1e745a6 commit 27dc80c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/Gui/Command.cpp
Expand Up @@ -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",
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
//===========================================================================
Expand Down
2 changes: 2 additions & 0 deletions src/Gui/Command.h
Expand Up @@ -351,6 +351,8 @@ class PythonCommand: public Command
const char* getStatusTip () const;
const char* getPixmap () const;
const char* getAccel () const;
bool isCheckable () const;
bool isChecked () const;
//@}

protected:
Expand Down
12 changes: 12 additions & 0 deletions src/Mod/TemplatePyMod/Commands.py
Expand Up @@ -231,6 +231,17 @@ def GetDefaultCommand(self):
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
#---------------------------------------------------------------------------
Expand All @@ -244,3 +255,4 @@ def GetResources(self):
FreeCADGui.addCommand('TemplatePyGrp_2',TemplatePyGrp_2())
FreeCADGui.addCommand('TemplatePyGrp_3',TemplatePyGrp_3())
FreeCADGui.addCommand('TemplatePyGroup',TemplatePyGroup())
FreeCADGui.addCommand('TemplatePyCheckable',TemplatePyCheckable())

0 comments on commit 27dc80c

Please sign in to comment.