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