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