Salome HOME
0e5ed865649b12de8b18cda659add071d3db77a6
[modules/gui.git] / src / PVViewer / PVViewer_ViewManager.cxx
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 #include "PVViewer_ViewManager.h"
20 #include "PVViewer_ViewModel.h"
21 #include "PVViewer_ViewWindow.h"
22 #include "PVViewer_LogWindowAdapter.h"
23 #include "PVViewer_GUIElements.h"
24 #include "PVViewer_Behaviors.h"
25 #include "PVViewer_EngineWrapper.h"
26
27 #include <utilities.h>
28 #include <SUIT_MessageBox.h>
29 #include <SUIT_Desktop.h>
30 #include <SUIT_Session.h>
31 #include <SUIT_Study.h>
32 #include <SUIT_ResourceMgr.h>
33 #include <PyInterp_Interp.h>
34 #include <PyConsole_Interp.h>
35 #include <PyConsole_Console.h>
36 #include <LogWindow.h>
37
38 #include <QApplication>
39 #include <QStringList>
40 #include <QDir>
41
42 #include <string>
43
44 #include <pqOptions.h>
45 #include <pqServer.h>
46 #include <pqSettings.h>
47 #include <pqServerDisconnectReaction.h>
48 #include <pqPVApplicationCore.h>
49 #include <pqTabbedMultiViewWidget.h>
50 #include <pqActiveObjects.h>
51 #include <pqServerConnectReaction.h>
52
53 #include <pqParaViewMenuBuilders.h>
54 #include <pqPipelineBrowserWidget.h>
55
56 //---------- Static init -----------------
57 pqPVApplicationCore* PVViewer_ViewManager::MyCoreApp = 0;
58 bool PVViewer_ViewManager::ConfigLoaded = false;
59 PVViewer_Behaviors * PVViewer_ViewManager::ParaviewBehaviors = NULL;
60
61 /*!
62   Constructor
63 */
64 PVViewer_ViewManager::PVViewer_ViewManager( SUIT_Study* study, SUIT_Desktop* desk, LogWindow * logWindow )
65 : SUIT_ViewManager( study, desk, new PVViewer_Viewer() ),
66   desktop(desk)
67 {
68   MESSAGE("PVViewer - view manager created ...")
69   setTitle( tr( "PARAVIEW_VIEW_TITLE" ) );
70   // Initialize minimal paraview stuff (if not already done)
71   ParaviewInitApp(desk, logWindow);
72
73   connect( desk, SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
74            this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
75 //  connect(this, SIGNAL(viewCreated(SUIT_ViewWindow*)), this, SLOT(onPVViewCreated(SUIT_ViewWindow*)));
76 }
77
78 pqPVApplicationCore * PVViewer_ViewManager::GetPVApplication()
79 {
80   return MyCoreApp;
81 }
82
83 /*!
84   \brief Static method, performs initialization of ParaView session.
85   \param fullSetup whether to instanciate all behaviors or just the minimal ones.
86   \return \c true if ParaView has been initialized successfully, otherwise false
87 */
88 bool PVViewer_ViewManager::ParaviewInitApp(SUIT_Desktop * aDesktop, LogWindow * logWindow)
89 {
90   if ( ! MyCoreApp) {
91       // Obtain command-line arguments
92       int argc = 0;
93       char** argv = 0;
94       QString aOptions = getenv("PARAVIEW_OPTIONS");
95       QStringList aOptList = aOptions.split(":", QString::SkipEmptyParts);
96       argv = new char*[aOptList.size() + 1];
97       QStringList args = QApplication::arguments();
98       argv[0] = (args.size() > 0)? strdup(args[0].toLatin1().constData()) : strdup("paravis");
99       argc++;
100
101       foreach (QString aStr, aOptList) {
102         argv[argc] = strdup( aStr.toLatin1().constData() );
103         argc++;
104       }
105       MyCoreApp = new pqPVApplicationCore (argc, argv);
106       if (MyCoreApp->getOptions()->GetHelpSelected() ||
107           MyCoreApp->getOptions()->GetUnknownArgument() ||
108           MyCoreApp->getOptions()->GetErrorMessage() ||
109           MyCoreApp->getOptions()->GetTellVersion()) {
110           return false;
111       }
112
113       // Direct VTK log messages to our SALOME window - TODO: review this
114       PVViewer_LogWindowAdapter * w = PVViewer_LogWindowAdapter::New();
115       w->setLogWindow(logWindow);
116       vtkOutputWindow::SetInstance(w);
117
118       new pqTabbedMultiViewWidget(); // registers a "MULTIVIEW_WIDGET" on creation
119
120       for (int i = 0; i < argc; i++)
121         free(argv[i]);
122       delete[] argv;
123   }
124   // Initialize GUI elements if needed:
125   PVViewer_GUIElements::GetInstance(aDesktop);
126   return true;
127 }
128
129 void PVViewer_ViewManager::ParaviewInitBehaviors(bool fullSetup, SUIT_Desktop* aDesktop)
130 {
131   if (!ParaviewBehaviors)
132       ParaviewBehaviors = new PVViewer_Behaviors(aDesktop);
133
134   if(fullSetup)
135     ParaviewBehaviors->instanciateAllBehaviors(aDesktop);
136   else
137     ParaviewBehaviors->instanciateMinimalBehaviors(aDesktop);
138 }
139
140 void PVViewer_ViewManager::ParaviewLoadConfigurations(bool force)
141 {
142   if (!ConfigLoaded || force)
143     {
144       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
145       QString aPath = resMgr->stringValue("resources", "PVViewer", QString());
146       if (!aPath.isNull()) {
147           MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewFilters.xml");
148           MyCoreApp->loadConfiguration(aPath + QDir::separator() + "ParaViewSources.xml");
149       }
150       ConfigLoaded = true;
151     }
152 }
153
154 void PVViewer_ViewManager::ParaviewCleanup()
155 {
156   // Disconnect from server
157   pqServer* server = pqActiveObjects::instance().activeServer();
158   if (server && server->isRemote())
159     {
160       MESSAGE("~PVViewer_Module(): Disconnecting from remote server ...");
161       pqServerDisconnectReaction::disconnectFromServer();
162     }
163
164   pqApplicationCore::instance()->settings()->sync();
165
166   pqPVApplicationCore * app = GetPVApplication();
167   // Schedule destruction of PVApplication singleton:
168   if (app)
169     app->deleteLater();
170 }
171
172 PVViewer_EngineWrapper * PVViewer_ViewManager::GetEngine()
173 {
174   return PVViewer_EngineWrapper::GetInstance();
175 }
176
177 bool PVViewer_ViewManager::ConnectToExternalPVServer(SUIT_Desktop* aDesktop)
178 {
179   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
180   bool noConnect = aResourceMgr->booleanValue( "PARAVIS", "no_ext_pv_server", false );
181   if (noConnect)
182     return true;
183
184   pqServer* server = pqActiveObjects::instance().activeServer();
185   if (server && server->isRemote())
186     {
187       // Already connected to an external server, do nothing
188       MESSAGE("connectToExternalPVServer(): Already connected to an external PVServer, won't reconnect.");
189       return false;
190     }
191
192   if (GetEngine()->GetGUIConnected())
193     {
194       // Should never be there as the above should already tell us that we are connected.
195       std::stringstream msg2;
196       msg2 << "Internal error while connecting to the pvserver.";
197       msg2 << "ParaView doesn't see a connection, but PARAVIS engine tells us there is already one!" << std::endl;
198       qWarning(msg2.str().c_str());  // will go to the ParaView console (see ParavisMessageOutput below)
199       SUIT_MessageBox::warning( aDesktop,
200                                       QString("Error connecting to PVServer"), QString(msg2.str().c_str()));
201       return false;
202     }
203
204   std::stringstream msg;
205
206   // Try to connect to the external PVServer - gives priority to an externally specified URL:
207   QString serverUrlEnv = getenv("PARAVIEW_PVSERVER_URL");
208   std::string serverUrl;
209   if (!serverUrlEnv.isEmpty())
210     serverUrl = serverUrlEnv.toStdString();
211   else
212     {
213       // Get the URL from the engine (possibly starting the pvserver)
214       serverUrl = GetEngine()->FindOrStartPVServer(0);  // take the first free port
215     }
216
217   msg << "connectToExternalPVServer(): Trying to connect to the external PVServer '" << serverUrl << "' ...";
218   MESSAGE(msg.str());
219
220   if (!pqServerConnectReaction::connectToServer(pqServerResource(serverUrl.c_str())))
221     {
222       std::stringstream msg2;
223       msg2 << "Error while connecting to the requested pvserver '" << serverUrl;
224       msg2 << "'. Might use default built-in connection instead!" << std::endl;
225       qWarning(msg2.str().c_str());  // will go to the ParaView console (see ParavisMessageOutput below)
226       SUIT_MessageBox::warning( aDesktop,
227                                 QString("Error connecting to PVServer"), QString(msg2.str().c_str()));
228       return false;
229     }
230   else
231     {
232       MESSAGE("connectToExternalPVServer(): Connected!");
233       GetEngine()->SetGUIConnected(true);
234     }
235   return true;
236 }
237
238 void PVViewer_ViewManager::onEmulateApply()
239 {
240   PVViewer_GUIElements * guiElements = PVViewer_GUIElements::GetInstance(desktop);
241   guiElements->onEmulateApply();
242 }
243
244 /*!Enable toolbars if view \a view is ParaView viewer and disable otherwise.
245 */
246 void PVViewer_ViewManager::onWindowActivated(SUIT_ViewWindow* view)
247 {
248   if (view)
249     {
250     PVViewer_ViewWindow* pvWindow = dynamic_cast<PVViewer_ViewWindow*>(view);
251     PVViewer_GUIElements * guiElements = PVViewer_GUIElements::GetInstance(desktop);
252     guiElements->setToolBarEnabled(pvWindow!=0);
253     }
254 }