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