Salome HOME
First integration of a new PV3D viewer.
[modules/gui.git] / src / PVViewer / PVViewer_Core.cxx
1 // Copyright (C) 2014-2022  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
22 #include "PVViewer_Core.h"
23 #include "PVViewer_OutputWindow.h"
24 #include "PVViewer_GUIElements.h"
25 #include "PVViewer_Behaviors.h"
26 #include "PVViewer_Core.h"
27
28 #include <Qtx.h>
29
30 #include <QApplication>
31 #include <QStringList>
32 #include <QDir>
33 #include <QMainWindow>
34 #include <QStandardPaths>
35
36 #include <string>
37
38 #include <pqOptions.h>
39 #include <pqSettings.h>
40 #include <pqPVApplicationCore.h>
41 #include <pqTabbedMultiViewWidget.h>
42 #include <pqParaViewMenuBuilders.h>
43 #include <pqActiveObjects.h>
44 #include <pqPipelineBrowserWidget.h>
45 #include <pqServerDisconnectReaction.h>
46
47 //---------- Static init -----------------
48 pqPVApplicationCore* PVViewer_Core::MyCoreApp = nullptr;
49 bool PVViewer_Core::MyPqTabWidSingletonLoaded = false;
50 bool PVViewer_Core::ConfigLoaded = false;
51 PVViewer_Behaviors * PVViewer_Core::ParaviewBehaviors = nullptr;
52
53 pqPVApplicationCore * PVViewer_Core::GetPVApplication()
54 {
55   return MyCoreApp;
56 }
57
58 bool PVViewer_Core::ParaViewInitAppCore()
59 {
60   if ( ! MyCoreApp) {
61       // Obtain command-line arguments
62       int argc = 0;
63       char** argv = 0;
64       QString aOptions = Qtx::getenv("PARAVIEW_OPTIONS");
65       QStringList aOptList = aOptions.split(":", QString::SkipEmptyParts);
66       argv = new char*[aOptList.size() + 4]; // add one, MPI requires argv[argc] = 0!
67       for (int i =0; i< aOptList.size() + 4; i++)
68           argv[i] = 0;
69       QStringList args = QApplication::arguments();
70       argv[0] = (args.size() > 0)? strdup(args[0].toLatin1().constData()) : strdup("paravis");
71       argc++;
72
73       foreach (QString aStr, aOptList) {
74         argv[argc] = strdup( aStr.toLatin1().constData() );
75         argc++;
76       }
77       argv[argc++] = strdup("--multi-servers");
78       // Make salome sharing the same server configuration than external one with "salome shell paraview"
79       QStringList li(QStandardPaths::standardLocations(QStandardPaths::ConfigLocation));
80       foreach(QString pathConfig,li)
81         {
82           QFileInfo fi(QDir(pathConfig),QString("ParaView"));
83           if(fi.exists() && fi.isDir())
84             {
85               QFileInfo fi2(fi.canonicalFilePath(),"servers.pvsc");
86               if(fi2.exists() && fi2.isFile())
87                 {
88                   QString addEntry(QString("--servers-file=%1").arg(fi2.canonicalFilePath()));
89                   std::string addEntry2(addEntry.toStdString());
90                   argv[argc++] = strdup(addEntry2.c_str());
91                   break;
92                 }
93             }
94         }
95       //
96       MyCoreApp = new pqPVApplicationCore (argc, argv);
97       for (int i = 0; i < argc; i++)
98         free(argv[i]);
99       delete[] argv;
100       if (MyCoreApp->getOptions()->GetHelpSelected() ||
101           MyCoreApp->getOptions()->GetUnknownArgument() ||
102           MyCoreApp->getOptions()->GetErrorMessage() ||
103           MyCoreApp->getOptions()->GetTellVersion()) {
104           return false;
105       }
106   }
107   return true;
108 }
109
110 /*!
111   \brief Static method, performs initialization of ParaView session.
112   \param fullSetup whether to instanciate all behaviors or just the minimal ones.
113   \return \c true if ParaView has been initialized successfully, otherwise false
114 */
115 bool PVViewer_Core::ParaviewInitApp()
116 {
117   if( ! MyPqTabWidSingletonLoaded )
118   {
119     if( ! ParaViewInitAppCore() )
120       return false;
121
122     // Direct VTK log messages to our SALOME window - TODO: review this
123     PVViewer_OutputWindow * w = PVViewer_OutputWindow::New();
124     vtkOutputWindow::SetInstance(w);
125     pqActiveObjects::instance().setActiveView(nullptr);
126     new pqTabbedMultiViewWidget(); // registers a "MULTIVIEW_WIDGET" on creation
127     // Initialization of ParaView GUI widgets will be done when these widgets are
128     // really needed.
129     // PVViewer_GUIElements* inst = PVViewer_GUIElements::GetInstance(aDesktop);
130     // inst->getPropertiesPanel();
131     PVViewer_Core::MyPqTabWidSingletonLoaded = true;
132     return true;
133   }
134   return true;
135 }
136
137 void PVViewer_Core::ParaviewInitBehaviors(bool fullSetup, QMainWindow* aDesktop)
138 {
139   if (!ParaviewBehaviors)
140       ParaviewBehaviors = new PVViewer_Behaviors(aDesktop);
141
142   if(fullSetup)
143     ParaviewBehaviors->instanciateAllBehaviors(aDesktop);
144   else
145     ParaviewBehaviors->instanciateMinimalBehaviors(aDesktop);
146 }
147
148 void PVViewer_Core::ParaviewLoadConfigurations(const QString & configPath, bool force)
149 {
150   if (!ConfigLoaded || force)
151     {
152       if (!configPath.isNull()) {
153           MyCoreApp->loadConfiguration(configPath + QDir::separator() + "ParaViewFilters.xml");
154           MyCoreApp->loadConfiguration(configPath + QDir::separator() + "ParaViewSources.xml");
155       }
156       ConfigLoaded = true;
157     }
158 }
159
160 void PVViewer_Core::ParaviewCleanup()
161 {
162   // Disconnect from server
163   pqServer* server = pqActiveObjects::instance().activeServer();
164   if (server && server->isRemote())
165     {
166       pqServerDisconnectReaction::disconnectFromServer();
167     }
168
169   pqApplicationCore::instance()->settings()->sync();
170
171   pqPVApplicationCore * app = GetPVApplication();
172   // Schedule destruction of PVApplication singleton:
173   if (app)
174     app->deleteLater();
175 }
176