Debian Codegraph @

src/Gui/WorkbenchManipulatorPython.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
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 "WorkbenchManipulatorPython.h"
26 #include "MenuManager.h"
27 #include "ToolBarManager.h"
28 #include <Base/Console.h>
29 #include <Base/Interpreter.h>
30
31 FC_LOG_LEVEL_INIT("WorkbenchManipulatorPython", true, true)
32
33 using namespace Gui;
34
35 void WorkbenchManipulatorPython::installManipulator(const Py::Object& obj)
36 {
37 auto manip = std::make_shared<WorkbenchManipulatorPython>(obj);
38 WorkbenchManipulator::installManipulator(manip);
39 }
40
41 void WorkbenchManipulatorPython::removeManipulator(const Py::Object& obj)
42 {
43 auto manip = getManipulators();
44 for (const auto& it : manip) {
45 auto ptr = std::dynamic_pointer_cast<WorkbenchManipulatorPython>(it);
46 if (ptr && ptr->object == obj) {
47 WorkbenchManipulator::removeManipulator(ptr);
48 break;
49 }
50 }
51 }
52
53 WorkbenchManipulatorPython::WorkbenchManipulatorPython(const Py::Object& obj)
54 : object(obj)
55 {}
56
57 WorkbenchManipulatorPython::~WorkbenchManipulatorPython()
58 {
59 Base::PyGILStateLocker lock;
60 object = Py::None();
61 }
62
63 /*!
64 * \brief WorkbenchManipulatorPython::modifyMenuBar
65 * \param menuBar
66 * The Python manipulator can be implemented as
67 * \code
68 * class Manipulator:
69 * def modifyMenuBar(self):
70 * return [{"remove" : "Std_Quit"},
71 * {"append" : "Std_About", "menuItem" : "Std_DlgMacroRecord"},
72 * {"insert" : "Std_About", "menuItem" : "Std_DlgParameter"}
73 * {"insert" : "Std_Windows", "menuItem" : "Std_DlgParameter", "after" : ""}]
74 *
75 * manip = Manipulator()
76 * Gui.addWorkbenchManipulator(manip)
77 * \endcode
78 * This manipulator removes the Std_Quit command, appends the Std_About command
79 * to the Macro menu, inserts it to the Tools menu before the Std_DlgParameter
80 * and adds the Std_Windows after the Std_DlgParameter command.
81 */
82 void WorkbenchManipulatorPython::modifyMenuBar(MenuItem* menuBar)
83 {
84 Base::PyGILStateLocker lock;
85 try {
86 tryModifyMenuBar(menuBar);
87 }
88 catch (Py::Exception&) {
89 Base::PyException exc; // extract the Python error text
90 exc.reportException();
91 }
92 }
93
94 void WorkbenchManipulatorPython::tryModifyMenuBar(MenuItem* menuBar)
95 {
96 if (object.hasAttr(std::string("modifyMenuBar"))) {
97 Py::Callable method(object.getAttr(std::string("modifyMenuBar")));
98 Py::Tuple args;
99 Py::Object result = method.apply(args);
100 if (result.isDict()) {
101 tryModifyMenuBar(Py::Dict(result), menuBar);
102 }
103 else if (result.isSequence()) {
104 Py::Sequence list(result);
105 for (const auto& it : list) {
106 if (it.isDict()) {
107 tryModifyMenuBar(Py::Dict(it), menuBar);
108 }
109 }
110 }
111 }
112 }
113
114 // NOLINTNEXTLINE
115 void WorkbenchManipulatorPython::tryModifyMenuBar(const Py::Dict& dict, MenuItem* menuBar)
116 {
117 std::string insert("insert");
118 std::string append("append");
119 std::string remove("remove");
120
121 // insert a new command
122 if (dict.hasKey(insert)) {
123 std::string command = static_cast<std::string>(Py::String(dict.getItem(insert)));
124 std::string itemName = static_cast<std::string>(Py::String(dict.getItem("menuItem")));
125 bool after = dict.hasKey(std::string("after"));
126
127 if (auto par = menuBar->findParentOf(itemName)) {
128 if (MenuItem* item = par->findItem(itemName)) {
129 if (after) {
130 item = par->afterItem(item);
131 }
132
133 if (item) {
134 auto add = new MenuItem(); // NOLINT
135 add->setCommand(command);
136 par->insertItem(item, add);
137 }
138 }
139 }
140 }
141 // append a command
142 else if (dict.hasKey(append)) {
143 std::string command = static_cast<std::string>(Py::String(dict.getItem(append)));
144 std::string itemName = static_cast<std::string>(Py::String(dict.getItem("menuItem")));
145
146 if (auto par = menuBar->findParentOf(itemName)) {
147 auto add = new MenuItem(); // NOLINT
148 add->setCommand(command);
149 par->appendItem(add);
150 }
151 }
152 // remove a command
153 else if (dict.hasKey(remove)) {
154 std::string command = static_cast<std::string>(Py::String(dict.getItem(remove)));
155 if (auto par = menuBar->findParentOf(command)) {
156 if (MenuItem* item = par->findItem(command)) {
157 if (item == menuBar) {
158 // Can't remove the menubar itself - Coverity issue 512853
159 FC_WARN("Cannot remove top-level menubar");
160 return;
161 }
162 par->removeItem(item);
163 delete item; // NOLINT
164 }
165 }
166 }
167 }
168
169 /*!
170 * \brief WorkbenchManipulatorPython::modifyContextMenu
171 * \param menuBar
172 * The Python manipulator can be implemented as
173 * \code
174 * class Manipulator:
175 * def modifyContextMenu(self, recipient):
176 * if recipient == "View":
177 * return [{"remove" : "Standard views"},
178 * {"insert" : "Std_Windows", "menuItem" : "View_Measure_Toggle_All"}]
179 *
180 * manip = Manipulator()
181 * Gui.addWorkbenchManipulator(manip)
182 * \endcode
183 * This manipulator removes the "Standard views sub-menu and
184 * adds the Std_Windows before the View_Measure_Toggle_All command.
185 */
186 void WorkbenchManipulatorPython::modifyContextMenu(const char* recipient, MenuItem* menuBar)
187 {
188 Base::PyGILStateLocker lock;
189 try {
190 tryModifyContextMenu(recipient, menuBar);
191 }
192 catch (Py::Exception&) {
193 Base::PyException exc; // extract the Python error text
194 exc.reportException();
195 }
196 }
197
198 void WorkbenchManipulatorPython::tryModifyContextMenu(const char* recipient, MenuItem* menuBar)
199 {
200 if (object.hasAttr(std::string("modifyContextMenu"))) {
201 Py::Callable method(object.getAttr(std::string("modifyContextMenu")));
202 Py::Tuple args(1);
203 args.setItem(0, Py::String(recipient));
204 Py::Object result = method.apply(args);
205 if (result.isDict()) {
206 tryModifyContextMenu(Py::Dict(result), menuBar);
207 }
208 else if (result.isSequence()) {
209 Py::Sequence list(result);
210 for (const auto& it : list) {
211 if (it.isDict()) {
212 tryModifyContextMenu(Py::Dict(it), menuBar);
213 }
214 }
215 }
216 }
217 }
218
219 void WorkbenchManipulatorPython::tryModifyContextMenu(const Py::Dict& dict, MenuItem* menuBar)
220 {
221 tryModifyMenuBar(dict, menuBar);
222 }
223
224 void WorkbenchManipulatorPython::modifyToolBars(ToolBarItem* toolBar)
225 {
226 Base::PyGILStateLocker lock;
227 try {
228 tryModifyToolBar(toolBar);
229 }
230 catch (Py::Exception&) {
231 Base::PyException exc; // extract the Python error text
232 exc.reportException();
233 }
234 }
235
236 /*!
237 * \brief WorkbenchManipulatorPython::tryModifyToolBar
238 * \param toolBar
239 * The Python manipulator can be implemented as
240 * \code
241 * class Manipulator:
242 * def modifyToolBars(self):
243 return [{"remove" : "Macro"},
244 {"append" : "Std_Quit", "toolBar" : "File"},
245 * {"insert" : "Std_Cut", "toolItem" : "Std_New"}]
246 *
247 * manip = Manipulator()
248 * Gui.addWorkbenchManipulator(manip)
249 * \endcode
250 * This manipulator removes the Macro toolbar, adds the
251 * Std_Quit to the File toolbar and adds Std_Cut the
252 * command to the toolbar where Std_New is part of.
253 */
254 void WorkbenchManipulatorPython::tryModifyToolBar(ToolBarItem* toolBar)
255 {
256 if (object.hasAttr(std::string("modifyToolBars"))) {
257 Py::Callable method(object.getAttr(std::string("modifyToolBars")));
258 Py::Tuple args;
259 Py::Object result = method.apply(args);
260 if (result.isDict()) {
261 tryModifyToolBar(Py::Dict(result), toolBar);
262 }
263 else if (result.isSequence()) {
264 Py::Sequence list(result);
265 for (const auto& it : list) {
266 if (it.isDict()) {
267 tryModifyToolBar(Py::Dict(it), toolBar);
268 }
269 }
270 }
271 }
272 }
273
274 // NOLINTNEXTLINE
275 void WorkbenchManipulatorPython::tryModifyToolBar(const Py::Dict& dict, ToolBarItem* toolBar)
276 {
277 std::string insert("insert");
278 std::string append("append");
279 std::string remove("remove");
280
281 // insert a new command
282 if (dict.hasKey(insert)) {
283
284 std::string command = static_cast<std::string>(Py::String(dict.getItem(insert)));
285 std::string itemName = static_cast<std::string>(Py::String(dict.getItem("toolItem")));
286
287 for (auto it : toolBar->getItems()) {
288 if (ToolBarItem* item = it->findItem(itemName)) {
289 auto add = new ToolBarItem(); // NOLINT
290 add->setCommand(command);
291 it->insertItem(item, add);
292 break;
293 }
294 }
295 }
296 // append a command
297 else if (dict.hasKey(append)) {
298 std::string command = static_cast<std::string>(Py::String(dict.getItem(append)));
299 std::string itemName = static_cast<std::string>(Py::String(dict.getItem("toolBar")));
300
301 if (ToolBarItem* item = toolBar->findItem(itemName)) {
302 auto add = new ToolBarItem(); // NOLINT
303 add->setCommand(command);
304 item->appendItem(add);
305 }
306 }
307 // remove a command or toolbar
308 else if (dict.hasKey(remove)) {
309 std::string command = static_cast<std::string>(Py::String(dict.getItem(remove)));
310
311 if (ToolBarItem* item = toolBar->findItem(command)) {
312 toolBar->removeItem(item);
313 delete item; // NOLINT
314 }
315 else {
316 for (auto it : toolBar->getItems()) {
317 if (ToolBarItem* item = it->findItem(command)) {
318 if (item == toolBar) {
319 // Can't remove the toolBar itself - Coverity issue 513838
320 FC_WARN("Cannot remove top-level toolbar");
321 return;
322 }
323 it->removeItem(item);
324 delete item; // NOLINT
325 break;
326 }
327 }
328 }
329 }
330 }
331
332 void WorkbenchManipulatorPython::modifyDockWindows(DockWindowItems* dockWindow)
333 {
334 Base::PyGILStateLocker lock;
335 try {
336 tryModifyDockWindows(dockWindow);
337 }
338 catch (Py::Exception&) {
339 Base::PyException exc; // extract the Python error text
340 exc.reportException();
341 }
342 }
343
344 void WorkbenchManipulatorPython::tryModifyDockWindows(DockWindowItems* dockWindow)
345 {
346 if (object.hasAttr(std::string("modifyDockWindows"))) {
347 Py::Callable method(object.getAttr(std::string("modifyDockWindows")));
348 Py::Tuple args;
349 Py::Object result = method.apply(args);
350 if (result.isDict()) {
351 tryModifyDockWindows(Py::Dict(result), dockWindow);
352 }
353 else if (result.isSequence()) {
354 Py::Sequence list(result);
355 for (const auto& it : list) {
356 if (it.isDict()) {
357 tryModifyDockWindows(Py::Dict(it), dockWindow);
358 }
359 }
360 }
361 }
362 }
363
364 void WorkbenchManipulatorPython::tryModifyDockWindows(
365 [[maybe_unused]] const Py::Dict& dict,
366 [[maybe_unused]] DockWindowItems* dockWindow
367 )
368 {}