File size: 20,086 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef APP_DOCUMENTOBSERVER_H
#define APP_DOCUMENTOBSERVER_H
#include <Base/BaseClass.h>
#include <Base/Bitmask.h>
#include <fastsignals/signal.h>
#include <memory>
#include <set>
#include <string>
#include <FCGlobal.h>
namespace App
{
class Document;
class DocumentObject;
class Property;
/**
* The DocumentT class is a helper class to store the name of a document.
* This can be useful when you cannot rely on that the document still exists when you have to
* access it.
*
* @author Werner Mayer
*/
class AppExport DocumentT
{
public:
/*! Constructor */
DocumentT();
/*! Constructor */
DocumentT(Document*); // explicit bombs
/*! Constructor */
explicit DocumentT(const std::string&);
/*! Constructor */
DocumentT(const DocumentT&);
/*! Destructor */
~DocumentT();
/*! Assignment operator */
void operator=(const DocumentT&);
/*! Assignment operator */
void operator=(const Document*);
/*! Assignment operator */
void operator=(const std::string&);
bool operator==(const DocumentT& other) const
{
return document == other.document;
}
bool operator<(const DocumentT& other) const
{
return document < other.document;
}
/*! Get a pointer to the document or 0 if it doesn't exist any more. */
Document* getDocument() const;
/*! Get the name of the document. */
const std::string& getDocumentName() const;
/*! Get the document as Python command. */
std::string getDocumentPython() const;
private:
std::string document;
};
/**
* The DocumentObjectT class is a helper class to store the names of a document object and its
* document. This can be useful when you cannot rely on that the document or the object still exists
* when you have to access it.
*
* @author Werner Mayer
*/
class AppExport DocumentObjectT
{
public:
/*! Constructor */
DocumentObjectT();
/*! Constructor */
DocumentObjectT(const DocumentObjectT&);
/*! Constructor */
DocumentObjectT(DocumentObjectT&&);
/*! Constructor */
explicit DocumentObjectT(const DocumentObject*);
/*! Constructor */
DocumentObjectT(const Document*, const std::string& objName);
/*! Constructor */
DocumentObjectT(const char* docName, const char* objName);
/*! Constructor */
explicit DocumentObjectT(const Property*);
/*! Destructor */
~DocumentObjectT();
/*! Assignment operator */
DocumentObjectT& operator=(const DocumentObjectT&);
/*! Assignment operator */
DocumentObjectT& operator=(DocumentObjectT&&);
/*! Assignment operator */
void operator=(const DocumentObject*);
/*! Assignment operator */
void operator=(const Property*);
/*! Equality operator */
bool operator==(const DocumentObjectT&) const;
/*! Get a pointer to the document or 0 if it doesn't exist any more. */
Document* getDocument() const;
/*! Get the name of the document. */
const std::string& getDocumentName() const;
/*! Get the document as Python command. */
std::string getDocumentPython() const;
/*! Get a pointer to the document object or 0 if it doesn't exist any more. */
DocumentObject* getObject() const;
/*! Get a pointer to the property or 0 if it doesn't exist any more. */
Property* getProperty() const;
/*! Get the name of the document object. */
const std::string& getObjectName() const;
/*! Get the label of the document object. */
const std::string& getObjectLabel() const;
/*! Get the name of the property. */
const std::string& getPropertyName() const;
/*! Get the document object as Python command. */
std::string getObjectPython() const;
/*! Get the property as Python command. */
std::string getPropertyPython() const;
/*! Get a pointer to the document or 0 if it doesn't exist any more or the type doesn't match.
*/
template<typename T>
inline T* getObjectAs() const
{
return freecad_cast<T*>(getObject());
}
template<typename T>
inline T* getPropertyAs() const
{
return freecad_cast<T*>(getProperty());
}
private:
std::string document;
std::string object;
std::string label;
std::string property;
};
class AppExport SubObjectT: public DocumentObjectT
{
public:
/*! Constructor */
SubObjectT();
/*! Constructor */
SubObjectT(const SubObjectT&);
/*! Constructor */
SubObjectT(SubObjectT&&);
/*! Constructor */
SubObjectT(const DocumentObjectT& obj, const char* subname);
/*! Constructor */
SubObjectT(const DocumentObject*, const char* subname);
/*! Constructor */
SubObjectT(const DocumentObject*); // explicit bombs
/*! Constructor */
SubObjectT(const char* docName, const char* objName, const char* subname);
/*! Assignment operator */
SubObjectT& operator=(const SubObjectT&);
/*! Assignment operator */
SubObjectT& operator=(SubObjectT&&);
/*! Assignment operator */
SubObjectT& operator=(const DocumentObjectT&);
/*! Assignment operator */
SubObjectT& operator=(const App::DocumentObject*);
/*! Equality operator */
bool operator==(const SubObjectT&) const;
/// Set the subname path to the sub-object
void setSubName(const char* subname);
/// Set the subname path to the sub-object
void setSubName(const std::string& subname)
{
setSubName(subname.c_str());
}
/// Return the subname path
const std::string& getSubName() const;
/** Return docname#objname (label)
* @param docName: optional document name. The document prefix will only be printed
* if it is different then the given 'doc'.
*/
std::string getObjectFullName(const char* docName = nullptr) const;
/** Return docname#objname.subname (label)
* @param doc: optional document name. The document prefix will only be printed
* if it is different then the given 'doc'.
*/
std::string getSubObjectFullName(const char* docName = nullptr) const;
/// Return the subname path without sub-element
std::string getSubNameNoElement() const;
/// Return the sub-element (Face, Edge, etc) of the subname path
const char* getElementName() const;
/// Check if there is any sub object reference
bool hasSubObject() const;
/// Check if there is any sub element reference
bool hasSubElement() const;
/// Return the new style sub-element name
std::string getNewElementName() const;
/** Return the old style sub-element name
* @param index: if given, then return the element type, and extract the index
*/
std::string getOldElementName(int* index = nullptr) const;
/// Return the sub-object
DocumentObject* getSubObject() const;
/// Return all objects along the subname path
std::vector<DocumentObject*> getSubObjectList() const;
bool operator<(const SubObjectT& other) const;
std::string getSubObjectPython(bool force = true) const;
/// Options used by normalize()
enum class NormalizeOption : uint8_t
{
/// Do not include sub-element reference in the output path
NoElement = 0x01,
/** Do not flatten the output path. If not specified, the output path
* will be flatten to exclude intermediate objects that belong to the
* same geo feature group before resolving. For example,
* Part.Fusion.Box. -> Part.Box.
*/
NoFlatten = 0x02,
/** Do not change the sub-object component inside the path. Each
* component of the subname object path can be either the object
* internal name, the label of the object if starts with '$', or an
* integer index. If this option is not specified, each component will
* be converted to object internal name, except for integer index.
*/
KeepSubName = 0x04,
/** Convert integer index in the path to sub-object internal name */
ConvertIndex = 0x08,
};
using NormalizeOptions = Base::Flags<NormalizeOption>;
/** Normalize the subname path to use only the object internal name and old style element name
* @return Return whether the subname has been changed
*/
bool normalize(NormalizeOptions options = NormalizeOption());
/// Return a normalize copy of itself
SubObjectT normalized(NormalizeOptions options = NormalizeOption()) const;
private:
std::string subname;
};
/**
* The PropertyLinkT class is a helper class to create Python statements for property links.
*/
class AppExport PropertyLinkT
{
public:
/*! Constructor */
PropertyLinkT();
/*! Constructor */
explicit PropertyLinkT(DocumentObject* obj);
/*! Constructor */
PropertyLinkT(DocumentObject* obj, const std::vector<std::string>& subNames);
/*! Constructor */
explicit PropertyLinkT(const std::vector<DocumentObject*>& objs);
/*! Constructor */
PropertyLinkT(const std::vector<DocumentObject*>& objs,
const std::vector<std::string>& subNames);
/*! Get the property as Python command. */
std::string getPropertyPython() const;
private:
std::string toPython;
};
/**
* @brief The DocumentWeakPtrT class
*/
class AppExport DocumentWeakPtrT
{
public:
explicit DocumentWeakPtrT(App::Document*) noexcept;
~DocumentWeakPtrT();
/*!
* \brief reset
* Releases the reference to the managed object. After the call *this manages no object.
*/
void reset() noexcept;
/*!
* \brief expired
* \return true if the managed object has already been deleted, false otherwise.
*/
bool expired() const noexcept;
/*!
* \brief operator *
* \return pointer to the document
*/
App::Document* operator*() const noexcept;
/*!
* \brief operator ->
* \return pointer to the document
*/
App::Document* operator->() const noexcept;
// disable
DocumentWeakPtrT(const DocumentWeakPtrT&) = delete;
DocumentWeakPtrT& operator=(const DocumentWeakPtrT&) = delete;
private:
class Private;
std::unique_ptr<Private> d;
};
/**
* @brief The DocumentObjectWeakPtrT class
*/
class AppExport DocumentObjectWeakPtrT
{
public:
explicit DocumentObjectWeakPtrT(App::DocumentObject*);
~DocumentObjectWeakPtrT();
// disable copy
DocumentObjectWeakPtrT(const DocumentObjectWeakPtrT &) = delete;
DocumentObjectWeakPtrT &operator=(const DocumentObjectWeakPtrT &) = delete;
// default move
DocumentObjectWeakPtrT(DocumentObjectWeakPtrT &&);
DocumentObjectWeakPtrT &operator=(DocumentObjectWeakPtrT &&);
/*!
* \brief reset
* Releases the reference to the managed object. After the call *this manages no object.
*/
void reset();
/*!
* \brief expired
* \return true if the managed object has already been deleted, false otherwise.
*/
bool expired() const noexcept;
/*!
* \brief operator =
* Assignment operator
*/
DocumentObjectWeakPtrT& operator=(App::DocumentObject* p);
/*!
* \brief operator *
* \return pointer to the document object
*/
App::DocumentObject* operator*() const noexcept;
/*!
* \brief operator ->
* \return pointer to the document object
*/
App::DocumentObject* operator->() const noexcept;
/*!
* \brief operator ==
* \return true if both objects are equal, false otherwise
*/
bool operator==(const DocumentObjectWeakPtrT& p) const noexcept;
/*!
* \brief operator !=
* \return true if both objects are inequal, false otherwise
*/
bool operator!=(const DocumentObjectWeakPtrT& p) const noexcept;
/*! Get a pointer to the object or 0 if it doesn't exist any more or the type doesn't match. */
template<typename T>
inline T* get() const noexcept
{
return freecad_cast<T*>(_get());
}
private:
App::DocumentObject* _get() const noexcept;
private:
class Private;
std::unique_ptr<Private> d;
};
/**
* @brief The WeakPtrT class
*/
template<class T>
class WeakPtrT
{
public:
explicit WeakPtrT(T* t)
: ptr(t)
{}
~WeakPtrT() = default;
/*!
* \brief reset
* Releases the reference to the managed object. After the call *this manages no object.
*/
void reset()
{
ptr.reset();
}
/*!
* \brief expired
* \return true if the managed object has already been deleted, false otherwise.
*/
bool expired() const
{
return ptr.expired();
}
/*!
* \brief operator =
* Assignment operator
*/
WeakPtrT<T>& operator=(T* p)
{
ptr = p;
return *this;
}
/*!
* \brief operator ->
* \return pointer to the document object
*/
T* operator*() const
{
return ptr.get<T>();
}
/*!
* \brief operator ->
* \return pointer to the document object
*/
T* operator->() const
{
return ptr.get<T>();
}
/*!
* \brief operator ==
* \return true if both objects are equal, false otherwise
*/
bool operator==(const WeakPtrT<T>& p) const
{
return ptr == p.ptr;
}
/*!
* \brief operator !=
* \return true if both objects are inequal, false otherwise
*/
bool operator!=(const WeakPtrT<T>& p) const
{
return ptr != p.ptr;
}
/*! Get a pointer to the object or 0 if it doesn't exist any more. */
T* get() const noexcept
{
return ptr.get<T>();
}
// disable
WeakPtrT(const WeakPtrT&) = delete;
WeakPtrT& operator=(const WeakPtrT&) = delete;
private:
DocumentObjectWeakPtrT ptr;
};
/**
* The DocumentObserver class simplifies the step to write classes that listen
* to what happens inside a document.
* This is very useful for classes that needs to be notified when an observed
* object has changed.
*
* @author Werner Mayer
*/
class AppExport DocumentObserver
{
public:
/// Constructor
DocumentObserver();
explicit DocumentObserver(Document*);
virtual ~DocumentObserver();
/** Attaches to another document, the old document
* is not longer observed then.
*/
void attachDocument(Document*);
/** Detaches from the current document, the document
* is not longer observed then.
*/
void detachDocument();
private:
/** Called when a new document was created */
virtual void slotCreatedDocument(const App::Document& Doc);
/** Called when a document is about to be closed */
virtual void slotDeletedDocument(const App::Document& Doc);
/** Called when a document is activated */
virtual void slotActivateDocument(const App::Document& Doc);
/** Checks if a new object was added. */
virtual void slotCreatedObject(const App::DocumentObject& Obj);
/** Checks if the given object is about to be removed. */
virtual void slotDeletedObject(const App::DocumentObject& Obj);
/** The property of an observed object has changed */
virtual void slotChangedObject(const App::DocumentObject& Obj, const App::Property& Prop);
/** Called when a given object is recomputed */
virtual void slotRecomputedObject(const App::DocumentObject& Obj);
/** Called when a observed document is recomputed */
virtual void slotRecomputedDocument(const App::Document& Doc);
protected:
Document* getDocument() const;
private:
App::Document* _document;
using Connection = fastsignals::connection;
Connection connectApplicationCreatedDocument;
Connection connectApplicationDeletedDocument;
Connection connectApplicationActivateDocument;
Connection connectDocumentCreatedObject;
Connection connectDocumentDeletedObject;
Connection connectDocumentChangedObject;
Connection connectDocumentRecomputedObject;
Connection connectDocumentRecomputed;
};
/**
* The DocumentObjectObserver class checks for a list of objects
* which of them get removed.
*
* @author Werner Mayer
*/
class AppExport DocumentObjectObserver: public DocumentObserver
{
public:
using const_iterator = std::set<App::DocumentObject*>::const_iterator;
/// Constructor
DocumentObjectObserver();
~DocumentObjectObserver() override;
const_iterator begin() const;
const_iterator end() const;
void addToObservation(App::DocumentObject*);
void removeFromObservation(App::DocumentObject*);
private:
/** Checks if a new document was created */
void slotCreatedDocument(const App::Document& Doc) override;
/** Checks if the given document is about to be closed */
void slotDeletedDocument(const App::Document& Doc) override;
/** Checks if a new object was added. */
void slotCreatedObject(const App::DocumentObject& Obj) override;
/** Checks if the given object is about to be removed. */
void slotDeletedObject(const App::DocumentObject& Obj) override;
/** The property of an observed object has changed */
void slotChangedObject(const App::DocumentObject& Obj, const App::Property& Prop) override;
/** This method gets called when all observed objects are deleted or the whole document is
* deleted. This method can be re-implemented to perform an extra step like closing a dialog
* that observes a document.
*/
virtual void cancelObservation();
private:
std::set<App::DocumentObject*> _objects;
};
} // namespace App
template<>
struct std::hash<App::DocumentObjectWeakPtrT>
{
std::size_t operator()(const App::DocumentObjectWeakPtrT& ptr) const noexcept
{
return std::hash<App::DocumentObject*>{}(*ptr);
}
};
ENABLE_BITMASK_OPERATORS(App::SubObjectT::NormalizeOption)
#endif // APP_DOCUMENTOBSERVER_H
|