Salome HOME
Implementation of "isDisplayed" persistent flag
[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
59   Model_Data();
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
75  public:
76   /// Returns the name of the feature visible by the user in the object browser
77   MODEL_EXPORT virtual std::string name();
78   /// Defines the name of the feature visible by the user in the object browser
79   MODEL_EXPORT virtual void setName(const std::string& theName);
80   /// Returns the attribute that references to another document
81   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID);
82   /// Returns the attribute that contains real value with double precision
83   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID);
84   /// Returns the attribute that contains integer value
85   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeInteger>
86     integer(const std::string& theID);
87   /// Returns the attribute that contains reference to a feature
88   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeReference>
89     reference(const std::string& theID);
90   /// Returns the attribute that contains selection to a shape
91   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelection>
92     selection(const std::string& theID);
93   /// Returns the attribute that contains selection to a shape
94   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelectionList> 
95     selectionList(const std::string& theID);
96   /// Returns the attribute that contains reference to an attribute of a feature
97   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttr>
98     refattr(const std::string& theID);
99   /// Returns the attribute that contains list of references to features
100   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefList>
101     reflist(const std::string& theID);
102   /// Returns the attribute that contains boolean value
103   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeBoolean>
104     boolean(const std::string& theID);
105   /// Returns the attribute that contains real value with double precision
106   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeString>
107     string(const std::string& theID);
108   /// Returns the attribute that contains integer values array
109   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeIntArray>
110     intArray(const std::string& theID);
111
112   /// Returns the generic attribute by identifier
113   /// \param theID identifier of the attribute
114   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID);
115   /// Returns all attributes of the feature of the given type
116   /// or all attributes if "theType" is empty
117   MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Attribute> >
118     attributes(const std::string& theType);
119   /// Returns all attributes ids of the feature of the given type
120   /// or all attributes if "theType" is empty
121   MODEL_EXPORT virtual std::list<std::string> attributesIDs(const std::string& theType);
122
123   /// Identifier by the id (not fast, iteration by map)
124   /// \param theAttr attribute already created in this data
125   MODEL_EXPORT virtual const std::string& id(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
126   /// Returns true if data belongs to same features
127   MODEL_EXPORT virtual bool isEqual(const std::shared_ptr<ModelAPI_Data>& theData);
128   /// Returns true if it is correctly connected t othe data model
129   MODEL_EXPORT virtual bool isValid();
130
131   /// Returns the label where the shape must be stored (used in ResultBody)
132   TDF_Label& shapeLab()
133   {
134     return myLab;
135   }
136
137   /// Initializes object by the attributes: must be called just after the object is created
138   /// for each attribute of the object
139   /// \param theID identifier of the attribute that can be referenced by this ID later
140   /// \param theAttrType type of the created attribute (received from the type method)
141   /// \returns the just created attribute
142   MODEL_EXPORT virtual AttributePtr 
143     addAttribute(const std::string& theID, const std::string theAttrType);
144
145   /// Useful method for "set" methods of the attributes: sends an UPDATE event and
146   /// makes attribute initialized
147   MODEL_EXPORT virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr);
148   /// Blocks sending "attribute updated" if theBlock is true
149   MODEL_EXPORT virtual void blockSendAttributeUpdated(const bool theBlock);
150
151   /// Puts feature to the document data sub-structure
152   MODEL_EXPORT void setLabel(TDF_Label theLab);
153
154   /// Sets the object of this data
155   MODEL_EXPORT virtual void setObject(ObjectPtr theObject)
156   {
157     myObject = theObject;
158   }
159
160   /// Erases all the data from the data model
161   MODEL_EXPORT virtual void erase();
162
163   /// Stores the state of the object to execute it later accordingly
164   MODEL_EXPORT virtual void execState(const ModelAPI_ExecState theState);
165
166   /// Returns the state of the latest execution of the feature
167   MODEL_EXPORT virtual ModelAPI_ExecState execState();
168
169   /// Registers error during the execution, causes the ExecutionFailed state
170   MODEL_EXPORT virtual void setError(const std::string& theError, bool theSend = true);
171
172   /// Registers error during the execution, causes the ExecutionFailed state
173   MODEL_EXPORT virtual std::string error() const;
174
175   /// Returns the identifier of feature-owner, unique in this document
176   MODEL_EXPORT virtual int featureId() const;
177
178   /// returns all objects referenced to this
179   MODEL_EXPORT virtual const std::set<AttributePtr>& refsToMe() {return myRefsToMe;}
180
181   /// returns all references by attributes of this data
182   /// \param theRefs returned list of pairs: id of referenced attribute and list of referenced objects
183   MODEL_EXPORT virtual void referencesToObjects(
184     std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs);
185
186   /// Copies all atributes content into theTarget data
187   MODEL_EXPORT virtual void copyTo(std::shared_ptr<ModelAPI_Data> theTarget);
188
189 protected:
190   /// Returns true if "is in history" custom behaviors is defined for the feature
191   MODEL_EXPORT virtual bool isInHistory();
192
193   /// Defines the custom "is in history" behavior
194   MODEL_EXPORT virtual void setIsInHistory(const bool theFlag);
195
196 private:
197   /// Removes all information about back references
198   void eraseBackReferences();
199   /// Adds a back reference (with identifier which attribute references to this object
200   /// It does not change the consealment flag of the data object result
201   /// \param theFeature feature referenced to this
202   /// \param theAttrID identifier of the attribute that is references from theFeature to this
203   void removeBackReference(FeaturePtr theFeature, std::string theAttrID);
204   /// Adds a back reference (with identifier which attribute references to this object
205   /// \param theFeature feature referenced to this
206   /// \param theAttrID identifier of the attribute that is references from theFeature to this
207   /// \param theApplyConcealment applies consealment flag changes
208   void addBackReference(FeaturePtr theFeature, std::string theAttrID, 
209     const bool theApplyConcealment = true);
210
211   /// Returns true if object must be displayed in the viewer: flag is stored in the
212   /// data model, so on undo/redo, open/save or recreation of object by history-playing it keeps
213   /// the original state i nthe current transaction.
214   MODEL_EXPORT virtual bool isDisplayed();
215
216   /// Sets the displayed/hidden state of the object. If it is changed, sends the "redisplay"
217   /// signal.
218   MODEL_EXPORT virtual void setDisplayed(const bool theDisplay);
219 };
220
221 /// Generic method to register back reference, used in referencing attributes.
222 /// Without concealment change, it will be done later, on synchronization.
223 #define ADD_BACK_REF(TARGET) \
224   if (TARGET.get() != NULL) { \
225     FeaturePtr anAttributeOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
226     if (anAttributeOwnerFeature.get()) { \
227       std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
228         (TARGET)->data()); \
229       aTargetData->addBackReference(anAttributeOwnerFeature, id(), false); \
230     } \
231   }
232
233 /// Generic method to unregister back reference, used in referencing attributes.
234 /// Without concealment change, it will be done later, on synchronization.
235 #define REMOVE_BACK_REF(TARGET) \
236   if (TARGET.get() != NULL) { \
237     FeaturePtr anAttOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
238     if (anAttOwnerFeature.get()) { \
239       std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
240         (TARGET)->data()); \
241       aTargetData->removeBackReference(anAttOwnerFeature, id()); \
242     } \
243   }
244
245 #endif