Debian Codegraph @

src/App/DocumentObserver.h

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