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