OpenLexocad  27.1
PropertyContainer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Base/Persistence.h>
4 #include <Core/PropertyMacros.h>
5 #include <Core/PropertyValue.h>
6 #include <Core/DynamicProperty.h>
7 #include <Core/Variant.h>
8 #include <Geom/BrepData.h>
9 
10 #include <list>
11 #include <map>
12 #include <set>
13 #include <unordered_map>
14 #include <unordered_set>
15 
16 #define USE_PROPERTYMAP
17 
18 static const char* PropertyName = "Property";
19 
20 class PropertyTree;
21 class CA_Writer;
22 class QFont;
23 class MD5;
24 
25 
26 
27 namespace Base
28 {
29 class Color;
30 }
31 
32 namespace Core
33 {
34 class FieldData
35 {
36 public:
37  FieldData(const Core::FieldData* /*fd*/){};
38 };
39 } // namespace Core
40 
41 namespace App
42 {
43 class ElementTool;
44 }
45 
46 
47 
48 #define MUTABLE_CONTAINER(_class_) \
49 public: \
50  static bool isMutableStatic() { return true; } \
51 \
52 public: \
53  virtual bool isMutable() const { return true; }
54 
55 #define IMMUTABLE_CONTAINER(_class_) \
56 public: \
57  friend class Core::PropertyBundle<_class_>; \
58 \
59 public: \
60  static bool isMutableStatic() { return false; } \
61 \
62 public: \
63  virtual bool isMutable() const { return false; } \
64 \
65 public: \
66  Core::PropertyBundle<_class_> getPropertyBundle() const \
67  { \
68  Core::PropertyBundle<_class_> dsBundle; \
69  dsBundle.setPropertyValues(this); \
70  return dsBundle; \
71  }
72 
73 
74 namespace Core
75 {
76 class CoreDocument;
77 class Placement;
78 class DocObject;
79 class ObjectExecutor;
80 class Property;
81 class DbgInfo;
82 class PropertyUser;
83 
85 {
86  Prop_None = 0,
93 };
94 
95 class LX_CORE_EXPORT LxFieldData
96 {
97 public:
98  LxFieldData(void);
99  LxFieldData(const LxFieldData* fd);
100  void addField(void* base, char* fieldname, void* fieldptr);
101  std::unordered_map<std::string, ptrdiff_t> m_fieldmap;
102  bool fieldsAdded = false;
103 };
104 
105 
106 
107 class LX_CORE_EXPORT PropertyContainer : public Base::Persistence
108 {
110  LX_NODE_HEADER();
112 
113 public:
114  friend class PostInitClass;
115  friend class CoreDocument;
116  friend class ObjectExecutor;
117  friend class Property;
118  friend class ::App::ElementTool;
119 
121  // //
122  // --------------------- BEGIN API --------------------- //
123  // //
124  // ATTENTION: DO NOT CHANGE ANY SIGNATURES IN THE API ! //
125  // //
127 
128  PropertyContainer(void);
129  // Copy constructor
131  virtual ~PropertyContainer(void) = default;
132 
135  bool addProperty(Core::Property* p, const std::string& name);
137  Core::Property* addProperty(Base::Type t, const std::string& name, const Core::Variant& defaultValue);
139  Core::PropertyUser* addPropertyUser(const std::string& name, const Core::Variant& value);
141  void removeProperty(Core::Property* p, std::string pName);
143  void removeAllProperties();
145  inline Core::Property* getPropertyByName(const std::string& name) const;
147  inline Core::Property* getPropertyByName(const Base::String& name) const;
149  virtual const char* getPropertyName(const Property* prop) const;
150 
152  template <typename T>
153  T* getPropertyByName(const std::string& name) const
154  {
155  Core::Property* p = getPropertyByName(name);
156 
157  if (p && p->isDerivedFrom<T>())
158  {
159  return static_cast<T*>(p);
160  }
161 
162  return nullptr;
163  }
164 
165 
167  void getPropertyMap(Core::PropertyMap&) const;
169  std::vector<std::pair<Core::Property*, std::string> > getPropertiesAndNameByType(Base::Type type) const;
170  std::vector<Core::Property*> getPropertiesByType(Base::Type type) const;
171  std::vector<Core::Property*> getPropertiesByTypes(const std::vector<Base::Type>& type) const;
173  bool setPropertyFromVariant(const std::string& name, const Core::Variant& value);
174 
175  bool setProperty(const std::string& name, double value);
176  bool setProperty(const std::string& name, int value);
177  bool setProperty(const std::string& name, const Geom::Vec& value);
178  bool setProperty(const std::string& name, const std::list<Geom::Vec>& value);
179  bool setProperty(const std::string& name, const Geom::Trsf& value);
180  bool setProperty(const std::string& name, const std::string& value);
181  bool setProperty(const std::string& name, const char* value);
182  bool setProperty(const std::string& name, const Base::String& value);
183  bool setProperty(const std::string& name, const Geom::Pnt& value);
184  bool setProperty(const std::string& name, const Geom::Pnt2d& value);
185  bool setProperty(const std::string& name, const std::vector<Geom::Pnt>& value);
186  bool setProperty(const std::string& name, const std::vector<Geom::Pnt2d>& value);
187  // bool setProperty(const std::string& name, const std::list<std::string>& value);
188  // bool setProperty(const std::string& name, const Core::Placement& value);
189  bool setProperty(const std::string& name, const MD5& value);
190  bool setProperty(const std::string& name, bool value);
191  bool setProperty(const std::string& name, Core::DocObject* value);
192  // without this method, method setProperty(const std::string& name, bool value) is called for const Core::DocObject
193  bool setProperty(const std::string& name, const Core::DocObject* value);
194  bool setProperty(const std::string& name, const std::unordered_set<Core::DocObject*>& value);
195  bool setProperty(const std::string& name, const std::list<Core::DocObject*>& value);
196  bool setProperty(const std::string& name, const std::vector<int>& value);
197  bool setProperty(const std::string& name, const QFont& value);
198  bool setProperty(const std::string& name, const Draw::DrawStyle& value);
199  bool setProperty(const std::string& name, const Draw::Arrowheads& value);
200  bool setProperty(const std::string& name, const Geom::Dir& value);
201  bool setProperty(const std::string& name, const Geom::Dir2d& value);
202  bool setProperty(const std::string& name, const Base::Color& value);
203  bool setProperty(const std::string& name, const Geom::Ax1& value);
204  bool setProperty(const std::string& name, const Geom::Ax2& value);
205  // bool setProperty(const std::string& name, const gp_Ax22d& value);
206  bool setProperty(const std::string& name, const Draw::Texture2Transform& value);
207  bool setProperty(const std::string& name, const std::list<std::list<Geom::Pnt> >& value);
208  bool setProperty(const std::string& name, const std::vector<double>& value);
209  bool setProperty(const std::string& name, pBrepData& value);
210 
211 
212 
218  virtual bool onBeforeChange(Core::Property* p, const Core::Variant& newValue = Core::Variant());
220  virtual void onChanged(Core::Property* p);
222  virtual void onDeleted();
224  virtual void onNew();
226  virtual void onPropertyStatusBitsChanged(const Property& prop, unsigned long oldStatus) {}
228  virtual void save(Base::AbstractWriter& writer, Base::PersistenceVersion& version);
230  virtual void restore(Base::AbstractXMLReader& reader, Base::PersistenceVersion& version);
233  bool enableNotify(const bool enable);
235  bool isNotifyEnabled(void) const;
237  void touch();
238 
241  bool isDeleted() const;
243  bool isStatusDeleted() const;
245  bool isMarkedForDelete() const;
247  bool isMarkedForDeleteFinal() const;
249  bool isDeletedFinal() const;
251  bool isNew() const;
253  bool isUpdated() const;
255  bool isValid() const;
257  bool hasErrors() const;
259  void setData(const std::string& key, void* data);
261  void* getData(const std::string& key, bool* ok = nullptr) const;
263  void removeData(const std::string& key);
265  bool hasSameValuesAs(const PropertyContainer* container) const;
267  void setPropertyValues(const PropertyContainer* other);
269  template <typename T>
270  T getPropertyValueByName(const std::string& propertyName, bool* ok = 0) const
271  {
272  Core::Property* p = getPropertyByName(propertyName);
273  if (p)
274  return p->getVariant().getValue<T>(ok);
275  else
276  return Core::Variant().getValue<T>(ok);
277  }
278 
281  virtual void ensureIntegrity();
286  void integrityMaintained();
288  void integrityLost();
290  bool checkIntegrity();
291 
292 
294  virtual bool onChangedDebug( Core::Property* p );
295 
296  virtual void checkAfterOnDeleted();
297 
298 
299 protected:
300  virtual void onBeforeChangeProperty(const Property* prop) {}
301  virtual void onChangedProperty(const Property* prop) {}
302 
304  void addPropertiesFrom(const Core::PropertyContainer* other);
305 
307  // //
308  // ---------------------- END API ---------------------- //
309  // //
311 
312 
313 #ifndef LXAPI // INTERFACES BELOW ARE -NOT- PART OF THE LEXOCAD API
314 
315 public:
317  Core::PropertyValueMap getPropertyValueMap() const;
319  std::map<std::string, Core::Variant> getKeyValueMap() const;
321  bool setPropertyFromKeyValue(const std::string& propertyName, const std::string& key, const Core::Variant& value);
323  std::set<Core::Property*> getUpdatedProperties() const;
325  void setUpdated();
326  void setUpdated(const Core::Property* p);
328  void setHasErrors();
330  void setNew();
332  void setValid();
334  void setDeleted();
336  void setMarkedForDelete();
338  void setMarkedForDeleteFinal();
340  void setDeleteFinal();
342  virtual void breakLinks();
343 
344  unsigned int getPropertyChangeStatus() const;
346  void setAllPropertiesValid();
348  void setPropertyValues(const Core::PropertyValueMap& other);
350  std::vector<Core::Property*> getPropertiesOrdered() const;
351 
352  virtual bool check_lx(char* f, char* n);
353 
356  // HPK: Do not use DynamicProperty. This is for
357  // testing transactions only.
358  virtual Core::Property* addDynamicProperty(const char* type,
359  const char* name = 0,
360  const char* group = 0,
361  const char* doc = 0,
362  short attr = 0,
363  bool ro = false,
364  bool hidden = false);
365 
366 #ifndef SWIG
367  DynamicProperty::PropData getDynamicPropertyData(const Property* prop) const;
368 #endif
369 
370  virtual bool removeDynamicProperty(const char* name);
371  virtual std::vector<std::string> getDynamicPropertyNames() const;
372  virtual Core::Property* getDynamicPropertyByName(const char* name) const;
374 
375 
376 
377  bool isRestored = false;
378 
379 #ifdef USE_PROPERTYMAP
380 
382  bool rollBack();
383 
385  void saveLastValidPropertyMap();
386 
388  const Core::PropertyValueMap& getLastValidPropertyMap() const;
389 #endif
390 
391 
392 protected:
393  enum class Status
394  {
395  New = 1,
396  Updated = 2,
397  Valid = 3,
398  MarkedForDelete = 4,
399  MarkedForDeleteFinal,
400  Deleted,
401  DeletedFinal,
402  Error
403  };
404 
405  virtual void setStatus(Status status);
406  Status getStatus() const;
407 
408  bool checkForAlreadyExistingProperty(Core::Property* property);
409 
411  virtual void restoreProperty(Core::Property* property,
412  const Base::String& name,
413  Base::AbstractXMLReader& reader,
415 
416  unsigned int _changedProperties;
417  //DynamicProperty dynamicProps;
418 
419 private:
421  void _setPropertyValues(const Core::PropertyValueMap& pmap, bool isInit = false);
423  void _setPropertyValues(const Core::PropertyContainer* other, bool isInit = false);
424  void getPropertyMapSetNotify(bool v);
425 
426  std::unordered_map<std::string, Core::Property*> _userPropertyMap;
427 #ifdef USE_PROPERTYMAP
428  std::unordered_map<std::string, PropertyValue> _lastValidPropertyMap;
429 #endif
430  std::unordered_map<std::string, void*> _data;
431  Status _status = Status::New;
432  bool _notify = true;
433 
437  bool _integrity = true;
438 #endif
439 };
440 
441 
442 
443 class LX_CORE_EXPORT PostInitClass
444 {
445 public:
447  ~PostInitClass();
449 };
450 
451 } // namespace Core
void breakLinks(Core::DocObject *from)
unsigned int _changedProperties
Definition: PropertyContainer.h:416
T * getPropertyByName(const std::string &name) const
Returns the property of type T with name 'name'. Returns 'nullptr' on failure.
Definition: PropertyContainer.h:153
CoreDocument()
Definition: Color.h:45
Definition: PropertyContainer.h:90
Definition: Persistence.h:14
Defines a non-persistent 3D Cartesian point.
Definition: Pnt.h:43
Definition: PropertyContainer.h:95
Definition: md5.h:69
Definition: PropertyContainer.h:107
Core::PropertyText name
Definition: CoreDocument.h:167
Definition: PropertyContainer.h:92
PropertyType
Definition: PropertyContainer.h:84
Definition: Dir2d.h:38
std::shared_ptr< Geom::BrepData > pBrepData
Definition: BrepData.h:28
A Utf-16 (windows) or ucs4 (unix) encoded string class.
Definition: String.h:23
Persistence class and root of the type system.
Definition: Persistence.h:23
void restoreProperty(Core::Property *property, const Base::String &name, Base::AbstractXMLReader &reader, Base::PersistenceVersion &version) override
LX_CORE_EXPORT Version & version
Definition: Type.h:52
Definition: CoreDocument.h:210
Definition: Variant.h:87
Definition: DocObject.h:26
virtual void onBeforeChangeProperty(const Property *prop)
Definition: PropertyContainer.h:300
Definition: DynamicProperty.h:139
T getValue(bool *ok) const
Definition: Variant.h:325
Definition: Ax2.h:67
std::unordered_map< std::string, Core::Property * > PropertyMap
Definition: PropertyValue.h:26
Definition: AbstractXMLReader.h:7
Definition: Trsf.h:57
Definition: Dir.h:45
std::unordered_map< std::string, ptrdiff_t > m_fieldmap
Definition: PropertyContainer.h:101
std::unordered_map< std::string, PropertyValue > PropertyValueMap
Definition: PropertyValue.h:27
Definition: Base.h:12
PostInitClass(Core::PropertyContainer *p)
Definition: PropertyContainer.h:446
bool isDerivedFrom() const
Definition: Base.h:108
Definition: PropertyContainer.h:91
Definition: DocObject.h:25
Definition: PropertyContainer.h:89
virtual void onPropertyStatusBitsChanged(const Property &prop, unsigned long oldStatus)
Is called when the Property StatusBits are changed.
Definition: PropertyContainer.h:226
Definition: Writer.h:16
Definition: PropertyContainer.h:87
Definition: Ax1.h:50
Defines a non-persistent vector in 3D space.
Definition: Vec.h:44
#define MUTABLE_CONTAINER(_class_)
Definition: PropertyContainer.h:48
T getPropertyValueByName(const std::string &propertyName, bool *ok=0) const
Returns the property value T with this name. If ok=true T holds a valid value.
Definition: PropertyContainer.h:270
Definition: PropertyContainer.h:443
FieldData(const Core::FieldData *)
Definition: PropertyContainer.h:37
Definition: PropertyContainer.h:34
LX_NODE_HEADER()
Definition: Property.h:77
Status
Definition: PropertyContainer.h:393
Core::PropertyContainer * container
Definition: PropertyContainer.h:448
Core::PropertyUser is a class that can hold properties defined by the user. Its value member is a Cor...
Definition: PropertyUser.h:15
Definition: AbstractXMLReader.h:5
Definition: PropertyContainer.h:86
basic_group< entity, Types... > group
Alias declaration for the most common use case.
Definition: entt.hpp:3822
virtual Core::Variant getVariant(void) const =0
Definition: PropertyContainer.h:88
Defines a non-persistent 2D cartesian point.
Definition: Pnt2d.h:33
virtual void onChangedProperty(const Property *prop)
Definition: PropertyContainer.h:301
virtual bool onChangedDebug(Core::DocObject *o, Core::Property *p)
TYPESYSTEM_HEADER()
Status
Definition: CoreDocument.h:152
Definition: DocObject.h:51