| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2023 Mario Passaglia <mpassaglia[at]cbc.uba.ar> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
**************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#include <Base/PythonTypeExt.h> |
| 26 |
|
| 27 |
#include "Command.h" |
| 28 |
#include "Action.h" |
| 29 |
#include "PythonWrapper.h" |
| 30 |
|
| 31 |
#include "CommandActionPy.h" |
| 32 |
#include "CommandPy.h" |
| 33 |
|
| 34 |
|
| 35 |
using namespace Gui; |
| 36 |
|
| 37 |
CommandActionPy::CommandActionPy(Py::PythonClassInstance* self, Py::Tuple& args, Py::Dict& kwds) |
| 38 |
: Py::PythonClass<CommandActionPy>::PythonClass(self, args, kwds) |
| 39 |
{ |
| 40 |
const char* name; |
| 41 |
if (!PyArg_ParseTuple(args.ptr(), "s", &name)) { |
| 42 |
throw Py::Exception(); |
| 43 |
} |
| 44 |
|
| 45 |
cmdName = name; |
| 46 |
cmd = Application::Instance->commandManager().getCommandByName(name); |
| 47 |
} |
| 48 |
|
| 49 |
CommandActionPy::~CommandActionPy() = default; |
| 50 |
|
| 51 |
Py::Object CommandActionPy::getAction() |
| 52 |
{ |
| 53 |
if (!cmd) { |
| 54 |
cmd = Application::Instance->commandManager().getCommandByName(cmdName.c_str()); |
| 55 |
} |
| 56 |
|
| 57 |
Action* action = cmd ? cmd->getAction() : nullptr; |
| 58 |
if (action) { |
| 59 |
PythonWrapper wrap; |
| 60 |
wrap.loadWidgetsModule(); |
| 61 |
|
| 62 |
return wrap.fromQAction(action->action()); |
| 63 |
} |
| 64 |
else { |
| 65 |
return Py::None(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
Py::Object CommandActionPy::getCommand() |
| 70 |
{ |
| 71 |
if (!cmd) { |
| 72 |
cmd = Application::Instance->commandManager().getCommandByName(cmdName.c_str()); |
| 73 |
} |
| 74 |
|
| 75 |
if (cmd) { |
| 76 |
auto cmdPy = new CommandPy(cmd); |
| 77 |
return Py::asObject(cmdPy); |
| 78 |
} |
| 79 |
|
| 80 |
return Py::None(); |
| 81 |
} |
| 82 |
PYCXX_NOARGS_METHOD_DECL(CommandActionPy, getCommand) |
| 83 |
|
| 84 |
void CommandActionPy::init_type() |
| 85 |
{ |
| 86 |
Base::PythonTypeExt ext(behaviors()); |
| 87 |
|
| 88 |
behaviors().name("Gui.CommandAction"); |
| 89 |
behaviors().doc("Descriptor to access the action of the commands"); |
| 90 |
behaviors().supportRepr(); |
| 91 |
behaviors().supportGetattro(); |
| 92 |
behaviors().supportSetattro(); |
| 93 |
ext.set_tp_descr_get(&CommandActionPy::descriptorGetter); |
| 94 |
ext.set_tp_descr_set(&CommandActionPy::descriptorSetter); |
| 95 |
PYCXX_ADD_NOARGS_METHOD(getCommand, getCommand, "Descriptor associated command"); |
| 96 |
|
| 97 |
behaviors().readyType(); |
| 98 |
} |
| 99 |
|
| 100 |
PyObject* CommandActionPy::descriptorGetter(PyObject* self, PyObject* /*obj*/, PyObject* /*type*/) |
| 101 |
{ |
| 102 |
auto cmdAction = Py::PythonClassObject<CommandActionPy>(self).getCxxObject(); |
| 103 |
|
| 104 |
return Py::new_reference_to(cmdAction->getAction()); |
| 105 |
} |
| 106 |
|
| 107 |
int CommandActionPy::descriptorSetter(PyObject* /*self*/, PyObject* /*obj*/, PyObject* value) |
| 108 |
{ |
| 109 |
if (value) { |
| 110 |
PyErr_SetString(PyExc_AttributeError, "Can't overwrite command action"); |
| 111 |
} |
| 112 |
else { |
| 113 |
PyErr_SetString(PyExc_AttributeError, "Can't delete command action"); |
| 114 |
} |
| 115 |
|
| 116 |
return -1; |
| 117 |
} |
| 118 |
|
| 119 |
Py::Object CommandActionPy::repr() |
| 120 |
{ |
| 121 |
std::stringstream s; |
| 122 |
s << this->cmdName << " command action descriptor"; |
| 123 |
|
| 124 |
return Py::String(s.str()); |
| 125 |
} |
| 126 |
|
| 127 |
Py::Object CommandActionPy::getattro(const Py::String& attr_) |
| 128 |
{ |
| 129 |
std::string attr = static_cast<std::string>(attr_); |
| 130 |
Py::Dict d; |
| 131 |
d["name"] = Py::String(this->cmdName); |
| 132 |
if (attr == "__dict__") { |
| 133 |
return d; |
| 134 |
} |
| 135 |
else if (attr == "name") { |
| 136 |
return d["name"]; |
| 137 |
} |
| 138 |
else { |
| 139 |
return genericGetAttro(attr_); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
int CommandActionPy::setattro(const Py::String& attr_, const Py::Object& value) |
| 144 |
{ |
| 145 |
std::string attr = static_cast<std::string>(attr_); |
| 146 |
if (attr == "name" && value.isString()) { |
| 147 |
cmdName = static_cast<std::string>(Py::String(value)); |
| 148 |
cmd = Application::Instance->commandManager().getCommandByName(cmdName.c_str()); |
| 149 |
} |
| 150 |
else { |
| 151 |
return genericSetAttro(attr_, value); |
| 152 |
} |
| 153 |
|
| 154 |
return 0; |
| 155 |
} |