1 // SALOME VTKViewer : build VTK viewer into Salome desktop
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
29 #include "SALOME_Actor.h"
31 #include "SVTK_View.h"
32 #include "SVTK_Renderer.h"
33 #include "SVTK_MainWindow.h"
34 #include "SVTK_RenderWindowInteractor.h"
35 #include "SALOME_ListIteratorOfListIO.hxx"
37 #include "VTKViewer_Algorithm.h"
38 #include "SVTK_Functor.h"
40 #include <vtkActorCollection.h>
41 #include <vtkRenderer.h>
44 //----------------------------------------------------------------------------
46 ::SVTK_SignalHandler(SVTK_MainWindow* theMainWindow):
47 QObject(theMainWindow),
48 myMainWindow(theMainWindow)
50 SVTK_RenderWindowInteractor* anInteractor = theMainWindow->GetInteractor();
52 connect(anInteractor,SIGNAL(KeyPressed(QKeyEvent*)),
53 this,SIGNAL(KeyPressed(QKeyEvent*)) );
54 connect(anInteractor,SIGNAL(KeyReleased(QKeyEvent*)),
55 this,SIGNAL(KeyReleased(QKeyEvent*)));
56 connect(anInteractor,SIGNAL(MouseButtonPressed(QMouseEvent*)),
57 this,SIGNAL(MouseButtonPressed(QMouseEvent*)));
58 connect(anInteractor,SIGNAL(MouseButtonReleased(QMouseEvent*)),
59 this,SIGNAL(MouseButtonReleased(QMouseEvent*)));
60 connect(anInteractor,SIGNAL(MouseDoubleClicked(QMouseEvent*)),
61 this,SIGNAL(MouseDoubleClicked(QMouseEvent*)));
62 connect(anInteractor,SIGNAL(MouseMove(QMouseEvent*)),
63 this,SIGNAL(MouseMove(QMouseEvent*)));
64 connect(anInteractor,SIGNAL(contextMenuRequested(QContextMenuEvent*)),
65 this,SIGNAL(contextMenuRequested(QContextMenuEvent*)));
66 connect(anInteractor,SIGNAL(selectionChanged()),
67 this,SIGNAL(selectionChanged()));
71 ::~SVTK_SignalHandler()
83 //----------------------------------------------------------------
86 ::Repaint(bool theUpdateTrihedron)
88 myMainWindow->Repaint(theUpdateTrihedron);
91 //----------------------------------------------------------------------------
96 return myMainWindow->GetRenderer();
103 return myMainWindow->getRenderer();
106 //----------------------------------------------------------------
109 struct THighlightAction
112 THighlightAction( bool theIsHighlight ):
113 myIsHighlight( theIsHighlight )
117 operator()( SALOME_Actor* theActor)
119 if(theActor->GetMapper() && theActor->hasIO()){
120 theActor->Highlight( myIsHighlight );
128 ::onSelectionChanged()
130 vtkActorCollection* anActors = myMainWindow->getRenderer()->GetActors();
132 using namespace SVTK;
133 ForEach<SALOME_Actor>(anActors,
134 THighlightAction( false ));
135 SVTK_Selector* aSelector = myMainWindow->GetSelector();
136 const SALOME_ListIO& aListIO = aSelector->StoredIObjects();
137 SALOME_ListIteratorOfListIO anIter(aListIO);
138 for(; anIter.More(); anIter.Next()){
139 ForEachIf<SALOME_Actor>(anActors,
140 TIsSameIObject<SALOME_Actor>(anIter.Value()),
141 THighlightAction(true));
144 myMainWindow->Repaint(false);
148 //----------------------------------------------------------------------------
150 ::SVTK_View(SVTK_MainWindow* theMainWindow) :
151 SVTK_SignalHandler(theMainWindow)
155 //----------------------------------------------------------------------------
161 //----------------------------------------------------------------
166 using namespace SVTK;
167 ForEach<SALOME_Actor>(getRenderer()->GetActors(),
168 THighlightAction( false ));
172 //----------------------------------------------------------------
175 ::highlight( const Handle(SALOME_InteractiveObject)& theIO,
179 using namespace SVTK;
180 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
181 TIsSameIObject<SALOME_Actor>( theIO ),
182 THighlightAction(theIsHighlight));
186 //----------------------------------------------------------------------------
189 ::SetSelectionProp(const double& theRed,
190 const double& theGreen,
191 const double& theBlue,
194 GetRenderer()->SetSelectionProp(theRed,theGreen,theBlue,theWidth);
197 //----------------------------------------------------------------------------
200 ::SetPreselectionProp(const double& theRed,
201 const double& theGreen,
202 const double& theBlue,
205 GetRenderer()->SetPreselectionProp(theRed,theGreen,theBlue,theWidth);
208 //----------------------------------------------------------------------------
211 ::SetSelectionTolerance(const double& theTolNodes,
212 const double& theTolCell)
214 GetRenderer()->SetSelectionTolerance(theTolNodes,theTolCell);
217 //----------------------------------------------------------------------------
220 ::isInViewer(const Handle(SALOME_InteractiveObject)& theIObject)
222 using namespace SVTK;
223 SALOME_Actor* anActor =
224 Find<SALOME_Actor>(getRenderer()->GetActors(),
225 TIsSameIObject<SALOME_Actor>(theIObject));
226 return anActor != NULL;
229 //----------------------------------------------------------------------------
232 ::isVisible(const Handle(SALOME_InteractiveObject)& theIObject)
234 using namespace SVTK;
235 SALOME_Actor* anActor =
236 Find<SALOME_Actor>(getRenderer()->GetActors(),
237 TIsSameIObject<SALOME_Actor>(theIObject));
238 return anActor != NULL && anActor->GetVisibility();
241 //----------------------------------------------------------------------------
244 ::rename(const Handle(SALOME_InteractiveObject)& theIObject,
245 const QString& theName)
247 using namespace SVTK;
248 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
249 TIsSameIObject<SALOME_Actor>(theIObject),
250 TSetFunction<SALOME_Actor,const char*,QString>
251 (&SALOME_Actor::setName,theName.latin1()));
254 //----------------------------------------------------------------------------
259 return myDisplayMode;
264 ::SetDisplayMode(int theMode)
267 ChangeRepresentationToWireframe();
269 ChangeRepresentationToSurface();
270 myDisplayMode = theMode;
275 ::SetDisplayMode(const Handle(SALOME_InteractiveObject)& theIObject,
278 using namespace SVTK;
279 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
280 TIsSameIObject<SALOME_Actor>(theIObject),
281 TSetFunction<SALOME_Actor,int>
282 (&SALOME_Actor::setDisplayMode,theMode));
285 //----------------------------------------------------------------------------
288 ::ChangeRepresentationToWireframe()
290 ChangeRepresentationToWireframe(getRenderer()->GetActors());
295 ::ChangeRepresentationToSurface()
297 ChangeRepresentationToSurface(getRenderer()->GetActors());
303 ::ChangeRepresentationToWireframe(vtkActorCollection* theCollection)
305 using namespace SVTK;
306 ForEach<SALOME_Actor>(theCollection,
307 TSetFunction<SALOME_Actor,int>
308 (&SALOME_Actor::setDisplayMode,0));
314 ::ChangeRepresentationToSurface(vtkActorCollection* theCollection)
316 using namespace SVTK;
317 ForEach<SALOME_Actor>(theCollection,
318 TSetFunction<SALOME_Actor,int>
319 (&SALOME_Actor::setDisplayMode,1));
323 //----------------------------------------------------------------------------
328 VTK::TSetFunction<vtkActor,int> mySetFunction;
330 mySetFunction(&vtkActor::SetVisibility,false)
333 operator()(SALOME_Actor* theActor)
335 theActor->SetVisibility(false);
336 // Erase dependent actors
337 vtkActorCollection* aCollection = vtkActorCollection::New();
338 theActor->GetChildActors(aCollection);
339 VTK::ForEach<vtkActor>(aCollection,mySetFunction);
340 aCollection->Delete();
349 using namespace SVTK;
350 ForEach<SALOME_Actor>(getRenderer()->GetActors(),
359 using namespace SVTK;
360 ForEach<SALOME_Actor>(getRenderer()->GetActors(),
361 TSetVisibility<SALOME_Actor>(true));
368 ::Erase(SALOME_Actor* theActor,
371 SVTK::TErase()(theActor);
380 ::Erase(const Handle(SALOME_InteractiveObject)& theIObject,
383 using namespace SVTK;
384 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
385 TIsSameIObject<SALOME_Actor>(theIObject),
391 //----------------------------------------------------------------------------
394 ::Display(SALOME_Actor* theActor,
397 GetRenderer()->AddActor(theActor);
398 theActor->SetVisibility(true);
406 ::Display(const Handle(SALOME_InteractiveObject)& theIObject,
409 using namespace SVTK;
410 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
411 TIsSameIObject<SALOME_Actor>(theIObject),
412 TSetVisibility<SALOME_Actor>(true));
420 ::DisplayOnly(const Handle(SALOME_InteractiveObject)& theIObject)
427 //----------------------------------------------------------------------------
432 SVTK_Renderer* myRenderer;
433 TRemoveAction(SVTK_Renderer* theRenderer):
434 myRenderer(theRenderer)
437 operator()(SALOME_Actor* theActor)
439 myRenderer->RemoveActor(theActor);
446 ::Remove(const Handle(SALOME_InteractiveObject)& theIObject,
449 using namespace SVTK;
450 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
451 TIsSameIObject<SALOME_Actor>(theIObject),
452 TRemoveAction(GetRenderer()));
459 ::Remove(SALOME_Actor* theActor,
462 GetRenderer()->RemoveActor(theActor);
469 ::RemoveAll(bool theIsUpdate)
471 vtkRenderer* aRenderer = getRenderer();
472 if(vtkActorCollection* anActors = aRenderer->GetActors()){
473 anActors->InitTraversal();
474 while(vtkActor *anAct = anActors->GetNextActor()){
475 if(SALOME_Actor* aSAct = SALOME_Actor::SafeDownCast(anAct)){
476 if(aSAct->hasIO() && aSAct->getIO()->hasEntry())
477 aRenderer->RemoveActor( anAct );
486 //----------------------------------------------------------------------------
489 ::GetTransparency(const Handle(SALOME_InteractiveObject)& theIObject)
491 using namespace SVTK;
492 SALOME_Actor* anActor =
493 Find<SALOME_Actor>(getRenderer()->GetActors(),
494 TIsSameIObject<SALOME_Actor>(theIObject));
496 return 1.0 - anActor->GetOpacity();
503 ::SetTransparency(const Handle(SALOME_InteractiveObject)& theIObject,
506 float anOpacity = 1.0 - theTrans;
507 using namespace SVTK;
508 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
509 TIsSameIObject<SALOME_Actor>(theIObject),
510 TSetFunction<SALOME_Actor,float>
511 (&SALOME_Actor::SetOpacity,anOpacity));
514 //----------------------------------------------------------------------------
517 ::SetColor(const Handle(SALOME_InteractiveObject)& theIObject,
518 const QColor& theColor)
520 float aColor[3] = {theColor.red()/255., theColor.green()/255., theColor.blue()/255.};
522 using namespace SVTK;
523 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
524 TIsSameIObject<SALOME_Actor>(theIObject),
525 TSetFunction<SALOME_Actor,const float*>
526 (&SALOME_Actor::SetColor,aColor));
532 ::GetColor(const Handle(SALOME_InteractiveObject)& theIObject)
534 using namespace SVTK;
535 SALOME_Actor* anActor =
536 Find<SALOME_Actor>(getRenderer()->GetActors(),
537 TIsSameIObject<SALOME_Actor>(theIObject));
540 anActor->GetColor(r,g,b);
541 return QColor(int(r*255),int(g*255),int(b*255));
544 return QColor(0,0,0);