Salome HOME
Issue #648: Preserve selection state
[modules/shaper.git] / src / Model / Model_Document.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Document.h
4 // Created:     28 Feb 2014
5 // Author:      Mikhail PONIKAROV
6
7 #ifndef Model_Document_H_
8 #define Model_Document_H_
9
10 #include <Model.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Result.h>
14 #include <ModelAPI_ResultParameter.h>
15
16 #include <TDocStd_Document.hxx>
17 #include <map>
18 #include <set>
19
20 class Handle_Model_Document;
21 class Model_Objects;
22
23 /**\class Model_Document
24  * \ingroup DataModel
25  * \brief Document for internal data structure of any object storage.
26  * Document contains all data that must be stored/retrived in the file.
27  * Also it provides acces to this data: open/save, transactions management etc.
28  */
29 class Model_Document : public ModelAPI_Document
30 {
31  public:
32   //! Returns the kind of the document: "PartSet", "Part", or something else.
33   //! This kind is used for feature buttons enable/disable depending on active document
34   //! (it uses workbench "document" identifier in XML configuration file for this)
35   MODEL_EXPORT virtual const std::string& kind() const {return myKind;}
36
37   //! Loads the OCAF document from the file.
38   //! \param theFileName full name of the file to load
39   //! \param theThis the common shared pointer to the document to manage with it later
40   //! \returns true if file was loaded successfully
41   MODEL_EXPORT virtual bool load(const char* theFileName, DocumentPtr theThis);
42
43   //! Saves the OCAF document to the file.
44   //! \param theFileName full name of the file to store
45   //! \param theResults the result full file names that were stored by "save"
46   //! \returns true if file was stored successfully
47   MODEL_EXPORT virtual bool save(const char* theFileName, std::list<std::string>& theResults);
48
49   //! Removes document data
50   //! \param theForever if it is false, document is just hiden (to keep possibility make it back on Undo/Redo)
51   MODEL_EXPORT virtual void close(const bool theForever = false);
52
53   //! Starts a new operation (opens a tansaction)
54   MODEL_EXPORT virtual void startOperation();
55   //! Finishes the previously started operation (closes the transaction)
56   //! \returns true if transaction in this document is not empty and really was performed
57   MODEL_EXPORT virtual bool finishOperation();
58   //! Aborts the operation 
59   MODEL_EXPORT virtual void abortOperation();
60   //! Returns true if operation has been started, but not yet finished or aborted
61   MODEL_EXPORT virtual bool isOperation() const;
62   //! Returns true if document was modified (since creation/opening)
63   MODEL_EXPORT virtual bool isModified();
64
65   //! Returns True if there are available Undos
66   MODEL_EXPORT virtual bool canUndo();
67   //! Undoes last operation
68   MODEL_EXPORT virtual void undo();
69   //! Returns True if there are available Redos
70   MODEL_EXPORT virtual bool canRedo();
71   //! Redoes last operation
72   MODEL_EXPORT virtual void redo();
73
74   //! Adds to the document the new feature of the given feature id
75   //! \param theID creates feature and puts it in the document
76   //! \param theMakeCurrent to make current this new feature in this document
77   MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID, const bool theMakeCurrent = true);
78
79   //! Return a list of features, which refers to the feature
80   //! \param theFeature a feature
81   //! \param theRefs a list of reference features
82   //! \param isSendError a flag whether the error message should be send
83   MODEL_EXPORT virtual void refsToFeature(FeaturePtr theFeature,
84                                           std::set<FeaturePtr>& theRefs,
85                                           const bool isSendError = true);
86
87   //! Removes the feature from the document (with result)
88   //! \param theFeature a removed feature
89   MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
90
91   //! Returns the first found object in the group by the object name
92   //! \param theGroupID group that contains an object
93   //! \param theName name of the object to search
94   //! \returns null if such object is not found
95   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> objectByName(
96     const std::string& theGroupID, const std::string& theName);
97
98   //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
99   //! \param theObject object of this document
100   //! \returns index started from zero, or -1 if object is invisible or belongs to another document
101   MODEL_EXPORT virtual const int index(std::shared_ptr<ModelAPI_Object> theObject);
102
103   //! Adds a new sub-document by the identifier, or returns existing one if it is already exist
104   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID);
105
106   //! Internal sub-document by ID
107   MODEL_EXPORT virtual std::shared_ptr<Model_Document> subDoc(std::string theDocID);
108
109   ///! Returns the id of hte document
110   MODEL_EXPORT virtual const std::string& id() const
111   {
112     return myID;
113   }
114
115   //! Returns the feature in the group by the index (started from zero)
116   //! \param theGroupID group that contains a feature
117   //! \param theIndex zero-based index of feature in the group
118   MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID, const int theIndex);
119
120   //! Returns the number of features in the group
121   MODEL_EXPORT virtual int size(const std::string& theGroupID);
122
123   //! Returns the feature that is currently edited in this document, normally
124   //! this is the latest created feature
125   //! \param theVisible use visible features only: flag is true for Object Browser functionality
126   //! \returns null if next created feature must be the first
127   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> currentFeature(const bool theVisible);
128
129   //! Sets the current feature: all features below will be disabled, new features
130   //! will be appended after this one.
131   //! \param theCurrent the selected feature as current: blow it everythin become disabled
132   //! \param theVisible use visible features only: flag is true for Object Browser functionality
133   MODEL_EXPORT virtual void setCurrentFeature(std::shared_ptr<ModelAPI_Feature> theCurrent,
134     const bool theVisible);
135
136   //! Makes the current feature one feature upper
137   MODEL_EXPORT virtual void setCurrentFeatureUp();
138
139   /// Creates a construction cresults
140   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
141       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
142   /// Creates a body results
143   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultBody> createBody(
144       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
145   /// Creates a part results
146   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> createPart(
147       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
148   /// Creates a group results
149   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultGroup> createGroup(
150       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
151
152   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultParameter> createParameter(
153       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
154
155   //! Returns a feature by result (owner of result)
156   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
157     feature(const std::shared_ptr<ModelAPI_Result>& theResult);
158
159   ///! Returns true if parametric updater need to execute feature on recomputartion
160   ///! On abort, undo or redo it is not necessary: results in document are updated automatically
161   bool& executeFeatures() {return myExecuteFeatures;}
162
163   //! Registers the name of the shape for the topological naming needs
164   void addNamingName(const TDF_Label theLabel, std::string theName);
165   //! Returns the label, keeper of the name  for the topological naming needs
166   TDF_Label findNamingName(std::string theName);
167   //! Returns the result by name of the result (names of results must be unique, used for naming
168   //! selection by name.
169   ResultPtr findByName(const std::string theName);
170
171   ///! Returns all features of the document including the hidden features which are not in
172   ///! history. Not very fast method, for calling once, not in big cycles.
173   MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
174
175  protected:
176   //! Returns (creates if needed) the general label
177   TDF_Label generalLabel() const;
178
179   //! Creates new document with binary file format
180   Model_Document(const std::string theID, const std::string theKind);
181
182   //! Returns the internal OCCT document of this interface
183   Handle_TDocStd_Document document()
184   {
185     return myDoc;
186   }
187
188   //! performs compactification of all nested operations into one
189   //! \returns true if resulting transaction is not empty and can be undoed
190   void compactNested();
191
192   //! Returns all sub documents
193   const std::set<std::string> subDocuments(const bool theActivatedOnly) const;
194
195   //! The implementation of undo: with or without recoursive calls in the sub-documents
196   void undoInternal(const bool theWithSubs, const bool theSynchronize);
197
198   //! Stores the Id of the current operation (normally is called for the root document)
199   void operationId(const std::string& theId);
200
201   //! Returns the list of Ids of the operations that can be undoed (called for the root document)
202   std::list<std::string> undoList() const;
203
204   //! Returns the list of Ids of the operations that can be redoed (called for the root document)
205   std::list<std::string> redoList() const;
206
207   //! Internally makes document know that feature was removed or added in history after creation
208   virtual void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
209   //! Internally makes document know that feature was removed or added in history after creation
210   virtual void updateHistory(const std::string theGroup);
211
212   //! Returns true if the document is root module document
213   bool isRoot() const;
214
215   //! Sets shared pointer to this
216   void setThis(DocumentPtr theDoc);
217
218   //! Returns the objects manager
219   Model_Objects* objects() {return myObjs;}
220
221   ///! Informs the document that it becomes active and some actions must be performed
222   virtual void setActive(const bool theFlag);
223   //! Returns true if this document is currently active
224   virtual bool isActive() const;
225
226   friend class Model_Application;
227   friend class Model_Session;
228   friend class Model_Update;
229   friend class Model_AttributeReference;
230   friend class Model_AttributeRefAttr;
231   friend class Model_AttributeRefList;
232   friend class DFBrowser;
233
234  private:
235   std::string myID;  ///< identifier of the document in the application
236   std::string myKind;  ///< kind of the document in the application
237   Handle_TDocStd_Document myDoc;  ///< OCAF document
238
239   Model_Objects *myObjs; ///< data manager of this document
240
241   //! counter value of transaction on the last "save" call, used for "IsModified" method
242   int myTransactionSave;
243   //! number of nested transactions performed (list becasue may be nested inside of nested)
244   //! the list is empty if not nested transaction is performed
245   std::list<int> myNestedNum;
246
247   //! Information related to the every user-transaction
248   struct Transaction {
249     int myOCAFNum; ///< number of OCAF transactions related to each "this" transaction, may be 0
250     std::string myId; ///< user-identifier string of transaction
251     //! default constructor with default Id
252     Transaction(): myOCAFNum(0), myId("") {}
253   };
254
255   //! transaction indexes (related to myTransactionsAfterSave) and info about the real transactions
256   //! in myDocument connected to this operation (may be zero for empty transaction)
257   std::list<Transaction> myTransactions;
258   //! list of info about transactions undone (first is oldest undone)
259   std::list<Transaction> myRedos;
260
261   //! Optimization for finding the shape-label by topological naming names
262   std::map<std::string, TDF_Label> myNamingNames;
263   //! If it is true, features are not executed on update (on abort, undo, redo)
264   bool myExecuteFeatures;
265
266   bool myIsActive; ///< flag that stores the active/not active state
267 };
268
269 #endif