Salome HOME
Copyright update 2022
[modules/paravis.git] / test / standalone / gui / PLMainWindow.cxx
1 // Copyright (C) 2010-2022  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 // Author: Adrien Bruneton (CEA)
20
21 #include "PVViewer_GUIElements.h"
22 #include "PLMainWindow.hxx"
23 #include "PVViewer_Core.h"
24 #include "PLViewTab.hxx"
25
26 #include <iostream>
27 #include <QObject>
28 #include <QFileDialog>
29 #include <QMessageBox>
30
31 #include <pqTabbedMultiViewWidget.h>
32 #include <pqApplicationCore.h>
33 #include <pqPVApplicationCore.h>
34 #include <pqObjectBuilder.h>
35 #include <pqLoadDataReaction.h>
36 #include <pqPipelineSource.h>
37 #include <pqManagePluginsReaction.h>
38 #include <pqPropertiesPanel.h>
39 #include <pqPipelineBrowserWidget.h>
40 #include <pqServerManagerModel.h>
41 #include <pqRenderView.h>
42 #include <pqActiveObjects.h>
43 #include <pqProxy.h>
44
45 #include <vtkSMSourceProxy.h>
46 #include <vtkSMProperty.h>
47 #include <vtkSMStringVectorProperty.h>
48
49 PLMainWindow::PLMainWindow(QWidget *parent) :
50   QMainWindow(parent),
51   _pAppC(0),
52   _simplePipeline(),
53   _autoApply(true),
54   _filterMenu(0)
55 {
56   _mainWindow.setupUi(this);
57   _autoApply = _mainWindow.actionAuto_apply->isChecked();
58   QObject::connect(this, SIGNAL(apply()), this, SLOT(onApply()));
59   QObject::connect(this, SIGNAL(changedCurrentFile(QString)), this, SLOT(onApply()));
60   addTab();
61 }
62
63 // Called after ParaView application initialisation:
64 void PLMainWindow::finishUISetup()
65 {
66   _pAppC = PVViewer_Core::GetPVApplication();
67   _pvgui = PVViewer_GUIElements::GetInstance(this);
68
69   QWidget * wprop = _pvgui->getPropertiesPanel();
70   QWidget * wpipe = _pvgui->getPipelineBrowserWidget();
71   wprop->setParent(_mainWindow.propFrame);
72   _mainWindow.verticalLayoutProp->addWidget(wprop);
73   wpipe->setParent(_mainWindow.pipelineFrame);
74   _mainWindow.verticalLayoutPipe->addWidget(wpipe);
75
76   // In this mockup, we play on the parent widget visibility (a QFrame), so show these:
77   _pvgui->getPipelineBrowserWidget()->show();
78   _pvgui->getPropertiesPanel()->show();
79   // and hide these:
80   _mainWindow.propFrame->hide();
81   _mainWindow.pipelineFrame->hide();
82 //  _pvgui->setToolBarEnabled(false);
83 //  _pvgui->setToolBarVisible(false);
84
85 }
86
87 void PLMainWindow::autoApplyCheck(bool isChecked)
88 {
89   _autoApply = isChecked;
90 }
91
92 void PLMainWindow::onApply()
93 {
94   if (_autoApply)
95     {
96       PVViewer_GUIElements * pvgui = PVViewer_GUIElements::GetInstance(this);
97       pqPropertiesPanel * wprop = pvgui->getPropertiesPanel();
98       wprop->apply();
99     }
100 }
101
102 void PLMainWindow::showProp(bool isChecked)
103 {
104   isChecked ? _mainWindow.propFrame->show() : _mainWindow.propFrame->hide();
105 }
106
107 void PLMainWindow::showPipeline(bool isChecked)
108 {
109   isChecked ? _mainWindow.pipelineFrame->show() : _mainWindow.pipelineFrame->hide();
110 }
111
112 void PLMainWindow::onBuildFilterMenu()
113 {
114   if(_filterMenu == 0)
115     {
116       _filterMenu = _pvgui->getFiltersMenu();
117 //      _filterMenu = new QMenu();
118       _filterMenu->setTitle("Filters");
119       this->menuBar()->addMenu(_filterMenu);
120     }
121   else
122     QMessageBox::warning(this, "Warning", "Filter menu already added!");
123 }
124
125 void PLMainWindow::addTab()
126 {
127   int c = _mainWindow.tabWidget->count();
128   PLViewTab * newTab = new PLViewTab(_mainWindow.tabWidget);
129   int newIdx = _mainWindow.tabWidget->addTab(newTab, QString("Tab %1").arg(c+1));
130   _mainWindow.tabWidget->setCurrentIndex(newIdx);
131
132   // Connect buttons
133   QObject::connect(newTab, SIGNAL(onInsertSingleView(PLViewTab *)), this, SLOT(insertSingleView(PLViewTab *)));
134   QObject::connect(newTab, SIGNAL(onInsertMultiView(PLViewTab *)), this, SLOT(insertMultiView(PLViewTab *)));
135   QObject::connect(newTab, SIGNAL(onInsertSpreadsheetView(PLViewTab *)), this, SLOT(insertSpreadsheetView(PLViewTab *)));
136 }
137
138 void PLMainWindow::deleteTab()
139 {
140   int c = _mainWindow.tabWidget->currentIndex();
141   if (c != -1)
142     {
143       _mainWindow.tabWidget->removeTab(c);
144     }
145 }
146
147 void PLMainWindow::currentTabChanged(int tabIdx)
148 {
149   QWidget * w = _mainWindow.tabWidget->widget(tabIdx);
150   if (w)
151     {
152       PLViewTab * viewtab = qobject_cast<PLViewTab *>(w);
153       if (viewtab && viewtab->getpqView())
154         {
155           pqActiveObjects::instance().setActiveView(viewtab->getpqView());
156         }
157     }
158 }
159
160 void PLMainWindow::doOpenFile()
161 {
162     // Clean up vizu
163 //    cleanAll();
164
165     // Load the stuff as wireframe in the main view:
166     QList<pqPipelineSource *> many = pqLoadDataReaction::loadData();
167     if (many.isEmpty())
168     {
169       std::cout << "no file selected!" << std::endl;
170       return;
171     }
172     if (many.count() > 1)
173       {
174         QMessageBox msgBox;
175         msgBox.setText("Select one file only!");
176         msgBox.exec();
177         cleanAll();
178         return;
179       }
180
181     pqPipelineSource * src = many.at(0);
182     std::cout << "num of out ports: " << src->getNumberOfOutputPorts() << std::endl;
183
184     // A cone to start with:
185 //    pqPipelineSource * src = this->_pAppC->getObjectBuilder()->createSource(QString("sources"), QString("ConeSource"),
186 //        this->_activeServer);
187     if(src)
188       _simplePipeline.push(src);
189
190     // Retrieve loaded file name
191     vtkSMProperty * prop = src->getSourceProxy()->GetProperty("FileName");
192     vtkSMStringVectorProperty * prop2 = vtkSMStringVectorProperty::SafeDownCast(prop);
193     QString fName(prop2->GetElement(0));
194
195     // Emit signal
196     emit changedCurrentFile(fName);
197 }
198
199 void PLMainWindow::insertSingleView(PLViewTab * tab)
200 {
201   // Create a new view proxy on the server
202   pqObjectBuilder* builder = _pAppC->getObjectBuilder();
203   pqServer* active_serv = pqActiveObjects::instance().activeServer();
204
205   std::cout << "About to create single view ..." << std::endl;
206   pqView * pqview = builder->createView(QString("RenderView"), active_serv);
207   std::cout << "Created: " << pqview << "!" << std::endl;
208
209   // Retrieve its widget and pass it to the Qt tab:
210   QWidget* viewWidget = pqview->widget();
211
212 //  QWidget* viewWidget = new QPushButton("toto");
213   tab->hideAndReplace(viewWidget, pqview);
214
215   pqActiveObjects::instance().setActiveView(pqview);
216 }
217
218 void PLMainWindow::insertSpreadsheetView(PLViewTab * tab)
219 {
220   // Create a new view proxy on the server
221   pqObjectBuilder* builder = _pAppC->getObjectBuilder();
222   pqServer* active_serv = pqActiveObjects::instance().activeServer();
223
224   std::cout << "About to create spreadsheet view ..." << std::endl;
225   pqView * pqview = builder->createView(QString("SpreadSheetView"), active_serv);
226   std::cout << "Created: " << pqview << "!" << std::endl;
227
228   // Retrieve its widget and pass it to the Qt tab:
229   QWidget* viewWidget = pqview->widget();
230
231 //  QWidget* viewWidget = new QPushButton("toto");
232   tab->hideAndReplace(viewWidget, pqview);
233
234   pqActiveObjects::instance().setActiveView(pqview);
235 }
236
237
238 void PLMainWindow::insertMultiView(PLViewTab * tab)
239 {
240   // Retrieve TabbedMultiView and see if it is already attached to someone:
241   PVViewer_GUIElements * pvgui = PVViewer_GUIElements::GetInstance(this);
242   pqTabbedMultiViewWidget * multiv = pvgui->getTabbedMultiViewWidget();
243
244   QWidget * parent = multiv->nativeParentWidget();
245   if (parent)
246     {
247       QMessageBox msgBox;
248       msgBox.setText("Multi-view already in use in another tab! Close it first.");
249       msgBox.exec();
250     }
251   else
252     {
253       tab->hideAndReplace(multiv, NULL);
254     }
255 }
256
257 void PLMainWindow::doShrink()
258 {
259   if(!_simplePipeline.isEmpty())
260   {
261     cleanAllButSource();
262
263     pqPipelineSource * src = this->_pAppC->getObjectBuilder()->createFilter(QString("filters"),
264         QString("ShrinkFilter"), _simplePipeline.top());
265     if(src)
266       _simplePipeline.push(src);
267
268     // Hit apply
269     emit apply();
270   }
271 }
272
273 void PLMainWindow::doSlice()
274 {
275   if(!_simplePipeline.isEmpty())
276   {
277     cleanAllButSource();
278
279     pqPipelineSource * src = this->_pAppC->getObjectBuilder()->createFilter(QString("filters"),
280         QString("Cut"), _simplePipeline.top());
281     if(src)
282       _simplePipeline.push(src);
283
284     // Hit apply
285     emit apply();
286   }
287 }
288
289 void PLMainWindow::doManagePlugins()
290 {
291    pqManagePluginsReaction::managePlugins();
292 }
293
294 void PLMainWindow::cleanAll()
295 {
296   this->_pAppC->getObjectBuilder()->destroyPipelineProxies();
297   _simplePipeline.resize(0);
298 }
299
300 void PLMainWindow::cleanAllButSource()
301 {
302   while(_simplePipeline.size() > 1)
303     this->_pAppC->getObjectBuilder()->destroy(_simplePipeline.pop());
304 }