+ support of checkable commands

This commit is contained in:
wmayer 2015-08-25 18:01:37 +02:00
parent 1e745a6a6f
commit 27dc80c846
3 changed files with 58 additions and 2 deletions

View File

@ -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
//===========================================================================

View File

@ -351,6 +351,8 @@ public:
const char* getStatusTip () const;
const char* getPixmap () const;
const char* getAccel () const;
bool isCheckable () const;
bool isChecked () const;
//@}
protected:

View File

@ -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())