1
// SPDX-License-Identifier: LGPL-2.1-or-later
3
/***************************************************************************
4
* Copyright (c) 2023 Mario Passaglia <mpassaglia[at]cbc.uba.ar> *
6
* This file is part of FreeCAD. *
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. *
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. *
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/>. *
22
**************************************************************************/
24
#include "PreCompiled.h"
25
#include <Base/PythonTypeExt.h>
29
#include "PythonWrapper.h"
31
#include "CommandActionPy.h"
37
CommandActionPy::CommandActionPy(Py::PythonClassInstance* self, Py::Tuple& args, Py::Dict& kwds)
38
: Py::PythonClass<CommandActionPy>::PythonClass(self, args, kwds)
41
if (!PyArg_ParseTuple(args.ptr(), "s", &name)) {
42
throw Py::Exception();
46
cmd = Application::Instance->commandManager().getCommandByName(name);
49
CommandActionPy::~CommandActionPy() = default;
51
Py::Object CommandActionPy::getAction()
54
cmd = Application::Instance->commandManager().getCommandByName(cmdName.c_str());
57
Action* action = cmd ? cmd->getAction() : nullptr;
60
wrap.loadWidgetsModule();
62
return wrap.fromQAction(action->action());
69
Py::Object CommandActionPy::getCommand()
72
cmd = Application::Instance->commandManager().getCommandByName(cmdName.c_str());
76
auto cmdPy = new CommandPy(cmd);
77
return Py::asObject(cmdPy);
82
PYCXX_NOARGS_METHOD_DECL(CommandActionPy, getCommand)
84
void CommandActionPy::init_type()
86
Base::PythonTypeExt ext(behaviors());
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");
97
behaviors().readyType();
100
PyObject* CommandActionPy::descriptorGetter(PyObject* self, PyObject* /*obj*/, PyObject* /*type*/)
102
auto cmdAction = Py::PythonClassObject<CommandActionPy>(self).getCxxObject();
104
return Py::new_reference_to(cmdAction->getAction());
107
int CommandActionPy::descriptorSetter(PyObject* /*self*/, PyObject* /*obj*/, PyObject* value)
110
PyErr_SetString(PyExc_AttributeError, "Can't overwrite command action");
113
PyErr_SetString(PyExc_AttributeError, "Can't delete command action");
119
Py::Object CommandActionPy::repr()
122
s << this->cmdName << " command action descriptor";
124
return Py::String(s.str());
127
Py::Object CommandActionPy::getattro(const Py::String& attr_)
129
std::string attr = static_cast<std::string>(attr_);
131
d["name"] = Py::String(this->cmdName);
132
if (attr == "__dict__") {
135
else if (attr == "name") {
139
return genericGetAttro(attr_);
143
int CommandActionPy::setattro(const Py::String& attr_, const Py::Object& value)
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());
151
return genericSetAttro(attr_, value);