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