Salome HOME
d8120a5b7c35ba51e3c5e85656b7d11c00847fad
[modules/shaper.git] / src / ModelAPI / ModelAPI_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 ModelAPI_Data_H_
21 #define ModelAPI_Data_H_
22
23 #ifdef _WINDOWS // to avoid too long decorated name warning
24 #pragma warning( disable : 4503 )
25 #endif
26
27 #include "ModelAPI.h"
28 #include <string>
29 #include <list>
30 #include <set>
31 #include <memory>
32
33 class ModelAPI_Attribute;
34 class ModelAPI_AttributeDocRef;
35 class ModelAPI_AttributeInteger;
36 class ModelAPI_AttributeDouble;
37 class ModelAPI_AttributeDoubleArray;
38 class ModelAPI_AttributeReference;
39 class ModelAPI_AttributeRefAttr;
40 class ModelAPI_AttributeRefList;
41 class ModelAPI_AttributeRefAttrList;
42 class ModelAPI_AttributeBoolean;
43 class ModelAPI_AttributeString;
44 class ModelAPI_AttributeStringArray;
45 class ModelAPI_Document;
46 class ModelAPI_Attribute;
47 class ModelAPI_Feature;
48 class ModelAPI_AttributeSelection;
49 class ModelAPI_AttributeSelectionList;
50 class ModelAPI_AttributeIntArray;
51 class ModelAPI_AttributeImage;
52 class ModelAPI_AttributeTables;
53 class ModelAPI_Object;
54 class GeomAPI_Shape;
55
56 /// Enumeration that contains the execution status of the Object
57 enum ModelAPI_ExecState {
58   ModelAPI_StateDone, ///< execution was performed and result is up to date
59   ModelAPI_StateMustBeUpdated, ///< execution must be performed to obtain the up to date result
60   ModelAPI_StateExecFailed, ///< execution was failed (results are deleted in this case)
61   ModelAPI_StateInvalidArgument, ///< execution was not performed (results are deleted in this case)
62   ModelAPI_StateNothing ///< internal state that actually means that nothing must be changed
63 };
64
65 /**\class ModelAPI_Data
66  * \ingroup DataModel
67  * \brief General object of the application that allows
68  * to get/set attributes from the document and compute result of an operation.
69  */
70
71 class MODELAPI_EXPORT ModelAPI_Data
72 {
73  public:
74
75   /// Returns the name of the feature visible by the user in the object browser
76   virtual std::wstring name() = 0;
77   /// Defines the name of the feature visible by the user in the object browser
78   virtual void setName(const std::wstring& theName) = 0;
79   /// Return \c true if the object has been renamed by the user
80   virtual bool hasUserDefinedName() const = 0;
81
82   /// Returns version of the feature (empty string if not applicable)
83   virtual std::string version() = 0;
84   /// Initialize the version of the feature
85   virtual void setVersion(const std::string& theVersion) = 0;
86
87   /// Returns the attribute that references to another document
88   virtual std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID) = 0;
89   /// Returns the attribute that contains real value with double precision
90   virtual std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID) = 0;
91   /// Returns the attribute that contains double values array
92   virtual std::shared_ptr<ModelAPI_AttributeDoubleArray> realArray(const std::string& theID) = 0;
93   /// Returns the attribute that contains integer value
94   virtual std::shared_ptr<ModelAPI_AttributeInteger> integer(const std::string& theID) = 0;
95   /// Returns the attribute that contains reference to a feature
96   virtual std::shared_ptr<ModelAPI_AttributeReference> reference(const std::string& theID) = 0;
97   /// Returns the attribute that contains selection to a shape
98   virtual std::shared_ptr<ModelAPI_AttributeSelection> selection(const std::string& theID) = 0;
99   /// Returns the attribute that contains selection to a shape
100   virtual std::shared_ptr<ModelAPI_AttributeSelectionList>
101     selectionList(const std::string& theID) = 0;
102   /// Returns the attribute that contains reference to an attribute of a feature
103   virtual std::shared_ptr<ModelAPI_AttributeRefAttr> refattr(const std::string& theID) = 0;
104   /// Returns the attribute that contains list of references to features
105   virtual std::shared_ptr<ModelAPI_AttributeRefList> reflist(const std::string& theID) = 0;
106   /// Returns the attribute that contains list of references to features or reference to
107   /// an attribute of a feature
108   virtual std::shared_ptr<ModelAPI_AttributeRefAttrList> refattrlist(const std::string& theID) = 0;
109   /// Returns the attribute that contains boolean value
110   virtual std::shared_ptr<ModelAPI_AttributeBoolean> boolean(const std::string& theID) = 0;
111   /// Returns the attribute that contains boolean value
112   virtual std::shared_ptr<ModelAPI_AttributeString> string(const std::string& theID) = 0;
113   /// Returns the attribute that contains integer values array
114   virtual std::shared_ptr<ModelAPI_AttributeIntArray> intArray(const std::string& theID) = 0;
115   /// Returns the attribute that contains string values array
116   virtual std::shared_ptr<ModelAPI_AttributeStringArray> stringArray(const std::string& theID) = 0;
117   /// Returns the attribute that contains image
118   virtual std::shared_ptr<ModelAPI_AttributeImage> image(const std::string& theID) = 0;
119   /// Returns the attribute that contains tables
120   virtual std::shared_ptr<ModelAPI_AttributeTables> tables(const std::string& theID) = 0;
121
122   /// Returns the generic attribute by identifier
123   /// \param theID identifier of the attribute
124   virtual std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID) = 0;
125   /// Returns all attributes of the feature of the given type
126   /// or all attributes if "theType" is empty
127   virtual std::list<std::shared_ptr<ModelAPI_Attribute> >
128   attributes(const std::string& theType) = 0;
129   /// Returns all attributes ids of the feature of the given type
130   /// or all attributes if "theType" is empty
131   virtual std::list<std::string> attributesIDs(const std::string& theType) = 0;
132   /// Identifier by the id (not fast, iteration by map)
133   /// \param theAttr attribute already created in this data
134   virtual const std::string& id(const std::shared_ptr<ModelAPI_Attribute>& theAttr) = 0;
135   /// Returns true if data belongs to same features
136   virtual bool isEqual(const std::shared_ptr<ModelAPI_Data>& theData) = 0;
137   /// Returns true if it is correctly connected to the data model
138   virtual bool isValid() = 0;
139
140   /// Initializes object by the attributes: must be called just after the object is created
141   /// for each attribute of the object
142   /// \param theID identifier of the attribute that can be referenced by this ID later
143   /// \param theAttrType type of the created attribute (received from the type method)
144   /// \param theIndex index of the attribute in the internal data structure, for not-floating
145   ///                 attributes it is -1 to let it automatically be added
146   /// \returns the just created attribute
147   virtual std::shared_ptr<ModelAPI_Attribute> addAttribute(
148     const std::string& theID, const std::string theAttrType, const int theIndex = -1) = 0;
149
150   /// Adds a floating attribute (that may be added/removed during the data life)
151   /// \param theID identifier of the attribute that can be referenced by this ID later
152   /// \param theAttrType type of the created attribute (received from the type method)
153   /// \param theGroup identifier of the group this attribute belongs to, may be an empty string
154   virtual std::shared_ptr<ModelAPI_Attribute>
155     addFloatingAttribute(const std::string& theID, const std::string theAttrType,
156       const std::string& theGroup) = 0;
157
158   /// Returns all groups of this data (ordered).
159   virtual void allGroups(std::list<std::string>& theGroups) = 0;
160
161   /// Returns an ordered list of attributes that belong to the given group
162   virtual void attributesOfGroup(const std::string& theGroup,
163     std::list<std::shared_ptr<ModelAPI_Attribute> >& theAttrs) = 0;
164
165   /// Remove all attributes of the given group
166   virtual void removeAttributes(const std::string& theGroup) = 0;
167
168   /// Useful method for "set" methods of the attributes: sends an UPDATE event and
169   /// makes attribute initialized
170   virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr) = 0;
171   /// Blocks sending "attribute updated" if theBlock is true
172   /// \param theID identifier of the attribute that can be referenced by this ID later
173   /// \param theAttrType type of the created attribute (received from the type method)
174   /// \returns the previous state of block
175   virtual bool blockSendAttributeUpdated(
176     const bool theBlock, const bool theSendMessage = true) = 0;
177
178   /// Erases all the data from the data model
179   virtual void erase() = 0;
180
181   /// To virtually destroy the fields of successors
182   virtual ~ModelAPI_Data();
183
184   /// Stores the state of the object to execute it later accordingly
185   virtual void execState(const ModelAPI_ExecState theState) = 0;
186
187   /// Returns the state of the latest execution of the feature
188   virtual ModelAPI_ExecState execState() = 0;
189
190   /// Registers error during the execution, causes the ExecutionFailed state
191   virtual void setError(const std::string& theError, bool theSend = true) = 0;
192
193   /// Returns error, arose during the execution
194   virtual std::string error() const = 0;
195
196   /// Returns the identifier of feature-owner, unique in this document
197   virtual int featureId() const = 0;
198
199   /// returns all objects referenced to this
200   virtual const std::set<std::shared_ptr<ModelAPI_Attribute> >& refsToMe() = 0;
201
202   /// returns all references by attributes of this data
203   /// \param theRefs returned list of pairs: id of referenced attribute and
204   ///                list of referenced objects
205   virtual void referencesToObjects(
206     std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >& theRefs) =0;
207
208   /// Copies all attributes content into theTarget data
209   virtual void copyTo(std::shared_ptr<ModelAPI_Data> theTarget) = 0;
210
211   /// Returns the invalid data pointer (to avoid working with NULL shared pointers in swig)
212   virtual std::shared_ptr<ModelAPI_Data> invalidPtr() = 0;
213
214   /// Identifier of the transaction when object (feature or result) was updated last time.
215   virtual int updateID() = 0;
216
217   /// Identifier of the transaction when object (feature or result) was updated last time.
218   /// This method is called by the updater.
219   virtual void setUpdateID(const int theID) = 0;
220
221   /// Returns the owner of this data
222   virtual std::shared_ptr<ModelAPI_Object> owner() = 0;
223
224   /// Returns true if the object is deleted, but some data is still kept in memory
225   virtual bool isDeleted() = 0;
226
227   /// Sets true if the object is deleted, but some data is still kept in memory
228   virtual void setIsDeleted(const bool theFlag) = 0;
229
230   /// Returns \c true if theAttribute1 is going earlier than theAttribute2 in the data
231   virtual bool isPrecedingAttribute(const std::string& theAttribute1,
232                                     const std::string& theAttribute2) const = 0;
233
234  protected:
235   /// Objects are created for features automatically
236   ModelAPI_Data();
237
238   /// Returns true if "is in history" custom behaviors is defined for the feature
239   virtual bool isInHistory() = 0;
240
241   /// Defines the custom "is in history" behavior
242   virtual void setIsInHistory(const bool theFlag) = 0;
243
244   /// Returns true if object must be displayed in the viewer: flag is stored in the
245   /// data model, so on undo/redo, open/save or recreation of object by history-playing it keeps
246   /// the original state in the current transaction.
247   virtual bool isDisplayed() = 0;
248
249   /// Sets the displayed/hidden state of the object. If it is changed, sends the "redisplay"
250   /// signal.
251   virtual void setDisplayed(const bool theDisplay) = 0;
252
253   friend class ModelAPI_Object;
254 };
255
256 typedef std::shared_ptr<ModelAPI_Data> DataPtr;
257
258 #endif