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