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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
47 ::SVTK_SignalHandler(SVTK_MainWindow* theMainWindow):
48 QObject(theMainWindow),
49 myMainWindow(theMainWindow)
51 SVTK_RenderWindowInteractor* anInteractor = theMainWindow->GetInteractor();
53 connect(anInteractor,SIGNAL(KeyPressed(QKeyEvent*)),
54 this,SIGNAL(KeyPressed(QKeyEvent*)) );
55 connect(anInteractor,SIGNAL(KeyReleased(QKeyEvent*)),
56 this,SIGNAL(KeyReleased(QKeyEvent*)));
57 connect(anInteractor,SIGNAL(MouseButtonPressed(QMouseEvent*)),
58 this,SIGNAL(MouseButtonPressed(QMouseEvent*)));
59 connect(anInteractor,SIGNAL(MouseButtonReleased(QMouseEvent*)),
60 this,SIGNAL(MouseButtonReleased(QMouseEvent*)));
61 connect(anInteractor,SIGNAL(MouseDoubleClicked(QMouseEvent*)),
62 this,SIGNAL(MouseDoubleClicked(QMouseEvent*)));
63 connect(anInteractor,SIGNAL(MouseMove(QMouseEvent*)),
64 this,SIGNAL(MouseMove(QMouseEvent*)));
65 connect(anInteractor,SIGNAL(contextMenuRequested(QContextMenuEvent*)),
66 this,SIGNAL(contextMenuRequested(QContextMenuEvent*)));
67 connect(anInteractor,SIGNAL(selectionChanged()),
68 this,SIGNAL(selectionChanged()));
75 ::~SVTK_SignalHandler()
80 \return corresponding svtk main window
91 Redirect the request to #SVTK_MainWindow::Repaint (just for flexibility)
95 ::Repaint(bool theUpdateTrihedron)
97 myMainWindow->Repaint(theUpdateTrihedron);
101 Redirect the request to #SVTK_MainWindow::GetRenderer (just for flexibility)
107 return myMainWindow->GetRenderer();
111 Redirect the request to #SVTK_MainWindow::getRenderer (just for flexibility)
117 return myMainWindow->getRenderer();
122 struct THighlightAction
125 THighlightAction( bool theIsHighlight ):
126 myIsHighlight( theIsHighlight )
130 operator()( SALOME_Actor* theActor)
132 if(theActor->GetMapper() && theActor->hasIO()){
133 theActor->Highlight( myIsHighlight );
140 SLOT: called on selection change
144 ::onSelectionChanged()
146 vtkActorCollection* anActors = myMainWindow->getRenderer()->GetActors();
148 using namespace SVTK;
149 ForEach<SALOME_Actor>(anActors,
150 THighlightAction( false ));
151 SVTK_Selector* aSelector = myMainWindow->GetSelector();
152 const SALOME_ListIO& aListIO = aSelector->StoredIObjects();
153 SALOME_ListIteratorOfListIO anIter(aListIO);
154 for(; anIter.More(); anIter.Next()){
155 ForEachIf<SALOME_Actor>(anActors,
156 TIsSameIObject<SALOME_Actor>(anIter.Value()),
157 THighlightAction(true));
160 myMainWindow->Repaint(false);
167 ::SVTK_View(SVTK_MainWindow* theMainWindow) :
168 SVTK_SignalHandler(theMainWindow)
181 Unhilights all objects in viewer
187 using namespace SVTK;
188 ForEach<SALOME_Actor>(getRenderer()->GetActors(),
189 THighlightAction( false ));
194 Hilights/unhilights object in viewer
195 \param theIO - object to be updated
196 \param theIsHighlight - if it is true, object will be hilighted, otherwise it will be unhilighted
197 \param theIsUpdate - update current viewer
201 ::highlight( const Handle(SALOME_InteractiveObject)& theIO,
205 using namespace SVTK;
206 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
207 TIsSameIObject<SALOME_Actor>( theIO ),
208 THighlightAction(theIsHighlight));
214 \param theEntry - entry that corresponds to intractive objects
216 Handle(SALOME_InteractiveObject)
218 ::FindIObject(const char* theEntry)
220 using namespace SVTK;
221 SALOME_Actor* anActor =
222 Find<SALOME_Actor>(getRenderer()->GetActors(),
223 TIsSameEntry<SALOME_Actor>(theEntry));
225 return anActor->getIO();
231 Redirect the request to #SVTK_Renderer::SetPreselectionProp
235 ::SetSelectionProp(const double& theRed,
236 const double& theGreen,
237 const double& theBlue,
240 GetRenderer()->SetSelectionProp(theRed,theGreen,theBlue,theWidth);
244 Redirect the request to #SVTK_Renderer::SetPreselectionProp
248 ::SetPreselectionProp(const double& theRed,
249 const double& theGreen,
250 const double& theBlue,
253 GetRenderer()->SetPreselectionProp(theRed,theGreen,theBlue,theWidth);
257 Redirect the request to #SVTK_Renderer::SetPreselectionProp
261 ::SetSelectionTolerance(const double& theTolNodes,
262 const double& theTolCell,
263 const double& theTolObjects)
265 GetRenderer()->SetSelectionTolerance(theTolNodes,theTolCell, theTolObjects);
269 \return true if object is in viewer or in collector
270 \param theIO - object to be checked
274 ::isInViewer(const Handle(SALOME_InteractiveObject)& theIObject)
276 using namespace SVTK;
277 SALOME_Actor* anActor =
278 Find<SALOME_Actor>(getRenderer()->GetActors(),
279 TIsSameIObject<SALOME_Actor>(theIObject));
280 return anActor != NULL;
284 \return true if object is displayed in viewer
285 \param theIO - object to be checked
289 ::isVisible(const Handle(SALOME_InteractiveObject)& theIObject)
291 using namespace SVTK;
292 SALOME_Actor* anActor =
293 Find<SALOME_Actor>(getRenderer()->GetActors(),
294 TIsSameIObject<SALOME_Actor>(theIObject));
295 return anActor != NULL && anActor->GetVisibility();
299 Changes name of object
300 \param theIObject - object to be renamed
301 \param theName - new name
305 ::rename(const Handle(SALOME_InteractiveObject)& theIObject,
306 const QString& theName)
308 using namespace SVTK;
309 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
310 TIsSameIObject<SALOME_Actor>(theIObject),
311 TSetFunction<SALOME_Actor,const char*,QString>
312 (&SALOME_Actor::setName,theName.latin1()));
316 \return current display mode (obsolete)
322 return myDisplayMode;
326 Set current display mode
327 \param theMode - new display mode
331 ::SetDisplayMode(int theMode)
334 ChangeRepresentationToWireframe();
336 ChangeRepresentationToSurface();
337 myDisplayMode = theMode;
341 Set current display mode
342 \param theIObject - object
343 \param theMode - new display mode
347 ::SetDisplayMode(const Handle(SALOME_InteractiveObject)& theIObject,
350 using namespace SVTK;
351 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
352 TIsSameIObject<SALOME_Actor>(theIObject),
353 TSetFunction<SALOME_Actor,int>
354 (&SALOME_Actor::setDisplayMode,theMode));
358 Change all actors to wireframe
362 ::ChangeRepresentationToWireframe()
364 ChangeRepresentationToWireframe(getRenderer()->GetActors());
368 Change all actors to shading
372 ::ChangeRepresentationToSurface()
374 ChangeRepresentationToSurface(getRenderer()->GetActors());
378 Change to wireframe a list of vtkactor
379 theCollection - list of vtkactor
383 ::ChangeRepresentationToWireframe(vtkActorCollection* theCollection)
385 using namespace SVTK;
386 ForEach<SALOME_Actor>(theCollection,
387 TSetFunction<SALOME_Actor,int>
388 (&SALOME_Actor::setDisplayMode,0));
393 Change to shading a list of vtkactor
394 theCollection - list of vtkactor
398 ::ChangeRepresentationToSurface(vtkActorCollection* theCollection)
400 using namespace SVTK;
401 ForEach<SALOME_Actor>(theCollection,
402 TSetFunction<SALOME_Actor,int>
403 (&SALOME_Actor::setDisplayMode,1));
411 VTK::TSetFunction<vtkActor,int> mySetFunction;
413 mySetFunction(&vtkActor::SetVisibility,false)
416 operator()(SALOME_Actor* theActor)
418 theActor->SetVisibility(false);
419 // Erase dependent actors
420 vtkActorCollection* aCollection = vtkActorCollection::New();
421 theActor->GetChildActors(aCollection);
422 VTK::ForEach<vtkActor>(aCollection,mySetFunction);
423 aCollection->Delete();
429 To erase all existing VTK presentations
435 using namespace SVTK;
436 ForEach<SALOME_Actor>(getRenderer()->GetActors(),
442 To display all existing VTK presentations
448 using namespace SVTK;
449 ForEach<SALOME_Actor>(getRenderer()->GetActors(),
450 TSetVisibility<SALOME_Actor>(true));
455 To erase VTK presentation
456 \param theActor - actor
457 \param theIsUpdate - updates current viewer
461 ::Erase(SALOME_Actor* theActor,
464 SVTK::TErase()(theActor);
472 To erase VTK presentation
473 \param theIObject - object
474 \param theIsUpdate - updates current viewer
478 ::Erase(const Handle(SALOME_InteractiveObject)& theIObject,
481 using namespace SVTK;
482 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
483 TIsSameIObject<SALOME_Actor>(theIObject),
490 To display the VTK presentation
494 ::Display(SALOME_Actor* theActor,
497 GetRenderer()->AddActor(theActor);
498 theActor->SetVisibility(true);
505 To display the VTK presentation
509 ::Display(const Handle(SALOME_InteractiveObject)& theIObject,
512 using namespace SVTK;
513 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
514 TIsSameIObject<SALOME_Actor>(theIObject),
515 TSetVisibility<SALOME_Actor>(true));
522 To display VTK presentation with defined #SALOME_InteractiveObject and erase all others
526 ::DisplayOnly(const Handle(SALOME_InteractiveObject)& theIObject)
537 SVTK_Renderer* myRenderer;
538 TRemoveAction(SVTK_Renderer* theRenderer):
539 myRenderer(theRenderer)
542 operator()(SALOME_Actor* theActor)
544 myRenderer->RemoveActor(theActor);
550 To remove the VTK presentation
554 ::Remove(const Handle(SALOME_InteractiveObject)& theIObject,
557 using namespace SVTK;
558 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
559 TIsSameIObject<SALOME_Actor>(theIObject),
560 TRemoveAction(GetRenderer()));
566 To remove the VTK presentation
570 ::Remove(SALOME_Actor* theActor,
573 GetRenderer()->RemoveActor(theActor);
579 To remove all VTK presentations
583 ::RemoveAll(bool theIsUpdate)
585 vtkRenderer* aRenderer = getRenderer();
586 if(vtkActorCollection* anActors = aRenderer->GetActors()){
587 anActors->InitTraversal();
588 while(vtkActor *anAct = anActors->GetNextActor()){
589 if(SALOME_Actor* aSAct = SALOME_Actor::SafeDownCast(anAct)){
590 if(aSAct->hasIO() && aSAct->getIO()->hasEntry())
591 aRenderer->RemoveActor( anAct );
601 \return current transparency
602 \param theIObject - object
606 ::GetTransparency(const Handle(SALOME_InteractiveObject)& theIObject)
608 using namespace SVTK;
609 SALOME_Actor* anActor =
610 Find<SALOME_Actor>(getRenderer()->GetActors(),
611 TIsSameIObject<SALOME_Actor>(theIObject));
613 return 1.0 - anActor->GetOpacity();
619 Sets current transparency
620 \param theIObject - object
621 \param theTrans - new transparency
625 ::SetTransparency(const Handle(SALOME_InteractiveObject)& theIObject,
628 vtkFloatingPointType anOpacity = 1.0 - theTrans;
629 using namespace SVTK;
630 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
631 TIsSameIObject<SALOME_Actor>(theIObject),
632 TSetFunction<SALOME_Actor,vtkFloatingPointType>
633 (&SALOME_Actor::SetOpacity,anOpacity));
638 \param theIObject - object
639 \param theColor - new color
643 ::SetColor(const Handle(SALOME_InteractiveObject)& theIObject,
644 const QColor& theColor)
646 vtkFloatingPointType aColor[3] = {theColor.red()/255., theColor.green()/255., theColor.blue()/255.};
648 using namespace SVTK;
649 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
650 TIsSameIObject<SALOME_Actor>(theIObject),
651 TSetFunction<SALOME_Actor,const vtkFloatingPointType*>
652 (&SALOME_Actor::SetColor,aColor));
658 \param theIObject - object
662 ::GetColor(const Handle(SALOME_InteractiveObject)& theIObject)
664 using namespace SVTK;
665 SALOME_Actor* anActor =
666 Find<SALOME_Actor>(getRenderer()->GetActors(),
667 TIsSameIObject<SALOME_Actor>(theIObject));
669 vtkFloatingPointType r,g,b;
670 anActor->GetColor(r,g,b);
671 return QColor(int(r*255),int(g*255),int(b*255));
674 return QColor(0,0,0);