| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <Base/Tools.h> |
| 24 |
|
| 25 |
#include "Application.h" |
| 26 |
#include "ElementNamingUtils.h" |
| 27 |
#include "Document.h" |
| 28 |
#include "DocumentObserver.h" |
| 29 |
#include "GeoFeature.h" |
| 30 |
#include "Link.h" |
| 31 |
|
| 32 |
using namespace App; |
| 33 |
namespace sp = std::placeholders; |
| 34 |
|
| 35 |
|
| 36 |
DocumentT::DocumentT() = default; |
| 37 |
|
| 38 |
DocumentT::DocumentT(Document* doc) |
| 39 |
{ |
| 40 |
document = doc->getName(); |
| 41 |
} |
| 42 |
|
| 43 |
DocumentT::DocumentT(const std::string& name) |
| 44 |
{ |
| 45 |
document = name; |
| 46 |
} |
| 47 |
|
| 48 |
DocumentT::DocumentT(const DocumentT& doc) |
| 49 |
{ |
| 50 |
document = doc.document; |
| 51 |
} |
| 52 |
|
| 53 |
DocumentT::~DocumentT() = default; |
| 54 |
|
| 55 |
void DocumentT::operator=(const DocumentT& doc) |
| 56 |
{ |
| 57 |
if (this == &doc) { |
| 58 |
return; |
| 59 |
} |
| 60 |
document = doc.document; |
| 61 |
} |
| 62 |
|
| 63 |
void DocumentT::operator=(const Document* doc) |
| 64 |
{ |
| 65 |
document = doc->getName(); |
| 66 |
} |
| 67 |
|
| 68 |
void DocumentT::operator=(const std::string& name) |
| 69 |
{ |
| 70 |
document = name; |
| 71 |
} |
| 72 |
|
| 73 |
Document* DocumentT::getDocument() const |
| 74 |
{ |
| 75 |
return GetApplication().getDocument(document.c_str()); |
| 76 |
} |
| 77 |
|
| 78 |
const std::string& DocumentT::getDocumentName() const |
| 79 |
{ |
| 80 |
return document; |
| 81 |
} |
| 82 |
|
| 83 |
std::string DocumentT::getDocumentPython() const |
| 84 |
{ |
| 85 |
std::stringstream str; |
| 86 |
str << "App.getDocument(\"" << document << "\")"; |
| 87 |
return str.str(); |
| 88 |
} |
| 89 |
|
| 90 |
// ----------------------------------------------------------------------------- |
| 91 |
|
| 92 |
DocumentObjectT::DocumentObjectT() = default; |
| 93 |
|
| 94 |
DocumentObjectT::DocumentObjectT(const DocumentObjectT& other) |
| 95 |
{ |
| 96 |
*this = other; |
| 97 |
} |
| 98 |
|
| 99 |
DocumentObjectT::DocumentObjectT(DocumentObjectT&& other) |
| 100 |
{ |
| 101 |
*this = std::move(other); |
| 102 |
} |
| 103 |
|
| 104 |
DocumentObjectT::DocumentObjectT(const DocumentObject* obj) |
| 105 |
{ |
| 106 |
*this = obj; |
| 107 |
} |
| 108 |
|
| 109 |
DocumentObjectT::DocumentObjectT(const Property* prop) |
| 110 |
{ |
| 111 |
*this = prop; |
| 112 |
} |
| 113 |
|
| 114 |
DocumentObjectT::DocumentObjectT(const Document* doc, const std::string& objName) |
| 115 |
{ |
| 116 |
if (doc && doc->getName()) { |
| 117 |
document = doc->getName(); |
| 118 |
} |
| 119 |
object = objName; |
| 120 |
} |
| 121 |
|
| 122 |
DocumentObjectT::DocumentObjectT(const char* docName, const char* objName) |
| 123 |
{ |
| 124 |
if (docName) { |
| 125 |
document = docName; |
| 126 |
} |
| 127 |
if (objName) { |
| 128 |
object = objName; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
DocumentObjectT::~DocumentObjectT() = default; |
| 133 |
|
| 134 |
DocumentObjectT& DocumentObjectT::operator=(const DocumentObjectT& obj) |
| 135 |
{ |
| 136 |
if (this == &obj) { |
| 137 |
return *this; |
| 138 |
} |
| 139 |
object = obj.object; |
| 140 |
label = obj.label; |
| 141 |
document = obj.document; |
| 142 |
property = obj.property; |
| 143 |
return *this; |
| 144 |
} |
| 145 |
|
| 146 |
DocumentObjectT& DocumentObjectT::operator=(DocumentObjectT&& obj) |
| 147 |
{ |
| 148 |
if (this == &obj) { |
| 149 |
return *this; |
| 150 |
} |
| 151 |
object = std::move(obj.object); |
| 152 |
label = std::move(obj.label); |
| 153 |
document = std::move(obj.document); |
| 154 |
property = std::move(obj.property); |
| 155 |
return *this; |
| 156 |
} |
| 157 |
|
| 158 |
void DocumentObjectT::operator=(const DocumentObject* obj) |
| 159 |
{ |
| 160 |
if (!obj || !obj->isAttachedToDocument()) { |
| 161 |
object.clear(); |
| 162 |
label.clear(); |
| 163 |
document.clear(); |
| 164 |
property.clear(); |
| 165 |
} |
| 166 |
else { |
| 167 |
object = obj->getNameInDocument(); |
| 168 |
label = obj->Label.getValue(); |
| 169 |
document = obj->getDocument()->getName(); |
| 170 |
property.clear(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void DocumentObjectT::operator=(const Property* prop) |
| 175 |
{ |
| 176 |
if (!prop || !prop->hasName() || !prop->getContainer() |
| 177 |
|| !prop->getContainer()->isDerivedFrom<App::DocumentObject>()) { |
| 178 |
object.clear(); |
| 179 |
label.clear(); |
| 180 |
document.clear(); |
| 181 |
property.clear(); |
| 182 |
} |
| 183 |
else { |
| 184 |
auto obj = static_cast<App::DocumentObject*>(prop->getContainer()); |
| 185 |
object = obj->getNameInDocument(); |
| 186 |
label = obj->Label.getValue(); |
| 187 |
document = obj->getDocument()->getName(); |
| 188 |
property = prop->getName(); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
bool DocumentObjectT::operator==(const DocumentObjectT& other) const |
| 193 |
{ |
| 194 |
return document == other.document && object == other.object && label == other.label |
| 195 |
&& property == other.property; |
| 196 |
} |
| 197 |
|
| 198 |
Document* DocumentObjectT::getDocument() const |
| 199 |
{ |
| 200 |
return GetApplication().getDocument(document.c_str()); |
| 201 |
} |
| 202 |
|
| 203 |
const std::string& DocumentObjectT::getDocumentName() const |
| 204 |
{ |
| 205 |
return document; |
| 206 |
} |
| 207 |
|
| 208 |
std::string DocumentObjectT::getDocumentPython() const |
| 209 |
{ |
| 210 |
std::stringstream str; |
| 211 |
str << "FreeCAD.getDocument(\"" << document << "\")"; |
| 212 |
return str.str(); |
| 213 |
} |
| 214 |
|
| 215 |
DocumentObject* DocumentObjectT::getObject() const |
| 216 |
{ |
| 217 |
DocumentObject* obj = nullptr; |
| 218 |
Document* doc = getDocument(); |
| 219 |
if (doc) { |
| 220 |
obj = doc->getObject(object.c_str()); |
| 221 |
} |
| 222 |
return obj; |
| 223 |
} |
| 224 |
|
| 225 |
const std::string& DocumentObjectT::getObjectName() const |
| 226 |
{ |
| 227 |
return object; |
| 228 |
} |
| 229 |
|
| 230 |
const std::string& DocumentObjectT::getObjectLabel() const |
| 231 |
{ |
| 232 |
return label; |
| 233 |
} |
| 234 |
|
| 235 |
std::string DocumentObjectT::getObjectPython() const |
| 236 |
{ |
| 237 |
std::stringstream str; |
| 238 |
str << "FreeCAD.getDocument('" << document << "').getObject('" << object << "')"; |
| 239 |
return str.str(); |
| 240 |
} |
| 241 |
|
| 242 |
const std::string& DocumentObjectT::getPropertyName() const |
| 243 |
{ |
| 244 |
return property; |
| 245 |
} |
| 246 |
|
| 247 |
std::string DocumentObjectT::getPropertyPython() const |
| 248 |
{ |
| 249 |
std::stringstream str; |
| 250 |
str << getObjectPython(); |
| 251 |
if (!property.empty()) { |
| 252 |
str << '.' << property; |
| 253 |
} |
| 254 |
return str.str(); |
| 255 |
} |
| 256 |
|
| 257 |
Property* DocumentObjectT::getProperty() const |
| 258 |
{ |
| 259 |
auto obj = getObject(); |
| 260 |
if (obj) { |
| 261 |
return obj->getPropertyByName(property.c_str()); |
| 262 |
} |
| 263 |
return nullptr; |
| 264 |
} |
| 265 |
|
| 266 |
// ----------------------------------------------------------------------------- |
| 267 |
|
| 268 |
SubObjectT::SubObjectT() = default; |
| 269 |
|
| 270 |
SubObjectT::SubObjectT(const SubObjectT&) = default; |
| 271 |
|
| 272 |
SubObjectT::SubObjectT(SubObjectT&& other) |
| 273 |
: DocumentObjectT(std::move(other)) |
| 274 |
, subname(std::move(other.subname)) |
| 275 |
{} |
| 276 |
|
| 277 |
SubObjectT::SubObjectT(const DocumentObject* obj, const char* s) |
| 278 |
: DocumentObjectT(obj) |
| 279 |
, subname(s ? s : "") |
| 280 |
{} |
| 281 |
|
| 282 |
SubObjectT::SubObjectT(const DocumentObject* obj) |
| 283 |
: DocumentObjectT(obj) |
| 284 |
{} |
| 285 |
|
| 286 |
SubObjectT::SubObjectT(const DocumentObjectT& obj, const char* s) |
| 287 |
: DocumentObjectT(obj) |
| 288 |
, subname(s ? s : "") |
| 289 |
{} |
| 290 |
|
| 291 |
SubObjectT::SubObjectT(const char* docName, const char* objName, const char* s) |
| 292 |
: DocumentObjectT(docName, objName) |
| 293 |
, subname(s ? s : "") |
| 294 |
{} |
| 295 |
|
| 296 |
bool SubObjectT::operator<(const SubObjectT& other) const |
| 297 |
{ |
| 298 |
if (getDocumentName() < other.getDocumentName()) { |
| 299 |
return true; |
| 300 |
} |
| 301 |
if (getDocumentName() > other.getDocumentName()) { |
| 302 |
return false; |
| 303 |
} |
| 304 |
if (getObjectName() < other.getObjectName()) { |
| 305 |
return true; |
| 306 |
} |
| 307 |
if (getObjectName() > other.getObjectName()) { |
| 308 |
return false; |
| 309 |
} |
| 310 |
if (getSubName() < other.getSubName()) { |
| 311 |
return true; |
| 312 |
} |
| 313 |
if (getSubName() > other.getSubName()) { |
| 314 |
return false; |
| 315 |
} |
| 316 |
return getPropertyName() < other.getPropertyName(); |
| 317 |
} |
| 318 |
|
| 319 |
SubObjectT& SubObjectT::operator=(const SubObjectT& other) |
| 320 |
{ |
| 321 |
if (this == &other) { |
| 322 |
return *this; |
| 323 |
} |
| 324 |
static_cast<DocumentObjectT&>(*this) = other; |
| 325 |
subname = other.subname; |
| 326 |
return *this; |
| 327 |
} |
| 328 |
|
| 329 |
SubObjectT& SubObjectT::operator=(SubObjectT&& other) |
| 330 |
{ |
| 331 |
if (this == &other) { |
| 332 |
return *this; |
| 333 |
} |
| 334 |
static_cast<DocumentObjectT&>(*this) = std::move(other); |
| 335 |
subname = std::move(other.subname); |
| 336 |
return *this; |
| 337 |
} |
| 338 |
|
| 339 |
SubObjectT& SubObjectT::operator=(const DocumentObjectT& other) |
| 340 |
{ |
| 341 |
if (this == &other) { |
| 342 |
return *this; |
| 343 |
} |
| 344 |
static_cast<DocumentObjectT&>(*this) = other; |
| 345 |
subname.clear(); |
| 346 |
return *this; |
| 347 |
} |
| 348 |
|
| 349 |
SubObjectT& SubObjectT::operator=(const DocumentObject* other) |
| 350 |
{ |
| 351 |
static_cast<DocumentObjectT&>(*this) = other; |
| 352 |
subname.clear(); |
| 353 |
return *this; |
| 354 |
} |
| 355 |
|
| 356 |
bool SubObjectT::operator==(const SubObjectT& other) const |
| 357 |
{ |
| 358 |
return static_cast<const DocumentObjectT&>(*this) == other && subname == other.subname; |
| 359 |
} |
| 360 |
|
| 361 |
namespace |
| 362 |
{ |
| 363 |
bool normalizeConvertIndex(const std::vector<App::DocumentObject*>& objs, const unsigned int idx) |
| 364 |
{ |
| 365 |
if (auto ext = objs[idx - 1]->getExtensionByType<App::LinkBaseExtension>(true)) { |
| 366 |
if ((ext->getElementCountValue() != 0) && !ext->getShowElementValue()) { |
| 367 |
// If the parent is a collapsed link array element, then we |
| 368 |
// have to keep the index no matter what, because there is |
| 369 |
// no sub-object corresponding to an array element. |
| 370 |
return true; |
| 371 |
} |
| 372 |
} |
| 373 |
return false; |
| 374 |
} |
| 375 |
} // namespace |
| 376 |
|
| 377 |
bool SubObjectT::normalize(NormalizeOptions options) |
| 378 |
{ |
| 379 |
bool noElement = options.testFlag(NormalizeOption::NoElement); |
| 380 |
bool flatten = !options.testFlag(NormalizeOption::NoFlatten); |
| 381 |
bool keepSub = options.testFlag(NormalizeOption::KeepSubName); |
| 382 |
bool convertIndex = options.testFlag(NormalizeOption::ConvertIndex); |
| 383 |
|
| 384 |
std::ostringstream ss; |
| 385 |
std::vector<int> subs; |
| 386 |
auto obj = getObject(); |
| 387 |
if (!obj) { |
| 388 |
return false; |
| 389 |
} |
| 390 |
auto objs = obj->getSubObjectList(subname.c_str(), &subs, flatten); |
| 391 |
if (objs.empty()) { |
| 392 |
return false; |
| 393 |
} |
| 394 |
for (unsigned i = 1; i < objs.size(); ++i) { |
| 395 |
// Keep digit-only subname, as it maybe an index to an array, which does |
| 396 |
// not expand its elements as objects. |
| 397 |
const char* end = subname.c_str() + subs[i]; |
| 398 |
const char* sub = end - 2; |
| 399 |
for (;; --sub) { |
| 400 |
if (sub < subname.c_str()) { |
| 401 |
sub = subname.c_str(); |
| 402 |
break; |
| 403 |
} |
| 404 |
if (*sub == '.') { |
| 405 |
++sub; |
| 406 |
break; |
| 407 |
} |
| 408 |
} |
| 409 |
bool _keepSub {}; |
| 410 |
if (!std::isdigit(sub[0])) { |
| 411 |
_keepSub = keepSub; |
| 412 |
} |
| 413 |
else if (!convertIndex) { |
| 414 |
_keepSub = true; |
| 415 |
} |
| 416 |
else { |
| 417 |
_keepSub = normalizeConvertIndex(objs, i); |
| 418 |
} |
| 419 |
if (_keepSub) { |
| 420 |
ss << std::string(sub, end); |
| 421 |
} |
| 422 |
else { |
| 423 |
ss << objs[i]->getNameInDocument() << "."; |
| 424 |
} |
| 425 |
} |
| 426 |
if (objs.size() > 1 && objs.front()->getSubObject(ss.str().c_str()) != objs.back()) { |
| 427 |
// something went wrong |
| 428 |
return false; |
| 429 |
} |
| 430 |
if (!noElement) { |
| 431 |
ss << getOldElementName(); |
| 432 |
} |
| 433 |
std::string sub = ss.str(); |
| 434 |
if (objs.front() != obj || subname != sub) { |
| 435 |
*this = objs.front(); |
| 436 |
subname = std::move(sub); |
| 437 |
return true; |
| 438 |
} |
| 439 |
return false; |
| 440 |
} |
| 441 |
|
| 442 |
SubObjectT App::SubObjectT::normalized(NormalizeOptions options) const |
| 443 |
{ |
| 444 |
SubObjectT res(*this); |
| 445 |
res.normalize(options); |
| 446 |
return res; |
| 447 |
} |
| 448 |
|
| 449 |
void SubObjectT::setSubName(const char* s) |
| 450 |
{ |
| 451 |
subname = s ? s : ""; |
| 452 |
} |
| 453 |
|
| 454 |
const std::string& SubObjectT::getSubName() const |
| 455 |
{ |
| 456 |
return subname; |
| 457 |
} |
| 458 |
|
| 459 |
std::string SubObjectT::getSubNameNoElement() const |
| 460 |
{ |
| 461 |
return Data::noElementName(subname.c_str()); |
| 462 |
} |
| 463 |
|
| 464 |
const char* SubObjectT::getElementName() const |
| 465 |
{ |
| 466 |
return Data::findElementName(subname.c_str()); |
| 467 |
} |
| 468 |
|
| 469 |
bool SubObjectT::hasSubObject() const |
| 470 |
{ |
| 471 |
return Data::findElementName(subname.c_str()) != subname.c_str(); |
| 472 |
} |
| 473 |
|
| 474 |
bool SubObjectT::hasSubElement() const |
| 475 |
{ |
| 476 |
auto element = getElementName(); |
| 477 |
return element && (element[0] != '\0'); |
| 478 |
} |
| 479 |
|
| 480 |
std::string SubObjectT::getNewElementName() const |
| 481 |
{ |
| 482 |
ElementNamePair element; |
| 483 |
auto obj = getObject(); |
| 484 |
if (!obj) { |
| 485 |
return {}; |
| 486 |
} |
| 487 |
GeoFeature::resolveElement(obj, subname.c_str(), element); |
| 488 |
return std::move(element.newName); |
| 489 |
} |
| 490 |
|
| 491 |
std::string SubObjectT::getOldElementName(int* index) const |
| 492 |
{ |
| 493 |
ElementNamePair element; |
| 494 |
auto obj = getObject(); |
| 495 |
if (!obj) { |
| 496 |
return {}; |
| 497 |
} |
| 498 |
GeoFeature::resolveElement(obj, subname.c_str(), element); |
| 499 |
if (!index) { |
| 500 |
return std::move(element.oldName); |
| 501 |
} |
| 502 |
std::size_t pos = element.oldName.find_first_of("0123456789"); |
| 503 |
if (pos == std::string::npos) { |
| 504 |
*index = -1; |
| 505 |
} |
| 506 |
else { |
| 507 |
*index = std::atoi(element.oldName.c_str() + pos); |
| 508 |
element.oldName.resize(pos); |
| 509 |
} |
| 510 |
return std::move(element.oldName); |
| 511 |
} |
| 512 |
|
| 513 |
App::DocumentObject* SubObjectT::getSubObject() const |
| 514 |
{ |
| 515 |
auto obj = getObject(); |
| 516 |
if (obj) { |
| 517 |
return obj->getSubObject(subname.c_str()); |
| 518 |
} |
| 519 |
return nullptr; |
| 520 |
} |
| 521 |
|
| 522 |
std::string SubObjectT::getSubObjectPython(bool force) const |
| 523 |
{ |
| 524 |
if (!force && subname.empty()) { |
| 525 |
return getObjectPython(); |
| 526 |
} |
| 527 |
std::stringstream str; |
| 528 |
str << "(" << getObjectPython() << ",u'" << Base::Tools::escapedUnicodeFromUtf8(subname.c_str()) |
| 529 |
<< "')"; |
| 530 |
return str.str(); |
| 531 |
} |
| 532 |
|
| 533 |
std::vector<App::DocumentObject*> SubObjectT::getSubObjectList() const |
| 534 |
{ |
| 535 |
auto obj = getObject(); |
| 536 |
if (obj) { |
| 537 |
return obj->getSubObjectList(subname.c_str()); |
| 538 |
} |
| 539 |
return {}; |
| 540 |
} |
| 541 |
|
| 542 |
std::string SubObjectT::getObjectFullName(const char* docName) const |
| 543 |
{ |
| 544 |
std::ostringstream ss; |
| 545 |
if (!docName || getDocumentName() != docName) { |
| 546 |
ss << getDocumentName(); |
| 547 |
if (auto doc = getDocument()) { |
| 548 |
if (doc->Label.getStrValue() != getDocumentName()) { |
| 549 |
ss << "(" << doc->Label.getValue() << ")"; |
| 550 |
} |
| 551 |
} |
| 552 |
ss << "#"; |
| 553 |
} |
| 554 |
ss << getObjectName(); |
| 555 |
if (!getObjectLabel().empty() && getObjectLabel() != getObjectName()) { |
| 556 |
ss << " (" << getObjectLabel() << ")"; |
| 557 |
} |
| 558 |
return ss.str(); |
| 559 |
} |
| 560 |
|
| 561 |
std::string SubObjectT::getSubObjectFullName(const char* docName) const |
| 562 |
{ |
| 563 |
if (subname.empty()) { |
| 564 |
return getObjectFullName(docName); |
| 565 |
} |
| 566 |
std::ostringstream ss; |
| 567 |
if (!docName || getDocumentName() != docName) { |
| 568 |
ss << getDocumentName(); |
| 569 |
if (auto doc = getDocument()) { |
| 570 |
if (doc->Label.getStrValue() != getDocumentName()) { |
| 571 |
ss << "(" << doc->Label.getValue() << ")"; |
| 572 |
} |
| 573 |
} |
| 574 |
ss << "#"; |
| 575 |
} |
| 576 |
ss << getObjectName() << "." << subname; |
| 577 |
auto sobj = getSubObject(); |
| 578 |
if (sobj && sobj->Label.getStrValue() != sobj->getNameInDocument()) { |
| 579 |
ss << " (" << sobj->Label.getValue() << ")"; |
| 580 |
} |
| 581 |
return ss.str(); |
| 582 |
} |
| 583 |
|
| 584 |
// ----------------------------------------------------------------------------- |
| 585 |
|
| 586 |
PropertyLinkT::PropertyLinkT() |
| 587 |
: toPython("None") |
| 588 |
{} |
| 589 |
|
| 590 |
PropertyLinkT::PropertyLinkT(DocumentObject* obj) |
| 591 |
: PropertyLinkT() |
| 592 |
{ |
| 593 |
if (obj) { |
| 594 |
std::ostringstream str; |
| 595 |
DocumentObjectT objT(obj); |
| 596 |
str << objT.getObjectPython(); |
| 597 |
|
| 598 |
toPython = str.str(); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
PropertyLinkT::PropertyLinkT(DocumentObject* obj, const std::vector<std::string>& subNames) |
| 603 |
: PropertyLinkT() |
| 604 |
{ |
| 605 |
if (obj) { |
| 606 |
std::ostringstream str; |
| 607 |
DocumentObjectT objT(obj); |
| 608 |
str << "(" << objT.getObjectPython() << ",["; |
| 609 |
for (const auto& it : subNames) { |
| 610 |
str << "'" << it << "',"; |
| 611 |
} |
| 612 |
str << "])"; |
| 613 |
|
| 614 |
toPython = str.str(); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
PropertyLinkT::PropertyLinkT(const std::vector<DocumentObject*>& objs) |
| 619 |
: PropertyLinkT() |
| 620 |
{ |
| 621 |
if (!objs.empty()) { |
| 622 |
std::stringstream str; |
| 623 |
str << "["; |
| 624 |
for (std::size_t i = 0; i < objs.size(); i++) { |
| 625 |
if (i > 0) { |
| 626 |
str << ", "; |
| 627 |
} |
| 628 |
|
| 629 |
App::DocumentObject* obj = objs[i]; |
| 630 |
if (obj) { |
| 631 |
DocumentObjectT objT(obj); |
| 632 |
str << objT.getObjectPython(); |
| 633 |
} |
| 634 |
else { |
| 635 |
str << "None"; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
str << "]"; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
PropertyLinkT::PropertyLinkT(const std::vector<DocumentObject*>& objs, |
| 644 |
const std::vector<std::string>& subNames) |
| 645 |
: PropertyLinkT() |
| 646 |
{ |
| 647 |
if (!objs.empty() && objs.size() == subNames.size()) { |
| 648 |
std::stringstream str; |
| 649 |
str << "["; |
| 650 |
for (std::size_t i = 0; i < subNames.size(); i++) { |
| 651 |
if (i > 0) { |
| 652 |
str << ",("; |
| 653 |
} |
| 654 |
else { |
| 655 |
str << "("; |
| 656 |
} |
| 657 |
|
| 658 |
App::DocumentObject* obj = objs[i]; |
| 659 |
if (obj) { |
| 660 |
DocumentObjectT objT(obj); |
| 661 |
str << objT.getObjectPython(); |
| 662 |
} |
| 663 |
else { |
| 664 |
str << "None"; |
| 665 |
} |
| 666 |
|
| 667 |
str << ","; |
| 668 |
str << "'" << subNames[i] << "'"; |
| 669 |
str << ")"; |
| 670 |
} |
| 671 |
|
| 672 |
str << "]"; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
std::string PropertyLinkT::getPropertyPython() const |
| 677 |
{ |
| 678 |
return toPython; |
| 679 |
} |
| 680 |
|
| 681 |
// ----------------------------------------------------------------------------- |
| 682 |
|
| 683 |
class DocumentWeakPtrT::Private |
| 684 |
{ |
| 685 |
public: |
| 686 |
explicit Private(App::Document* doc) |
| 687 |
: _document(doc) |
| 688 |
{ |
| 689 |
if (doc) { |
| 690 |
// NOLINTBEGIN |
| 691 |
connectApplicationDeletedDocument = App::GetApplication().signalDeleteDocument.connect( |
| 692 |
std::bind(&Private::deletedDocument, this, sp::_1)); |
| 693 |
// NOLINTEND |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
void deletedDocument(const App::Document& doc) |
| 698 |
{ |
| 699 |
if (_document == &doc) { |
| 700 |
reset(); |
| 701 |
} |
| 702 |
} |
| 703 |
void reset() |
| 704 |
{ |
| 705 |
connectApplicationDeletedDocument.disconnect(); |
| 706 |
_document = nullptr; |
| 707 |
} |
| 708 |
|
| 709 |
App::Document* _document; |
| 710 |
using Connection = boost::signals2::scoped_connection; |
| 711 |
Connection connectApplicationDeletedDocument; |
| 712 |
}; |
| 713 |
|
| 714 |
DocumentWeakPtrT::DocumentWeakPtrT(App::Document* doc) noexcept |
| 715 |
: d(new Private(doc)) |
| 716 |
{} |
| 717 |
|
| 718 |
DocumentWeakPtrT::~DocumentWeakPtrT() = default; |
| 719 |
|
| 720 |
void DocumentWeakPtrT::reset() noexcept |
| 721 |
{ |
| 722 |
d->reset(); |
| 723 |
} |
| 724 |
|
| 725 |
bool DocumentWeakPtrT::expired() const noexcept |
| 726 |
{ |
| 727 |
return (d->_document == nullptr); |
| 728 |
} |
| 729 |
|
| 730 |
App::Document* DocumentWeakPtrT::operator*() const noexcept |
| 731 |
{ |
| 732 |
return d->_document; |
| 733 |
} |
| 734 |
|
| 735 |
App::Document* DocumentWeakPtrT::operator->() const noexcept |
| 736 |
{ |
| 737 |
return d->_document; |
| 738 |
} |
| 739 |
|
| 740 |
// ----------------------------------------------------------------------------- |
| 741 |
|
| 742 |
class DocumentObjectWeakPtrT::Private |
| 743 |
{ |
| 744 |
public: |
| 745 |
explicit Private(App::DocumentObject* obj) |
| 746 |
: object(obj) |
| 747 |
{ |
| 748 |
set(obj); |
| 749 |
} |
| 750 |
void deletedDocument(const App::Document& doc) |
| 751 |
{ |
| 752 |
// When deleting document then there is no way to undo it |
| 753 |
if (object && object->getDocument() == &doc) { |
| 754 |
reset(); |
| 755 |
} |
| 756 |
} |
| 757 |
void createdObject(const App::DocumentObject& obj) noexcept |
| 758 |
{ |
| 759 |
// When undoing the removal |
| 760 |
if (object == &obj) { |
| 761 |
indocument = true; |
| 762 |
} |
| 763 |
} |
| 764 |
void deletedObject(const App::DocumentObject& obj) noexcept |
| 765 |
{ |
| 766 |
if (object == &obj) { |
| 767 |
indocument = false; |
| 768 |
} |
| 769 |
} |
| 770 |
void reset() |
| 771 |
{ |
| 772 |
connectApplicationDeletedDocument.disconnect(); |
| 773 |
connectDocumentCreatedObject.disconnect(); |
| 774 |
connectDocumentDeletedObject.disconnect(); |
| 775 |
object = nullptr; |
| 776 |
indocument = false; |
| 777 |
} |
| 778 |
void set(App::DocumentObject* obj) |
| 779 |
{ |
| 780 |
object = obj; |
| 781 |
if (obj) { |
| 782 |
// NOLINTBEGIN |
| 783 |
indocument = true; |
| 784 |
connectApplicationDeletedDocument = App::GetApplication().signalDeleteDocument.connect( |
| 785 |
std::bind(&Private::deletedDocument, this, sp::_1)); |
| 786 |
App::Document* doc = obj->getDocument(); |
| 787 |
connectDocumentCreatedObject = |
| 788 |
doc->signalNewObject.connect(std::bind(&Private::createdObject, this, sp::_1)); |
| 789 |
connectDocumentDeletedObject = |
| 790 |
doc->signalDeletedObject.connect(std::bind(&Private::deletedObject, this, sp::_1)); |
| 791 |
// NOLINTEND |
| 792 |
} |
| 793 |
} |
| 794 |
App::DocumentObject* get() const noexcept |
| 795 |
{ |
| 796 |
return indocument ? object : nullptr; |
| 797 |
} |
| 798 |
|
| 799 |
App::DocumentObject* object; |
| 800 |
bool indocument {false}; |
| 801 |
using Connection = boost::signals2::scoped_connection; |
| 802 |
Connection connectApplicationDeletedDocument; |
| 803 |
Connection connectDocumentCreatedObject; |
| 804 |
Connection connectDocumentDeletedObject; |
| 805 |
}; |
| 806 |
|
| 807 |
DocumentObjectWeakPtrT::DocumentObjectWeakPtrT(App::DocumentObject* obj) |
| 808 |
: d(new Private(obj)) |
| 809 |
{} |
| 810 |
|
| 811 |
DocumentObjectWeakPtrT::~DocumentObjectWeakPtrT() = default; |
| 812 |
|
| 813 |
App::DocumentObject* DocumentObjectWeakPtrT::_get() const noexcept |
| 814 |
{ |
| 815 |
return d->get(); |
| 816 |
} |
| 817 |
|
| 818 |
DocumentObjectWeakPtrT::DocumentObjectWeakPtrT(DocumentObjectWeakPtrT&&) = default; |
| 819 |
DocumentObjectWeakPtrT& DocumentObjectWeakPtrT::operator=(DocumentObjectWeakPtrT&&) = default; |
| 820 |
|
| 821 |
void DocumentObjectWeakPtrT::reset() |
| 822 |
{ |
| 823 |
d->reset(); |
| 824 |
} |
| 825 |
|
| 826 |
bool DocumentObjectWeakPtrT::expired() const noexcept |
| 827 |
{ |
| 828 |
return !d->indocument; |
| 829 |
} |
| 830 |
|
| 831 |
DocumentObjectWeakPtrT& DocumentObjectWeakPtrT::operator=(App::DocumentObject* p) |
| 832 |
{ |
| 833 |
d->reset(); |
| 834 |
d->set(p); |
| 835 |
return *this; |
| 836 |
} |
| 837 |
|
| 838 |
App::DocumentObject* DocumentObjectWeakPtrT::operator*() const noexcept |
| 839 |
{ |
| 840 |
return d->get(); |
| 841 |
} |
| 842 |
|
| 843 |
App::DocumentObject* DocumentObjectWeakPtrT::operator->() const noexcept |
| 844 |
{ |
| 845 |
return d->get(); |
| 846 |
} |
| 847 |
|
| 848 |
bool DocumentObjectWeakPtrT::operator==(const DocumentObjectWeakPtrT& p) const noexcept |
| 849 |
{ |
| 850 |
return d->get() == p.d->get(); |
| 851 |
} |
| 852 |
|
| 853 |
bool DocumentObjectWeakPtrT::operator!=(const DocumentObjectWeakPtrT& p) const noexcept |
| 854 |
{ |
| 855 |
return d->get() != p.d->get(); |
| 856 |
} |
| 857 |
|
| 858 |
// ----------------------------------------------------------------------------- |
| 859 |
|
| 860 |
DocumentObserver::DocumentObserver() |
| 861 |
: _document(nullptr) |
| 862 |
{ |
| 863 |
// NOLINTBEGIN |
| 864 |
this->connectApplicationCreatedDocument = App::GetApplication().signalNewDocument.connect( |
| 865 |
std::bind(&DocumentObserver::slotCreatedDocument, this, sp::_1)); |
| 866 |
this->connectApplicationDeletedDocument = App::GetApplication().signalDeleteDocument.connect( |
| 867 |
std::bind(&DocumentObserver::slotDeletedDocument, this, sp::_1)); |
| 868 |
this->connectApplicationActivateDocument = App::GetApplication().signalActiveDocument.connect( |
| 869 |
std::bind(&DocumentObserver::slotActivateDocument, this, sp::_1)); |
| 870 |
// NOLINTEND |
| 871 |
} |
| 872 |
|
| 873 |
DocumentObserver::DocumentObserver(Document* doc) |
| 874 |
: DocumentObserver() |
| 875 |
{ |
| 876 |
// Connect to application and given document |
| 877 |
attachDocument(doc); |
| 878 |
} |
| 879 |
|
| 880 |
DocumentObserver::~DocumentObserver() |
| 881 |
{ |
| 882 |
// disconnect from application and document |
| 883 |
this->connectApplicationCreatedDocument.disconnect(); |
| 884 |
this->connectApplicationDeletedDocument.disconnect(); |
| 885 |
this->connectApplicationActivateDocument.disconnect(); |
| 886 |
detachDocument(); |
| 887 |
} |
| 888 |
|
| 889 |
Document* DocumentObserver::getDocument() const |
| 890 |
{ |
| 891 |
return this->_document; |
| 892 |
} |
| 893 |
|
| 894 |
void DocumentObserver::attachDocument(Document* doc) |
| 895 |
{ |
| 896 |
if (_document != doc) { |
| 897 |
detachDocument(); |
| 898 |
_document = doc; |
| 899 |
|
| 900 |
// NOLINTBEGIN |
| 901 |
this->connectDocumentCreatedObject = _document->signalNewObject.connect( |
| 902 |
std::bind(&DocumentObserver::slotCreatedObject, this, sp::_1)); |
| 903 |
this->connectDocumentDeletedObject = _document->signalDeletedObject.connect( |
| 904 |
std::bind(&DocumentObserver::slotDeletedObject, this, sp::_1)); |
| 905 |
this->connectDocumentChangedObject = _document->signalChangedObject.connect( |
| 906 |
std::bind(&DocumentObserver::slotChangedObject, this, sp::_1, sp::_2)); |
| 907 |
this->connectDocumentRecomputedObject = _document->signalRecomputedObject.connect( |
| 908 |
std::bind(&DocumentObserver::slotRecomputedObject, this, sp::_1)); |
| 909 |
this->connectDocumentRecomputed = _document->signalRecomputed.connect( |
| 910 |
std::bind(&DocumentObserver::slotRecomputedDocument, this, sp::_1)); |
| 911 |
// NOLINTEND |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
void DocumentObserver::detachDocument() |
| 916 |
{ |
| 917 |
if (this->_document) { |
| 918 |
this->_document = nullptr; |
| 919 |
this->connectDocumentCreatedObject.disconnect(); |
| 920 |
this->connectDocumentDeletedObject.disconnect(); |
| 921 |
this->connectDocumentChangedObject.disconnect(); |
| 922 |
this->connectDocumentRecomputedObject.disconnect(); |
| 923 |
this->connectDocumentRecomputed.disconnect(); |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
void DocumentObserver::slotCreatedDocument(const App::Document& /*Doc*/) |
| 928 |
{} |
| 929 |
|
| 930 |
void DocumentObserver::slotDeletedDocument(const App::Document& /*Doc*/) |
| 931 |
{} |
| 932 |
|
| 933 |
void DocumentObserver::slotActivateDocument(const App::Document& /*Doc*/) |
| 934 |
{} |
| 935 |
|
| 936 |
void DocumentObserver::slotCreatedObject(const App::DocumentObject& /*Obj*/) |
| 937 |
{} |
| 938 |
|
| 939 |
void DocumentObserver::slotDeletedObject(const App::DocumentObject& /*Obj*/) |
| 940 |
{} |
| 941 |
|
| 942 |
void DocumentObserver::slotChangedObject(const App::DocumentObject& /*Obj*/, |
| 943 |
const App::Property& /*Prop*/) |
| 944 |
{} |
| 945 |
|
| 946 |
void DocumentObserver::slotRecomputedObject(const DocumentObject& /*Obj*/) |
| 947 |
{} |
| 948 |
|
| 949 |
void DocumentObserver::slotRecomputedDocument(const Document& /*doc*/) |
| 950 |
{} |
| 951 |
|
| 952 |
|
| 953 |
// ----------------------------------------------------------------------------- |
| 954 |
|
| 955 |
DocumentObjectObserver::DocumentObjectObserver() = default; |
| 956 |
|
| 957 |
DocumentObjectObserver::~DocumentObjectObserver() = default; |
| 958 |
|
| 959 |
DocumentObjectObserver::const_iterator DocumentObjectObserver::begin() const |
| 960 |
{ |
| 961 |
return _objects.begin(); |
| 962 |
} |
| 963 |
|
| 964 |
DocumentObjectObserver::const_iterator DocumentObjectObserver::end() const |
| 965 |
{ |
| 966 |
return _objects.end(); |
| 967 |
} |
| 968 |
|
| 969 |
void DocumentObjectObserver::addToObservation(App::DocumentObject* obj) |
| 970 |
{ |
| 971 |
_objects.insert(obj); |
| 972 |
} |
| 973 |
|
| 974 |
void DocumentObjectObserver::removeFromObservation(App::DocumentObject* obj) |
| 975 |
{ |
| 976 |
_objects.erase(obj); |
| 977 |
} |
| 978 |
|
| 979 |
void DocumentObjectObserver::slotCreatedDocument(const App::Document&) |
| 980 |
{} |
| 981 |
|
| 982 |
void DocumentObjectObserver::slotDeletedDocument(const App::Document& Doc) |
| 983 |
{ |
| 984 |
if (this->getDocument() == &Doc) { |
| 985 |
this->detachDocument(); |
| 986 |
_objects.clear(); |
| 987 |
cancelObservation(); |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
void DocumentObjectObserver::slotCreatedObject(const App::DocumentObject&) |
| 992 |
{} |
| 993 |
|
| 994 |
void DocumentObjectObserver::slotDeletedObject(const App::DocumentObject& Obj) |
| 995 |
{ |
| 996 |
std::set<App::DocumentObject*>::iterator it = |
| 997 |
_objects.find(const_cast<App::DocumentObject*>(&Obj)); |
| 998 |
if (it != _objects.end()) { |
| 999 |
_objects.erase(it); |
| 1000 |
} |
| 1001 |
if (_objects.empty()) { |
| 1002 |
cancelObservation(); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
void DocumentObjectObserver::slotChangedObject(const App::DocumentObject&, const App::Property&) |
| 1007 |
{} |
| 1008 |
|
| 1009 |
void DocumentObjectObserver::cancelObservation() |
| 1010 |
{} |