| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2002 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 |
#include <QDir> |
| 24 |
#include <QFileInfo> |
| 25 |
#include <boost/algorithm/string/predicate.hpp> |
| 26 |
|
| 27 |
#include <Base/Console.h> |
| 28 |
#include <Base/Exception.h> |
| 29 |
#include <Base/Reader.h> |
| 30 |
#include <Base/Writer.h> |
| 31 |
#include <Base/Tools.h> |
| 32 |
|
| 33 |
#include "PropertyLinks.h" |
| 34 |
#include "Application.h" |
| 35 |
#include "Document.h" |
| 36 |
#include "DocumentObject.h" |
| 37 |
#include "DocumentObjectPy.h" |
| 38 |
#include "DocumentObserver.h" |
| 39 |
#include "ObjectIdentifier.h" |
| 40 |
#include "ElementNamingUtils.h" |
| 41 |
#include "GeoFeature.h" |
| 42 |
|
| 43 |
|
| 44 |
FC_LOG_LEVEL_INIT("PropertyLinks", true, true) |
| 45 |
|
| 46 |
using namespace App; |
| 47 |
using namespace Base; |
| 48 |
using namespace std; |
| 49 |
namespace sp = std::placeholders; |
| 50 |
|
| 51 |
//************************************************************************** |
| 52 |
//************************************************************************** |
| 53 |
// PropertyLinkBase |
| 54 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 55 |
|
| 56 |
TYPESYSTEM_SOURCE_ABSTRACT(App::PropertyLinkBase, App::Property) |
| 57 |
|
| 58 |
// clang-format off |
| 59 |
static std::unordered_map<std::string, std::set<PropertyLinkBase*>> _LabelMap; |
| 60 |
static std::unordered_map<App::DocumentObject*, std::unordered_set<PropertyLinkBase*>> _ElementRefMap; |
| 61 |
// clang-format on |
| 62 |
|
| 63 |
PropertyLinkBase::PropertyLinkBase() = default; |
| 64 |
|
| 65 |
PropertyLinkBase::~PropertyLinkBase() |
| 66 |
{ |
| 67 |
unregisterLabelReferences(); |
| 68 |
unregisterElementReference(); |
| 69 |
} |
| 70 |
|
| 71 |
void PropertyLinkBase::setAllowExternal(bool allow) |
| 72 |
{ |
| 73 |
setFlag(LinkAllowExternal, allow); |
| 74 |
} |
| 75 |
|
| 76 |
void PropertyLinkBase::setSilentRestore(bool allow) |
| 77 |
{ |
| 78 |
setFlag(LinkSilentRestore, allow); |
| 79 |
} |
| 80 |
|
| 81 |
void PropertyLinkBase::setReturnNewElement(bool enable) |
| 82 |
{ |
| 83 |
setFlag(LinkNewElement, enable); |
| 84 |
} |
| 85 |
|
| 86 |
void PropertyLinkBase::hasSetValue() |
| 87 |
{ |
| 88 |
auto owner = freecad_cast<DocumentObject*>(getContainer()); |
| 89 |
if (owner) { |
| 90 |
owner->clearOutListCache(); |
| 91 |
} |
| 92 |
Property::hasSetValue(); |
| 93 |
} |
| 94 |
|
| 95 |
bool PropertyLinkBase::isSame(const Property& other) const |
| 96 |
{ |
| 97 |
if (&other == this) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
if (other.isDerivedFrom<PropertyLinkBase>() |
| 101 |
|| getScope() != static_cast<const PropertyLinkBase*>(&other)->getScope()) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
static std::vector<App::DocumentObject*> ret; |
| 106 |
static std::vector<std::string> subs; |
| 107 |
static std::vector<App::DocumentObject*> ret2; |
| 108 |
static std::vector<std::string> subs2; |
| 109 |
|
| 110 |
ret.clear(); |
| 111 |
subs.clear(); |
| 112 |
ret2.clear(); |
| 113 |
subs2.clear(); |
| 114 |
getLinks(ret, true, &subs, false); |
| 115 |
static_cast<const PropertyLinkBase*>(&other)->getLinks(ret2, true, &subs2, true); |
| 116 |
|
| 117 |
return ret == ret2 && subs == subs2; |
| 118 |
} |
| 119 |
|
| 120 |
void PropertyLinkBase::unregisterElementReference() |
| 121 |
{ |
| 122 |
for (auto obj : _ElementRefs) { |
| 123 |
auto it = _ElementRefMap.find(obj); |
| 124 |
if (it != _ElementRefMap.end()) { |
| 125 |
it->second.erase(this); |
| 126 |
if (it->second.empty()) { |
| 127 |
_ElementRefMap.erase(it); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
_ElementRefs.clear(); |
| 132 |
} |
| 133 |
|
| 134 |
void PropertyLinkBase::unregisterLabelReferences() |
| 135 |
{ |
| 136 |
for (auto& label : _LabelRefs) { |
| 137 |
auto it = _LabelMap.find(label); |
| 138 |
if (it != _LabelMap.end()) { |
| 139 |
it->second.erase(this); |
| 140 |
if (it->second.empty()) { |
| 141 |
_LabelMap.erase(it); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
_LabelRefs.clear(); |
| 146 |
} |
| 147 |
|
| 148 |
void PropertyLinkBase::getLabelReferences(std::vector<std::string>& subs, const char* subname) |
| 149 |
{ |
| 150 |
const char* dot; |
| 151 |
for (; (subname = strchr(subname, '$')) != nullptr; subname = dot + 1) { |
| 152 |
++subname; |
| 153 |
dot = strchr(subname, '.'); |
| 154 |
if (!dot) { |
| 155 |
break; |
| 156 |
} |
| 157 |
subs.emplace_back(subname, dot - subname); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
void PropertyLinkBase::registerLabelReferences(std::vector<std::string>&& labels, bool reset) |
| 162 |
{ |
| 163 |
if (reset) { |
| 164 |
unregisterLabelReferences(); |
| 165 |
} |
| 166 |
for (auto& label : labels) { |
| 167 |
auto res = _LabelRefs.insert(std::move(label)); |
| 168 |
if (res.second) { |
| 169 |
_LabelMap[*res.first].insert(this); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void PropertyLinkBase::checkLabelReferences(const std::vector<std::string>& subs, bool reset) |
| 175 |
{ |
| 176 |
if (reset) { |
| 177 |
unregisterLabelReferences(); |
| 178 |
} |
| 179 |
std::vector<std::string> labels; |
| 180 |
for (auto& sub : subs) { |
| 181 |
labels.clear(); |
| 182 |
getLabelReferences(labels, sub.c_str()); |
| 183 |
registerLabelReferences(std::move(labels), false); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
std::string PropertyLinkBase::updateLabelReference(const App::DocumentObject* parent, |
| 188 |
const char* subname, |
| 189 |
const App::DocumentObject* obj, |
| 190 |
const std::string& ref, |
| 191 |
const char* newLabel) |
| 192 |
{ |
| 193 |
if (!obj || !obj->isAttachedToDocument() || !parent || !parent->isAttachedToDocument()) { |
| 194 |
return {}; |
| 195 |
} |
| 196 |
|
| 197 |
// Because the label is allowed to be the same across different |
| 198 |
// hierarchies, we have to search for all occurrences, and make sure the |
| 199 |
// referenced sub-object at the found hierarchy is actually the given |
| 200 |
// object. |
| 201 |
for (const char* pos = subname; ((pos = strstr(pos, ref.c_str())) != nullptr); |
| 202 |
pos += ref.size()) { |
| 203 |
auto sub = std::string(subname, pos + ref.size() - subname); |
| 204 |
auto sobj = parent->getSubObject(sub.c_str()); |
| 205 |
if (sobj == obj) { |
| 206 |
sub = subname; |
| 207 |
sub.replace(pos + 1 - subname, ref.size() - 2, newLabel); |
| 208 |
return sub; |
| 209 |
} |
| 210 |
} |
| 211 |
return {}; |
| 212 |
} |
| 213 |
|
| 214 |
std::vector<std::pair<Property*, std::unique_ptr<Property>>> |
| 215 |
PropertyLinkBase::updateLabelReferences(App::DocumentObject* obj, const char* newLabel) |
| 216 |
{ |
| 217 |
std::vector<std::pair<Property*, std::unique_ptr<Property>>> ret; |
| 218 |
if (!obj || !obj->isAttachedToDocument()) { |
| 219 |
return ret; |
| 220 |
} |
| 221 |
auto it = _LabelMap.find(obj->Label.getStrValue()); |
| 222 |
if (it == _LabelMap.end()) { |
| 223 |
return ret; |
| 224 |
} |
| 225 |
std::string ref("$"); |
| 226 |
ref += obj->Label.getValue(); |
| 227 |
ref += '.'; |
| 228 |
std::vector<PropertyLinkBase*> props; |
| 229 |
props.reserve(it->second.size()); |
| 230 |
props.insert(props.end(), it->second.begin(), it->second.end()); |
| 231 |
for (auto prop : props) { |
| 232 |
if (!prop->getContainer()) { |
| 233 |
continue; |
| 234 |
} |
| 235 |
std::unique_ptr<Property> copy(prop->CopyOnLabelChange(obj, ref, newLabel)); |
| 236 |
if (copy) { |
| 237 |
ret.emplace_back(prop, std::move(copy)); |
| 238 |
} |
| 239 |
} |
| 240 |
return ret; |
| 241 |
} |
| 242 |
|
| 243 |
static std::string propertyName(const Property* prop) |
| 244 |
{ |
| 245 |
if (!prop) { |
| 246 |
return {}; |
| 247 |
} |
| 248 |
if (!prop->getContainer() || !prop->hasName()) { |
| 249 |
auto xlink = freecad_cast<const PropertyXLink*>(prop); |
| 250 |
if (xlink) { |
| 251 |
return propertyName(xlink->parent()); |
| 252 |
} |
| 253 |
} |
| 254 |
return prop->getFullName(); |
| 255 |
} |
| 256 |
|
| 257 |
const std::unordered_set<PropertyLinkBase*>& |
| 258 |
PropertyLinkBase::getElementReferences(DocumentObject* feature) |
| 259 |
{ |
| 260 |
static std::unordered_set<PropertyLinkBase*> none; |
| 261 |
|
| 262 |
auto it = _ElementRefMap.find(feature); |
| 263 |
if (it == _ElementRefMap.end()) { |
| 264 |
return none; |
| 265 |
} |
| 266 |
|
| 267 |
return it->second; |
| 268 |
} |
| 269 |
|
| 270 |
void PropertyLinkBase::updateElementReferences(DocumentObject* feature, bool reverse) |
| 271 |
{ |
| 272 |
if (!feature || !feature->getNameInDocument()) { |
| 273 |
return; |
| 274 |
} |
| 275 |
auto it = _ElementRefMap.find(feature); |
| 276 |
if (it == _ElementRefMap.end()) { |
| 277 |
return; |
| 278 |
} |
| 279 |
std::vector<PropertyLinkBase*> props; |
| 280 |
props.reserve(it->second.size()); |
| 281 |
props.insert(props.end(), it->second.begin(), it->second.end()); |
| 282 |
for (auto prop : props) { |
| 283 |
if (prop->getContainer()) { |
| 284 |
try { |
| 285 |
prop->updateElementReference(feature, reverse, true); |
| 286 |
} |
| 287 |
catch (Base::Exception& e) { |
| 288 |
e.reportException(); |
| 289 |
FC_ERR("Failed to update element reference of " << propertyName(prop)); |
| 290 |
} |
| 291 |
catch (std::exception& e) { |
| 292 |
FC_ERR("Failed to update element reference of " << propertyName(prop) << ": " |
| 293 |
<< e.what()); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
void PropertyLinkBase::updateAllElementReferences(bool reverse) |
| 300 |
{ |
| 301 |
for (auto reference : _ElementRefMap) { |
| 302 |
for (auto prop : reference.second) { |
| 303 |
if (prop->getContainer()) { |
| 304 |
try { |
| 305 |
prop->updateElementReference(reference.first, reverse, true); |
| 306 |
} |
| 307 |
catch (Base::Exception& e) { |
| 308 |
e.reportException(); |
| 309 |
FC_ERR("Failed to update element reference of " << propertyName(prop)); |
| 310 |
} |
| 311 |
catch (std::exception& e) { |
| 312 |
FC_ERR("Failed to update element reference of " << propertyName(prop) << ": " |
| 313 |
<< e.what()); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
void PropertyLinkBase::_registerElementReference(App::DocumentObject* obj, |
| 321 |
std::string& sub, |
| 322 |
ShadowSub& shadow) |
| 323 |
{ |
| 324 |
if (!obj || !obj->getNameInDocument() || sub.empty()) { |
| 325 |
return; |
| 326 |
} |
| 327 |
if (shadow.newName.empty()) { |
| 328 |
_updateElementReference(nullptr, obj, sub, shadow, false); |
| 329 |
return; |
| 330 |
} |
| 331 |
GeoFeature* geo = nullptr; |
| 332 |
const char* element = nullptr; |
| 333 |
ShadowSub elementName; |
| 334 |
GeoFeature::resolveElement(obj, |
| 335 |
sub.c_str(), |
| 336 |
elementName, |
| 337 |
true, |
| 338 |
GeoFeature::ElementNameType::Export, |
| 339 |
nullptr, |
| 340 |
&element, |
| 341 |
&geo); |
| 342 |
if (!geo || !element || !element[0]) { |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
if (_ElementRefs.insert(geo).second) { |
| 347 |
_ElementRefMap[geo].insert(this); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
class StringGuard |
| 352 |
{ |
| 353 |
public: |
| 354 |
explicit StringGuard(char* c) |
| 355 |
: c(c) |
| 356 |
{ |
| 357 |
v1 = c[0]; |
| 358 |
v2 = c[1]; |
| 359 |
c[0] = '.'; |
| 360 |
c[1] = 0; |
| 361 |
} |
| 362 |
~StringGuard() |
| 363 |
{ |
| 364 |
c[0] = v1; |
| 365 |
c[1] = v2; |
| 366 |
} |
| 367 |
|
| 368 |
char* c; |
| 369 |
char v1; |
| 370 |
char v2; |
| 371 |
}; |
| 372 |
|
| 373 |
void PropertyLinkBase::restoreLabelReference(const DocumentObject* obj, |
| 374 |
std::string& subname, |
| 375 |
ShadowSub* shadow) |
| 376 |
{ |
| 377 |
std::ostringstream ss; |
| 378 |
char* sub = &subname[0]; |
| 379 |
char* next = sub; |
| 380 |
for (char* dot = strchr(next, '.'); dot; next = dot + 1, dot = strchr(next, '.')) { |
| 381 |
if (dot != next && dot[-1] != '@') { |
| 382 |
continue; |
| 383 |
} |
| 384 |
DocumentObject* sobj; |
| 385 |
try { |
| 386 |
StringGuard guard(dot - 1); |
| 387 |
sobj = obj->getSubObject(subname.c_str()); |
| 388 |
if (!sobj) { |
| 389 |
FC_ERR("Failed to restore label reference " << obj->getFullName() << '.' |
| 390 |
<< ss.str()); |
| 391 |
return; |
| 392 |
} |
| 393 |
} |
| 394 |
catch (...) { |
| 395 |
throw; |
| 396 |
} |
| 397 |
ss.write(sub, next - sub); |
| 398 |
ss << '$' << sobj->Label.getStrValue() << '.'; |
| 399 |
sub = dot + 1; |
| 400 |
} |
| 401 |
if (sub == subname.c_str()) { |
| 402 |
return; |
| 403 |
} |
| 404 |
|
| 405 |
size_t count = sub - subname.c_str(); |
| 406 |
const auto& newSub = ss.str(); |
| 407 |
if (shadow && shadow->oldName.size() >= count) { |
| 408 |
shadow->oldName = newSub + (shadow->oldName.c_str() + count); |
| 409 |
} |
| 410 |
if (shadow && shadow->newName.size() >= count) { |
| 411 |
shadow->newName = newSub + (shadow->newName.c_str() + count); |
| 412 |
} |
| 413 |
subname = newSub + sub; |
| 414 |
} |
| 415 |
|
| 416 |
bool PropertyLinkBase::_updateElementReference(DocumentObject* feature, |
| 417 |
App::DocumentObject* obj, |
| 418 |
std::string& sub, |
| 419 |
ShadowSub& shadow, |
| 420 |
bool reverse, |
| 421 |
bool notify) |
| 422 |
{ |
| 423 |
if (!obj || !obj->getNameInDocument()) { |
| 424 |
return false; |
| 425 |
} |
| 426 |
ShadowSub elementName; |
| 427 |
const char* subname; |
| 428 |
if (shadow.newName.size()) { |
| 429 |
subname = shadow.newName.c_str(); |
| 430 |
} |
| 431 |
else if (shadow.oldName.size()) { |
| 432 |
subname = shadow.oldName.c_str(); |
| 433 |
} |
| 434 |
else { |
| 435 |
subname = sub.c_str(); |
| 436 |
} |
| 437 |
GeoFeature* geo = nullptr; |
| 438 |
const char* element = nullptr; |
| 439 |
auto ret = GeoFeature::resolveElement(obj, |
| 440 |
subname, |
| 441 |
elementName, |
| 442 |
true, |
| 443 |
GeoFeature::ElementNameType::Export, |
| 444 |
feature, |
| 445 |
&element, |
| 446 |
&geo); |
| 447 |
if (!ret || !geo || !element || !element[0]) { |
| 448 |
if (elementName.oldName.size()) { |
| 449 |
shadow.oldName.swap(elementName.oldName); |
| 450 |
} |
| 451 |
return false; |
| 452 |
} |
| 453 |
|
| 454 |
if (_ElementRefs.insert(geo).second) { |
| 455 |
_ElementRefMap[geo].insert(this); |
| 456 |
} |
| 457 |
|
| 458 |
if (!reverse) { |
| 459 |
if (elementName.newName.empty()) { |
| 460 |
shadow.oldName.swap(elementName.oldName); |
| 461 |
return false; |
| 462 |
} |
| 463 |
if (shadow == elementName) { |
| 464 |
return false; |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
bool missing = GeoFeature::hasMissingElement(elementName.oldName.c_str()); |
| 469 |
if (feature == geo && (missing || reverse)) { |
| 470 |
// If the referenced element is missing, or we are generating element |
| 471 |
// map for the first time, or we are re-generating the element map due |
| 472 |
// to version change, i.e. 'reverse', try search by geometry first |
| 473 |
const char* oldElement = Data::findElementName(shadow.oldName.c_str()); |
| 474 |
if (!Data::hasMissingElement(oldElement)) { |
| 475 |
auto names = geo->searchElementCache(oldElement); |
| 476 |
if (names.empty()) { |
| 477 |
// try floating point tolerance |
| 478 |
names = geo->searchElementCache(oldElement, Data::SearchOptions()); |
| 479 |
} |
| 480 |
if (names.size()) { |
| 481 |
missing = false; |
| 482 |
std::string newsub(subname, strlen(subname) - strlen(element)); |
| 483 |
newsub += names.front(); |
| 484 |
GeoFeature::resolveElement(obj, |
| 485 |
newsub.c_str(), |
| 486 |
elementName, |
| 487 |
true, |
| 488 |
GeoFeature::ElementNameType::Export, |
| 489 |
feature); |
| 490 |
const auto& oldName = shadow.newName.size() ? shadow.newName : shadow.oldName; |
| 491 |
const auto& newName = |
| 492 |
elementName.newName.size() ? elementName.newName : elementName.oldName; |
| 493 |
if (oldName != newName) { |
| 494 |
FC_LOG(propertyName(this) |
| 495 |
<< " auto change element reference " << ret->getFullName() << " " |
| 496 |
<< oldName << " -> " << newName); |
| 497 |
} |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
if (notify) { |
| 503 |
aboutToSetValue(); |
| 504 |
} |
| 505 |
|
| 506 |
auto updateSub = [&](const std::string& newSub) { |
| 507 |
if (sub != newSub) { |
| 508 |
// signalUpdateElementReference(sub, newSub); |
| 509 |
sub = newSub; |
| 510 |
} |
| 511 |
}; |
| 512 |
|
| 513 |
if (missing) { |
| 514 |
FC_WARN(propertyName(this) |
| 515 |
<< " missing element reference " << ret->getFullName() << " " |
| 516 |
<< (elementName.newName.size() ? elementName.newName : elementName.oldName)); |
| 517 |
shadow.oldName.swap(elementName.oldName); |
| 518 |
} |
| 519 |
else { |
| 520 |
FC_TRACE(propertyName(this) << " element reference shadow update " << ret->getFullName() |
| 521 |
<< " " << shadow.newName << " -> " << elementName.newName); |
| 522 |
shadow.swap(elementName); |
| 523 |
if (shadow.newName.size() && Data::hasMappedElementName(sub.c_str())) { |
| 524 |
updateSub(shadow.newName); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
if (reverse) { |
| 529 |
if (shadow.newName.size() && Data::hasMappedElementName(sub.c_str())) { |
| 530 |
updateSub(shadow.newName); |
| 531 |
} |
| 532 |
else { |
| 533 |
updateSub(shadow.oldName); |
| 534 |
} |
| 535 |
return true; |
| 536 |
} |
| 537 |
if (missing) { |
| 538 |
if (sub != shadow.newName) { |
| 539 |
updateSub(shadow.oldName); |
| 540 |
} |
| 541 |
return true; |
| 542 |
} |
| 543 |
auto pos2 = shadow.newName.rfind('.'); |
| 544 |
if (pos2 == std::string::npos) { |
| 545 |
return true; |
| 546 |
} |
| 547 |
++pos2; |
| 548 |
auto pos = sub.rfind('.'); |
| 549 |
if (pos == std::string::npos) { |
| 550 |
pos = 0; |
| 551 |
} |
| 552 |
else { |
| 553 |
++pos; |
| 554 |
} |
| 555 |
if (pos == pos2) { |
| 556 |
if (sub.compare(pos, sub.size() - pos, &shadow.newName[pos2]) != 0) { |
| 557 |
FC_LOG("element reference update " << sub << " -> " << shadow.newName); |
| 558 |
std::string newSub(sub); |
| 559 |
newSub.replace(pos, sub.size() - pos, &shadow.newName[pos2]); |
| 560 |
updateSub(newSub); |
| 561 |
} |
| 562 |
} |
| 563 |
else if (sub != shadow.oldName) { |
| 564 |
FC_LOG("element reference update " << sub << " -> " << shadow.oldName); |
| 565 |
updateSub(shadow.oldName); |
| 566 |
} |
| 567 |
return true; |
| 568 |
} |
| 569 |
|
| 570 |
std::pair<DocumentObject*, std::string> |
| 571 |
PropertyLinkBase::tryReplaceLink(const PropertyContainer* owner, |
| 572 |
DocumentObject* obj, |
| 573 |
const DocumentObject* parent, |
| 574 |
DocumentObject* oldObj, |
| 575 |
DocumentObject* newObj, |
| 576 |
const char* subname) |
| 577 |
{ |
| 578 |
std::pair<DocumentObject*, std::string> res; |
| 579 |
res.first = 0; |
| 580 |
if (!obj) { |
| 581 |
return res; |
| 582 |
} |
| 583 |
|
| 584 |
if (oldObj == obj) { |
| 585 |
if (owner == parent) { |
| 586 |
res.first = newObj; |
| 587 |
if (subname) { |
| 588 |
res.second = subname; |
| 589 |
} |
| 590 |
return res; |
| 591 |
} |
| 592 |
return res; |
| 593 |
} |
| 594 |
else if (newObj == obj) { |
| 595 |
// This means the new object is already sub-object of this parent |
| 596 |
// (consider a case of swapping the tool and base object of the Cut |
| 597 |
// feature). We'll swap the old and new object. |
| 598 |
return tryReplaceLink(owner, obj, parent, newObj, oldObj, subname); |
| 599 |
} |
| 600 |
if (!subname || !subname[0]) { |
| 601 |
return res; |
| 602 |
} |
| 603 |
|
| 604 |
App::DocumentObject* prev = obj; |
| 605 |
std::size_t prevPos = 0; |
| 606 |
std::string sub = subname; |
| 607 |
for (auto pos = sub.find('.'); pos != std::string::npos; pos = sub.find('.', pos)) { |
| 608 |
++pos; |
| 609 |
char c = sub[pos]; |
| 610 |
if (c == '.') { |
| 611 |
continue; |
| 612 |
} |
| 613 |
sub[pos] = 0; |
| 614 |
auto sobj = obj->getSubObject(sub.c_str()); |
| 615 |
sub[pos] = c; |
| 616 |
if (!sobj) { |
| 617 |
break; |
| 618 |
} |
| 619 |
if (sobj == oldObj) { |
| 620 |
if (prev == parent) { |
| 621 |
if (sub[prevPos] == '$') { |
| 622 |
sub.replace(prevPos + 1, pos - 1 - prevPos, newObj->Label.getValue()); |
| 623 |
} |
| 624 |
else { |
| 625 |
sub.replace(prevPos, pos - 1 - prevPos, newObj->getNameInDocument()); |
| 626 |
} |
| 627 |
res.first = obj; |
| 628 |
res.second = std::move(sub); |
| 629 |
return res; |
| 630 |
} |
| 631 |
break; |
| 632 |
} |
| 633 |
else if (sobj == newObj) { |
| 634 |
return tryReplaceLink(owner, obj, parent, newObj, oldObj, subname); |
| 635 |
} |
| 636 |
else if (prev == parent) { |
| 637 |
break; |
| 638 |
} |
| 639 |
prev = sobj; |
| 640 |
prevPos = pos; |
| 641 |
} |
| 642 |
return res; |
| 643 |
} |
| 644 |
|
| 645 |
std::pair<DocumentObject*, std::vector<std::string>> |
| 646 |
PropertyLinkBase::tryReplaceLinkSubs(const PropertyContainer* owner, |
| 647 |
DocumentObject* obj, |
| 648 |
const DocumentObject* parent, |
| 649 |
DocumentObject* oldObj, |
| 650 |
DocumentObject* newObj, |
| 651 |
const std::vector<std::string>& subs) |
| 652 |
{ |
| 653 |
std::pair<DocumentObject*, std::vector<std::string>> res; |
| 654 |
res.first = 0; |
| 655 |
if (!obj) { |
| 656 |
return res; |
| 657 |
} |
| 658 |
|
| 659 |
auto r = tryReplaceLink(owner, obj, parent, oldObj, newObj); |
| 660 |
if (r.first) { |
| 661 |
res.first = r.first; |
| 662 |
res.second = subs; |
| 663 |
return res; |
| 664 |
} |
| 665 |
for (auto it = subs.begin(); it != subs.end(); ++it) { |
| 666 |
auto r = tryReplaceLink(owner, obj, parent, oldObj, newObj, it->c_str()); |
| 667 |
if (r.first) { |
| 668 |
if (!res.first) { |
| 669 |
res.first = r.first; |
| 670 |
res.second.insert(res.second.end(), subs.begin(), it); |
| 671 |
} |
| 672 |
res.second.push_back(std::move(r.second)); |
| 673 |
} |
| 674 |
else if (res.first) { |
| 675 |
res.second.push_back(*it); |
| 676 |
} |
| 677 |
} |
| 678 |
return res; |
| 679 |
} |
| 680 |
|
| 681 |
//************************************************************************** |
| 682 |
//************************************************************************** |
| 683 |
// PropertyLinkListBase |
| 684 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 685 |
|
| 686 |
TYPESYSTEM_SOURCE_ABSTRACT(App::PropertyLinkListBase, App::PropertyLinkBase) |
| 687 |
|
| 688 |
//************************************************************************** |
| 689 |
//************************************************************************** |
| 690 |
// PropertyLink |
| 691 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 692 |
|
| 693 |
TYPESYSTEM_SOURCE(App::PropertyLink, App::PropertyLinkBase) |
| 694 |
TYPESYSTEM_SOURCE(App::PropertyLinkChild, App::PropertyLink) |
| 695 |
TYPESYSTEM_SOURCE(App::PropertyLinkGlobal, App::PropertyLink) |
| 696 |
TYPESYSTEM_SOURCE(App::PropertyLinkHidden, App::PropertyLink) |
| 697 |
|
| 698 |
//************************************************************************** |
| 699 |
// Construction/Destruction |
| 700 |
|
| 701 |
|
| 702 |
PropertyLink::PropertyLink() = default; |
| 703 |
|
| 704 |
PropertyLink::~PropertyLink() |
| 705 |
{ |
| 706 |
resetLink(); |
| 707 |
} |
| 708 |
|
| 709 |
//************************************************************************** |
| 710 |
// Base class implementer |
| 711 |
|
| 712 |
void PropertyLink::resetLink() |
| 713 |
{ |
| 714 |
// in case this property gets dynamically removed |
| 715 |
// maintain the back link in the DocumentObject class if it is from a document object |
| 716 |
if (_pcScope != LinkScope::Hidden && _pcLink && getContainer() |
| 717 |
&& getContainer()->isDerivedFrom<App::DocumentObject>()) { |
| 718 |
App::DocumentObject* parent = static_cast<DocumentObject*>(getContainer()); |
| 719 |
// before accessing internals make sure the object is not about to be destroyed |
| 720 |
// otherwise the backlink contains dangling pointers |
| 721 |
if (!parent->testStatus(ObjectStatus::Destroy)) { |
| 722 |
if (_pcLink) { |
| 723 |
_pcLink->_removeBackLink(parent); |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
_pcLink = nullptr; |
| 728 |
} |
| 729 |
|
| 730 |
void PropertyLink::setValue(App::DocumentObject* lValue) |
| 731 |
{ |
| 732 |
auto parent = dynamic_cast<App::DocumentObject*>(getContainer()); |
| 733 |
if (!testFlag(LinkAllowExternal) && parent && lValue |
| 734 |
&& parent->getDocument() != lValue->getDocument()) { |
| 735 |
throw Base::ValueError("PropertyLink does not support external object"); |
| 736 |
} |
| 737 |
|
| 738 |
aboutToSetValue(); |
| 739 |
|
| 740 |
// maintain the back link in the DocumentObject class if it is from a document object |
| 741 |
if (_pcScope != LinkScope::Hidden && parent) { |
| 742 |
// before accessing internals make sure the object is not about to be destroyed |
| 743 |
// otherwise the backlink contains dangling pointers |
| 744 |
if (!parent->testStatus(ObjectStatus::Destroy)) { |
| 745 |
if (_pcLink) { |
| 746 |
_pcLink->_removeBackLink(parent); |
| 747 |
} |
| 748 |
if (lValue) { |
| 749 |
lValue->_addBackLink(parent); |
| 750 |
} |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
_pcLink = lValue; |
| 755 |
hasSetValue(); |
| 756 |
} |
| 757 |
|
| 758 |
App::DocumentObject* PropertyLink::getValue() const |
| 759 |
{ |
| 760 |
return _pcLink; |
| 761 |
} |
| 762 |
|
| 763 |
App::DocumentObject* PropertyLink::getValue(Base::Type t) const |
| 764 |
{ |
| 765 |
return (_pcLink && _pcLink->isDerivedFrom(t)) ? _pcLink : nullptr; |
| 766 |
} |
| 767 |
|
| 768 |
PyObject* PropertyLink::getPyObject() |
| 769 |
{ |
| 770 |
if (_pcLink) { |
| 771 |
return _pcLink->getPyObject(); |
| 772 |
} |
| 773 |
else { |
| 774 |
Py_Return; |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
void PropertyLink::setPyObject(PyObject* value) |
| 779 |
{ |
| 780 |
Base::PyTypeCheck(&value, &DocumentObjectPy::Type); |
| 781 |
if (value) { |
| 782 |
DocumentObjectPy* pcObject = static_cast<DocumentObjectPy*>(value); |
| 783 |
setValue(pcObject->getDocumentObjectPtr()); |
| 784 |
} |
| 785 |
else { |
| 786 |
setValue(nullptr); |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
void PropertyLink::Save(Base::Writer& writer) const |
| 791 |
{ |
| 792 |
writer.Stream() << writer.ind() << "<Link value=\"" << (_pcLink ? _pcLink->getExportName() : "") |
| 793 |
<< "\"/>" << std::endl; |
| 794 |
} |
| 795 |
|
| 796 |
void PropertyLink::Restore(Base::XMLReader& reader) |
| 797 |
{ |
| 798 |
// read my element |
| 799 |
reader.readElement("Link"); |
| 800 |
// get the value of my attribute |
| 801 |
std::string name = reader.getName(reader.getAttribute<const char*>("value")); |
| 802 |
|
| 803 |
// Property not in a DocumentObject! |
| 804 |
assert(getContainer()->isDerivedFrom<App::DocumentObject>()); |
| 805 |
|
| 806 |
if (!name.empty()) { |
| 807 |
DocumentObject* parent = static_cast<DocumentObject*>(getContainer()); |
| 808 |
|
| 809 |
App::Document* document = parent->getDocument(); |
| 810 |
DocumentObject* object = document ? document->getObject(name.c_str()) : nullptr; |
| 811 |
if (!object) { |
| 812 |
if (reader.isVerbose()) { |
| 813 |
Base::Console().warning("Lost link to '%s' while loading, maybe " |
| 814 |
"an object was not loaded correctly\n", |
| 815 |
name.c_str()); |
| 816 |
} |
| 817 |
} |
| 818 |
else if (parent == object) { |
| 819 |
if (reader.isVerbose()) { |
| 820 |
Base::Console().warning("Object '%s' links to itself, nullify it\n", name.c_str()); |
| 821 |
} |
| 822 |
object = nullptr; |
| 823 |
} |
| 824 |
|
| 825 |
setValue(object); |
| 826 |
} |
| 827 |
else { |
| 828 |
setValue(nullptr); |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
Property* PropertyLink::Copy() const |
| 833 |
{ |
| 834 |
PropertyLink* p = new PropertyLink(); |
| 835 |
p->_pcLink = _pcLink; |
| 836 |
return p; |
| 837 |
} |
| 838 |
|
| 839 |
void PropertyLink::Paste(const Property& from) |
| 840 |
{ |
| 841 |
if (!from.isDerivedFrom<PropertyLink>()) { |
| 842 |
throw Base::TypeError("Incompatible property to paste to"); |
| 843 |
} |
| 844 |
|
| 845 |
setValue(static_cast<const PropertyLink&>(from)._pcLink); |
| 846 |
} |
| 847 |
|
| 848 |
void PropertyLink::getLinks(std::vector<App::DocumentObject*>& objs, |
| 849 |
bool all, |
| 850 |
std::vector<std::string>* subs, |
| 851 |
bool newStyle) const |
| 852 |
{ |
| 853 |
(void)newStyle; |
| 854 |
(void)subs; |
| 855 |
if ((all || _pcScope != LinkScope::Hidden) && _pcLink && _pcLink->isAttachedToDocument()) { |
| 856 |
objs.push_back(_pcLink); |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
void PropertyLink::getLinksTo(std::vector<App::ObjectIdentifier>& identifiers, |
| 861 |
App::DocumentObject* obj, |
| 862 |
const char* subname, |
| 863 |
bool all) const |
| 864 |
{ |
| 865 |
(void)subname; |
| 866 |
if (!all && _pcScope == LinkScope::Hidden) { |
| 867 |
return; // Don't get hidden links unless all is specified. |
| 868 |
} |
| 869 |
if (obj && _pcLink == obj) { |
| 870 |
identifiers.emplace_back(*this); |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
void PropertyLink::breakLink(App::DocumentObject* obj, bool clear) |
| 875 |
{ |
| 876 |
if (_pcLink == obj || (clear && getContainer() == obj)) { |
| 877 |
setValue(nullptr); |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
bool PropertyLink::adjustLink(const std::set<App::DocumentObject*>& inList) |
| 882 |
{ |
| 883 |
(void)inList; |
| 884 |
return false; |
| 885 |
} |
| 886 |
|
| 887 |
Property* PropertyLink::CopyOnLinkReplace(const App::DocumentObject* parent, |
| 888 |
App::DocumentObject* oldObj, |
| 889 |
App::DocumentObject* newObj) const |
| 890 |
{ |
| 891 |
auto res = tryReplaceLink(getContainer(), _pcLink, parent, oldObj, newObj); |
| 892 |
if (res.first) { |
| 893 |
auto p = new PropertyLink(); |
| 894 |
p->_pcLink = res.first; |
| 895 |
return p; |
| 896 |
} |
| 897 |
return nullptr; |
| 898 |
} |
| 899 |
|
| 900 |
//************************************************************************** |
| 901 |
// PropertyLinkList |
| 902 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 903 |
|
| 904 |
TYPESYSTEM_SOURCE(App::PropertyLinkList, App::PropertyLinkListBase) |
| 905 |
TYPESYSTEM_SOURCE(App::PropertyLinkListChild, App::PropertyLinkList) |
| 906 |
TYPESYSTEM_SOURCE(App::PropertyLinkListGlobal, App::PropertyLinkList) |
| 907 |
TYPESYSTEM_SOURCE(App::PropertyLinkListHidden, App::PropertyLinkList) |
| 908 |
|
| 909 |
//************************************************************************** |
| 910 |
// Construction/Destruction |
| 911 |
|
| 912 |
|
| 913 |
PropertyLinkList::PropertyLinkList() = default; |
| 914 |
|
| 915 |
PropertyLinkList::~PropertyLinkList() |
| 916 |
{ |
| 917 |
// in case this property gety dynamically removed |
| 918 |
|
| 919 |
// maintain the back link in the DocumentObject class |
| 920 |
if (_pcScope != LinkScope::Hidden && !_lValueList.empty() && getContainer() |
| 921 |
&& getContainer()->isDerivedFrom<App::DocumentObject>()) { |
| 922 |
App::DocumentObject* parent = static_cast<DocumentObject*>(getContainer()); |
| 923 |
// before accessing internals make sure the object is not about to be destroyed |
| 924 |
// otherwise the backlink contains dangling pointers |
| 925 |
if (!parent->testStatus(ObjectStatus::Destroy)) { |
| 926 |
for (auto* obj : _lValueList) { |
| 927 |
if (obj) { |
| 928 |
obj->_removeBackLink(parent); |
| 929 |
} |
| 930 |
} |
| 931 |
} |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
void PropertyLinkList::setSize(int newSize) |
| 936 |
{ |
| 937 |
for (int i = newSize; i < (int)_lValueList.size(); ++i) { |
| 938 |
auto obj = _lValueList[i]; |
| 939 |
if (!obj || !obj->isAttachedToDocument()) { |
| 940 |
continue; |
| 941 |
} |
| 942 |
_nameMap.erase(obj->getNameInDocument()); |
| 943 |
|
| 944 |
if (_pcScope != LinkScope::Hidden) { |
| 945 |
obj->_removeBackLink(static_cast<DocumentObject*>(getContainer())); |
| 946 |
} |
| 947 |
} |
| 948 |
_lValueList.resize(newSize); |
| 949 |
} |
| 950 |
|
| 951 |
void PropertyLinkList::setSize(int newSize, const_reference def) |
| 952 |
{ |
| 953 |
auto oldSize = getSize(); |
| 954 |
setSize(newSize); |
| 955 |
for (auto i = oldSize; i < newSize; ++i) { |
| 956 |
_lValueList[i] = def; |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
void PropertyLinkList::set1Value(int idx, DocumentObject* const& value) |
| 961 |
{ |
| 962 |
DocumentObject* obj = nullptr; |
| 963 |
if (idx >= 0 && idx < (int)_lValueList.size()) { |
| 964 |
obj = _lValueList[idx]; |
| 965 |
if (obj == value) { |
| 966 |
return; |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
if (!value || !value->isAttachedToDocument()) { |
| 971 |
throw Base::ValueError("invalid document object"); |
| 972 |
} |
| 973 |
|
| 974 |
_nameMap.clear(); |
| 975 |
|
| 976 |
if (getContainer() && getContainer()->isDerivedFrom<App::DocumentObject>()) { |
| 977 |
App::DocumentObject* parent = static_cast<DocumentObject*>(getContainer()); |
| 978 |
// before accessing internals make sure the object is not about to be destroyed |
| 979 |
// otherwise the backlink contains dangling pointers |
| 980 |
if (!parent->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 981 |
if (obj) { |
| 982 |
obj->_removeBackLink(static_cast<DocumentObject*>(getContainer())); |
| 983 |
} |
| 984 |
if (value) { |
| 985 |
value->_addBackLink(static_cast<DocumentObject*>(getContainer())); |
| 986 |
} |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
inherited::set1Value(idx, value); |
| 991 |
} |
| 992 |
|
| 993 |
void PropertyLinkList::setValues(const std::vector<DocumentObject*>& value) |
| 994 |
{ |
| 995 |
if (value.size() == 1 && !value[0]) { |
| 996 |
// one null element means clear, as backward compatibility for old code |
| 997 |
setValues(std::vector<DocumentObject*>()); |
| 998 |
return; |
| 999 |
} |
| 1000 |
|
| 1001 |
auto parent = freecad_cast<App::DocumentObject*>(getContainer()); |
| 1002 |
for (auto obj : value) { |
| 1003 |
if (!obj || !obj->isAttachedToDocument()) { |
| 1004 |
throw Base::ValueError("PropertyLinkList: invalid document object"); |
| 1005 |
} |
| 1006 |
if (!testFlag(LinkAllowExternal) && parent && parent->getDocument() != obj->getDocument()) { |
| 1007 |
throw Base::ValueError("PropertyLinkList does not support external object"); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
_nameMap.clear(); |
| 1011 |
|
| 1012 |
// maintain the back link in the DocumentObject class |
| 1013 |
if (parent) { |
| 1014 |
// before accessing internals make sure the object is not about to be destroyed |
| 1015 |
// otherwise the backlink contains dangling pointers |
| 1016 |
if (!parent->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 1017 |
for (auto* obj : _lValueList) { |
| 1018 |
if (obj) { |
| 1019 |
obj->_removeBackLink(parent); |
| 1020 |
} |
| 1021 |
} |
| 1022 |
for (auto* obj : value) { |
| 1023 |
if (obj) { |
| 1024 |
obj->_addBackLink(parent); |
| 1025 |
} |
| 1026 |
} |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
inherited::setValues(value); |
| 1031 |
} |
| 1032 |
|
| 1033 |
PyObject* PropertyLinkList::getPyObject() |
| 1034 |
{ |
| 1035 |
int count = getSize(); |
| 1036 |
#if 0 // FIXME: Should switch to tuple |
| 1037 |
Py::Tuple sequence(count); |
| 1038 |
#else |
| 1039 |
Py::List sequence(count); |
| 1040 |
#endif |
| 1041 |
for (int i = 0; i < count; i++) { |
| 1042 |
auto obj = _lValueList[i]; |
| 1043 |
if (obj && obj->isAttachedToDocument()) { |
| 1044 |
sequence.setItem(i, Py::asObject(_lValueList[i]->getPyObject())); |
| 1045 |
} |
| 1046 |
else { |
| 1047 |
sequence.setItem(i, Py::None()); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
return Py::new_reference_to(sequence); |
| 1052 |
} |
| 1053 |
|
| 1054 |
DocumentObject* PropertyLinkList::getPyValue(PyObject* item) const |
| 1055 |
{ |
| 1056 |
Base::PyTypeCheck(&item, &DocumentObjectPy::Type); |
| 1057 |
|
| 1058 |
return item ? static_cast<DocumentObjectPy*>(item)->getDocumentObjectPtr() : nullptr; |
| 1059 |
} |
| 1060 |
|
| 1061 |
void PropertyLinkList::Save(Base::Writer& writer) const |
| 1062 |
{ |
| 1063 |
writer.Stream() << writer.ind() << "<LinkList count=\"" << getSize() << "\">" << endl; |
| 1064 |
writer.incInd(); |
| 1065 |
for (int i = 0; i < getSize(); i++) { |
| 1066 |
DocumentObject* obj = _lValueList[i]; |
| 1067 |
if (obj) { |
| 1068 |
writer.Stream() << writer.ind() << "<Link value=\"" << obj->getExportName() << "\"/>" |
| 1069 |
<< endl; |
| 1070 |
} |
| 1071 |
else { |
| 1072 |
writer.Stream() << writer.ind() << "<Link value=\"\"/>" << endl; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
writer.decInd(); |
| 1077 |
writer.Stream() << writer.ind() << "</LinkList>" << endl; |
| 1078 |
} |
| 1079 |
|
| 1080 |
void PropertyLinkList::Restore(Base::XMLReader& reader) |
| 1081 |
{ |
| 1082 |
// read my element |
| 1083 |
reader.readElement("LinkList"); |
| 1084 |
// get the value of my attribute |
| 1085 |
int count = reader.getAttribute<long>("count"); |
| 1086 |
App::PropertyContainer* container = getContainer(); |
| 1087 |
if (!container) { |
| 1088 |
throw Base::RuntimeError("Property is not part of a container"); |
| 1089 |
} |
| 1090 |
if (!container->isDerivedFrom<App::DocumentObject>()) { |
| 1091 |
std::stringstream str; |
| 1092 |
str << "Container is not a document object (" << container->getTypeId().getName() << ")"; |
| 1093 |
throw Base::TypeError(str.str()); |
| 1094 |
} |
| 1095 |
|
| 1096 |
std::vector<DocumentObject*> values; |
| 1097 |
values.reserve(count); |
| 1098 |
for (int i = 0; i < count; i++) { |
| 1099 |
reader.readElement("Link"); |
| 1100 |
std::string name = reader.getName(reader.getAttribute<const char*>("value")); |
| 1101 |
// In order to do copy/paste it must be allowed to have defined some |
| 1102 |
// referenced objects in XML which do not exist anymore in the new |
| 1103 |
// document. Thus, we should silently ignore this. |
| 1104 |
// Property not in an object! |
| 1105 |
DocumentObject* father = static_cast<DocumentObject*>(getContainer()); |
| 1106 |
App::Document* document = father->getDocument(); |
| 1107 |
DocumentObject* child = document ? document->getObject(name.c_str()) : nullptr; |
| 1108 |
if (child) { |
| 1109 |
values.push_back(child); |
| 1110 |
} |
| 1111 |
else if (reader.isVerbose()) { |
| 1112 |
FC_WARN("Lost link to " << (document ? document->getName() : "") << " " << name |
| 1113 |
<< " while loading, maybe an object was not loaded correctly"); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
reader.readEndElement("LinkList"); |
| 1118 |
|
| 1119 |
// assignment |
| 1120 |
setValues(values); |
| 1121 |
} |
| 1122 |
|
| 1123 |
Property* PropertyLinkList::CopyOnLinkReplace(const App::DocumentObject* parent, |
| 1124 |
App::DocumentObject* oldObj, |
| 1125 |
App::DocumentObject* newObj) const |
| 1126 |
{ |
| 1127 |
std::vector<DocumentObject*> links; |
| 1128 |
bool copied = false; |
| 1129 |
bool found = false; |
| 1130 |
for (auto it = _lValueList.begin(); it != _lValueList.end(); ++it) { |
| 1131 |
auto res = tryReplaceLink(getContainer(), *it, parent, oldObj, newObj); |
| 1132 |
if (res.first) { |
| 1133 |
found = true; |
| 1134 |
if (!copied) { |
| 1135 |
copied = true; |
| 1136 |
links.insert(links.end(), _lValueList.begin(), it); |
| 1137 |
} |
| 1138 |
links.push_back(res.first); |
| 1139 |
} |
| 1140 |
else if (*it == newObj) { |
| 1141 |
// in case newObj already exists here, we shall remove all existing |
| 1142 |
// entry, and insert it to take over oldObj's position. |
| 1143 |
if (!copied) { |
| 1144 |
copied = true; |
| 1145 |
links.insert(links.end(), _lValueList.begin(), it); |
| 1146 |
} |
| 1147 |
} |
| 1148 |
else if (copied) { |
| 1149 |
links.push_back(*it); |
| 1150 |
} |
| 1151 |
} |
| 1152 |
if (!found) { |
| 1153 |
return nullptr; |
| 1154 |
} |
| 1155 |
auto p = new PropertyLinkList(); |
| 1156 |
p->_lValueList = std::move(links); |
| 1157 |
return p; |
| 1158 |
} |
| 1159 |
|
| 1160 |
Property* PropertyLinkList::Copy() const |
| 1161 |
{ |
| 1162 |
PropertyLinkList* p = new PropertyLinkList(); |
| 1163 |
p->_lValueList = _lValueList; |
| 1164 |
return p; |
| 1165 |
} |
| 1166 |
|
| 1167 |
void PropertyLinkList::Paste(const Property& from) |
| 1168 |
{ |
| 1169 |
if (!from.isDerivedFrom<PropertyLinkList>()) { |
| 1170 |
throw Base::TypeError("Incompatible property to paste to"); |
| 1171 |
} |
| 1172 |
|
| 1173 |
setValues(static_cast<const PropertyLinkList&>(from)._lValueList); |
| 1174 |
} |
| 1175 |
|
| 1176 |
unsigned int PropertyLinkList::getMemSize() const |
| 1177 |
{ |
| 1178 |
return static_cast<unsigned int>(_lValueList.size() * sizeof(App::DocumentObject*)); |
| 1179 |
} |
| 1180 |
|
| 1181 |
|
| 1182 |
DocumentObject* PropertyLinkList::find(const char* name, int* pindex) const |
| 1183 |
{ |
| 1184 |
const int DONT_MAP_UNDER = 10; |
| 1185 |
if (!name) { |
| 1186 |
return nullptr; |
| 1187 |
} |
| 1188 |
if (_lValueList.size() <= DONT_MAP_UNDER) { |
| 1189 |
int index = -1; |
| 1190 |
for (auto obj : _lValueList) { |
| 1191 |
++index; |
| 1192 |
if (obj && obj->getNameInDocument() && boost::equals(name, obj->getNameInDocument())) { |
| 1193 |
if (pindex) { |
| 1194 |
*pindex = index; |
| 1195 |
} |
| 1196 |
return obj; |
| 1197 |
} |
| 1198 |
} |
| 1199 |
return nullptr; |
| 1200 |
} |
| 1201 |
// We're using a map. Do we need to (re)create it? |
| 1202 |
if (_nameMap.empty() || _nameMap.size() > _lValueList.size()) { |
| 1203 |
_nameMap.clear(); |
| 1204 |
for (int i = 0; i < (int)_lValueList.size(); ++i) { |
| 1205 |
auto obj = _lValueList[i]; |
| 1206 |
if (obj && obj->isAttachedToDocument()) { |
| 1207 |
_nameMap[obj->getNameInDocument()] = i; |
| 1208 |
} |
| 1209 |
} |
| 1210 |
} |
| 1211 |
// Now lookup up in that map |
| 1212 |
auto it = _nameMap.find(name); |
| 1213 |
if (it == _nameMap.end()) { |
| 1214 |
return nullptr; |
| 1215 |
} |
| 1216 |
if (pindex) { |
| 1217 |
*pindex = it->second; |
| 1218 |
} |
| 1219 |
return _lValueList[it->second]; |
| 1220 |
} |
| 1221 |
|
| 1222 |
DocumentObject* PropertyLinkList::findUsingMap(const std::string& name, int* pindex) const |
| 1223 |
{ |
| 1224 |
if (_nameMap.size() == _lValueList.size()) { |
| 1225 |
auto it = _nameMap.find(name); |
| 1226 |
if (it == _nameMap.end()) { |
| 1227 |
return nullptr; |
| 1228 |
} |
| 1229 |
if (pindex) { |
| 1230 |
*pindex = it->second; |
| 1231 |
} |
| 1232 |
return _lValueList[it->second]; |
| 1233 |
} |
| 1234 |
return find(name.c_str(), pindex); |
| 1235 |
} |
| 1236 |
|
| 1237 |
void PropertyLinkList::getLinks(std::vector<App::DocumentObject*>& objs, |
| 1238 |
bool all, |
| 1239 |
std::vector<std::string>* subs, |
| 1240 |
bool newStyle) const |
| 1241 |
{ |
| 1242 |
(void)subs; |
| 1243 |
(void)newStyle; |
| 1244 |
if (all || _pcScope != LinkScope::Hidden) { |
| 1245 |
objs.reserve(objs.size() + _lValueList.size()); |
| 1246 |
for (auto obj : _lValueList) { |
| 1247 |
if (obj && obj->isAttachedToDocument()) { |
| 1248 |
objs.push_back(obj); |
| 1249 |
} |
| 1250 |
} |
| 1251 |
} |
| 1252 |
} |
| 1253 |
|
| 1254 |
void PropertyLinkList::getLinksTo(std::vector<App::ObjectIdentifier>& identifiers, |
| 1255 |
App::DocumentObject* obj, |
| 1256 |
const char* subname, |
| 1257 |
bool all) const |
| 1258 |
{ |
| 1259 |
(void)subname; |
| 1260 |
if (!obj || (!all && _pcScope == LinkScope::Hidden)) { |
| 1261 |
return; |
| 1262 |
} |
| 1263 |
int i = -1; |
| 1264 |
for (auto docObj : _lValueList) { |
| 1265 |
++i; |
| 1266 |
if (docObj == obj) { |
| 1267 |
identifiers.emplace_back(*this, i); |
| 1268 |
break; |
| 1269 |
} |
| 1270 |
} |
| 1271 |
} |
| 1272 |
|
| 1273 |
void PropertyLinkList::breakLink(App::DocumentObject* obj, bool clear) |
| 1274 |
{ |
| 1275 |
if (clear && getContainer() == obj) { |
| 1276 |
setValues({}); |
| 1277 |
return; |
| 1278 |
} |
| 1279 |
std::vector<App::DocumentObject*> values; |
| 1280 |
values.reserve(_lValueList.size()); |
| 1281 |
for (auto o : _lValueList) { |
| 1282 |
if (o != obj) { |
| 1283 |
values.push_back(o); |
| 1284 |
} |
| 1285 |
} |
| 1286 |
if (values.size() != _lValueList.size()) { |
| 1287 |
setValues(values); |
| 1288 |
} |
| 1289 |
} |
| 1290 |
|
| 1291 |
bool PropertyLinkList::adjustLink(const std::set<App::DocumentObject*>& inList) |
| 1292 |
{ |
| 1293 |
(void)inList; |
| 1294 |
return false; |
| 1295 |
} |
| 1296 |
|
| 1297 |
|
| 1298 |
//************************************************************************** |
| 1299 |
// PropertyLinkSub |
| 1300 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 1301 |
|
| 1302 |
TYPESYSTEM_SOURCE(App::PropertyLinkSub, App::PropertyLinkBase) |
| 1303 |
TYPESYSTEM_SOURCE(App::PropertyLinkSubChild, App::PropertyLinkSub) |
| 1304 |
TYPESYSTEM_SOURCE(App::PropertyLinkSubGlobal, App::PropertyLinkSub) |
| 1305 |
TYPESYSTEM_SOURCE(App::PropertyLinkSubHidden, App::PropertyLinkSub) |
| 1306 |
|
| 1307 |
//************************************************************************** |
| 1308 |
// Construction/Destruction |
| 1309 |
|
| 1310 |
|
| 1311 |
PropertyLinkSub::PropertyLinkSub() = default; |
| 1312 |
|
| 1313 |
PropertyLinkSub::~PropertyLinkSub() |
| 1314 |
{ |
| 1315 |
// in case this property is dynamically removed |
| 1316 |
|
| 1317 |
if (_pcLinkSub && getContainer() |
| 1318 |
&& getContainer()->isDerivedFrom<App::DocumentObject>()) { |
| 1319 |
App::DocumentObject* parent = static_cast<DocumentObject*>(getContainer()); |
| 1320 |
// before accessing internals make sure the object is not about to be destroyed |
| 1321 |
// otherwise the backlink contains dangling pointers |
| 1322 |
if (!parent->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 1323 |
if (_pcLinkSub) { |
| 1324 |
_pcLinkSub->_removeBackLink(parent); |
| 1325 |
} |
| 1326 |
} |
| 1327 |
} |
| 1328 |
} |
| 1329 |
|
| 1330 |
void PropertyLinkSub::setSyncSubObject(bool enable) |
| 1331 |
{ |
| 1332 |
_Flags.set((std::size_t)LinkSyncSubObject, enable); |
| 1333 |
} |
| 1334 |
|
| 1335 |
void PropertyLinkSub::setValue(App::DocumentObject* lValue, |
| 1336 |
const std::vector<std::string>& SubList, |
| 1337 |
std::vector<ShadowSub>&& shadows) |
| 1338 |
{ |
| 1339 |
setValue(lValue, std::vector<std::string>(SubList), std::move(shadows)); |
| 1340 |
} |
| 1341 |
|
| 1342 |
void PropertyLinkSub::setValue(App::DocumentObject* lValue, |
| 1343 |
std::vector<std::string>&& subs, |
| 1344 |
std::vector<ShadowSub>&& shadows) |
| 1345 |
{ |
| 1346 |
auto parent = freecad_cast<App::DocumentObject*>(getContainer()); |
| 1347 |
if (lValue) { |
| 1348 |
if (!lValue->isAttachedToDocument()) { |
| 1349 |
throw Base::ValueError("PropertyLinkSub: invalid document object"); |
| 1350 |
} |
| 1351 |
if (!testFlag(LinkAllowExternal) && parent |
| 1352 |
&& parent->getDocument() != lValue->getDocument()) { |
| 1353 |
throw Base::ValueError("PropertyLinkSub does not support external object"); |
| 1354 |
} |
| 1355 |
} |
| 1356 |
aboutToSetValue(); |
| 1357 |
|
| 1358 |
if (parent) { |
| 1359 |
// before accessing internals make sure the object is not about to be destroyed |
| 1360 |
// otherwise the backlink contains dangling pointers |
| 1361 |
if (!parent->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 1362 |
if (_pcLinkSub) { |
| 1363 |
_pcLinkSub->_removeBackLink(parent); |
| 1364 |
} |
| 1365 |
if (lValue) { |
| 1366 |
lValue->_addBackLink(parent); |
| 1367 |
} |
| 1368 |
} |
| 1369 |
} |
| 1370 |
|
| 1371 |
_pcLinkSub = lValue; |
| 1372 |
_cSubList = std::move(subs); |
| 1373 |
if (shadows.size() == _cSubList.size()) { |
| 1374 |
_ShadowSubList = std::move(shadows); |
| 1375 |
onContainerRestored(); // re-register element references |
| 1376 |
} |
| 1377 |
else { |
| 1378 |
updateElementReference(nullptr); |
| 1379 |
} |
| 1380 |
checkLabelReferences(_cSubList); |
| 1381 |
hasSetValue(); |
| 1382 |
} |
| 1383 |
|
| 1384 |
App::DocumentObject* PropertyLinkSub::getValue() const |
| 1385 |
{ |
| 1386 |
return _pcLinkSub; |
| 1387 |
} |
| 1388 |
|
| 1389 |
const std::vector<std::string>& PropertyLinkSub::getSubValues() const |
| 1390 |
{ |
| 1391 |
return _cSubList; |
| 1392 |
} |
| 1393 |
|
| 1394 |
static inline const std::string& getSubNameWithStyle(const std::string& subName, |
| 1395 |
const PropertyLinkBase::ShadowSub& shadow, |
| 1396 |
bool newStyle, |
| 1397 |
std::string& tmp) |
| 1398 |
{ |
| 1399 |
if (!newStyle) { |
| 1400 |
if (!shadow.oldName.empty()) { |
| 1401 |
return shadow.oldName; |
| 1402 |
} |
| 1403 |
} |
| 1404 |
else if (!shadow.newName.empty()) { |
| 1405 |
if (Data::hasMissingElement(shadow.oldName.c_str())) { |
| 1406 |
auto pos = shadow.newName.rfind('.'); |
| 1407 |
if (pos != std::string::npos) { |
| 1408 |
tmp = shadow.newName.substr(0, pos + 1); |
| 1409 |
tmp += shadow.oldName; |
| 1410 |
return tmp; |
| 1411 |
} |
| 1412 |
} |
| 1413 |
return shadow.newName; |
| 1414 |
} |
| 1415 |
return subName; |
| 1416 |
} |
| 1417 |
|
| 1418 |
std::vector<std::string> PropertyLinkSub::getSubValues(bool newStyle) const |
| 1419 |
{ |
| 1420 |
assert(_cSubList.size() == _ShadowSubList.size()); |
| 1421 |
std::vector<std::string> ret; |
| 1422 |
ret.reserve(_cSubList.size()); |
| 1423 |
std::string tmp; |
| 1424 |
for (size_t i = 0; i < _ShadowSubList.size(); ++i) { |
| 1425 |
ret.push_back(getSubNameWithStyle(_cSubList[i], _ShadowSubList[i], newStyle, tmp)); |
| 1426 |
} |
| 1427 |
return ret; |
| 1428 |
} |
| 1429 |
|
| 1430 |
std::vector<std::string> PropertyLinkSub::getSubValuesStartsWith(const char* starter, |
| 1431 |
bool newStyle) const |
| 1432 |
{ |
| 1433 |
assert(_cSubList.size() == _ShadowSubList.size()); |
| 1434 |
std::vector<std::string> ret; |
| 1435 |
std::string tmp; |
| 1436 |
for (size_t i = 0; i < _ShadowSubList.size(); ++i) { |
| 1437 |
const auto& sub = getSubNameWithStyle(_cSubList[i], _ShadowSubList[i], newStyle, tmp); |
| 1438 |
auto element = Data::findElementName(sub.c_str()); |
| 1439 |
if (element && boost::starts_with(element, starter)) { |
| 1440 |
ret.emplace_back(element); |
| 1441 |
} |
| 1442 |
} |
| 1443 |
return ret; |
| 1444 |
} |
| 1445 |
|
| 1446 |
App::DocumentObject* PropertyLinkSub::getValue(Base::Type t) const |
| 1447 |
{ |
| 1448 |
return (_pcLinkSub && _pcLinkSub->isDerivedFrom(t)) ? _pcLinkSub : nullptr; |
| 1449 |
} |
| 1450 |
|
| 1451 |
PyObject* PropertyLinkSub::getPyObject() |
| 1452 |
{ |
| 1453 |
Py::Tuple tup(2); |
| 1454 |
Py::List list(static_cast<int>(_cSubList.size())); |
| 1455 |
if (_pcLinkSub) { |
| 1456 |
tup[0] = Py::asObject(_pcLinkSub->getPyObject()); |
| 1457 |
int i = 0; |
| 1458 |
for (auto& sub : getSubValues(testFlag(LinkNewElement))) { |
| 1459 |
list[i++] = Py::String(sub); |
| 1460 |
} |
| 1461 |
tup[1] = list; |
| 1462 |
return Py::new_reference_to(tup); |
| 1463 |
} |
| 1464 |
else { |
| 1465 |
return Py::new_reference_to(Py::None()); |
| 1466 |
} |
| 1467 |
} |
| 1468 |
|
| 1469 |
void PropertyLinkSub::setPyObject(PyObject* value) |
| 1470 |
{ |
| 1471 |
if (PyObject_TypeCheck(value, &(DocumentObjectPy::Type))) { |
| 1472 |
DocumentObjectPy* pcObject = static_cast<DocumentObjectPy*>(value); |
| 1473 |
setValue(pcObject->getDocumentObjectPtr()); |
| 1474 |
} |
| 1475 |
else if (PyTuple_Check(value) || PyList_Check(value)) { |
| 1476 |
Py::Sequence seq(value); |
| 1477 |
if (seq.size() == 0) { |
| 1478 |
setValue(nullptr); |
| 1479 |
} |
| 1480 |
else if (seq.size() != 2) { |
| 1481 |
throw Base::ValueError("Expect input sequence of size 2"); |
| 1482 |
} |
| 1483 |
else if (PyObject_TypeCheck(seq[0].ptr(), &(DocumentObjectPy::Type))) { |
| 1484 |
DocumentObjectPy* pcObj = static_cast<DocumentObjectPy*>(seq[0].ptr()); |
| 1485 |
static const char* errMsg = |
| 1486 |
"type of second element in tuple must be str or sequence of str"; |
| 1487 |
PropertyString propString; |
| 1488 |
if (seq[1].isString()) { |
| 1489 |
std::vector<std::string> vals; |
| 1490 |
propString.setPyObject(seq[1].ptr()); |
| 1491 |
vals.emplace_back(propString.getValue()); |
| 1492 |
setValue(pcObj->getDocumentObjectPtr(), std::move(vals)); |
| 1493 |
} |
| 1494 |
else if (seq[1].isSequence()) { |
| 1495 |
Py::Sequence list(seq[1]); |
| 1496 |
std::vector<std::string> vals(list.size()); |
| 1497 |
unsigned int i = 0; |
| 1498 |
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it, ++i) { |
| 1499 |
if (!(*it).isString()) { |
| 1500 |
throw Base::TypeError(errMsg); |
| 1501 |
} |
| 1502 |
propString.setPyObject((*it).ptr()); |
| 1503 |
vals[i] = propString.getValue(); |
| 1504 |
} |
| 1505 |
setValue(pcObj->getDocumentObjectPtr(), std::move(vals)); |
| 1506 |
} |
| 1507 |
else { |
| 1508 |
throw Base::TypeError(errMsg); |
| 1509 |
} |
| 1510 |
} |
| 1511 |
else { |
| 1512 |
std::string error = |
| 1513 |
std::string("type of first element in tuple must be 'DocumentObject', not "); |
| 1514 |
error += seq[0].ptr()->ob_type->tp_name; |
| 1515 |
throw Base::TypeError(error); |
| 1516 |
} |
| 1517 |
} |
| 1518 |
else if (Py_None == value) { |
| 1519 |
setValue(nullptr); |
| 1520 |
} |
| 1521 |
else { |
| 1522 |
std::string error = std::string( |
| 1523 |
"type must be 'DocumentObject', 'NoneType' or ('DocumentObject',['String',]) not "); |
| 1524 |
error += value->ob_type->tp_name; |
| 1525 |
throw Base::TypeError(error); |
| 1526 |
} |
| 1527 |
} |
| 1528 |
|
| 1529 |
static bool updateLinkReference(App::PropertyLinkBase* prop, |
| 1530 |
App::DocumentObject* feature, |
| 1531 |
bool reverse, |
| 1532 |
bool notify, |
| 1533 |
App::DocumentObject* link, |
| 1534 |
std::vector<std::string>& subs, |
| 1535 |
std::vector<int>& mapped, |
| 1536 |
std::vector<PropertyLinkBase::ShadowSub>& shadows) |
| 1537 |
{ |
| 1538 |
if (!feature) { |
| 1539 |
shadows.clear(); |
| 1540 |
prop->unregisterElementReference(); |
| 1541 |
} |
| 1542 |
shadows.resize(subs.size()); |
| 1543 |
if (!link || !link->isAttachedToDocument()) { |
| 1544 |
return false; |
| 1545 |
} |
| 1546 |
auto owner = freecad_cast<DocumentObject*>(prop->getContainer()); |
| 1547 |
if (owner && owner->isRestoring()) { |
| 1548 |
return false; |
| 1549 |
} |
| 1550 |
int i = 0; |
| 1551 |
bool touched = false; |
| 1552 |
for (auto& sub : subs) { |
| 1553 |
if (prop->_updateElementReference(feature, |
| 1554 |
link, |
| 1555 |
sub, |
| 1556 |
shadows[i++], |
| 1557 |
reverse, |
| 1558 |
notify && !touched)) { |
| 1559 |
touched = true; |
| 1560 |
} |
| 1561 |
} |
| 1562 |
if (!touched) { |
| 1563 |
return false; |
| 1564 |
} |
| 1565 |
for (int idx : mapped) { |
| 1566 |
if (idx < (int)subs.size() && !shadows[idx].newName.empty()) { |
| 1567 |
subs[idx] = shadows[idx].newName; |
| 1568 |
} |
| 1569 |
} |
| 1570 |
mapped.clear(); |
| 1571 |
if (owner && feature) { |
| 1572 |
owner->onUpdateElementReference(prop); |
| 1573 |
} |
| 1574 |
return true; |
| 1575 |
} |
| 1576 |
|
| 1577 |
void PropertyLinkSub::afterRestore() |
| 1578 |
{ |
| 1579 |
_ShadowSubList.resize(_cSubList.size()); |
| 1580 |
if (!testFlag(LinkRestoreLabel) || !_pcLinkSub || !_pcLinkSub->isAttachedToDocument()) { |
| 1581 |
return; |
| 1582 |
} |
| 1583 |
setFlag(LinkRestoreLabel, false); |
| 1584 |
for (std::size_t i = 0; i < _cSubList.size(); ++i) { |
| 1585 |
restoreLabelReference(_pcLinkSub, _cSubList[i], &_ShadowSubList[i]); |
| 1586 |
} |
| 1587 |
} |
| 1588 |
|
| 1589 |
void PropertyLinkSub::onContainerRestored() |
| 1590 |
{ |
| 1591 |
unregisterElementReference(); |
| 1592 |
if (!_pcLinkSub || !_pcLinkSub->isAttachedToDocument()) { |
| 1593 |
return; |
| 1594 |
} |
| 1595 |
for (std::size_t i = 0; i < _cSubList.size(); ++i) { |
| 1596 |
_registerElementReference(_pcLinkSub, _cSubList[i], _ShadowSubList[i]); |
| 1597 |
} |
| 1598 |
} |
| 1599 |
|
| 1600 |
void PropertyLinkSub::updateElementReference(DocumentObject* feature, bool reverse, bool notify) |
| 1601 |
{ |
| 1602 |
if (!updateLinkReference(this, |
| 1603 |
feature, |
| 1604 |
reverse, |
| 1605 |
notify, |
| 1606 |
_pcLinkSub, |
| 1607 |
_cSubList, |
| 1608 |
_mapped, |
| 1609 |
_ShadowSubList)) { |
| 1610 |
return; |
| 1611 |
} |
| 1612 |
if (notify) { |
| 1613 |
hasSetValue(); |
| 1614 |
} |
| 1615 |
} |
| 1616 |
|
| 1617 |
bool PropertyLinkSub::referenceChanged() const |
| 1618 |
{ |
| 1619 |
return !_mapped.empty(); |
| 1620 |
} |
| 1621 |
|
| 1622 |
std::string |
| 1623 |
PropertyLinkBase::importSubName(Base::XMLReader& reader, const char* sub, bool& restoreLabel) |
| 1624 |
{ |
| 1625 |
if (!reader.doNameMapping()) { |
| 1626 |
return sub; |
| 1627 |
} |
| 1628 |
std::ostringstream str; |
| 1629 |
for (const char* dot = strchr(sub, '.'); dot; sub = dot + 1, dot = strchr(sub, '.')) { |
| 1630 |
size_t count = dot - sub; |
| 1631 |
const char* tail = "."; |
| 1632 |
if (count && dot[-1] == '@') { |
| 1633 |
// tail=='@' means we are exporting a label reference. So retain |
| 1634 |
// this marker so that the label can be restored in afterRestore(). |
| 1635 |
tail = "@."; |
| 1636 |
--count; |
| 1637 |
restoreLabel = true; |
| 1638 |
} |
| 1639 |
str << reader.getName(std::string(sub, count).c_str()) << tail; |
| 1640 |
} |
| 1641 |
str << sub; |
| 1642 |
return str.str(); |
| 1643 |
} |
| 1644 |
|
| 1645 |
const char* PropertyLinkBase::exportSubName(std::string& output, |
| 1646 |
const App::DocumentObject* obj, |
| 1647 |
const char* sub, |
| 1648 |
bool first_obj) |
| 1649 |
{ |
| 1650 |
std::ostringstream str; |
| 1651 |
const char* res = sub; |
| 1652 |
|
| 1653 |
if (!sub || !sub[0]) { |
| 1654 |
return res; |
| 1655 |
} |
| 1656 |
|
| 1657 |
bool touched = false; |
| 1658 |
if (first_obj) { |
| 1659 |
auto dot = strchr(sub, '.'); |
| 1660 |
if (!dot) { |
| 1661 |
return res; |
| 1662 |
} |
| 1663 |
const char* hash; |
| 1664 |
for (hash = sub; hash < dot && *hash != '#'; ++hash) {} |
| 1665 |
App::Document* doc = nullptr; |
| 1666 |
if (*hash == '#') { |
| 1667 |
doc = GetApplication().getDocument(std::string(sub, hash - sub).c_str()); |
| 1668 |
} |
| 1669 |
else { |
| 1670 |
hash = nullptr; |
| 1671 |
if (obj && obj->isAttachedToDocument()) { |
| 1672 |
doc = obj->getDocument(); |
| 1673 |
} |
| 1674 |
} |
| 1675 |
if (!doc) { |
| 1676 |
FC_ERR("Failed to get document for the first object in " << sub); |
| 1677 |
return res; |
| 1678 |
} |
| 1679 |
obj = doc->getObject(std::string(sub, dot - sub).c_str()); |
| 1680 |
if (!obj || !obj->isAttachedToDocument()) { |
| 1681 |
return res; |
| 1682 |
} |
| 1683 |
if (hash) { |
| 1684 |
if (!obj->isExporting()) { |
| 1685 |
str << doc->getName() << '#'; |
| 1686 |
} |
| 1687 |
sub = hash + 1; |
| 1688 |
} |
| 1689 |
} |
| 1690 |
else if (!obj || !obj->isAttachedToDocument()) { |
| 1691 |
return res; |
| 1692 |
} |
| 1693 |
|
| 1694 |
for (const char* dot = strchr(sub, '.'); dot; sub = dot + 1, dot = strchr(sub, '.')) { |
| 1695 |
// name with trailing '.' |
| 1696 |
auto name = std::string(sub, dot - sub + 1); |
| 1697 |
if (first_obj) { |
| 1698 |
first_obj = false; |
| 1699 |
} |
| 1700 |
else { |
| 1701 |
obj = obj->getSubObject(name.c_str()); |
| 1702 |
} |
| 1703 |
if (!obj || !obj->isAttachedToDocument()) { |
| 1704 |
FC_WARN("missing sub object '" << name << "' in '" << sub << "'"); |
| 1705 |
break; |
| 1706 |
} |
| 1707 |
if (obj->isExporting()) { |
| 1708 |
if (name[0] == '$') { |
| 1709 |
if (name.compare(1, name.size() - 2, obj->Label.getValue()) != 0) { |
| 1710 |
str << obj->getExportName(true) << "@."; |
| 1711 |
touched = true; |
| 1712 |
continue; |
| 1713 |
} |
| 1714 |
} |
| 1715 |
else if (name.compare(0, name.size() - 1, obj->getNameInDocument()) == 0) { |
| 1716 |
str << obj->getExportName(true) << '.'; |
| 1717 |
touched = true; |
| 1718 |
continue; |
| 1719 |
} |
| 1720 |
} |
| 1721 |
str << name; |
| 1722 |
} |
| 1723 |
if (!touched) { |
| 1724 |
return res; |
| 1725 |
} |
| 1726 |
str << sub; |
| 1727 |
output = str.str(); |
| 1728 |
return output.c_str(); |
| 1729 |
} |
| 1730 |
|
| 1731 |
App::DocumentObject* PropertyLinkBase::tryImport(const App::Document* doc, |
| 1732 |
const App::DocumentObject* obj, |
| 1733 |
const std::map<std::string, std::string>& nameMap) |
| 1734 |
{ |
| 1735 |
if (doc && obj && obj->isAttachedToDocument()) { |
| 1736 |
auto it = nameMap.find(obj->getExportName(true)); |
| 1737 |
if (it != nameMap.end()) { |
| 1738 |
obj = doc->getObject(it->second.c_str()); |
| 1739 |
if (!obj) { |
| 1740 |
FC_THROWM(Base::RuntimeError, "Cannot find import object " << it->second); |
| 1741 |
} |
| 1742 |
} |
| 1743 |
} |
| 1744 |
return const_cast<DocumentObject*>(obj); |
| 1745 |
} |
| 1746 |
|
| 1747 |
std::string PropertyLinkBase::tryImportSubName(const App::DocumentObject* obj, |
| 1748 |
const char* _subname, |
| 1749 |
const App::Document* doc, |
| 1750 |
const std::map<std::string, std::string>& nameMap) |
| 1751 |
{ |
| 1752 |
if (!doc || !obj || !obj->isAttachedToDocument()) { |
| 1753 |
return {}; |
| 1754 |
} |
| 1755 |
|
| 1756 |
std::ostringstream ss; |
| 1757 |
std::string subname(_subname); |
| 1758 |
char* sub = &subname[0]; |
| 1759 |
char* next = sub; |
| 1760 |
for (char* dot = strchr(next, '.'); dot; next = dot + 1, dot = strchr(next, '.')) { |
| 1761 |
StringGuard guard(dot); |
| 1762 |
auto sobj = obj->getSubObject(subname.c_str()); |
| 1763 |
if (!sobj) { |
| 1764 |
FC_ERR("Failed to restore label reference " << obj->getFullName() << '.' << subname); |
| 1765 |
return {}; |
| 1766 |
} |
| 1767 |
dot[0] = 0; |
| 1768 |
if (next[0] == '$') { |
| 1769 |
if (strcmp(next + 1, sobj->Label.getValue()) != 0) { |
| 1770 |
continue; |
| 1771 |
} |
| 1772 |
} |
| 1773 |
else if (strcmp(next, sobj->getNameInDocument()) != 0) { |
| 1774 |
continue; |
| 1775 |
} |
| 1776 |
auto it = nameMap.find(sobj->getExportName(true)); |
| 1777 |
if (it == nameMap.end()) { |
| 1778 |
continue; |
| 1779 |
} |
| 1780 |
auto imported = doc->getObject(it->second.c_str()); |
| 1781 |
if (!imported) { |
| 1782 |
FC_THROWM(RuntimeError, "Failed to find imported object " << it->second); |
| 1783 |
} |
| 1784 |
ss.write(sub, next - sub); |
| 1785 |
if (next[0] == '$') { |
| 1786 |
ss << '$' << imported->Label.getStrValue() << '.'; |
| 1787 |
} |
| 1788 |
else { |
| 1789 |
ss << it->second << '.'; |
| 1790 |
} |
| 1791 |
sub = dot + 1; |
| 1792 |
} |
| 1793 |
if (sub != subname.c_str()) { |
| 1794 |
return ss.str(); |
| 1795 |
} |
| 1796 |
return {}; |
| 1797 |
} |
| 1798 |
|
| 1799 |
void PropertyLinkBase::_getLinksTo(std::vector<App::ObjectIdentifier>& identifiers, |
| 1800 |
App::DocumentObject* obj, |
| 1801 |
const char* subname, |
| 1802 |
const std::vector<std::string>& subs, |
| 1803 |
const std::vector<PropertyLinkBase::ShadowSub>& shadows) const |
| 1804 |
{ |
| 1805 |
if (!subname) { |
| 1806 |
identifiers.emplace_back(*this); |
| 1807 |
return; |
| 1808 |
} |
| 1809 |
App::SubObjectT objT(obj, subname); |
| 1810 |
auto subObject = objT.getSubObject(); |
| 1811 |
auto subElement = objT.getOldElementName(); |
| 1812 |
|
| 1813 |
int i = -1; |
| 1814 |
for (const auto& sub : subs) { |
| 1815 |
++i; |
| 1816 |
if (sub == subname) { |
| 1817 |
identifiers.emplace_back(*this); |
| 1818 |
return; |
| 1819 |
} |
| 1820 |
if (!subObject) { |
| 1821 |
continue; |
| 1822 |
} |
| 1823 |
// After above, there is a subobject and the subname doesn't match our current entry |
| 1824 |
App::SubObjectT sobjT(obj, sub.c_str()); |
| 1825 |
if (sobjT.getSubObject() == subObject && sobjT.getOldElementName() == subElement) { |
| 1826 |
identifiers.emplace_back(*this); |
| 1827 |
return; |
| 1828 |
} |
| 1829 |
// And the oldElementName ( short, I.E. "Edge5" ) doesn't match. |
| 1830 |
if (i < (int)shadows.size()) { |
| 1831 |
const auto& [shadowNewName, shadowOldName] = shadows[i]; |
| 1832 |
if (shadowNewName == subname || shadowOldName == subname) { |
| 1833 |
identifiers.emplace_back(*this); |
| 1834 |
return; |
| 1835 |
} |
| 1836 |
if (!subObject) { |
| 1837 |
continue; |
| 1838 |
} |
| 1839 |
App::SubObjectT shadowobjT(obj, |
| 1840 |
shadowNewName.empty() ? shadowOldName.c_str() |
| 1841 |
: shadowNewName.c_str()); |
| 1842 |
if (shadowobjT.getSubObject() == subObject |
| 1843 |
&& shadowobjT.getOldElementName() == subElement) { |
| 1844 |
identifiers.emplace_back(*this); |
| 1845 |
return; |
| 1846 |
} |
| 1847 |
} |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
#define ATTR_SHADOWED "shadowed" |
| 1852 |
#define ATTR_SHADOW "shadow" |
| 1853 |
#define ATTR_MAPPED "mapped" |
| 1854 |
|
| 1855 |
#define IGNORE_SHADOW false |
| 1856 |
|
| 1857 |
void PropertyLinkSub::Save(Base::Writer& writer) const |
| 1858 |
{ |
| 1859 |
assert(_cSubList.size() == _ShadowSubList.size()); |
| 1860 |
|
| 1861 |
std::string internal_name; |
| 1862 |
// it can happen that the object is still alive but is not part of the document anymore and thus |
| 1863 |
// returns 0 |
| 1864 |
if (_pcLinkSub && _pcLinkSub->isAttachedToDocument()) { |
| 1865 |
internal_name = _pcLinkSub->getExportName(); |
| 1866 |
} |
| 1867 |
writer.Stream() << writer.ind() << "<LinkSub value=\"" << internal_name << "\" count=\"" |
| 1868 |
<< _cSubList.size(); |
| 1869 |
writer.Stream() << "\">" << std::endl; |
| 1870 |
writer.incInd(); |
| 1871 |
auto owner = freecad_cast<DocumentObject*>(getContainer()); |
| 1872 |
bool exporting = owner && owner->isExporting(); |
| 1873 |
for (unsigned int i = 0; i < _cSubList.size(); i++) { |
| 1874 |
const auto& shadow = _ShadowSubList[i]; |
| 1875 |
// shadow.oldName stores the old style element name. For backward |
| 1876 |
// compatibility reason, we shall store the old name into attribute |
| 1877 |
// 'value' whenever possible. |
| 1878 |
const auto& sub = shadow.oldName.empty() ? _cSubList[i] : shadow.oldName; |
| 1879 |
writer.Stream() << writer.ind() << "<Sub value=\""; |
| 1880 |
if (exporting) { |
| 1881 |
std::string exportName; |
| 1882 |
writer.Stream() << encodeAttribute(exportSubName(exportName, _pcLinkSub, sub.c_str())); |
| 1883 |
if (!shadow.oldName.empty() && shadow.newName == _cSubList[i]) { |
| 1884 |
writer.Stream() << "\" " ATTR_MAPPED "=\"1"; |
| 1885 |
} |
| 1886 |
} |
| 1887 |
else { |
| 1888 |
writer.Stream() << encodeAttribute(sub); |
| 1889 |
if (!_cSubList[i].empty()) { |
| 1890 |
if (sub != _cSubList[i]) { |
| 1891 |
// Stores the actual value that is shadowed. For new version FC, |
| 1892 |
// we will restore this shadowed value instead. |
| 1893 |
writer.Stream() << "\" " ATTR_SHADOWED "=\"" << encodeAttribute(_cSubList[i]); |
| 1894 |
} |
| 1895 |
else if (!shadow.newName.empty()) { |
| 1896 |
// Here means the user set value is old style element name. |
| 1897 |
// We shall then store the shadow somewhere else. |
| 1898 |
writer.Stream() << "\" " ATTR_SHADOW "=\"" << encodeAttribute(shadow.newName); |
| 1899 |
} |
| 1900 |
} |
| 1901 |
} |
| 1902 |
writer.Stream() << "\"/>" << endl; |
| 1903 |
} |
| 1904 |
writer.decInd(); |
| 1905 |
writer.Stream() << writer.ind() << "</LinkSub>" << endl; |
| 1906 |
} |
| 1907 |
|
| 1908 |
void PropertyLinkSub::Restore(Base::XMLReader& reader) |
| 1909 |
{ |
| 1910 |
// read my element |
| 1911 |
reader.readElement("LinkSub"); |
| 1912 |
// get the values of my attributes |
| 1913 |
std::string name = reader.getName(reader.getAttribute<const char*>("value")); |
| 1914 |
int count = reader.getAttribute<long>("count"); |
| 1915 |
|
| 1916 |
// Property not in a DocumentObject! |
| 1917 |
assert(getContainer()->isDerivedFrom<App::DocumentObject>()); |
| 1918 |
App::Document* document = static_cast<DocumentObject*>(getContainer())->getDocument(); |
| 1919 |
|
| 1920 |
DocumentObject* pcObject = nullptr; |
| 1921 |
if (!name.empty()) { |
| 1922 |
pcObject = document ? document->getObject(name.c_str()) : nullptr; |
| 1923 |
if (!pcObject) { |
| 1924 |
if (reader.isVerbose()) { |
| 1925 |
FC_WARN("Lost link to " |
| 1926 |
<< name << " while loading, maybe an object was not loaded correctly"); |
| 1927 |
} |
| 1928 |
} |
| 1929 |
} |
| 1930 |
|
| 1931 |
std::vector<int> mapped; |
| 1932 |
std::vector<std::string> values(count); |
| 1933 |
std::vector<ShadowSub> shadows(count); |
| 1934 |
bool restoreLabel = false; |
| 1935 |
// Sub may store '.' separated object names, so be aware of the possible mapping when import |
| 1936 |
for (int i = 0; i < count; i++) { |
| 1937 |
reader.readElement("Sub"); |
| 1938 |
shadows[i].oldName = importSubName(reader, reader.getAttribute<const char*>("value"), restoreLabel); |
| 1939 |
if (reader.hasAttribute(ATTR_SHADOWED) && !IGNORE_SHADOW) { |
| 1940 |
values[i] = shadows[i].newName = |
| 1941 |
importSubName(reader, reader.getAttribute<const char*>(ATTR_SHADOWED), restoreLabel); |
| 1942 |
} |
| 1943 |
else { |
| 1944 |
values[i] = shadows[i].oldName; |
| 1945 |
if (reader.hasAttribute(ATTR_SHADOW) && !IGNORE_SHADOW) { |
| 1946 |
shadows[i].newName = |
| 1947 |
importSubName(reader, reader.getAttribute<const char*>(ATTR_SHADOW), restoreLabel); |
| 1948 |
} |
| 1949 |
} |
| 1950 |
if (reader.hasAttribute(ATTR_MAPPED)) { |
| 1951 |
mapped.push_back(i); |
| 1952 |
} |
| 1953 |
} |
| 1954 |
setFlag(LinkRestoreLabel, restoreLabel); |
| 1955 |
|
| 1956 |
reader.readEndElement("LinkSub"); |
| 1957 |
|
| 1958 |
if (pcObject) { |
| 1959 |
setValue(pcObject, std::move(values), std::move(shadows)); |
| 1960 |
_mapped = std::move(mapped); |
| 1961 |
} |
| 1962 |
else { |
| 1963 |
setValue(nullptr); |
| 1964 |
} |
| 1965 |
} |
| 1966 |
|
| 1967 |
template<class Func, class... Args> |
| 1968 |
std::vector<std::string> updateLinkSubs(const App::DocumentObject* obj, |
| 1969 |
const std::vector<std::string>& subs, |
| 1970 |
Func* f, |
| 1971 |
Args&&... args) |
| 1972 |
{ |
| 1973 |
if (!obj || !obj->isAttachedToDocument()) { |
| 1974 |
return {}; |
| 1975 |
} |
| 1976 |
|
| 1977 |
std::vector<std::string> res; |
| 1978 |
for (auto it = subs.begin(); it != subs.end(); ++it) { |
| 1979 |
const auto& sub = *it; |
| 1980 |
auto new_sub = (*f)(obj, sub.c_str(), args...); |
| 1981 |
if (new_sub.size()) { |
| 1982 |
if (res.empty()) { |
| 1983 |
res.reserve(subs.size()); |
| 1984 |
res.insert(res.end(), subs.begin(), it); |
| 1985 |
} |
| 1986 |
res.push_back(std::move(new_sub)); |
| 1987 |
} |
| 1988 |
else if (!res.empty()) { |
| 1989 |
res.push_back(sub); |
| 1990 |
} |
| 1991 |
} |
| 1992 |
return res; |
| 1993 |
} |
| 1994 |
|
| 1995 |
Property* |
| 1996 |
PropertyLinkSub::CopyOnImportExternal(const std::map<std::string, std::string>& nameMap) const |
| 1997 |
{ |
| 1998 |
auto owner = dynamic_cast<const DocumentObject*>(getContainer()); |
| 1999 |
if (!owner || !owner->getDocument()) { |
| 2000 |
return nullptr; |
| 2001 |
} |
| 2002 |
if (!_pcLinkSub || !_pcLinkSub->isAttachedToDocument()) { |
| 2003 |
return nullptr; |
| 2004 |
} |
| 2005 |
|
| 2006 |
auto subs = |
| 2007 |
updateLinkSubs(_pcLinkSub, _cSubList, &tryImportSubName, owner->getDocument(), nameMap); |
| 2008 |
auto linked = tryImport(owner->getDocument(), _pcLinkSub, nameMap); |
| 2009 |
if (subs.empty() && linked == _pcLinkSub) { |
| 2010 |
return nullptr; |
| 2011 |
} |
| 2012 |
|
| 2013 |
PropertyLinkSub* p = new PropertyLinkSub(); |
| 2014 |
p->_pcLinkSub = linked; |
| 2015 |
if (subs.empty()) { |
| 2016 |
p->_cSubList = _cSubList; |
| 2017 |
} |
| 2018 |
else { |
| 2019 |
p->_cSubList = std::move(subs); |
| 2020 |
} |
| 2021 |
return p; |
| 2022 |
} |
| 2023 |
|
| 2024 |
Property* PropertyLinkSub::CopyOnLabelChange(App::DocumentObject* obj, |
| 2025 |
const std::string& ref, |
| 2026 |
const char* newLabel) const |
| 2027 |
{ |
| 2028 |
auto owner = dynamic_cast<const DocumentObject*>(getContainer()); |
| 2029 |
if (!owner || !owner->getDocument()) { |
| 2030 |
return nullptr; |
| 2031 |
} |
| 2032 |
if (!_pcLinkSub || !_pcLinkSub->isAttachedToDocument()) { |
| 2033 |
return nullptr; |
| 2034 |
} |
| 2035 |
|
| 2036 |
auto subs = updateLinkSubs(_pcLinkSub, _cSubList, &updateLabelReference, obj, ref, newLabel); |
| 2037 |
if (subs.empty()) { |
| 2038 |
return nullptr; |
| 2039 |
} |
| 2040 |
|
| 2041 |
PropertyLinkSub* p = new PropertyLinkSub(); |
| 2042 |
p->_pcLinkSub = _pcLinkSub; |
| 2043 |
p->_cSubList = std::move(subs); |
| 2044 |
return p; |
| 2045 |
} |
| 2046 |
|
| 2047 |
Property* PropertyLinkSub::CopyOnLinkReplace(const App::DocumentObject* parent, |
| 2048 |
App::DocumentObject* oldObj, |
| 2049 |
App::DocumentObject* newObj) const |
| 2050 |
{ |
| 2051 |
auto res = tryReplaceLinkSubs(getContainer(), _pcLinkSub, parent, oldObj, newObj, _cSubList); |
| 2052 |
if (res.first) { |
| 2053 |
PropertyLinkSub* p = new PropertyLinkSub(); |
| 2054 |
p->_pcLinkSub = res.first; |
| 2055 |
p->_cSubList = std::move(res.second); |
| 2056 |
return p; |
| 2057 |
} |
| 2058 |
return nullptr; |
| 2059 |
} |
| 2060 |
|
| 2061 |
Property* PropertyLinkSub::Copy() const |
| 2062 |
{ |
| 2063 |
PropertyLinkSub* p = new PropertyLinkSub(); |
| 2064 |
p->_pcLinkSub = _pcLinkSub; |
| 2065 |
p->_cSubList = _cSubList; |
| 2066 |
p->_ShadowSubList = _ShadowSubList; |
| 2067 |
return p; |
| 2068 |
} |
| 2069 |
|
| 2070 |
void PropertyLinkSub::Paste(const Property& from) |
| 2071 |
{ |
| 2072 |
if (!from.isDerivedFrom<PropertyLinkSub>()) { |
| 2073 |
throw Base::TypeError("Incompatible property to paste to"); |
| 2074 |
} |
| 2075 |
auto& link = static_cast<const PropertyLinkSub&>(from); |
| 2076 |
setValue(link._pcLinkSub, link._cSubList, std::vector<ShadowSub>(link._ShadowSubList)); |
| 2077 |
} |
| 2078 |
|
| 2079 |
void PropertyLinkSub::getLinks(std::vector<App::DocumentObject*>& objs, |
| 2080 |
bool all, |
| 2081 |
std::vector<std::string>* subs, |
| 2082 |
bool newStyle) const |
| 2083 |
{ |
| 2084 |
if (all || _pcScope != LinkScope::Hidden) { |
| 2085 |
if (_pcLinkSub && _pcLinkSub->isAttachedToDocument()) { |
| 2086 |
// we use to run this method everytime the program needed to access the sub-elements in |
| 2087 |
// a property link, but it caused multiple issues (#23441 and #23402) so it has been |
| 2088 |
// commented out. |
| 2089 |
// updateElementReferences(_pcLinkSub); |
| 2090 |
objs.push_back(_pcLinkSub); |
| 2091 |
if (subs) { |
| 2092 |
*subs = getSubValues(newStyle); |
| 2093 |
} |
| 2094 |
} |
| 2095 |
} |
| 2096 |
} |
| 2097 |
|
| 2098 |
void PropertyLinkSub::getLinksTo(std::vector<App::ObjectIdentifier>& identifiers, |
| 2099 |
App::DocumentObject* obj, |
| 2100 |
const char* subname, |
| 2101 |
bool all) const |
| 2102 |
{ |
| 2103 |
if (all || _pcScope != LinkScope::Hidden) { |
| 2104 |
if (obj && obj == _pcLinkSub) { |
| 2105 |
_getLinksTo(identifiers, obj, subname, _cSubList, _ShadowSubList); |
| 2106 |
} |
| 2107 |
} |
| 2108 |
} |
| 2109 |
|
| 2110 |
void PropertyLinkSub::breakLink(App::DocumentObject* obj, bool clear) |
| 2111 |
{ |
| 2112 |
if (obj == _pcLinkSub || (clear && getContainer() == obj)) { |
| 2113 |
setValue(nullptr); |
| 2114 |
} |
| 2115 |
} |
| 2116 |
|
| 2117 |
static App::DocumentObject* |
| 2118 |
adjustLinkSubs(App::PropertyLinkBase* prop, |
| 2119 |
const std::set<App::DocumentObject*>& inList, |
| 2120 |
App::DocumentObject* link, |
| 2121 |
std::vector<std::string>& subs, |
| 2122 |
std::map<App::DocumentObject*, std::vector<std::string>>* links = nullptr) |
| 2123 |
{ |
| 2124 |
App::DocumentObject* newLink = nullptr; |
| 2125 |
for (auto& sub : subs) { |
| 2126 |
size_t pos = sub.find('.'); |
| 2127 |
for (; pos != std::string::npos; pos = sub.find('.', pos + 1)) { |
| 2128 |
auto sobj = link->getSubObject(sub.substr(0, pos + 1).c_str()); |
| 2129 |
if (!sobj |
| 2130 |
|| (!prop->testFlag(PropertyLinkBase::LinkAllowExternal) |
| 2131 |
&& sobj->getDocument() != link->getDocument())) { |
| 2132 |
pos = std::string::npos; |
| 2133 |
break; |
| 2134 |
} |
| 2135 |
if (!newLink) { |
| 2136 |
if (inList.contains(sobj)) { |
| 2137 |
continue; |
| 2138 |
} |
| 2139 |
newLink = sobj; |
| 2140 |
if (links) { |
| 2141 |
(*links)[sobj].push_back(sub.substr(pos + 1)); |
| 2142 |
} |
| 2143 |
else { |
| 2144 |
sub = sub.substr(pos + 1); |
| 2145 |
} |
| 2146 |
} |
| 2147 |
else if (links) { |
| 2148 |
(*links)[sobj].push_back(sub.substr(pos + 1)); |
| 2149 |
} |
| 2150 |
else if (sobj == newLink) { |
| 2151 |
sub = sub.substr(pos + 1); |
| 2152 |
} |
| 2153 |
break; |
| 2154 |
} |
| 2155 |
if (pos == std::string::npos) { |
| 2156 |
return nullptr; |
| 2157 |
} |
| 2158 |
} |
| 2159 |
return newLink; |
| 2160 |
} |
| 2161 |
|
| 2162 |
bool PropertyLinkSub::adjustLink(const std::set<App::DocumentObject*>& inList) |
| 2163 |
{ |
| 2164 |
if (_pcScope == LinkScope::Hidden) { |
| 2165 |
return false; |
| 2166 |
} |
| 2167 |
if (!_pcLinkSub || !_pcLinkSub->isAttachedToDocument() || !inList.contains(_pcLinkSub)) { |
| 2168 |
return false; |
| 2169 |
} |
| 2170 |
auto subs = _cSubList; |
| 2171 |
auto link = adjustLinkSubs(this, inList, _pcLinkSub, subs); |
| 2172 |
if (link) { |
| 2173 |
setValue(link, std::move(subs)); |
| 2174 |
return true; |
| 2175 |
} |
| 2176 |
return false; |
| 2177 |
} |
| 2178 |
|
| 2179 |
//************************************************************************** |
| 2180 |
// PropertyLinkSubList |
| 2181 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 2182 |
|
| 2183 |
TYPESYSTEM_SOURCE(App::PropertyLinkSubList, App::PropertyLinkBase) |
| 2184 |
TYPESYSTEM_SOURCE(App::PropertyLinkSubListChild, App::PropertyLinkSubList) |
| 2185 |
TYPESYSTEM_SOURCE(App::PropertyLinkSubListGlobal, App::PropertyLinkSubList) |
| 2186 |
TYPESYSTEM_SOURCE(App::PropertyLinkSubListHidden, App::PropertyLinkSubList) |
| 2187 |
|
| 2188 |
//************************************************************************** |
| 2189 |
// Construction/Destruction |
| 2190 |
|
| 2191 |
|
| 2192 |
PropertyLinkSubList::PropertyLinkSubList() = default; |
| 2193 |
|
| 2194 |
PropertyLinkSubList::~PropertyLinkSubList() |
| 2195 |
{ |
| 2196 |
// in case this property is dynamically removed |
| 2197 |
|
| 2198 |
// maintain backlinks |
| 2199 |
if (!_lValueList.empty() && getContainer() |
| 2200 |
&& getContainer()->isDerivedFrom<App::DocumentObject>()) { |
| 2201 |
App::DocumentObject* parent = static_cast<DocumentObject*>(getContainer()); |
| 2202 |
// before accessing internals make sure the object is not about to be destroyed |
| 2203 |
// otherwise the backlink contains dangling pointers |
| 2204 |
if (!parent->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 2205 |
for (auto* obj : _lValueList) { |
| 2206 |
if (obj) { |
| 2207 |
obj->_removeBackLink(parent); |
| 2208 |
} |
| 2209 |
} |
| 2210 |
} |
| 2211 |
} |
| 2212 |
} |
| 2213 |
|
| 2214 |
void PropertyLinkSubList::setSyncSubObject(bool enable) |
| 2215 |
{ |
| 2216 |
_Flags.set((std::size_t)LinkSyncSubObject, enable); |
| 2217 |
} |
| 2218 |
|
| 2219 |
void PropertyLinkSubList::verifyObject(App::DocumentObject* obj, App::DocumentObject* parent) |
| 2220 |
{ |
| 2221 |
if (obj) { |
| 2222 |
if (!obj->isAttachedToDocument()) { |
| 2223 |
throw Base::ValueError("PropertyLinkSubList: invalid document object"); |
| 2224 |
} |
| 2225 |
if (!testFlag(LinkAllowExternal) && parent && parent->getDocument() != obj->getDocument()) { |
| 2226 |
throw Base::ValueError("PropertyLinkSubList does not support external object"); |
| 2227 |
} |
| 2228 |
} |
| 2229 |
} |
| 2230 |
|
| 2231 |
void PropertyLinkSubList::setSize(int newSize) |
| 2232 |
{ |
| 2233 |
_lValueList.resize(newSize); |
| 2234 |
_lSubList.resize(newSize); |
| 2235 |
_ShadowSubList.resize(newSize); |
| 2236 |
} |
| 2237 |
|
| 2238 |
int PropertyLinkSubList::getSize() const |
| 2239 |
{ |
| 2240 |
return static_cast<int>(_lValueList.size()); |
| 2241 |
} |
| 2242 |
|
| 2243 |
void PropertyLinkSubList::setValue(DocumentObject* lValue, const char* SubName) |
| 2244 |
{ |
| 2245 |
auto parent = freecad_cast<App::DocumentObject*>(getContainer()); |
| 2246 |
verifyObject(lValue, parent); |
| 2247 |
|
| 2248 |
// maintain backlinks |
| 2249 |
if (parent) { |
| 2250 |
// before accessing internals make sure the object is not about to be destroyed |
| 2251 |
// otherwise the backlink contains dangling pointers |
| 2252 |
if (!parent->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 2253 |
for (auto* obj : _lValueList) { |
| 2254 |
if (obj) { |
| 2255 |
obj->_removeBackLink(parent); |
| 2256 |
} |
| 2257 |
} |
| 2258 |
if (lValue) { |
| 2259 |
lValue->_addBackLink(parent); |
| 2260 |
} |
| 2261 |
} |
| 2262 |
} |
| 2263 |
|
| 2264 |
if (lValue) { |
| 2265 |
aboutToSetValue(); |
| 2266 |
_lValueList.resize(1); |
| 2267 |
_lValueList[0] = lValue; |
| 2268 |
_lSubList.resize(1); |
| 2269 |
_lSubList[0] = SubName; |
| 2270 |
} |
| 2271 |
else { |
| 2272 |
aboutToSetValue(); |
| 2273 |
_lValueList.clear(); |
| 2274 |
_lSubList.clear(); |
| 2275 |
} |
| 2276 |
updateElementReference(nullptr); |
| 2277 |
checkLabelReferences(_lSubList); |
| 2278 |
hasSetValue(); |
| 2279 |
} |
| 2280 |
|
| 2281 |
void PropertyLinkSubList::setValues(const std::vector<DocumentObject*>& lValue, |
| 2282 |
const std::vector<const char*>& lSubNames) |
| 2283 |
{ |
| 2284 |
auto parent = freecad_cast<App::DocumentObject*>(getContainer()); |
| 2285 |
for (auto obj : lValue) { |
| 2286 |
verifyObject(obj, parent); |
| 2287 |
} |
| 2288 |
|
| 2289 |
if (lValue.size() != lSubNames.size()) { |
| 2290 |
throw Base::ValueError( |
| 2291 |
"PropertyLinkSubList::setValues: size of subelements list != size of objects list"); |
| 2292 |
} |
| 2293 |
|
| 2294 |
// maintain backlinks. |
| 2295 |
if (parent) { |
| 2296 |
// before accessing internals make sure the object is not about to be destroyed |
| 2297 |
// otherwise the backlink contains dangling pointers |
| 2298 |
if (!parent->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 2299 |
//_lValueList can contain items multiple times, but we trust the document |
| 2300 |
// object to ensure that this works |
| 2301 |
for (auto* obj : _lValueList) { |
| 2302 |
if (obj) { |
| 2303 |
obj->_removeBackLink(parent); |
| 2304 |
} |
| 2305 |
} |
| 2306 |
|
| 2307 |
// maintain backlinks. lValue can contain items multiple times, but we trust the |
| 2308 |
// document object to ensure that the backlink is only added once |
| 2309 |
for (auto* obj : lValue) { |
| 2310 |
if (obj) { |
| 2311 |
obj->_addBackLink(parent); |
| 2312 |
} |
| 2313 |
} |
| 2314 |
} |
| 2315 |
} |
| 2316 |
|
| 2317 |
aboutToSetValue(); |
| 2318 |
_lValueList = lValue; |
| 2319 |
_lSubList.resize(lSubNames.size()); |
| 2320 |
int i = 0; |
| 2321 |
for (std::vector<const char*>::const_iterator it = lSubNames.begin(); it != lSubNames.end(); |
| 2322 |
++it, ++i) { |
| 2323 |
if (*it) { |
| 2324 |
_lSubList[i] = *it; |
| 2325 |
} |
| 2326 |
} |
| 2327 |
updateElementReference(nullptr); |
| 2328 |
checkLabelReferences(_lSubList); |
| 2329 |
hasSetValue(); |
| 2330 |
} |
| 2331 |
|
| 2332 |
void PropertyLinkSubList::setValues(const std::vector<DocumentObject*>& lValue, |
| 2333 |
const std::vector<std::string>& lSubNames, |
| 2334 |
std::vector<ShadowSub>&& ShadowSubList) |
| 2335 |
{ |
| 2336 |
setValues(std::vector<DocumentObject*>(lValue), |
| 2337 |
std::vector<std::string>(lSubNames), |
| 2338 |
std::move(ShadowSubList)); |
| 2339 |
} |
| 2340 |
|
| 2341 |
void PropertyLinkSubList::setValues(std::vector<DocumentObject*>&& lValue, |
| 2342 |
std::vector<std::string>&& lSubNames, |
| 2343 |
std::vector<ShadowSub>&& ShadowSubList) |
| 2344 |
{ |
| 2345 |
auto parent = freecad_cast<App::DocumentObject*>(getContainer()); |
| 2346 |
for (auto obj : lValue) { |
| 2347 |
verifyObject(obj, parent); |
| 2348 |
} |
| 2349 |
if (lValue.size() != lSubNames.size()) { |
| 2350 |
throw Base::ValueError( |
| 2351 |
"PropertyLinkSubList::setValues: size of subelements list != size of objects list"); |
| 2352 |
} |
| 2353 |
|
| 2354 |
// maintain backlinks. |
| 2355 |
if (parent) { |
| 2356 |
// before accessing internals make sure the object is not about to be destroyed |
| 2357 |
// otherwise the backlink contains dangling pointers |
| 2358 |
if (!parent->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 2359 |
//_lValueList can contain items multiple times, but we trust the document |
| 2360 |
// object to ensure that this works |
| 2361 |
for (auto* obj : _lValueList) { |
| 2362 |
if (obj) { |
| 2363 |
obj->_removeBackLink(parent); |
| 2364 |
} |
| 2365 |
} |
| 2366 |
|
| 2367 |
// maintain backlinks. lValue can contain items multiple times, but we trust the |
| 2368 |
// document object to ensure that the backlink is only added once |
| 2369 |
for (auto* obj : lValue) { |
| 2370 |
if (obj) { |
| 2371 |
obj->_addBackLink(parent); |
| 2372 |
} |
| 2373 |
} |
| 2374 |
} |
| 2375 |
} |
| 2376 |
|
| 2377 |
aboutToSetValue(); |
| 2378 |
_lValueList = std::move(lValue); |
| 2379 |
_lSubList = std::move(lSubNames); |
| 2380 |
if (ShadowSubList.size() == _lSubList.size()) { |
| 2381 |
_ShadowSubList = std::move(ShadowSubList); |
| 2382 |
onContainerRestored(); // re-register element references |
| 2383 |
} |
| 2384 |
else { |
| 2385 |
updateElementReference(nullptr); |
| 2386 |
} |
| 2387 |
checkLabelReferences(_lSubList); |
| 2388 |
hasSetValue(); |
| 2389 |
} |
| 2390 |
|
| 2391 |
void PropertyLinkSubList::setValue(DocumentObject* lValue, const std::vector<std::string>& SubList) |
| 2392 |
{ |
| 2393 |
auto parent = dynamic_cast<App::DocumentObject*>(getContainer()); |
| 2394 |
verifyObject(lValue, parent); |
| 2395 |
|
| 2396 |
// maintain backlinks. |
| 2397 |
if (parent) { |
| 2398 |
// before accessing internals make sure the object is not about to be destroyed |
| 2399 |
// otherwise the backlink contains dangling pointers |
| 2400 |
if (!parent->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 2401 |
//_lValueList can contain items multiple times, but we trust the document |
| 2402 |
// object to ensure that this works |
| 2403 |
for (auto* obj : _lValueList) { |
| 2404 |
if (obj) { |
| 2405 |
obj->_removeBackLink(parent); |
| 2406 |
} |
| 2407 |
} |
| 2408 |
|
| 2409 |
// maintain backlinks. lValue can contain items multiple times, but we trust the |
| 2410 |
// document object to ensure that the backlink is only added once |
| 2411 |
if (lValue) { |
| 2412 |
lValue->_addBackLink(parent); |
| 2413 |
} |
| 2414 |
} |
| 2415 |
} |
| 2416 |
|
| 2417 |
aboutToSetValue(); |
| 2418 |
std::size_t size = SubList.size(); |
| 2419 |
this->_lValueList.clear(); |
| 2420 |
this->_lSubList.clear(); |
| 2421 |
if (size == 0) { |
| 2422 |
if (lValue) { |
| 2423 |
this->_lValueList.push_back(lValue); |
| 2424 |
this->_lSubList.emplace_back(); |
| 2425 |
} |
| 2426 |
} |
| 2427 |
else { |
| 2428 |
this->_lSubList = SubList; |
| 2429 |
this->_lValueList.insert(this->_lValueList.begin(), size, lValue); |
| 2430 |
} |
| 2431 |
updateElementReference(nullptr); |
| 2432 |
checkLabelReferences(_lSubList); |
| 2433 |
hasSetValue(); |
| 2434 |
} |
| 2435 |
|
| 2436 |
void PropertyLinkSubList::addValue(App::DocumentObject* obj, |
| 2437 |
const std::vector<std::string>& subs, |
| 2438 |
bool reset) |
| 2439 |
{ |
| 2440 |
auto parent = freecad_cast<App::DocumentObject*>(getContainer()); |
| 2441 |
verifyObject(obj, parent); |
| 2442 |
|
| 2443 |
// maintain backlinks. |
| 2444 |
if (parent) { |
| 2445 |
// before accessing internals make sure the object is not about to be destroyed |
| 2446 |
// otherwise the backlink contains dangling pointers |
| 2447 |
if (!parent->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 2448 |
//_lValueList can contain items multiple times, but we trust the document |
| 2449 |
// object to ensure that this works |
| 2450 |
if (reset) { |
| 2451 |
for (auto* value : _lValueList) { |
| 2452 |
if (value && value == obj) { |
| 2453 |
value->_removeBackLink(parent); |
| 2454 |
} |
| 2455 |
} |
| 2456 |
} |
| 2457 |
|
| 2458 |
// maintain backlinks. lValue can contain items multiple times, but we trust the |
| 2459 |
// document object to ensure that the backlink is only added once |
| 2460 |
if (obj) { |
| 2461 |
obj->_addBackLink(parent); |
| 2462 |
} |
| 2463 |
} |
| 2464 |
} |
| 2465 |
|
| 2466 |
std::vector<DocumentObject*> valueList; |
| 2467 |
std::vector<std::string> subList; |
| 2468 |
|
| 2469 |
if (reset) { |
| 2470 |
for (std::size_t i = 0; i < _lValueList.size(); i++) { |
| 2471 |
if (_lValueList[i] != obj) { |
| 2472 |
valueList.push_back(_lValueList[i]); |
| 2473 |
subList.push_back(_lSubList[i]); |
| 2474 |
} |
| 2475 |
} |
| 2476 |
} |
| 2477 |
else { |
| 2478 |
valueList = _lValueList; |
| 2479 |
subList = _lSubList; |
| 2480 |
} |
| 2481 |
|
| 2482 |
std::size_t size = subs.size(); |
| 2483 |
if (size == 0) { |
| 2484 |
if (obj) { |
| 2485 |
valueList.push_back(obj); |
| 2486 |
subList.emplace_back(); |
| 2487 |
} |
| 2488 |
} |
| 2489 |
else if (obj) { |
| 2490 |
subList.insert(subList.end(), subs.begin(), subs.end()); |
| 2491 |
valueList.insert(valueList.end(), size, obj); |
| 2492 |
} |
| 2493 |
|
| 2494 |
aboutToSetValue(); |
| 2495 |
_lValueList = valueList; |
| 2496 |
_lSubList = subList; |
| 2497 |
updateElementReference(nullptr); |
| 2498 |
checkLabelReferences(_lSubList); |
| 2499 |
hasSetValue(); |
| 2500 |
} |
| 2501 |
|
| 2502 |
const string PropertyLinkSubList::getPyReprString() const |
| 2503 |
{ |
| 2504 |
assert(this->_lValueList.size() == this->_lSubList.size()); |
| 2505 |
|
| 2506 |
if (this->_lValueList.empty()) { |
| 2507 |
return std::string("None"); |
| 2508 |
} |
| 2509 |
|
| 2510 |
std::stringstream strm; |
| 2511 |
strm << "["; |
| 2512 |
for (std::size_t i = 0; i < this->_lSubList.size(); i++) { |
| 2513 |
if (i > 0) { |
| 2514 |
strm << ",("; |
| 2515 |
} |
| 2516 |
else { |
| 2517 |
strm << "("; |
| 2518 |
} |
| 2519 |
App::DocumentObject* obj = this->_lValueList[i]; |
| 2520 |
if (obj) { |
| 2521 |
strm << "App.getDocument('" << obj->getDocument()->getName() << "').getObject('" |
| 2522 |
<< obj->getNameInDocument() << "')"; |
| 2523 |
} |
| 2524 |
else { |
| 2525 |
strm << "None"; |
| 2526 |
} |
| 2527 |
strm << ","; |
| 2528 |
strm << "'" << this->_lSubList[i] << "'"; |
| 2529 |
strm << ")"; |
| 2530 |
} |
| 2531 |
strm << "]"; |
| 2532 |
return strm.str(); |
| 2533 |
} |
| 2534 |
|
| 2535 |
DocumentObject* PropertyLinkSubList::getValue() const |
| 2536 |
{ |
| 2537 |
App::DocumentObject* ret = nullptr; |
| 2538 |
// FIXME: cache this to avoid iterating each time, to improve speed |
| 2539 |
for (auto i : this->_lValueList) { |
| 2540 |
if (!ret) { |
| 2541 |
ret = i; |
| 2542 |
} |
| 2543 |
if (ret != i) { |
| 2544 |
return nullptr; |
| 2545 |
} |
| 2546 |
} |
| 2547 |
return ret; |
| 2548 |
} |
| 2549 |
|
| 2550 |
int PropertyLinkSubList::removeValue(App::DocumentObject* lValue) |
| 2551 |
{ |
| 2552 |
assert(this->_lValueList.size() == this->_lSubList.size()); |
| 2553 |
|
| 2554 |
std::size_t num = std::count(this->_lValueList.begin(), this->_lValueList.end(), lValue); |
| 2555 |
if (num == 0) { |
| 2556 |
return 0; |
| 2557 |
} |
| 2558 |
|
| 2559 |
std::vector<DocumentObject*> links; |
| 2560 |
std::vector<std::string> subs; |
| 2561 |
links.reserve(this->_lValueList.size() - num); |
| 2562 |
subs.reserve(this->_lSubList.size() - num); |
| 2563 |
|
| 2564 |
for (std::size_t i = 0; i < this->_lValueList.size(); ++i) { |
| 2565 |
if (this->_lValueList[i] != lValue) { |
| 2566 |
links.push_back(this->_lValueList[i]); |
| 2567 |
subs.push_back(this->_lSubList[i]); |
| 2568 |
} |
| 2569 |
} |
| 2570 |
|
| 2571 |
setValues(links, subs); |
| 2572 |
return static_cast<int>(num); |
| 2573 |
} |
| 2574 |
|
| 2575 |
void PropertyLinkSubList::setSubListValues(const std::vector<PropertyLinkSubList::SubSet>& values) |
| 2576 |
{ |
| 2577 |
std::vector<DocumentObject*> links; |
| 2578 |
std::vector<std::string> subs; |
| 2579 |
for (std::vector<PropertyLinkSubList::SubSet>::const_iterator it = values.begin(); |
| 2580 |
it != values.end(); |
| 2581 |
++it) { |
| 2582 |
if (it->second.empty()) { |
| 2583 |
links.push_back(it->first); |
| 2584 |
subs.emplace_back(); |
| 2585 |
continue; |
| 2586 |
} |
| 2587 |
for (std::vector<std::string>::const_iterator jt = it->second.begin(); |
| 2588 |
jt != it->second.end(); |
| 2589 |
++jt) { |
| 2590 |
links.push_back(it->first); |
| 2591 |
subs.push_back(*jt); |
| 2592 |
} |
| 2593 |
} |
| 2594 |
setValues(links, subs); |
| 2595 |
} |
| 2596 |
|
| 2597 |
std::vector<PropertyLinkSubList::SubSet> PropertyLinkSubList::getSubListValues(bool newStyle) const |
| 2598 |
{ |
| 2599 |
std::vector<PropertyLinkSubList::SubSet> values; |
| 2600 |
if (_lValueList.size() != _lSubList.size()) { |
| 2601 |
throw Base::ValueError("PropertyLinkSubList::getSubListValues: size of subelements list != " |
| 2602 |
"size of objects list"); |
| 2603 |
} |
| 2604 |
|
| 2605 |
assert(_ShadowSubList.size() == _lSubList.size()); |
| 2606 |
|
| 2607 |
for (std::size_t i = 0; i < _lValueList.size(); i++) { |
| 2608 |
App::DocumentObject* link = _lValueList[i]; |
| 2609 |
std::string sub; |
| 2610 |
if (newStyle && !_ShadowSubList[i].newName.empty()) { |
| 2611 |
sub = _ShadowSubList[i].newName; |
| 2612 |
} |
| 2613 |
else if (!newStyle && !_ShadowSubList[i].oldName.empty()) { |
| 2614 |
sub = _ShadowSubList[i].oldName; |
| 2615 |
} |
| 2616 |
else { |
| 2617 |
sub = _lSubList[i]; |
| 2618 |
} |
| 2619 |
if (values.empty() || values.back().first != link) { |
| 2620 |
// new object started, start a new subset. |
| 2621 |
values.emplace_back(link, std::vector<std::string>()); |
| 2622 |
} |
| 2623 |
values.back().second.push_back(sub); |
| 2624 |
} |
| 2625 |
return values; |
| 2626 |
} |
| 2627 |
|
| 2628 |
PyObject* PropertyLinkSubList::getPyObject() |
| 2629 |
{ |
| 2630 |
std::vector<SubSet> subLists = getSubListValues(); |
| 2631 |
std::size_t count = subLists.size(); |
| 2632 |
#if 0 // FIXME: Should switch to tuple |
| 2633 |
Py::Tuple sequence(count); |
| 2634 |
#else |
| 2635 |
Py::List sequence(count); |
| 2636 |
#endif |
| 2637 |
for (std::size_t i = 0; i < count; i++) { |
| 2638 |
Py::Tuple tup(2); |
| 2639 |
tup[0] = Py::asObject(subLists[i].first->getPyObject()); |
| 2640 |
|
| 2641 |
const std::vector<std::string>& sub = subLists[i].second; |
| 2642 |
Py::Tuple items(sub.size()); |
| 2643 |
for (std::size_t j = 0; j < sub.size(); j++) { |
| 2644 |
items[j] = Py::String(sub[j]); |
| 2645 |
} |
| 2646 |
|
| 2647 |
tup[1] = items; |
| 2648 |
sequence[i] = tup; |
| 2649 |
} |
| 2650 |
|
| 2651 |
return Py::new_reference_to(sequence); |
| 2652 |
} |
| 2653 |
|
| 2654 |
void PropertyLinkSubList::setPyObject(PyObject* value) |
| 2655 |
{ |
| 2656 |
try { // try PropertyLinkSub syntax |
| 2657 |
PropertyLinkSub dummy; |
| 2658 |
dummy.setPyObject(value); |
| 2659 |
this->setValue(dummy.getValue(), dummy.getSubValues()); |
| 2660 |
return; |
| 2661 |
} |
| 2662 |
catch (...) { |
| 2663 |
} |
| 2664 |
try { |
| 2665 |
// try PropertyLinkList syntax |
| 2666 |
PropertyLinkList dummy; |
| 2667 |
dummy.setPyObject(value); |
| 2668 |
const auto& values = dummy.getValues(); |
| 2669 |
std::vector<std::string> subs(values.size()); |
| 2670 |
this->setValues(values, subs); |
| 2671 |
return; |
| 2672 |
} |
| 2673 |
catch (...) { |
| 2674 |
} |
| 2675 |
|
| 2676 |
static const char* errMsg = |
| 2677 |
"Expects sequence of items of type DocObj, (DocObj,SubName), or (DocObj, (SubName,...))"; |
| 2678 |
|
| 2679 |
if (!PyTuple_Check(value) && !PyList_Check(value)) { |
| 2680 |
throw Base::TypeError(errMsg); |
| 2681 |
} |
| 2682 |
|
| 2683 |
Py::Sequence list(value); |
| 2684 |
Py::Sequence::size_type size = list.size(); |
| 2685 |
|
| 2686 |
std::vector<DocumentObject*> values; |
| 2687 |
values.reserve(size); |
| 2688 |
std::vector<std::string> SubNames; |
| 2689 |
SubNames.reserve(size); |
| 2690 |
for (Py::Sequence::size_type i = 0; i < size; i++) { |
| 2691 |
Py::Object item = list[i]; |
| 2692 |
if ((item.isTuple() || item.isSequence()) && PySequence_Size(*item) == 2) { |
| 2693 |
Py::Sequence seq(item); |
| 2694 |
if (PyObject_TypeCheck(seq[0].ptr(), &(DocumentObjectPy::Type))) { |
| 2695 |
auto obj = static_cast<DocumentObjectPy*>(seq[0].ptr())->getDocumentObjectPtr(); |
| 2696 |
PropertyString propString; |
| 2697 |
if (seq[1].isString()) { |
| 2698 |
values.push_back(obj); |
| 2699 |
propString.setPyObject(seq[1].ptr()); |
| 2700 |
SubNames.emplace_back(propString.getValue()); |
| 2701 |
} |
| 2702 |
else if (seq[1].isSequence()) { |
| 2703 |
Py::Sequence list(seq[1]); |
| 2704 |
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { |
| 2705 |
if (!(*it).isString()) { |
| 2706 |
throw Base::TypeError(errMsg); |
| 2707 |
} |
| 2708 |
values.push_back(obj); |
| 2709 |
propString.setPyObject((*it).ptr()); |
| 2710 |
SubNames.emplace_back(propString.getValue()); |
| 2711 |
} |
| 2712 |
} |
| 2713 |
else { |
| 2714 |
throw Base::TypeError(errMsg); |
| 2715 |
} |
| 2716 |
} |
| 2717 |
} |
| 2718 |
else if (PyObject_TypeCheck(*item, &(DocumentObjectPy::Type))) { |
| 2719 |
DocumentObjectPy* pcObj; |
| 2720 |
pcObj = static_cast<DocumentObjectPy*>(*item); |
| 2721 |
values.push_back(pcObj->getDocumentObjectPtr()); |
| 2722 |
SubNames.emplace_back(); |
| 2723 |
} |
| 2724 |
else { |
| 2725 |
throw Base::TypeError(errMsg); |
| 2726 |
} |
| 2727 |
} |
| 2728 |
setValues(values, SubNames); |
| 2729 |
} |
| 2730 |
|
| 2731 |
void PropertyLinkSubList::afterRestore() |
| 2732 |
{ |
| 2733 |
assert(_lSubList.size() == _ShadowSubList.size()); |
| 2734 |
if (!testFlag(LinkRestoreLabel)) { |
| 2735 |
return; |
| 2736 |
} |
| 2737 |
setFlag(LinkRestoreLabel, false); |
| 2738 |
for (size_t i = 0; i < _lSubList.size(); ++i) { |
| 2739 |
restoreLabelReference(_lValueList[i], _lSubList[i], &_ShadowSubList[i]); |
| 2740 |
} |
| 2741 |
} |
| 2742 |
|
| 2743 |
void PropertyLinkSubList::onContainerRestored() |
| 2744 |
{ |
| 2745 |
unregisterElementReference(); |
| 2746 |
for (size_t i = 0; i < _lSubList.size(); ++i) { |
| 2747 |
_registerElementReference(_lValueList[i], _lSubList[i], _ShadowSubList[i]); |
| 2748 |
} |
| 2749 |
} |
| 2750 |
|
| 2751 |
void PropertyLinkSubList::updateElementReference(DocumentObject* feature, bool reverse, bool notify) |
| 2752 |
{ |
| 2753 |
if (!feature) { |
| 2754 |
_ShadowSubList.clear(); |
| 2755 |
unregisterElementReference(); |
| 2756 |
} |
| 2757 |
_ShadowSubList.resize(_lSubList.size()); |
| 2758 |
auto owner = freecad_cast<DocumentObject*>(getContainer()); |
| 2759 |
if (owner && owner->isRestoring()) { |
| 2760 |
return; |
| 2761 |
} |
| 2762 |
int i = 0; |
| 2763 |
bool touched = false; |
| 2764 |
for (auto& sub : _lSubList) { |
| 2765 |
auto obj = _lValueList[i]; |
| 2766 |
if (_updateElementReference(feature, |
| 2767 |
obj, |
| 2768 |
sub, |
| 2769 |
_ShadowSubList[i++], |
| 2770 |
reverse, |
| 2771 |
notify && !touched)) { |
| 2772 |
touched = true; |
| 2773 |
} |
| 2774 |
} |
| 2775 |
if (!touched) { |
| 2776 |
return; |
| 2777 |
} |
| 2778 |
|
| 2779 |
std::vector<int> mapped; |
| 2780 |
mapped.reserve(_mapped.size()); |
| 2781 |
for (int idx : _mapped) { |
| 2782 |
if (idx < (int)_lSubList.size()) { |
| 2783 |
if (!_ShadowSubList[idx].newName.empty()) { |
| 2784 |
_lSubList[idx] = _ShadowSubList[idx].newName; |
| 2785 |
} |
| 2786 |
else { |
| 2787 |
mapped.push_back(idx); |
| 2788 |
} |
| 2789 |
} |
| 2790 |
} |
| 2791 |
_mapped.swap(mapped); |
| 2792 |
if (owner && feature) { |
| 2793 |
owner->onUpdateElementReference(this); |
| 2794 |
} |
| 2795 |
if (notify) { |
| 2796 |
hasSetValue(); |
| 2797 |
} |
| 2798 |
} |
| 2799 |
|
| 2800 |
bool PropertyLinkSubList::referenceChanged() const |
| 2801 |
{ |
| 2802 |
return !_mapped.empty(); |
| 2803 |
} |
| 2804 |
|
| 2805 |
void PropertyLinkSubList::Save(Base::Writer& writer) const |
| 2806 |
{ |
| 2807 |
assert(_lSubList.size() == _ShadowSubList.size()); |
| 2808 |
|
| 2809 |
int count = 0; |
| 2810 |
for (auto obj : _lValueList) { |
| 2811 |
if (obj && obj->isAttachedToDocument()) { |
| 2812 |
++count; |
| 2813 |
} |
| 2814 |
} |
| 2815 |
writer.Stream() << writer.ind() << "<LinkSubList count=\"" << count << "\">" << endl; |
| 2816 |
writer.incInd(); |
| 2817 |
auto owner = freecad_cast<DocumentObject*>(getContainer()); |
| 2818 |
bool exporting = owner && owner->isExporting(); |
| 2819 |
for (int i = 0; i < getSize(); i++) { |
| 2820 |
auto obj = _lValueList[i]; |
| 2821 |
if (!obj || !obj->isAttachedToDocument()) { |
| 2822 |
continue; |
| 2823 |
} |
| 2824 |
const auto& shadow = _ShadowSubList[i]; |
| 2825 |
// shadow.oldName stores the old style element name. For backward |
| 2826 |
// compatibility reason, we shall store the old name into attribute |
| 2827 |
// 'value' whenever possible. |
| 2828 |
const auto& sub = shadow.oldName.empty() ? _lSubList[i] : shadow.oldName; |
| 2829 |
|
| 2830 |
writer.Stream() << writer.ind() << "<Link obj=\"" << obj->getExportName() << "\" sub=\""; |
| 2831 |
if (exporting) { |
| 2832 |
std::string exportName; |
| 2833 |
writer.Stream() << encodeAttribute(exportSubName(exportName, obj, sub.c_str())); |
| 2834 |
if (!shadow.oldName.empty() && _lSubList[i] == shadow.newName) { |
| 2835 |
writer.Stream() << "\" " ATTR_MAPPED "=\"1"; |
| 2836 |
} |
| 2837 |
} |
| 2838 |
else { |
| 2839 |
writer.Stream() << encodeAttribute(sub); |
| 2840 |
if (!_lSubList[i].empty()) { |
| 2841 |
if (sub != _lSubList[i]) { |
| 2842 |
// Stores the actual value that is shadowed. For new version FC, |
| 2843 |
// we will restore this shadowed value instead. |
| 2844 |
writer.Stream() << "\" " ATTR_SHADOWED "=\"" << encodeAttribute(_lSubList[i]); |
| 2845 |
} |
| 2846 |
else if (!shadow.newName.empty()) { |
| 2847 |
// Here means the user set value is old style element name. |
| 2848 |
// We shall then store the shadow somewhere else. |
| 2849 |
writer.Stream() << "\" " ATTR_SHADOW "=\"" << encodeAttribute(shadow.newName); |
| 2850 |
} |
| 2851 |
} |
| 2852 |
} |
| 2853 |
writer.Stream() << "\"/>" << endl; |
| 2854 |
} |
| 2855 |
|
| 2856 |
writer.decInd(); |
| 2857 |
writer.Stream() << writer.ind() << "</LinkSubList>" << endl; |
| 2858 |
} |
| 2859 |
|
| 2860 |
void PropertyLinkSubList::Restore(Base::XMLReader& reader) |
| 2861 |
{ |
| 2862 |
// read my element |
| 2863 |
reader.readElement("LinkSubList"); |
| 2864 |
// get the value of my attribute |
| 2865 |
int count = reader.getAttribute<long>("count"); |
| 2866 |
|
| 2867 |
std::vector<DocumentObject*> values; |
| 2868 |
values.reserve(count); |
| 2869 |
std::vector<std::string> SubNames; |
| 2870 |
SubNames.reserve(count); |
| 2871 |
std::vector<ShadowSub> shadows; |
| 2872 |
shadows.reserve(count); |
| 2873 |
DocumentObject* father = freecad_cast<DocumentObject*>(getContainer()); |
| 2874 |
App::Document* document = father ? father->getDocument() : nullptr; |
| 2875 |
std::vector<int> mapped; |
| 2876 |
bool restoreLabel = false; |
| 2877 |
for (int i = 0; i < count; i++) { |
| 2878 |
reader.readElement("Link"); |
| 2879 |
std::string name = reader.getName(reader.getAttribute<const char*>("obj")); |
| 2880 |
// In order to do copy/paste it must be allowed to have defined some |
| 2881 |
// referenced objects in XML which do not exist anymore in the new |
| 2882 |
// document. Thus, we should silently ignore this. |
| 2883 |
// Property not in an object! |
| 2884 |
DocumentObject* child = document ? document->getObject(name.c_str()) : nullptr; |
| 2885 |
if (child) { |
| 2886 |
values.push_back(child); |
| 2887 |
shadows.emplace_back(); |
| 2888 |
auto& shadow = shadows.back(); |
| 2889 |
shadow.oldName = importSubName(reader, reader.getAttribute<const char*>("sub"), restoreLabel); |
| 2890 |
if (reader.hasAttribute(ATTR_SHADOWED) && !IGNORE_SHADOW) { |
| 2891 |
shadow.newName = |
| 2892 |
importSubName(reader, reader.getAttribute<const char*>(ATTR_SHADOWED), restoreLabel); |
| 2893 |
SubNames.push_back(shadow.newName); |
| 2894 |
} |
| 2895 |
else { |
| 2896 |
SubNames.push_back(shadow.oldName); |
| 2897 |
if (reader.hasAttribute(ATTR_SHADOW) && !IGNORE_SHADOW) { |
| 2898 |
shadow.newName = |
| 2899 |
importSubName(reader, reader.getAttribute<const char*>(ATTR_SHADOW), restoreLabel); |
| 2900 |
} |
| 2901 |
} |
| 2902 |
if (reader.hasAttribute(ATTR_MAPPED)) { |
| 2903 |
mapped.push_back(i); |
| 2904 |
} |
| 2905 |
} |
| 2906 |
else if (reader.isVerbose()) { |
| 2907 |
Base::Console().warning("Lost link to '%s' while loading, maybe " |
| 2908 |
"an object was not loaded correctly\n", |
| 2909 |
name.c_str()); |
| 2910 |
} |
| 2911 |
} |
| 2912 |
setFlag(LinkRestoreLabel, restoreLabel); |
| 2913 |
|
| 2914 |
reader.readEndElement("LinkSubList"); |
| 2915 |
|
| 2916 |
// assignment |
| 2917 |
setValues(values, SubNames, std::move(shadows)); |
| 2918 |
_mapped.swap(mapped); |
| 2919 |
} |
| 2920 |
|
| 2921 |
bool PropertyLinkSubList::upgrade(Base::XMLReader& reader, const char* typeName) |
| 2922 |
{ |
| 2923 |
Base::Type type = Base::Type::fromName(typeName); |
| 2924 |
if (type.isDerivedFrom(PropertyLink::getClassTypeId())) { |
| 2925 |
PropertyLink prop; |
| 2926 |
prop.setContainer(getContainer()); |
| 2927 |
prop.Restore(reader); |
| 2928 |
setValue(prop.getValue()); |
| 2929 |
return true; |
| 2930 |
} |
| 2931 |
else if (type.isDerivedFrom(PropertyLinkList::getClassTypeId())) { |
| 2932 |
PropertyLinkList prop; |
| 2933 |
prop.setContainer(getContainer()); |
| 2934 |
prop.Restore(reader); |
| 2935 |
std::vector<std::string> subnames; |
| 2936 |
subnames.resize(prop.getSize()); |
| 2937 |
setValues(prop.getValues(), subnames); |
| 2938 |
return true; |
| 2939 |
} |
| 2940 |
else if (type.isDerivedFrom(PropertyLinkSub::getClassTypeId())) { |
| 2941 |
PropertyLinkSub prop; |
| 2942 |
prop.setContainer(getContainer()); |
| 2943 |
prop.Restore(reader); |
| 2944 |
setValue(prop.getValue(), prop.getSubValues()); |
| 2945 |
return true; |
| 2946 |
} |
| 2947 |
|
| 2948 |
return false; |
| 2949 |
} |
| 2950 |
|
| 2951 |
Property* |
| 2952 |
PropertyLinkSubList::CopyOnImportExternal(const std::map<std::string, std::string>& nameMap) const |
| 2953 |
{ |
| 2954 |
auto owner = dynamic_cast<const DocumentObject*>(getContainer()); |
| 2955 |
if (!owner || !owner->getDocument() || _lValueList.size() != _lSubList.size()) { |
| 2956 |
return nullptr; |
| 2957 |
} |
| 2958 |
std::vector<App::DocumentObject*> values; |
| 2959 |
std::vector<std::string> subs; |
| 2960 |
auto itSub = _lSubList.begin(); |
| 2961 |
for (auto itValue = _lValueList.begin(); itValue != _lValueList.end(); ++itValue, ++itSub) { |
| 2962 |
auto value = *itValue; |
| 2963 |
const auto& sub = *itSub; |
| 2964 |
if (!value || !value->isAttachedToDocument()) { |
| 2965 |
if (!values.empty()) { |
| 2966 |
values.push_back(value); |
| 2967 |
subs.push_back(sub); |
| 2968 |
} |
| 2969 |
continue; |
| 2970 |
} |
| 2971 |
auto linked = tryImport(owner->getDocument(), value, nameMap); |
| 2972 |
auto new_sub = tryImportSubName(value, sub.c_str(), owner->getDocument(), nameMap); |
| 2973 |
if (linked != value || !new_sub.empty()) { |
| 2974 |
if (values.empty()) { |
| 2975 |
values.reserve(_lValueList.size()); |
| 2976 |
values.insert(values.end(), _lValueList.begin(), itValue); |
| 2977 |
subs.reserve(_lSubList.size()); |
| 2978 |
subs.insert(subs.end(), _lSubList.begin(), itSub); |
| 2979 |
} |
| 2980 |
values.push_back(linked); |
| 2981 |
subs.push_back(std::move(new_sub)); |
| 2982 |
} |
| 2983 |
else if (!values.empty()) { |
| 2984 |
values.push_back(linked); |
| 2985 |
subs.push_back(sub); |
| 2986 |
} |
| 2987 |
} |
| 2988 |
if (values.empty()) { |
| 2989 |
return nullptr; |
| 2990 |
} |
| 2991 |
std::unique_ptr<PropertyLinkSubList> p(new PropertyLinkSubList); |
| 2992 |
p->_lValueList = std::move(values); |
| 2993 |
p->_lSubList = std::move(subs); |
| 2994 |
return p.release(); |
| 2995 |
} |
| 2996 |
|
| 2997 |
Property* PropertyLinkSubList::CopyOnLabelChange(App::DocumentObject* obj, |
| 2998 |
const std::string& ref, |
| 2999 |
const char* newLabel) const |
| 3000 |
{ |
| 3001 |
auto owner = dynamic_cast<const DocumentObject*>(getContainer()); |
| 3002 |
if (!owner || !owner->getDocument()) { |
| 3003 |
return nullptr; |
| 3004 |
} |
| 3005 |
std::vector<App::DocumentObject*> values; |
| 3006 |
std::vector<std::string> subs; |
| 3007 |
auto itSub = _lSubList.begin(); |
| 3008 |
for (auto itValue = _lValueList.begin(); itValue != _lValueList.end(); ++itValue, ++itSub) { |
| 3009 |
auto value = *itValue; |
| 3010 |
const auto& sub = *itSub; |
| 3011 |
if (!value || !value->isAttachedToDocument()) { |
| 3012 |
if (!values.empty()) { |
| 3013 |
values.push_back(value); |
| 3014 |
subs.push_back(sub); |
| 3015 |
} |
| 3016 |
continue; |
| 3017 |
} |
| 3018 |
auto new_sub = updateLabelReference(value, sub.c_str(), obj, ref, newLabel); |
| 3019 |
if (!new_sub.empty()) { |
| 3020 |
if (values.empty()) { |
| 3021 |
values.reserve(_lValueList.size()); |
| 3022 |
values.insert(values.end(), _lValueList.begin(), itValue); |
| 3023 |
subs.reserve(_lSubList.size()); |
| 3024 |
subs.insert(subs.end(), _lSubList.begin(), itSub); |
| 3025 |
} |
| 3026 |
values.push_back(value); |
| 3027 |
subs.push_back(std::move(new_sub)); |
| 3028 |
} |
| 3029 |
else if (!values.empty()) { |
| 3030 |
values.push_back(value); |
| 3031 |
subs.push_back(sub); |
| 3032 |
} |
| 3033 |
} |
| 3034 |
if (values.empty()) { |
| 3035 |
return nullptr; |
| 3036 |
} |
| 3037 |
std::unique_ptr<PropertyLinkSubList> p(new PropertyLinkSubList); |
| 3038 |
p->_lValueList = std::move(values); |
| 3039 |
p->_lSubList = std::move(subs); |
| 3040 |
return p.release(); |
| 3041 |
} |
| 3042 |
|
| 3043 |
Property* PropertyLinkSubList::CopyOnLinkReplace(const App::DocumentObject* parent, |
| 3044 |
App::DocumentObject* oldObj, |
| 3045 |
App::DocumentObject* newObj) const |
| 3046 |
{ |
| 3047 |
std::vector<App::DocumentObject*> values; |
| 3048 |
std::vector<std::string> subs; |
| 3049 |
auto itSub = _lSubList.begin(); |
| 3050 |
std::vector<size_t> positions; |
| 3051 |
for (auto itValue = _lValueList.begin(); itValue != _lValueList.end(); ++itValue, ++itSub) { |
| 3052 |
auto value = *itValue; |
| 3053 |
const auto& sub = *itSub; |
| 3054 |
if (!value || !value->isAttachedToDocument()) { |
| 3055 |
if (!values.empty()) { |
| 3056 |
values.push_back(value); |
| 3057 |
subs.push_back(sub); |
| 3058 |
} |
| 3059 |
continue; |
| 3060 |
} |
| 3061 |
auto res = tryReplaceLink(getContainer(), value, parent, oldObj, newObj, sub.c_str()); |
| 3062 |
if (res.first) { |
| 3063 |
if (values.empty()) { |
| 3064 |
values.reserve(_lValueList.size()); |
| 3065 |
values.insert(values.end(), _lValueList.begin(), itValue); |
| 3066 |
subs.reserve(_lSubList.size()); |
| 3067 |
subs.insert(subs.end(), _lSubList.begin(), itSub); |
| 3068 |
} |
| 3069 |
if (res.first == newObj) { |
| 3070 |
// check for duplication |
| 3071 |
auto itS = subs.begin(); |
| 3072 |
for (auto itV = values.begin(); itV != values.end();) { |
| 3073 |
if (*itV == res.first && *itS == res.second) { |
| 3074 |
itV = values.erase(itV); |
| 3075 |
itS = subs.erase(itS); |
| 3076 |
} |
| 3077 |
else { |
| 3078 |
++itV; |
| 3079 |
++itS; |
| 3080 |
} |
| 3081 |
} |
| 3082 |
positions.push_back(values.size()); |
| 3083 |
} |
| 3084 |
values.push_back(res.first); |
| 3085 |
subs.push_back(std::move(res.second)); |
| 3086 |
} |
| 3087 |
else if (!values.empty()) { |
| 3088 |
bool duplicate = false; |
| 3089 |
if (value == newObj) { |
| 3090 |
for (auto pos : positions) { |
| 3091 |
if (sub == subs[pos]) { |
| 3092 |
duplicate = true; |
| 3093 |
break; |
| 3094 |
} |
| 3095 |
} |
| 3096 |
} |
| 3097 |
if (!duplicate) { |
| 3098 |
values.push_back(value); |
| 3099 |
subs.push_back(sub); |
| 3100 |
} |
| 3101 |
} |
| 3102 |
} |
| 3103 |
if (values.empty()) { |
| 3104 |
return nullptr; |
| 3105 |
} |
| 3106 |
std::unique_ptr<PropertyLinkSubList> p(new PropertyLinkSubList); |
| 3107 |
p->_lValueList = std::move(values); |
| 3108 |
p->_lSubList = std::move(subs); |
| 3109 |
return p.release(); |
| 3110 |
} |
| 3111 |
|
| 3112 |
Property* PropertyLinkSubList::Copy() const |
| 3113 |
{ |
| 3114 |
PropertyLinkSubList* p = new PropertyLinkSubList(); |
| 3115 |
p->_lValueList = _lValueList; |
| 3116 |
p->_lSubList = _lSubList; |
| 3117 |
p->_ShadowSubList = _ShadowSubList; |
| 3118 |
return p; |
| 3119 |
} |
| 3120 |
|
| 3121 |
void PropertyLinkSubList::Paste(const Property& from) |
| 3122 |
{ |
| 3123 |
if (!from.isDerivedFrom<PropertyLinkSubList>()) { |
| 3124 |
throw Base::TypeError("Incompatible property to paste to"); |
| 3125 |
} |
| 3126 |
auto& link = static_cast<const PropertyLinkSubList&>(from); |
| 3127 |
setValues(link._lValueList, link._lSubList, std::vector<ShadowSub>(link._ShadowSubList)); |
| 3128 |
} |
| 3129 |
|
| 3130 |
unsigned int PropertyLinkSubList::getMemSize() const |
| 3131 |
{ |
| 3132 |
unsigned int size = |
| 3133 |
static_cast<unsigned int>(_lValueList.size() * sizeof(App::DocumentObject*)); |
| 3134 |
for (int i = 0; i < getSize(); i++) { |
| 3135 |
size += _lSubList[i].size(); |
| 3136 |
} |
| 3137 |
return size; |
| 3138 |
} |
| 3139 |
|
| 3140 |
std::vector<std::string> PropertyLinkSubList::getSubValues(bool newStyle) const |
| 3141 |
{ |
| 3142 |
assert(_lSubList.size() == _ShadowSubList.size()); |
| 3143 |
std::vector<std::string> ret; |
| 3144 |
ret.reserve(_ShadowSubList.size()); |
| 3145 |
std::string tmp; |
| 3146 |
for (size_t i = 0; i < _ShadowSubList.size(); ++i) { |
| 3147 |
ret.push_back(getSubNameWithStyle(_lSubList[i], _ShadowSubList[i], newStyle, tmp)); |
| 3148 |
} |
| 3149 |
return ret; |
| 3150 |
} |
| 3151 |
|
| 3152 |
void PropertyLinkSubList::getLinks(std::vector<App::DocumentObject*>& objs, |
| 3153 |
bool all, |
| 3154 |
std::vector<std::string>* subs, |
| 3155 |
bool newStyle) const |
| 3156 |
{ |
| 3157 |
if (all || _pcScope != LinkScope::Hidden) { |
| 3158 |
objs.reserve(objs.size() + _lValueList.size()); |
| 3159 |
for (auto obj : _lValueList) { |
| 3160 |
if (obj && obj->isAttachedToDocument()) { |
| 3161 |
// we use to run this method everytime the program needed to access the sub-elements in |
| 3162 |
// a property link, but it caused multiple issues (#23441 and #23402) so it has been |
| 3163 |
// commented out. |
| 3164 |
// updateElementReferences(obj); |
| 3165 |
objs.push_back(obj); |
| 3166 |
} |
| 3167 |
} |
| 3168 |
if (subs) { |
| 3169 |
auto _subs = getSubValues(newStyle); |
| 3170 |
subs->reserve(subs->size() + _subs.size()); |
| 3171 |
std::move(_subs.begin(), _subs.end(), std::back_inserter(*subs)); |
| 3172 |
} |
| 3173 |
} |
| 3174 |
} |
| 3175 |
|
| 3176 |
void PropertyLinkSubList::getLinksTo(std::vector<App::ObjectIdentifier>& identifiers, |
| 3177 |
App::DocumentObject* obj, |
| 3178 |
const char* subname, |
| 3179 |
bool all) const |
| 3180 |
{ |
| 3181 |
if (!obj || (!all && _pcScope == LinkScope::Hidden)) { |
| 3182 |
return; |
| 3183 |
} |
| 3184 |
App::SubObjectT objT(obj, subname); |
| 3185 |
auto subObject = objT.getSubObject(); |
| 3186 |
auto subElement = objT.getOldElementName(); |
| 3187 |
|
| 3188 |
int i = -1; |
| 3189 |
for (const auto& docObj : _lValueList) { |
| 3190 |
++i; |
| 3191 |
if (docObj != obj) { |
| 3192 |
continue; |
| 3193 |
} |
| 3194 |
// If we don't specify a subname we looking for all; or if the subname is in our |
| 3195 |
// property, add this entry to our result |
| 3196 |
if (!subname || (i < (int)_lSubList.size() && subname == _lSubList[i])) { |
| 3197 |
identifiers.emplace_back(*this, i); |
| 3198 |
continue; |
| 3199 |
} |
| 3200 |
// If we couldn't find any subobjects or this object's index is in our list, ignore it |
| 3201 |
if (!subObject || i < (int)_lSubList.size()) { |
| 3202 |
continue; |
| 3203 |
} |
| 3204 |
App::SubObjectT sobjT(obj, _lSubList[i].c_str()); |
| 3205 |
if (sobjT.getSubObject() == subObject && sobjT.getOldElementName() == subElement) { |
| 3206 |
identifiers.emplace_back(*this); |
| 3207 |
continue; |
| 3208 |
} |
| 3209 |
if (i < (int)_ShadowSubList.size()) { |
| 3210 |
const auto& shadow = _ShadowSubList[i]; |
| 3211 |
App::SubObjectT sobjT(obj, |
| 3212 |
shadow.newName.empty() ? shadow.oldName.c_str() |
| 3213 |
: shadow.newName.c_str()); |
| 3214 |
if (sobjT.getSubObject() == subObject && sobjT.getOldElementName() == subElement) { |
| 3215 |
identifiers.emplace_back(*this); |
| 3216 |
continue; |
| 3217 |
} |
| 3218 |
} |
| 3219 |
} |
| 3220 |
} |
| 3221 |
|
| 3222 |
void PropertyLinkSubList::breakLink(App::DocumentObject* obj, bool clear) |
| 3223 |
{ |
| 3224 |
std::vector<DocumentObject*> values; |
| 3225 |
std::vector<std::string> subs; |
| 3226 |
|
| 3227 |
if (clear && getContainer() == obj) { |
| 3228 |
setValues(values, subs); |
| 3229 |
return; |
| 3230 |
} |
| 3231 |
assert(_lValueList.size() == _lSubList.size()); |
| 3232 |
|
| 3233 |
values.reserve(_lValueList.size()); |
| 3234 |
subs.reserve(_lSubList.size()); |
| 3235 |
|
| 3236 |
int i = -1; |
| 3237 |
for (auto o : _lValueList) { |
| 3238 |
++i; |
| 3239 |
if (o == obj) { |
| 3240 |
continue; |
| 3241 |
} |
| 3242 |
values.push_back(o); |
| 3243 |
subs.push_back(_lSubList[i]); |
| 3244 |
} |
| 3245 |
if (values.size() != _lValueList.size()) { |
| 3246 |
setValues(values, subs); |
| 3247 |
} |
| 3248 |
} |
| 3249 |
|
| 3250 |
bool PropertyLinkSubList::adjustLink(const std::set<App::DocumentObject*>& inList) |
| 3251 |
{ |
| 3252 |
if (_pcScope == LinkScope::Hidden) { |
| 3253 |
return false; |
| 3254 |
} |
| 3255 |
auto subs = _lSubList; |
| 3256 |
auto links = _lValueList; |
| 3257 |
int idx = -1; |
| 3258 |
bool touched = false; |
| 3259 |
for (std::string& sub : subs) { |
| 3260 |
++idx; |
| 3261 |
auto& link = links[idx]; |
| 3262 |
if (!link || !link->isAttachedToDocument() || !inList.contains(link)) { |
| 3263 |
continue; |
| 3264 |
} |
| 3265 |
touched = true; |
| 3266 |
size_t pos = sub.find('.'); |
| 3267 |
for (; pos != std::string::npos; pos = sub.find('.', pos + 1)) { |
| 3268 |
auto sobj = link->getSubObject(sub.substr(0, pos + 1).c_str()); |
| 3269 |
if (!sobj || sobj->getDocument() != link->getDocument()) { |
| 3270 |
pos = std::string::npos; |
| 3271 |
break; |
| 3272 |
} |
| 3273 |
if (!inList.contains(sobj)) { |
| 3274 |
link = sobj; |
| 3275 |
sub = sub.substr(pos + 1); |
| 3276 |
break; |
| 3277 |
} |
| 3278 |
} |
| 3279 |
if (pos == std::string::npos) { |
| 3280 |
return false; |
| 3281 |
} |
| 3282 |
} |
| 3283 |
if (touched) { |
| 3284 |
setValues(links, subs); |
| 3285 |
} |
| 3286 |
return touched; |
| 3287 |
} |
| 3288 |
|
| 3289 |
//************************************************************************** |
| 3290 |
// DocInfo |
| 3291 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 3292 |
|
| 3293 |
// Key on absolute path. |
| 3294 |
// Because of possible symbolic links, multiple entry may refer to the same |
| 3295 |
// file. We used to rely on QFileInfo::canonicalFilePath to resolve it, but |
| 3296 |
// has now been changed to simply use the absoluteFilePath(), and rely on user |
| 3297 |
// to be aware of possible duplicated file location. The reason being that |
| 3298 |
// some user (especially Linux user) use symlink to organize file tree. |
| 3299 |
using DocInfoMap = std::map<QString, DocInfoPtr>; |
| 3300 |
DocInfoMap _DocInfoMap; |
| 3301 |
|
| 3302 |
class App::DocInfo: public std::enable_shared_from_this<App::DocInfo> |
| 3303 |
{ |
| 3304 |
public: |
| 3305 |
using Connection = boost::signals2::scoped_connection; |
| 3306 |
Connection connFinishRestoreDocument; |
| 3307 |
Connection connPendingReloadDocument; |
| 3308 |
Connection connDeleteDocument; |
| 3309 |
Connection connSaveDocument; |
| 3310 |
Connection connDeletedObject; |
| 3311 |
|
| 3312 |
DocInfoMap::iterator myPos; |
| 3313 |
std::string myPath; |
| 3314 |
App::Document* pcDoc {nullptr}; |
| 3315 |
std::set<PropertyXLink*> links; |
| 3316 |
|
| 3317 |
static std::string getDocPath(const char* filename, |
| 3318 |
App::Document* pDoc, |
| 3319 |
bool relative, |
| 3320 |
QString* fullPath = nullptr) |
| 3321 |
{ |
| 3322 |
bool absolute; |
| 3323 |
// The path could be an URI, in that case |
| 3324 |
// TODO: build a far much more resilient approach to test for an URI |
| 3325 |
QString path = QString::fromUtf8(filename); |
| 3326 |
if (path.startsWith(QLatin1String("https://"))) { |
| 3327 |
// We do have an URI |
| 3328 |
if (fullPath) { |
| 3329 |
*fullPath = path; |
| 3330 |
} |
| 3331 |
return std::string(filename); |
| 3332 |
} |
| 3333 |
|
| 3334 |
// make sure the filename is absolute path |
| 3335 |
path = QDir::cleanPath(path); |
| 3336 |
if ((absolute = QFileInfo(path).isAbsolute())) { |
| 3337 |
if (fullPath) { |
| 3338 |
*fullPath = path; |
| 3339 |
} |
| 3340 |
if (!relative) { |
| 3341 |
return std::string(path.toUtf8().constData()); |
| 3342 |
} |
| 3343 |
} |
| 3344 |
|
| 3345 |
const char* docPath = pDoc->getFileName(); |
| 3346 |
if (!docPath || *docPath == 0) { |
| 3347 |
throw Base::RuntimeError("Owner document not saved"); |
| 3348 |
} |
| 3349 |
|
| 3350 |
QFileInfo docFileInfo{QString::fromUtf8(docPath)}; |
| 3351 |
QDir docDir(docFileInfo.canonicalPath()); |
| 3352 |
if (!absolute) { |
| 3353 |
path = QDir::cleanPath(docDir.absoluteFilePath(path)); |
| 3354 |
if (fullPath) { |
| 3355 |
*fullPath = path; |
| 3356 |
} |
| 3357 |
} |
| 3358 |
|
| 3359 |
if (relative) { |
| 3360 |
return std::string(docDir.relativeFilePath(path).toUtf8().constData()); |
| 3361 |
} |
| 3362 |
else { |
| 3363 |
return std::string(path.toUtf8().constData()); |
| 3364 |
} |
| 3365 |
} |
| 3366 |
|
| 3367 |
static DocInfoPtr |
| 3368 |
get(const char* filename, App::Document* pDoc, PropertyXLink* l, const char* objName) |
| 3369 |
{ |
| 3370 |
QString path; |
| 3371 |
l->filePath = getDocPath(filename, pDoc, true, &path); |
| 3372 |
|
| 3373 |
FC_LOG("finding doc " << filename); |
| 3374 |
|
| 3375 |
auto it = _DocInfoMap.find(path); |
| 3376 |
DocInfoPtr info; |
| 3377 |
if (it != _DocInfoMap.end()) { |
| 3378 |
info = it->second; |
| 3379 |
if (!info->pcDoc) { |
| 3380 |
QString fullpath(info->getFullPath()); |
| 3381 |
if (fullpath.size() |
| 3382 |
&& App::GetApplication().addPendingDocument( |
| 3383 |
fullpath.toUtf8().constData(), |
| 3384 |
objName, |
| 3385 |
l->testFlag(PropertyLinkBase::LinkAllowPartial)) |
| 3386 |
== 0) { |
| 3387 |
for (App::Document* doc : App::GetApplication().getDocuments()) { |
| 3388 |
if (getFullPath(doc->getFileName()) == fullpath) { |
| 3389 |
info->attach(doc); |
| 3390 |
break; |
| 3391 |
} |
| 3392 |
} |
| 3393 |
} |
| 3394 |
} |
| 3395 |
} |
| 3396 |
else { |
| 3397 |
info = std::make_shared<DocInfo>(); |
| 3398 |
auto ret = _DocInfoMap.insert(std::make_pair(path, info)); |
| 3399 |
info->init(ret.first, objName, l); |
| 3400 |
} |
| 3401 |
|
| 3402 |
if (info->pcDoc) { |
| 3403 |
// make sure to attach only external object |
| 3404 |
auto owner = freecad_cast<DocumentObject*>(l->getContainer()); |
| 3405 |
if (owner && owner->getDocument() == info->pcDoc) { |
| 3406 |
return info; |
| 3407 |
} |
| 3408 |
} |
| 3409 |
|
| 3410 |
info->links.insert(l); |
| 3411 |
return info; |
| 3412 |
} |
| 3413 |
|
| 3414 |
static QString getFullPath(const char* p) |
| 3415 |
{ |
| 3416 |
QString path = QString::fromUtf8(p); |
| 3417 |
if (path.isEmpty()) { |
| 3418 |
return path; |
| 3419 |
} |
| 3420 |
|
| 3421 |
if (path.startsWith(QLatin1String("https://"))) { |
| 3422 |
return path; |
| 3423 |
} |
| 3424 |
else { |
| 3425 |
return QFileInfo(path).absoluteFilePath(); |
| 3426 |
} |
| 3427 |
} |
| 3428 |
|
| 3429 |
QString getFullPath() const |
| 3430 |
{ |
| 3431 |
QString path = myPos->first; |
| 3432 |
if (path.startsWith(QLatin1String("https://"))) { |
| 3433 |
return path; |
| 3434 |
} |
| 3435 |
else { |
| 3436 |
return QFileInfo(myPos->first).absoluteFilePath(); |
| 3437 |
} |
| 3438 |
} |
| 3439 |
|
| 3440 |
const char* filePath() const |
| 3441 |
{ |
| 3442 |
return myPath.c_str(); |
| 3443 |
} |
| 3444 |
|
| 3445 |
void deinit() |
| 3446 |
{ |
| 3447 |
FC_LOG("deinit " << (pcDoc ? pcDoc->getName() : filePath())); |
| 3448 |
assert(links.empty()); |
| 3449 |
connFinishRestoreDocument.disconnect(); |
| 3450 |
connPendingReloadDocument.disconnect(); |
| 3451 |
connDeleteDocument.disconnect(); |
| 3452 |
connSaveDocument.disconnect(); |
| 3453 |
connDeletedObject.disconnect(); |
| 3454 |
|
| 3455 |
auto me = shared_from_this(); |
| 3456 |
_DocInfoMap.erase(myPos); |
| 3457 |
myPos = _DocInfoMap.end(); |
| 3458 |
myPath.clear(); |
| 3459 |
pcDoc = nullptr; |
| 3460 |
} |
| 3461 |
|
| 3462 |
void init(DocInfoMap::iterator pos, const char* objName, PropertyXLink* l) |
| 3463 |
{ |
| 3464 |
myPos = pos; |
| 3465 |
myPath = myPos->first.toUtf8().constData(); |
| 3466 |
App::Application& app = App::GetApplication(); |
| 3467 |
// NOLINTBEGIN |
| 3468 |
connFinishRestoreDocument = app.signalFinishRestoreDocument.connect( |
| 3469 |
std::bind(&DocInfo::slotFinishRestoreDocument, this, sp::_1)); |
| 3470 |
connPendingReloadDocument = app.signalPendingReloadDocument.connect( |
| 3471 |
std::bind(&DocInfo::slotFinishRestoreDocument, this, sp::_1)); |
| 3472 |
connDeleteDocument = |
| 3473 |
app.signalDeleteDocument.connect(std::bind(&DocInfo::slotDeleteDocument, this, sp::_1)); |
| 3474 |
connSaveDocument = |
| 3475 |
app.signalSaveDocument.connect(std::bind(&DocInfo::slotSaveDocument, this, sp::_1)); |
| 3476 |
// NOLINTEND |
| 3477 |
|
| 3478 |
QString fullpath(getFullPath()); |
| 3479 |
if (fullpath.isEmpty()) { |
| 3480 |
FC_ERR("document not found " << filePath()); |
| 3481 |
} |
| 3482 |
else { |
| 3483 |
for (App::Document* doc : App::GetApplication().getDocuments()) { |
| 3484 |
if (getFullPath(doc->getFileName()) == fullpath) { |
| 3485 |
if (doc->testStatus(App::Document::PartialDoc) && !doc->getObject(objName)) { |
| 3486 |
break; |
| 3487 |
} |
| 3488 |
attach(doc); |
| 3489 |
return; |
| 3490 |
} |
| 3491 |
} |
| 3492 |
FC_LOG("document pending " << filePath()); |
| 3493 |
app.addPendingDocument(fullpath.toUtf8().constData(), |
| 3494 |
objName, |
| 3495 |
l->testFlag(PropertyLinkBase::LinkAllowPartial)); |
| 3496 |
} |
| 3497 |
} |
| 3498 |
|
| 3499 |
void attach(Document* doc) |
| 3500 |
{ |
| 3501 |
assert(!pcDoc); |
| 3502 |
pcDoc = doc; |
| 3503 |
FC_LOG("attaching " << doc->getName() << ", " << doc->getFileName()); |
| 3504 |
std::map<App::PropertyLinkBase*, std::vector<App::PropertyXLink*>> parentLinks; |
| 3505 |
for (auto it = links.begin(), itNext = it; it != links.end(); it = itNext) { |
| 3506 |
++itNext; |
| 3507 |
auto link = *it; |
| 3508 |
if (link->_pcLink) { |
| 3509 |
continue; |
| 3510 |
} |
| 3511 |
if (link->parentProp) { |
| 3512 |
parentLinks[link->parentProp].push_back(link); |
| 3513 |
continue; |
| 3514 |
} |
| 3515 |
auto obj = doc->getObject(link->objectName.c_str()); |
| 3516 |
if (obj) { |
| 3517 |
link->restoreLink(obj); |
| 3518 |
} |
| 3519 |
else if (doc->testStatus(App::Document::PartialDoc)) { |
| 3520 |
App::GetApplication().addPendingDocument(doc->FileName.getValue(), |
| 3521 |
link->objectName.c_str(), |
| 3522 |
false); |
| 3523 |
FC_WARN("reloading partial document '" << doc->FileName.getValue() |
| 3524 |
<< "' due to object " << link->objectName); |
| 3525 |
} |
| 3526 |
else { |
| 3527 |
FC_WARN("object '" << link->objectName << "' not found in document '" |
| 3528 |
<< doc->getName() << "'"); |
| 3529 |
} |
| 3530 |
} |
| 3531 |
for (auto& v : parentLinks) { |
| 3532 |
v.first->setFlag(PropertyLinkBase::LinkRestoring); |
| 3533 |
v.first->aboutToSetValue(); |
| 3534 |
for (auto link : v.second) { |
| 3535 |
auto obj = doc->getObject(link->objectName.c_str()); |
| 3536 |
if (obj) { |
| 3537 |
link->restoreLink(obj); |
| 3538 |
} |
| 3539 |
else if (doc->testStatus(App::Document::PartialDoc)) { |
| 3540 |
App::GetApplication().addPendingDocument(doc->FileName.getValue(), |
| 3541 |
link->objectName.c_str(), |
| 3542 |
false); |
| 3543 |
FC_WARN("reloading partial document '" |
| 3544 |
<< doc->FileName.getValue() << "' due to object " << link->objectName); |
| 3545 |
} |
| 3546 |
else { |
| 3547 |
FC_WARN("object '" << link->objectName << "' not found in document '" |
| 3548 |
<< doc->getName() << "'"); |
| 3549 |
} |
| 3550 |
} |
| 3551 |
v.first->hasSetValue(); |
| 3552 |
v.first->setFlag(PropertyLinkBase::LinkRestoring, false); |
| 3553 |
} |
| 3554 |
} |
| 3555 |
|
| 3556 |
void remove(PropertyXLink* l) |
| 3557 |
{ |
| 3558 |
auto it = links.find(l); |
| 3559 |
if (it != links.end()) { |
| 3560 |
links.erase(it); |
| 3561 |
if (links.empty()) { |
| 3562 |
deinit(); |
| 3563 |
} |
| 3564 |
} |
| 3565 |
} |
| 3566 |
|
| 3567 |
static void restoreDocument(const App::Document& doc) |
| 3568 |
{ |
| 3569 |
auto it = _DocInfoMap.find(getFullPath(doc.FileName.getValue())); |
| 3570 |
if (it == _DocInfoMap.end()) { |
| 3571 |
return; |
| 3572 |
} |
| 3573 |
it->second->slotFinishRestoreDocument(doc); |
| 3574 |
} |
| 3575 |
|
| 3576 |
void slotFinishRestoreDocument(const App::Document& doc) |
| 3577 |
{ |
| 3578 |
if (pcDoc) { |
| 3579 |
return; |
| 3580 |
} |
| 3581 |
QString fullpath(getFullPath()); |
| 3582 |
if (!fullpath.isEmpty() && getFullPath(doc.getFileName()) == fullpath) { |
| 3583 |
attach(const_cast<App::Document*>(&doc)); |
| 3584 |
} |
| 3585 |
} |
| 3586 |
|
| 3587 |
void slotSaveDocument(const App::Document& doc) |
| 3588 |
{ |
| 3589 |
if (!pcDoc) { |
| 3590 |
slotFinishRestoreDocument(doc); |
| 3591 |
return; |
| 3592 |
} |
| 3593 |
if (&doc != pcDoc) { |
| 3594 |
return; |
| 3595 |
} |
| 3596 |
|
| 3597 |
QFileInfo info(myPos->first); |
| 3598 |
QString path(info.absoluteFilePath()); |
| 3599 |
const char* filename = doc.getFileName(); |
| 3600 |
QString docPath(getFullPath(filename)); |
| 3601 |
|
| 3602 |
if (path.isEmpty() || path != docPath) { |
| 3603 |
FC_LOG("document '" << doc.getName() << "' path changed"); |
| 3604 |
auto me = shared_from_this(); |
| 3605 |
auto ret = _DocInfoMap.insert(std::make_pair(docPath, me)); |
| 3606 |
if (!ret.second) { |
| 3607 |
// is that even possible? |
| 3608 |
FC_WARN("document '" << doc.getName() << "' path exists, detach"); |
| 3609 |
slotDeleteDocument(doc); |
| 3610 |
return; |
| 3611 |
} |
| 3612 |
_DocInfoMap.erase(myPos); |
| 3613 |
myPos = ret.first; |
| 3614 |
|
| 3615 |
std::set<PropertyXLink*> tmp; |
| 3616 |
tmp.swap(links); |
| 3617 |
for (auto link : tmp) { |
| 3618 |
auto owner = static_cast<DocumentObject*>(link->getContainer()); |
| 3619 |
// adjust file path for each PropertyXLink |
| 3620 |
DocInfo::get(filename, owner->getDocument(), link, link->objectName.c_str()); |
| 3621 |
} |
| 3622 |
} |
| 3623 |
|
| 3624 |
// time stamp changed, touch the linking document. |
| 3625 |
std::set<Document*> docs; |
| 3626 |
for (auto link : links) { |
| 3627 |
auto linkdoc = static_cast<DocumentObject*>(link->getContainer())->getDocument(); |
| 3628 |
auto ret = docs.insert(linkdoc); |
| 3629 |
if (ret.second) { |
| 3630 |
// This will signal the Gui::Document to call setModified(); |
| 3631 |
FC_LOG("touch document " << linkdoc->getName() << " on time stamp change of " |
| 3632 |
<< link->getFullName()); |
| 3633 |
linkdoc->Comment.touch(); |
| 3634 |
} |
| 3635 |
} |
| 3636 |
} |
| 3637 |
|
| 3638 |
void slotDeleteDocument(const App::Document& doc) |
| 3639 |
{ |
| 3640 |
for (auto it = links.begin(), itNext = it; it != links.end(); it = itNext) { |
| 3641 |
++itNext; |
| 3642 |
auto link = *it; |
| 3643 |
auto obj = freecad_cast<DocumentObject*>(link->getContainer()); |
| 3644 |
if (obj && obj->getDocument() == &doc) { |
| 3645 |
links.erase(it); |
| 3646 |
// must call unlink here, so that PropertyLink::resetLink can |
| 3647 |
// remove back link before the owner object is marked as being |
| 3648 |
// destroyed |
| 3649 |
link->unlink(); |
| 3650 |
} |
| 3651 |
} |
| 3652 |
if (links.empty()) { |
| 3653 |
deinit(); |
| 3654 |
return; |
| 3655 |
} |
| 3656 |
if (pcDoc != &doc) { |
| 3657 |
return; |
| 3658 |
} |
| 3659 |
std::map<App::PropertyLinkBase*, std::vector<App::PropertyXLink*>> parentLinks; |
| 3660 |
for (auto link : links) { |
| 3661 |
link->setFlag(PropertyLinkBase::LinkDetached); |
| 3662 |
if (link->parentProp) { |
| 3663 |
parentLinks[link->parentProp].push_back(link); |
| 3664 |
} |
| 3665 |
else { |
| 3666 |
parentLinks[nullptr].push_back(link); |
| 3667 |
} |
| 3668 |
} |
| 3669 |
for (auto& v : parentLinks) { |
| 3670 |
if (v.first) { |
| 3671 |
v.first->setFlag(PropertyLinkBase::LinkDetached); |
| 3672 |
v.first->aboutToSetValue(); |
| 3673 |
} |
| 3674 |
for (auto l : v.second) { |
| 3675 |
l->detach(); |
| 3676 |
} |
| 3677 |
if (v.first) { |
| 3678 |
v.first->hasSetValue(); |
| 3679 |
v.first->setFlag(PropertyLinkBase::LinkDetached, false); |
| 3680 |
} |
| 3681 |
} |
| 3682 |
pcDoc = nullptr; |
| 3683 |
} |
| 3684 |
|
| 3685 |
bool hasXLink(const App::Document* doc) const |
| 3686 |
{ |
| 3687 |
for (auto link : links) { |
| 3688 |
auto obj = freecad_cast<DocumentObject*>(link->getContainer()); |
| 3689 |
if (obj && obj->getDocument() == doc) { |
| 3690 |
return true; |
| 3691 |
} |
| 3692 |
} |
| 3693 |
return false; |
| 3694 |
} |
| 3695 |
|
| 3696 |
static void breakLinks(App::DocumentObject* obj, bool clear) |
| 3697 |
{ |
| 3698 |
auto doc = obj->getDocument(); |
| 3699 |
for (auto itD = _DocInfoMap.begin(), itDNext = itD; itD != _DocInfoMap.end(); |
| 3700 |
itD = itDNext) { |
| 3701 |
++itDNext; |
| 3702 |
auto docInfo = itD->second; |
| 3703 |
if (docInfo->pcDoc != doc) { |
| 3704 |
continue; |
| 3705 |
} |
| 3706 |
auto& links = docInfo->links; |
| 3707 |
std::set<PropertyLinkBase*> parentLinks; |
| 3708 |
for (auto it = links.begin(), itNext = it; it != links.end(); it = itNext) { |
| 3709 |
++itNext; |
| 3710 |
auto link = *it; |
| 3711 |
if (link->_pcLink != obj && !(clear && link->getContainer() == obj)) { |
| 3712 |
continue; |
| 3713 |
} |
| 3714 |
if (link->parentProp) { |
| 3715 |
parentLinks.insert(link->parentProp); |
| 3716 |
} |
| 3717 |
else { |
| 3718 |
link->breakLink(obj, clear); |
| 3719 |
} |
| 3720 |
} |
| 3721 |
for (auto link : parentLinks) { |
| 3722 |
link->breakLink(obj, clear); |
| 3723 |
} |
| 3724 |
} |
| 3725 |
} |
| 3726 |
}; |
| 3727 |
|
| 3728 |
void PropertyLinkBase::breakLinks(App::DocumentObject* link, |
| 3729 |
const std::vector<App::DocumentObject*>& objs, |
| 3730 |
bool clear) |
| 3731 |
{ |
| 3732 |
std::vector<Property*> props; |
| 3733 |
for (auto obj : objs) { |
| 3734 |
props.clear(); |
| 3735 |
obj->getPropertyList(props); |
| 3736 |
for (auto prop : props) { |
| 3737 |
auto linkProp = freecad_cast<PropertyLinkBase*>(prop); |
| 3738 |
if (linkProp) { |
| 3739 |
linkProp->breakLink(link, clear); |
| 3740 |
} |
| 3741 |
} |
| 3742 |
} |
| 3743 |
DocInfo::breakLinks(link, clear); |
| 3744 |
} |
| 3745 |
|
| 3746 |
//************************************************************************** |
| 3747 |
// PropertyXLink |
| 3748 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 3749 |
|
| 3750 |
TYPESYSTEM_SOURCE(App::PropertyXLink, App::PropertyLink) |
| 3751 |
|
| 3752 |
PropertyXLink::PropertyXLink(bool _allowPartial, PropertyLinkBase* parent) |
| 3753 |
: parentProp(parent) |
| 3754 |
{ |
| 3755 |
setAllowPartial(_allowPartial); |
| 3756 |
setAllowExternal(true); |
| 3757 |
setSyncSubObject(true); |
| 3758 |
if (parent) { |
| 3759 |
setContainer(parent->getContainer()); |
| 3760 |
} |
| 3761 |
} |
| 3762 |
|
| 3763 |
PropertyXLink::~PropertyXLink() |
| 3764 |
{ |
| 3765 |
try { |
| 3766 |
unlink(); |
| 3767 |
} catch (std::bad_weak_ptr &) { |
| 3768 |
FC_WARN("Bad pointer exception caught when destroying PropertyXLink\n"); |
| 3769 |
} |
| 3770 |
} |
| 3771 |
|
| 3772 |
void PropertyXLink::setSyncSubObject(bool enable) |
| 3773 |
{ |
| 3774 |
_Flags.set((std::size_t)LinkSyncSubObject, enable); |
| 3775 |
} |
| 3776 |
|
| 3777 |
void PropertyXLink::unlink() |
| 3778 |
{ |
| 3779 |
if (docInfo) { |
| 3780 |
docInfo->remove(this); |
| 3781 |
docInfo.reset(); |
| 3782 |
} |
| 3783 |
objectName.clear(); |
| 3784 |
resetLink(); |
| 3785 |
} |
| 3786 |
|
| 3787 |
void PropertyXLink::detach() |
| 3788 |
{ |
| 3789 |
if (docInfo && _pcLink) { |
| 3790 |
aboutToSetValue(); |
| 3791 |
resetLink(); |
| 3792 |
updateElementReference(nullptr); |
| 3793 |
hasSetValue(); |
| 3794 |
} |
| 3795 |
} |
| 3796 |
|
| 3797 |
void PropertyXLink::aboutToSetValue() |
| 3798 |
{ |
| 3799 |
if (parentProp) { |
| 3800 |
parentProp->aboutToSetChildValue(*this); |
| 3801 |
} |
| 3802 |
else { |
| 3803 |
PropertyLinkBase::aboutToSetValue(); |
| 3804 |
} |
| 3805 |
} |
| 3806 |
|
| 3807 |
void PropertyXLink::hasSetValue() |
| 3808 |
{ |
| 3809 |
if (parentProp) { |
| 3810 |
parentProp->hasSetChildValue(*this); |
| 3811 |
} |
| 3812 |
else { |
| 3813 |
PropertyLinkBase::hasSetValue(); |
| 3814 |
} |
| 3815 |
} |
| 3816 |
|
| 3817 |
void PropertyXLink::setSubName(const char* subname) |
| 3818 |
{ |
| 3819 |
std::vector<std::string> subs; |
| 3820 |
if (!Base::Tools::isNullOrEmpty(subname)) { |
| 3821 |
subs.emplace_back(subname); |
| 3822 |
} |
| 3823 |
aboutToSetValue(); |
| 3824 |
setSubValues(std::move(subs)); |
| 3825 |
hasSetValue(); |
| 3826 |
} |
| 3827 |
|
| 3828 |
void PropertyXLink::setSubValues(std::vector<std::string>&& subs, std::vector<ShadowSub>&& shadows) |
| 3829 |
{ |
| 3830 |
_SubList = std::move(subs); |
| 3831 |
_ShadowSubList.clear(); |
| 3832 |
if (shadows.size() == _SubList.size()) { |
| 3833 |
_ShadowSubList = std::move(shadows); |
| 3834 |
onContainerRestored(); // re-register element references |
| 3835 |
} |
| 3836 |
else { |
| 3837 |
updateElementReference(nullptr); |
| 3838 |
} |
| 3839 |
checkLabelReferences(_SubList); |
| 3840 |
} |
| 3841 |
|
| 3842 |
void PropertyXLink::setValue(App::DocumentObject* lValue) |
| 3843 |
{ |
| 3844 |
setValue(lValue, nullptr); |
| 3845 |
} |
| 3846 |
|
| 3847 |
void PropertyXLink::setValue(App::DocumentObject* lValue, const char* subname) |
| 3848 |
{ |
| 3849 |
std::vector<std::string> subs; |
| 3850 |
if (!Base::Tools::isNullOrEmpty(subname)) { |
| 3851 |
subs.emplace_back(subname); |
| 3852 |
} |
| 3853 |
setValue(lValue, std::move(subs)); |
| 3854 |
} |
| 3855 |
|
| 3856 |
void PropertyXLink::restoreLink(App::DocumentObject* lValue) |
| 3857 |
{ |
| 3858 |
assert(!_pcLink && lValue && docInfo); |
| 3859 |
|
| 3860 |
auto owner = freecad_cast<DocumentObject*>(getContainer()); |
| 3861 |
if (!owner || !owner->isAttachedToDocument()) { |
| 3862 |
throw Base::RuntimeError("invalid container"); |
| 3863 |
} |
| 3864 |
|
| 3865 |
bool touched = owner->isTouched(); |
| 3866 |
setFlag(LinkDetached, false); |
| 3867 |
setFlag(LinkRestoring); |
| 3868 |
aboutToSetValue(); |
| 3869 |
|
| 3870 |
if (!owner->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 3871 |
lValue->_addBackLink(owner); |
| 3872 |
} |
| 3873 |
|
| 3874 |
_pcLink = lValue; |
| 3875 |
updateElementReference(nullptr); |
| 3876 |
hasSetValue(); |
| 3877 |
setFlag(LinkRestoring, false); |
| 3878 |
|
| 3879 |
if (!touched && owner->isTouched() && docInfo && docInfo->pcDoc |
| 3880 |
&& stamp == docInfo->pcDoc->LastModifiedDate.getValue()) { |
| 3881 |
owner->purgeTouched(); |
| 3882 |
} |
| 3883 |
} |
| 3884 |
|
| 3885 |
void PropertyXLink::setValue(App::DocumentObject* lValue, |
| 3886 |
std::vector<std::string>&& subs, |
| 3887 |
std::vector<ShadowSub>&& shadows) |
| 3888 |
{ |
| 3889 |
if (_pcLink == lValue && _SubList == subs) { |
| 3890 |
return; |
| 3891 |
} |
| 3892 |
|
| 3893 |
if (lValue && (!lValue->isAttachedToDocument() || !lValue->getDocument())) { |
| 3894 |
throw Base::ValueError("Invalid object"); |
| 3895 |
} |
| 3896 |
|
| 3897 |
auto owner = freecad_cast<DocumentObject*>(getContainer()); |
| 3898 |
if (!owner || !owner->isAttachedToDocument()) { |
| 3899 |
throw Base::RuntimeError("invalid container"); |
| 3900 |
} |
| 3901 |
|
| 3902 |
if (lValue == owner) { |
| 3903 |
throw Base::ValueError("self linking"); |
| 3904 |
} |
| 3905 |
|
| 3906 |
aboutToSetValue(); |
| 3907 |
|
| 3908 |
DocInfoPtr info; |
| 3909 |
const char* name = ""; |
| 3910 |
if (lValue) { |
| 3911 |
name = lValue->getNameInDocument(); |
| 3912 |
if (lValue->getDocument() != owner->getDocument()) { |
| 3913 |
if (!docInfo || lValue->getDocument() != docInfo->pcDoc) { |
| 3914 |
const char* filename = lValue->getDocument()->getFileName(); |
| 3915 |
if (!filename || *filename == 0) { |
| 3916 |
throw Base::RuntimeError("Linked document not saved"); |
| 3917 |
} |
| 3918 |
FC_LOG("xlink set to new document " << lValue->getDocument()->getName()); |
| 3919 |
info = DocInfo::get(filename, owner->getDocument(), this, name); |
| 3920 |
assert(info && info->pcDoc == lValue->getDocument()); |
| 3921 |
} |
| 3922 |
else { |
| 3923 |
info = docInfo; |
| 3924 |
} |
| 3925 |
} |
| 3926 |
} |
| 3927 |
|
| 3928 |
setFlag(LinkDetached, false); |
| 3929 |
|
| 3930 |
if (!owner->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 3931 |
if (_pcLink) { |
| 3932 |
_pcLink->_removeBackLink(owner); |
| 3933 |
} |
| 3934 |
if (lValue) { |
| 3935 |
lValue->_addBackLink(owner); |
| 3936 |
} |
| 3937 |
} |
| 3938 |
|
| 3939 |
if (docInfo != info) { |
| 3940 |
unlink(); |
| 3941 |
docInfo = info; |
| 3942 |
} |
| 3943 |
if (!docInfo) { |
| 3944 |
filePath.clear(); |
| 3945 |
} |
| 3946 |
_pcLink = lValue; |
| 3947 |
if (docInfo && docInfo->pcDoc) { |
| 3948 |
stamp = docInfo->pcDoc->LastModifiedDate.getValue(); |
| 3949 |
} |
| 3950 |
objectName = name; |
| 3951 |
setSubValues(std::move(subs), std::move(shadows)); |
| 3952 |
hasSetValue(); |
| 3953 |
} |
| 3954 |
|
| 3955 |
void PropertyXLink::setValue(std::string&& filename, |
| 3956 |
std::string&& name, |
| 3957 |
std::vector<std::string>&& subs, |
| 3958 |
std::vector<ShadowSub>&& shadows) |
| 3959 |
{ |
| 3960 |
if (name.empty()) { |
| 3961 |
setValue(nullptr, std::move(subs), std::move(shadows)); |
| 3962 |
return; |
| 3963 |
} |
| 3964 |
auto owner = freecad_cast<DocumentObject*>(getContainer()); |
| 3965 |
if (!owner || !owner->isAttachedToDocument()) { |
| 3966 |
throw Base::RuntimeError("invalid container"); |
| 3967 |
} |
| 3968 |
|
| 3969 |
DocumentObject* pObject = nullptr; |
| 3970 |
DocInfoPtr info; |
| 3971 |
if (!filename.empty()) { |
| 3972 |
owner->getDocument()->signalLinkXsetValue(filename); |
| 3973 |
info = DocInfo::get(filename.c_str(), owner->getDocument(), this, name.c_str()); |
| 3974 |
if (info->pcDoc) { |
| 3975 |
pObject = info->pcDoc->getObject(name.c_str()); |
| 3976 |
} |
| 3977 |
} |
| 3978 |
else { |
| 3979 |
pObject = owner->getDocument()->getObject(name.c_str()); |
| 3980 |
} |
| 3981 |
|
| 3982 |
if (pObject) { |
| 3983 |
setValue(pObject, std::move(subs), std::move(shadows)); |
| 3984 |
return; |
| 3985 |
} |
| 3986 |
setFlag(LinkDetached, false); |
| 3987 |
aboutToSetValue(); |
| 3988 |
|
| 3989 |
if (_pcLink && !owner->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { |
| 3990 |
_pcLink->_removeBackLink(owner); |
| 3991 |
} |
| 3992 |
|
| 3993 |
_pcLink = nullptr; |
| 3994 |
if (docInfo != info) { |
| 3995 |
unlink(); |
| 3996 |
docInfo = info; |
| 3997 |
} |
| 3998 |
if (!docInfo) { |
| 3999 |
filePath.clear(); |
| 4000 |
} |
| 4001 |
if (docInfo && docInfo->pcDoc) { |
| 4002 |
stamp = docInfo->pcDoc->LastModifiedDate.getValue(); |
| 4003 |
} |
| 4004 |
objectName = std::move(name); |
| 4005 |
setSubValues(std::move(subs), std::move(shadows)); |
| 4006 |
hasSetValue(); |
| 4007 |
} |
| 4008 |
|
| 4009 |
void PropertyXLink::setValue(App::DocumentObject* link, |
| 4010 |
const std::vector<std::string>& subs, |
| 4011 |
std::vector<ShadowSub>&& shadows) |
| 4012 |
{ |
| 4013 |
setValue(link, std::vector<std::string>(subs), std::move(shadows)); |
| 4014 |
} |
| 4015 |
|
| 4016 |
App::Document* PropertyXLink::getDocument() const |
| 4017 |
{ |
| 4018 |
return docInfo ? docInfo->pcDoc : nullptr; |
| 4019 |
} |
| 4020 |
|
| 4021 |
const char* PropertyXLink::getDocumentPath() const |
| 4022 |
{ |
| 4023 |
return docInfo ? docInfo->filePath() : filePath.c_str(); |
| 4024 |
} |
| 4025 |
|
| 4026 |
const char* PropertyXLink::getObjectName() const |
| 4027 |
{ |
| 4028 |
return objectName.c_str(); |
| 4029 |
} |
| 4030 |
|
| 4031 |
bool PropertyXLink::upgrade(Base::XMLReader& reader, const char* typeName) |
| 4032 |
{ |
| 4033 |
if (strcmp(typeName, App::PropertyLinkGlobal::getClassTypeId().getName()) == 0 |
| 4034 |
|| strcmp(typeName, App::PropertyLink::getClassTypeId().getName()) == 0 |
| 4035 |
|| strcmp(typeName, App::PropertyLinkChild::getClassTypeId().getName()) == 0) { |
| 4036 |
PropertyLink::Restore(reader); |
| 4037 |
return true; |
| 4038 |
} |
| 4039 |
FC_ERR("Cannot upgrade from " << typeName); |
| 4040 |
return false; |
| 4041 |
} |
| 4042 |
|
| 4043 |
int PropertyXLink::checkRestore(std::string* msg) const |
| 4044 |
{ |
| 4045 |
if (!docInfo) { |
| 4046 |
if (!_pcLink && !objectName.empty()) { |
| 4047 |
// this condition means linked object not found |
| 4048 |
if (msg) { |
| 4049 |
std::ostringstream ss; |
| 4050 |
ss << "Link not restored" << std::endl; |
| 4051 |
ss << "Object: " << objectName; |
| 4052 |
if (!filePath.empty()) { |
| 4053 |
ss << std::endl << "File: " << filePath; |
| 4054 |
} |
| 4055 |
*msg = ss.str(); |
| 4056 |
} |
| 4057 |
return 2; |
| 4058 |
} |
| 4059 |
return 0; |
| 4060 |
} |
| 4061 |
if (!_pcLink) { |
| 4062 |
if (testFlag(LinkSilentRestore)) { |
| 4063 |
return 0; |
| 4064 |
} |
| 4065 |
if (testFlag(LinkAllowPartial) |
| 4066 |
&& (!docInfo->pcDoc || docInfo->pcDoc->testStatus(App::Document::PartialDoc))) { |
| 4067 |
return 0; |
| 4068 |
} |
| 4069 |
if (msg) { |
| 4070 |
std::ostringstream ss; |
| 4071 |
ss << "Link not restored" << std::endl; |
| 4072 |
ss << "Linked object: " << objectName; |
| 4073 |
if (docInfo->pcDoc) { |
| 4074 |
ss << std::endl << "Linked document: " << docInfo->pcDoc->Label.getValue(); |
| 4075 |
} |
| 4076 |
else if (!filePath.empty()) { |
| 4077 |
ss << std::endl << "Linked file: " << filePath; |
| 4078 |
} |
| 4079 |
*msg = ss.str(); |
| 4080 |
} |
| 4081 |
return 2; |
| 4082 |
} |
| 4083 |
if (!docInfo->pcDoc || stamp == docInfo->pcDoc->LastModifiedDate.getValue()) { |
| 4084 |
return 0; |
| 4085 |
} |
| 4086 |
|
| 4087 |
if (msg) { |
| 4088 |
std::ostringstream ss; |
| 4089 |
ss << "Time stamp changed on link " << _pcLink->getFullName(); |
| 4090 |
*msg = ss.str(); |
| 4091 |
} |
| 4092 |
return 1; |
| 4093 |
} |
| 4094 |
|
| 4095 |
void PropertyXLink::afterRestore() |
| 4096 |
{ |
| 4097 |
assert(_SubList.size() == _ShadowSubList.size()); |
| 4098 |
if (!testFlag(LinkRestoreLabel) || !_pcLink || !_pcLink->isAttachedToDocument()) { |
| 4099 |
return; |
| 4100 |
} |
| 4101 |
|
| 4102 |
setFlag(LinkRestoreLabel, false); |
| 4103 |
for (size_t i = 0; i < _SubList.size(); ++i) { |
| 4104 |
restoreLabelReference(_pcLink, _SubList[i], &_ShadowSubList[i]); |
| 4105 |
} |
| 4106 |
} |
| 4107 |
|
| 4108 |
void PropertyXLink::onContainerRestored() |
| 4109 |
{ |
| 4110 |
if (!_pcLink || !_pcLink->isAttachedToDocument()) { |
| 4111 |
return; |
| 4112 |
} |
| 4113 |
for (size_t i = 0; i < _SubList.size(); ++i) { |
| 4114 |
_registerElementReference(_pcLink, _SubList[i], _ShadowSubList[i]); |
| 4115 |
} |
| 4116 |
} |
| 4117 |
|
| 4118 |
void PropertyXLink::updateElementReference(DocumentObject* feature, bool reverse, bool notify) |
| 4119 |
{ |
| 4120 |
if (!updateLinkReference(this, |
| 4121 |
feature, |
| 4122 |
reverse, |
| 4123 |
notify, |
| 4124 |
_pcLink, |
| 4125 |
_SubList, |
| 4126 |
_mapped, |
| 4127 |
_ShadowSubList)) { |
| 4128 |
return; |
| 4129 |
} |
| 4130 |
if (notify) { |
| 4131 |
hasSetValue(); |
| 4132 |
} |
| 4133 |
} |
| 4134 |
|
| 4135 |
bool PropertyXLink::referenceChanged() const |
| 4136 |
{ |
| 4137 |
return !_mapped.empty(); |
| 4138 |
} |
| 4139 |
|
| 4140 |
void PropertyXLink::Save(Base::Writer& writer) const |
| 4141 |
{ |
| 4142 |
auto owner = dynamic_cast<const DocumentObject*>(getContainer()); |
| 4143 |
if (!owner || !owner->getDocument()) { |
| 4144 |
return; |
| 4145 |
} |
| 4146 |
|
| 4147 |
assert(_SubList.size() == _ShadowSubList.size()); |
| 4148 |
|
| 4149 |
auto exporting = owner->isExporting(); |
| 4150 |
if (_pcLink && exporting && _pcLink->isExporting()) { |
| 4151 |
// this means, we are exporting the owner and the linked object together. |
| 4152 |
// Lets save the export name |
| 4153 |
writer.Stream() << writer.ind() << "<XLink name=\"" << _pcLink->getExportName(); |
| 4154 |
} |
| 4155 |
else { |
| 4156 |
const char* path = filePath.c_str(); |
| 4157 |
std::string _path; |
| 4158 |
if (exporting) { |
| 4159 |
// Here means we are exporting the owner but not exporting the |
| 4160 |
// linked object. Try to use absolute file path for easy transition |
| 4161 |
// into document at different directory |
| 4162 |
if (docInfo) { |
| 4163 |
_path = docInfo->filePath(); |
| 4164 |
} |
| 4165 |
else { |
| 4166 |
auto pDoc = owner->getDocument(); |
| 4167 |
const char* docPath = pDoc->getFileName(); |
| 4168 |
if (!Base::Tools::isNullOrEmpty(docPath)) { |
| 4169 |
if (!filePath.empty()) { |
| 4170 |
_path = DocInfo::getDocPath(filePath.c_str(), pDoc, false); |
| 4171 |
} |
| 4172 |
else { |
| 4173 |
_path = docPath; |
| 4174 |
} |
| 4175 |
} |
| 4176 |
else { |
| 4177 |
FC_WARN("PropertyXLink export without saving the document"); |
| 4178 |
} |
| 4179 |
} |
| 4180 |
if (!_path.empty()) { |
| 4181 |
path = _path.c_str(); |
| 4182 |
} |
| 4183 |
} |
| 4184 |
writer.Stream() << writer.ind() << "<XLink file=\"" << encodeAttribute(path) |
| 4185 |
<< "\" stamp=\"" |
| 4186 |
<< (docInfo && docInfo->pcDoc ? docInfo->pcDoc->LastModifiedDate.getValue() |
| 4187 |
: "") |
| 4188 |
<< "\" name=\"" << objectName; |
| 4189 |
} |
| 4190 |
|
| 4191 |
if (testFlag(LinkAllowPartial)) { |
| 4192 |
writer.Stream() << "\" partial=\"1"; |
| 4193 |
} |
| 4194 |
|
| 4195 |
if (_SubList.empty()) { |
| 4196 |
writer.Stream() << "\"/>" << std::endl; |
| 4197 |
} |
| 4198 |
else if (_SubList.size() == 1) { |
| 4199 |
const auto& subName = _SubList[0]; |
| 4200 |
const auto& shadowSub = _ShadowSubList[0]; |
| 4201 |
const auto& sub = shadowSub.oldName.empty() ? subName : shadowSub.oldName; |
| 4202 |
if (exporting) { |
| 4203 |
std::string exportName; |
| 4204 |
writer.Stream() << "\" sub=\"" |
| 4205 |
<< encodeAttribute(exportSubName(exportName, _pcLink, sub.c_str())); |
| 4206 |
if (!shadowSub.oldName.empty() && shadowSub.newName == subName) { |
| 4207 |
writer.Stream() << "\" " ATTR_MAPPED "=\"1"; |
| 4208 |
} |
| 4209 |
} |
| 4210 |
else { |
| 4211 |
writer.Stream() << "\" sub=\"" << encodeAttribute(sub); |
| 4212 |
if (!sub.empty()) { |
| 4213 |
if (sub != subName) { |
| 4214 |
writer.Stream() << "\" " ATTR_SHADOWED "=\"" << encodeAttribute(subName); |
| 4215 |
} |
| 4216 |
else if (!shadowSub.newName.empty()) { |
| 4217 |
writer.Stream() |
| 4218 |
<< "\" " ATTR_SHADOW "=\"" << encodeAttribute(shadowSub.newName); |
| 4219 |
} |
| 4220 |
} |
| 4221 |
} |
| 4222 |
writer.Stream() << "\"/>" << std::endl; |
| 4223 |
} |
| 4224 |
else { |
| 4225 |
writer.Stream() << "\" count=\"" << _SubList.size() << "\">" << std::endl; |
| 4226 |
writer.incInd(); |
| 4227 |
for (unsigned int i = 0; i < _SubList.size(); i++) { |
| 4228 |
const auto& shadow = _ShadowSubList[i]; |
| 4229 |
// shadow.oldName stores the old style element name. For backward |
| 4230 |
// compatibility reason, we shall store the old name into attribute |
| 4231 |
// 'value' whenever possible. |
| 4232 |
const auto& sub = shadow.oldName.empty() ? _SubList[i] : shadow.oldName; |
| 4233 |
writer.Stream() << writer.ind() << "<Sub value=\""; |
| 4234 |
if (exporting) { |
| 4235 |
std::string exportName; |
| 4236 |
writer.Stream() << encodeAttribute(exportSubName(exportName, _pcLink, sub.c_str())); |
| 4237 |
if (!shadow.oldName.empty() && shadow.newName == _SubList[i]) { |
| 4238 |
writer.Stream() << "\" " ATTR_MAPPED "=\"1"; |
| 4239 |
} |
| 4240 |
} |
| 4241 |
else { |
| 4242 |
writer.Stream() << encodeAttribute(sub); |
| 4243 |
if (!_SubList[i].empty()) { |
| 4244 |
if (sub != _SubList[i]) { |
| 4245 |
writer.Stream() |
| 4246 |
<< "\" " ATTR_SHADOWED "=\"" << encodeAttribute(_SubList[i]); |
| 4247 |
} |
| 4248 |
else if (!shadow.newName.empty()) { |
| 4249 |
writer.Stream() |
| 4250 |
<< "\" " ATTR_SHADOW "=\"" << encodeAttribute(shadow.newName); |
| 4251 |
} |
| 4252 |
} |
| 4253 |
} |
| 4254 |
writer.Stream() << "\"/>" << endl; |
| 4255 |
} |
| 4256 |
writer.decInd(); |
| 4257 |
writer.Stream() << writer.ind() << "</XLink>" << endl; |
| 4258 |
} |
| 4259 |
} |
| 4260 |
|
| 4261 |
void PropertyXLink::Restore(Base::XMLReader& reader) |
| 4262 |
{ |
| 4263 |
// read my element |
| 4264 |
reader.readElement("XLink"); |
| 4265 |
std::string stampAttr, file; |
| 4266 |
if (reader.hasAttribute("stamp")) { |
| 4267 |
stampAttr = reader.getAttribute<const char*>("stamp"); |
| 4268 |
} |
| 4269 |
if (reader.hasAttribute("file")) { |
| 4270 |
file = reader.getAttribute<const char*>("file"); |
| 4271 |
} |
| 4272 |
|
| 4273 |
setFlag(LinkAllowPartial, |
| 4274 |
reader.hasAttribute("partial") && reader.getAttribute<bool>("partial")); |
| 4275 |
std::string name; |
| 4276 |
if (file.empty()) { |
| 4277 |
name = reader.getName(reader.getAttribute<const char*>("name")); |
| 4278 |
} |
| 4279 |
else { |
| 4280 |
name = reader.getAttribute<const char*>("name"); |
| 4281 |
} |
| 4282 |
|
| 4283 |
DocumentObject* object = nullptr; |
| 4284 |
if (!name.empty() && file.empty()) { |
| 4285 |
if (auto parent = freecad_cast<DocumentObject*>(getContainer())) { |
| 4286 |
Document* document = parent->getDocument(); |
| 4287 |
object = document ? document->getObject(name.c_str()) : nullptr; |
| 4288 |
if (!object && reader.isVerbose()) { |
| 4289 |
FC_WARN("Lost link to '" << name |
| 4290 |
<< "' while loading, maybe " |
| 4291 |
"an object was not loaded correctly"); |
| 4292 |
} |
| 4293 |
} |
| 4294 |
} |
| 4295 |
|
| 4296 |
std::vector<std::string> subs; |
| 4297 |
std::vector<ShadowSub> shadows; |
| 4298 |
std::vector<int> mapped; |
| 4299 |
bool restoreLabel = false; |
| 4300 |
if (reader.hasAttribute("sub")) { |
| 4301 |
if (reader.hasAttribute(ATTR_MAPPED)) { |
| 4302 |
mapped.push_back(0); |
| 4303 |
} |
| 4304 |
subs.emplace_back(); |
| 4305 |
auto& subname = subs.back(); |
| 4306 |
shadows.emplace_back(); |
| 4307 |
auto& shadow = shadows.back(); |
| 4308 |
shadow.oldName = importSubName(reader, reader.getAttribute<const char*>("sub"), restoreLabel); |
| 4309 |
if (reader.hasAttribute(ATTR_SHADOWED) && !IGNORE_SHADOW) { |
| 4310 |
subname = shadow.newName = |
| 4311 |
importSubName(reader, reader.getAttribute<const char*>(ATTR_SHADOWED), restoreLabel); |
| 4312 |
} |
| 4313 |
else { |
| 4314 |
subname = shadow.oldName; |
| 4315 |
if (reader.hasAttribute(ATTR_SHADOW) && !IGNORE_SHADOW) { |
| 4316 |
shadow.newName = |
| 4317 |
importSubName(reader, reader.getAttribute<const char*>(ATTR_SHADOW), restoreLabel); |
| 4318 |
} |
| 4319 |
} |
| 4320 |
} |
| 4321 |
else if (reader.hasAttribute("count")) { |
| 4322 |
int count = reader.getAttribute<long>("count"); |
| 4323 |
subs.resize(count); |
| 4324 |
shadows.resize(count); |
| 4325 |
for (int i = 0; i < count; i++) { |
| 4326 |
reader.readElement("Sub"); |
| 4327 |
shadows[i].oldName = importSubName(reader, reader.getAttribute<const char*>("value"), restoreLabel); |
| 4328 |
if (reader.hasAttribute(ATTR_SHADOWED) && !IGNORE_SHADOW) { |
| 4329 |
subs[i] = shadows[i].newName = |
| 4330 |
importSubName(reader, reader.getAttribute<const char*>(ATTR_SHADOWED), restoreLabel); |
| 4331 |
} |
| 4332 |
else { |
| 4333 |
subs[i] = shadows[i].oldName; |
| 4334 |
if (reader.hasAttribute(ATTR_SHADOW) && !IGNORE_SHADOW) { |
| 4335 |
shadows[i].newName = |
| 4336 |
importSubName(reader, reader.getAttribute<const char*>(ATTR_SHADOW), restoreLabel); |
| 4337 |
} |
| 4338 |
} |
| 4339 |
if (reader.hasAttribute(ATTR_MAPPED)) { |
| 4340 |
mapped.push_back(i); |
| 4341 |
} |
| 4342 |
} |
| 4343 |
reader.readEndElement("XLink"); |
| 4344 |
} |
| 4345 |
setFlag(LinkRestoreLabel, restoreLabel); |
| 4346 |
|
| 4347 |
if (name.empty()) { |
| 4348 |
setValue(nullptr); |
| 4349 |
return; |
| 4350 |
} |
| 4351 |
|
| 4352 |
if (!file.empty() || (!object && !name.empty())) { |
| 4353 |
this->stamp = stampAttr; |
| 4354 |
setValue(std::move(file), std::move(name), std::move(subs), std::move(shadows)); |
| 4355 |
} |
| 4356 |
else { |
| 4357 |
setValue(object, std::move(subs), std::move(shadows)); |
| 4358 |
} |
| 4359 |
_mapped = std::move(mapped); |
| 4360 |
} |
| 4361 |
|
| 4362 |
Property* |
| 4363 |
PropertyXLink::CopyOnImportExternal(const std::map<std::string, std::string>& nameMap) const |
| 4364 |
{ |
| 4365 |
auto owner = freecad_cast<const DocumentObject*>(getContainer()); |
| 4366 |
if (!owner || !owner->getDocument() || !_pcLink || !_pcLink->isAttachedToDocument()) { |
| 4367 |
return nullptr; |
| 4368 |
} |
| 4369 |
|
| 4370 |
auto subs = updateLinkSubs(_pcLink, _SubList, &tryImportSubName, owner->getDocument(), nameMap); |
| 4371 |
auto linked = tryImport(owner->getDocument(), _pcLink, nameMap); |
| 4372 |
if (subs.empty() && linked == _pcLink) { |
| 4373 |
return nullptr; |
| 4374 |
} |
| 4375 |
|
| 4376 |
std::unique_ptr<PropertyXLink> p(new PropertyXLink); |
| 4377 |
copyTo(*p, linked, &subs); |
| 4378 |
return p.release(); |
| 4379 |
} |
| 4380 |
|
| 4381 |
Property* PropertyXLink::CopyOnLinkReplace(const App::DocumentObject* parent, |
| 4382 |
App::DocumentObject* oldObj, |
| 4383 |
App::DocumentObject* newObj) const |
| 4384 |
{ |
| 4385 |
auto res = tryReplaceLinkSubs(getContainer(), _pcLink, parent, oldObj, newObj, _SubList); |
| 4386 |
if (!res.first) { |
| 4387 |
return nullptr; |
| 4388 |
} |
| 4389 |
std::unique_ptr<PropertyXLink> p(new PropertyXLink); |
| 4390 |
copyTo(*p, res.first, &res.second); |
| 4391 |
return p.release(); |
| 4392 |
} |
| 4393 |
|
| 4394 |
Property* PropertyXLink::CopyOnLabelChange(App::DocumentObject* obj, |
| 4395 |
const std::string& ref, |
| 4396 |
const char* newLabel) const |
| 4397 |
{ |
| 4398 |
auto owner = dynamic_cast<const DocumentObject*>(getContainer()); |
| 4399 |
if (!owner || !owner->getDocument() || !_pcLink || !_pcLink->isAttachedToDocument()) { |
| 4400 |
return nullptr; |
| 4401 |
} |
| 4402 |
auto subs = updateLinkSubs(_pcLink, _SubList, &updateLabelReference, obj, ref, newLabel); |
| 4403 |
if (subs.empty()) { |
| 4404 |
return nullptr; |
| 4405 |
} |
| 4406 |
std::unique_ptr<PropertyXLink> p(new PropertyXLink); |
| 4407 |
copyTo(*p, _pcLink, &subs); |
| 4408 |
return p.release(); |
| 4409 |
} |
| 4410 |
|
| 4411 |
void PropertyXLink::copyTo(PropertyXLink& other, |
| 4412 |
DocumentObject* linked, |
| 4413 |
std::vector<std::string>* subs) const |
| 4414 |
{ |
| 4415 |
if (!linked) { |
| 4416 |
linked = _pcLink; |
| 4417 |
} |
| 4418 |
if (linked && linked->isAttachedToDocument()) { |
| 4419 |
other.docName = linked->getDocument()->getName(); |
| 4420 |
other.objectName = linked->getNameInDocument(); |
| 4421 |
other.docInfo.reset(); |
| 4422 |
other.filePath.clear(); |
| 4423 |
} |
| 4424 |
else { |
| 4425 |
other.objectName = objectName; |
| 4426 |
other.docName.clear(); |
| 4427 |
other.docInfo = docInfo; |
| 4428 |
other.filePath = filePath; |
| 4429 |
} |
| 4430 |
if (subs) { |
| 4431 |
other._SubList = std::move(*subs); |
| 4432 |
} |
| 4433 |
else { |
| 4434 |
other._SubList = _SubList; |
| 4435 |
other._ShadowSubList = _ShadowSubList; |
| 4436 |
} |
| 4437 |
other._Flags = _Flags; |
| 4438 |
} |
| 4439 |
|
| 4440 |
Property* PropertyXLink::Copy() const |
| 4441 |
{ |
| 4442 |
std::unique_ptr<PropertyXLink> p(new PropertyXLink); |
| 4443 |
copyTo(*p); |
| 4444 |
return p.release(); |
| 4445 |
} |
| 4446 |
|
| 4447 |
void PropertyXLink::Paste(const Property& from) |
| 4448 |
{ |
| 4449 |
if (!from.isDerivedFrom<PropertyXLink>()) { |
| 4450 |
throw Base::TypeError("Incompatible property to paste to"); |
| 4451 |
} |
| 4452 |
|
| 4453 |
const auto& other = static_cast<const PropertyXLink&>(from); |
| 4454 |
if (!other.docName.empty()) { |
| 4455 |
auto doc = GetApplication().getDocument(other.docName.c_str()); |
| 4456 |
if (!doc) { |
| 4457 |
FC_WARN("Document '" << other.docName << "' not found"); |
| 4458 |
return; |
| 4459 |
} |
| 4460 |
auto obj = doc->getObject(other.objectName.c_str()); |
| 4461 |
if (!obj) { |
| 4462 |
FC_WARN("Object '" << other.docName << '#' << other.objectName << "' not found"); |
| 4463 |
return; |
| 4464 |
} |
| 4465 |
setValue(obj, |
| 4466 |
std::vector<std::string>(other._SubList), |
| 4467 |
std::vector<ShadowSub>(other._ShadowSubList)); |
| 4468 |
} |
| 4469 |
else { |
| 4470 |
setValue(std::string(other.filePath), |
| 4471 |
std::string(other.objectName), |
| 4472 |
std::vector<std::string>(other._SubList), |
| 4473 |
std::vector<ShadowSub>(other._ShadowSubList)); |
| 4474 |
} |
| 4475 |
setFlag(LinkAllowPartial, other.testFlag(LinkAllowPartial)); |
| 4476 |
} |
| 4477 |
|
| 4478 |
bool PropertyXLink::supportXLink(const App::Property* prop) |
| 4479 |
{ |
| 4480 |
return prop->isDerivedFrom<PropertyXLink>() |
| 4481 |
|| prop->isDerivedFrom<PropertyXLinkSubList>() |
| 4482 |
|| prop->isDerivedFrom<PropertyXLinkContainer>(); |
| 4483 |
} |
| 4484 |
|
| 4485 |
bool PropertyXLink::hasXLink(const App::Document* doc) |
| 4486 |
{ |
| 4487 |
for (auto& v : _DocInfoMap) { |
| 4488 |
if (v.second->hasXLink(doc)) { |
| 4489 |
return true; |
| 4490 |
} |
| 4491 |
} |
| 4492 |
return false; |
| 4493 |
} |
| 4494 |
|
| 4495 |
bool PropertyXLink::hasXLink(const std::vector<App::DocumentObject*>& objs, |
| 4496 |
std::vector<App::Document*>* unsaved) |
| 4497 |
{ |
| 4498 |
std::set<App::Document*> docs; |
| 4499 |
bool ret = false; |
| 4500 |
for (auto o : objs) { |
| 4501 |
if (o && o->isAttachedToDocument() && docs.insert(o->getDocument()).second) { |
| 4502 |
if (!hasXLink(o->getDocument())) { |
| 4503 |
continue; |
| 4504 |
} |
| 4505 |
if (!unsaved) { |
| 4506 |
return true; |
| 4507 |
} |
| 4508 |
ret = true; |
| 4509 |
if (!o->getDocument()->isSaved()) { |
| 4510 |
unsaved->push_back(o->getDocument()); |
| 4511 |
} |
| 4512 |
} |
| 4513 |
} |
| 4514 |
return ret; |
| 4515 |
} |
| 4516 |
|
| 4517 |
void PropertyXLink::restoreDocument(const App::Document& doc) |
| 4518 |
{ |
| 4519 |
DocInfo::restoreDocument(doc); |
| 4520 |
} |
| 4521 |
|
| 4522 |
std::map<App::Document*, std::set<App::Document*>> |
| 4523 |
PropertyXLink::getDocumentOutList(App::Document* doc) |
| 4524 |
{ |
| 4525 |
std::map<App::Document*, std::set<App::Document*>> ret; |
| 4526 |
for (auto& v : _DocInfoMap) { |
| 4527 |
for (auto link : v.second->links) { |
| 4528 |
if (!v.second->pcDoc || link->getScope() == LinkScope::Hidden |
| 4529 |
|| link->testStatus(Property::PropTransient) |
| 4530 |
|| link->testStatus(Property::Transient) |
| 4531 |
|| link->testStatus(Property::PropNoPersist)) { |
| 4532 |
continue; |
| 4533 |
} |
| 4534 |
auto obj = dynamic_cast<App::DocumentObject*>(link->getContainer()); |
| 4535 |
if (!obj || !obj->isAttachedToDocument() || !obj->getDocument()) { |
| 4536 |
continue; |
| 4537 |
} |
| 4538 |
if (doc && obj->getDocument() != doc) { |
| 4539 |
continue; |
| 4540 |
} |
| 4541 |
ret[obj->getDocument()].insert(v.second->pcDoc); |
| 4542 |
} |
| 4543 |
} |
| 4544 |
return ret; |
| 4545 |
} |
| 4546 |
|
| 4547 |
std::map<App::Document*, std::set<App::Document*>> |
| 4548 |
PropertyXLink::getDocumentInList(App::Document* doc) |
| 4549 |
{ |
| 4550 |
std::map<App::Document*, std::set<App::Document*>> ret; |
| 4551 |
for (auto& v : _DocInfoMap) { |
| 4552 |
if (!v.second->pcDoc || (doc && doc != v.second->pcDoc) || v.second->links.empty()) { |
| 4553 |
continue; |
| 4554 |
} |
| 4555 |
auto& docs = ret[v.second->pcDoc]; |
| 4556 |
for (auto link : v.second->links) { |
| 4557 |
if (link->getScope() == LinkScope::Hidden || link->testStatus(Property::PropTransient) |
| 4558 |
|| link->testStatus(Property::Transient) |
| 4559 |
|| link->testStatus(Property::PropNoPersist)) { |
| 4560 |
continue; |
| 4561 |
} |
| 4562 |
auto obj = dynamic_cast<App::DocumentObject*>(link->getContainer()); |
| 4563 |
if (obj && obj->isAttachedToDocument() && obj->getDocument()) { |
| 4564 |
docs.insert(obj->getDocument()); |
| 4565 |
} |
| 4566 |
} |
| 4567 |
} |
| 4568 |
return ret; |
| 4569 |
} |
| 4570 |
|
| 4571 |
PyObject* PropertyXLink::getPyObject() |
| 4572 |
{ |
| 4573 |
if (!_pcLink) { |
| 4574 |
Py_Return; |
| 4575 |
} |
| 4576 |
const auto& subs = getSubValues(false); |
| 4577 |
if (subs.empty()) { |
| 4578 |
return _pcLink->getPyObject(); |
| 4579 |
} |
| 4580 |
Py::Tuple ret(2); |
| 4581 |
ret.setItem(0, Py::Object(_pcLink->getPyObject(), true)); |
| 4582 |
PropertyString propString; |
| 4583 |
if (subs.size() == 1) { |
| 4584 |
propString.setValue(subs.front()); |
| 4585 |
ret.setItem(1, Py::asObject(propString.getPyObject())); |
| 4586 |
} |
| 4587 |
else { |
| 4588 |
Py::List list(subs.size()); |
| 4589 |
int i = 0; |
| 4590 |
for (auto& sub : subs) { |
| 4591 |
propString.setValue(sub); |
| 4592 |
list[i++] = Py::asObject(propString.getPyObject()); |
| 4593 |
} |
| 4594 |
ret.setItem(1, list); |
| 4595 |
} |
| 4596 |
return Py::new_reference_to(ret); |
| 4597 |
} |
| 4598 |
|
| 4599 |
void PropertyXLink::setPyObject(PyObject* value) |
| 4600 |
{ |
| 4601 |
if (PySequence_Check(value)) { |
| 4602 |
Py::Sequence seq(value); |
| 4603 |
if (seq.size() != 2) { |
| 4604 |
throw Base::ValueError("Expect input sequence of size 2"); |
| 4605 |
} |
| 4606 |
std::vector<std::string> subs; |
| 4607 |
Py::Object pyObj(seq[0].ptr()); |
| 4608 |
Py::Object pySub(seq[1].ptr()); |
| 4609 |
if (pyObj.isNone()) { |
| 4610 |
setValue(nullptr); |
| 4611 |
return; |
| 4612 |
} |
| 4613 |
else if (!PyObject_TypeCheck(pyObj.ptr(), &DocumentObjectPy::Type)) { |
| 4614 |
throw Base::TypeError("Expect the first element to be of 'DocumentObject'"); |
| 4615 |
} |
| 4616 |
PropertyString propString; |
| 4617 |
if (pySub.isString()) { |
| 4618 |
propString.setPyObject(pySub.ptr()); |
| 4619 |
subs.push_back(propString.getStrValue()); |
| 4620 |
} |
| 4621 |
else if (pySub.isSequence()) { |
| 4622 |
Py::Sequence seq(pySub); |
| 4623 |
subs.reserve(seq.size()); |
| 4624 |
for (Py_ssize_t i = 0; i < seq.size(); ++i) { |
| 4625 |
Py::Object sub(seq[i]); |
| 4626 |
if (!sub.isString()) { |
| 4627 |
throw Base::TypeError("Expect only string inside second argument"); |
| 4628 |
} |
| 4629 |
propString.setPyObject(sub.ptr()); |
| 4630 |
subs.push_back(propString.getStrValue()); |
| 4631 |
} |
| 4632 |
} |
| 4633 |
else { |
| 4634 |
throw Base::TypeError("Expect the second element to be a string or sequence of string"); |
| 4635 |
} |
| 4636 |
setValue(static_cast<DocumentObjectPy*>(pyObj.ptr())->getDocumentObjectPtr(), |
| 4637 |
std::move(subs)); |
| 4638 |
} |
| 4639 |
else if (PyObject_TypeCheck(value, &(DocumentObjectPy::Type))) { |
| 4640 |
setValue(static_cast<DocumentObjectPy*>(value)->getDocumentObjectPtr()); |
| 4641 |
} |
| 4642 |
else if (Py_None == value) { |
| 4643 |
setValue(nullptr); |
| 4644 |
} |
| 4645 |
else { |
| 4646 |
throw Base::TypeError( |
| 4647 |
"type must be 'DocumentObject', 'None', or '(DocumentObject, SubName)' or " |
| 4648 |
"'DocumentObject, [SubName..])"); |
| 4649 |
} |
| 4650 |
} |
| 4651 |
|
| 4652 |
const char* PropertyXLink::getSubName(bool newStyle) const |
| 4653 |
{ |
| 4654 |
if (_SubList.empty() || _ShadowSubList.empty()) { |
| 4655 |
return ""; |
| 4656 |
} |
| 4657 |
return getSubNameWithStyle(_SubList[0], _ShadowSubList[0], newStyle, tmpShadow).c_str(); |
| 4658 |
} |
| 4659 |
|
| 4660 |
void PropertyXLink::getLinks(std::vector<App::DocumentObject*>& objs, |
| 4661 |
bool all, |
| 4662 |
std::vector<std::string>* subs, |
| 4663 |
bool newStyle) const |
| 4664 |
{ |
| 4665 |
if ((all || _pcScope != LinkScope::Hidden) && _pcLink && _pcLink->isAttachedToDocument()) { |
| 4666 |
// we use to run this method everytime the program needed to access the sub-elements in |
| 4667 |
// a property link, but it caused multiple issues (#23441 and #23402) so it has been |
| 4668 |
// commented out. |
| 4669 |
// updateElementReferences(_pcLink, false); |
| 4670 |
objs.push_back(_pcLink); |
| 4671 |
if (subs && _SubList.size() == _ShadowSubList.size()) { |
| 4672 |
*subs = getSubValues(newStyle); |
| 4673 |
} |
| 4674 |
} |
| 4675 |
} |
| 4676 |
|
| 4677 |
void PropertyXLink::getLinksTo(std::vector<App::ObjectIdentifier>& identifiers, |
| 4678 |
App::DocumentObject* obj, |
| 4679 |
const char* subname, |
| 4680 |
bool all) const |
| 4681 |
{ |
| 4682 |
if (all || _pcScope != LinkScope::Hidden) { |
| 4683 |
if (obj && obj == _pcLink) { |
| 4684 |
_getLinksTo(identifiers, obj, subname, _SubList, _ShadowSubList); |
| 4685 |
} |
| 4686 |
} |
| 4687 |
} |
| 4688 |
|
| 4689 |
bool PropertyXLink::adjustLink(const std::set<App::DocumentObject*>& inList) |
| 4690 |
{ |
| 4691 |
if (_pcScope == LinkScope::Hidden) { |
| 4692 |
return false; |
| 4693 |
} |
| 4694 |
if (!_pcLink || !_pcLink->isAttachedToDocument() || !inList.contains(_pcLink)) { |
| 4695 |
return false; |
| 4696 |
} |
| 4697 |
auto subs = _SubList; |
| 4698 |
auto link = adjustLinkSubs(this, inList, _pcLink, subs); |
| 4699 |
if (link) { |
| 4700 |
setValue(link, std::move(subs)); |
| 4701 |
return true; |
| 4702 |
} |
| 4703 |
return false; |
| 4704 |
} |
| 4705 |
|
| 4706 |
std::vector<std::string> PropertyXLink::getSubValues(bool newStyle) const |
| 4707 |
{ |
| 4708 |
assert(_SubList.size() == _ShadowSubList.size()); |
| 4709 |
std::vector<std::string> ret; |
| 4710 |
ret.reserve(_SubList.size()); |
| 4711 |
std::string tmp; |
| 4712 |
for (size_t i = 0; i < _ShadowSubList.size(); ++i) { |
| 4713 |
ret.push_back(getSubNameWithStyle(_SubList[i], _ShadowSubList[i], newStyle, tmp)); |
| 4714 |
} |
| 4715 |
return ret; |
| 4716 |
} |
| 4717 |
|
| 4718 |
std::vector<std::string> PropertyXLink::getSubValuesStartsWith(const char* starter, |
| 4719 |
bool newStyle) const |
| 4720 |
{ |
| 4721 |
(void)newStyle; |
| 4722 |
|
| 4723 |
std::vector<std::string> temp; |
| 4724 |
for (const auto& it : _SubList) { |
| 4725 |
if (strncmp(starter, it.c_str(), strlen(starter)) == 0) { |
| 4726 |
temp.push_back(it); |
| 4727 |
} |
| 4728 |
} |
| 4729 |
return temp; |
| 4730 |
} |
| 4731 |
|
| 4732 |
void PropertyXLink::setAllowPartial(bool enable) |
| 4733 |
{ |
| 4734 |
setFlag(LinkAllowPartial, enable); |
| 4735 |
if (enable) { |
| 4736 |
return; |
| 4737 |
} |
| 4738 |
auto owner = dynamic_cast<const DocumentObject*>(getContainer()); |
| 4739 |
if (!owner) { |
| 4740 |
return; |
| 4741 |
} |
| 4742 |
if (!App::GetApplication().isRestoring() && !owner->getDocument()->isPerformingTransaction() |
| 4743 |
&& !_pcLink && docInfo && !filePath.empty() && !objectName.empty() |
| 4744 |
&& (!docInfo->pcDoc || docInfo->pcDoc->testStatus(Document::PartialDoc))) { |
| 4745 |
auto path = docInfo->getDocPath(filePath.c_str(), owner->getDocument(), false); |
| 4746 |
if (!path.empty()) { |
| 4747 |
App::GetApplication().openDocument(path.c_str()); |
| 4748 |
} |
| 4749 |
} |
| 4750 |
} |
| 4751 |
|
| 4752 |
//************************************************************************** |
| 4753 |
// PropertyXLinkSub |
| 4754 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 4755 |
|
| 4756 |
TYPESYSTEM_SOURCE(App::PropertyXLinkSub, App::PropertyXLink) |
| 4757 |
TYPESYSTEM_SOURCE(App::PropertyXLinkSubHidden, App::PropertyXLinkSub) |
| 4758 |
|
| 4759 |
PropertyXLinkSub::PropertyXLinkSub(bool allowPartial, PropertyLinkBase* parent) |
| 4760 |
: PropertyXLink(allowPartial, parent) |
| 4761 |
{} |
| 4762 |
|
| 4763 |
PropertyXLinkSub::~PropertyXLinkSub() = default; |
| 4764 |
|
| 4765 |
bool PropertyXLinkSub::upgrade(Base::XMLReader& reader, const char* typeName) |
| 4766 |
{ |
| 4767 |
if (strcmp(typeName, PropertyLinkSubGlobal::getClassTypeId().getName()) == 0 |
| 4768 |
|| strcmp(typeName, PropertyLinkSub::getClassTypeId().getName()) == 0 |
| 4769 |
|| strcmp(typeName, PropertyLinkSubChild::getClassTypeId().getName()) == 0) { |
| 4770 |
App::PropertyLinkSub linkProp; |
| 4771 |
linkProp.setContainer(getContainer()); |
| 4772 |
linkProp.Restore(reader); |
| 4773 |
setValue(linkProp.getValue(), linkProp.getSubValues()); |
| 4774 |
return true; |
| 4775 |
} |
| 4776 |
return PropertyXLink::upgrade(reader, typeName); |
| 4777 |
} |
| 4778 |
|
| 4779 |
PyObject* PropertyXLinkSub::getPyObject() |
| 4780 |
{ |
| 4781 |
if (!_pcLink) { |
| 4782 |
Py_Return; |
| 4783 |
} |
| 4784 |
Py::Tuple ret(2); |
| 4785 |
ret.setItem(0, Py::Object(_pcLink->getPyObject(), true)); |
| 4786 |
const auto& subs = getSubValues(false); |
| 4787 |
Py::List list(subs.size()); |
| 4788 |
int i = 0; |
| 4789 |
PropertyString propString; |
| 4790 |
for (auto& sub : subs) { |
| 4791 |
propString.setValue(sub); |
| 4792 |
list[i++] = Py::asObject(propString.getPyObject()); |
| 4793 |
} |
| 4794 |
ret.setItem(1, list); |
| 4795 |
return Py::new_reference_to(ret); |
| 4796 |
} |
| 4797 |
|
| 4798 |
//************************************************************************** |
| 4799 |
// PropertyXLinkSubList |
| 4800 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 4801 |
|
| 4802 |
TYPESYSTEM_SOURCE(App::PropertyXLinkSubList, App::PropertyLinkBase) |
| 4803 |
|
| 4804 |
//************************************************************************** |
| 4805 |
// Construction/Destruction |
| 4806 |
|
| 4807 |
|
| 4808 |
PropertyXLinkSubList::PropertyXLinkSubList() |
| 4809 |
{ |
| 4810 |
_pcScope = LinkScope::Global; |
| 4811 |
setSyncSubObject(true); |
| 4812 |
} |
| 4813 |
|
| 4814 |
PropertyXLinkSubList::~PropertyXLinkSubList() = default; |
| 4815 |
|
| 4816 |
void PropertyXLinkSubList::setSyncSubObject(bool enable) |
| 4817 |
{ |
| 4818 |
_Flags.set((std::size_t)LinkSyncSubObject, enable); |
| 4819 |
} |
| 4820 |
|
| 4821 |
int PropertyXLinkSubList::getSize() const |
| 4822 |
{ |
| 4823 |
return static_cast<int>(_Links.size()); |
| 4824 |
} |
| 4825 |
|
| 4826 |
void PropertyXLinkSubList::setValue(DocumentObject* lValue, const char* SubName) |
| 4827 |
{ |
| 4828 |
std::map<DocumentObject*, std::vector<std::string>> values; |
| 4829 |
if (lValue) { |
| 4830 |
auto& subs = values[lValue]; |
| 4831 |
if (SubName) { |
| 4832 |
subs.emplace_back(SubName); |
| 4833 |
} |
| 4834 |
} |
| 4835 |
setValues(std::move(values)); |
| 4836 |
} |
| 4837 |
|
| 4838 |
void PropertyXLinkSubList::setValues(const std::vector<DocumentObject*>& lValue, |
| 4839 |
const std::vector<const char*>& lSubNames) |
| 4840 |
{ |
| 4841 |
#define CHECK_SUB_SIZE(_l, _r) \ |
| 4842 |
do { \ |
| 4843 |
if (_l.size() != _r.size()) \ |
| 4844 |
FC_THROWM(Base::ValueError, "object and subname size mismatch"); \ |
| 4845 |
} while (0) |
| 4846 |
CHECK_SUB_SIZE(lValue, lSubNames); |
| 4847 |
std::map<DocumentObject*, std::vector<std::string>> values; |
| 4848 |
int i = 0; |
| 4849 |
for (auto& obj : lValue) { |
| 4850 |
const char* sub = lSubNames[i++]; |
| 4851 |
if (sub) { |
| 4852 |
values[obj].emplace_back(sub); |
| 4853 |
} |
| 4854 |
} |
| 4855 |
setValues(std::move(values)); |
| 4856 |
} |
| 4857 |
|
| 4858 |
void PropertyXLinkSubList::setValues(const std::vector<DocumentObject*>& lValue, |
| 4859 |
const std::vector<std::string>& lSubNames) |
| 4860 |
{ |
| 4861 |
CHECK_SUB_SIZE(lValue, lSubNames); |
| 4862 |
std::map<DocumentObject*, std::vector<std::string>> values; |
| 4863 |
int i = 0; |
| 4864 |
for (auto& obj : lValue) { |
| 4865 |
values[obj].push_back(lSubNames[i++]); |
| 4866 |
} |
| 4867 |
setValues(std::move(values)); |
| 4868 |
} |
| 4869 |
|
| 4870 |
void PropertyXLinkSubList::setSubListValues(const std::vector<PropertyLinkSubList::SubSet>& svalues) |
| 4871 |
{ |
| 4872 |
std::map<DocumentObject*, std::vector<std::string>> values; |
| 4873 |
for (auto& v : svalues) { |
| 4874 |
auto& s = values[v.first]; |
| 4875 |
s.reserve(s.size() + v.second.size()); |
| 4876 |
s.insert(s.end(), v.second.begin(), v.second.end()); |
| 4877 |
} |
| 4878 |
setValues(std::move(values)); |
| 4879 |
} |
| 4880 |
|
| 4881 |
void PropertyXLinkSubList::setValues( |
| 4882 |
const std::map<App::DocumentObject*, std::vector<std::string>>& values) |
| 4883 |
{ |
| 4884 |
setValues(std::map<App::DocumentObject*, std::vector<std::string>>(values)); |
| 4885 |
} |
| 4886 |
|
| 4887 |
void PropertyXLinkSubList::setValues( |
| 4888 |
std::map<App::DocumentObject*, std::vector<std::string>>&& values) |
| 4889 |
{ |
| 4890 |
for (auto& v : values) { |
| 4891 |
if (!v.first || !v.first->isAttachedToDocument()) { |
| 4892 |
FC_THROWM(Base::ValueError, "invalid document object"); |
| 4893 |
} |
| 4894 |
} |
| 4895 |
|
| 4896 |
atomic_change guard(*this); |
| 4897 |
|
| 4898 |
for (auto it = _Links.begin(), itNext = it; it != _Links.end(); it = itNext) { |
| 4899 |
++itNext; |
| 4900 |
auto iter = values.find(it->getValue()); |
| 4901 |
if (iter == values.end()) { |
| 4902 |
_Links.erase(it); |
| 4903 |
continue; |
| 4904 |
} |
| 4905 |
it->setSubValues(std::move(iter->second)); |
| 4906 |
values.erase(iter); |
| 4907 |
} |
| 4908 |
|
| 4909 |
for (auto& v : values) { |
| 4910 |
_Links.emplace_back(testFlag(LinkAllowPartial), this); |
| 4911 |
_Links.back().setValue(v.first, std::move(v.second)); |
| 4912 |
} |
| 4913 |
guard.tryInvoke(); |
| 4914 |
} |
| 4915 |
|
| 4916 |
void PropertyXLinkSubList::addValue(App::DocumentObject* obj, |
| 4917 |
const std::vector<std::string>& subs, |
| 4918 |
bool reset) |
| 4919 |
{ |
| 4920 |
addValue(obj, std::vector<std::string>(subs), reset); |
| 4921 |
} |
| 4922 |
|
| 4923 |
void PropertyXLinkSubList::addValue(App::DocumentObject* obj, |
| 4924 |
std::vector<std::string>&& subs, |
| 4925 |
bool reset) |
| 4926 |
{ |
| 4927 |
|
| 4928 |
if (!obj || !obj->isAttachedToDocument()) { |
| 4929 |
FC_THROWM(Base::ValueError, "invalid document object"); |
| 4930 |
} |
| 4931 |
|
| 4932 |
for (auto& l : _Links) { |
| 4933 |
if (l.getValue() == obj) { |
| 4934 |
auto s = l.getSubValues(); |
| 4935 |
if (s.empty() || reset) { |
| 4936 |
l.setSubValues(std::move(subs)); |
| 4937 |
} |
| 4938 |
else { |
| 4939 |
s.reserve(s.size() + subs.size()); |
| 4940 |
std::move(subs.begin(), subs.end(), std::back_inserter(s)); |
| 4941 |
l.setSubValues(std::move(s)); |
| 4942 |
} |
| 4943 |
return; |
| 4944 |
} |
| 4945 |
} |
| 4946 |
atomic_change guard(*this); |
| 4947 |
_Links.emplace_back(testFlag(LinkAllowPartial), this); |
| 4948 |
_Links.back().setValue(obj, std::move(subs)); |
| 4949 |
guard.tryInvoke(); |
| 4950 |
} |
| 4951 |
|
| 4952 |
void PropertyXLinkSubList::setValue(DocumentObject* lValue, const std::vector<std::string>& SubList) |
| 4953 |
{ |
| 4954 |
std::map<DocumentObject*, std::vector<std::string>> values; |
| 4955 |
if (lValue) { |
| 4956 |
values[lValue] = SubList; |
| 4957 |
} |
| 4958 |
setValues(std::move(values)); |
| 4959 |
} |
| 4960 |
|
| 4961 |
void PropertyXLinkSubList::setValues(const std::vector<DocumentObject*>& values) |
| 4962 |
{ |
| 4963 |
atomic_change guard(*this); |
| 4964 |
_Links.clear(); |
| 4965 |
for (auto obj : values) { |
| 4966 |
_Links.emplace_back(testFlag(LinkAllowPartial), this); |
| 4967 |
_Links.back().setValue(obj); |
| 4968 |
} |
| 4969 |
guard.tryInvoke(); |
| 4970 |
} |
| 4971 |
|
| 4972 |
void PropertyXLinkSubList::set1Value(int idx, |
| 4973 |
DocumentObject* value, |
| 4974 |
const std::vector<std::string>& SubList) |
| 4975 |
{ |
| 4976 |
if (idx < -1 || idx > getSize()) { |
| 4977 |
throw Base::RuntimeError("index out of bound"); |
| 4978 |
} |
| 4979 |
|
| 4980 |
if (idx < 0 || idx + 1 == getSize()) { |
| 4981 |
if (SubList.empty()) { |
| 4982 |
addValue(value, SubList); |
| 4983 |
return; |
| 4984 |
} |
| 4985 |
atomic_change guard(*this); |
| 4986 |
_Links.emplace_back(testFlag(LinkAllowPartial), this); |
| 4987 |
_Links.back().setValue(value); |
| 4988 |
guard.tryInvoke(); |
| 4989 |
return; |
| 4990 |
} |
| 4991 |
|
| 4992 |
auto it = _Links.begin(); |
| 4993 |
for (; idx; --idx) { |
| 4994 |
++it; |
| 4995 |
} |
| 4996 |
it->setValue(value, SubList); |
| 4997 |
} |
| 4998 |
|
| 4999 |
const string PropertyXLinkSubList::getPyReprString() const |
| 5000 |
{ |
| 5001 |
if (_Links.empty()) { |
| 5002 |
return std::string("None"); |
| 5003 |
} |
| 5004 |
std::ostringstream ss; |
| 5005 |
ss << '['; |
| 5006 |
for (auto& link : _Links) { |
| 5007 |
auto obj = link.getValue(); |
| 5008 |
if (!obj || !obj->isAttachedToDocument()) { |
| 5009 |
continue; |
| 5010 |
} |
| 5011 |
ss << "(App.getDocument('" << obj->getDocument()->getName() << "').getObject('" |
| 5012 |
<< obj->getNameInDocument() << "'), ("; |
| 5013 |
const auto& subs = link.getSubValues(); |
| 5014 |
if (subs.empty()) { |
| 5015 |
ss << "''"; |
| 5016 |
} |
| 5017 |
else { |
| 5018 |
for (auto& sub : subs) { |
| 5019 |
ss << "'" << sub << "',"; |
| 5020 |
} |
| 5021 |
} |
| 5022 |
ss << ")), "; |
| 5023 |
} |
| 5024 |
ss << ']'; |
| 5025 |
return ss.str(); |
| 5026 |
} |
| 5027 |
|
| 5028 |
DocumentObject* PropertyXLinkSubList::getValue() const |
| 5029 |
{ |
| 5030 |
if (!_Links.empty()) { |
| 5031 |
return _Links.begin()->getValue(); |
| 5032 |
} |
| 5033 |
return nullptr; |
| 5034 |
} |
| 5035 |
|
| 5036 |
int PropertyXLinkSubList::removeValue(App::DocumentObject* lValue) |
| 5037 |
{ |
| 5038 |
atomic_change guard(*this, false); |
| 5039 |
int ret = 0; |
| 5040 |
for (auto it = _Links.begin(); it != _Links.end();) { |
| 5041 |
if (it->getValue() != lValue) { |
| 5042 |
++it; |
| 5043 |
} |
| 5044 |
else { |
| 5045 |
guard.aboutToChange(); |
| 5046 |
it = _Links.erase(it); |
| 5047 |
++ret; |
| 5048 |
} |
| 5049 |
} |
| 5050 |
guard.tryInvoke(); |
| 5051 |
return ret; |
| 5052 |
} |
| 5053 |
|
| 5054 |
PyObject* PropertyXLinkSubList::getPyObject() |
| 5055 |
{ |
| 5056 |
Py::List list; |
| 5057 |
for (auto& link : _Links) { |
| 5058 |
auto obj = link.getValue(); |
| 5059 |
if (!obj || !obj->isAttachedToDocument()) { |
| 5060 |
continue; |
| 5061 |
} |
| 5062 |
|
| 5063 |
Py::Tuple tup(2); |
| 5064 |
tup[0] = Py::asObject(obj->getPyObject()); |
| 5065 |
|
| 5066 |
const auto& subs = link.getSubValues(); |
| 5067 |
Py::Tuple items(subs.size()); |
| 5068 |
for (std::size_t j = 0; j < subs.size(); j++) { |
| 5069 |
items[j] = Py::String(subs[j]); |
| 5070 |
} |
| 5071 |
tup[1] = items; |
| 5072 |
list.append(tup); |
| 5073 |
} |
| 5074 |
return Py::new_reference_to(list); |
| 5075 |
} |
| 5076 |
|
| 5077 |
void PropertyXLinkSubList::setPyObject(PyObject* value) |
| 5078 |
{ |
| 5079 |
try { // try PropertyLinkSub syntax |
| 5080 |
PropertyLinkSub dummy; |
| 5081 |
dummy.setAllowExternal(true); |
| 5082 |
dummy.setPyObject(value); |
| 5083 |
this->setValue(dummy.getValue(), dummy.getSubValues()); |
| 5084 |
return; |
| 5085 |
} |
| 5086 |
catch (Base::Exception&) { |
| 5087 |
} |
| 5088 |
|
| 5089 |
if (!PyTuple_Check(value) && !PyList_Check(value)) { |
| 5090 |
throw Base::TypeError( |
| 5091 |
"Invalid type. Accepts (DocumentObject, (subname...)) or sequence of such type."); |
| 5092 |
} |
| 5093 |
Py::Sequence seq(value); |
| 5094 |
std::map<DocumentObject*, std::vector<std::string>> values; |
| 5095 |
try { |
| 5096 |
for (Py_ssize_t i = 0; i < seq.size(); ++i) { |
| 5097 |
PropertyLinkSub link; |
| 5098 |
link.setAllowExternal(true); |
| 5099 |
link.setPyObject(seq[i].ptr()); |
| 5100 |
const auto& subs = link.getSubValues(); |
| 5101 |
auto& s = values[link.getValue()]; |
| 5102 |
s.reserve(s.size() + subs.size()); |
| 5103 |
s.insert(s.end(), subs.begin(), subs.end()); |
| 5104 |
} |
| 5105 |
} |
| 5106 |
catch (Base::Exception&) { |
| 5107 |
throw Base::TypeError( |
| 5108 |
"Invalid type inside sequence. Must be type of (DocumentObject, (subname...))"); |
| 5109 |
} |
| 5110 |
setValues(std::move(values)); |
| 5111 |
} |
| 5112 |
|
| 5113 |
void PropertyXLinkSubList::afterRestore() |
| 5114 |
{ |
| 5115 |
for (auto& l : _Links) { |
| 5116 |
l.afterRestore(); |
| 5117 |
} |
| 5118 |
} |
| 5119 |
|
| 5120 |
void PropertyXLinkSubList::onContainerRestored() |
| 5121 |
{ |
| 5122 |
for (auto& l : _Links) { |
| 5123 |
l.onContainerRestored(); |
| 5124 |
} |
| 5125 |
} |
| 5126 |
|
| 5127 |
void PropertyXLinkSubList::updateElementReference(DocumentObject* feature, |
| 5128 |
bool reverse, |
| 5129 |
bool notify) |
| 5130 |
{ |
| 5131 |
for (auto& l : _Links) { |
| 5132 |
l.updateElementReference(feature, reverse, notify); |
| 5133 |
} |
| 5134 |
} |
| 5135 |
|
| 5136 |
bool PropertyXLinkSubList::referenceChanged() const |
| 5137 |
{ |
| 5138 |
for (auto& l : _Links) { |
| 5139 |
if (l.referenceChanged()) { |
| 5140 |
return true; |
| 5141 |
} |
| 5142 |
} |
| 5143 |
return false; |
| 5144 |
} |
| 5145 |
|
| 5146 |
void PropertyXLinkSubList::Save(Base::Writer& writer) const |
| 5147 |
{ |
| 5148 |
writer.Stream() << writer.ind() << "<XLinkSubList count=\"" << _Links.size(); |
| 5149 |
if (testFlag(LinkAllowPartial)) { |
| 5150 |
writer.Stream() << "\" partial=\"1"; |
| 5151 |
} |
| 5152 |
writer.Stream() << "\">" << endl; |
| 5153 |
writer.incInd(); |
| 5154 |
for (auto& l : _Links) { |
| 5155 |
l.Save(writer); |
| 5156 |
} |
| 5157 |
writer.decInd(); |
| 5158 |
writer.Stream() << writer.ind() << "</XLinkSubList>" << endl; |
| 5159 |
} |
| 5160 |
|
| 5161 |
void PropertyXLinkSubList::Restore(Base::XMLReader& reader) |
| 5162 |
{ |
| 5163 |
reader.readElement("XLinkSubList"); |
| 5164 |
setFlag(LinkAllowPartial, |
| 5165 |
reader.hasAttribute("partial") && reader.getAttribute<bool>("partial")); |
| 5166 |
int count = reader.getAttribute<long>("count"); |
| 5167 |
atomic_change guard(*this, false); |
| 5168 |
_Links.clear(); |
| 5169 |
for (int i = 0; i < count; ++i) { |
| 5170 |
_Links.emplace_back(false, this); |
| 5171 |
_Links.back().Restore(reader); |
| 5172 |
} |
| 5173 |
reader.readEndElement("XLinkSubList"); |
| 5174 |
guard.tryInvoke(); |
| 5175 |
} |
| 5176 |
|
| 5177 |
Property* |
| 5178 |
PropertyXLinkSubList::CopyOnImportExternal(const std::map<std::string, std::string>& nameMap) const |
| 5179 |
{ |
| 5180 |
std::unique_ptr<Property> copy; |
| 5181 |
auto it = _Links.begin(); |
| 5182 |
for (; it != _Links.end(); ++it) { |
| 5183 |
copy.reset(it->CopyOnImportExternal(nameMap)); |
| 5184 |
if (copy) { |
| 5185 |
break; |
| 5186 |
} |
| 5187 |
} |
| 5188 |
if (!copy) { |
| 5189 |
return nullptr; |
| 5190 |
} |
| 5191 |
std::unique_ptr<PropertyXLinkSubList> p(new PropertyXLinkSubList); |
| 5192 |
for (auto iter = _Links.begin(); iter != it; ++iter) { |
| 5193 |
p->_Links.emplace_back(); |
| 5194 |
iter->copyTo(p->_Links.back()); |
| 5195 |
} |
| 5196 |
p->_Links.emplace_back(); |
| 5197 |
static_cast<PropertyXLinkSub&>(*copy).copyTo(p->_Links.back()); |
| 5198 |
for (++it; it != _Links.end(); ++it) { |
| 5199 |
p->_Links.emplace_back(); |
| 5200 |
copy.reset(it->CopyOnImportExternal(nameMap)); |
| 5201 |
if (copy) { |
| 5202 |
static_cast<PropertyXLinkSub&>(*copy).copyTo(p->_Links.back()); |
| 5203 |
} |
| 5204 |
else { |
| 5205 |
it->copyTo(p->_Links.back()); |
| 5206 |
} |
| 5207 |
} |
| 5208 |
return p.release(); |
| 5209 |
} |
| 5210 |
|
| 5211 |
Property* PropertyXLinkSubList::CopyOnLabelChange(App::DocumentObject* obj, |
| 5212 |
const std::string& ref, |
| 5213 |
const char* newLabel) const |
| 5214 |
{ |
| 5215 |
std::unique_ptr<Property> copy; |
| 5216 |
auto it = _Links.begin(); |
| 5217 |
for (; it != _Links.end(); ++it) { |
| 5218 |
copy.reset(it->CopyOnLabelChange(obj, ref, newLabel)); |
| 5219 |
if (copy) { |
| 5220 |
break; |
| 5221 |
} |
| 5222 |
} |
| 5223 |
if (!copy) { |
| 5224 |
return nullptr; |
| 5225 |
} |
| 5226 |
std::unique_ptr<PropertyXLinkSubList> p(new PropertyXLinkSubList); |
| 5227 |
for (auto iter = _Links.begin(); iter != it; ++iter) { |
| 5228 |
p->_Links.emplace_back(); |
| 5229 |
iter->copyTo(p->_Links.back()); |
| 5230 |
} |
| 5231 |
p->_Links.emplace_back(); |
| 5232 |
static_cast<PropertyXLinkSub&>(*copy).copyTo(p->_Links.back()); |
| 5233 |
for (++it; it != _Links.end(); ++it) { |
| 5234 |
p->_Links.emplace_back(); |
| 5235 |
copy.reset(it->CopyOnLabelChange(obj, ref, newLabel)); |
| 5236 |
if (copy) { |
| 5237 |
static_cast<PropertyXLinkSub&>(*copy).copyTo(p->_Links.back()); |
| 5238 |
} |
| 5239 |
else { |
| 5240 |
it->copyTo(p->_Links.back()); |
| 5241 |
} |
| 5242 |
} |
| 5243 |
return p.release(); |
| 5244 |
} |
| 5245 |
|
| 5246 |
Property* PropertyXLinkSubList::CopyOnLinkReplace(const App::DocumentObject* parent, |
| 5247 |
App::DocumentObject* oldObj, |
| 5248 |
App::DocumentObject* newObj) const |
| 5249 |
{ |
| 5250 |
std::unique_ptr<Property> copy; |
| 5251 |
PropertyXLinkSub* copied = nullptr; |
| 5252 |
std::set<std::string> subs; |
| 5253 |
auto it = _Links.begin(); |
| 5254 |
for (; it != _Links.end(); ++it) { |
| 5255 |
copy.reset(it->CopyOnLinkReplace(parent, oldObj, newObj)); |
| 5256 |
if (copy) { |
| 5257 |
copied = static_cast<PropertyXLinkSub*>(copy.get()); |
| 5258 |
if (copied->getValue() == newObj) { |
| 5259 |
for (auto& sub : copied->getSubValues()) { |
| 5260 |
subs.insert(sub); |
| 5261 |
} |
| 5262 |
} |
| 5263 |
break; |
| 5264 |
} |
| 5265 |
} |
| 5266 |
if (!copy) { |
| 5267 |
return nullptr; |
| 5268 |
} |
| 5269 |
std::unique_ptr<PropertyXLinkSubList> p(new PropertyXLinkSubList); |
| 5270 |
for (auto iter = _Links.begin(); iter != it; ++iter) { |
| 5271 |
if (iter->getValue() == newObj && copied->getValue() == newObj) { |
| 5272 |
// merge subnames in case new object already exists |
| 5273 |
for (auto& sub : iter->getSubValues()) { |
| 5274 |
if (subs.insert(sub).second) { |
| 5275 |
copied->_SubList.push_back(sub); |
| 5276 |
} |
| 5277 |
} |
| 5278 |
} |
| 5279 |
else { |
| 5280 |
p->_Links.emplace_back(); |
| 5281 |
iter->copyTo(p->_Links.back()); |
| 5282 |
} |
| 5283 |
} |
| 5284 |
p->_Links.emplace_back(); |
| 5285 |
copied->copyTo(p->_Links.back()); |
| 5286 |
copied = &p->_Links.back(); |
| 5287 |
for (++it; it != _Links.end(); ++it) { |
| 5288 |
if ((it->getValue() == newObj || it->getValue() == oldObj) |
| 5289 |
&& copied->getValue() == newObj) { |
| 5290 |
// merge subnames in case new object already exists |
| 5291 |
for (auto& sub : it->getSubValues()) { |
| 5292 |
if (subs.insert(sub).second) { |
| 5293 |
copied->_SubList.push_back(sub); |
| 5294 |
} |
| 5295 |
} |
| 5296 |
continue; |
| 5297 |
} |
| 5298 |
p->_Links.emplace_back(); |
| 5299 |
copy.reset(it->CopyOnLinkReplace(parent, oldObj, newObj)); |
| 5300 |
if (copy) { |
| 5301 |
static_cast<PropertyXLinkSub&>(*copy).copyTo(p->_Links.back()); |
| 5302 |
} |
| 5303 |
else { |
| 5304 |
it->copyTo(p->_Links.back()); |
| 5305 |
} |
| 5306 |
} |
| 5307 |
return p.release(); |
| 5308 |
} |
| 5309 |
|
| 5310 |
Property* PropertyXLinkSubList::Copy() const |
| 5311 |
{ |
| 5312 |
PropertyXLinkSubList* p = new PropertyXLinkSubList(); |
| 5313 |
for (auto& l : _Links) { |
| 5314 |
p->_Links.emplace_back(testFlag(LinkAllowPartial), p); |
| 5315 |
l.copyTo(p->_Links.back()); |
| 5316 |
} |
| 5317 |
return p; |
| 5318 |
} |
| 5319 |
|
| 5320 |
void PropertyXLinkSubList::Paste(const Property& from) |
| 5321 |
{ |
| 5322 |
if (!from.isDerivedFrom<PropertyXLinkSubList>()) { |
| 5323 |
throw Base::TypeError("Incompatible property to paste to"); |
| 5324 |
} |
| 5325 |
|
| 5326 |
aboutToSetValue(); |
| 5327 |
_Links.clear(); |
| 5328 |
for (auto& l : static_cast<const PropertyXLinkSubList&>(from)._Links) { |
| 5329 |
_Links.emplace_back(testFlag(LinkAllowPartial), this); |
| 5330 |
_Links.back().Paste(l); |
| 5331 |
} |
| 5332 |
hasSetValue(); |
| 5333 |
} |
| 5334 |
|
| 5335 |
unsigned int PropertyXLinkSubList::getMemSize() const |
| 5336 |
{ |
| 5337 |
unsigned int size = 0; |
| 5338 |
for (auto& l : _Links) { |
| 5339 |
size += l.getMemSize(); |
| 5340 |
} |
| 5341 |
return size; |
| 5342 |
} |
| 5343 |
|
| 5344 |
const std::vector<std::string>& PropertyXLinkSubList::getSubValues(App::DocumentObject* obj) const |
| 5345 |
{ |
| 5346 |
for (auto& l : _Links) { |
| 5347 |
if (l.getValue() == obj) { |
| 5348 |
return l.getSubValues(); |
| 5349 |
} |
| 5350 |
} |
| 5351 |
FC_THROWM(Base::RuntimeError, "object not found"); |
| 5352 |
} |
| 5353 |
|
| 5354 |
std::vector<std::string> PropertyXLinkSubList::getSubValues(App::DocumentObject* obj, |
| 5355 |
bool newStyle) const |
| 5356 |
{ |
| 5357 |
for (auto& l : _Links) { |
| 5358 |
if (l.getValue() == obj) { |
| 5359 |
return l.getSubValues(newStyle); |
| 5360 |
} |
| 5361 |
} |
| 5362 |
return {}; |
| 5363 |
} |
| 5364 |
|
| 5365 |
void PropertyXLinkSubList::getLinks(std::vector<App::DocumentObject*>& objs, |
| 5366 |
bool all, |
| 5367 |
std::vector<std::string>* subs, |
| 5368 |
bool newStyle) const |
| 5369 |
{ |
| 5370 |
if (all || _pcScope != LinkScope::Hidden) { |
| 5371 |
if (!subs) { |
| 5372 |
objs.reserve(objs.size() + _Links.size()); |
| 5373 |
for (auto& l : _Links) { |
| 5374 |
auto obj = l.getValue(); |
| 5375 |
if (obj && obj->isAttachedToDocument()) { |
| 5376 |
objs.push_back(obj); |
| 5377 |
} |
| 5378 |
} |
| 5379 |
return; |
| 5380 |
} |
| 5381 |
size_t count = 0; |
| 5382 |
for (auto& l : _Links) { |
| 5383 |
auto obj = l.getValue(); |
| 5384 |
if (obj && obj->isAttachedToDocument()) { |
| 5385 |
count += std::max((int)l.getSubValues().size(), 1); |
| 5386 |
} |
| 5387 |
} |
| 5388 |
if (!count) { |
| 5389 |
objs.reserve(objs.size() + _Links.size()); |
| 5390 |
for (auto& l : _Links) { |
| 5391 |
auto obj = l.getValue(); |
| 5392 |
if (obj && obj->isAttachedToDocument()) { |
| 5393 |
objs.push_back(obj); |
| 5394 |
} |
| 5395 |
} |
| 5396 |
return; |
| 5397 |
} |
| 5398 |
|
| 5399 |
objs.reserve(objs.size() + count); |
| 5400 |
subs->reserve(subs->size() + count); |
| 5401 |
for (auto& l : _Links) { |
| 5402 |
auto obj = l.getValue(); |
| 5403 |
if (obj && obj->isAttachedToDocument()) { |
| 5404 |
// we use to run this method everytime the program needed to access the sub-elements in |
| 5405 |
// a property link, but it caused multiple issues (#23441 and #23402) so it has been |
| 5406 |
// commented out. |
| 5407 |
// updateElementReferences(obj); |
| 5408 |
|
| 5409 |
auto subnames = l.getSubValues(newStyle); |
| 5410 |
if (subnames.empty()) { |
| 5411 |
subnames.emplace_back(""); |
| 5412 |
} |
| 5413 |
for (auto& sub : subnames) { |
| 5414 |
objs.push_back(obj); |
| 5415 |
subs->push_back(std::move(sub)); |
| 5416 |
} |
| 5417 |
} |
| 5418 |
} |
| 5419 |
} |
| 5420 |
} |
| 5421 |
|
| 5422 |
// Same algorithm as _getLinksTo above, but returns all matches |
| 5423 |
void PropertyXLinkSubList::_getLinksToList( |
| 5424 |
std::vector<App::ObjectIdentifier>& identifiers, |
| 5425 |
App::DocumentObject* obj, |
| 5426 |
const char* subname, |
| 5427 |
const std::vector<std::string>& subs, |
| 5428 |
const std::vector<PropertyLinkBase::ShadowSub>& shadows) const |
| 5429 |
{ |
| 5430 |
if (!subname) { |
| 5431 |
identifiers.emplace_back(*this); |
| 5432 |
return; |
| 5433 |
} |
| 5434 |
App::SubObjectT objT(obj, subname); |
| 5435 |
auto subObject = objT.getSubObject(); |
| 5436 |
auto subElement = objT.getOldElementName(); |
| 5437 |
|
| 5438 |
int i = -1; |
| 5439 |
for (const auto& sub : subs) { |
| 5440 |
++i; |
| 5441 |
if (sub == subname) { |
| 5442 |
identifiers.emplace_back(*this, i); |
| 5443 |
continue; |
| 5444 |
} |
| 5445 |
if (!subObject) { |
| 5446 |
continue; |
| 5447 |
} |
| 5448 |
// There is a subobject and the subname doesn't match our current entry |
| 5449 |
App::SubObjectT sobjT(obj, sub.c_str()); |
| 5450 |
if (sobjT.getSubObject() == subObject && sobjT.getOldElementName() == subElement) { |
| 5451 |
identifiers.emplace_back(*this, i); |
| 5452 |
continue; |
| 5453 |
} |
| 5454 |
// The oldElementName ( short, I.E. "Edge5" ) doesn't match. |
| 5455 |
if (i < (int)shadows.size()) { |
| 5456 |
const auto& [shadowNewName, shadowOldName] = shadows[i]; |
| 5457 |
if (shadowNewName == subname || shadowOldName == subname) { |
| 5458 |
identifiers.emplace_back(*this, i); |
| 5459 |
continue; |
| 5460 |
} |
| 5461 |
if (!subObject) { |
| 5462 |
continue; |
| 5463 |
} |
| 5464 |
App::SubObjectT shadowobjT(obj, |
| 5465 |
shadowNewName.empty() ? shadowOldName.c_str() |
| 5466 |
: shadowNewName.c_str()); |
| 5467 |
if (shadowobjT.getSubObject() == subObject |
| 5468 |
&& shadowobjT.getOldElementName() == subElement) { |
| 5469 |
identifiers.emplace_back(*this, i); |
| 5470 |
continue; |
| 5471 |
} |
| 5472 |
} |
| 5473 |
} |
| 5474 |
} |
| 5475 |
|
| 5476 |
void PropertyXLinkSubList::getLinksTo(std::vector<App::ObjectIdentifier>& identifiers, |
| 5477 |
App::DocumentObject* obj, |
| 5478 |
const char* subname, |
| 5479 |
bool all) const |
| 5480 |
{ |
| 5481 |
if (!all && _pcScope != LinkScope::Hidden) { |
| 5482 |
return; |
| 5483 |
} |
| 5484 |
for (auto& l : _Links) { |
| 5485 |
if (obj && obj == l._pcLink) { |
| 5486 |
_getLinksToList(identifiers, obj, subname, l._SubList, l._ShadowSubList); |
| 5487 |
} |
| 5488 |
} |
| 5489 |
} |
| 5490 |
|
| 5491 |
void PropertyXLinkSubList::breakLink(App::DocumentObject* obj, bool clear) |
| 5492 |
{ |
| 5493 |
if (clear && getContainer() == obj) { |
| 5494 |
setValue(nullptr); |
| 5495 |
return; |
| 5496 |
} |
| 5497 |
atomic_change guard(*this, false); |
| 5498 |
for (auto& l : _Links) { |
| 5499 |
if (l.getValue() == obj) { |
| 5500 |
guard.aboutToChange(); |
| 5501 |
l.setValue(nullptr); |
| 5502 |
} |
| 5503 |
} |
| 5504 |
guard.tryInvoke(); |
| 5505 |
} |
| 5506 |
|
| 5507 |
bool PropertyXLinkSubList::adjustLink(const std::set<App::DocumentObject*>& inList) |
| 5508 |
{ |
| 5509 |
if (_pcScope == LinkScope::Hidden) { |
| 5510 |
return false; |
| 5511 |
} |
| 5512 |
std::map<App::DocumentObject*, std::vector<std::string>> values; |
| 5513 |
bool touched = false; |
| 5514 |
int count = 0; |
| 5515 |
for (auto& l : _Links) { |
| 5516 |
auto obj = l.getValue(); |
| 5517 |
if (!obj || !obj->isAttachedToDocument()) { |
| 5518 |
++count; |
| 5519 |
continue; |
| 5520 |
} |
| 5521 |
if (inList.contains(obj) && adjustLinkSubs(this, inList, obj, l._SubList, &values)) { |
| 5522 |
touched = true; |
| 5523 |
} |
| 5524 |
} |
| 5525 |
if (touched) { |
| 5526 |
decltype(_Links) tmp; |
| 5527 |
if (count) { |
| 5528 |
// XLink allows detached state, i.e. with closed external document. So |
| 5529 |
// we need to preserve empty link |
| 5530 |
for (auto it = _Links.begin(), itNext = it; it != _Links.end(); it = itNext) { |
| 5531 |
++itNext; |
| 5532 |
if (!it->getValue()) { |
| 5533 |
tmp.splice(tmp.end(), _Links, it); |
| 5534 |
} |
| 5535 |
} |
| 5536 |
} |
| 5537 |
setValues(std::move(values)); |
| 5538 |
_Links.splice(_Links.end(), tmp); |
| 5539 |
} |
| 5540 |
return touched; |
| 5541 |
} |
| 5542 |
|
| 5543 |
int PropertyXLinkSubList::checkRestore(std::string* msg) const |
| 5544 |
{ |
| 5545 |
for (auto& l : _Links) { |
| 5546 |
int res; |
| 5547 |
if ((res = l.checkRestore(msg))) { |
| 5548 |
return res; |
| 5549 |
} |
| 5550 |
} |
| 5551 |
return 0; |
| 5552 |
} |
| 5553 |
|
| 5554 |
bool PropertyXLinkSubList::upgrade(Base::XMLReader& reader, const char* typeName) |
| 5555 |
{ |
| 5556 |
if (strcmp(typeName, PropertyLinkListGlobal::getClassTypeId().getName()) == 0 |
| 5557 |
|| strcmp(typeName, PropertyLinkList::getClassTypeId().getName()) == 0 |
| 5558 |
|| strcmp(typeName, PropertyLinkListChild::getClassTypeId().getName()) == 0) { |
| 5559 |
PropertyLinkList linkProp; |
| 5560 |
linkProp.setContainer(getContainer()); |
| 5561 |
linkProp.Restore(reader); |
| 5562 |
setValues(linkProp.getValues()); |
| 5563 |
return true; |
| 5564 |
} |
| 5565 |
else if (strcmp(typeName, PropertyLinkSubListGlobal::getClassTypeId().getName()) == 0 |
| 5566 |
|| strcmp(typeName, PropertyLinkSubList::getClassTypeId().getName()) == 0 |
| 5567 |
|| strcmp(typeName, PropertyLinkSubListChild::getClassTypeId().getName()) == 0) { |
| 5568 |
PropertyLinkSubList linkProp; |
| 5569 |
linkProp.setContainer(getContainer()); |
| 5570 |
linkProp.Restore(reader); |
| 5571 |
std::map<DocumentObject*, std::vector<std::string>> values; |
| 5572 |
const auto& objs = linkProp.getValues(); |
| 5573 |
const auto& subs = linkProp.getSubValues(); |
| 5574 |
assert(objs.size() == subs.size()); |
| 5575 |
for (size_t i = 0; i < objs.size(); ++i) { |
| 5576 |
values[objs[i]].push_back(subs[i]); |
| 5577 |
} |
| 5578 |
setValues(std::move(values)); |
| 5579 |
return true; |
| 5580 |
} |
| 5581 |
_Links.clear(); |
| 5582 |
_Links.emplace_back(testFlag(LinkAllowPartial), this); |
| 5583 |
if (!_Links.back().upgrade(reader, typeName)) { |
| 5584 |
_Links.clear(); |
| 5585 |
return false; |
| 5586 |
} |
| 5587 |
return true; |
| 5588 |
} |
| 5589 |
|
| 5590 |
void PropertyXLinkSubList::setAllowPartial(bool enable) |
| 5591 |
{ |
| 5592 |
setFlag(LinkAllowPartial, enable); |
| 5593 |
for (auto& l : _Links) { |
| 5594 |
l.setAllowPartial(enable); |
| 5595 |
} |
| 5596 |
} |
| 5597 |
|
| 5598 |
void PropertyXLinkSubList::hasSetChildValue(Property&) |
| 5599 |
{ |
| 5600 |
if (!signalCounter) { |
| 5601 |
hasSetValue(); |
| 5602 |
} |
| 5603 |
} |
| 5604 |
|
| 5605 |
void PropertyXLinkSubList::aboutToSetChildValue(Property&) |
| 5606 |
{ |
| 5607 |
if (!signalCounter || !hasChanged) { |
| 5608 |
aboutToSetValue(); |
| 5609 |
if (signalCounter) { |
| 5610 |
hasChanged = true; |
| 5611 |
} |
| 5612 |
} |
| 5613 |
} |
| 5614 |
|
| 5615 |
std::vector<App::DocumentObject*> PropertyXLinkSubList::getValues() const |
| 5616 |
{ |
| 5617 |
std::vector<DocumentObject*> xLinks; |
| 5618 |
getLinks(xLinks); |
| 5619 |
return (xLinks); |
| 5620 |
} |
| 5621 |
|
| 5622 |
//************************************************************************** |
| 5623 |
// PropertyXLinkList |
| 5624 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 5625 |
|
| 5626 |
TYPESYSTEM_SOURCE(App::PropertyXLinkList, App::PropertyXLinkSubList) |
| 5627 |
|
| 5628 |
//************************************************************************** |
| 5629 |
// Construction/Destruction |
| 5630 |
|
| 5631 |
PropertyXLinkList::PropertyXLinkList() = default; |
| 5632 |
|
| 5633 |
PropertyXLinkList::~PropertyXLinkList() = default; |
| 5634 |
|
| 5635 |
PyObject* PropertyXLinkList::getPyObject() |
| 5636 |
{ |
| 5637 |
for (auto& link : _Links) { |
| 5638 |
auto obj = link.getValue(); |
| 5639 |
if (!obj || !obj->isAttachedToDocument()) { |
| 5640 |
continue; |
| 5641 |
} |
| 5642 |
if (link.hasSubName()) { |
| 5643 |
return PropertyXLinkSubList::getPyObject(); |
| 5644 |
} |
| 5645 |
} |
| 5646 |
|
| 5647 |
Py::List list; |
| 5648 |
for (auto& link : _Links) { |
| 5649 |
auto obj = link.getValue(); |
| 5650 |
if (!obj || !obj->isAttachedToDocument()) { |
| 5651 |
continue; |
| 5652 |
} |
| 5653 |
list.append(Py::asObject(obj->getPyObject())); |
| 5654 |
} |
| 5655 |
return Py::new_reference_to(list); |
| 5656 |
} |
| 5657 |
|
| 5658 |
void PropertyXLinkList::setPyObject(PyObject* value) |
| 5659 |
{ |
| 5660 |
try { // try PropertyLinkList syntax |
| 5661 |
PropertyLinkList dummy; |
| 5662 |
dummy.setAllowExternal(true); |
| 5663 |
dummy.setPyObject(value); |
| 5664 |
this->setValues(dummy.getValues()); |
| 5665 |
return; |
| 5666 |
} |
| 5667 |
catch (Base::Exception&) { |
| 5668 |
} |
| 5669 |
|
| 5670 |
PropertyXLinkSubList::setPyObject(value); |
| 5671 |
} |
| 5672 |
|
| 5673 |
//************************************************************************** |
| 5674 |
// PropertyXLinkContainer |
| 5675 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 5676 |
TYPESYSTEM_SOURCE_ABSTRACT(App::PropertyXLinkContainer, App::PropertyLinkBase) |
| 5677 |
|
| 5678 |
PropertyXLinkContainer::PropertyXLinkContainer() |
| 5679 |
{ |
| 5680 |
_pcScope = LinkScope::Global; |
| 5681 |
_LinkRestored = false; |
| 5682 |
} |
| 5683 |
|
| 5684 |
PropertyXLinkContainer::~PropertyXLinkContainer() = default; |
| 5685 |
|
| 5686 |
void PropertyXLinkContainer::afterRestore() |
| 5687 |
{ |
| 5688 |
_DocMap.clear(); |
| 5689 |
if (!_XLinkRestores) { |
| 5690 |
return; |
| 5691 |
} |
| 5692 |
_Deps.clear(); |
| 5693 |
for (auto& info : *_XLinkRestores) { |
| 5694 |
auto obj = info.xlink->getValue(); |
| 5695 |
if (!obj) { |
| 5696 |
continue; |
| 5697 |
} |
| 5698 |
if (!info.docName.empty()) { |
| 5699 |
if (info.docName != obj->getDocument()->getName()) { |
| 5700 |
_DocMap[info.docName] = obj->getDocument()->getName(); |
| 5701 |
} |
| 5702 |
if (info.docLabel != obj->getDocument()->Label.getValue()) { |
| 5703 |
_DocMap[App::quote(info.docLabel)] = obj->getDocument()->Label.getValue(); |
| 5704 |
} |
| 5705 |
} |
| 5706 |
if (_Deps.insert(std::make_pair(obj, info.xlink->getScope() == LinkScope::Hidden)).second) { |
| 5707 |
_XLinks[obj->getFullName()] = std::move(info.xlink); |
| 5708 |
onAddDep(obj); |
| 5709 |
} |
| 5710 |
} |
| 5711 |
_XLinkRestores.reset(); |
| 5712 |
} |
| 5713 |
|
| 5714 |
void PropertyXLinkContainer::breakLink(App::DocumentObject* obj, bool clear) |
| 5715 |
{ |
| 5716 |
if (!obj || !obj->isAttachedToDocument()) { |
| 5717 |
return; |
| 5718 |
} |
| 5719 |
auto owner = dynamic_cast<App::DocumentObject*>(getContainer()); |
| 5720 |
if (!owner || !owner->isAttachedToDocument()) { |
| 5721 |
return; |
| 5722 |
} |
| 5723 |
if (!clear || obj != owner) { |
| 5724 |
auto it = _Deps.find(obj); |
| 5725 |
if (it == _Deps.end()) { |
| 5726 |
return; |
| 5727 |
} |
| 5728 |
aboutToSetValue(); |
| 5729 |
onBreakLink(obj); |
| 5730 |
if (obj->getDocument() != owner->getDocument()) { |
| 5731 |
_XLinks.erase(obj->getFullName()); |
| 5732 |
} |
| 5733 |
else if (!it->second) { |
| 5734 |
obj->_removeBackLink(owner); |
| 5735 |
} |
| 5736 |
_Deps.erase(it); |
| 5737 |
onRemoveDep(obj); |
| 5738 |
hasSetValue(); |
| 5739 |
return; |
| 5740 |
} |
| 5741 |
if (obj != owner) { |
| 5742 |
return; |
| 5743 |
} |
| 5744 |
for (auto& v : _Deps) { |
| 5745 |
auto key = v.first; |
| 5746 |
if (!key || !key->isAttachedToDocument()) { |
| 5747 |
continue; |
| 5748 |
} |
| 5749 |
onBreakLink(key); |
| 5750 |
if (!v.second && key->getDocument() == owner->getDocument()) { |
| 5751 |
key->_removeBackLink(owner); |
| 5752 |
} |
| 5753 |
} |
| 5754 |
_XLinks.clear(); |
| 5755 |
_Deps.clear(); |
| 5756 |
} |
| 5757 |
|
| 5758 |
int PropertyXLinkContainer::checkRestore(std::string* msg) const |
| 5759 |
{ |
| 5760 |
if (_LinkRestored) { |
| 5761 |
for (auto& v : _XLinks) { |
| 5762 |
int res = v.second->checkRestore(msg); |
| 5763 |
if (res) { |
| 5764 |
return res; |
| 5765 |
} |
| 5766 |
} |
| 5767 |
} |
| 5768 |
return 0; |
| 5769 |
} |
| 5770 |
|
| 5771 |
void PropertyXLinkContainer::Save(Base::Writer& writer) const |
| 5772 |
{ |
| 5773 |
|
| 5774 |
writer.Stream() << writer.ind() << "<XLinks count=\"" << _XLinks.size(); |
| 5775 |
|
| 5776 |
std::map<App::Document*, int> docSet; |
| 5777 |
auto owner = freecad_cast<App::DocumentObject*>(getContainer()); |
| 5778 |
if (owner && !owner->isExporting()) { |
| 5779 |
// Document name and label can change on restore, we shall record the |
| 5780 |
// current document name and label and pair it with the associated |
| 5781 |
// xlink, so that we can restore them correctly. |
| 5782 |
int i = -1; |
| 5783 |
for (auto& v : _XLinks) { |
| 5784 |
++i; |
| 5785 |
auto obj = v.second->getValue(); |
| 5786 |
if (obj && obj->getDocument()) { |
| 5787 |
docSet.insert(std::make_pair(obj->getDocument(), i)); |
| 5788 |
} |
| 5789 |
} |
| 5790 |
|
| 5791 |
if (!docSet.empty()) { |
| 5792 |
writer.Stream() << "\" docs=\"" << docSet.size(); |
| 5793 |
} |
| 5794 |
} |
| 5795 |
|
| 5796 |
std::ostringstream ss; |
| 5797 |
int hidden = 0; |
| 5798 |
int i = -1; |
| 5799 |
for (auto& v : _XLinks) { |
| 5800 |
++i; |
| 5801 |
if (v.second->getScope() == LinkScope::Hidden) { |
| 5802 |
ss << i << ' '; |
| 5803 |
++hidden; |
| 5804 |
} |
| 5805 |
} |
| 5806 |
if (hidden) { |
| 5807 |
writer.Stream() << "\" hidden=\"" << ss.str(); |
| 5808 |
} |
| 5809 |
|
| 5810 |
writer.Stream() << "\">" << std::endl; |
| 5811 |
writer.incInd(); |
| 5812 |
|
| 5813 |
for (auto& v : docSet) { |
| 5814 |
writer.Stream() << writer.ind() << "<DocMap " |
| 5815 |
<< "name=\"" << v.first->getName() << "\" label=\"" |
| 5816 |
<< encodeAttribute(v.first->Label.getValue()) << "\" index=\"" << v.second |
| 5817 |
<< "\"/>" << std::endl; |
| 5818 |
} |
| 5819 |
|
| 5820 |
for (auto& v : _XLinks) { |
| 5821 |
v.second->Save(writer); |
| 5822 |
} |
| 5823 |
writer.decInd(); |
| 5824 |
|
| 5825 |
writer.Stream() << writer.ind() << "</XLinks>" << std::endl; |
| 5826 |
} |
| 5827 |
|
| 5828 |
void PropertyXLinkContainer::Restore(Base::XMLReader& reader) |
| 5829 |
{ |
| 5830 |
reader.readElement("XLinks"); |
| 5831 |
auto count = reader.getAttribute<unsigned long>("count"); |
| 5832 |
_XLinkRestores = std::make_unique<std::vector<RestoreInfo>>(count); |
| 5833 |
|
| 5834 |
if (reader.hasAttribute("hidden")) { |
| 5835 |
std::istringstream iss(reader.getAttribute<const char*>("hidden")); |
| 5836 |
int index; |
| 5837 |
while (iss >> index) { |
| 5838 |
if (index >= 0 && index < static_cast<int>(count)) { |
| 5839 |
_XLinkRestores->at(index).hidden = true; |
| 5840 |
} |
| 5841 |
} |
| 5842 |
} |
| 5843 |
|
| 5844 |
if (reader.hasAttribute("docs")) { |
| 5845 |
auto docCount = reader.getAttribute<unsigned long>("docs"); |
| 5846 |
_DocMap.clear(); |
| 5847 |
for (unsigned i = 0; i < docCount; ++i) { |
| 5848 |
reader.readElement("DocMap"); |
| 5849 |
auto index = reader.getAttribute<unsigned long>("index"); |
| 5850 |
if (index >= count) { |
| 5851 |
FC_ERR(propertyName(this) << " invalid document map entry"); |
| 5852 |
continue; |
| 5853 |
} |
| 5854 |
auto& info = _XLinkRestores->at(index); |
| 5855 |
info.docName = reader.getAttribute<const char*>("name"); |
| 5856 |
info.docLabel = reader.getAttribute<const char*>("label"); |
| 5857 |
} |
| 5858 |
} |
| 5859 |
|
| 5860 |
for (auto& info : *_XLinkRestores) { |
| 5861 |
info.xlink.reset(createXLink()); |
| 5862 |
if (info.hidden) { |
| 5863 |
info.xlink->setScope(LinkScope::Hidden); |
| 5864 |
} |
| 5865 |
info.xlink->Restore(reader); |
| 5866 |
} |
| 5867 |
reader.readEndElement("XLinks"); |
| 5868 |
} |
| 5869 |
|
| 5870 |
void PropertyXLinkContainer::aboutToSetChildValue(App::Property& prop) |
| 5871 |
{ |
| 5872 |
auto xlink = dynamic_cast<App::PropertyXLink*>(&prop); |
| 5873 |
if (xlink && xlink->testFlag(LinkDetached)) { |
| 5874 |
auto obj = const_cast<App::DocumentObject*>(xlink->getValue()); |
| 5875 |
if (_Deps.erase(obj)) { |
| 5876 |
_onBreakLink(xlink->getValue()); |
| 5877 |
onRemoveDep(obj); |
| 5878 |
} |
| 5879 |
} |
| 5880 |
} |
| 5881 |
|
| 5882 |
void PropertyXLinkContainer::onBreakLink(DocumentObject*) |
| 5883 |
{} |
| 5884 |
|
| 5885 |
void PropertyXLinkContainer::_onBreakLink(DocumentObject* obj) |
| 5886 |
{ |
| 5887 |
try { |
| 5888 |
onBreakLink(obj); |
| 5889 |
} |
| 5890 |
catch (Base::Exception& e) { |
| 5891 |
e.reportException(); |
| 5892 |
FC_ERR("Exception on breaking link property " << getFullName()); |
| 5893 |
} |
| 5894 |
catch (std::exception& e) { |
| 5895 |
FC_ERR("Exception on breaking link property " << getFullName() << ": " << e.what()); |
| 5896 |
} |
| 5897 |
catch (...) { |
| 5898 |
FC_ERR("Exception on breaking link property " << getFullName()); |
| 5899 |
} |
| 5900 |
} |
| 5901 |
|
| 5902 |
PropertyXLink* PropertyXLinkContainer::createXLink() |
| 5903 |
{ |
| 5904 |
return new PropertyXLink(false, this); |
| 5905 |
} |
| 5906 |
|
| 5907 |
bool PropertyXLinkContainer::isLinkedToDocument(const App::Document& doc) const |
| 5908 |
{ |
| 5909 |
auto iter = _XLinks.lower_bound(doc.getName()); |
| 5910 |
if (iter != _XLinks.end()) { |
| 5911 |
size_t len = strlen(doc.getName()); |
| 5912 |
return iter->first.size() > len && iter->first[len] == '#' |
| 5913 |
&& boost::starts_with(iter->first, doc.getName()); |
| 5914 |
} |
| 5915 |
return false; |
| 5916 |
} |
| 5917 |
|
| 5918 |
void PropertyXLinkContainer::updateDeps(std::map<DocumentObject*, bool>&& newDeps) |
| 5919 |
{ |
| 5920 |
auto owner = freecad_cast<App::DocumentObject*>(getContainer()); |
| 5921 |
if (!owner || !owner->isAttachedToDocument()) { |
| 5922 |
return; |
| 5923 |
} |
| 5924 |
newDeps.erase(owner); |
| 5925 |
|
| 5926 |
for (auto& v : newDeps) { |
| 5927 |
auto obj = v.first; |
| 5928 |
if (obj && obj->isAttachedToDocument()) { |
| 5929 |
auto it = _Deps.find(obj); |
| 5930 |
if (it != _Deps.end()) { |
| 5931 |
if (v.second != it->second) { |
| 5932 |
if (v.second) { |
| 5933 |
obj->_removeBackLink(owner); |
| 5934 |
} |
| 5935 |
else { |
| 5936 |
obj->_addBackLink(owner); |
| 5937 |
} |
| 5938 |
} |
| 5939 |
_Deps.erase(it); |
| 5940 |
continue; |
| 5941 |
} |
| 5942 |
if (owner->getDocument() != obj->getDocument()) { |
| 5943 |
auto& xlink = _XLinks[obj->getFullName()]; |
| 5944 |
if (!xlink) { |
| 5945 |
xlink.reset(createXLink()); |
| 5946 |
xlink->setValue(obj); |
| 5947 |
} |
| 5948 |
xlink->setScope(v.second ? LinkScope::Hidden : LinkScope::Global); |
| 5949 |
} |
| 5950 |
else if (!v.second) { |
| 5951 |
obj->_addBackLink(owner); |
| 5952 |
} |
| 5953 |
|
| 5954 |
onAddDep(obj); |
| 5955 |
} |
| 5956 |
} |
| 5957 |
for (auto& v : _Deps) { |
| 5958 |
auto obj = v.first; |
| 5959 |
if (!obj || !obj->isAttachedToDocument()) { |
| 5960 |
continue; |
| 5961 |
} |
| 5962 |
if (obj->getDocument() == owner->getDocument()) { |
| 5963 |
if (!v.second) { |
| 5964 |
obj->_removeBackLink(owner); |
| 5965 |
} |
| 5966 |
} |
| 5967 |
else { |
| 5968 |
_XLinks.erase(obj->getFullName()); |
| 5969 |
} |
| 5970 |
onRemoveDep(obj); |
| 5971 |
} |
| 5972 |
_Deps = std::move(newDeps); |
| 5973 |
|
| 5974 |
_LinkRestored = testFlag(LinkRestoring); |
| 5975 |
|
| 5976 |
if (!_LinkRestored && !testFlag(LinkDetached)) { |
| 5977 |
for (auto it = _XLinks.begin(), itNext = it; it != _XLinks.end(); it = itNext) { |
| 5978 |
++itNext; |
| 5979 |
if (!it->second->getValue()) { |
| 5980 |
_XLinks.erase(it); |
| 5981 |
} |
| 5982 |
} |
| 5983 |
} |
| 5984 |
} |
| 5985 |
|
| 5986 |
void PropertyXLinkContainer::clearDeps() |
| 5987 |
{ |
| 5988 |
auto owner = dynamic_cast<App::DocumentObject*>(getContainer()); |
| 5989 |
if (!owner || !owner->isAttachedToDocument()) { |
| 5990 |
return; |
| 5991 |
} |
| 5992 |
|
| 5993 |
if (!owner->testStatus(ObjectStatus::Destroy)) { |
| 5994 |
for (auto& v : _Deps) { |
| 5995 |
auto obj = v.first; |
| 5996 |
if (!v.second && obj && obj->isAttachedToDocument() |
| 5997 |
&& obj->getDocument() == owner->getDocument()) { |
| 5998 |
obj->_removeBackLink(owner); |
| 5999 |
} |
| 6000 |
} |
| 6001 |
} |
| 6002 |
|
| 6003 |
_Deps.clear(); |
| 6004 |
_XLinks.clear(); |
| 6005 |
_LinkRestored = false; |
| 6006 |
} |
| 6007 |
|
| 6008 |
void PropertyXLinkContainer::getLinks(std::vector<App::DocumentObject*>& objs, |
| 6009 |
bool all, |
| 6010 |
std::vector<std::string>* /*subs*/, |
| 6011 |
bool /*newStyle*/) const |
| 6012 |
{ |
| 6013 |
for (auto& v : _Deps) { |
| 6014 |
if (all || !v.second) { |
| 6015 |
objs.push_back(v.first); |
| 6016 |
} |
| 6017 |
} |
| 6018 |
} |