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