Salome HOME
1d82ad14be9a89ccb4619adf152f3f955142b01e
[modules/shaper.git] / src / Model / Model_Data.h
1 // File:        Model_Data.hxx
2 // Created:     21 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #ifndef Model_Data_H_
6 #define Model_Data_H_
7
8 #include <Model.h>
9 #include <ModelAPI_Attribute.h>
10 #include <ModelAPI_AttributeBoolean.h>
11 #include <ModelAPI_AttributeDocRef.h>
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_AttributeInteger.h>
14 #include <ModelAPI_AttributeRefAttr.h>
15 #include <ModelAPI_AttributeReference.h>
16 #include <ModelAPI_AttributeRefList.h>
17 #include <ModelAPI_AttributeString.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Feature.h>
20 #include <ModelAPI_Object.h>
21
22 #include <TDF_Label.hxx>
23
24 #include <memory>
25
26 #include <map>
27 #include <list>
28 #include <string>
29 #include <set>
30
31 class ModelAPI_Attribute;
32
33 /**\class Model_Data
34  * \ingroup DataModel
35  * \brief General object of the application that allows
36  * to get/set attributes from the document and compute result of an operation.
37  */
38
39 class Model_Data : public ModelAPI_Data
40 {
41   TDF_Label myLab;  ///< label of the feature in the document
42   /// All attributes of the object identified by the attribute ID
43   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> > myAttrs;
44
45   /// needed here to emit signal that object changed on change of the attribute
46   ObjectPtr myObject;
47
48   /// List of attributes referenced to owner (updated only during the transaction change)
49   std::set<AttributePtr> myRefsToMe;
50
51   Model_Data();
52
53   /// Returns label of this feature
54   TDF_Label label()
55   {
56     return myLab;
57   }
58
59   friend class Model_Document;
60   friend class Model_Update;
61   friend class Model_AttributeReference;
62   friend class Model_AttributeRefAttr;
63   friend class Model_AttributeRefList;
64   friend class Model_AttributeSelection;
65
66  public:
67   /// Returns the name of the feature visible by the user in the object browser
68   MODEL_EXPORT virtual std::string name();
69   /// Defines the name of the feature visible by the user in the object browser
70   MODEL_EXPORT virtual void setName(const std::string& theName);
71   /// Returns the attribute that references to another document
72   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID);
73   /// Returns the attribute that contains real value with double precision
74   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID);
75   /// Returns the attribute that contains integer value
76   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeInteger>
77     integer(const std::string& theID);
78   /// Returns the attribute that contains reference to a feature
79   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeReference>
80     reference(const std::string& theID);
81   /// Returns the attribute that contains selection to a shape
82   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelection>
83     selection(const std::string& theID);
84   /// Returns the attribute that contains selection to a shape
85   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelectionList> 
86     selectionList(const std::string& theID);
87   /// Returns the attribute that contains reference to an attribute of a feature
88   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttr>
89     refattr(const std::string& theID);
90   /// Returns the attribute that contains list of references to features
91   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefList>
92     reflist(const std::string& theID);
93   /// Returns the attribute that contains boolean value
94   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeBoolean>
95     boolean(const std::string& theID);
96   /// Returns the attribute that contains real value with double precision
97   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeString>
98     string(const std::string& theID);
99   /// Returns the generic attribute by identifier
100   /// \param theID identifier of the attribute
101   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID);
102   /// Returns all attributes of the feature of the given type
103   /// or all attributes if "theType" is empty
104   MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Attribute> >
105     attributes(const std::string& theType);
106   /// Returns all attributes ids of the feature of the given type
107   /// or all attributes if "theType" is empty
108   MODEL_EXPORT virtual std::list<std::string> attributesIDs(const std::string& theType);
109
110   /// Identifier by the id (not fast, iteration by map)
111   /// \param theAttr attribute already created in this data
112   MODEL_EXPORT virtual const std::string& id(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
113   /// Returns true if data belongs to same features
114   MODEL_EXPORT virtual bool isEqual(const std::shared_ptr<ModelAPI_Data>& theData);
115   /// Returns true if it is correctly connected t othe data model
116   MODEL_EXPORT virtual bool isValid();
117
118   /// Returns the label where the shape must be stored (used in ResultBody)
119   TDF_Label& shapeLab()
120   {
121     return myLab;
122   }
123
124   /// Initializes object by the attributes: must be called just after the object is created
125   /// for each attribute of the object
126   /// \param theID identifier of the attribute that can be referenced by this ID later
127   /// \param theAttrType type of the created attribute (received from the type method)
128   MODEL_EXPORT virtual void addAttribute(const std::string& theID, const std::string theAttrType);
129
130   /// Useful method for "set" methods of the attributes: sends an UPDATE event and
131   /// makes attribute initialized
132   MODEL_EXPORT virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr);
133
134   /// Puts feature to the document data sub-structure
135   MODEL_EXPORT void setLabel(TDF_Label theLab);
136
137   /// Sets the object of this data
138   MODEL_EXPORT virtual void setObject(ObjectPtr theObject)
139   {
140     myObject = theObject;
141   }
142
143   MODEL_EXPORT virtual void erase();
144
145   /// Makes feature must be updated later (on rebuild). Normally the Updater must call it
146   /// in case of not-automatic update to true
147   MODEL_EXPORT virtual void mustBeUpdated(const bool theFlag);
148
149   /// Returns true if feature must be updated (re-executed) on rebuild
150   MODEL_EXPORT virtual bool mustBeUpdated();
151
152   /// Returns the identifier of feature-owner, unique in this document
153   MODEL_EXPORT virtual int featureId() const;
154
155   // returns all objects referenced to this
156   MODEL_EXPORT virtual const std::set<AttributePtr>& refsToMe() {return myRefsToMe;}
157
158 private:
159   // removes all information about back references
160   void eraseBackReferences();
161   // adds a back reference (with identifier which attribute references to this object
162   void addBackReference(FeaturePtr theFeature, std::string theAttrID);
163   // returns all references by attributes of this data
164   // \param the returned list of pairs: id of referenced attribute and list of referenced objects
165   void referencesToObjects(std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs);
166 };
167
168 #endif