1
/***************************************************************************
2
* Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
#include "SelectionObserverPython.h"
26
#include <Base/Interpreter.h>
29
FC_LOG_LEVEL_INIT("Selection",false,true,true)
33
std::vector<SelectionObserverPython*> SelectionObserverPython::_instances;
35
SelectionObserverPython::SelectionObserverPython(const Py::Object& obj, ResolveMode resolve)
36
: SelectionObserver(true, resolve), inst(obj)
39
#define FC_PY_ELEMENT(_name) FC_PY_GetCallable(obj.ptr(),#_name,py_##_name);
43
SelectionObserverPython::~SelectionObserverPython() = default;
45
void SelectionObserverPython::addObserver(const Py::Object& obj, ResolveMode resolve)
47
_instances.push_back(new SelectionObserverPython(obj, resolve));
50
void SelectionObserverPython::removeObserver(const Py::Object& obj)
52
SelectionObserverPython* obs=nullptr;
53
for (std::vector<SelectionObserverPython*>::iterator it =
54
_instances.begin(); it != _instances.end(); ++it) {
55
if ((*it)->inst == obj) {
65
void SelectionObserverPython::onSelectionChanged(const SelectionChanges& msg)
69
case SelectionChanges::AddSelection:
72
case SelectionChanges::RmvSelection:
75
case SelectionChanges::SetSelection:
78
case SelectionChanges::ClrSelection:
81
case SelectionChanges::SetPreselect:
84
case SelectionChanges::RmvPreselect:
85
removePreselection(msg);
87
case SelectionChanges::PickedListChanged:
95
void SelectionObserverPython::pickedListChanged()
97
if(py_pickedListChanged.isNone())
99
Base::PyGILStateLocker lock;
101
Py::Callable(py_pickedListChanged).apply(Py::Tuple());
103
catch (Py::Exception&) {
104
Base::PyException e; // extract the Python error text
109
void SelectionObserverPython::addSelection(const SelectionChanges& msg)
111
if(py_addSelection.isNone())
113
Base::PyGILStateLocker lock;
116
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
117
args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : ""));
118
args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : ""));
120
tuple[0] = Py::Float(msg.x);
121
tuple[1] = Py::Float(msg.y);
122
tuple[2] = Py::Float(msg.z);
123
args.setItem(3, tuple);
124
Base::pyCall(py_addSelection.ptr(),args.ptr());
126
catch (Py::Exception&) {
127
Base::PyException e; // extract the Python error text
132
void SelectionObserverPython::removeSelection(const SelectionChanges& msg)
134
if(py_removeSelection.isNone())
136
Base::PyGILStateLocker lock;
139
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
140
args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : ""));
141
args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : ""));
142
Base::pyCall(py_removeSelection.ptr(),args.ptr());
144
catch (Py::Exception&) {
145
Base::PyException e; // extract the Python error text
150
void SelectionObserverPython::setSelection(const SelectionChanges& msg)
152
if(py_setSelection.isNone())
154
Base::PyGILStateLocker lock;
157
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
158
Base::pyCall(py_setSelection.ptr(),args.ptr());
160
catch (Py::Exception&) {
161
Base::PyException e; // extract the Python error text
166
void SelectionObserverPython::clearSelection(const SelectionChanges& msg)
168
if(py_clearSelection.isNone())
170
Base::PyGILStateLocker lock;
173
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
174
Base::pyCall(py_clearSelection.ptr(),args.ptr());
176
catch (Py::Exception&) {
177
Base::PyException e; // extract the Python error text
182
void SelectionObserverPython::setPreselection(const SelectionChanges& msg)
184
if(py_setPreselection.isNone())
186
Base::PyGILStateLocker lock;
189
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
190
args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : ""));
191
args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : ""));
192
Base::pyCall(py_setPreselection.ptr(),args.ptr());
194
catch (Py::Exception&) {
195
Base::PyException e; // extract the Python error text
200
void SelectionObserverPython::removePreselection(const SelectionChanges& msg)
202
if(py_removePreselection.isNone())
204
Base::PyGILStateLocker lock;
207
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
208
args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : ""));
209
args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : ""));
210
Base::pyCall(py_removePreselection.ptr(),args.ptr());
212
catch (Py::Exception&) {
213
Base::PyException e; // extract the Python error text