1 // Copyright (C) 2023 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include <QColorDialog>
25 #include <vtkActorCollection.h>
27 #include "SPV3D_ViewModel.h"
28 #include "SPV3D_ViewWindow.h"
29 #include "SPV3D_Prs.h"
30 #include "PVViewer_Core.h"
32 #include <pqActiveObjects.h>
33 #include <pqApplicationCore.h>
34 #include <pqObjectBuilder.h>
35 #include <pqPipelineSource.h>
36 #include <pqRenderView.h>
38 #include <vtkSMPropertyHelper.h>
39 #include <vtkSMParaViewPipelineControllerWithRendering.h>
40 #include <vtkSMRenderViewProxy.h>
41 #include <vtkSMRepresentationProxy.h>
42 #include <vtkSMSourceProxy.h>
43 #include <vtkSMPVRepresentationProxy.h>
45 #include "SUIT_ViewModel.h"
46 #include "SUIT_ViewManager.h"
48 #include "SALOME_InteractiveObject.hxx"
50 #include "QtxActionToolMgr.h"
51 #include "QtxBackgroundTool.h"
53 #include <pqObjectBuilder.h>
54 #include <pqApplicationCore.h>
55 #include <pqServerManagerModel.h>
56 #include <pqServerResource.h>
58 #include <pqPVApplicationCore.h>
59 #include <pqParaViewBehaviors.h>
61 #include <QApplication>
63 // VSR: Uncomment below line to allow texture background support in PV3D viewer
64 #define PV3D_ENABLE_TEXTURED_BACKGROUND
69 SPV3D_ViewModel::SPV3D_ViewModel()
71 mySelectionEnabled = true;
77 SPV3D_ViewModel::~SPV3D_ViewModel()
82 void SPV3D_ViewModel::initialize()
86 void SPV3D_ViewModel::render() const
92 /*!Create new instance of view window on desktop \a theDesktop.
93 *\retval SUIT_ViewWindow* - created view window pointer.
95 SUIT_ViewWindow* SPV3D_ViewModel::createView( SUIT_Desktop* theDesktop )
97 SPV3D_ViewWindow* aViewWindow = new SPV3D_ViewWindow(theDesktop, this);
99 aViewWindow->SetSelectionEnabled( isSelectionEnabled() );
100 PVViewer_Core::ParaviewInitApp();
101 QApplication::instance()->installEventFilter( PVViewer_Core::GetPVAppCore() );
102 new pqParaViewBehaviors(aViewWindow,aViewWindow);
103 pqObjectBuilder *builder(pqApplicationCore::instance()->getObjectBuilder());
104 QObject::connect(builder, &pqObjectBuilder::sourceCreated, this, &SPV3D_ViewModel::onSourceCreated);
105 pqServer *serv(pqApplicationCore::instance()->getServerManagerModel()->findServer(pqServerResource("builtin:")));
106 pqView *view=builder->createView(QString("RenderView"),serv);
115 Sets new view manager
116 \param theViewManager - new view manager
118 void SPV3D_ViewModel::setViewManager(SUIT_ViewManager* theViewManager)
120 SUIT_ViewModel::setViewManager(theViewManager);
122 if ( !theViewManager )
125 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
126 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
128 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
129 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
131 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
132 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
134 connect(theViewManager, SIGNAL(viewCreated(SUIT_ViewWindow*)),
135 this, SLOT(onViewCreated(SUIT_ViewWindow*)));
139 Builds popup for vtk viewer
141 void SPV3D_ViewModel::contextMenuPopup( QMenu */*thePopup*/ )
147 SLOT: called on mouse button press, empty implementation
149 void SPV3D_ViewModel::onMousePress(SUIT_ViewWindow* /*vw*/, QMouseEvent* /*event*/)
153 SLOT: called on mouse move, empty implementation
155 void SPV3D_ViewModel::onMouseMove(SUIT_ViewWindow* /*vw*/, QMouseEvent* /*event*/)
159 SLOT: called on mouse button release, empty implementation
161 void SPV3D_ViewModel::onMouseRelease(SUIT_ViewWindow* /*vw*/, QMouseEvent* /*event*/)
165 Enables/disables selection
166 \param isEnabled - new state
168 void SPV3D_ViewModel::enableSelection(bool isEnabled)
170 mySelectionEnabled = isEnabled;
171 //!! To be done for view windows
172 if (SUIT_ViewManager* aViewManager = getViewManager()) {
173 QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
174 for ( int i = 0; i < aViews.count(); i++ )
176 if ( SPV3D_ViewWindow* aView = dynamic_cast<SPV3D_ViewWindow*>(aViews.at( i )) )
177 aView->SetSelectionEnabled( isEnabled );
182 //clear current selection in the viewer
183 bool blocked = blockSignals( true );
184 /*if ( SUIT_ViewManager* aViewMgr = getViewManager() ) {
185 if( SPV3D_ViewWindow* aViewWindow = dynamic_cast<SPV3D_ViewWindow*>( aViewMgr->getActiveView() ) ){
189 blockSignals( blocked );
194 pqView *SPV3D_ViewModel::getView() const
201 \param prs - presentation
203 void SPV3D_ViewModel::Display( const SALOME_PV3DPrs* prs )
205 if(const SPV3D_Prs* aPrs = dynamic_cast<const SPV3D_Prs*>( prs ))
207 if( !aPrs->GetRepresentation() )
209 pqObjectBuilder *builder(pqApplicationCore::instance()->getObjectBuilder());
210 pqActiveObjects::instance().setActiveView(getView());
211 pqPipelineSource *mySourceProducer = aPrs->GetSourceProducer();
212 aPrs->SetSourceProducer( mySourceProducer );
213 pqDataRepresentation* myRepr(builder->createDataRepresentation(mySourceProducer->getOutputPort(0),getView(),"CADRepresentation"));//"GeometryRepresentation"
214 vtkSMViewProxy::RepresentationVisibilityChanged(myRepr->getViewProxy(), myRepr->getProxy(), true);
215 aPrs->SetRepresentation(myRepr);
217 pqDataRepresentation* myRepr = aPrs->GetRepresentation();
218 myRepr->setVisible(1);
219 vtkSMPVRepresentationProxy* proxy(dynamic_cast<vtkSMPVRepresentationProxy*>(myRepr->getProxy()));
222 vtkSMPropertyHelper inputHelper(proxy, "Input");
223 vtkSMSourceProxy* input = vtkSMSourceProxy::SafeDownCast(inputHelper.GetAsProxy());
224 input->UpdatePipeline();
226 getView()->resetDisplay();
233 \param prs - presentation
234 \param forced - removes object from view
236 void SPV3D_ViewModel::Erase( const SALOME_PV3DPrs* prs, const bool /*forced*/ )
238 // NYI - hide a source
239 if(const SPV3D_Prs* aPrs = dynamic_cast<const SPV3D_Prs*>( prs )){
242 aPrs->GetRepresentation()->setVisible(0);
244 //pqObjectBuilder* builder = pqApplicationCore::instance()->getObjectBuilder();
245 //pqServer* activeServer = pqActiveObjects::instance().activeServer();
246 //builder->destroySources(activeServer);
251 Erase all presentations
252 \param forced - removes all objects from view
254 void SPV3D_ViewModel::EraseAll( SALOME_Displayer* d, const bool forced )
256 SALOME_View::EraseAll( d, forced );
257 if(SPV3D_ViewWindow* aViewWindow = dynamic_cast<SPV3D_ViewWindow*>(getViewManager()->getActiveView()))
258 aViewWindow->EraseAll();
263 Create presentation corresponding to the entry
266 SALOME_Prs* SPV3D_ViewModel::CreatePrs( const char* entry )
268 if(SPV3D_ViewWindow* aViewWindow = dynamic_cast<SPV3D_ViewWindow*>(getViewManager()->getActiveView()))
270 return aViewWindow->findOrCreatePrs( entry );
276 \return true if object is displayed in viewer
277 \param obj - object to be checked
279 bool SPV3D_ViewModel::isVisible( const Handle(SALOME_InteractiveObject)& io )
281 if(SPV3D_ViewWindow* aViewWindow = dynamic_cast<SPV3D_ViewWindow*>(getViewManager()->getActiveView()))
282 return aViewWindow->isVisible( io );
287 \Collect objects visible in viewer
288 \param theList - visible objects collection
290 void SPV3D_ViewModel::GetVisible( SALOME_ListIO &/*theList*/ )
296 Updates current viewer
298 void SPV3D_ViewModel::Repaint()
304 void SPV3D_ViewModel::onViewCreated( SUIT_ViewWindow */*view*/) {
305 #ifdef VGL_WORKAROUND
306 if ( SPV3D_ViewWindow* svw = dynamic_cast<SPV3D_ViewWindow*>( view ) )
307 QTimer::singleShot(500, [svw] () { svw->Repaint(); } );
311 //-----------------------------------------------------------------------------
312 void SPV3D_ViewModel::onSourceCreated(pqPipelineSource* source)
314 std::string sourceXMLName(source->getSourceProxy()->GetXMLName());
315 pqObjectBuilder* builder = pqApplicationCore::instance()->getObjectBuilder();
316 if (sourceXMLName == "XMLPolyDataReader" || sourceXMLName == "GeometryGenerator" || sourceXMLName == "ShapeSource" || sourceXMLName == "CubeSource")
318 if (this->GeometrySource)
320 if (this->GeometrySource == source) {
323 builder->destroy(this->GeometrySource);
325 this->GeometrySource = source;
329 qWarning("Unsupported Source");
332 this->showSelectedMode();
335 //-----------------------------------------------------------------------------
336 void SPV3D_ViewModel::showSelectedMode()
338 vtkNew<vtkSMParaViewPipelineControllerWithRendering> controller;
339 pqView* activeView = pqActiveObjects::instance().activeView();
342 if (this->GeometrySource)
344 #if PARAVIEW_VERSION_MINOR <= 10
345 this->GeometrySource->updatePipeline();
346 this->GeometrySource->setModifiedState(pqProxy::UNMODIFIED);
349 vtkSMSourceProxy *sourceProxy = this->GeometrySource->getSourceProxy();
350 vtkSMViewProxy *viewProxy = activeView->getViewProxy();
351 //const char *representationType = "CADRepresentation";
352 controller->Show(sourceProxy, 0, viewProxy);//, representationType);
356 activeView->render();
357 activeView->resetDisplay();