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