Salome HOME
Multi server mode for PARAVIS. This allow to integrate modules using local visualizat...
[modules/gui.git] / src / PVViewer / PVViewer_ViewManager.cxx
1 // Copyright (C) 2014-2016  CEA/DEN, EDF R&D, OPEN CASCADE
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 // Author: Adrien Bruneton (CEA)
20
21 #include "PVViewer_ViewManager.h"
22 #include "PVViewer_ViewWindow.h"
23 #include "PVViewer_ViewModel.h"
24 #include "PVViewer_GUIElements.h"
25 #include "PVViewer_Core.h"
26 #include "PVViewer_InitSingleton.h"
27 #include "PVServer_ServiceWrapper.h"
28
29 #include <utilities.h>
30
31 #include <LogWindow.h>
32 #include <SUIT_Desktop.h>
33 #include <SUIT_Study.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_ResourceMgr.h>
37
38 #include <pqServer.h>
39 #include <pqServerConnectReaction.h>
40 #include <pqActiveObjects.h>
41
42 /*!
43   Constructor
44 */
45 PVViewer_ViewManager::PVViewer_ViewManager( SUIT_Study* study, SUIT_Desktop* desk, LogWindow * logWindow )
46 : SUIT_ViewManager( study, desk, new PVViewer_Viewer() ),
47   desktop(desk)
48 {
49   MESSAGE("PVViewer - view manager created ...")
50   setTitle( tr( "PARAVIEW_VIEW_TITLE" ) );
51
52   // Initialize minimal paraview stuff (if not already done)
53   PVViewer_InitSingleton::Init(desk, logWindow);
54
55   connect( desk, SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
56            this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
57 }
58
59
60 PVServer_ServiceWrapper * PVViewer_ViewManager::GetService()
61 {
62   return PVServer_ServiceWrapper::GetInstance();
63 }
64
65 QString PVViewer_ViewManager::GetPVConfigPath()
66 {
67   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
68   return resMgr->stringValue("resources", "PVViewer", QString());
69 }
70
71 bool PVViewer_ViewManager::ConnectToExternalPVServer(QMainWindow* aDesktop)
72 {
73   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
74   bool noConnect = aResourceMgr->booleanValue( "PARAVIS", "no_ext_pv_server", false );
75   if (noConnect)
76     return true;
77
78   pqServer* server = pqActiveObjects::instance().activeServer();
79   if (server && server->isRemote())
80     {
81       // Already connected to an external server, do nothing
82       MESSAGE("connectToExternalPVServer(): Already connected to an external PVServer, won't reconnect.");
83       return false;
84     }
85
86   if (GetService()->GetGUIConnected())
87     {
88       // Should never be there as the above should already tell us that we are connected.
89       std::stringstream msg2;
90       msg2 << "Internal error while connecting to the pvserver.";
91       msg2 << "ParaView doesn't see a connection, but PARAVIS engine tells us there is already one!" << std::endl;
92       qWarning(msg2.str().c_str());  // will go to the ParaView console (see ParavisMessageOutput below)
93       SUIT_MessageBox::warning( aDesktop,
94                                       QString("Error connecting to PVServer"), QString(msg2.str().c_str()));
95       return false;
96     }
97
98   std::stringstream msg;
99
100   // Try to connect to the external PVServer - gives priority to an externally specified URL:
101   QString serverUrlEnv = getenv("PARAVIEW_PVSERVER_URL");
102   std::string serverUrl;
103   if (!serverUrlEnv.isEmpty())
104     serverUrl = serverUrlEnv.toStdString();
105   else
106     {
107       // Get the URL from the engine (possibly starting the pvserver)
108       serverUrl = GetService()->FindOrStartPVServer(0);  // take the first free port
109     }
110
111   msg << "connectToExternalPVServer(): Trying to connect to the external PVServer '" << serverUrl << "' ...";
112   MESSAGE(msg.str());
113
114   if (!pqServerConnectReaction::connectToServer(pqServerResource(serverUrl.c_str())))
115     {
116       std::stringstream msg2;
117       msg2 << "Error while connecting to the requested pvserver '" << serverUrl;
118       msg2 << "'. Might use default built-in connection instead!" << std::endl;
119       qWarning(msg2.str().c_str());  // will go to the ParaView console (see ParavisMessageOutput below)
120       SUIT_MessageBox::warning( aDesktop,
121                                 QString("Error connecting to PVServer"), QString(msg2.str().c_str()));
122       return false;
123     }
124   else
125     {
126       MESSAGE("connectToExternalPVServer(): Connected!");
127       GetService()->SetGUIConnected(true);
128     }
129   return true;
130 }
131
132
133 /*!Enable toolbars if view \a view is ParaView viewer and disable otherwise.
134 */
135 void PVViewer_ViewManager::onWindowActivated(SUIT_ViewWindow* view)
136 {
137   if (view)
138     {
139     PVViewer_ViewWindow* pvWindow = dynamic_cast<PVViewer_ViewWindow*>(view);
140     PVViewer_GUIElements * guiElements = PVViewer_GUIElements::GetInstance(desktop);
141     guiElements->setToolBarEnabled(pvWindow!=0);
142     }
143 }