Salome HOME
Issue #66 : update all features on undo/redo/abort
[modules/shaper.git] / src / Model / Model_Document.h
1 // File:        Model_Document.h
2 // Created:     28 Feb 2014
3 // Author:      Mikhail PONIKAROV
4
5 #ifndef Model_Document_HeaderFile
6 #define Model_Document_HeaderFile
7
8 #include <Model.h>
9 #include <ModelAPI_Document.h>
10
11 #include <TDocStd_Document.hxx>
12 #include <map>
13 #include <set>
14
15 class Handle_Model_Document;
16
17 /**\class Model_Document
18  * \ingroup DataModel
19  * \brief Document for internal data structure of any object storage.
20  * Document contains all data that must be stored/retrived in the file.
21  * Also it provides acces to this data: open/save, transactions management etc.
22  */
23 class Model_Document: public ModelAPI_Document
24 {
25 public:
26
27   //! Loads the OCAF document from the file.
28   //! \param theFileName full name of the file to load
29   //! \param theStudyID identifier of the SALOME study to associate with loaded file
30   //! \returns true if file was loaded successfully
31   MODEL_EXPORT virtual bool load(const char* theFileName);
32
33   //! Saves the OCAF document to the file.
34   //! \param theFileName full name of the file to store
35   //! \returns true if file was stored successfully
36   MODEL_EXPORT virtual bool save(const char* theFileName);
37
38   //! Removes document data
39   MODEL_EXPORT virtual void close();
40
41   //! Starts a new operation (opens a tansaction)
42   MODEL_EXPORT virtual void startOperation();
43   //! Finishes the previously started operation (closes the transaction)
44   //! Returns true if transaction in this document is not empty and really was performed
45   MODEL_EXPORT virtual void finishOperation();
46   //! Aborts the operation 
47   MODEL_EXPORT virtual void abortOperation();
48   //! Returns true if operation has been started, but not yet finished or aborted
49   MODEL_EXPORT virtual bool isOperation();
50   //! Returns true if document was modified (since creation/opening)
51   MODEL_EXPORT virtual bool isModified();
52
53   //! Returns True if there are available Undos
54   MODEL_EXPORT virtual bool canUndo();
55   //! Undoes last operation
56   MODEL_EXPORT virtual void undo();
57   //! Returns True if there are available Redos
58   MODEL_EXPORT virtual bool canRedo();
59   //! Redoes last operation
60   MODEL_EXPORT virtual void redo();
61
62   //! Adds to the document the new feature of the given feature id
63   //! \param creates feature and puts it in the document
64   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
65
66   //! Removes the feature from the document
67   MODEL_EXPORT virtual void removeFeature(boost::shared_ptr<ModelAPI_Feature> theFeature);
68
69   //! Returns the existing feature by the label
70   //! \param theLabel base label of the feature
71   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Feature> feature(TDF_Label& theLabel);
72
73   //! Adds a new sub-document by the identifier, or returns existing one if it is already exist
74   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID);
75
76   ///! Returns the id of hte document
77   MODEL_EXPORT virtual const std::string& id() const {return myID;}
78
79   //! Returns the feature in the group by the index (started from zero)
80   //! \param theGroupID group that contains a feature
81   //! \param theIndex zero-based index of feature in the group
82   //! \param isOperation if it is true, returns feature (not Object)
83   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Feature> 
84     feature(const std::string& theGroupID, const int theIndex, const bool isOperation = false);
85
86   //! Returns the number of features in the group
87   MODEL_EXPORT virtual int size(const std::string& theGroupID);
88
89 protected:
90
91   //! Returns (creates if needed) the group label
92   TDF_Label groupLabel(const std::string theGroup);
93
94   //! Initializes feature with a unique name in this group (unique name is generated as 
95   //! feature type + "_" + index
96   void setUniqueName(boost::shared_ptr<ModelAPI_Feature> theFeature);
97
98   //! Adds to the document the new feature
99   void addFeature(const boost::shared_ptr<ModelAPI_Feature> theFeature);
100
101   //! Returns the object by the feature
102   boost::shared_ptr<ModelAPI_Feature> objectByFeature(
103     const boost::shared_ptr<ModelAPI_Feature> theFeature);
104
105   //! Synchronizes myFeatures list with the updated document
106   void synchronizeFeatures(const bool theMarkUpdated = false);
107
108   //! Creates new document with binary file format
109   Model_Document(const std::string theID);
110
111   Handle_TDocStd_Document document() {return myDoc;}
112
113   friend class Model_Application;
114   friend class Model_PluginManager;
115
116 private:
117   std::string myID; ///< identifier of the document in the application
118   Handle_TDocStd_Document myDoc; ///< OCAF document
119   /// number of transactions after the last "save" call, used for "IsModified" method
120   int myTransactionsAfterSave;
121   /// number of nested transactions performed (or -1 if not nested)
122   int myNestedNum;
123   /// All features managed by this document (not only in history of OB)
124   std::vector<boost::shared_ptr<ModelAPI_Feature> > myFeatures;
125
126   ///< set of identifiers of sub-documents of this document
127   std::set<std::string> mySubs;
128   /// transaction indexes (related to myTransactionsAfterSave) which were empty in this doc
129   std::map<int, bool> myIsEmptyTr;
130 };
131
132 #endif