Salome HOME
Copyright update 2022
[modules/gui.git] / src / PVViewer / PVViewer_GUIElements.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 #include "PVViewer_GUIElements.h"
22 #include "PVServer_ServiceWrapper.h"
23 #include "PVViewer_Core.h"
24 #include "PVViewer_ViewManager.h"
25
26 #include <pqActiveObjects.h>
27 #include <pqAnimationManager.h>
28 #include <pqAnimationTimeToolbar.h>
29 #include <pqApplicationCore.h>
30 #include <pqApplyBehavior.h>
31 #include <pqAxesToolbar.h>
32 #include <pqCameraToolbar.h>
33 #include <pqColorToolbar.h>
34 #include <pqMainControlsToolbar.h>
35 #include <pqPVApplicationCore.h>
36 #include <pqParaViewMenuBuilders.h>
37 #include <pqPipelineBrowserWidget.h>
38 #include <pqPipelineModel.h>
39 #include <pqPropertiesPanel.h>
40 #include <pqPythonManager.h>
41 #include <pqRepresentationToolbar.h>
42 #include <pqServerConnectReaction.h>
43 #include <pqServerManagerModel.h>
44 #include <pqServerResource.h>
45 #include <pqSetName.h>
46 #include <pqVCRToolbar.h>
47 #include <pqPipelineSource.h>
48
49 #include <vtkSMSessionProxyManager.h>
50 #include <vtkSMProxyIterator.h>
51
52 #include <vtkPVConfig.h>
53
54 #include <QAction>
55 #include <QCoreApplication>
56 #include <QLayout>
57 #include <QList>
58 #include <QMainWindow>
59 #include <QMenu>
60 #include <QToolBar>
61
62 PVViewer_GUIElements * PVViewer_GUIElements::theInstance = 0;
63
64 PVViewer_GUIElements::PVViewer_GUIElements(QMainWindow* desktop) :
65   propertiesPanel(0),
66   pipelineBrowserWidget(0),
67   pipelineModel(0),
68   sourcesMenu(0),
69   filtersMenu(0),
70   macrosMenu(0),
71   catalystMenu(0),
72   mainToolBar(0),
73   vcrToolbar(0),
74   timeToolbar(0),
75   colorToolbar(0),
76   reprToolbar(0),
77   cameraToolbar(0),
78   axesToolbar(0),
79   macrosToolbar(0),
80   commonToolbar(0),
81   dataToolbar(0),
82   myDesktop(desktop),
83   myPVWidgetsFlag(false),
84   mainAction(0),
85   vcrAction(0),
86   timeAction(0),
87   colorAction(0),
88   reprAction(0),
89   cameraAction(0),
90   axesAction(0),
91   macrosAction(0),
92   commonAction(0),
93   dataAction(0)
94 {
95 }
96
97 PVViewer_GUIElements * PVViewer_GUIElements::GetInstance(QMainWindow* desk)
98 {
99   if (! theInstance)
100     theInstance = new PVViewer_GUIElements(desk);
101   return theInstance;
102 }
103
104 /**
105  * See ParaView source code: pqParaViewMenuBuilders::buildToolbars()
106  * to keep this function up to date:
107  */
108 void PVViewer_GUIElements::buildPVWidgets()
109 {
110   if (!myPVWidgetsFlag) {
111
112     //Pipeline Browser
113     if ( !pipelineBrowserWidget ) {
114         pqPipelineModel* pipelineModel = new pqPipelineModel(*pqApplicationCore::instance()->getServerManagerModel(), this);
115         pipelineModel->setView(pqActiveObjects::instance().activeView());
116         pipelineBrowserWidget  = new pqPipelineBrowserWidget(myDesktop);
117         pipelineBrowserWidget->setModel(pipelineModel);
118     }
119
120     // Properties panel
121     if ( !propertiesPanel ) {
122       propertiesPanel = new pqPropertiesPanel(myDesktop);
123     }
124     
125     // Sources Menu
126     if (!sourcesMenu) {
127       sourcesMenu = new QMenu(0);
128       pqParaViewMenuBuilders::buildSourcesMenu(*sourcesMenu, myDesktop);
129     }
130
131     //Filer Menu
132     if (!filtersMenu) {
133       filtersMenu = new QMenu(0);
134       pqParaViewMenuBuilders::buildFiltersMenu(*filtersMenu, myDesktop);
135     }
136
137     // Macros Menu
138     if (!macrosMenu) {
139       macrosMenu = new QMenu(0);
140       pqParaViewMenuBuilders::buildMacrosMenu(*macrosMenu);
141     }
142
143     // Catalyst Menu
144     if (!catalystMenu) {
145       catalystMenu = new QMenu(0);
146 #if PARAVIEW_VERSION_MAJOR==5 && PARAVIEW_VERSION_MINOR<9
147       pqParaViewMenuBuilders::buildCatalystMenu(*catalystMenu, myDesktop);
148 #else
149       pqParaViewMenuBuilders::buildCatalystMenu(*catalystMenu);
150 #endif
151     }
152
153     mainToolBar = new pqMainControlsToolbar(myDesktop)
154       << pqSetName("MainControlsToolbar");
155     mainToolBar->layout()->setSpacing(0);
156
157     vcrToolbar = new pqVCRToolbar(myDesktop)
158       << pqSetName("VCRToolbar");
159     vcrToolbar->layout()->setSpacing(0);
160
161     timeToolbar = new pqAnimationTimeToolbar(myDesktop)
162       << pqSetName("currentTimeToolbar");
163     timeToolbar->layout()->setSpacing(0);
164
165     colorToolbar = new pqColorToolbar(myDesktop)
166       << pqSetName("variableToolbar");
167     colorToolbar->layout()->setSpacing(0);
168
169     reprToolbar = new pqRepresentationToolbar(myDesktop)
170       << pqSetName("representationToolbar");
171     reprToolbar->layout()->setSpacing(0);
172
173     cameraToolbar = new pqCameraToolbar(myDesktop)
174       << pqSetName("cameraToolbar");
175     cameraToolbar->layout()->setSpacing(0);
176     
177     axesToolbar = new pqAxesToolbar(myDesktop)
178       << pqSetName("axesToolbar");
179     axesToolbar->layout()->setSpacing(0);
180     
181     // Give the macros menu to the pqPythonMacroSupervisor
182     pqPythonManager* manager = qobject_cast<pqPythonManager*>(
183     pqApplicationCore::instance()->manager("PYTHON_MANAGER"));
184
185     macrosToolbar = new QToolBar("Macros Toolbars", myDesktop)
186       << pqSetName("MacrosToolbar");
187     manager->addWidgetForRunMacros(macrosToolbar);
188     
189     commonToolbar = new QToolBar("Common", myDesktop) << pqSetName("Common");
190     commonToolbar->layout()->setSpacing(0);
191     
192     dataToolbar = new QToolBar("DataAnalysis", myDesktop) << pqSetName("DataAnalysis");
193     dataToolbar->layout()->setSpacing(0);
194     
195     // add Toolbars    
196     myDesktop->addToolBar(Qt::TopToolBarArea, mainToolBar);
197     myDesktop->addToolBar(Qt::TopToolBarArea, vcrToolbar);
198     myDesktop->addToolBar(Qt::TopToolBarArea, timeToolbar);
199     myDesktop->addToolBar(Qt::TopToolBarArea, colorToolbar);
200     myDesktop->insertToolBarBreak(colorToolbar);
201     myDesktop->addToolBar(Qt::TopToolBarArea, reprToolbar);
202     myDesktop->addToolBar(Qt::TopToolBarArea, cameraToolbar);
203     myDesktop->addToolBar(Qt::TopToolBarArea, axesToolbar);
204     myDesktop->addToolBar(Qt::TopToolBarArea, macrosToolbar);
205     myDesktop->addToolBar(Qt::TopToolBarArea, commonToolbar);
206     myDesktop->addToolBar(Qt::TopToolBarArea, dataToolbar);
207     
208     mainAction = mainToolBar->toggleViewAction();
209     vcrAction = vcrToolbar->toggleViewAction();
210     timeAction = timeToolbar->toggleViewAction();
211     colorAction = colorToolbar->toggleViewAction();
212     reprAction = reprToolbar->toggleViewAction();
213     cameraAction = cameraToolbar->toggleViewAction();
214     axesAction = axesToolbar->toggleViewAction();
215     macrosAction = macrosToolbar->toggleViewAction();
216     commonAction = commonToolbar->toggleViewAction();
217     dataAction = dataToolbar->toggleViewAction();
218
219     // The piece od the code below is neccessary to correct update "Pipeline Browser",
220     // "Properties Panel", toolbars and menus
221     
222     // Initilaize pqApplyBehavior here because witout pqPropertiesPanel instance it doesn't make sence
223     pqApplyBehavior* applyBehavior = new pqApplyBehavior(this);
224     foreach (pqPropertiesPanel* ppanel, myDesktop->findChildren<pqPropertiesPanel*>())
225       {
226         applyBehavior->registerPanel(ppanel);
227       }
228     
229     QMetaObject::invokeMethod( &pqActiveObjects::instance(),
230                                "portChanged",
231                                Qt::AutoConnection,
232                                Q_ARG( pqOutputPort*, pqActiveObjects::instance().activePort() ) );
233     
234     QMetaObject::invokeMethod( &pqActiveObjects::instance(),
235                                "viewChanged",
236                                Qt::AutoConnection,
237                                Q_ARG( pqView*, pqActiveObjects::instance().activeView() ) ); 
238
239     pqServerManagerModel *smModel = pqApplicationCore::instance()->getServerManagerModel();
240     pqServer* serv = pqActiveObjects::instance().activeServer();    
241
242     if (serv) {
243       QMetaObject::invokeMethod( smModel,
244                                  "serverAdded",
245                                  Qt::AutoConnection,
246                                  Q_ARG( pqServer*, serv ) );
247       
248       QMetaObject::invokeMethod( serv,
249                                  "nameChanged",
250                                  Qt::AutoConnection,
251                                  Q_ARG( pqServerManagerModelItem* , NULL ) );
252     }
253     
254     //publishExistingSources();
255     myPVWidgetsFlag = true;
256   }
257 }
258
259 void PVViewer_GUIElements::setToolBarVisible(bool show)
260 {  
261   QCoreApplication::processEvents();
262   if (!myPVWidgetsFlag)
263     return;
264   
265   mainAction->setChecked(!show);
266   mainAction->setVisible(show);
267   mainAction->trigger();
268
269   setVCRTimeToolBarVisible(show);
270
271   colorAction->setChecked(!show);
272   colorAction->setVisible(show);
273   colorAction->trigger();
274   reprAction->setChecked(!show);
275   reprAction->setVisible(show);
276   reprAction->trigger();
277   cameraAction->setChecked(!show);
278   cameraAction->setVisible(show);
279   cameraAction->trigger();
280   axesAction->setChecked(!show);
281   axesAction->setVisible(show);
282   axesAction->trigger();
283   macrosAction->setChecked(!show);
284   macrosAction->setVisible(show);
285   macrosAction->trigger();
286   commonAction->setChecked(!show);
287   commonAction->setVisible(show);
288   commonAction->trigger();
289   dataAction->setChecked(!show);
290   dataAction->setVisible(show);
291   dataAction->trigger();
292 }
293
294 void PVViewer_GUIElements::setVCRTimeToolBarVisible(bool show)
295 {
296   vcrAction->setChecked(!show);
297   vcrAction->setVisible(show);
298   vcrAction->trigger();
299   timeAction->setChecked(!show);
300   timeAction->setVisible(show);
301   timeAction->trigger();
302 }
303
304 QList<QToolBar*> PVViewer_GUIElements::getToolbars()
305 {
306   buildPVWidgets();
307   QList<QToolBar*> l;
308   l << mainToolBar << vcrToolbar << timeToolbar << colorToolbar
309     << reprToolbar << cameraToolbar << axesToolbar << macrosToolbar
310     << commonToolbar << dataToolbar;
311   return l;
312 }
313
314 void PVViewer_GUIElements::setToolBarEnabled(bool enabled)
315 {
316   if (!myPVWidgetsFlag)
317     return;
318   
319   mainToolBar  ->setEnabled(enabled);
320   vcrToolbar   ->setEnabled(enabled);
321   timeToolbar  ->setEnabled(enabled);
322   colorToolbar ->setEnabled(enabled);
323   reprToolbar  ->setEnabled(enabled);
324   cameraToolbar->setEnabled(enabled);
325   axesToolbar  ->setEnabled(enabled);
326   macrosToolbar->setEnabled(enabled);
327   commonToolbar->setEnabled(enabled);
328   dataToolbar  ->setEnabled(enabled);
329 }
330
331
332 pqPropertiesPanel* PVViewer_GUIElements::getPropertiesPanel()
333 {
334   buildPVWidgets();
335   return propertiesPanel;
336 }
337
338 pqPipelineBrowserWidget* PVViewer_GUIElements::getPipelineBrowserWidget()
339 {
340   buildPVWidgets();
341   return pipelineBrowserWidget;   
342 }
343
344
345 pqVCRToolbar* PVViewer_GUIElements::getVCRToolbar()
346 {
347   buildPVWidgets();
348   return vcrToolbar;
349 }
350
351 pqAnimationTimeToolbar* PVViewer_GUIElements::getTimeToolbar()
352 {
353   buildPVWidgets();
354   return timeToolbar;
355 }
356
357 QMenu* PVViewer_GUIElements::getFiltersMenu() {
358   buildPVWidgets();
359   return filtersMenu;
360 }
361 QMenu* PVViewer_GUIElements::getSourcesMenu() {
362   buildPVWidgets();
363   return sourcesMenu;
364 }
365
366 QMenu* PVViewer_GUIElements::getMacrosMenu()  {
367   buildPVWidgets();
368   return macrosMenu;
369 }
370
371 QMenu* PVViewer_GUIElements::getCatalystMenu()  {
372   buildPVWidgets();
373   return catalystMenu;
374 }
375
376 void PVViewer_GUIElements::publishExistingSources() {
377   vtkSMSessionProxyManager* pxm = pqActiveObjects::instance().proxyManager();
378   pqServerManagerModel* smmodel = pqApplicationCore::instance()->getServerManagerModel();
379   if( !pxm || !smmodel )
380     return;
381   vtkSMProxyIterator* iter = vtkSMProxyIterator::New();
382   iter->SetModeToOneGroup();
383   iter->SetSessionProxyManager( pxm );
384   for ( iter->Begin( "sources" ); !iter->IsAtEnd(); iter->Next() ) {
385     if ( pqProxy* item = smmodel->findItem<pqProxy*>( iter->GetProxy() ) ) {
386       pqPipelineSource* source = qobject_cast<pqPipelineSource*>( item );
387       QMetaObject::invokeMethod( smmodel,
388                                  "sourceAdded",
389                                  Qt::AutoConnection,
390                                  Q_ARG( pqPipelineSource* , source ) );
391     }
392   }
393 }