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