| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 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 |
|
| 24 |
#include <sstream> |
| 25 |
#include <QApplication> |
| 26 |
#include <QEvent> |
| 27 |
#include <QFileInfo> |
| 28 |
#include <QMenu> |
| 29 |
#include <QPixmap> |
| 30 |
#include <Inventor/SoPickedPoint.h> |
| 31 |
#include <Inventor/details/SoDetail.h> |
| 32 |
|
| 33 |
|
| 34 |
#include <App/DocumentObjectPy.h> |
| 35 |
#include <Base/Interpreter.h> |
| 36 |
#include <Base/Tools.h> |
| 37 |
#include <CXX/Python3/Objects.hxx> |
| 38 |
|
| 39 |
#include "ViewProviderFeaturePython.h" |
| 40 |
#include "Application.h" |
| 41 |
#include "BitmapFactory.h" |
| 42 |
#include "Document.h" |
| 43 |
#include "PythonWrapper.h" |
| 44 |
#include "View3DInventorViewer.h" |
| 45 |
#include "ViewProviderDocumentObjectPy.h" |
| 46 |
#include "Selection.h" |
| 47 |
|
| 48 |
|
| 49 |
FC_LOG_LEVEL_INIT("ViewProviderFeaturePython", true, true) |
| 50 |
|
| 51 |
|
| 52 |
using namespace Gui; |
| 53 |
namespace sp = std::placeholders; |
| 54 |
|
| 55 |
|
| 56 |
// ---------------------------------------------------------------------------- |
| 57 |
|
| 58 |
ViewProviderFeaturePythonImp::ViewProviderFeaturePythonImp( |
| 59 |
ViewProviderDocumentObject* vp, |
| 60 |
App::PropertyPythonObject& proxy |
| 61 |
) |
| 62 |
: object(vp) |
| 63 |
, Proxy(proxy) |
| 64 |
{} |
| 65 |
|
| 66 |
ViewProviderFeaturePythonImp::~ViewProviderFeaturePythonImp() |
| 67 |
{ |
| 68 |
Base::PyGILStateLocker lock; |
| 69 |
#undef FC_PY_ELEMENT |
| 70 |
#define FC_PY_ELEMENT(_name) py_##_name = Py::None(); |
| 71 |
|
| 72 |
try { |
| 73 |
FC_PY_VIEW_OBJECT |
| 74 |
} |
| 75 |
catch (Py::Exception& e) { |
| 76 |
e.clear(); |
| 77 |
} |
| 78 |
|
| 79 |
this->selectionObserver.~SelectionObserverPythonHandler(); |
| 80 |
} |
| 81 |
|
| 82 |
void ViewProviderFeaturePythonImp::init(PyObject* pyobj) |
| 83 |
{ |
| 84 |
Base::PyGILStateLocker lock; |
| 85 |
has__object__ = !!PyObject_HasAttrString(pyobj, "__object__"); |
| 86 |
|
| 87 |
#undef FC_PY_ELEMENT |
| 88 |
#define FC_PY_ELEMENT(_name) FC_PY_ELEMENT_INIT(_name) |
| 89 |
|
| 90 |
FC_PY_VIEW_OBJECT |
| 91 |
|
| 92 |
this->selectionObserver.init(pyobj); |
| 93 |
} |
| 94 |
|
| 95 |
#define FC_PY_CALL_CHECK(_name) _FC_PY_CALL_CHECK(_name, return (NotImplemented)) |
| 96 |
|
| 97 |
QIcon ViewProviderFeaturePythonImp::getIcon() const |
| 98 |
{ |
| 99 |
_FC_PY_CALL_CHECK(getIcon, return (QIcon())); |
| 100 |
|
| 101 |
// Run the getIcon method of the proxy object. |
| 102 |
Base::PyGILStateLocker lock; |
| 103 |
try { |
| 104 |
Py::Object ret(Base::pyCall(py_getIcon.ptr())); |
| 105 |
if (ret.isNone()) { |
| 106 |
return {}; |
| 107 |
} |
| 108 |
|
| 109 |
if (ret.isString()) { |
| 110 |
std::string content = Py::String(ret).as_std_string("utf-8"); |
| 111 |
QPixmap icon; |
| 112 |
if (BitmapFactory().findPixmapInCache(content.c_str(), icon)) { |
| 113 |
return icon; |
| 114 |
} |
| 115 |
|
| 116 |
// Check if the passed string is a filename, otherwise treat as xpm data |
| 117 |
QFileInfo fi(QString::fromUtf8(content.c_str())); |
| 118 |
if (fi.isFile() && fi.exists()) { |
| 119 |
icon.load(fi.absoluteFilePath()); |
| 120 |
} |
| 121 |
else { |
| 122 |
QByteArray ary; |
| 123 |
int strlen = (int)content.size(); |
| 124 |
ary.resize(strlen); |
| 125 |
for (int j = 0; j < strlen; j++) { |
| 126 |
ary[j] = content[j]; |
| 127 |
} |
| 128 |
// Make sure to remove crap around the XPM data |
| 129 |
QList<QByteArray> lines = ary.split('\n'); |
| 130 |
QByteArray buffer; |
| 131 |
buffer.reserve(ary.size() + lines.size()); |
| 132 |
for (const auto& line : lines) { |
| 133 |
QByteArray trim = line.trimmed(); |
| 134 |
if (!trim.isEmpty()) { |
| 135 |
buffer.append(trim); |
| 136 |
buffer.append('\n'); |
| 137 |
} |
| 138 |
} |
| 139 |
icon.loadFromData(buffer, "XPM"); |
| 140 |
} |
| 141 |
if (!icon.isNull()) { |
| 142 |
return icon; |
| 143 |
} |
| 144 |
} |
| 145 |
else { |
| 146 |
PythonWrapper wrap; |
| 147 |
wrap.loadGuiModule(); |
| 148 |
wrap.loadWidgetsModule(); |
| 149 |
QIcon* picon = wrap.toQIcon(ret.ptr()); |
| 150 |
if (picon) { |
| 151 |
return *picon; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
catch (Py::Exception&) { |
| 156 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 157 |
PyErr_Clear(); |
| 158 |
} |
| 159 |
else { |
| 160 |
Base::PyException e; // extract the Python error text |
| 161 |
e.reportException(); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return {}; |
| 166 |
} |
| 167 |
|
| 168 |
std::map<BitmapFactoryInst::Position, std::string> ViewProviderFeaturePythonImp::getOverlayIcons() const |
| 169 |
{ |
| 170 |
std::map<BitmapFactoryInst::Position, std::string> overlays; |
| 171 |
_FC_PY_CALL_CHECK(getOverlayIcons, return overlays); |
| 172 |
|
| 173 |
Base::PyGILStateLocker lock; |
| 174 |
try { |
| 175 |
Py::Object ret(Base::pyCall(py_getOverlayIcons.ptr())); |
| 176 |
if (ret.isNone()) { |
| 177 |
return overlays; |
| 178 |
} |
| 179 |
|
| 180 |
// Expect a dictionary (dict) from Python |
| 181 |
if (!PyDict_Check(ret.ptr())) { |
| 182 |
return overlays; |
| 183 |
} |
| 184 |
|
| 185 |
Py::Dict dict(ret); |
| 186 |
PyObject *key, *value; |
| 187 |
Py_ssize_t pos = 0; |
| 188 |
|
| 189 |
// Iterate over the dictionary items |
| 190 |
while (PyDict_Next(dict.ptr(), &pos, &key, &value)) { |
| 191 |
// Key should be an integer (from the enum) |
| 192 |
if (!PyLong_Check(key)) { |
| 193 |
continue; |
| 194 |
} |
| 195 |
// Value should be a string |
| 196 |
if (!PyUnicode_Check(value)) { |
| 197 |
continue; |
| 198 |
} |
| 199 |
|
| 200 |
long position_val = PyLong_AsLong(key); |
| 201 |
// Basic validation for the enum range |
| 202 |
if (position_val >= BitmapFactoryInst::TopLeft |
| 203 |
&& position_val <= BitmapFactoryInst::BottomRight) { |
| 204 |
auto position = static_cast<BitmapFactoryInst::Position>(position_val); |
| 205 |
std::string iconName = Py::String(value).as_std_string("utf-8"); |
| 206 |
if (!iconName.empty()) { |
| 207 |
overlays[position] = iconName; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
catch (Py::Exception&) { |
| 213 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 214 |
PyErr_Clear(); |
| 215 |
} |
| 216 |
else { |
| 217 |
Base::PyException e; |
| 218 |
e.reportException(); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
return overlays; |
| 223 |
} |
| 224 |
|
| 225 |
bool ViewProviderFeaturePythonImp::claimChildren(std::vector<App::DocumentObject*>& children) const |
| 226 |
{ |
| 227 |
_FC_PY_CALL_CHECK(claimChildren, return (false)); |
| 228 |
|
| 229 |
Base::PyGILStateLocker lock; |
| 230 |
try { |
| 231 |
Py::Sequence list(Base::pyCall(py_claimChildren.ptr())); |
| 232 |
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { |
| 233 |
PyObject* item = (*it).ptr(); |
| 234 |
if (PyObject_TypeCheck(item, &(App::DocumentObjectPy::Type))) { |
| 235 |
App::DocumentObject* obj |
| 236 |
= static_cast<App::DocumentObjectPy*>(item)->getDocumentObjectPtr(); |
| 237 |
children.push_back(obj); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
catch (Py::Exception&) { |
| 242 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 243 |
PyErr_Clear(); |
| 244 |
return false; |
| 245 |
} |
| 246 |
Base::PyException e; // extract the Python error text |
| 247 |
e.reportException(); |
| 248 |
} |
| 249 |
|
| 250 |
return true; |
| 251 |
} |
| 252 |
|
| 253 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::useNewSelectionModel() const |
| 254 |
{ |
| 255 |
FC_PY_CALL_CHECK(useNewSelectionModel); |
| 256 |
|
| 257 |
// Run the useNewSelectionModel method of the proxy object. |
| 258 |
Base::PyGILStateLocker lock; |
| 259 |
try { |
| 260 |
Py::Boolean ok(Py::Callable(py_useNewSelectionModel).apply(Py::Tuple())); |
| 261 |
return ok ? Accepted : Rejected; |
| 262 |
} |
| 263 |
catch (Py::Exception&) { |
| 264 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 265 |
PyErr_Clear(); |
| 266 |
return NotImplemented; |
| 267 |
} |
| 268 |
Base::PyException e; // extract the Python error text |
| 269 |
e.reportException(); |
| 270 |
} |
| 271 |
|
| 272 |
return Accepted; |
| 273 |
} |
| 274 |
|
| 275 |
void ViewProviderFeaturePythonImp::onSelectionChanged(const SelectionChanges& changes) |
| 276 |
{ |
| 277 |
this->selectionObserver.handleSelectionChanged(changes); |
| 278 |
} |
| 279 |
|
| 280 |
bool ViewProviderFeaturePythonImp::getElement(const SoDetail* det, std::string& res) const |
| 281 |
{ |
| 282 |
_FC_PY_CALL_CHECK(getElement, return (false)); |
| 283 |
|
| 284 |
// Run the onChanged method of the proxy object. |
| 285 |
Base::PyGILStateLocker lock; |
| 286 |
try { |
| 287 |
PyObject* pivy = nullptr; |
| 288 |
// Note: As there is no ref'counting mechanism for the SoDetail class we must |
| 289 |
// pass '0' as the last parameter so that the Python object does not 'own' |
| 290 |
// the detail object. |
| 291 |
pivy = Base::Interpreter().createSWIGPointerObj( |
| 292 |
"pivy.coin", |
| 293 |
"SoDetail *", |
| 294 |
const_cast<void*>(static_cast<const void*>(det)), |
| 295 |
0 |
| 296 |
); |
| 297 |
Py::Tuple args(1); |
| 298 |
args.setItem(0, Py::Object(pivy, true)); |
| 299 |
Py::String name(Base::pyCall(py_getElement.ptr(), args.ptr())); |
| 300 |
res = name; |
| 301 |
return true; |
| 302 |
} |
| 303 |
catch (const Base::Exception& e) { |
| 304 |
e.reportException(); |
| 305 |
} |
| 306 |
catch (Py::Exception&) { |
| 307 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 308 |
PyErr_Clear(); |
| 309 |
return false; |
| 310 |
} |
| 311 |
Base::PyException e; // extract the Python error text |
| 312 |
e.reportException(); |
| 313 |
} |
| 314 |
|
| 315 |
return true; |
| 316 |
} |
| 317 |
|
| 318 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::getElementPicked( |
| 319 |
const SoPickedPoint* pp, |
| 320 |
std::string& subname |
| 321 |
) const |
| 322 |
{ |
| 323 |
FC_PY_CALL_CHECK(getElementPicked); |
| 324 |
|
| 325 |
Base::PyGILStateLocker lock; |
| 326 |
try { |
| 327 |
PyObject* pivy = nullptr; |
| 328 |
pivy = Base::Interpreter().createSWIGPointerObj( |
| 329 |
"pivy.coin", |
| 330 |
"SoPickedPoint *", |
| 331 |
const_cast<void*>(static_cast<const void*>(pp)), |
| 332 |
0 |
| 333 |
); |
| 334 |
Py::Tuple args(1); |
| 335 |
args.setItem(0, Py::Object(pivy, true)); |
| 336 |
Py::Object ret(Base::pyCall(py_getElementPicked.ptr(), args.ptr())); |
| 337 |
if (!ret.isString()) { |
| 338 |
return Rejected; |
| 339 |
} |
| 340 |
subname = ret.as_string(); |
| 341 |
return Accepted; |
| 342 |
} |
| 343 |
catch (const Base::Exception& e) { |
| 344 |
e.reportException(); |
| 345 |
} |
| 346 |
catch (Py::Exception&) { |
| 347 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 348 |
PyErr_Clear(); |
| 349 |
return NotImplemented; |
| 350 |
} |
| 351 |
Base::PyException e; // extract the Python error text |
| 352 |
e.reportException(); |
| 353 |
} |
| 354 |
|
| 355 |
return Rejected; |
| 356 |
} |
| 357 |
|
| 358 |
bool ViewProviderFeaturePythonImp::getDetail(const char* name, SoDetail*& det) const |
| 359 |
{ |
| 360 |
_FC_PY_CALL_CHECK(getDetail, return (false)); |
| 361 |
|
| 362 |
// Run the onChanged method of the proxy object. |
| 363 |
Base::PyGILStateLocker lock; |
| 364 |
try { |
| 365 |
Py::Tuple args(1); |
| 366 |
args.setItem(0, Py::String(name)); |
| 367 |
Py::Object pydet(Base::pyCall(py_getDetail.ptr(), args.ptr())); |
| 368 |
void* ptr = nullptr; |
| 369 |
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoDetail *", pydet.ptr(), &ptr, 0); |
| 370 |
auto detail = static_cast<SoDetail*>(ptr); |
| 371 |
det = detail ? detail->copy() : nullptr; |
| 372 |
return true; |
| 373 |
} |
| 374 |
catch (const Base::Exception& e) { |
| 375 |
e.reportException(); |
| 376 |
} |
| 377 |
catch (Py::Exception&) { |
| 378 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 379 |
PyErr_Clear(); |
| 380 |
return false; |
| 381 |
} |
| 382 |
Base::PyException e; // extract the Python error text |
| 383 |
e.reportException(); |
| 384 |
} |
| 385 |
|
| 386 |
return true; |
| 387 |
} |
| 388 |
|
| 389 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::getDetailPath( |
| 390 |
const char* name, |
| 391 |
SoFullPath* path, |
| 392 |
bool append, |
| 393 |
SoDetail*& det |
| 394 |
) const |
| 395 |
{ |
| 396 |
FC_PY_CALL_CHECK(getDetailPath); |
| 397 |
|
| 398 |
Base::PyGILStateLocker lock; |
| 399 |
auto length = path->getLength(); |
| 400 |
try { |
| 401 |
PyObject* pivy = nullptr; |
| 402 |
pivy = Base::Interpreter() |
| 403 |
.createSWIGPointerObj("pivy.coin", "SoFullPath *", static_cast<void*>(path), 1); |
| 404 |
path->ref(); |
| 405 |
Py::Tuple args(3); |
| 406 |
args.setItem(0, Py::String(name)); |
| 407 |
args.setItem(1, Py::Object(pivy, true)); |
| 408 |
args.setItem(2, Py::Boolean(append)); |
| 409 |
Py::Object pyDet(Base::pyCall(py_getDetailPath.ptr(), args.ptr())); |
| 410 |
if (!pyDet.isTrue()) { |
| 411 |
return Rejected; |
| 412 |
} |
| 413 |
if (pyDet.isBoolean()) { |
| 414 |
return Accepted; |
| 415 |
} |
| 416 |
void* ptr = nullptr; |
| 417 |
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoDetail *", pyDet.ptr(), &ptr, 0); |
| 418 |
auto detail = static_cast<SoDetail*>(ptr); |
| 419 |
det = detail ? detail->copy() : nullptr; |
| 420 |
if (det) { |
| 421 |
return Accepted; |
| 422 |
} |
| 423 |
delete det; |
| 424 |
} |
| 425 |
catch (const Base::Exception& e) { |
| 426 |
e.reportException(); |
| 427 |
} |
| 428 |
catch (Py::Exception&) { |
| 429 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 430 |
PyErr_Clear(); |
| 431 |
return NotImplemented; |
| 432 |
} |
| 433 |
Base::PyException e; // extract the Python error text |
| 434 |
e.reportException(); |
| 435 |
} |
| 436 |
path->truncate(length); |
| 437 |
return Rejected; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
std::vector<Base::Vector3d> ViewProviderFeaturePythonImp::getSelectionShape(const char* /*Element*/) const |
| 442 |
{ |
| 443 |
return {}; |
| 444 |
} |
| 445 |
|
| 446 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::setEdit(int ModNum) |
| 447 |
{ |
| 448 |
FC_PY_CALL_CHECK(setEdit) |
| 449 |
|
| 450 |
Base::PyGILStateLocker lock; |
| 451 |
try { |
| 452 |
if (has__object__) { |
| 453 |
Py::Tuple args(1); |
| 454 |
args.setItem(0, Py::Long(ModNum)); |
| 455 |
Py::Object ret(Base::pyCall(py_setEdit.ptr(), args.ptr())); |
| 456 |
if (ret.isNone()) { |
| 457 |
return NotImplemented; |
| 458 |
} |
| 459 |
Py::Boolean ok(ret); |
| 460 |
bool value = static_cast<bool>(ok); |
| 461 |
return value ? Accepted : Rejected; |
| 462 |
} |
| 463 |
else { |
| 464 |
Py::Tuple args(2); |
| 465 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 466 |
args.setItem(1, Py::Long(ModNum)); |
| 467 |
Py::Object ret(Base::pyCall(py_setEdit.ptr(), args.ptr())); |
| 468 |
if (ret.isNone()) { |
| 469 |
return NotImplemented; |
| 470 |
} |
| 471 |
Py::Boolean ok(ret); |
| 472 |
bool value = static_cast<bool>(ok); |
| 473 |
return value ? Accepted : Rejected; |
| 474 |
} |
| 475 |
} |
| 476 |
catch (Py::Exception&) { |
| 477 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 478 |
PyErr_Clear(); |
| 479 |
return NotImplemented; |
| 480 |
} |
| 481 |
Base::PyException e; // extract the Python error text |
| 482 |
e.reportException(); |
| 483 |
} |
| 484 |
|
| 485 |
return Rejected; |
| 486 |
} |
| 487 |
|
| 488 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::unsetEdit(int ModNum) |
| 489 |
{ |
| 490 |
FC_PY_CALL_CHECK(unsetEdit) |
| 491 |
|
| 492 |
Base::PyGILStateLocker lock; |
| 493 |
try { |
| 494 |
if (has__object__) { |
| 495 |
Py::Tuple args(1); |
| 496 |
args.setItem(0, Py::Long(ModNum)); |
| 497 |
Py::Object ret(Base::pyCall(py_unsetEdit.ptr(), args.ptr())); |
| 498 |
if (ret.isNone()) { |
| 499 |
return NotImplemented; |
| 500 |
} |
| 501 |
Py::Boolean ok(ret); |
| 502 |
bool value = static_cast<bool>(ok); |
| 503 |
return value ? Accepted : Rejected; |
| 504 |
} |
| 505 |
else { |
| 506 |
Py::Tuple args(2); |
| 507 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 508 |
args.setItem(1, Py::Long(ModNum)); |
| 509 |
Py::Object ret(Base::pyCall(py_unsetEdit.ptr(), args.ptr())); |
| 510 |
if (ret.isNone()) { |
| 511 |
return NotImplemented; |
| 512 |
} |
| 513 |
Py::Boolean ok(ret); |
| 514 |
bool value = static_cast<bool>(ok); |
| 515 |
return value ? Accepted : Rejected; |
| 516 |
} |
| 517 |
} |
| 518 |
catch (Py::Exception&) { |
| 519 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 520 |
PyErr_Clear(); |
| 521 |
return NotImplemented; |
| 522 |
} |
| 523 |
Base::PyException e; // extract the Python error text |
| 524 |
e.reportException(); |
| 525 |
} |
| 526 |
|
| 527 |
return Rejected; |
| 528 |
} |
| 529 |
|
| 530 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::setEditViewer( |
| 531 |
View3DInventorViewer* viewer, |
| 532 |
int ModNum |
| 533 |
) |
| 534 |
{ |
| 535 |
FC_PY_CALL_CHECK(setEditViewer) |
| 536 |
|
| 537 |
Base::PyGILStateLocker lock; |
| 538 |
try { |
| 539 |
Py::Tuple args(3); |
| 540 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 541 |
args.setItem(1, Py::Object(viewer->getPyObject(), true)); |
| 542 |
args.setItem(2, Py::Long(ModNum)); |
| 543 |
Py::Object ret(Base::pyCall(py_setEditViewer.ptr(), args.ptr())); |
| 544 |
return ret.isTrue() ? Accepted : Rejected; |
| 545 |
} |
| 546 |
catch (Py::Exception&) { |
| 547 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 548 |
PyErr_Clear(); |
| 549 |
return NotImplemented; |
| 550 |
} |
| 551 |
Base::PyException e; // extract the Python error text |
| 552 |
e.reportException(); |
| 553 |
} |
| 554 |
return Rejected; |
| 555 |
} |
| 556 |
|
| 557 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::unsetEditViewer( |
| 558 |
View3DInventorViewer* viewer |
| 559 |
) |
| 560 |
{ |
| 561 |
FC_PY_CALL_CHECK(unsetEditViewer) |
| 562 |
|
| 563 |
// Run the onChanged method of the proxy object. |
| 564 |
Base::PyGILStateLocker lock; |
| 565 |
try { |
| 566 |
Py::Tuple args(2); |
| 567 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 568 |
args.setItem(1, Py::Object(viewer->getPyObject(), true)); |
| 569 |
Py::Object ret(Base::pyCall(py_unsetEditViewer.ptr(), args.ptr())); |
| 570 |
return ret.isTrue() ? Accepted : Rejected; |
| 571 |
} |
| 572 |
catch (Py::Exception&) { |
| 573 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 574 |
PyErr_Clear(); |
| 575 |
return NotImplemented; |
| 576 |
} |
| 577 |
Base::PyException e; // extract the Python error text |
| 578 |
e.reportException(); |
| 579 |
} |
| 580 |
return Rejected; |
| 581 |
} |
| 582 |
|
| 583 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::doubleClicked() |
| 584 |
{ |
| 585 |
FC_PY_CALL_CHECK(doubleClicked) |
| 586 |
|
| 587 |
// Run the onChanged method of the proxy object. |
| 588 |
Base::PyGILStateLocker lock; |
| 589 |
try { |
| 590 |
if (has__object__) { |
| 591 |
Py::Boolean ok(Base::pyCall(py_doubleClicked.ptr())); |
| 592 |
bool value = (bool)ok; |
| 593 |
return value ? Accepted : Rejected; |
| 594 |
} |
| 595 |
else { |
| 596 |
Py::Tuple args(1); |
| 597 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 598 |
Py::Boolean ok(Base::pyCall(py_doubleClicked.ptr(), args.ptr())); |
| 599 |
bool value = (bool)ok; |
| 600 |
return value ? Accepted : Rejected; |
| 601 |
} |
| 602 |
} |
| 603 |
catch (Py::Exception&) { |
| 604 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 605 |
PyErr_Clear(); |
| 606 |
return NotImplemented; |
| 607 |
} |
| 608 |
Base::PyException e; // extract the Python error text |
| 609 |
e.reportException(); |
| 610 |
} |
| 611 |
|
| 612 |
return Rejected; |
| 613 |
} |
| 614 |
|
| 615 |
bool ViewProviderFeaturePythonImp::setupContextMenu(QMenu* menu) |
| 616 |
{ |
| 617 |
_FC_PY_CALL_CHECK(setupContextMenu, return (false)); |
| 618 |
|
| 619 |
// Run the attach method of the proxy object. |
| 620 |
Base::PyGILStateLocker lock; |
| 621 |
try { |
| 622 |
if (has__object__) { |
| 623 |
PythonWrapper wrap; |
| 624 |
wrap.loadGuiModule(); |
| 625 |
wrap.loadWidgetsModule(); |
| 626 |
Py::Tuple args(1); |
| 627 |
args.setItem(0, wrap.fromQWidget(menu, "QMenu")); |
| 628 |
return Base::pyCall(py_setupContextMenu.ptr(), args.ptr()).isTrue(); |
| 629 |
} |
| 630 |
else { |
| 631 |
PythonWrapper wrap; |
| 632 |
wrap.loadGuiModule(); |
| 633 |
wrap.loadWidgetsModule(); |
| 634 |
Py::Tuple args(2); |
| 635 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 636 |
args.setItem(1, wrap.fromQWidget(menu, "QMenu")); |
| 637 |
return Base::pyCall(py_setupContextMenu.ptr(), args.ptr()).isTrue(); |
| 638 |
} |
| 639 |
} |
| 640 |
catch (Py::Exception&) { |
| 641 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 642 |
PyErr_Clear(); |
| 643 |
return false; |
| 644 |
} |
| 645 |
Base::PyException e; // extract the Python error text |
| 646 |
e.reportException(); |
| 647 |
} |
| 648 |
return true; |
| 649 |
} |
| 650 |
|
| 651 |
void ViewProviderFeaturePythonImp::attach(App::DocumentObject* pcObject) |
| 652 |
{ |
| 653 |
_FC_PY_CALL_CHECK(attach, return); |
| 654 |
|
| 655 |
// Run the attach method of the proxy object. |
| 656 |
Base::PyGILStateLocker lock; |
| 657 |
try { |
| 658 |
if (has__object__) { |
| 659 |
Base::pyCall(py_attach.ptr()); |
| 660 |
} |
| 661 |
else { |
| 662 |
Py::Tuple args(1); |
| 663 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 664 |
Base::pyCall(py_attach.ptr(), args.ptr()); |
| 665 |
} |
| 666 |
|
| 667 |
// #0000415: Now simulate a property change event to call |
| 668 |
// claimChildren if implemented. |
| 669 |
pcObject->Label.touch(); |
| 670 |
} |
| 671 |
catch (Py::Exception&) { |
| 672 |
Base::PyException e; // extract the Python error text |
| 673 |
e.reportException(); |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
void ViewProviderFeaturePythonImp::updateData(const App::Property* prop) |
| 678 |
{ |
| 679 |
if (py_updateData.isNone()) { |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
// Run the updateData method of the proxy object. |
| 684 |
Base::PyGILStateLocker lock; |
| 685 |
try { |
| 686 |
if (has__object__) { |
| 687 |
Py::Tuple args(1); |
| 688 |
const char* prop_name = object->getObject()->getPropertyName(prop); |
| 689 |
if (prop_name) { |
| 690 |
args.setItem(0, Py::String(prop_name)); |
| 691 |
Base::pyCall(py_updateData.ptr(), args.ptr()); |
| 692 |
} |
| 693 |
} |
| 694 |
else { |
| 695 |
Py::Tuple args(2); |
| 696 |
args.setItem(0, Py::Object(object->getObject()->getPyObject(), true)); |
| 697 |
const char* prop_name = object->getObject()->getPropertyName(prop); |
| 698 |
if (prop_name) { |
| 699 |
args.setItem(1, Py::String(prop_name)); |
| 700 |
Base::pyCall(py_updateData.ptr(), args.ptr()); |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
catch (Py::Exception&) { |
| 705 |
Base::PyException e; // extract the Python error text |
| 706 |
e.reportException(); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
void ViewProviderFeaturePythonImp::onChanged(const App::Property* prop) |
| 711 |
{ |
| 712 |
if (py_onChanged.isNone()) { |
| 713 |
return; |
| 714 |
} |
| 715 |
|
| 716 |
// Run the onChanged method of the proxy object. |
| 717 |
Base::PyGILStateLocker lock; |
| 718 |
try { |
| 719 |
if (has__object__) { |
| 720 |
Py::Tuple args(1); |
| 721 |
const char* prop_name = object->getPropertyName(prop); |
| 722 |
if (prop_name) { |
| 723 |
args.setItem(0, Py::String(prop_name)); |
| 724 |
Base::pyCall(py_onChanged.ptr(), args.ptr()); |
| 725 |
} |
| 726 |
} |
| 727 |
else { |
| 728 |
Py::Tuple args(2); |
| 729 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 730 |
const char* prop_name = object->getPropertyName(prop); |
| 731 |
if (prop_name) { |
| 732 |
args.setItem(1, Py::String(prop_name)); |
| 733 |
Base::pyCall(py_onChanged.ptr(), args.ptr()); |
| 734 |
} |
| 735 |
} |
| 736 |
} |
| 737 |
catch (Py::Exception&) { |
| 738 |
Base::PyException e; // extract the Python error text |
| 739 |
e.reportException(); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
void ViewProviderFeaturePythonImp::onBeforeChange(const App::Property* prop) |
| 744 |
{ |
| 745 |
if (py_onBeforeChange.isNone()) { |
| 746 |
return; |
| 747 |
} |
| 748 |
|
| 749 |
// Run the onChanged method of the proxy object. |
| 750 |
Base::PyGILStateLocker lock; |
| 751 |
try { |
| 752 |
if (has__object__) { |
| 753 |
Py::Tuple args(1); |
| 754 |
const char* prop_name = object->getPropertyName(prop); |
| 755 |
if (prop_name) { |
| 756 |
args.setItem(0, Py::String(prop_name)); |
| 757 |
Base::pyCall(py_onBeforeChange.ptr(), args.ptr()); |
| 758 |
} |
| 759 |
} |
| 760 |
else { |
| 761 |
Py::Tuple args(2); |
| 762 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 763 |
const char* prop_name = object->getPropertyName(prop); |
| 764 |
if (prop_name) { |
| 765 |
args.setItem(1, Py::String(prop_name)); |
| 766 |
Base::pyCall(py_onBeforeChange.ptr(), args.ptr()); |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
catch (Py::Exception&) { |
| 771 |
Base::PyException e; // extract the Python error text |
| 772 |
e.reportException(); |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
void ViewProviderFeaturePythonImp::startRestoring() |
| 777 |
{} |
| 778 |
|
| 779 |
void ViewProviderFeaturePythonImp::finishRestoring() |
| 780 |
{ |
| 781 |
Base::PyGILStateLocker lock; |
| 782 |
try { |
| 783 |
Py::Object vp = Proxy.getValue(); |
| 784 |
if (vp.isNone()) { |
| 785 |
object->show(); |
| 786 |
Proxy.setValue(Py::Long(1)); |
| 787 |
} |
| 788 |
else { |
| 789 |
_FC_PY_CALL_CHECK(finishRestoring, return); |
| 790 |
Base::pyCall(py_finishRestoring.ptr()); |
| 791 |
} |
| 792 |
} |
| 793 |
catch (Py::Exception&) { |
| 794 |
Base::PyException e; // extract the Python error text |
| 795 |
e.reportException(); |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
void ViewProviderFeaturePythonImp::beforeDelete() |
| 800 |
{ |
| 801 |
_FC_PY_CALL_CHECK(beforeDelete, return); |
| 802 |
|
| 803 |
Base::PyGILStateLocker lock; |
| 804 |
try { |
| 805 |
if (has__object__) { |
| 806 |
Base::pyCall(py_beforeDelete.ptr()); |
| 807 |
} |
| 808 |
else { |
| 809 |
Py::Tuple args(1); |
| 810 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 811 |
Base::pyCall(py_beforeDelete.ptr(), args.ptr()); |
| 812 |
} |
| 813 |
} |
| 814 |
catch (Py::Exception&) { |
| 815 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 816 |
PyErr_Clear(); |
| 817 |
} |
| 818 |
else { |
| 819 |
Base::PyException e; |
| 820 |
e.reportException(); |
| 821 |
} |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::onDelete( |
| 826 |
const std::vector<std::string>& sub |
| 827 |
) |
| 828 |
{ |
| 829 |
FC_PY_CALL_CHECK(onDelete); |
| 830 |
|
| 831 |
Base::PyGILStateLocker lock; |
| 832 |
try { |
| 833 |
Py::Tuple seq(sub.size()); |
| 834 |
int index = 0; |
| 835 |
for (const auto& it : sub) { |
| 836 |
seq.setItem(index++, Py::String(it)); |
| 837 |
} |
| 838 |
|
| 839 |
if (has__object__) { |
| 840 |
Py::Tuple args(1); |
| 841 |
args.setItem(0, seq); |
| 842 |
Py::Boolean ok(Base::pyCall(py_onDelete.ptr(), args.ptr())); |
| 843 |
return ok ? Accepted : Rejected; |
| 844 |
} |
| 845 |
else { |
| 846 |
Py::Tuple args(2); |
| 847 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 848 |
args.setItem(1, seq); |
| 849 |
Py::Boolean ok(Base::pyCall(py_onDelete.ptr(), args.ptr())); |
| 850 |
return ok ? Accepted : Rejected; |
| 851 |
} |
| 852 |
} |
| 853 |
catch (Py::Exception&) { |
| 854 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 855 |
PyErr_Clear(); |
| 856 |
return NotImplemented; |
| 857 |
} |
| 858 |
Base::PyException e; // extract the Python error text |
| 859 |
e.reportException(); |
| 860 |
return Rejected; |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::canDelete( |
| 865 |
App::DocumentObject* obj |
| 866 |
) const |
| 867 |
{ |
| 868 |
FC_PY_CALL_CHECK(canDelete); |
| 869 |
|
| 870 |
Base::PyGILStateLocker lock; |
| 871 |
try { |
| 872 |
Py::Tuple args(1); |
| 873 |
args.setItem(0, obj ? Py::Object(obj->getPyObject(), true) : Py::Object()); |
| 874 |
return Py::Boolean(Base::pyCall(py_canDelete.ptr(), args.ptr())) ? Accepted : Rejected; |
| 875 |
} |
| 876 |
catch (Py::Exception&) { |
| 877 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 878 |
PyErr_Clear(); |
| 879 |
return NotImplemented; |
| 880 |
} |
| 881 |
Base::PyException e; // extract the Python error text |
| 882 |
e.reportException(); |
| 883 |
return Rejected; |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::canAddToSceneGraph() const |
| 888 |
{ |
| 889 |
FC_PY_CALL_CHECK(canAddToSceneGraph); |
| 890 |
|
| 891 |
Base::PyGILStateLocker lock; |
| 892 |
try { |
| 893 |
return Py::Boolean(Py::Callable(py_canAddToSceneGraph).apply(Py::Tuple())) ? Accepted |
| 894 |
: Rejected; |
| 895 |
} |
| 896 |
catch (Py::Exception&) { |
| 897 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 898 |
PyErr_Clear(); |
| 899 |
return NotImplemented; |
| 900 |
} |
| 901 |
Base::PyException e; // extract the Python error text |
| 902 |
e.reportException(); |
| 903 |
} |
| 904 |
return Accepted; |
| 905 |
} |
| 906 |
|
| 907 |
bool ViewProviderFeaturePythonImp::getDefaultDisplayMode(std::string& mode) const |
| 908 |
{ |
| 909 |
_FC_PY_CALL_CHECK(getDefaultDisplayMode, return (0)); |
| 910 |
|
| 911 |
// Run the getDefaultDisplayMode method of the proxy object. |
| 912 |
Base::PyGILStateLocker lock; |
| 913 |
try { |
| 914 |
Py::String str(Base::pyCall(py_getDefaultDisplayMode.ptr())); |
| 915 |
mode = str.as_std_string("ascii"); |
| 916 |
return true; |
| 917 |
} |
| 918 |
catch (Py::Exception&) { |
| 919 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 920 |
PyErr_Clear(); |
| 921 |
return false; |
| 922 |
} |
| 923 |
Base::PyException e; // extract the Python error text |
| 924 |
e.reportException(); |
| 925 |
} |
| 926 |
|
| 927 |
return true; |
| 928 |
} |
| 929 |
|
| 930 |
std::vector<std::string> ViewProviderFeaturePythonImp::getDisplayModes() const |
| 931 |
{ |
| 932 |
std::vector<std::string> modes; |
| 933 |
_FC_PY_CALL_CHECK(getDisplayModes, return (modes)); |
| 934 |
|
| 935 |
// Run the getDisplayModes method of the proxy object. |
| 936 |
Base::PyGILStateLocker lock; |
| 937 |
try { |
| 938 |
if (has__object__) { |
| 939 |
Py::Sequence list(Base::pyCall(py_getDisplayModes.ptr())); |
| 940 |
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { |
| 941 |
Py::String str(*it); |
| 942 |
modes.push_back(str.as_std_string("ascii")); |
| 943 |
} |
| 944 |
} |
| 945 |
else { |
| 946 |
Py::Tuple args(1); |
| 947 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 948 |
Py::Sequence list(Base::pyCall(py_getDisplayModes.ptr(), args.ptr())); |
| 949 |
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { |
| 950 |
Py::String str(*it); |
| 951 |
modes.push_back(str.as_std_string("ascii")); |
| 952 |
} |
| 953 |
} |
| 954 |
} |
| 955 |
catch (Py::Exception&) { |
| 956 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 957 |
PyErr_Clear(); |
| 958 |
} |
| 959 |
else { |
| 960 |
Base::PyException e; // extract the Python error text |
| 961 |
e.reportException(); |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
return modes; |
| 966 |
} |
| 967 |
|
| 968 |
std::string ViewProviderFeaturePythonImp::setDisplayMode(const char* ModeName) |
| 969 |
{ |
| 970 |
_FC_PY_CALL_CHECK(setDisplayMode, return (ModeName)); |
| 971 |
|
| 972 |
// Run the setDisplayMode method of the proxy object. |
| 973 |
Base::PyGILStateLocker lock; |
| 974 |
try { |
| 975 |
Py::Tuple args(1); |
| 976 |
args.setItem(0, Py::String(ModeName)); |
| 977 |
Py::String str(Base::pyCall(py_setDisplayMode.ptr(), args.ptr())); |
| 978 |
return str.as_std_string("ascii"); |
| 979 |
} |
| 980 |
catch (Py::Exception&) { |
| 981 |
Base::PyException e; // extract the Python error text |
| 982 |
e.reportException(); |
| 983 |
} |
| 984 |
|
| 985 |
return ModeName; |
| 986 |
} |
| 987 |
|
| 988 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::canDragObjects() const |
| 989 |
{ |
| 990 |
FC_PY_CALL_CHECK(canDragObjects); |
| 991 |
|
| 992 |
Base::PyGILStateLocker lock; |
| 993 |
try { |
| 994 |
Py::Boolean ok(Base::pyCall(py_canDragObjects.ptr())); |
| 995 |
return static_cast<bool>(ok) ? Accepted : Rejected; |
| 996 |
} |
| 997 |
catch (Py::Exception&) { |
| 998 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 999 |
PyErr_Clear(); |
| 1000 |
return NotImplemented; |
| 1001 |
} |
| 1002 |
Base::PyException e; // extract the Python error text |
| 1003 |
e.reportException(); |
| 1004 |
} |
| 1005 |
|
| 1006 |
return Rejected; |
| 1007 |
} |
| 1008 |
|
| 1009 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::canDragObject( |
| 1010 |
App::DocumentObject* obj |
| 1011 |
) const |
| 1012 |
{ |
| 1013 |
FC_PY_CALL_CHECK(canDragObject); |
| 1014 |
|
| 1015 |
Base::PyGILStateLocker lock; |
| 1016 |
try { |
| 1017 |
Py::Tuple args(1); |
| 1018 |
args.setItem(0, Py::Object(obj->getPyObject(), true)); |
| 1019 |
Py::Boolean ok(Base::pyCall(py_canDragObject.ptr(), args.ptr())); |
| 1020 |
return static_cast<bool>(ok) ? Accepted : Rejected; |
| 1021 |
} |
| 1022 |
catch (Py::Exception&) { |
| 1023 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1024 |
PyErr_Clear(); |
| 1025 |
return NotImplemented; |
| 1026 |
} |
| 1027 |
Base::PyException e; // extract the Python error text |
| 1028 |
e.reportException(); |
| 1029 |
} |
| 1030 |
|
| 1031 |
return Rejected; |
| 1032 |
} |
| 1033 |
|
| 1034 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::dragObject(App::DocumentObject* obj) |
| 1035 |
{ |
| 1036 |
FC_PY_CALL_CHECK(dragObject); |
| 1037 |
|
| 1038 |
// Run the onChanged method of the proxy object. |
| 1039 |
Base::PyGILStateLocker lock; |
| 1040 |
try { |
| 1041 |
if (has__object__) { |
| 1042 |
Py::Tuple args(1); |
| 1043 |
args.setItem(0, Py::Object(obj->getPyObject(), true)); |
| 1044 |
Base::pyCall(py_dragObject.ptr(), args.ptr()); |
| 1045 |
return Accepted; |
| 1046 |
} |
| 1047 |
else { |
| 1048 |
Py::Tuple args(2); |
| 1049 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 1050 |
args.setItem(1, Py::Object(obj->getPyObject(), true)); |
| 1051 |
Base::pyCall(py_dragObject.ptr(), args.ptr()); |
| 1052 |
return Accepted; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
catch (Py::Exception&) { |
| 1056 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1057 |
PyErr_Clear(); |
| 1058 |
return NotImplemented; |
| 1059 |
} |
| 1060 |
Base::PyException e; // extract the Python error text |
| 1061 |
e.reportException(); |
| 1062 |
} |
| 1063 |
|
| 1064 |
return Rejected; |
| 1065 |
} |
| 1066 |
|
| 1067 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::canDropObjects() const |
| 1068 |
{ |
| 1069 |
FC_PY_CALL_CHECK(canDropObjects); |
| 1070 |
|
| 1071 |
Base::PyGILStateLocker lock; |
| 1072 |
try { |
| 1073 |
Py::Boolean ok(Base::pyCall(py_canDropObjects.ptr())); |
| 1074 |
return static_cast<bool>(ok) ? Accepted : Rejected; |
| 1075 |
} |
| 1076 |
catch (Py::Exception&) { |
| 1077 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1078 |
PyErr_Clear(); |
| 1079 |
return NotImplemented; |
| 1080 |
} |
| 1081 |
Base::PyException e; // extract the Python error text |
| 1082 |
e.reportException(); |
| 1083 |
} |
| 1084 |
|
| 1085 |
return Rejected; |
| 1086 |
} |
| 1087 |
|
| 1088 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::canDropObject( |
| 1089 |
App::DocumentObject* obj |
| 1090 |
) const |
| 1091 |
{ |
| 1092 |
FC_PY_CALL_CHECK(canDropObject); |
| 1093 |
|
| 1094 |
Base::PyGILStateLocker lock; |
| 1095 |
try { |
| 1096 |
Py::Tuple args(1); |
| 1097 |
args.setItem(0, Py::Object(obj->getPyObject(), true)); |
| 1098 |
Py::Boolean ok(Base::pyCall(py_canDropObject.ptr(), args.ptr())); |
| 1099 |
return static_cast<bool>(ok) ? Accepted : Rejected; |
| 1100 |
} |
| 1101 |
catch (Py::Exception&) { |
| 1102 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1103 |
PyErr_Clear(); |
| 1104 |
return NotImplemented; |
| 1105 |
} |
| 1106 |
Base::PyException e; // extract the Python error text |
| 1107 |
e.reportException(); |
| 1108 |
} |
| 1109 |
|
| 1110 |
return Rejected; |
| 1111 |
} |
| 1112 |
|
| 1113 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::dropObject(App::DocumentObject* obj) |
| 1114 |
{ |
| 1115 |
FC_PY_CALL_CHECK(dropObject); |
| 1116 |
|
| 1117 |
Base::PyGILStateLocker lock; |
| 1118 |
try { |
| 1119 |
if (has__object__) { |
| 1120 |
Py::Tuple args(1); |
| 1121 |
args.setItem(0, Py::Object(obj->getPyObject(), true)); |
| 1122 |
Base::pyCall(py_dropObject.ptr(), args.ptr()); |
| 1123 |
return Accepted; |
| 1124 |
} |
| 1125 |
else { |
| 1126 |
Py::Tuple args(2); |
| 1127 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 1128 |
args.setItem(1, Py::Object(obj->getPyObject(), true)); |
| 1129 |
Base::pyCall(py_dropObject.ptr(), args.ptr()); |
| 1130 |
return Accepted; |
| 1131 |
} |
| 1132 |
} |
| 1133 |
catch (Py::Exception&) { |
| 1134 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1135 |
PyErr_Clear(); |
| 1136 |
return NotImplemented; |
| 1137 |
} |
| 1138 |
Base::PyException::throwException(); |
| 1139 |
} |
| 1140 |
|
| 1141 |
return Rejected; |
| 1142 |
} |
| 1143 |
|
| 1144 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::canDragAndDropObject( |
| 1145 |
App::DocumentObject* obj |
| 1146 |
) const |
| 1147 |
{ |
| 1148 |
FC_PY_CALL_CHECK(canDragAndDropObject); |
| 1149 |
|
| 1150 |
Base::PyGILStateLocker lock; |
| 1151 |
try { |
| 1152 |
Py::TupleN args(Py::Object(obj->getPyObject(), true)); |
| 1153 |
Py::Boolean ok(Base::pyCall(py_canDragAndDropObject.ptr(), args.ptr())); |
| 1154 |
return static_cast<bool>(ok) ? Accepted : Rejected; |
| 1155 |
} |
| 1156 |
catch (Py::Exception&) { |
| 1157 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1158 |
PyErr_Clear(); |
| 1159 |
return NotImplemented; |
| 1160 |
} |
| 1161 |
Base::PyException e; // extract the Python error text |
| 1162 |
e.reportException(); |
| 1163 |
} |
| 1164 |
|
| 1165 |
return Rejected; |
| 1166 |
} |
| 1167 |
|
| 1168 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::canDropObjectEx( |
| 1169 |
App::DocumentObject* obj, |
| 1170 |
App::DocumentObject* owner, |
| 1171 |
const char* subname, |
| 1172 |
const std::vector<std::string>& elements |
| 1173 |
) const |
| 1174 |
{ |
| 1175 |
FC_PY_CALL_CHECK(canDropObjectEx); |
| 1176 |
|
| 1177 |
Base::PyGILStateLocker lock; |
| 1178 |
try { |
| 1179 |
Py::Tuple args(4); |
| 1180 |
args.setItem(0, Py::Object(obj->getPyObject(), true)); |
| 1181 |
args.setItem(1, owner ? Py::Object(owner->getPyObject(), true) : Py::None()); |
| 1182 |
args.setItem(2, Py::String(subname ? subname : "")); |
| 1183 |
Py::Tuple tuple(elements.size()); |
| 1184 |
int i = 0; |
| 1185 |
for (auto& element : elements) { |
| 1186 |
tuple.setItem(i++, Py::String(element)); |
| 1187 |
} |
| 1188 |
args.setItem(3, tuple); |
| 1189 |
Py::Boolean ok(Base::pyCall(py_canDropObjectEx.ptr(), args.ptr())); |
| 1190 |
return static_cast<bool>(ok) ? Accepted : Rejected; |
| 1191 |
} |
| 1192 |
catch (Py::Exception&) { |
| 1193 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1194 |
PyErr_Clear(); |
| 1195 |
return NotImplemented; |
| 1196 |
} |
| 1197 |
Base::PyException e; // extract the Python error text |
| 1198 |
e.reportException(); |
| 1199 |
} |
| 1200 |
|
| 1201 |
return Rejected; |
| 1202 |
} |
| 1203 |
|
| 1204 |
bool ViewProviderFeaturePythonImp::dropObjectEx( |
| 1205 |
App::DocumentObject* obj, |
| 1206 |
App::DocumentObject* owner, |
| 1207 |
const char* subname, |
| 1208 |
const std::vector<std::string>& elements, |
| 1209 |
std::string& ret |
| 1210 |
) |
| 1211 |
{ |
| 1212 |
_FC_PY_CALL_CHECK(dropObjectEx, return (false)); |
| 1213 |
|
| 1214 |
Base::PyGILStateLocker lock; |
| 1215 |
try { |
| 1216 |
Py::Tuple tuple(elements.size()); |
| 1217 |
int i = 0; |
| 1218 |
for (auto& element : elements) { |
| 1219 |
tuple.setItem(i++, Py::String(element)); |
| 1220 |
} |
| 1221 |
Py::Object res; |
| 1222 |
Py::TupleN args( |
| 1223 |
Py::Object(object->getPyObject(), true), |
| 1224 |
Py::Object(obj->getPyObject(), true), |
| 1225 |
owner ? Py::Object(owner->getPyObject(), true) : Py::Object(), |
| 1226 |
Py::String(subname ? subname : ""), |
| 1227 |
tuple |
| 1228 |
); |
| 1229 |
res = Base::pyCall(py_dropObjectEx.ptr(), args.ptr()); |
| 1230 |
if (!res.isNone()) { |
| 1231 |
ret = res.as_string(); |
| 1232 |
} |
| 1233 |
return true; |
| 1234 |
} |
| 1235 |
catch (Py::Exception&) { |
| 1236 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1237 |
PyErr_Clear(); |
| 1238 |
return false; |
| 1239 |
} |
| 1240 |
Base::PyException::throwException(); |
| 1241 |
} |
| 1242 |
return true; |
| 1243 |
} |
| 1244 |
|
| 1245 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::isShow() const |
| 1246 |
{ |
| 1247 |
FC_PY_CALL_CHECK(isShow); |
| 1248 |
|
| 1249 |
Base::PyGILStateLocker lock; |
| 1250 |
try { |
| 1251 |
Py::Boolean ok(Base::pyCall(py_isShow.ptr())); |
| 1252 |
return static_cast<bool>(ok) ? Accepted : Rejected; |
| 1253 |
} |
| 1254 |
catch (Py::Exception&) { |
| 1255 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1256 |
PyErr_Clear(); |
| 1257 |
return NotImplemented; |
| 1258 |
} |
| 1259 |
Base::PyException e; // extract the Python error text |
| 1260 |
e.reportException(); |
| 1261 |
} |
| 1262 |
|
| 1263 |
return Rejected; |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::canRemoveChildrenFromRoot() const |
| 1268 |
{ |
| 1269 |
|
| 1270 |
FC_PY_CALL_CHECK(canRemoveChildrenFromRoot); |
| 1271 |
|
| 1272 |
Base::PyGILStateLocker lock; |
| 1273 |
try { |
| 1274 |
Py::Boolean ok(Base::pyCall(py_canRemoveChildrenFromRoot.ptr())); |
| 1275 |
return static_cast<bool>(ok) ? Accepted : Rejected; |
| 1276 |
} |
| 1277 |
catch (Py::Exception&) { |
| 1278 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1279 |
PyErr_Clear(); |
| 1280 |
return NotImplemented; |
| 1281 |
} |
| 1282 |
Base::PyException e; // extract the Python error text |
| 1283 |
e.reportException(); |
| 1284 |
} |
| 1285 |
return Rejected; |
| 1286 |
} |
| 1287 |
|
| 1288 |
bool ViewProviderFeaturePythonImp::getDropPrefix(std::string& prefix) const |
| 1289 |
{ |
| 1290 |
|
| 1291 |
_FC_PY_CALL_CHECK(getDropPrefix, return (false)); |
| 1292 |
|
| 1293 |
Base::PyGILStateLocker lock; |
| 1294 |
try { |
| 1295 |
Py::Object ret(Base::pyCall(py_getDropPrefix.ptr())); |
| 1296 |
if (ret.isNone()) { |
| 1297 |
return false; |
| 1298 |
} |
| 1299 |
prefix = ret.as_string(); |
| 1300 |
return true; |
| 1301 |
} |
| 1302 |
catch (Py::Exception&) { |
| 1303 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1304 |
PyErr_Clear(); |
| 1305 |
return false; |
| 1306 |
} |
| 1307 |
Base::PyException e; // extract the Python error text |
| 1308 |
e.reportException(); |
| 1309 |
} |
| 1310 |
return true; |
| 1311 |
} |
| 1312 |
|
| 1313 |
ViewProviderFeaturePythonImp::ValueT ViewProviderFeaturePythonImp::replaceObject( |
| 1314 |
App::DocumentObject* oldObj, |
| 1315 |
App::DocumentObject* newObj |
| 1316 |
) |
| 1317 |
{ |
| 1318 |
if (!oldObj || !oldObj->isAttachedToDocument() || !newObj || !newObj->isAttachedToDocument()) { |
| 1319 |
return NotImplemented; |
| 1320 |
} |
| 1321 |
|
| 1322 |
FC_PY_CALL_CHECK(replaceObject); |
| 1323 |
|
| 1324 |
Base::PyGILStateLocker lock; |
| 1325 |
try { |
| 1326 |
Py::TupleN args(Py::asObject(oldObj->getPyObject()), Py::asObject(newObj->getPyObject())); |
| 1327 |
Py::Boolean ok(Base::pyCall(py_replaceObject.ptr(), args.ptr())); |
| 1328 |
return ok ? Accepted : Rejected; |
| 1329 |
} |
| 1330 |
catch (Py::Exception&) { |
| 1331 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1332 |
PyErr_Clear(); |
| 1333 |
return NotImplemented; |
| 1334 |
} |
| 1335 |
Base::PyException e; // extract the Python error text |
| 1336 |
e.reportException(); |
| 1337 |
} |
| 1338 |
return Rejected; |
| 1339 |
} |
| 1340 |
|
| 1341 |
bool ViewProviderFeaturePythonImp::getLinkedViewProvider( |
| 1342 |
ViewProviderDocumentObject*& vp, |
| 1343 |
std::string* subname, |
| 1344 |
bool recursive |
| 1345 |
) const |
| 1346 |
{ |
| 1347 |
_FC_PY_CALL_CHECK(getLinkedViewProvider, return (false)); |
| 1348 |
|
| 1349 |
Base::PyGILStateLocker lock; |
| 1350 |
try { |
| 1351 |
Py::Tuple args(1); |
| 1352 |
args.setItem(0, Py::Boolean(recursive)); |
| 1353 |
Py::Object res(Base::pyCall(py_getLinkedViewProvider.ptr(), args.ptr())); |
| 1354 |
if (res.isNone()) { |
| 1355 |
return true; |
| 1356 |
} |
| 1357 |
if (PyObject_TypeCheck(res.ptr(), &ViewProviderDocumentObjectPy::Type)) { |
| 1358 |
vp = static_cast<ViewProviderDocumentObjectPy*>(res.ptr()) |
| 1359 |
->getViewProviderDocumentObjectPtr(); |
| 1360 |
return true; |
| 1361 |
} |
| 1362 |
else if (PySequence_Check(res.ptr()) && PySequence_Length(res.ptr()) == 2) { |
| 1363 |
Py::Sequence seq(res); |
| 1364 |
Py::Object item0(seq[0].ptr()); |
| 1365 |
Py::Object item1(seq[1].ptr()); |
| 1366 |
if (PyObject_TypeCheck(item0.ptr(), &ViewProviderDocumentObjectPy::Type) |
| 1367 |
&& item1.isString()) { |
| 1368 |
if (subname) { |
| 1369 |
*subname = Py::String(item1).as_std_string("utf-8"); |
| 1370 |
} |
| 1371 |
vp = static_cast<ViewProviderDocumentObjectPy*>(item0.ptr()) |
| 1372 |
->getViewProviderDocumentObjectPtr(); |
| 1373 |
return true; |
| 1374 |
} |
| 1375 |
} |
| 1376 |
FC_ERR("getLinkedViewProvider(): invalid return type, expects ViewObject or (ViewObject, subname)"); |
| 1377 |
return true; |
| 1378 |
} |
| 1379 |
catch (Py::Exception&) { |
| 1380 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1381 |
PyErr_Clear(); |
| 1382 |
return false; |
| 1383 |
} |
| 1384 |
Base::PyException e; // extract the Python error text |
| 1385 |
e.reportException(); |
| 1386 |
} |
| 1387 |
return true; |
| 1388 |
} |
| 1389 |
|
| 1390 |
bool ViewProviderFeaturePythonImp::editProperty(const char* name) |
| 1391 |
{ |
| 1392 |
_FC_PY_CALL_CHECK(editProperty, return false); |
| 1393 |
Base::PyGILStateLocker lock; |
| 1394 |
try { |
| 1395 |
Py::Tuple args(1); |
| 1396 |
args.setItem(0, Py::String(name)); |
| 1397 |
Py::Object ret(Base::pyCall(py_editProperty.ptr(), args.ptr())); |
| 1398 |
return ret.isTrue(); |
| 1399 |
} |
| 1400 |
catch (Py::Exception&) { |
| 1401 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 1402 |
PyErr_Clear(); |
| 1403 |
return false; |
| 1404 |
} |
| 1405 |
|
| 1406 |
Base::PyException e; // extract the Python error text |
| 1407 |
e.reportException(); |
| 1408 |
} |
| 1409 |
return false; |
| 1410 |
} |
| 1411 |
|
| 1412 |
// --------------------------------------------------------- |
| 1413 |
|
| 1414 |
namespace Gui |
| 1415 |
{ |
| 1416 |
PROPERTY_SOURCE_TEMPLATE(Gui::ViewProviderFeaturePython, Gui::ViewProviderDocumentObject) |
| 1417 |
// explicit template instantiation |
| 1418 |
template class GuiExport ViewProviderFeaturePythonT<ViewProviderDocumentObject>; |
| 1419 |
} // namespace Gui |
| 1420 |
|
| 1421 |
// --------------------------------------------------------- |
| 1422 |
|
| 1423 |
namespace Gui |
| 1424 |
{ |
| 1425 |
PROPERTY_SOURCE_TEMPLATE(Gui::ViewProviderGeometryPython, Gui::ViewProviderGeometryObject) |
| 1426 |
// explicit template instantiation |
| 1427 |
template class GuiExport ViewProviderFeaturePythonT<ViewProviderGeometryObject>; |
| 1428 |
} // namespace Gui |