25
#include "PreCompiled.h"
26
#include "WorkbenchManipulatorPython.h"
27
#include "MenuManager.h"
28
#include "ToolBarManager.h"
29
#include <Base/Interpreter.h>
33
void WorkbenchManipulatorPython::installManipulator(const Py::Object& obj)
35
auto manip = std::make_shared<WorkbenchManipulatorPython>(obj);
36
WorkbenchManipulator::installManipulator(manip);
39
void WorkbenchManipulatorPython::removeManipulator(const Py::Object& obj)
41
auto manip = getManipulators();
42
for (const auto& it : manip) {
43
auto ptr = std::dynamic_pointer_cast<WorkbenchManipulatorPython>(it);
44
if (ptr && ptr->object == obj) {
45
WorkbenchManipulator::removeManipulator(ptr);
51
WorkbenchManipulatorPython::WorkbenchManipulatorPython(const Py::Object& obj)
56
WorkbenchManipulatorPython::~WorkbenchManipulatorPython()
58
Base::PyGILStateLocker lock;
81
void WorkbenchManipulatorPython::modifyMenuBar(MenuItem* menuBar)
83
Base::PyGILStateLocker lock;
85
tryModifyMenuBar(menuBar);
87
catch (Py::Exception&) {
88
Base::PyException exc;
89
exc.ReportException();
93
void WorkbenchManipulatorPython::tryModifyMenuBar(MenuItem* menuBar)
95
if (object.hasAttr(std::string("modifyMenuBar"))) {
96
Py::Callable method(object.getAttr(std::string("modifyMenuBar")));
98
Py::Object result = method.apply(args);
99
if (result.isDict()) {
100
tryModifyMenuBar(Py::Dict(result), menuBar);
102
else if (result.isSequence()) {
103
Py::Sequence list(result);
104
for (const auto& it : list) {
106
tryModifyMenuBar(Py::Dict(it), menuBar);
114
void WorkbenchManipulatorPython::tryModifyMenuBar(const Py::Dict& dict, MenuItem* menuBar)
116
std::string insert("insert");
117
std::string append("append");
118
std::string remove("remove");
121
if (dict.hasKey(insert)) {
122
std::string command = static_cast<std::string>(Py::String(dict.getItem(insert)));
123
std::string itemName = static_cast<std::string>(Py::String(dict.getItem("menuItem")));
124
bool after = dict.hasKey(std::string("after"));
126
if (auto par = menuBar->findParentOf(itemName)) {
127
if (MenuItem* item = par->findItem(itemName)) {
129
item = par->afterItem(item);
133
auto add = new MenuItem();
134
add->setCommand(command);
135
par->insertItem(item, add);
141
else if (dict.hasKey(append)) {
142
std::string command = static_cast<std::string>(Py::String(dict.getItem(append)));
143
std::string itemName = static_cast<std::string>(Py::String(dict.getItem("menuItem")));
145
if (auto par = menuBar->findParentOf(itemName)) {
146
auto add = new MenuItem();
147
add->setCommand(command);
148
par->appendItem(add);
152
else if (dict.hasKey(remove)) {
153
std::string command = static_cast<std::string>(Py::String(dict.getItem(remove)));
154
if (auto par = menuBar->findParentOf(command)) {
155
if (MenuItem* item = par->findItem(command)) {
156
par->removeItem(item);
181
void WorkbenchManipulatorPython::modifyContextMenu(const char* recipient, MenuItem* menuBar)
183
Base::PyGILStateLocker lock;
185
tryModifyContextMenu(recipient, menuBar);
187
catch (Py::Exception&) {
188
Base::PyException exc;
189
exc.ReportException();
193
void WorkbenchManipulatorPython::tryModifyContextMenu(const char* recipient, MenuItem* menuBar)
195
if (object.hasAttr(std::string("modifyContextMenu"))) {
196
Py::Callable method(object.getAttr(std::string("modifyContextMenu")));
198
args.setItem(0, Py::String(recipient));
199
Py::Object result = method.apply(args);
200
if (result.isDict()) {
201
tryModifyContextMenu(Py::Dict(result), menuBar);
203
else if (result.isSequence()) {
204
Py::Sequence list(result);
205
for (const auto& it : list) {
207
tryModifyContextMenu(Py::Dict(it), menuBar);
214
void WorkbenchManipulatorPython::tryModifyContextMenu(const Py::Dict& dict, MenuItem* menuBar)
216
tryModifyMenuBar(dict, menuBar);
219
void WorkbenchManipulatorPython::modifyToolBars(ToolBarItem* toolBar)
221
Base::PyGILStateLocker lock;
223
tryModifyToolBar(toolBar);
225
catch (Py::Exception&) {
226
Base::PyException exc;
227
exc.ReportException();
249
void WorkbenchManipulatorPython::tryModifyToolBar(ToolBarItem* toolBar)
251
if (object.hasAttr(std::string("modifyToolBars"))) {
252
Py::Callable method(object.getAttr(std::string("modifyToolBars")));
254
Py::Object result = method.apply(args);
255
if (result.isDict()) {
256
tryModifyToolBar(Py::Dict(result), toolBar);
258
else if (result.isSequence()) {
259
Py::Sequence list(result);
260
for (const auto& it : list) {
262
tryModifyToolBar(Py::Dict(it), toolBar);
270
void WorkbenchManipulatorPython::tryModifyToolBar(const Py::Dict& dict, ToolBarItem* toolBar)
272
std::string insert("insert");
273
std::string append("append");
274
std::string remove("remove");
277
if (dict.hasKey(insert)) {
279
std::string command = static_cast<std::string>(Py::String(dict.getItem(insert)));
280
std::string itemName = static_cast<std::string>(Py::String(dict.getItem("toolItem")));
282
for (auto it : toolBar->getItems()) {
283
if (ToolBarItem* item = it->findItem(itemName)) {
284
auto add = new ToolBarItem();
285
add->setCommand(command);
286
it->insertItem(item, add);
292
else if (dict.hasKey(append)) {
293
std::string command = static_cast<std::string>(Py::String(dict.getItem(append)));
294
std::string itemName = static_cast<std::string>(Py::String(dict.getItem("toolBar")));
296
if (ToolBarItem* item = toolBar->findItem(itemName)) {
297
auto add = new ToolBarItem();
298
add->setCommand(command);
299
item->appendItem(add);
303
else if (dict.hasKey(remove)) {
304
std::string command = static_cast<std::string>(Py::String(dict.getItem(remove)));
306
if (ToolBarItem* item = toolBar->findItem(command)) {
307
toolBar->removeItem(item);
311
for (auto it : toolBar->getItems()) {
312
if (ToolBarItem* item = it->findItem(command)) {
313
it->removeItem(item);
322
void WorkbenchManipulatorPython::modifyDockWindows(DockWindowItems* dockWindow)
324
Base::PyGILStateLocker lock;
326
tryModifyDockWindows(dockWindow);
328
catch (Py::Exception&) {
329
Base::PyException exc;
330
exc.ReportException();
334
void WorkbenchManipulatorPython::tryModifyDockWindows(DockWindowItems* dockWindow)
336
if (object.hasAttr(std::string("modifyDockWindows"))) {
337
Py::Callable method(object.getAttr(std::string("modifyDockWindows")));
339
Py::Object result = method.apply(args);
340
if (result.isDict()) {
341
tryModifyDockWindows(Py::Dict(result), dockWindow);
343
else if (result.isSequence()) {
344
Py::Sequence list(result);
345
for (const auto& it : list) {
347
tryModifyDockWindows(Py::Dict(it), dockWindow);
354
void WorkbenchManipulatorPython::tryModifyDockWindows([[maybe_unused]]const Py::Dict& dict,
355
[[maybe_unused]]DockWindowItems* dockWindow)