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