Salome HOME
Merge branch 'csgroup_IS2'
[modules/shaper.git] / src / Model / Model_Data.h
1 // Copyright (C) 2014-2021  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 email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef Model_Data_H_
21 #define Model_Data_H_
22
23 #include <Model.h>
24 #include <ModelAPI_Attribute.h>
25 #include <ModelAPI_AttributeBoolean.h>
26 #include <ModelAPI_AttributeDocRef.h>
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_AttributeInteger.h>
29 #include <ModelAPI_AttributeRefAttr.h>
30 #include <ModelAPI_AttributeReference.h>
31 #include <ModelAPI_AttributeRefList.h>
32 #include <ModelAPI_AttributeRefAttrList.h>
33 #include <ModelAPI_AttributeString.h>
34 #include <ModelAPI_AttributeStringArray.h>
35 #include <ModelAPI_AttributeIntArray.h>
36 #include <ModelAPI_Data.h>
37 #include <ModelAPI_Feature.h>
38 #include <ModelAPI_Folder.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   typedef std::map<std::string, std::pair<std::shared_ptr<ModelAPI_Attribute>, int> > AttributeMap;
62
63   TDF_Label myLab;  ///< label of the feature in the document
64   /// All attributes of the object identified by the attribute ID
65   /// (the attribute is stored together with its index in the feature)
66   AttributeMap myAttrs;
67   /// Array of flags of this data
68   Handle(TDataStd_BooleanArray) myFlags;
69
70   /// needed here to emit signal that object changed on change of the attribute
71   ObjectPtr myObject;
72
73   /// List of attributes referenced to owner (updated only during the transaction change)
74   std::set<AttributePtr> myRefsToMe;
75   /// flag that may block the "attribute updated" sending
76   bool mySendAttributeUpdated;
77   /// if some attribute was changed, but mySendAttributeUpdated was false, this stores this
78   std::list<ModelAPI_Attribute*> myWasChangedButBlocked;
79
80   /// Returns label of this feature
81   TDF_Label label()
82   {
83     return myLab;
84   }
85
86   friend class Model_Document;
87   friend class Model_Objects;
88   friend class Model_Update;
89   friend class Model_AttributeReference;
90   friend class Model_AttributeRefAttr;
91   friend class Model_AttributeRefList;
92   friend class Model_AttributeRefAttrList;
93   friend class Model_AttributeSelection;
94   friend class Model_AttributeSelectionList;
95   friend class Model_ValidatorsFactory;
96   friend class Model_SelectionNaming;
97   friend class Model_ResultConstruction;
98   friend class Model_ResultBody;
99   friend class Model_Tools;
100
101  public:
102   /// The simplest constructor. "setLabel" must be called just after to initialize correctly.
103   Model_Data();
104   /// Returns the name of the feature visible by the user in the object browser
105   MODEL_EXPORT virtual std::wstring name();
106   /// Defines the name of the feature visible by the user in the object browser
107   MODEL_EXPORT virtual void setName(const std::wstring& theName);
108   /// Return \c true if the object has been renamed by the user
109   MODEL_EXPORT virtual bool hasUserDefinedName() const;
110   /// Returns version of the feature (empty string if not applicable)
111   MODEL_EXPORT virtual std::string version();
112   /// Initialize the version of the feature
113   MODEL_EXPORT virtual void setVersion(const std::string& theVersion);
114   /// Returns the attribute that references to another document
115   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID);
116   /// Returns the attribute that contains real value with double precision
117   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID);
118   /// Returns the attribute that contains double values array
119   MODEL_EXPORT virtual
120     std::shared_ptr<ModelAPI_AttributeDoubleArray> realArray(const std::string& theID);
121   /// Returns the attribute that contains integer value
122   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeInteger>
123     integer(const std::string& theID);
124   /// Returns the attribute that contains reference to a feature
125   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeReference>
126     reference(const std::string& theID);
127   /// Returns the attribute that contains selection to a shape
128   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelection>
129     selection(const std::string& theID);
130   /// Returns the attribute that contains selection to a shape
131   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelectionList>
132     selectionList(const std::string& theID);
133   /// Returns the attribute that contains reference to an attribute of a feature
134   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttr>
135     refattr(const std::string& theID);
136   /// Returns the attribute that contains list of references to features
137   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefList>
138     reflist(const std::string& theID);
139   /// Returns the attribute that contains list of references to features
140   /// or reference to an attribute of a feature
141   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttrList>
142     refattrlist(const std::string& theID);
143   /// Returns the attribute that contains boolean value
144   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeBoolean>
145     boolean(const std::string& theID);
146   /// Returns the attribute that contains real value with double precision
147   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeString>
148     string(const std::string& theID);
149   /// Returns the attribute that contains integer values array
150   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeIntArray>
151     intArray(const std::string& theID);
152   /// Returns the attribute that contains string values array
153   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeStringArray>
154     stringArray(const std::string& theID);
155   /// Returns the attribute that contains string values array
156   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeTables>
157     tables(const std::string& theID);
158
159   /// Returns the generic attribute by identifier
160   /// \param theID identifier of the attribute
161   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID);
162   /// Returns all attributes of the feature of the given type
163   /// or all attributes if "theType" is empty
164   MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Attribute> >
165     attributes(const std::string& theType);
166   /// Returns all attributes ids of the feature of the given type
167   /// or all attributes if "theType" is empty
168   MODEL_EXPORT virtual std::list<std::string> attributesIDs(const std::string& theType);
169
170   /// Identifier by the id (not fast, iteration by map)
171   /// \param theAttr attribute already created in this data
172   MODEL_EXPORT virtual const std::string& id(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
173   /// Returns true if data belongs to same features
174   MODEL_EXPORT virtual bool isEqual(const std::shared_ptr<ModelAPI_Data>& theData);
175   /// Returns true if it is correctly connected to the data model
176   MODEL_EXPORT virtual bool isValid();
177
178   /// Returns the label where the shape must be stored (used in ResultBody)
179   TDF_Label shapeLab() const
180   {
181     return myLab.IsNull() ? myLab : myLab.Father().FindChild(2);
182   }
183
184   /// Initializes object by the attributes: must be called just after the object is created
185   /// for each attribute of the object
186   /// \param theID identifier of the attribute that can be referenced by this ID later
187   /// \param theAttrType type of the created attribute (received from the type method)
188   /// \param theIndex index of the attribute in the internal data structure, for not-floating
189   ///                 attributes it is -1 to let it automatically be added
190   /// \returns the just created attribute
191   MODEL_EXPORT virtual AttributePtr
192     addAttribute(const std::string& theID, const std::string theAttrType, const int theIndex = -1);
193
194   /// Adds a floating attribute (that may be added/removed during the data life)
195   /// \param theID identifier of the attribute that can be referenced by this ID later
196   /// \param theAttrType type of the created attribute (received from the type method)
197   /// \param theGroup identifier of the group this attribute belongs to, may be an empty string
198   MODEL_EXPORT virtual AttributePtr
199     addFloatingAttribute(const std::string& theID, const std::string theAttrType,
200       const std::string& theGroup);
201
202   /// Returns all groups of this data (ordered).
203   MODEL_EXPORT virtual void allGroups(std::list<std::string>& theGroups);
204
205   /// Returns an ordered list of attributes that belong to the given group
206   MODEL_EXPORT virtual void attributesOfGroup(const std::string& theGroup,
207     std::list<std::shared_ptr<ModelAPI_Attribute> >& theAttrs);
208
209   /// Remove all attributes of the given group
210   MODEL_EXPORT virtual void removeAttributes(const std::string& theGroup);
211
212   /// Useful method for "set" methods of the attributes: sends an UPDATE event and
213   /// makes attribute initialized
214   MODEL_EXPORT virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr);
215   /// Blocks sending "attribute updated" if theBlock is true
216   /// \param theBlock allows switching on/off the blocking state
217   /// \param theSendMessage if false, it does not send the update message
218   ///            even if something is changed
219   ///            (normally is it used in attributeChanged because this message will be sent anyway)
220   /// \returns the previous state of block
221   MODEL_EXPORT virtual bool blockSendAttributeUpdated(
222     const bool theBlock, const bool theSendMessage = true);
223
224   /// Puts feature to the document data sub-structure
225   MODEL_EXPORT void setLabel(TDF_Label theLab);
226
227   /// Sets the object of this data
228   MODEL_EXPORT virtual void setObject(ObjectPtr theObject)
229   {
230     myObject = theObject;
231   }
232
233   /// Erases all the data from the data model
234   MODEL_EXPORT virtual void erase();
235
236   /// Stores the state of the object to execute it later accordingly
237   MODEL_EXPORT virtual void execState(const ModelAPI_ExecState theState);
238
239   /// Returns the state of the latest execution of the feature
240   MODEL_EXPORT virtual ModelAPI_ExecState execState();
241
242   /// Registers error during the execution, causes the ExecutionFailed state
243   MODEL_EXPORT virtual void setError(const std::string& theError, bool theSend = true);
244
245   /// Erases the error string if it is not empty
246   void eraseErrorString();
247
248   /// Registers error during the execution, causes the ExecutionFailed state
249   MODEL_EXPORT virtual std::string error() const;
250
251   /// Returns the identifier of feature-owner, unique in this document
252   MODEL_EXPORT virtual int featureId() const;
253
254   /// returns all objects referenced to this
255   MODEL_EXPORT virtual const std::set<AttributePtr>& refsToMe() {return myRefsToMe;}
256
257   /// returns all references by attributes of this data
258   /// \param theRefs returned list of pairs:
259   ///                id of referenced attribute and list of referenced objects
260   MODEL_EXPORT virtual void referencesToObjects(
261     std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs);
262
263   /// Copies all attributes content into theTarget data
264   MODEL_EXPORT virtual void copyTo(std::shared_ptr<ModelAPI_Data> theTarget);
265
266   /// Returns the invalid data pointer (to avoid working with NULL shared pointers in swig)
267   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Data> invalidPtr();
268
269   /// Returns the invalid data pointer: static method
270   static std::shared_ptr<ModelAPI_Data> invalidData();
271
272   /// Identifier of the transaction when object (feature or result) was updated last time.
273   MODEL_EXPORT virtual int updateID();
274
275   /// Identifier of the transaction when object (feature or result) was updated last time.
276   /// This method is called by the updater.
277   MODEL_EXPORT virtual void setUpdateID(const int theID);
278
279   /// Returns true if the given object is owner of this data (needed for correct erase of object
280   /// with duplicated data)
281   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> owner();
282
283 protected:
284   /// Returns true if "is in history" custom behaviors is defined for the feature
285   MODEL_EXPORT virtual bool isInHistory();
286
287   /// Defines the custom "is in history" behavior
288   MODEL_EXPORT virtual void setIsInHistory(const bool theFlag);
289
290   /// Returns true if the object is deleted, but some data is still kept in memory
291   MODEL_EXPORT virtual bool isDeleted();
292
293   /// Sets true if the object is deleted, but some data is still kept in memory
294   MODEL_EXPORT virtual void setIsDeleted(const bool theFlag);
295
296   /// Erases all attributes from myAttrs, but keeping them in the data structure
297   void clearAttributes();
298
299 private:
300   /// Removes a back reference (with identifier which attribute references to this object)
301   /// \param theFeature feature referenced to this
302   /// \param theAttrID identifier of the attribute that is references from theFeature to this
303   void removeBackReference(ObjectPtr theObject, std::string theAttrID);
304   /// Removes a back reference (by the attribute)
305   /// \param theAttr the referenced attribute
306   void removeBackReference(AttributePtr theAttr);
307   /// Adds a back reference (with identifier which attribute references to this object
308   /// \param theFeature feature referenced to this
309   /// \param theAttrID identifier of the attribute that is references from theFeature to this
310   /// \param theApplyConcealment applies concealment flag changes
311   void addBackReference(FeaturePtr theFeature, std::string theAttrID,
312     const bool theApplyConcealment = true);
313   /// Adds a back reference to an object
314   /// \param theObject object referenced to this
315   /// \param theAttrID identifier of the attribute that is references from theFolder to this
316   void addBackReference(ObjectPtr theObject, std::string theAttrID);
317
318   /// Makes the concealment flag up to date for this object-owner.
319   MODEL_EXPORT virtual void updateConcealmentFlag();
320
321   /// Returns true if object must be displayed in the viewer: flag is stored in the
322   /// data model, so on undo/redo, open/save or recreation of object by history-playing it keeps
323   /// the original state in the current transaction.
324   MODEL_EXPORT virtual bool isDisplayed();
325
326   /// Sets the displayed/hidden state of the object. If it is changed, sends the "redisplay"
327   /// signal.
328   MODEL_EXPORT virtual void setDisplayed(const bool theDisplay);
329
330   /// Returns \c true if theAttribute1 is going earlier than theAttribute2 in the data
331   MODEL_EXPORT virtual bool isPrecedingAttribute(const std::string& theAttribute1,
332                                                  const std::string& theAttribute2) const;
333
334 };
335
336 /// Generic method to register back reference, used in referencing attributes.
337 /// Without concealment change, it will be done later, on synchronization.
338 #define ADD_BACK_REF(TARGET) \
339   if (TARGET.get() != NULL) { \
340     std::shared_ptr<Model_Data> aTargetData = \
341         std::dynamic_pointer_cast<Model_Data>((TARGET)->data()); \
342     FeaturePtr anAttributeOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
343     if (anAttributeOwnerFeature.get()) \
344       aTargetData->addBackReference(anAttributeOwnerFeature, id(), false); \
345     else { \
346       FolderPtr anAttributeOwnerFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(owner()); \
347       if (anAttributeOwnerFolder.get()) \
348         aTargetData->addBackReference(ObjectPtr(anAttributeOwnerFolder), id()); \
349     } \
350   }
351
352 /// Generic method to unregister back reference, used in referencing attributes.
353 /// Without concealment change, it will be done later, on synchronization.
354 #define REMOVE_BACK_REF(TARGET) \
355   if (TARGET.get() != NULL) { \
356     std::shared_ptr<Model_Data> aTargetData = \
357         std::dynamic_pointer_cast<Model_Data>((TARGET)->data()); \
358     FeaturePtr anAttOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
359     if (anAttOwnerFeature.get()) \
360       aTargetData->removeBackReference(anAttOwnerFeature, id()); \
361     else { \
362       FolderPtr anAttributeOwnerFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(owner()); \
363       if (anAttributeOwnerFolder.get()) \
364         aTargetData->removeBackReference(ObjectPtr(anAttributeOwnerFolder), id()); \
365     } \
366   }
367
368 #endif