Salome HOME
Preparing migration to 4.3
[modules/paravis.git] / src / PVGUI / PV_Events.h
1 // Copyright (C) 2010-2015  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 //  File   : PV_Events.h
20 //  Author : Vitaly Smetannikov 
21 //  Module : PARAVIS
22 //
23
24 #ifndef PV_EVENTS_H
25 #define PV_EVENTS_H
26
27 #include "SALOME_Event.h"
28 #include "SalomeApp_Study.h"
29 #include "SalomeApp_Application.h"
30
31 #include "SUIT_Session.h"
32
33
34 /*!
35  * Definition of events
36  */
37 namespace PARAVIS {
38   
39   /*!
40    * Event which returns SalomeApp_Application instance which corresponds to th given study ID
41    */
42   struct TGetGUIApplication: public SALOME_Event
43   {
44     int myStudyId;
45     typedef SalomeApp_Application* TResult;
46     TResult myResult;
47     
48     TGetGUIApplication(const int theStudyId):myStudyId(theStudyId), myResult(0) {}
49     
50     virtual void Execute()
51     {
52       MESSAGE("Find application for study with id = : " << myStudyId);
53       SUIT_Session* aSession = SUIT_Session::session();
54       QList<SUIT_Application*> anApplications = aSession->applications();
55       for (int i = 0; i < anApplications.count() && !myResult; i++ ){
56         if ( anApplications[i]->activeStudy() && anApplications[i]->activeStudy()->id() == myStudyId )
57           myResult = dynamic_cast<SalomeApp_Application*>( anApplications[i] );
58       }
59       if ( !myResult ) {
60         MESSAGE("Error: application is not found for study with id = : " << myStudyId);
61       }
62     }
63   };
64   
65   /*!
66    * Parent event for all events what is going to work with PARAVIS module instance
67    */
68   struct TModuleEvent: public SALOME_Event
69   {
70     SalomeApp_Application* myApp;
71
72     TModuleEvent(SalomeApp_Application* theApp ):myApp(theApp) {}
73
74       //! Returns pointer on PARAVIS module instance
75     PVGUI_Module* getModule() 
76     {
77       PVGUI_Module* aPVModule = 0;
78       CAM_Application::ModuleList aList = myApp->modules();
79       foreach(CAM_Module* aModule, aList) {
80         if (aModule->moduleName() == "ParaViS") {
81           aPVModule = dynamic_cast<PVGUI_Module*>(aModule);
82           break;
83         }
84       }
85       return aPVModule;
86     }
87   };
88
89   /*!
90    * Event which activates PARAVIS module if it is not active
91    */
92   struct TActivateModule: public TModuleEvent
93   {
94     TActivateModule(SalomeApp_Application* theApp ):TModuleEvent(theApp) {}
95     
96     virtual void Execute()
97     {
98       PVGUI_Module* aPVModule = getModule();
99       if ((aPVModule == 0) || (myApp->activeModule() != aPVModule)) {
100         myApp->activateModule("ParaViS");
101       }
102     }
103   };
104
105   /*!
106    * Returns trace string collected in PARAVIS module in current session
107    */
108   struct TGetTrace: public TModuleEvent
109   {
110     typedef std::string TResult;
111     TResult myResult;
112     TGetTrace(SalomeApp_Application* theApp) :
113       TModuleEvent(theApp)
114     {
115       myResult = "";
116     }
117
118     virtual void Execute()
119     {
120       PVGUI_Module* aPVModule = getModule();
121       if (aPVModule)
122         myResult = qPrintable(aPVModule->getTraceString());
123     }
124   };
125
126   /*!
127    * Parent event for all operations with files across PARAVIS module
128    */
129   struct TParavisFileEvent: public TModuleEvent
130   {
131     const char* myName;
132
133     TParavisFileEvent(SalomeApp_Application* theApp, const char* theFileName ) :
134       TModuleEvent(theApp), myName(theFileName)
135     {}
136   };
137
138   /*!
139    * Event to save trace string inte disk file
140    */
141   struct TSaveTrace: public TParavisFileEvent
142   {
143     TSaveTrace(SalomeApp_Application* theApp, const char* theFileName ) :
144       TParavisFileEvent(theApp, theFileName)
145     {}
146     
147     virtual void Execute()
148     {
149       PVGUI_Module* aPVModule = getModule();
150       if (aPVModule)
151         aPVModule->saveTrace(myName);
152     }
153   };
154
155
156   /*!
157    * Event to import file to PARAVIS
158    */
159   struct TImportFile: public TParavisFileEvent
160   {
161     TImportFile(SalomeApp_Application* theApp, const char* theFileName ) :
162       TParavisFileEvent(theApp, theFileName)
163     {}
164     
165     virtual void Execute()
166     {
167       PVGUI_Module* aPVModule = getModule();
168       if (aPVModule)
169         aPVModule->openFile(myName);
170     }
171   };
172
173   /*!
174    * Event to execute a script to PARAVIS
175    */
176   struct TExecuteScript: public TParavisFileEvent
177   {
178     TExecuteScript(SalomeApp_Application* theApp, const char* theFileName ) :
179       TParavisFileEvent(theApp, theFileName)
180     {}
181     
182     virtual void Execute()
183     {
184       PVGUI_Module* aPVModule = getModule();
185       if (aPVModule)
186         aPVModule->executeScript(myName);
187     }
188   };
189
190   /*!
191    * Event to save current Paraview state.
192    * This event is used for Study saving
193    */
194   struct TSaveStateFile: public TParavisFileEvent
195   {
196     TSaveStateFile(SalomeApp_Application* theApp, const char* theFileName ) :
197       TParavisFileEvent(theApp, theFileName)      
198     {}
199     
200     virtual void Execute()
201     {
202       PVGUI_Module* aPVModule = getModule();
203       if (aPVModule)
204         aPVModule->saveParaviewState(myName);
205     }
206   };
207
208
209   /*!
210    * Event to restore Paraview state from disk file.
211    * This event is used for Study restoring
212    */
213   struct TLoadStateFile: public TParavisFileEvent
214   {
215     TLoadStateFile(SalomeApp_Application* theApp, const char* theFileName ) :
216       TParavisFileEvent(theApp, theFileName)      
217     {}
218     
219     virtual void Execute()
220     {
221       PVGUI_Module* aPVModule = getModule();
222       if (aPVModule)
223         aPVModule->loadParaviewState(myName);
224     }
225   };
226
227   /*!
228    * Returns currently active Paraview server
229    */
230   struct TGetActiveServer: public TModuleEvent
231   {
232     typedef pqServer* TResult;
233     TResult myResult;
234
235     TGetActiveServer(SalomeApp_Application* theApp ) :
236       TModuleEvent(theApp), myResult(0)   
237     {}
238     
239     virtual void Execute()
240     {
241       PVGUI_Module* aPVModule = getModule();
242       if (aPVModule)
243         myResult = aPVModule->getActiveServer();
244     }
245   };
246
247 };
248
249
250 #endif  //PV_EVENTS_H