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