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