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>
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)
264 GetRenderer()->SetSelectionTolerance(theTolNodes,theTolCell);
268 \return true if object is in viewer or in collector
269 \param theIO - object to be checked
273 ::isInViewer(const Handle(SALOME_InteractiveObject)& theIObject)
275 using namespace SVTK;
276 SALOME_Actor* anActor =
277 Find<SALOME_Actor>(getRenderer()->GetActors(),
278 TIsSameIObject<SALOME_Actor>(theIObject));
279 return anActor != NULL;
283 \return true if object is displayed in viewer
284 \param theIO - object to be checked
288 ::isVisible(const Handle(SALOME_InteractiveObject)& theIObject)
290 using namespace SVTK;
291 SALOME_Actor* anActor =
292 Find<SALOME_Actor>(getRenderer()->GetActors(),
293 TIsSameIObject<SALOME_Actor>(theIObject));
294 return anActor != NULL && anActor->GetVisibility();
298 Changes name of object
299 \param theIObject - object to be renamed
300 \param theName - new name
304 ::rename(const Handle(SALOME_InteractiveObject)& theIObject,
305 const QString& theName)
307 using namespace SVTK;
308 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
309 TIsSameIObject<SALOME_Actor>(theIObject),
310 TSetFunction<SALOME_Actor,const char*,QString>
311 (&SALOME_Actor::setName,theName.latin1()));
315 \return current display mode (obsolete)
321 return myDisplayMode;
325 Set current display mode
326 \param theMode - new display mode
330 ::SetDisplayMode(int theMode)
333 ChangeRepresentationToWireframe();
335 ChangeRepresentationToSurface();
336 myDisplayMode = theMode;
340 Set current display mode
341 \param theIObject - object
342 \param theMode - new display mode
346 ::SetDisplayMode(const Handle(SALOME_InteractiveObject)& theIObject,
349 using namespace SVTK;
350 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
351 TIsSameIObject<SALOME_Actor>(theIObject),
352 TSetFunction<SALOME_Actor,int>
353 (&SALOME_Actor::setDisplayMode,theMode));
357 Change all actors to wireframe
361 ::ChangeRepresentationToWireframe()
363 ChangeRepresentationToWireframe(getRenderer()->GetActors());
367 Change all actors to shading
371 ::ChangeRepresentationToSurface()
373 ChangeRepresentationToSurface(getRenderer()->GetActors());
377 Change to wireframe a list of vtkactor
378 theCollection - list of vtkactor
382 ::ChangeRepresentationToWireframe(vtkActorCollection* theCollection)
384 using namespace SVTK;
385 ForEach<SALOME_Actor>(theCollection,
386 TSetFunction<SALOME_Actor,int>
387 (&SALOME_Actor::setDisplayMode,0));
392 Change to shading a list of vtkactor
393 theCollection - list of vtkactor
397 ::ChangeRepresentationToSurface(vtkActorCollection* theCollection)
399 using namespace SVTK;
400 ForEach<SALOME_Actor>(theCollection,
401 TSetFunction<SALOME_Actor,int>
402 (&SALOME_Actor::setDisplayMode,1));
410 VTK::TSetFunction<vtkActor,int> mySetFunction;
412 mySetFunction(&vtkActor::SetVisibility,false)
415 operator()(SALOME_Actor* theActor)
417 theActor->SetVisibility(false);
418 // Erase dependent actors
419 vtkActorCollection* aCollection = vtkActorCollection::New();
420 theActor->GetChildActors(aCollection);
421 VTK::ForEach<vtkActor>(aCollection,mySetFunction);
422 aCollection->Delete();
428 To erase all existing VTK presentations
434 using namespace SVTK;
435 ForEach<SALOME_Actor>(getRenderer()->GetActors(),
441 To display all existing VTK presentations
447 using namespace SVTK;
448 ForEach<SALOME_Actor>(getRenderer()->GetActors(),
449 TSetVisibility<SALOME_Actor>(true));
454 To erase VTK presentation
455 \param theActor - actor
456 \param theIsUpdate - updates current viewer
460 ::Erase(SALOME_Actor* theActor,
463 SVTK::TErase()(theActor);
471 To erase VTK presentation
472 \param theIObject - object
473 \param theIsUpdate - updates current viewer
477 ::Erase(const Handle(SALOME_InteractiveObject)& theIObject,
480 using namespace SVTK;
481 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
482 TIsSameIObject<SALOME_Actor>(theIObject),
489 To display the VTK presentation
493 ::Display(SALOME_Actor* theActor,
496 GetRenderer()->AddActor(theActor);
497 theActor->SetVisibility(true);
504 To display the VTK presentation
508 ::Display(const Handle(SALOME_InteractiveObject)& theIObject,
511 using namespace SVTK;
512 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
513 TIsSameIObject<SALOME_Actor>(theIObject),
514 TSetVisibility<SALOME_Actor>(true));
521 To display VTK presentation with defined #SALOME_InteractiveObject and erase all others
525 ::DisplayOnly(const Handle(SALOME_InteractiveObject)& theIObject)
536 SVTK_Renderer* myRenderer;
537 TRemoveAction(SVTK_Renderer* theRenderer):
538 myRenderer(theRenderer)
541 operator()(SALOME_Actor* theActor)
543 myRenderer->RemoveActor(theActor);
549 To remove the VTK presentation
553 ::Remove(const Handle(SALOME_InteractiveObject)& theIObject,
556 using namespace SVTK;
557 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
558 TIsSameIObject<SALOME_Actor>(theIObject),
559 TRemoveAction(GetRenderer()));
565 To remove the VTK presentation
569 ::Remove(SALOME_Actor* theActor,
572 GetRenderer()->RemoveActor(theActor);
578 To remove all VTK presentations
582 ::RemoveAll(bool theIsUpdate)
584 vtkRenderer* aRenderer = getRenderer();
585 if(vtkActorCollection* anActors = aRenderer->GetActors()){
586 anActors->InitTraversal();
587 while(vtkActor *anAct = anActors->GetNextActor()){
588 if(SALOME_Actor* aSAct = SALOME_Actor::SafeDownCast(anAct)){
589 if(aSAct->hasIO() && aSAct->getIO()->hasEntry())
590 aRenderer->RemoveActor( anAct );
600 \return current transparency
601 \param theIObject - object
605 ::GetTransparency(const Handle(SALOME_InteractiveObject)& theIObject)
607 using namespace SVTK;
608 SALOME_Actor* anActor =
609 Find<SALOME_Actor>(getRenderer()->GetActors(),
610 TIsSameIObject<SALOME_Actor>(theIObject));
612 return 1.0 - anActor->GetOpacity();
618 Sets current transparency
619 \param theIObject - object
620 \param theTrans - new transparency
624 ::SetTransparency(const Handle(SALOME_InteractiveObject)& theIObject,
627 vtkFloatingPointType anOpacity = 1.0 - theTrans;
628 using namespace SVTK;
629 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
630 TIsSameIObject<SALOME_Actor>(theIObject),
631 TSetFunction<SALOME_Actor,vtkFloatingPointType>
632 (&SALOME_Actor::SetOpacity,anOpacity));
637 \param theIObject - object
638 \param theColor - new color
642 ::SetColor(const Handle(SALOME_InteractiveObject)& theIObject,
643 const QColor& theColor)
645 vtkFloatingPointType aColor[3] = {theColor.red()/255., theColor.green()/255., theColor.blue()/255.};
647 using namespace SVTK;
648 ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
649 TIsSameIObject<SALOME_Actor>(theIObject),
650 TSetFunction<SALOME_Actor,const vtkFloatingPointType*>
651 (&SALOME_Actor::SetColor,aColor));
657 \param theIObject - object
661 ::GetColor(const Handle(SALOME_InteractiveObject)& theIObject)
663 using namespace SVTK;
664 SALOME_Actor* anActor =
665 Find<SALOME_Actor>(getRenderer()->GetActors(),
666 TIsSameIObject<SALOME_Actor>(theIObject));
668 vtkFloatingPointType r,g,b;
669 anActor->GetColor(r,g,b);
670 return QColor(int(r*255),int(g*255),int(b*255));
673 return QColor(0,0,0);