Salome HOME
Fix for the issue #1179
[modules/shaper.git] / src / Model / Model_Data.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Data.hxx
4 // Created:     21 Mar 2014
5 // Author:      Mikhail PONIKAROV
6
7 #ifndef Model_Data_H_
8 #define Model_Data_H_
9
10 #include <Model.h>
11 #include <ModelAPI_Attribute.h>
12 #include <ModelAPI_AttributeBoolean.h>
13 #include <ModelAPI_AttributeDocRef.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeInteger.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeReference.h>
18 #include <ModelAPI_AttributeRefList.h>
19 #include <ModelAPI_AttributeString.h>
20 #include <ModelAPI_AttributeIntArray.h>
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Object.h>
24
25 #include <TDF_Label.hxx>
26 #include <TDataStd_BooleanArray.hxx>
27
28 #include <memory>
29
30 #include <map>
31 #include <list>
32 #include <string>
33 #include <set>
34
35 class ModelAPI_Attribute;
36
37 /**\class Model_Data
38  * \ingroup DataModel
39  * \brief General object of the application that allows
40  * to get/set attributes from the document and compute result of an operation.
41  */
42
43 class Model_Data : public ModelAPI_Data
44 {
45   TDF_Label myLab;  ///< label of the feature in the document
46   /// All attributes of the object identified by the attribute ID
47   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> > myAttrs;
48   /// Array of flags of this data
49   Handle(TDataStd_BooleanArray) myFlags;
50
51   /// needed here to emit signal that object changed on change of the attribute
52   ObjectPtr myObject;
53
54   /// List of attributes referenced to owner (updated only during the transaction change)
55   std::set<AttributePtr> myRefsToMe;
56   /// flag that may block the "attribute updated" sending
57   bool mySendAttributeUpdated;
58   /// if some attribute was changed, but mySendAttributeUpdated was false, this flag stores this
59   bool myWasChangedButBlocked;
60
61   /// Returns label of this feature
62   TDF_Label label()
63   {
64     return myLab;
65   }
66
67   friend class Model_Document;
68   friend class Model_Objects;
69   friend class Model_Update;
70   friend class Model_AttributeReference;
71   friend class Model_AttributeRefAttr;
72   friend class Model_AttributeRefList;
73   friend class Model_AttributeSelection;
74   friend class Model_AttributeSelectionList;
75
76  public:
77   /// The simplest constructor. "setLabel" must be called just after to initialize correctly.
78   Model_Data();
79   /// Returns the name of the feature visible by the user in the object browser
80   MODEL_EXPORT virtual std::string name();
81   /// Defines the name of the feature visible by the user in the object browser
82   MODEL_EXPORT virtual void setName(const std::string& theName);
83   /// Returns the attribute that references to another document
84   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID);
85   /// Returns the attribute that contains real value with double precision
86   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID);
87   /// Returns the attribute that contains integer value
88   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeInteger>
89     integer(const std::string& theID);
90   /// Returns the attribute that contains reference to a feature
91   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeReference>
92     reference(const std::string& theID);
93   /// Returns the attribute that contains selection to a shape
94   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelection>
95     selection(const std::string& theID);
96   /// Returns the attribute that contains selection to a shape
97   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelectionList> 
98     selectionList(const std::string& theID);
99   /// Returns the attribute that contains reference to an attribute of a feature
100   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttr>
101     refattr(const std::string& theID);
102   /// Returns the attribute that contains list of references to features
103   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefList>
104     reflist(const std::string& theID);
105   /// Returns the attribute that contains boolean value
106   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeBoolean>
107     boolean(const std::string& theID);
108   /// Returns the attribute that contains real value with double precision
109   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeString>
110     string(const std::string& theID);
111   /// Returns the attribute that contains integer values array
112   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeIntArray>
113     intArray(const std::string& theID);
114
115   /// Returns the generic attribute by identifier
116   /// \param theID identifier of the attribute
117   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID);
118   /// Returns all attributes of the feature of the given type
119   /// or all attributes if "theType" is empty
120   MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Attribute> >
121     attributes(const std::string& theType);
122   /// Returns all attributes ids of the feature of the given type
123   /// or all attributes if "theType" is empty
124   MODEL_EXPORT virtual std::list<std::string> attributesIDs(const std::string& theType);
125
126   /// Identifier by the id (not fast, iteration by map)
127   /// \param theAttr attribute already created in this data
128   MODEL_EXPORT virtual const std::string& id(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
129   /// Returns true if data belongs to same features
130   MODEL_EXPORT virtual bool isEqual(const std::shared_ptr<ModelAPI_Data>& theData);
131   /// Returns true if it is correctly connected t othe data model
132   MODEL_EXPORT virtual bool isValid();
133
134   /// Returns the label where the shape must be stored (used in ResultBody)
135   TDF_Label& shapeLab()
136   {
137     return myLab;
138   }
139
140   /// Initializes object by the attributes: must be called just after the object is created
141   /// for each attribute of the object
142   /// \param theID identifier of the attribute that can be referenced by this ID later
143   /// \param theAttrType type of the created attribute (received from the type method)
144   /// \returns the just created attribute
145   MODEL_EXPORT virtual AttributePtr 
146     addAttribute(const std::string& theID, const std::string theAttrType);
147
148   /// Useful method for "set" methods of the attributes: sends an UPDATE event and
149   /// makes attribute initialized
150   MODEL_EXPORT virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr);
151   /// Blocks sending "attribute updated" if theBlock is true
152   MODEL_EXPORT virtual void blockSendAttributeUpdated(const bool theBlock);
153
154   /// Puts feature to the document data sub-structure
155   MODEL_EXPORT void setLabel(TDF_Label theLab);
156
157   /// Sets the object of this data
158   MODEL_EXPORT virtual void setObject(ObjectPtr theObject)
159   {
160     myObject = theObject;
161   }
162
163   /// Erases all the data from the data model
164   MODEL_EXPORT virtual void erase();
165
166   /// Stores the state of the object to execute it later accordingly
167   MODEL_EXPORT virtual void execState(const ModelAPI_ExecState theState);
168
169   /// Returns the state of the latest execution of the feature
170   MODEL_EXPORT virtual ModelAPI_ExecState execState();
171
172   /// Registers error during the execution, causes the ExecutionFailed state
173   MODEL_EXPORT virtual void setError(const std::string& theError, bool theSend = true);
174
175   /// Erases the error string if it is not empty
176   void eraseErrorString();
177
178   /// Registers error during the execution, causes the ExecutionFailed state
179   MODEL_EXPORT virtual std::string error() const;
180
181   /// Returns the identifier of feature-owner, unique in this document
182   MODEL_EXPORT virtual int featureId() const;
183
184   /// returns all objects referenced to this
185   MODEL_EXPORT virtual const std::set<AttributePtr>& refsToMe() {return myRefsToMe;}
186
187   /// returns all references by attributes of this data
188   /// \param theRefs returned list of pairs: id of referenced attribute and list of referenced objects
189   MODEL_EXPORT virtual void referencesToObjects(
190     std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs);
191
192   /// Copies all atributes content into theTarget data
193   MODEL_EXPORT virtual void copyTo(std::shared_ptr<ModelAPI_Data> theTarget);
194
195   /// Returns the invalid data pointer (to avoid working with NULL shared ptrs in swig)
196   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Data> invalidPtr();
197
198   /// Returns the invalid data pointer: static method
199   static std::shared_ptr<ModelAPI_Data> invalidData();
200
201   /// Identifier of the transaction when object (feature or result) was updated last time.
202   MODEL_EXPORT virtual int updateID();
203
204   /// Identifier of the transaction when object (feature or result) was updated last time.
205   /// This method is called by the updater.
206   MODEL_EXPORT virtual void setUpdateID(const int theID);
207
208   /// Returns true if the given object is owner of this data (needed for correct erase of object
209   /// with duplicated data)
210   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> owner();
211
212 protected:
213   /// Returns true if "is in history" custom behaviors is defined for the feature
214   MODEL_EXPORT virtual bool isInHistory();
215
216   /// Defines the custom "is in history" behavior
217   MODEL_EXPORT virtual void setIsInHistory(const bool theFlag);
218
219   /// Returns true if the object is deleted, but some data is still keept in memory
220   MODEL_EXPORT virtual bool isDeleted();
221
222   /// Sets true if the object is deleted, but some data is still keept in memory
223   MODEL_EXPORT virtual void setIsDeleted(const bool theFlag);
224
225 private:
226   /// Removes all information about back references
227   void eraseBackReferences();
228   /// Removes a back reference (with identifier which attribute references to this object)
229   /// \param theFeature feature referenced to this
230   /// \param theAttrID identifier of the attribute that is references from theFeature to this
231   void removeBackReference(FeaturePtr theFeature, std::string theAttrID);
232   /// Removes a back reference (by the attribute)
233   /// \param theAttr the referenced attribute
234   void removeBackReference(AttributePtr theAttr);
235   /// Adds a back reference (with identifier which attribute references to this object
236   /// \param theFeature feature referenced to this
237   /// \param theAttrID identifier of the attribute that is references from theFeature to this
238   /// \param theApplyConcealment applies consealment flag changes
239   void addBackReference(FeaturePtr theFeature, std::string theAttrID, 
240     const bool theApplyConcealment = true);
241
242   /// Makes the concealment flag up to date for this object-owner.
243   MODEL_EXPORT virtual void updateConcealmentFlag();
244
245   /// Returns true if object must be displayed in the viewer: flag is stored in the
246   /// data model, so on undo/redo, open/save or recreation of object by history-playing it keeps
247   /// the original state i nthe current transaction.
248   MODEL_EXPORT virtual bool isDisplayed();
249
250   /// Sets the displayed/hidden state of the object. If it is changed, sends the "redisplay"
251   /// signal.
252   MODEL_EXPORT virtual void setDisplayed(const bool theDisplay);
253 };
254
255 /// Generic method to register back reference, used in referencing attributes.
256 /// Without concealment change, it will be done later, on synchronization.
257 #define ADD_BACK_REF(TARGET) \
258   if (TARGET.get() != NULL) { \
259     FeaturePtr anAttributeOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
260     if (anAttributeOwnerFeature.get()) { \
261       std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
262         (TARGET)->data()); \
263       aTargetData->addBackReference(anAttributeOwnerFeature, id(), false); \
264     } \
265   }
266
267 /// Generic method to unregister back reference, used in referencing attributes.
268 /// Without concealment change, it will be done later, on synchronization.
269 #define REMOVE_BACK_REF(TARGET) \
270   if (TARGET.get() != NULL) { \
271     FeaturePtr anAttOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
272     if (anAttOwnerFeature.get()) { \
273       std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
274         (TARGET)->data()); \
275       aTargetData->removeBackReference(anAttOwnerFeature, id()); \
276     } \
277   }
278
279 #endif