| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2018 Stefan Tröger <[email protected]> * |
| 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 <Base/Interpreter.h> |
| 24 |
|
| 25 |
#include "Application.h" |
| 26 |
#include "Document.h" |
| 27 |
#include "DocumentObserverPython.h" |
| 28 |
#include "ViewProvider.h" |
| 29 |
#include "ViewProviderDocumentObject.h" |
| 30 |
|
| 31 |
|
| 32 |
using namespace Gui; |
| 33 |
namespace sp = std::placeholders; |
| 34 |
|
| 35 |
std::vector<DocumentObserverPython*> DocumentObserverPython::_instances; |
| 36 |
|
| 37 |
void DocumentObserverPython::addObserver(const Py::Object& obj) |
| 38 |
{ |
| 39 |
_instances.push_back(new DocumentObserverPython(obj)); |
| 40 |
} |
| 41 |
|
| 42 |
void DocumentObserverPython::removeObserver(const Py::Object& obj) |
| 43 |
{ |
| 44 |
DocumentObserverPython* obs = nullptr; |
| 45 |
for (std::vector<DocumentObserverPython*>::iterator it = _instances.begin(); |
| 46 |
it != _instances.end(); |
| 47 |
++it) { |
| 48 |
if ((*it)->inst == obj) { |
| 49 |
obs = *it; |
| 50 |
_instances.erase(it); |
| 51 |
break; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
delete obs; |
| 56 |
} |
| 57 |
|
| 58 |
DocumentObserverPython::DocumentObserverPython(const Py::Object& obj) |
| 59 |
: inst(obj) |
| 60 |
{ |
| 61 |
// NOLINTBEGIN |
| 62 |
#define FC_PY_ELEMENT_ARG1(_name1, _name2) \ |
| 63 |
do { \ |
| 64 |
FC_PY_GetCallable(obj.ptr(), "slot" #_name1, py##_name1.py); \ |
| 65 |
if (!py##_name1.py.isNone()) \ |
| 66 |
py##_name1.slot = Application::Instance->signal##_name2.connect( \ |
| 67 |
std::bind(&DocumentObserverPython::slot##_name1, this, sp::_1) \ |
| 68 |
); \ |
| 69 |
} while (0); |
| 70 |
|
| 71 |
#define FC_PY_ELEMENT_ARG2(_name1, _name2) \ |
| 72 |
do { \ |
| 73 |
FC_PY_GetCallable(obj.ptr(), "slot" #_name1, py##_name1.py); \ |
| 74 |
if (!py##_name1.py.isNone()) \ |
| 75 |
py##_name1.slot = Application::Instance->signal##_name2.connect( \ |
| 76 |
std::bind(&DocumentObserverPython::slot##_name1, this, sp::_1, sp::_2) \ |
| 77 |
); \ |
| 78 |
} while (0); |
| 79 |
|
| 80 |
FC_PY_ELEMENT_ARG1(CreatedDocument, NewDocument) |
| 81 |
FC_PY_ELEMENT_ARG1(DeletedDocument, DeleteDocument) |
| 82 |
FC_PY_ELEMENT_ARG1(RelabelDocument, RelabelDocument) |
| 83 |
FC_PY_ELEMENT_ARG1(RenameDocument, RenameDocument) |
| 84 |
FC_PY_ELEMENT_ARG1(ActivateDocument, ActiveDocument) |
| 85 |
FC_PY_ELEMENT_ARG1(CreatedObject, NewObject) |
| 86 |
FC_PY_ELEMENT_ARG1(DeletedObject, DeletedObject) |
| 87 |
FC_PY_ELEMENT_ARG2(BeforeChangeObject, BeforeChangeObject) |
| 88 |
FC_PY_ELEMENT_ARG2(ChangedObject, ChangedObject) |
| 89 |
FC_PY_ELEMENT_ARG1(InEdit, InEdit) |
| 90 |
FC_PY_ELEMENT_ARG1(ResetEdit, ResetEdit) |
| 91 |
// NOLINTEND |
| 92 |
} |
| 93 |
|
| 94 |
DocumentObserverPython::~DocumentObserverPython() = default; |
| 95 |
|
| 96 |
void DocumentObserverPython::slotCreatedDocument(const Gui::Document& Doc) |
| 97 |
{ |
| 98 |
Base::PyGILStateLocker lock; |
| 99 |
try { |
| 100 |
Py::Tuple args(1); |
| 101 |
args.setItem(0, Py::asObject(const_cast<Gui::Document&>(Doc).getPyObject())); |
| 102 |
Base::pyCall(pyCreatedDocument.ptr(), args.ptr()); |
| 103 |
} |
| 104 |
catch (Py::Exception&) { |
| 105 |
Base::PyException e; // extract the Python error text |
| 106 |
e.reportException(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
void DocumentObserverPython::slotDeletedDocument(const Gui::Document& Doc) |
| 111 |
{ |
| 112 |
Base::PyGILStateLocker lock; |
| 113 |
try { |
| 114 |
Py::Tuple args(1); |
| 115 |
args.setItem(0, Py::asObject(const_cast<Gui::Document&>(Doc).getPyObject())); |
| 116 |
Base::pyCall(pyDeletedDocument.ptr(), args.ptr()); |
| 117 |
} |
| 118 |
catch (Py::Exception&) { |
| 119 |
Base::PyException e; // extract the Python error text |
| 120 |
e.reportException(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
void DocumentObserverPython::slotRelabelDocument(const Gui::Document& Doc) |
| 125 |
{ |
| 126 |
Base::PyGILStateLocker lock; |
| 127 |
try { |
| 128 |
Py::Tuple args(1); |
| 129 |
args.setItem(0, Py::asObject(const_cast<Gui::Document&>(Doc).getPyObject())); |
| 130 |
Base::pyCall(pyRelabelDocument.ptr(), args.ptr()); |
| 131 |
} |
| 132 |
catch (Py::Exception&) { |
| 133 |
Base::PyException e; // extract the Python error text |
| 134 |
e.reportException(); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void DocumentObserverPython::slotRenameDocument(const Gui::Document& Doc) |
| 139 |
{ |
| 140 |
Base::PyGILStateLocker lock; |
| 141 |
try { |
| 142 |
Py::Tuple args(1); |
| 143 |
args.setItem(0, Py::asObject(const_cast<Gui::Document&>(Doc).getPyObject())); |
| 144 |
Base::pyCall(pyRenameDocument.ptr(), args.ptr()); |
| 145 |
} |
| 146 |
catch (Py::Exception&) { |
| 147 |
Base::PyException e; // extract the Python error text |
| 148 |
e.reportException(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void DocumentObserverPython::slotActivateDocument(const Gui::Document& Doc) |
| 153 |
{ |
| 154 |
Base::PyGILStateLocker lock; |
| 155 |
try { |
| 156 |
Py::Tuple args(1); |
| 157 |
args.setItem(0, Py::asObject(const_cast<Gui::Document&>(Doc).getPyObject())); |
| 158 |
Base::pyCall(pyActivateDocument.ptr(), args.ptr()); |
| 159 |
} |
| 160 |
catch (Py::Exception&) { |
| 161 |
Base::PyException e; // extract the Python error text |
| 162 |
e.reportException(); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
void DocumentObserverPython::slotCreatedObject(const Gui::ViewProvider& Obj) |
| 167 |
{ |
| 168 |
Base::PyGILStateLocker lock; |
| 169 |
try { |
| 170 |
Py::Tuple args(1); |
| 171 |
args.setItem(0, Py::asObject(const_cast<Gui::ViewProvider&>(Obj).getPyObject())); |
| 172 |
Base::pyCall(pyCreatedObject.ptr(), args.ptr()); |
| 173 |
} |
| 174 |
catch (Py::Exception&) { |
| 175 |
Base::PyException e; // extract the Python error text |
| 176 |
e.reportException(); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
void DocumentObserverPython::slotDeletedObject(const Gui::ViewProvider& Obj) |
| 181 |
{ |
| 182 |
Base::PyGILStateLocker lock; |
| 183 |
try { |
| 184 |
Py::Tuple args(1); |
| 185 |
args.setItem(0, Py::asObject(const_cast<Gui::ViewProvider&>(Obj).getPyObject())); |
| 186 |
Base::pyCall(pyDeletedObject.ptr(), args.ptr()); |
| 187 |
} |
| 188 |
catch (Py::Exception&) { |
| 189 |
Base::PyException e; // extract the Python error text |
| 190 |
e.reportException(); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
void DocumentObserverPython::slotBeforeChangeObject( |
| 195 |
const Gui::ViewProvider& Obj, |
| 196 |
const App::Property& Prop |
| 197 |
) |
| 198 |
{ |
| 199 |
Base::PyGILStateLocker lock; |
| 200 |
try { |
| 201 |
Py::Tuple args(2); |
| 202 |
args.setItem(0, Py::asObject(const_cast<Gui::ViewProvider&>(Obj).getPyObject())); |
| 203 |
// If a property is touched but not part of a document object then its name is null. |
| 204 |
// In this case the slot function must not be called. |
| 205 |
const char* prop_name = Obj.getPropertyName(&Prop); |
| 206 |
if (prop_name) { |
| 207 |
args.setItem(1, Py::String(prop_name)); |
| 208 |
Base::pyCall(pyBeforeChangeObject.ptr(), args.ptr()); |
| 209 |
} |
| 210 |
} |
| 211 |
catch (Py::Exception&) { |
| 212 |
Base::PyException e; // extract the Python error text |
| 213 |
e.reportException(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
void DocumentObserverPython::slotChangedObject(const Gui::ViewProvider& Obj, const App::Property& Prop) |
| 218 |
{ |
| 219 |
Base::PyGILStateLocker lock; |
| 220 |
try { |
| 221 |
Py::Tuple args(2); |
| 222 |
args.setItem(0, Py::asObject(const_cast<Gui::ViewProvider&>(Obj).getPyObject())); |
| 223 |
// If a property is touched but not part of a document object then its name is null. |
| 224 |
// In this case the slot function must not be called. |
| 225 |
const char* prop_name = Obj.getPropertyName(&Prop); |
| 226 |
if (prop_name) { |
| 227 |
args.setItem(1, Py::String(prop_name)); |
| 228 |
Base::pyCall(pyChangedObject.ptr(), args.ptr()); |
| 229 |
} |
| 230 |
} |
| 231 |
catch (Py::Exception&) { |
| 232 |
Base::PyException e; // extract the Python error text |
| 233 |
e.reportException(); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void DocumentObserverPython::slotInEdit(const Gui::ViewProviderDocumentObject& Obj) |
| 238 |
{ |
| 239 |
Base::PyGILStateLocker lock; |
| 240 |
try { |
| 241 |
Py::Tuple args(1); |
| 242 |
args.setItem(0, Py::asObject(const_cast<Gui::ViewProviderDocumentObject&>(Obj).getPyObject())); |
| 243 |
Base::pyCall(pyInEdit.ptr(), args.ptr()); |
| 244 |
} |
| 245 |
catch (Py::Exception&) { |
| 246 |
Base::PyException e; // extract the Python error text |
| 247 |
e.reportException(); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
void DocumentObserverPython::slotResetEdit(const Gui::ViewProviderDocumentObject& Obj) |
| 252 |
{ |
| 253 |
Base::PyGILStateLocker lock; |
| 254 |
try { |
| 255 |
Py::Tuple args(1); |
| 256 |
args.setItem(0, Py::asObject(const_cast<Gui::ViewProviderDocumentObject&>(Obj).getPyObject())); |
| 257 |
Base::pyCall(pyResetEdit.ptr(), args.ptr()); |
| 258 |
} |
| 259 |
catch (Py::Exception&) { |
| 260 |
Base::PyException e; // extract the Python error text |
| 261 |
e.reportException(); |
| 262 |
} |
| 263 |
} |