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