Salome HOME
89b9d548c1d5d1b1416b57dde0ad8ec9b92c1aa6
[modules/shaper.git] / src / Model / Model_Session.h
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef Model_Session_H_
22 #define Model_Session_H_
23
24 #include "Model.h"
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Feature.h>
27
28 #include <Events_Listener.h>
29 #include <map>
30
31 class Model_Document;
32
33 /**\class Model_Session
34  * \ingroup DataModel
35  * \brief Object that knows (from the initial XML file) which
36  * plugin contains which feature, loads and stores reference to loaded plugins by
37  * the feature functionality request.
38  */
39 class Model_Session : public ModelAPI_Session, public Events_Listener
40 {
41   bool myPluginsInfoLoaded;  ///< it true if plugins information is loaded
42   /// map of feature IDs to plugin name and document kind
43   std::map<std::string, std::pair<std::string, std::string> > myPlugins;
44   std::map<std::string, ModelAPI_Plugin*> myPluginObjs;  ///< instances of the already plugins
45   std::string myCurrentPluginName;  ///< name of the plugin that must be loaded currently
46   std::shared_ptr<ModelAPI_Document> myCurrentDoc;  ///< current working document
47
48   /// if true, generates error if document is updated outside of transaction
49   bool myCheckTransactions;
50   bool myOperationAttachedToNext; ///< the current operation must be committed twice, with nested
51  public:
52
53   //! Loads the OCAF document from the file.
54   //! \param theFileName full name of the file to load
55   //! \returns true if file was loaded successfully
56   MODEL_EXPORT virtual bool load(const char* theFileName);
57
58   //! Saves the OCAF document to the file.
59   //! \param theFileName full name of the file to store
60   //! \param theResults the result full file names that were stored by "save"
61   //! \returns true if file was stored successfully
62   MODEL_EXPORT virtual bool save(const char* theFileName, std::list<std::string>& theResults);
63
64   //! Closes all documents
65   MODEL_EXPORT virtual void closeAll();
66
67   //! Starts a new operation (opens a transaction)
68   //! \param theId string-identifier of the started transaction
69   //! \param theAttachedToNested if it is true,
70   //!                            it means that this transaction is attached to the nested
71   //!          where it is located and will be committed on the next commit with the nested
72   MODEL_EXPORT virtual void startOperation(
73     const std::string& theId = "", const bool theAttachedToNested = false);
74   //! Finishes the previously started operation (closes the transaction)
75   MODEL_EXPORT virtual void finishOperation();
76   //! Aborts the operation
77   MODEL_EXPORT virtual void abortOperation();
78   //! Returns true if operation has been started, but not yet finished or aborted
79   MODEL_EXPORT virtual bool isOperation();
80   //! Returns true if document was modified (since creation/opening)
81   MODEL_EXPORT virtual bool isModified();
82
83   //! Returns True if there are available Undos
84   MODEL_EXPORT virtual bool canUndo();
85   //! Undoes last operation
86   MODEL_EXPORT virtual void undo();
87   //! Returns True if there are available Redos
88   MODEL_EXPORT virtual bool canRedo();
89   //! Redoes last operation
90   MODEL_EXPORT virtual void redo();
91   //! Returns stack of performed operations
92   MODEL_EXPORT virtual std::list<std::string> undoList();
93   //! Returns stack of rolled back operations
94   MODEL_EXPORT virtual std::list<std::string> redoList();
95
96   /// Returns the root document of the application (that may contains sub-documents)
97   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Document> moduleDocument();
98
99   /// Returns the document by ID, loads if not loaded yet. Returns null if no such document.
100   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Document> document(int theDocID);
101
102   /// Return true if root document has been already created
103   MODEL_EXPORT virtual bool hasModuleDocument();
104
105   /// Returns the current document that used for current work in the application
106   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Document> activeDocument();
107
108   /// Defines the current document that used for current work in the application
109   MODEL_EXPORT virtual void setActiveDocument(
110     std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal = true);
111
112   /// Returns all the opened documents of the session (without postponed)
113   MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Document> > allOpenedDocuments();
114
115   /// Returns true if document is not loaded yet
116   MODEL_EXPORT virtual bool isLoadByDemand(const std::string theDocID, const int theDocIndex);
117
118   /// Registers the plugin that creates features.
119   /// It is obligatory for each plugin to call this function on loading to be found by
120   /// the plugin manager on call of the feature)
121   MODEL_EXPORT virtual void registerPlugin(ModelAPI_Plugin* thePlugin);
122
123   /// Processes the configuration file reading
124   MODEL_EXPORT virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
125
126   /// Copies the document to the new one
127   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Document> copy(
128       std::shared_ptr<ModelAPI_Document> theSource, const int theDestID);
129
130   /// Returns the validators factory: the only one instance per application
131   MODEL_EXPORT virtual ModelAPI_ValidatorsFactory* validators();
132
133   /// Sets the flag to check modifications outside the transaction or not
134   void setCheckTransactions(const bool theCheck)
135   {
136     myCheckTransactions = theCheck;
137   }
138
139   /// Is called only once, on startup of the application
140   Model_Session();
141
142   /// Returns the global identifier of the current transaction (needed for the update algo)
143   MODEL_EXPORT virtual int transactionID();
144
145  protected:
146   /// Loads (if not done yet) the information about the features and plugins
147   void LoadPluginsInfo();
148
149   /// Creates the feature object using plugins functionality
150   FeaturePtr createFeature(std::string theFeatureID, Model_Document* theDocOwner);
151
152   friend class Model_Document;
153   friend class Model_Objects;
154 };
155
156 #endif