Salome HOME
Implementation of features cashing and undo/redo functionality in document.
[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 #include <Event_Message.h>
11
12 #include <TDocStd_Document.hxx>
13 #include <map>
14 #include <set>
15
16 class Handle_Model_Document;
17
18 /**\class Model_Document
19  * \ingroup DataModel
20  * \brief Document for internal data structure of any object storage. Corresponds to the SALOME study.
21  * Document contains all data of te SALOME Study specific to this module
22  * that must be written into the HDF file.
23  * Also it provides acces to this data: open/save, transactions management etc.
24  * to provide access to all stored data.
25  */
26
27 class Model_Document: public ModelAPI_Document
28 {
29 public:
30
31   //! Loads the OCAF document from the file.
32   //! \param theFileName full name of the file to load
33   //! \param theStudyID identifier of the SALOME study to associate with loaded file
34   //! \returns true if file was loaded successfully
35   MODEL_EXPORT virtual bool load(const char* theFileName);
36
37   //! Saves the OCAF document to the file.
38   //! \param theFileName full name of the file to store
39   //! \returns true if file was stored successfully
40   MODEL_EXPORT virtual bool save(const char* theFileName);
41
42   //! Removes document data
43   MODEL_EXPORT virtual void close();
44
45   //! Starts a new operation (opens a tansaction)
46   MODEL_EXPORT virtual void startOperation();
47   //! Finishes the previously started operation (closes the transaction)
48   //! Returns true if transaction in this document is not empty and really was performed
49   MODEL_EXPORT virtual void finishOperation();
50   //! Aborts the operation 
51   MODEL_EXPORT virtual void abortOperation();
52   //! Returns true if operation has been started, but not yet finished or aborted
53   MODEL_EXPORT virtual bool isOperation();
54   //! Returns true if document was modified (since creation/opening)
55   MODEL_EXPORT virtual bool isModified();
56
57   //! Returns True if there are available Undos
58   MODEL_EXPORT virtual bool canUndo();
59   //! Undoes last operation
60   MODEL_EXPORT virtual void undo();
61   //! Returns True if there are available Redos
62   MODEL_EXPORT virtual bool canRedo();
63   //! Redoes last operation
64   MODEL_EXPORT virtual void redo();
65
66   //! Adds to the document the new feature of the given feature id
67   //! \param creates feature and puts it in the document
68   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> addFeature(std::string theID);
69
70   //! Returns the existing feature by the label
71   //! \param theLabel base label of the feature
72   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> feature(TDF_Label& theLabel);
73
74   //! Adds a new sub-document by the identifier, or returns existing one if it is already exist
75   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID);
76
77   //! Creates an iterator of the features by the specific groups
78   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Iterator> featuresIterator(
79     const std::string theGroup);
80
81   MODEL_EXPORT virtual const std::string& id() const {return myID;}
82
83   //! Returns the feature in the group by the index (started from zero)
84   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> 
85     feature(const std::string& theGroupID, const int theIndex);
86
87   ///! Returns the vector of groups already added to the document
88   MODEL_EXPORT virtual const std::vector<std::string>& getGroups() const;
89
90   //! Returns the index of feature in the group (zero based)
91   //! \retruns -1 if not found
92   MODEL_EXPORT virtual int featureIndex(std::shared_ptr<ModelAPI_Feature> theFeature);
93
94 protected:
95
96   //! Returns (creates if needed) the group label
97   TDF_Label groupLabel(const std::string theGroup);
98
99   //! Initializes feature with a unique name in this group (unique name is generated as 
100   //! feature type + "_" + index
101   void setUniqueName(std::shared_ptr<ModelAPI_Feature> theFeature);
102
103   //! Adds to the document the new feature
104   void addFeature(const std::shared_ptr<ModelAPI_Feature> theFeature);
105
106   //! Synchronizes myGroups, myGroupsNames, myFeatures and mySubs list with the updated document
107   void synchronizeFeatures();
108
109   //! Creates new document with binary file format
110   Model_Document(const std::string theID);
111
112   friend class Model_Application;
113
114 private:
115   std::string myID; ///< identifier of the document in the application
116   Handle_TDocStd_Document myDoc; ///< OCAF document
117   /// number of transactions after the last "save" call, used for "IsModified" method
118   int myTransactionsAfterSave;
119   /// root labels of the features groups identified by names
120   std::map<std::string, TDF_Label> myGroups;
121   std::vector<std::string> myGroupsNames; ///< names of added groups to the document
122   /// Features managed by this document: by group name
123   std::map<std::string, std::vector<std::shared_ptr<ModelAPI_Feature> > > myFeatures;
124   std::set<std::string> mySubs; ///< set of identifiers of sub-documents of this document
125   /// transaction indexes (related to myTransactionsAfterSave) which were empty in this doc
126   std::map<int, bool> myIsEmptyTr;
127 };
128
129 /// Event ID that model is updated
130 static const char * EVENT_FEATURE_UPDATED = "FeatureUpdated";
131
132 /// Message that feature was changed (used for Object Browser update)
133 class ModelAPI_FeatureUpdatedMessage : public Event_Message {
134   std::shared_ptr<ModelAPI_Feature> myFeature; ///< which feature is changed
135 public:
136   /// sender is not important, all information is located in the feature
137   ModelAPI_FeatureUpdatedMessage(std::shared_ptr<ModelAPI_Feature> theFeature);
138
139   /// Returns the ID of this message
140   static const Event_ID messageId();
141
142   /// Returns the feature that has been updated
143   std::shared_ptr<ModelAPI_Feature> feature();
144 };
145
146 #endif