| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#include "SelectionObserverPython.h" |
| 26 |
#include <Base/Interpreter.h> |
| 27 |
|
| 28 |
|
| 29 |
FC_LOG_LEVEL_INIT("Selection", false, true, true) |
| 30 |
|
| 31 |
using namespace Gui; |
| 32 |
|
| 33 |
std::vector<SelectionObserverPython*> SelectionObserverPython::_instances; |
| 34 |
|
| 35 |
void SelectionObserverPythonHandler::init(PyObject* obj) |
| 36 |
{ |
| 37 |
#undef FC_PY_ELEMENT |
| 38 |
#define FC_PY_ELEMENT(_name) FC_PY_GetCallable(obj, #_name, py_##_name); |
| 39 |
FC_PY_SEL_OBSERVER |
| 40 |
} |
| 41 |
|
| 42 |
SelectionObserverPythonHandler::~SelectionObserverPythonHandler() |
| 43 |
{ |
| 44 |
#undef FC_PY_ELEMENT |
| 45 |
#define FC_PY_ELEMENT(_name) py_##_name = Py::None(); |
| 46 |
|
| 47 |
try { |
| 48 |
FC_PY_SEL_OBSERVER |
| 49 |
} |
| 50 |
catch (Py::Exception& e) { |
| 51 |
e.clear(); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
void SelectionObserverPythonHandler::handleSelectionChanged(const SelectionChanges& msg) |
| 56 |
{ |
| 57 |
switch (msg.Type) { |
| 58 |
case SelectionChanges::AddSelection: |
| 59 |
addSelection(msg); |
| 60 |
break; |
| 61 |
case SelectionChanges::RmvSelection: |
| 62 |
removeSelection(msg); |
| 63 |
break; |
| 64 |
case SelectionChanges::SetSelection: |
| 65 |
setSelection(msg); |
| 66 |
break; |
| 67 |
case SelectionChanges::ClrSelection: |
| 68 |
clearSelection(msg); |
| 69 |
break; |
| 70 |
case SelectionChanges::SetPreselect: |
| 71 |
setPreselection(msg); |
| 72 |
break; |
| 73 |
case SelectionChanges::RmvPreselect: |
| 74 |
removePreselection(msg); |
| 75 |
break; |
| 76 |
case SelectionChanges::PickedListChanged: |
| 77 |
pickedListChanged(); |
| 78 |
break; |
| 79 |
default: |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void SelectionObserverPythonHandler::pickedListChanged() |
| 85 |
{ |
| 86 |
if (py_pickedListChanged.isNone()) { |
| 87 |
return; |
| 88 |
} |
| 89 |
Base::PyGILStateLocker lock; |
| 90 |
try { |
| 91 |
Py::Callable(py_pickedListChanged).apply(Py::Tuple()); |
| 92 |
} |
| 93 |
catch (Py::Exception&) { |
| 94 |
Base::PyException e; // extract the Python error text |
| 95 |
e.reportException(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void SelectionObserverPythonHandler::addSelection(const SelectionChanges& msg) |
| 100 |
{ |
| 101 |
if (py_addSelection.isNone()) { |
| 102 |
return; |
| 103 |
} |
| 104 |
Base::PyGILStateLocker lock; |
| 105 |
try { |
| 106 |
Py::Tuple args(4); |
| 107 |
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : "")); |
| 108 |
args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : "")); |
| 109 |
args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : "")); |
| 110 |
Py::Tuple tuple(3); |
| 111 |
tuple[0] = Py::Float(msg.x); |
| 112 |
tuple[1] = Py::Float(msg.y); |
| 113 |
tuple[2] = Py::Float(msg.z); |
| 114 |
args.setItem(3, tuple); |
| 115 |
Base::pyCall(py_addSelection.ptr(), args.ptr()); |
| 116 |
} |
| 117 |
catch (Py::Exception&) { |
| 118 |
Base::PyException e; // extract the Python error text |
| 119 |
e.reportException(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
void SelectionObserverPythonHandler::removeSelection(const SelectionChanges& msg) |
| 124 |
{ |
| 125 |
if (py_removeSelection.isNone()) { |
| 126 |
return; |
| 127 |
} |
| 128 |
Base::PyGILStateLocker lock; |
| 129 |
try { |
| 130 |
Py::Tuple args(3); |
| 131 |
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : "")); |
| 132 |
args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : "")); |
| 133 |
args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : "")); |
| 134 |
Base::pyCall(py_removeSelection.ptr(), args.ptr()); |
| 135 |
} |
| 136 |
catch (Py::Exception&) { |
| 137 |
Base::PyException e; // extract the Python error text |
| 138 |
e.reportException(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void SelectionObserverPythonHandler::setSelection(const SelectionChanges& msg) |
| 143 |
{ |
| 144 |
if (py_setSelection.isNone()) { |
| 145 |
return; |
| 146 |
} |
| 147 |
Base::PyGILStateLocker lock; |
| 148 |
try { |
| 149 |
Py::Tuple args(1); |
| 150 |
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : "")); |
| 151 |
Base::pyCall(py_setSelection.ptr(), args.ptr()); |
| 152 |
} |
| 153 |
catch (Py::Exception&) { |
| 154 |
Base::PyException e; // extract the Python error text |
| 155 |
e.reportException(); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
void SelectionObserverPythonHandler::clearSelection(const SelectionChanges& msg) |
| 160 |
{ |
| 161 |
if (py_clearSelection.isNone()) { |
| 162 |
return; |
| 163 |
} |
| 164 |
Base::PyGILStateLocker lock; |
| 165 |
try { |
| 166 |
Py::Tuple args(1); |
| 167 |
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : "")); |
| 168 |
Base::pyCall(py_clearSelection.ptr(), args.ptr()); |
| 169 |
} |
| 170 |
catch (Py::Exception&) { |
| 171 |
Base::PyException e; // extract the Python error text |
| 172 |
e.reportException(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
void SelectionObserverPythonHandler::setPreselection(const SelectionChanges& msg) |
| 177 |
{ |
| 178 |
if (py_setPreselection.isNone()) { |
| 179 |
return; |
| 180 |
} |
| 181 |
Base::PyGILStateLocker lock; |
| 182 |
try { |
| 183 |
Py::Tuple args(3); |
| 184 |
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : "")); |
| 185 |
args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : "")); |
| 186 |
args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : "")); |
| 187 |
Base::pyCall(py_setPreselection.ptr(), args.ptr()); |
| 188 |
} |
| 189 |
catch (Py::Exception&) { |
| 190 |
Base::PyException e; // extract the Python error text |
| 191 |
e.reportException(); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
void SelectionObserverPythonHandler::removePreselection(const SelectionChanges& msg) |
| 196 |
{ |
| 197 |
if (py_removePreselection.isNone()) { |
| 198 |
return; |
| 199 |
} |
| 200 |
Base::PyGILStateLocker lock; |
| 201 |
try { |
| 202 |
Py::Tuple args(3); |
| 203 |
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : "")); |
| 204 |
args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : "")); |
| 205 |
args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : "")); |
| 206 |
Base::pyCall(py_removePreselection.ptr(), args.ptr()); |
| 207 |
} |
| 208 |
catch (Py::Exception&) { |
| 209 |
Base::PyException e; // extract the Python error text |
| 210 |
e.reportException(); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
SelectionObserverPython::SelectionObserverPython(const Py::Object& obj, ResolveMode resolve) |
| 216 |
: SelectionObserver(true, resolve) |
| 217 |
, inst(obj) |
| 218 |
{ |
| 219 |
this->init(obj.ptr()); |
| 220 |
} |
| 221 |
|
| 222 |
SelectionObserverPython::~SelectionObserverPython() = default; |
| 223 |
|
| 224 |
void SelectionObserverPython::addObserver(const Py::Object& obj, ResolveMode resolve) |
| 225 |
{ |
| 226 |
_instances.push_back(new SelectionObserverPython(obj, resolve)); |
| 227 |
} |
| 228 |
|
| 229 |
void SelectionObserverPython::removeObserver(const Py::Object& obj) |
| 230 |
{ |
| 231 |
SelectionObserverPython* obs = nullptr; |
| 232 |
for (auto it = _instances.begin(); it != _instances.end(); ++it) { |
| 233 |
if ((*it)->inst == obj) { |
| 234 |
obs = *it; |
| 235 |
_instances.erase(it); |
| 236 |
break; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
delete obs; |
| 241 |
} |
| 242 |
|
| 243 |
void SelectionObserverPython::onSelectionChanged(const SelectionChanges& msg) |
| 244 |
{ |
| 245 |
handleSelectionChanged(msg); |
| 246 |
} |