| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Jürgen Riegel <[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 |
|
| 24 |
#include <QAction> |
| 25 |
#include <QMenu> |
| 26 |
#include <Inventor/SoFullPath.h> |
| 27 |
#include <Inventor/SoPickedPoint.h> |
| 28 |
#include <Inventor/actions/SoSearchAction.h> |
| 29 |
#include <Inventor/details/SoDetail.h> |
| 30 |
#include <Inventor/misc/SoChildList.h> |
| 31 |
#include <Inventor/nodes/SoSeparator.h> |
| 32 |
|
| 33 |
|
| 34 |
#include <App/Document.h> |
| 35 |
#include <App/Origin.h> |
| 36 |
#include <Base/Tools.h> |
| 37 |
|
| 38 |
#include "ViewProviderDocumentObjectPy.h" |
| 39 |
#include "ActionFunction.h" |
| 40 |
#include "Application.h" |
| 41 |
#include "Command.h" |
| 42 |
#include "Document.h" |
| 43 |
#include "MDIView.h" |
| 44 |
#include "SoFCUnifiedSelection.h" |
| 45 |
#include "Tree.h" |
| 46 |
#include "ViewProviderDocumentObject.h" |
| 47 |
#include "ViewProviderExtension.h" |
| 48 |
#include "TaskView/TaskAppearance.h" |
| 49 |
|
| 50 |
|
| 51 |
FC_LOG_LEVEL_INIT("Gui", true, true) |
| 52 |
|
| 53 |
using namespace Gui; |
| 54 |
|
| 55 |
|
| 56 |
PROPERTY_SOURCE(Gui::ViewProviderDocumentObject, Gui::ViewProvider) |
| 57 |
|
| 58 |
ViewProviderDocumentObject::ViewProviderDocumentObject() |
| 59 |
{ |
| 60 |
static const char* dogroup = "Display Options"; |
| 61 |
static const char* sgroup = "Selection"; |
| 62 |
|
| 63 |
ADD_PROPERTY_TYPE(DisplayMode, ((long)0), dogroup, App::Prop_None, "Set the display mode"); |
| 64 |
ADD_PROPERTY_TYPE(Visibility, (true), dogroup, App::Prop_None, "Show the object in the 3d view"); |
| 65 |
ADD_PROPERTY_TYPE(ShowInTree, (true), dogroup, App::Prop_None, "Show the object in the tree view"); |
| 66 |
|
| 67 |
ADD_PROPERTY_TYPE(SelectionStyle, ((long)0), sgroup, App::Prop_None, "Set the object selection style"); |
| 68 |
static const char* SelectionStyleEnum[] = {"Shape", "BoundBox", nullptr}; |
| 69 |
SelectionStyle.setEnums(SelectionStyleEnum); |
| 70 |
|
| 71 |
static const char* OnTopEnum[] = {"Disabled", "Enabled", "Object", "Element", nullptr}; |
| 72 |
ADD_PROPERTY_TYPE( |
| 73 |
OnTopWhenSelected, |
| 74 |
((long int)0), |
| 75 |
sgroup, |
| 76 |
App::Prop_None, |
| 77 |
"Enabled: Display the object on top of any other object when selected\n" |
| 78 |
"Object: On top only if the whole object is selected\n" |
| 79 |
"Element: On top only if some sub-element of the object is selected" |
| 80 |
); |
| 81 |
OnTopWhenSelected.setEnums(OnTopEnum); |
| 82 |
|
| 83 |
sPixmap = "Feature"; |
| 84 |
} |
| 85 |
|
| 86 |
ViewProviderDocumentObject::~ViewProviderDocumentObject() |
| 87 |
{ |
| 88 |
// Make sure that the property class does not destruct our string list |
| 89 |
DisplayMode.setContainer(nullptr); |
| 90 |
DisplayMode.setEnums(nullptr); |
| 91 |
} |
| 92 |
|
| 93 |
void ViewProviderDocumentObject::getTaskViewContent(std::vector<Gui::TaskView::TaskContent*>& vec) const |
| 94 |
{ |
| 95 |
vec.push_back(new Gui::TaskView::TaskAppearance()); |
| 96 |
} |
| 97 |
|
| 98 |
void ViewProviderDocumentObject::startRestoring() |
| 99 |
{ |
| 100 |
hide(); |
| 101 |
auto vector = getExtensionsDerivedFromType<Gui::ViewProviderExtension>(); |
| 102 |
for (Gui::ViewProviderExtension* ext : vector) { |
| 103 |
ext->extensionStartRestoring(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
void ViewProviderDocumentObject::finishRestoring() |
| 108 |
{ |
| 109 |
auto vector = getExtensionsDerivedFromType<Gui::ViewProviderExtension>(); |
| 110 |
for (Gui::ViewProviderExtension* ext : vector) { |
| 111 |
ext->extensionFinishRestoring(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
bool ViewProviderDocumentObject::isAttachedToDocument() const |
| 116 |
{ |
| 117 |
return (!testStatus(Detach)); |
| 118 |
} |
| 119 |
|
| 120 |
const char* ViewProviderDocumentObject::detachFromDocument() |
| 121 |
{ |
| 122 |
// here we can return an empty string since the object |
| 123 |
// name comes from the document object |
| 124 |
setStatus(Detach, true); |
| 125 |
return ""; |
| 126 |
} |
| 127 |
|
| 128 |
bool ViewProviderDocumentObject::removeDynamicProperty(const char* name) |
| 129 |
{ |
| 130 |
App::Property* prop = getDynamicPropertyByName(name); |
| 131 |
if (!prop || prop->testStatus(App::Property::LockDynamic)) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
// transactions of view providers are also managed in App::Document. |
| 136 |
App::DocumentObject* docobject = getObject(); |
| 137 |
App::Document* document = docobject ? docobject->getDocument() : nullptr; |
| 138 |
if (document) { |
| 139 |
document->addOrRemovePropertyOfObject(this, prop, false); |
| 140 |
} |
| 141 |
|
| 142 |
return ViewProvider::removeDynamicProperty(name); |
| 143 |
} |
| 144 |
|
| 145 |
App::Property* ViewProviderDocumentObject::addDynamicProperty( |
| 146 |
const char* type, |
| 147 |
const char* name, |
| 148 |
const char* group, |
| 149 |
const char* doc, |
| 150 |
short attr, |
| 151 |
bool ro, |
| 152 |
bool hidden |
| 153 |
) |
| 154 |
{ |
| 155 |
auto prop = ViewProvider::addDynamicProperty(type, name, group, doc, attr, ro, hidden); |
| 156 |
if (prop) { |
| 157 |
// transactions of view providers are also managed in App::Document. |
| 158 |
App::DocumentObject* docobject = getObject(); |
| 159 |
App::Document* document = docobject ? docobject->getDocument() : nullptr; |
| 160 |
if (document) { |
| 161 |
document->addOrRemovePropertyOfObject(this, prop, true); |
| 162 |
} |
| 163 |
} |
| 164 |
return prop; |
| 165 |
} |
| 166 |
|
| 167 |
void ViewProviderDocumentObject::onBeforeChange(const App::Property* prop) |
| 168 |
{ |
| 169 |
if (isAttachedToDocument()) { |
| 170 |
App::DocumentObject* obj = getObject(); |
| 171 |
App::Document* doc = obj ? obj->getDocument() : nullptr; |
| 172 |
if (doc) { |
| 173 |
onBeforeChangeProperty(doc, prop); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
ViewProvider::onBeforeChange(prop); |
| 178 |
} |
| 179 |
|
| 180 |
void ViewProviderDocumentObject::onChanged(const App::Property* prop) |
| 181 |
{ |
| 182 |
if (prop == &DisplayMode) { |
| 183 |
setActiveMode(); |
| 184 |
} |
| 185 |
else if (prop == &Visibility) { |
| 186 |
// use this bit to check whether show() or hide() must be called |
| 187 |
if (!Visibility.testStatus(App::Property::User2)) { |
| 188 |
Visibility.setStatus(App::Property::User2, true); |
| 189 |
Visibility.getValue() ? show() : hide(); |
| 190 |
Visibility.setStatus(App::Property::User2, false); |
| 191 |
} |
| 192 |
if (!Visibility.testStatus(App::Property::User1) && getObject() |
| 193 |
&& getObject()->Visibility.getValue() != Visibility.getValue()) { |
| 194 |
// Changing the visibility of a document object will automatically set |
| 195 |
// the document modified but if the 'TouchDocument' flag is not set then |
| 196 |
// this is undesired behaviour. So, if this change marks the document as |
| 197 |
// modified then it must be be reversed. |
| 198 |
if (!testStatus(Gui::ViewStatus::TouchDocument)) { |
| 199 |
// Note: reverting document modified status like that is not |
| 200 |
// appropriate because we can't tell if there is any other |
| 201 |
// property being changed due to the change of Visibility here. |
| 202 |
// Temporary setting the Visibility property as 'NoModify' is |
| 203 |
// the proper way. |
| 204 |
Base::ObjectStatusLocker<App::Property::Status, App::Property> guard( |
| 205 |
App::Property::NoModify, |
| 206 |
&Visibility |
| 207 |
); |
| 208 |
// bool mod = false; |
| 209 |
// if (pcDocument) |
| 210 |
// mod = pcDocument->isModified(); |
| 211 |
getObject()->Visibility.setValue(Visibility.getValue()); |
| 212 |
// if (pcDocument) |
| 213 |
// pcDocument->setModified(mod); |
| 214 |
} |
| 215 |
else { |
| 216 |
getObject()->Visibility.setValue(Visibility.getValue()); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
else if (prop == &SelectionStyle) { |
| 221 |
if (getRoot()->isOfType(SoFCSelectionRoot::getClassTypeId())) { |
| 222 |
static_cast<SoFCSelectionRoot*>(getRoot())->selectionStyle = SelectionStyle.getValue() |
| 223 |
? SoFCSelectionRoot::Box |
| 224 |
: SoFCSelectionRoot::Full; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (prop && !prop->testStatus(App::Property::NoModify) && pcDocument |
| 229 |
&& !pcDocument->isModified() && testStatus(Gui::ViewStatus::TouchDocument)) { |
| 230 |
if (prop) { |
| 231 |
FC_LOG(prop->getFullName() << " changed"); |
| 232 |
} |
| 233 |
pcDocument->setModified(true); |
| 234 |
} |
| 235 |
|
| 236 |
ViewProvider::onChanged(prop); |
| 237 |
} |
| 238 |
|
| 239 |
void ViewProviderDocumentObject::hide() |
| 240 |
{ |
| 241 |
ViewProvider::hide(); |
| 242 |
// use this bit to check whether 'Visibility' must be adjusted |
| 243 |
if (!Visibility.testStatus(App::Property::User2)) { |
| 244 |
Visibility.setStatus(App::Property::User2, true); |
| 245 |
Visibility.setValue(false); |
| 246 |
Visibility.setStatus(App::Property::User2, false); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
bool ViewProviderDocumentObject::isShowable() const |
| 251 |
{ |
| 252 |
return _Showable; |
| 253 |
} |
| 254 |
|
| 255 |
void ViewProviderDocumentObject::setShowable(bool enable) |
| 256 |
{ |
| 257 |
if (_Showable == enable) { |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
_Showable = enable; |
| 262 |
int which = getModeSwitch()->whichChild.getValue(); |
| 263 |
if (_Showable && which == -1 && Visibility.getValue()) { |
| 264 |
setModeSwitch(); |
| 265 |
} |
| 266 |
else if (!_Showable) { |
| 267 |
if (which >= 0) { |
| 268 |
ViewProvider::hide(); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
void ViewProviderDocumentObject::startDefaultEditMode() |
| 274 |
{ |
| 275 |
QString text = QObject::tr("Edit %1").arg(QString::fromUtf8(getObject()->Label.getValue())); |
| 276 |
Gui::Command::openCommand(text.toUtf8()); |
| 277 |
|
| 278 |
Gui::Document* document = this->getDocument(); |
| 279 |
if (document) { |
| 280 |
document->setEdit(this, ViewProvider::Default); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
void ViewProviderDocumentObject::addDefaultAction(QMenu* menu, const QString& text) |
| 285 |
{ |
| 286 |
QAction* act = menu->addAction(text); |
| 287 |
act->setData(QVariant((int)ViewProvider::Default)); |
| 288 |
auto func = new Gui::ActionFunction(menu); |
| 289 |
func->trigger(act, [this]() { this->startDefaultEditMode(); }); |
| 290 |
} |
| 291 |
|
| 292 |
void ViewProviderDocumentObject::setModeSwitch() |
| 293 |
{ |
| 294 |
if (isShowable()) { |
| 295 |
ViewProvider::setModeSwitch(); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
void ViewProviderDocumentObject::show() |
| 300 |
{ |
| 301 |
if (TreeWidget::isObjectShowable(getObject())) { |
| 302 |
ViewProvider::show(); |
| 303 |
} |
| 304 |
else { |
| 305 |
Visibility.setValue(false); |
| 306 |
if (getObject()) { |
| 307 |
getObject()->Visibility.setValue(false); |
| 308 |
} |
| 309 |
return; |
| 310 |
} |
| 311 |
|
| 312 |
// use this bit to check whether 'Visibility' must be adjusted |
| 313 |
if (!Visibility.testStatus(App::Property::User2)) { |
| 314 |
Visibility.setStatus(App::Property::User2, true); |
| 315 |
Visibility.setValue(true); |
| 316 |
Visibility.setStatus(App::Property::User2, false); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
const char* ViewProviderDocumentObject::getTransactionText() const |
| 321 |
{ |
| 322 |
return QT_TRANSLATE_NOOP("Command", "Edit"); |
| 323 |
} |
| 324 |
|
| 325 |
void ViewProviderDocumentObject::updateView() |
| 326 |
{ |
| 327 |
if (!pcObject || testStatus(ViewStatus::UpdatingView)) { |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
Base::ObjectStatusLocker<ViewStatus, ViewProviderDocumentObject> lock(ViewStatus::UpdatingView, this); |
| 332 |
|
| 333 |
// Disable object visibility syncing |
| 334 |
Base::ObjectStatusLocker<App::Property::Status, App::Property> lock2( |
| 335 |
App::Property::User1, |
| 336 |
&Visibility |
| 337 |
); |
| 338 |
|
| 339 |
std::map<std::string, App::Property*> Map; |
| 340 |
pcObject->getPropertyMap(Map); |
| 341 |
|
| 342 |
// Hide the object temporarily to speed up the update |
| 343 |
bool vis = ViewProvider::isShow(); |
| 344 |
if (vis) { |
| 345 |
ViewProvider::hide(); |
| 346 |
} |
| 347 |
for (const auto& it : Map) { |
| 348 |
updateData(it.second); |
| 349 |
} |
| 350 |
if (vis && Visibility.getValue()) { |
| 351 |
ViewProvider::show(); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
void ViewProviderDocumentObject::attach(App::DocumentObject* pcObj) |
| 356 |
{ |
| 357 |
// save Object pointer |
| 358 |
pcObject = pcObj; |
| 359 |
|
| 360 |
if (pcObj && pcObj->isAttachedToDocument() |
| 361 |
&& Visibility.getValue() != pcObj->Visibility.getValue()) { |
| 362 |
pcObj->Visibility.setValue(Visibility.getValue()); |
| 363 |
} |
| 364 |
|
| 365 |
// Retrieve the supported display modes of the view provider |
| 366 |
aDisplayModesArray = this->getDisplayModes(); |
| 367 |
|
| 368 |
if (aDisplayModesArray.empty()) { |
| 369 |
aDisplayModesArray.emplace_back(""); |
| 370 |
} |
| 371 |
|
| 372 |
// We must collect the const char* of the strings and give it to PropertyEnumeration, |
| 373 |
// but we are still responsible for them, i.e. the property class must not delete the literals. |
| 374 |
// for (auto it = aDisplayModesArray.begin(); it != aDisplayModesArray.end(); ++it) { |
| 375 |
for (const auto& it : aDisplayModesArray) { |
| 376 |
aDisplayEnumsArray.push_back(it.c_str()); |
| 377 |
} |
| 378 |
aDisplayEnumsArray.push_back(nullptr); // null termination |
| 379 |
DisplayMode.setEnums(&(aDisplayEnumsArray[0])); |
| 380 |
|
| 381 |
if (!isRestoring()) { |
| 382 |
// set the active mode |
| 383 |
const char* defmode = this->getDefaultDisplayMode(); |
| 384 |
if (defmode) { |
| 385 |
DisplayMode.setValue(defmode); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
// attach the extensions |
| 390 |
auto vector = getExtensionsDerivedFromType<Gui::ViewProviderExtension>(); |
| 391 |
for (Gui::ViewProviderExtension* ext : vector) { |
| 392 |
ext->extensionAttach(pcObj); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
void ViewProviderDocumentObject::reattach(App::DocumentObject* pcObj) |
| 397 |
{ |
| 398 |
auto vector = getExtensionsDerivedFromType<Gui::ViewProviderExtension>(); |
| 399 |
for (Gui::ViewProviderExtension* ext : vector) { |
| 400 |
ext->extensionReattach(pcObj); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
void ViewProviderDocumentObject::update(const App::Property* prop) |
| 405 |
{ |
| 406 |
// bypass view provider update to always allow changing visibility from |
| 407 |
// document object |
| 408 |
if (prop == &getObject()->Visibility) { |
| 409 |
if (!isRestoring() && Visibility.getValue() != getObject()->Visibility.getValue()) { |
| 410 |
Visibility.setValue(!Visibility.getValue()); |
| 411 |
} |
| 412 |
} |
| 413 |
else { |
| 414 |
// Disable object visibility syncing |
| 415 |
Base::ObjectStatusLocker<App::Property::Status, App::Property> guard( |
| 416 |
App::Property::User1, |
| 417 |
&Visibility |
| 418 |
); |
| 419 |
ViewProvider::update(prop); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
Gui::Document* ViewProviderDocumentObject::getDocument() const |
| 424 |
{ |
| 425 |
if (!pcObject) { |
| 426 |
throw Base::RuntimeError("View provider detached"); |
| 427 |
} |
| 428 |
if (pcDocument) { |
| 429 |
return pcDocument; |
| 430 |
} |
| 431 |
else { |
| 432 |
App::Document* pAppDoc = pcObject->getDocument(); |
| 433 |
return Gui::Application::Instance->getDocument(pAppDoc); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
Gui::MDIView* ViewProviderDocumentObject::getActiveView() const |
| 438 |
{ |
| 439 |
if (!pcObject) { |
| 440 |
throw Base::RuntimeError("View provider detached"); |
| 441 |
} |
| 442 |
|
| 443 |
if (!pcObject->isAttachedToDocument()) { |
| 444 |
// Check if view provider is attached to a document as an annotation |
| 445 |
for (auto doc : App::GetApplication().getDocuments()) { |
| 446 |
auto guiDoc = Gui::Application::Instance->getDocument(doc); |
| 447 |
if (guiDoc->isAnnotationViewProvider(this)) { |
| 448 |
return guiDoc->getActiveView(); |
| 449 |
} |
| 450 |
} |
| 451 |
return nullptr; |
| 452 |
} |
| 453 |
|
| 454 |
App::Document* pAppDoc = pcObject->getDocument(); |
| 455 |
Gui::Document* pGuiDoc = Gui::Application::Instance->getDocument(pAppDoc); |
| 456 |
return pGuiDoc->getActiveView(); |
| 457 |
} |
| 458 |
|
| 459 |
Gui::MDIView* ViewProviderDocumentObject::getEditingView() const |
| 460 |
{ |
| 461 |
if (!pcObject) { |
| 462 |
throw Base::RuntimeError("View provider detached"); |
| 463 |
} |
| 464 |
App::Document* pAppDoc = pcObject->getDocument(); |
| 465 |
Gui::Document* pGuiDoc = Gui::Application::Instance->getDocument(pAppDoc); |
| 466 |
return pGuiDoc->getEditingViewOfViewProvider(const_cast<ViewProviderDocumentObject*>(this)); |
| 467 |
} |
| 468 |
|
| 469 |
Gui::MDIView* ViewProviderDocumentObject::getInventorView() const |
| 470 |
{ |
| 471 |
if (!pcObject) { |
| 472 |
throw Base::RuntimeError("View provider detached"); |
| 473 |
} |
| 474 |
App::Document* pAppDoc = pcObject->getDocument(); |
| 475 |
Gui::Document* pGuiDoc = Gui::Application::Instance->getDocument(pAppDoc); |
| 476 |
|
| 477 |
Gui::MDIView* mdi = pGuiDoc->getEditingViewOfViewProvider( |
| 478 |
const_cast<ViewProviderDocumentObject*>(this) |
| 479 |
); |
| 480 |
if (!mdi) { |
| 481 |
mdi = pGuiDoc->getViewOfViewProvider(const_cast<ViewProviderDocumentObject*>(this)); |
| 482 |
} |
| 483 |
|
| 484 |
return mdi; |
| 485 |
} |
| 486 |
|
| 487 |
Gui::MDIView* ViewProviderDocumentObject::getViewOfNode(SoNode* node) const |
| 488 |
{ |
| 489 |
if (!pcObject) { |
| 490 |
throw Base::RuntimeError("View provider detached"); |
| 491 |
} |
| 492 |
App::Document* pAppDoc = pcObject->getDocument(); |
| 493 |
Gui::Document* pGuiDoc = Gui::Application::Instance->getDocument(pAppDoc); |
| 494 |
return pGuiDoc->getViewOfNode(node); |
| 495 |
} |
| 496 |
|
| 497 |
SoNode* ViewProviderDocumentObject::findFrontRootOfType(const SoType& type) const |
| 498 |
{ |
| 499 |
if (!pcObject) { |
| 500 |
return nullptr; |
| 501 |
} |
| 502 |
// first get the document this object is part of and get its GUI counterpart |
| 503 |
App::Document* pAppDoc = pcObject->getDocument(); |
| 504 |
Gui::Document* pGuiDoc = Gui::Application::Instance->getDocument(pAppDoc); |
| 505 |
|
| 506 |
SoSearchAction searchAction; |
| 507 |
searchAction.setType(type); |
| 508 |
searchAction.setInterest(SoSearchAction::FIRST); |
| 509 |
|
| 510 |
// search in all view providers for the node type |
| 511 |
std::vector<App::DocumentObject*> obj = pAppDoc->getObjects(); |
| 512 |
for (auto& it : obj) { |
| 513 |
const ViewProvider* vp = pGuiDoc->getViewProvider(it); |
| 514 |
// Ignore 'this' view provider. It could also happen that vp is 0, e.g. when |
| 515 |
// several objects have been added to the App::Document before notifying the |
| 516 |
// Gui::Document |
| 517 |
if (!vp || vp == this) { |
| 518 |
continue; |
| 519 |
} |
| 520 |
SoSeparator* front = vp->getFrontRoot(); |
| 521 |
// if (front && front->getTypeId() == type) |
| 522 |
// return front; |
| 523 |
if (front) { |
| 524 |
searchAction.apply(front); |
| 525 |
SoPath* path = searchAction.getPath(); |
| 526 |
if (path) { |
| 527 |
return path->getTail(); |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
return nullptr; |
| 533 |
} |
| 534 |
|
| 535 |
void ViewProviderDocumentObject::setActiveMode() |
| 536 |
{ |
| 537 |
if (DisplayMode.isValid()) { |
| 538 |
const char* mode = DisplayMode.getValueAsString(); |
| 539 |
if (mode) { |
| 540 |
setDisplayMode(mode); |
| 541 |
} |
| 542 |
} |
| 543 |
if (!Visibility.getValue()) { |
| 544 |
ViewProvider::hide(); |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
bool ViewProviderDocumentObject::canDelete(App::DocumentObject* obj) const |
| 549 |
{ |
| 550 |
Q_UNUSED(obj); |
| 551 |
auto* o = getObject(); |
| 552 |
return o->hasExtension(App::GroupExtension::getExtensionClassTypeId()) |
| 553 |
|| o->isDerivedFrom<App::Origin>(); |
| 554 |
} |
| 555 |
|
| 556 |
PyObject* ViewProviderDocumentObject::getPyObject() |
| 557 |
{ |
| 558 |
if (!pyViewObject) { |
| 559 |
pyViewObject = new ViewProviderDocumentObjectPy(this); |
| 560 |
} |
| 561 |
pyViewObject->IncRef(); |
| 562 |
return pyViewObject; |
| 563 |
} |
| 564 |
|
| 565 |
bool ViewProviderDocumentObject::canDropObjectEx( |
| 566 |
App::DocumentObject* obj, |
| 567 |
App::DocumentObject* owner, |
| 568 |
const char* subname, |
| 569 |
const std::vector<std::string>& elements |
| 570 |
) const |
| 571 |
{ |
| 572 |
auto vector = getExtensionsDerivedFromType<Gui::ViewProviderExtension>(); |
| 573 |
for (Gui::ViewProviderExtension* ext : vector) { |
| 574 |
if (ext->extensionCanDropObjectEx(obj, owner, subname, elements)) { |
| 575 |
return true; |
| 576 |
} |
| 577 |
} |
| 578 |
if (obj && obj->getDocument() != getObject()->getDocument()) { |
| 579 |
return false; |
| 580 |
} |
| 581 |
return canDropObject(obj); |
| 582 |
} |
| 583 |
|
| 584 |
int ViewProviderDocumentObject::replaceObject(App::DocumentObject* oldObj, App::DocumentObject* newObj) |
| 585 |
{ |
| 586 |
if (!oldObj || !oldObj->isAttachedToDocument() || !newObj || !newObj->isAttachedToDocument()) { |
| 587 |
FC_THROWM(Base::RuntimeError, "Invalid object"); |
| 588 |
} |
| 589 |
|
| 590 |
auto obj = getObject(); |
| 591 |
if (!obj || !obj->isAttachedToDocument()) { |
| 592 |
FC_THROWM(Base::RuntimeError, "View provider not attached"); |
| 593 |
} |
| 594 |
|
| 595 |
int res = ViewProvider::replaceObject(oldObj, newObj); |
| 596 |
if (res >= 0) { |
| 597 |
return res; |
| 598 |
} |
| 599 |
|
| 600 |
std::vector<std::pair<App::DocumentObjectT, std::unique_ptr<App::Property>>> propChanges; |
| 601 |
std::vector<App::Property*> props; |
| 602 |
obj->getPropertyList(props); |
| 603 |
for (auto prop : props) { |
| 604 |
auto linkProp = freecad_cast<App::PropertyLinkBase*>(prop); |
| 605 |
if (!linkProp) { |
| 606 |
continue; |
| 607 |
} |
| 608 |
std::unique_ptr<App::Property> copy(linkProp->CopyOnLinkReplace(obj, oldObj, newObj)); |
| 609 |
if (!copy) { |
| 610 |
continue; |
| 611 |
} |
| 612 |
propChanges.emplace_back(prop, std::move(copy)); |
| 613 |
} |
| 614 |
|
| 615 |
if (propChanges.empty()) { |
| 616 |
return 0; |
| 617 |
} |
| 618 |
|
| 619 |
// Global search for affected links |
| 620 |
for (auto doc : App::GetApplication().getDocuments()) { |
| 621 |
for (auto o : doc->getObjects()) { |
| 622 |
if (o == obj) { |
| 623 |
continue; |
| 624 |
} |
| 625 |
std::vector<App::Property*> props; |
| 626 |
o->getPropertyList(props); |
| 627 |
for (auto prop : props) { |
| 628 |
auto linkProp = freecad_cast<App::PropertyLinkBase*>(prop); |
| 629 |
if (!linkProp) { |
| 630 |
continue; |
| 631 |
} |
| 632 |
std::unique_ptr<App::Property> copy(linkProp->CopyOnLinkReplace(obj, oldObj, newObj)); |
| 633 |
if (!copy) { |
| 634 |
continue; |
| 635 |
} |
| 636 |
propChanges.emplace_back(App::DocumentObjectT(prop), std::move(copy)); |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
for (auto& v : propChanges) { |
| 642 |
auto prop = v.first.getProperty(); |
| 643 |
if (prop) { |
| 644 |
prop->Paste(*v.second.get()); |
| 645 |
} |
| 646 |
} |
| 647 |
return 1; |
| 648 |
} |
| 649 |
|
| 650 |
bool ViewProviderDocumentObject::showInTree() const |
| 651 |
{ |
| 652 |
return ShowInTree.getValue(); |
| 653 |
} |
| 654 |
|
| 655 |
bool ViewProviderDocumentObject::getElementPicked(const SoPickedPoint* pp, std::string& subname) const |
| 656 |
{ |
| 657 |
auto vector = getExtensionsDerivedFromType<Gui::ViewProviderExtension>(); |
| 658 |
for (Gui::ViewProviderExtension* ext : vector) { |
| 659 |
if (ext->extensionGetElementPicked(pp, subname)) { |
| 660 |
return true; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
auto childRoot = getChildRoot(); |
| 665 |
int idx; |
| 666 |
if (!childRoot || (idx = pcModeSwitch->whichChild.getValue()) < 0 |
| 667 |
|| pcModeSwitch->getChild(idx) != childRoot) { |
| 668 |
return ViewProvider::getElementPicked(pp, subname); |
| 669 |
} |
| 670 |
|
| 671 |
SoPath* path = pp->getPath(); |
| 672 |
idx = path->findNode(childRoot); |
| 673 |
if (idx < 0 || idx + 1 >= path->getLength()) { |
| 674 |
return false; |
| 675 |
} |
| 676 |
auto vp = getDocument()->getViewProvider(path->getNode(idx + 1)); |
| 677 |
if (!vp) { |
| 678 |
return false; |
| 679 |
} |
| 680 |
auto obj = vp->getObject(); |
| 681 |
if (!obj || !obj->isAttachedToDocument()) { |
| 682 |
return false; |
| 683 |
} |
| 684 |
std::ostringstream str; |
| 685 |
str << obj->getNameInDocument() << '.'; |
| 686 |
if (vp->getElementPicked(pp, subname)) { |
| 687 |
str << subname; |
| 688 |
} |
| 689 |
subname = str.str(); |
| 690 |
return true; |
| 691 |
} |
| 692 |
|
| 693 |
bool ViewProviderDocumentObject::getDetailPath( |
| 694 |
const char* subname, |
| 695 |
SoFullPath* path, |
| 696 |
bool append, |
| 697 |
SoDetail*& det |
| 698 |
) const |
| 699 |
{ |
| 700 |
auto len = path->getLength(); |
| 701 |
if (!append && len >= 2) { |
| 702 |
len -= 2; |
| 703 |
} |
| 704 |
if (ViewProvider::getDetailPath(subname, path, append, det)) { |
| 705 |
if (det || !subname || !*subname) { |
| 706 |
return true; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
if (det) { |
| 711 |
delete det; |
| 712 |
det = nullptr; |
| 713 |
} |
| 714 |
|
| 715 |
const char* dot = strchr(subname, '.'); |
| 716 |
if (!dot) { |
| 717 |
return false; |
| 718 |
} |
| 719 |
auto obj = getObject(); |
| 720 |
if (!obj || !obj->isAttachedToDocument()) { |
| 721 |
return false; |
| 722 |
} |
| 723 |
auto sobj = obj->getSubObject(std::string(subname, dot - subname + 1).c_str()); |
| 724 |
if (!sobj) { |
| 725 |
return false; |
| 726 |
} |
| 727 |
auto vp = Application::Instance->getViewProvider(sobj); |
| 728 |
if (!vp) { |
| 729 |
return false; |
| 730 |
} |
| 731 |
|
| 732 |
auto childRoot = getChildRoot(); |
| 733 |
if (!childRoot) { |
| 734 |
path->truncate(len); |
| 735 |
} |
| 736 |
else { |
| 737 |
auto idx = pcModeSwitch->whichChild.getValue(); |
| 738 |
if (idx < 0 || pcModeSwitch->getChild(idx) != childRoot) { |
| 739 |
return false; |
| 740 |
} |
| 741 |
path->append(childRoot); |
| 742 |
} |
| 743 |
bool ret = false; |
| 744 |
if (path->getLength()) { |
| 745 |
SoNode* tail = path->getTail(); |
| 746 |
const SoChildList* children = tail->getChildren(); |
| 747 |
if (children && children->find(vp->getRoot()) >= 0) { |
| 748 |
ret = vp->getDetailPath(dot + 1, path, true, det); |
| 749 |
} |
| 750 |
} |
| 751 |
return ret; |
| 752 |
} |
| 753 |
|
| 754 |
void ViewProviderDocumentObject::onPropertyStatusChanged(const App::Property& prop, unsigned long oldStatus) |
| 755 |
{ |
| 756 |
(void)oldStatus; |
| 757 |
if (!App::Document::isAnyRestoring() && pcObject && pcObject->getDocument()) { |
| 758 |
pcObject->getDocument()->signalChangePropertyEditor(*pcObject->getDocument(), prop); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
ViewProviderDocumentObject* ViewProviderDocumentObject::getLinkedViewProvider( |
| 763 |
std::string* subname, |
| 764 |
bool recursive |
| 765 |
) const |
| 766 |
{ |
| 767 |
(void)subname; |
| 768 |
auto self = const_cast<ViewProviderDocumentObject*>(this); |
| 769 |
if (!pcObject || !pcObject->isAttachedToDocument()) { |
| 770 |
return self; |
| 771 |
} |
| 772 |
auto linked = pcObject->getLinkedObject(recursive); |
| 773 |
if (!linked || linked == pcObject) { |
| 774 |
return self; |
| 775 |
} |
| 776 |
auto res = freecad_cast<ViewProviderDocumentObject*>( |
| 777 |
Application::Instance->getViewProvider(linked) |
| 778 |
); |
| 779 |
if (!res) { |
| 780 |
res = self; |
| 781 |
} |
| 782 |
return res; |
| 783 |
} |
| 784 |
|
| 785 |
std::string ViewProviderDocumentObject::getFullName() const |
| 786 |
{ |
| 787 |
if (pcObject) { |
| 788 |
return pcObject->getFullName() + ".ViewObject"; |
| 789 |
} |
| 790 |
return std::string("?"); |
| 791 |
} |