Debian Codegraph @

src/Gui/CommandPyImp.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2020 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <sstream>
24 #include <QRegularExpression>
25 #include <QRegularExpressionMatch>
26
27 #include <Base/PyWrapParseTupleAndKeywords.h>
28
29 #include "Command.h"
30 #include "Action.h"
31 #include "Application.h"
32 #include "MainWindow.h"
33 #include "Selection.h"
34 #include "Window.h"
35 #include "PythonWrapper.h"
36
37 // inclusion of the generated files (generated out of CommandPy.pyi)
38 #include "CommandPy.h"
39 #include "CommandPy.cpp"
40 #include "ShortcutManager.h"
41
42
43 // returns a string which represents the object e.g. when printed in python
44 std::string CommandPy::representation() const
45 {
46 return {"<Command object>"};
47 }
48
49 PyObject* CommandPy::get(PyObject* args)
50 {
51 char* pName;
52 if (!PyArg_ParseTuple(args, "s", &pName)) {
53 return nullptr;
54 }
55
56 Command* cmd = Application::Instance->commandManager().getCommandByName(pName);
57 if (cmd) {
58 auto cmdPy = new CommandPy(cmd);
59 return cmdPy;
60 }
61
62 Py_Return;
63 }
64
65 PyObject* CommandPy::update(PyObject* args)
66 {
67 if (!PyArg_ParseTuple(args, "")) {
68 return nullptr;
69 }
70
71 getMainWindow()->updateActions();
72 Py_Return;
73 }
74
75 PyObject* CommandPy::listAll(PyObject* args)
76 {
77 if (!PyArg_ParseTuple(args, "")) {
78 return nullptr;
79 }
80
81 std::vector<Command*> cmds = Application::Instance->commandManager().getAllCommands();
82 PyObject* pyList = PyList_New(cmds.size());
83 int i = 0;
84 for (const auto& cmd : cmds) {
85 PyObject* str = PyUnicode_FromString(cmd->getName());
86 PyList_SetItem(pyList, i++, str);
87 }
88 return pyList;
89 }
90
91 PyObject* CommandPy::listByShortcut(PyObject* args)
92 {
93 char* shortcut_to_find;
94 PyObject* bIsRegularExp = Py_False;
95 if (!PyArg_ParseTuple(args, "s|O!", &shortcut_to_find, &PyBool_Type, &bIsRegularExp)) {
96 return nullptr;
97 }
98
99 std::vector<Command*> cmds = Application::Instance->commandManager().getAllCommands();
100 std::vector<std::string> matches;
101 for (Command* c : cmds) {
102 Action* action = c->getAction();
103 if (action) {
104 QString spc = QStringLiteral(" ");
105 if (Base::asBoolean(bIsRegularExp)) {
106 QRegularExpression re(
107 QString::fromLatin1(shortcut_to_find),
108 QRegularExpression::CaseInsensitiveOption
109 );
110 if (!re.isValid()) {
111 std::stringstream str;
112 str << "Invalid regular expression:" << ' ' << shortcut_to_find;
113 throw Py::RuntimeError(str.str());
114 }
115
116 if (re.match(action->shortcut().toString().remove(spc).toUpper()).hasMatch()) {
117 matches.emplace_back(c->getName());
118 }
119 }
120 else if (action->shortcut().toString().remove(spc).toUpper()
121 == QString::fromLatin1(shortcut_to_find).remove(spc).toUpper()) {
122 matches.emplace_back(c->getName());
123 }
124 }
125 }
126
127 PyObject* pyList = PyList_New(matches.size());
128 int i = 0;
129 for (const std::string& match : matches) {
130 PyObject* str = PyUnicode_FromString(match.c_str());
131 PyList_SetItem(pyList, i++, str);
132 }
133 return pyList;
134 }
135
136 PyObject* CommandPy::run(PyObject* args)
137 {
138 int item = 0;
139 if (!PyArg_ParseTuple(args, "|i", &item)) {
140 return nullptr;
141 }
142
143 Gui::Command::LogDisabler d1;
144 Gui::SelectionLogDisabler d2;
145
146 Command* cmd = this->getCommandPtr();
147 if (cmd) {
148 cmd->invoke(item);
149 Py_Return;
150 }
151 else {
152 PyErr_Format(Base::PyExc_FC_GeneralError, "No such command");
153 return nullptr;
154 }
155 }
156
157 PyObject* CommandPy::isActive(PyObject* args) const
158 {
159 if (!PyArg_ParseTuple(args, "")) {
160 return nullptr;
161 }
162
163 Command* cmd = this->getCommandPtr();
164 if (cmd) {
165 PY_TRY
166 {
167 return Py::new_reference_to(Py::Boolean(cmd->isActive()));
168 }
169 PY_CATCH;
170 }
171 else {
172 PyErr_Format(Base::PyExc_FC_GeneralError, "No such command");
173 return nullptr;
174 }
175 }
176
177 PyObject* CommandPy::getShortcut(PyObject* args)
178 {
179 if (!PyArg_ParseTuple(args, "")) {
180 return nullptr;
181 }
182
183 Command* cmd = this->getCommandPtr();
184 if (cmd) {
185 PyObject* str = PyUnicode_FromString(
186 cmd->getAction() ? cmd->getAction()->shortcut().toString().toStdString().c_str() : ""
187 );
188 return str;
189 }
190 else {
191 PyErr_Format(Base::PyExc_FC_GeneralError, "No such command");
192 return nullptr;
193 }
194 }
195
196 PyObject* CommandPy::setShortcut(PyObject* args)
197 {
198 char* pShortcut;
199 if (!PyArg_ParseTuple(args, "s", &pShortcut)) {
200 return nullptr;
201 }
202
203 Command* cmd = this->getCommandPtr();
204 if (cmd) {
205 ShortcutManager::instance()->setShortcut(cmd->getName(), pShortcut);
206 return Py::new_reference_to(Py::Boolean(true));
207 }
208 else {
209 PyErr_Format(Base::PyExc_FC_GeneralError, "No such command");
210 return nullptr;
211 }
212 }
213
214 PyObject* CommandPy::resetShortcut(PyObject* args)
215 {
216
217 if (!PyArg_ParseTuple(args, "")) {
218 return nullptr;
219 }
220
221 Command* cmd = this->getCommandPtr();
222 if (cmd) {
223 ShortcutManager::instance()->reset(cmd->getName());
224 return Py::new_reference_to(Py::Boolean(true));
225 }
226 else {
227 PyErr_Format(Base::PyExc_FC_GeneralError, "No such command");
228 return nullptr;
229 }
230 }
231
232 PyObject* CommandPy::getInfo(PyObject* args)
233 {
234 if (!PyArg_ParseTuple(args, "")) {
235 return nullptr;
236 }
237
238 Command* cmd = this->getCommandPtr();
239 if (cmd) {
240 Action* action = cmd->getAction();
241 PyObject* pyDict = PyDict_New();
242 const char* cmdName = cmd->getName();
243 const char* menuTxt = cmd->getMenuText();
244 const char* tooltipTxt = cmd->getToolTipText();
245 const char* whatsThisTxt = cmd->getWhatsThis();
246 const char* statustipTxt = cmd->getStatusTip();
247 const char* pixMapTxt = cmd->getPixmap();
248 std::string shortcutTxt;
249 if (action) {
250 shortcutTxt = action->shortcut().toString().toStdString();
251 }
252
253 PyObject* strCmdName = PyUnicode_FromString(cmdName);
254 PyObject* strMenuTxt = PyUnicode_FromString(menuTxt ? menuTxt : "");
255 PyObject* strTooltipTxt = PyUnicode_FromString(tooltipTxt ? tooltipTxt : "");
256 PyObject* strWhatsThisTxt = PyUnicode_FromString(whatsThisTxt ? whatsThisTxt : "");
257 PyObject* strStatustipTxt = PyUnicode_FromString(statustipTxt ? statustipTxt : "");
258 PyObject* strPixMapTxt = PyUnicode_FromString(pixMapTxt ? pixMapTxt : "");
259 PyObject* strShortcutTxt = PyUnicode_FromString(
260 !shortcutTxt.empty() ? shortcutTxt.c_str() : ""
261 );
262 PyDict_SetItemString(pyDict, "name", strCmdName);
263 PyDict_SetItemString(pyDict, "menuText", strMenuTxt);
264 PyDict_SetItemString(pyDict, "toolTip", strTooltipTxt);
265 PyDict_SetItemString(pyDict, "whatsThis", strWhatsThisTxt);
266 PyDict_SetItemString(pyDict, "statusTip", strStatustipTxt);
267 PyDict_SetItemString(pyDict, "pixmap", strPixMapTxt);
268 PyDict_SetItemString(pyDict, "shortcut", strShortcutTxt);
269 return pyDict;
270 }
271 else {
272 PyErr_Format(Base::PyExc_FC_GeneralError, "No such command");
273 return nullptr;
274 }
275 }
276
277 PyObject* CommandPy::getAction(PyObject* args)
278 {
279 if (!PyArg_ParseTuple(args, "")) {
280 return nullptr;
281 }
282
283 Command* cmd = this->getCommandPtr();
284 if (cmd) {
285 Action* action = cmd->getAction();
286 auto* group = qobject_cast<ActionGroup*>(action);
287
288 PythonWrapper wrap;
289 wrap.loadWidgetsModule();
290
291 Py::List list;
292 if (group) {
293 const auto actions = group->actions();
294 for (auto a : actions) {
295 list.append(wrap.fromQAction(a));
296 }
297 }
298 else if (action) {
299 list.append(wrap.fromQAction(action->action()));
300 }
301
302 return Py::new_reference_to(list);
303 }
304 else {
305 PyErr_Format(Base::PyExc_FC_GeneralError, "No such command");
306 return nullptr;
307 }
308 }
309
310
311 PyObject* CommandPy::createCustomCommand(PyObject* args, PyObject* kw)
312 {
313 const char* macroFile;
314 const char* menuTxt = nullptr;
315 const char* tooltipTxt = nullptr;
316 const char* whatsthisTxt = nullptr;
317 const char* statustipTxt = nullptr;
318 const char* pixmapTxt = nullptr;
319 const char* shortcutTxt = nullptr;
320 static std::array<const char*, 8> kwlist {
321 "macroFile",
322 "menuText",
323 "toolTip",
324 "whatsThis",
325 "statusTip",
326 "pixmap",
327 "shortcut",
328 nullptr
329 };
330 if (!Base::Wrapped_ParseTupleAndKeywords(
331 args,
332 kw,
333 "s|zzzzzz",
334 kwlist,
335 &macroFile,
336 &menuTxt,
337 &tooltipTxt,
338 &whatsthisTxt,
339 &statustipTxt,
340 &pixmapTxt,
341 &shortcutTxt
342 )) {
343 return nullptr;
344 }
345
346 auto name = Application::Instance->commandManager().newMacroName();
347 CommandManager& commandManager = Application::Instance->commandManager();
348 auto macro = new MacroCommand(name.c_str(), false);
349 commandManager.addCommand(macro);
350
351 macro->setScriptName(macroFile);
352
353 if (menuTxt) {
354 macro->setMenuText(menuTxt);
355 }
356
357 if (tooltipTxt) {
358 macro->setToolTipText(tooltipTxt);
359 }
360
361 if (whatsthisTxt) {
362 macro->setWhatsThis(whatsthisTxt);
363 }
364
365 if (statustipTxt) {
366 macro->setStatusTip(statustipTxt);
367 }
368
369 if (pixmapTxt) {
370 macro->setPixmap(pixmapTxt);
371 }
372
373 if (shortcutTxt) {
374 macro->setAccel(shortcutTxt);
375 }
376
377 return PyUnicode_FromString(name.c_str());
378 }
379
380 PyObject* CommandPy::removeCustomCommand(PyObject* args)
381 {
382 const char* actionName = nullptr;
383 if (!PyArg_ParseTuple(args, "s", &actionName)) {
384 return nullptr;
385 }
386
387 CommandManager& commandManager = Application::Instance->commandManager();
388 std::vector<Command*> macros = commandManager.getGroupCommands("Macros");
389
390 auto action = std::find_if(macros.begin(), macros.end(), [actionName](const Command* c) {
391 return std::string(c->getName()) == std::string(actionName);
392 });
393
394 if (action != macros.end()) {
395 commandManager.removeCommand(*action);
396 return Py::new_reference_to(Py::Boolean(true));
397 }
398 else {
399 return Py::new_reference_to(Py::Boolean(false));
400 }
401 }
402
403 PyObject* CommandPy::findCustomCommand(PyObject* args)
404 {
405 const char* macroScriptName = nullptr;
406 if (!PyArg_ParseTuple(args, "s", &macroScriptName)) {
407 return nullptr;
408 }
409
410 CommandManager& commandManager = Application::Instance->commandManager();
411 std::vector<Command*> macros = commandManager.getGroupCommands("Macros");
412
413 auto action = std::find_if(macros.begin(), macros.end(), [macroScriptName](const Command* c) {
414 if (auto mc = dynamic_cast<const MacroCommand*>(c)) {
415 if (std::string(mc->getScriptName()) == std::string(macroScriptName)) {
416 return true;
417 }
418 }
419 return false;
420 });
421
422 if (action != macros.end()) {
423 return PyUnicode_FromString((*action)->getName());
424 }
425 else {
426 Py_Return;
427 }
428 }
429
430 PyObject* CommandPy::getCustomAttributes(const char* /*attr*/) const
431 {
432 return nullptr;
433 }
434
435 int CommandPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
436 {
437 return 0;
438 }