Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / Model / Model_Objects.h
1 // Copyright (C) 2014-2019  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_Objects_H_
21 #define Model_Objects_H_
22
23 #include <Model.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_Folder.h>
27 #include <ModelAPI_Result.h>
28 #include <ModelAPI_ResultParameter.h>
29
30 #include <TDocStd_Document.hxx>
31 #include <NCollection_DataMap.hxx>
32 #include <TDF_Label.hxx>
33 #include <TDF_LabelList.hxx>
34 #include <map>
35 #include <set>
36 #include <vector>
37
38 // for TDF_Label map usage
39 static Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper);
40 static Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2);
41
42 extern int kUNDEFINED_FEATURE_INDEX;
43
44 /**\class Model_Objects
45  * \ingroup DataModel
46  * \brief Manager of objects of the document. Normally one this class corresponds to
47  * one document and just helper to manage objects (ModelAPI_Objects) inside of the document
48  * on the level of data storage.
49  */
50 class Model_Objects
51 {
52  public:
53   //! Registers the feature in the data structure
54   //! \param theFeature feature that must be added to the data structure
55   //! \param theAfterThis the feature will be added after this feature;
56   //!        if it is null, the added feature will be the first
57   void addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis);
58
59   //! Return a list of features, which refers to the feature
60   //! \param theFeature a feature
61   //! \param theRefs a list of reference features
62   //! \param isSendError a flag whether the error message should be send
63   void refsToFeature(FeaturePtr theFeature,
64                      std::set<FeaturePtr>& theRefs,
65                      const bool isSendError = true);
66
67   //! Removes the feature from the document (with result)
68   //! \param theFeature a removed feature
69   void removeFeature(FeaturePtr theFeature);
70
71   //! Moves the feature to make it after the given one in the history.
72   void moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis);
73
74   //! Returns the existing feature by the label
75   //! \param theLabel base label of the feature
76   FeaturePtr feature(TDF_Label theLabel) const;
77
78   //! Returns the existing object: result or feature
79   //! \param theLabel base label of the object
80   ObjectPtr object(TDF_Label theLabel);
81
82   //! Returns the first found object in the group by the object name
83   //! \param theGroupID group that contains an object
84   //! \param theName name of the object to search
85   //! \returns null if such object is not found
86   std::shared_ptr<ModelAPI_Object> objectByName(
87     const std::string& theGroupID, const std::string& theName);
88
89   //! Returns the result by the result name
90   ResultPtr findByName(const std::string theName);
91
92
93   //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
94   //! \param theObject object of this document
95   //! \param theAllowFolder take into account grouping feature by folders
96   //! \returns index started from zero, or -1 if object is invisible or belongs to another document
97   const int index(std::shared_ptr<ModelAPI_Object> theObject,
98                   const bool theAllowFolder = false);
99
100   //! Returns the feature in the group by the index (started from zero)
101   //! \param theGroupID group that contains a feature
102   //! \param theIndex zero-based index of feature in the group
103   //! \param theAllowFolder take into account grouping feature by folders
104   ObjectPtr object(const std::string& theGroupID,
105                    const int theIndex,
106                    const bool theAllowFolder = false);
107
108   //! Returns the number of features in the group
109   //! \param theGroupID group of objects
110   //! \param theAllowFolder take into account grouping feature by folders
111   int size(const std::string& theGroupID, const bool theAllowFolder = false);
112
113   //! Returns the parent object of this child. This may be result or feature, parent of a
114   //! top result. Fast method, that uses internal data structure specifics.
115   std::shared_ptr<ModelAPI_Object> parent(const std::shared_ptr<ModelAPI_Object> theChild);
116
117   //! Returns all (and disabled) results of the given type.
118   //! Not fast method (iterates all features).
119   void allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults);
120
121   //! Returns the number of all features: in the history or not
122   int numInternalFeatures();
123   //! Returns the feature by zero-based index: features in the history or not
124   std::shared_ptr<ModelAPI_Feature> internalFeature(const int theIndex);
125
126   /// Creates a construction result
127   std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
128       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
129   /// Creates a body result
130   std::shared_ptr<ModelAPI_ResultBody> createBody(
131       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
132   /// Creates a part result
133   std::shared_ptr<ModelAPI_ResultPart> createPart(
134       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
135   /// Copies a part result, keeping the reference to origin
136   std::shared_ptr<ModelAPI_ResultPart> copyPart(
137       const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
138       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
139   /// Creates a group result
140   std::shared_ptr<ModelAPI_ResultGroup> createGroup(
141       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
142   /// Creates a field result
143   std::shared_ptr<ModelAPI_ResultField> createField(
144       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
145   /// Creates a parameter result
146   std::shared_ptr<ModelAPI_ResultParameter> createParameter(
147       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
148
149   //! Returns a feature by result (owner of result)
150   std::shared_ptr<ModelAPI_Feature>
151     feature(const std::shared_ptr<ModelAPI_Result>& theResult);
152
153   /// Creates a folder (group of the features in the object browser)
154   std::shared_ptr<ModelAPI_Folder> createFolder(
155       const std::shared_ptr<ModelAPI_Feature>& theBeforeThis);
156   //! Removes the folder from the document (all features in the folder will be kept).
157   void removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder);
158   //! Search a folder applicable for the list of features
159   //! (it means the list of features stored in the folder should be consequential)
160   //! \param theFeatures list of features to be added to folder
161   //! \param theBelow    search the folder below the features (if \c false, search above)
162   //! \return Empty pointer if there is no applicable folder
163   std::shared_ptr<ModelAPI_Folder> findFolder(
164       const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
165       const bool theBelow);
166   //! Search a folder containing the given feature.
167   //! Additionally calculates a zero-based index of the feature in this folder.
168   //! \param theFeature feature to search
169   //! \param theIndexInFolder zero-based index in the folder or -1 if the feature is top-level.
170   //! \return the folder containing the feature or empty pointer if the feature is top-level.
171   std::shared_ptr<ModelAPI_Folder> findContainingFolder(
172       const std::shared_ptr<ModelAPI_Feature>& theFeature,
173       int& theIndexInFolder);
174   //! Add a list of features to the folder. The correctness of the adding is not performed
175   //! (such checks have been done in corresponding find.. method).
176   //! \return \c true if the movement is successful
177   bool moveToFolder(const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
178                     const std::shared_ptr<ModelAPI_Folder>& theFolder);
179   //! Remove features from the folder
180   //! \param theFeatures list of features to be removed
181   //! \param theBefore   extract features before the folder (this parameter is applicable only
182   //!                    when all features in the folder are taking out,
183   //!                    in other cases the direction is taken automatically)
184   //! \return \c true if the features have been moved out
185   bool removeFromFolder(const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
186                         const bool theBefore = true);
187
188   //! Sets the owner of this manager
189   void setOwner(DocumentPtr theDoc);
190
191   //! Returns the owner of this manager
192   DocumentPtr owner() {return myDoc;}
193
194   //! Deletes all managed features with emitting of corresponded signal
195   ~Model_Objects();
196
197  protected:
198
199   //! Returns (creates if needed) the features label
200   TDF_Label featuresLabel() const;
201
202   //! Initializes feature with a unique name in this group (unique name is generated as
203   //! feature type + "_" + index
204   void setUniqueName(FeaturePtr theFeature);
205
206   //! Initializes the folder with an unique name ("Folder_" + index)
207   void setUniqueName(FolderPtr theFolder);
208
209   //! Synchronizes myFeatures list with the updated document
210   //! \param theUpdated list of labels that are marked as modified, so features must be also
211   //! \param theUpdateReferences causes the update of back-references
212   //! \param theExecuteFeatures requires re-execute modified persistent features
213   //!            (not needed on undo/redo/abort/open)
214   //! \param theOpen - on open nothing must be re-executed, except not persistent results
215   //! \param theFlush makes flush all events in the end of all modifications of this method
216   void synchronizeFeatures(const TDF_LabelList& theUpdated, const bool theUpdateReferences,
217     const bool theOpen, const bool theExecuteFeatures, const bool theFlush);
218   //! Synchronizes the BackReferences list in Data of Features and Results
219   void synchronizeBackRefs();
220
221   //! Creates manager on the OCAF document main label
222   Model_Objects(TDF_Label theMainLab);
223
224   //! Initializes the data fields of the feature
225   void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
226
227   //! Allows to store the result in the data tree of the document
228   //! (attaches 'data' of result to tree)
229   void storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
230                    std::shared_ptr<ModelAPI_Result> theResult,
231                    const int theResultIndex = 0);
232
233   //! returns the label of result by index; creates this label if it was not created before
234   TDF_Label resultLabel(const std::shared_ptr<ModelAPI_Data>& theFeatureData,
235                         const int theResultIndex);
236
237   //! Updates the results list of the feature basing on the current data tree
238   //! theProcessed is used to avoid update twice (since the function is recursive)
239   void updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed);
240
241   /// Internally makes document know that feature was removed or added in history after creation
242   void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
243
244   /// Internally makes document know that feature was removed or added in history after creation
245   void updateHistory(const std::string theGroup);
246
247   /// Clears the history arrays related to this object
248   void clearHistory(ObjectPtr theObj);
249
250   /// Creates the history: up to date with the current state
251   void createHistory(const std::string& theGroupID);
252
253   /// Returns the next (from the history point of view) feature, any: invisible or disabled
254   /// \param theCurrent previous to the resulting feature
255   /// \param theReverse if it is true, iterates in reversed order (next becomes previous)
256   /// \param theIndex may be used for optimization: index of theCurrent in references array
257   FeaturePtr nextFeature(FeaturePtr theCurrent, int& theIndex, const bool theReverse = false);
258   /// Returns to the first (from the history point of view) feature, any: invisible or disabled
259   FeaturePtr firstFeature();
260   /// Returns to the last (from the history point of view) feature, any: invisible or disabled
261   FeaturePtr lastFeature();
262   /// Returns true if theLater is in history of features creation later than theCurrent
263   bool isLater(FeaturePtr theLater, FeaturePtr theCurrent) const;
264
265   /// Returns the next or previous label
266   /// \param theCurrent given label
267   /// \param theReverse if it is true, iterates in reversed order (next becomes previous)
268   TDF_Label nextLabel(TDF_Label theCurrent, int& theIndex, const bool theReverse = false);
269
270   /// Returns the result group identifier of the given feature (for this at least one result must
271   /// be created before)
272   std::string featureResultGroup(FeaturePtr theFeature);
273
274   //! Returns all features of the document including the hidden features which are not in
275   //! history. Not very fast method, for calling once, not in big cycles.
276   std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
277
278   //! Returns all objects of the document including the hidden features which are not in
279   //! history. Not very fast method, for calling once, not in big cycles.
280   std::list<std::shared_ptr<ModelAPI_Object> > allObjects();
281
282   //! synchronizes back references for the given object basing on the collected data
283   void synchronizeBackRefsForObject(
284     const std::set<std::shared_ptr<ModelAPI_Attribute>>& theNewRefs, ObjectPtr theObject);
285
286   /// Just removes all features without touching the document data (to be able undo)
287   virtual void eraseAllFeatures();
288
289   // Check whether the predefined-image of the result had user-defined name.
290   // If yes, return this name.
291   bool hasCustomName(DataPtr theFeatureData,
292                      ResultPtr theResult,
293                      int theResultIndex,
294                      std::string& theParentName) const;
295
296   /// Return object representing a folder or empty pointer
297   const ObjectPtr& folder(TDF_Label theLabel) const;
298
299  private:
300   TDF_Label myMain; ///< main label of the data storage
301
302   DocumentPtr myDoc; ///< document, owner of this objects manager: needed for events creation
303
304   /// All managed features (not only in history of OB)
305   /// For optimization mapped by labels
306   NCollection_DataMap<TDF_Label, FeaturePtr> myFeatures;
307
308   /// Managed folders
309   NCollection_DataMap<TDF_Label, ObjectPtr> myFolders;
310
311   /// Map from group id to the array that contains all objects located in history.
312   /// Each array is updated by demand from scratch, by browsing all the features in the history.
313   std::map<std::string, std::vector<ObjectPtr> > myHistory;
314
315   friend class Model_Document;
316   friend class Model_Session;
317   friend class Model_Update;
318   friend class Model_AttributeReference;
319   friend class Model_AttributeRefAttr;
320   friend class Model_AttributeRefList;
321   friend class Model_AttributeSelection;
322   friend class Model_SelectionNaming;
323 };
324
325 #endif