Salome HOME
Renamed the viewer part into PVViewer_*
[modules/paravis.git] / src / PVGUI / view / PVViewer_ViewManager.cxx
1 // Copyright (C) 2010-2014  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 #include <PVViewer_ViewManager.h>
20 #include <PVViewer_ViewModel.h>
21 #include <PVViewer_ViewWindow.h>
22 #include <PVViewer_Behaviors.h>
23 #include <PVViewer_LogWindowAdapter.h>
24 #include <utilities.h>
25 #include <SalomeApp_Application.h>
26 #include <SALOMEconfig.h>
27 #include <SALOME_LifeCycleCORBA.hxx>
28 #include <SUIT_MessageBox.h>
29 #include <SUIT_Desktop.h>
30 #include <SUIT_Session.h>
31 #include <PyInterp_Interp.h>
32 #include <PyConsole_Interp.h>
33 #include <PyConsole_Console.h>
34
35 #include <QApplication>
36 #include <QStringList>
37 #include <string>
38
39 #include <pqOptions.h>
40 #include <pqServer.h>
41 #include <pqSettings.h>
42 #include <pqServerDisconnectReaction.h>
43 #include <pqPVApplicationCore.h>
44 #include <pqTabbedMultiViewWidget.h>
45 #include <pqActiveObjects.h>
46 #include <pqServerConnectReaction.h>
47
48 //---------- Static init -----------------
49 pqPVApplicationCore* PVViewer_ViewManager::MyCoreApp = 0;
50 PARAVIS_ORB::PARAVIS_Gen_var PVViewer_ViewManager::MyEngine;
51
52
53 /*!
54   Constructor
55 */
56 PVViewer_ViewManager::PVViewer_ViewManager( SUIT_Study* study, SUIT_Desktop* desk )
57 : SUIT_ViewManager( study, desk, new PVViewer_Viewer() )
58 {
59   MESSAGE("PARAVIS - view manager created ...")
60   setTitle( tr( "PARAVIEW_VIEW_TITLE" ) );
61 }
62
63 pqPVApplicationCore * PVViewer_ViewManager::GetPVApplication()
64 {
65   return MyCoreApp;
66 }
67
68 /*!
69   \brief Static method, performs initialization of ParaView session.
70   \param fullSetup whether to instanciate all behaviors or just the minimal ones.
71   \return \c true if ParaView has been initialized successfully, otherwise false
72 */
73 bool PVViewer_ViewManager::ParaviewInitApp(SUIT_Desktop * aDesktop)
74 {
75   if ( ! MyCoreApp) {
76       // Obtain command-line arguments
77       int argc = 0;
78       char** argv = 0;
79       QString aOptions = getenv("PARAVIS_OPTIONS");
80       QStringList aOptList = aOptions.split(":", QString::SkipEmptyParts);
81       argv = new char*[aOptList.size() + 1];
82       QStringList args = QApplication::arguments();
83       argv[0] = (args.size() > 0)? strdup(args[0].toLatin1().constData()) : strdup("paravis");
84       argc++;
85
86       foreach (QString aStr, aOptList) {
87         argv[argc] = strdup( aStr.toLatin1().constData() );
88         argc++;
89       }
90       MyCoreApp = new pqPVApplicationCore (argc, argv);
91       if (MyCoreApp->getOptions()->GetHelpSelected() ||
92           MyCoreApp->getOptions()->GetUnknownArgument() ||
93           MyCoreApp->getOptions()->GetErrorMessage() ||
94           MyCoreApp->getOptions()->GetTellVersion()) {
95           return false;
96       }
97
98       // Direct VTK log messages to our SALOME window - TODO: review this
99       vtkOutputWindow::SetInstance(PVViewer_LogWindowAdapter::New());
100
101       new pqTabbedMultiViewWidget(); // registers a "MULTIVIEW_WIDGET" on creation
102
103       // At this stage, the pqPythonManager has been initialized, i.e. the current process has
104       // activated the embedded Python interpreter. "paraview" package has also been imported once already.
105       // Make sure the current process executes paraview's Python command with the "fromGUI" flag.
106       // This is used in pvsimple.py to avoid reconnecting the GUI thread to the pvserver (when
107       // user types "import pvsimple" in SALOME's console).
108       SalomeApp_Application* app =
109                   dynamic_cast< SalomeApp_Application* >(SUIT_Session::session()->activeApplication());
110       PyConsole_Interp* pyInterp = app->pythonConsole()->getInterp();
111       {
112         PyLockWrapper aGil;
113         std::string cmd = "import paraview; paraview.fromGUI = True;";
114         pyInterp->run(cmd.c_str());
115       }
116
117       for (int i = 0; i < argc; i++)
118         free(argv[i]);
119       delete[] argv;
120   }
121
122   return true;
123 }
124
125 void PVViewer_ViewManager::ParaviewInitBehaviors(bool fullSetup, SUIT_Desktop* aDesktop)
126 {
127   PVViewer_Behaviors * behav = new PVViewer_Behaviors(aDesktop);
128   if(fullSetup)
129     behav->instanciateAllBehaviors(aDesktop);
130   else
131     behav->instanciateMinimalBehaviors(aDesktop);
132 }
133
134 void PVViewer_ViewManager::ParaviewCleanup()
135 {
136   // Disconnect from server
137   pqServer* server = pqActiveObjects::instance().activeServer();
138   if (server && server->isRemote())
139     {
140       MESSAGE("~PVViewer_Module(): Disconnecting from remote server ...");
141       pqServerDisconnectReaction::disconnectFromServer();
142     }
143
144   pqApplicationCore::instance()->settings()->sync();
145
146   pqPVApplicationCore * app = GetPVApplication();
147   // Schedule destruction of PVApplication singleton:
148   if (app)
149     app->deleteLater();
150 }
151
152 PARAVIS_ORB::PARAVIS_Gen_var PVViewer_ViewManager::GetEngine()
153 {
154   // initialize PARAVIS module engine (load, if necessary)
155   if ( CORBA::is_nil( MyEngine ) ) {
156     Engines::EngineComponent_var comp =
157         SalomeApp_Application::lcc()->FindOrLoad_Component( "FactoryServer", "PARAVIS" );
158     MyEngine = PARAVIS_ORB::PARAVIS_Gen::_narrow( comp );
159   }
160   return MyEngine;
161 }
162
163 bool PVViewer_ViewManager::ConnectToExternalPVServer(SUIT_Desktop* aDesktop)
164 {
165   pqServer* server = pqActiveObjects::instance().activeServer();
166   if (server && server->isRemote())
167     {
168       // Already connected to an external server, do nothing
169       MESSAGE("connectToExternalPVServer(): Already connected to an external PVServer, won't reconnect.");
170       return false;
171     }
172
173   if (GetEngine()->GetGUIConnected())
174     {
175       // Should never be there as the above should already tell us that we are connected.
176       std::stringstream msg2;
177       msg2 << "Internal error while connecting to the pvserver.";
178       msg2 << "ParaView doesn't see a connection, but PARAVIS engine tells us there is already one!" << std::endl;
179       qWarning(msg2.str().c_str());  // will go to the ParaView console (see ParavisMessageOutput below)
180       SUIT_MessageBox::warning( aDesktop,
181                                       QString("Error connecting to PVServer"), QString(msg2.str().c_str()));
182       return false;
183     }
184
185   std::stringstream msg;
186
187   // Try to connect to the external PVServer - gives priority to an externally specified URL:
188   QString serverUrlEnv = getenv("PARAVIS_PVSERVER_URL");
189   std::string serverUrl;
190   if (!serverUrlEnv.isEmpty())
191     serverUrl = serverUrlEnv.toStdString();
192   else
193     {
194       // Get the URL from the engine (possibly starting the pvserver)
195       CORBA::String_var url = GetEngine()->FindOrStartPVServer(0);  // take the first free port
196       serverUrl = (char *)url;
197     }
198
199   msg << "connectToExternalPVServer(): Trying to connect to the external PVServer '" << serverUrl << "' ...";
200   MESSAGE(msg.str());
201
202   if (!pqServerConnectReaction::connectToServer(pqServerResource(serverUrl.c_str())))
203     {
204       std::stringstream msg2;
205       msg2 << "Error while connecting to the requested pvserver '" << serverUrl;
206       msg2 << "'. Might use default built-in connection instead!" << std::endl;
207       qWarning(msg2.str().c_str());  // will go to the ParaView console (see ParavisMessageOutput below)
208       SUIT_MessageBox::warning( aDesktop,
209                                 QString("Error connecting to PVServer"), QString(msg2.str().c_str()));
210       return false;
211     }
212   else
213     {
214       MESSAGE("connectToExternalPVServer(): Connected!");
215       GetEngine()->SetGUIConnected(true);
216     }
217   return true;
218 }