1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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, or (at your option) any later version.
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
23 // SALOME VTKViewer : build VTK viewer into Salome desktop
27 #include "SALOME_Actor.h"
29 #include "SVTK_View.h"
30 #include "SVTK_Renderer.h"
31 #include "SVTK_ViewWindow.h"
32 #include "SVTK_RenderWindowInteractor.h"
33 #include "SALOME_ListIO.hxx"
35 #include "VTKViewer_Algorithm.h"
36 #include "SVTK_Functor.h"
38 #include <vtkActorCollection.h>
39 #include <vtkRenderer.h>
40 #include <vtkProperty.h>
46 ::SVTK_SignalHandler(SVTK_ViewWindow* 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()));
74 ::~SVTK_SignalHandler()
79 \return corresponding svtk main window
90 Redirect the request to #SVTK_ViewWindow::Repaint (just for flexibility)
94 ::Repaint(bool theUpdateTrihedron)
96 myMainWindow->Repaint(theUpdateTrihedron);
100 Redirect the request to #SVTK_ViewWindow::GetRenderer (just for flexibility)
106 return myMainWindow->GetRenderer();
110 Redirect the request to #SVTK_ViewWindow::getRenderer (just for flexibility)
116 return myMainWindow->getRenderer();
121 struct THighlightAction
124 THighlightAction( bool theIsHighlight ):
125 myIsHighlight( theIsHighlight )
129 operator()( SALOME_Actor* theActor)
131 if(theActor->GetMapper() && theActor->hasIO()){
132 theActor->Highlight( myIsHighlight );
139 SLOT: called on selection change
143 ::onSelectionChanged()
145 VTK::ActorCollectionCopy aCopy(myMainWindow->getRenderer()->GetActors());
146 vtkActorCollection* anActors = aCopy.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_ViewWindow* theMainWindow) :
168 SVTK_SignalHandler(theMainWindow)
181 Unhilights all objects in viewer
187 using namespace SVTK;
188 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
189 ForEach<SALOME_Actor>(aCopy.GetActors(),
190 THighlightAction( false ));
195 Hilights/unhilights object in viewer
196 \param theIO - object to be updated
197 \param theIsHighlight - if it is true, object will be hilighted, otherwise it will be unhilighted
198 \param theIsUpdate - update current viewer
202 ::highlight( const Handle(SALOME_InteractiveObject)& theIO,
206 using namespace SVTK;
207 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
208 ForEachIf<SALOME_Actor>(aCopy.GetActors(),
209 TIsSameIObject<SALOME_Actor>( theIO ),
210 THighlightAction(theIsHighlight));
216 \param theEntry - entry that corresponds to intractive objects
218 Handle(SALOME_InteractiveObject)
220 ::FindIObject(const char* theEntry)
222 using namespace SVTK;
223 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
224 SALOME_Actor* anActor =
225 Find<SALOME_Actor>(aCopy.GetActors(),
226 TIsSameEntry<SALOME_Actor>(theEntry));
228 return anActor->getIO();
234 Redirect the request to #SVTK_Renderer::SetPreselectionProp
238 ::SetSelectionProp(const double& theRed,
239 const double& theGreen,
240 const double& theBlue,
243 GetRenderer()->SetSelectionProp(theRed,theGreen,theBlue,theWidth);
247 Redirect the request to #SVTK_Renderer::SetPreselectionProp
251 ::SetPreselectionProp(const double& theRed,
252 const double& theGreen,
253 const double& theBlue,
256 GetRenderer()->SetPreselectionProp(theRed,theGreen,theBlue,theWidth);
260 Redirect the request to #SVTK_Renderer::SetPreselectionProp
264 ::SetSelectionTolerance(const double& theTolNodes,
265 const double& theTolCell,
266 const double& theTolObjects)
268 GetRenderer()->SetSelectionTolerance(theTolNodes, theTolCell, theTolObjects);
272 \return true if object is in viewer or in collector
273 \param theIO - object to be checked
277 ::isInViewer(const Handle(SALOME_InteractiveObject)& theIObject)
279 using namespace SVTK;
280 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
281 SALOME_Actor* anActor =
282 Find<SALOME_Actor>(aCopy.GetActors(),
283 TIsSameIObject<SALOME_Actor>(theIObject));
284 return anActor != NULL;
289 struct TIsActorVisibleAction
292 TIsActorVisibleAction(bool& theResult):
298 operator()(SALOME_Actor* theActor)
301 myResult = theActor->GetVisibility();
307 \return true if object is displayed in viewer
308 \param theIO - object to be checked
312 ::isVisible(const Handle(SALOME_InteractiveObject)& theIObject)
314 using namespace SVTK;
316 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
317 ForEachIf<SALOME_Actor>(aCopy.GetActors(),
318 TIsSameIObject<SALOME_Actor>(theIObject),
319 TIsActorVisibleAction(aResult));
324 Changes name of object
325 \param theIObject - object to be renamed
326 \param theName - new name
330 ::rename(const Handle(SALOME_InteractiveObject)& theIObject,
331 const QString& theName)
333 using namespace SVTK;
334 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
335 ForEachIf<SALOME_Actor>(aCopy.GetActors(),
336 TIsSameIObject<SALOME_Actor>(theIObject),
337 TSetFunction<SALOME_Actor,const char*,const char*>
338 (&SALOME_Actor::setName,theName.toLatin1().data()));
342 \return current display mode (obsolete)
348 return myDisplayMode;
352 Set current display mode
353 \param theMode - new display mode
357 ::SetDisplayMode(int theMode)
360 ChangeRepresentationToWireframe();
361 else if (theMode == 1)
362 ChangeRepresentationToSurface();
363 else if (theMode == 2) {
364 ChangeRepresentationToSurfaceWithEdges();
367 myDisplayMode = theMode;
371 Set current display mode
372 \param theIObject - object
373 \param theMode - new display mode
377 ::SetDisplayMode(const Handle(SALOME_InteractiveObject)& theIObject,
380 using namespace SVTK;
381 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
382 ForEachIf<SALOME_Actor>(aCopy.GetActors(),
383 TIsSameIObject<SALOME_Actor>(theIObject),
384 TSetFunction<SALOME_Actor,int>
385 (&SALOME_Actor::setDisplayMode,theMode));
389 Change all actors to wireframe
393 ::ChangeRepresentationToWireframe()
395 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
396 ChangeRepresentationToWireframe(aCopy.GetActors());
400 Change all actors to shading
404 ::ChangeRepresentationToSurface()
406 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
407 ChangeRepresentationToSurface(aCopy.GetActors());
411 Change all actors to shading with edges
415 ::ChangeRepresentationToSurfaceWithEdges()
417 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
418 ChangeRepresentationToSurfaceWithEdges(aCopy.GetActors());
422 Change to wireframe a list of vtkactor
423 theCollection - list of vtkactor
427 ::ChangeRepresentationToWireframe(vtkActorCollection* theCollection)
429 using namespace SVTK;
430 ForEach<SALOME_Actor>(theCollection,
431 TSetFunction<SALOME_Actor,int>
432 (&SALOME_Actor::setDisplayMode,0));
437 Change to shading a list of vtkactor
438 theCollection - list of vtkactor
442 ::ChangeRepresentationToSurface(vtkActorCollection* theCollection)
444 using namespace SVTK;
445 ForEach<SALOME_Actor>(theCollection,
446 TSetFunction<SALOME_Actor,int>
447 (&SALOME_Actor::setDisplayMode,1));
452 Change to shading with edges a list of vtkactor
453 theCollection - list of vtkactor
457 ::ChangeRepresentationToSurfaceWithEdges(vtkActorCollection* theCollection)
459 using namespace SVTK;
460 ForEach<SALOME_Actor>(theCollection,
461 TSetFunction<SALOME_Actor,int>
462 (&SALOME_Actor::setDisplayMode,3));
470 VTK::TSetFunction<vtkActor,int> mySetFunction;
472 mySetFunction(&vtkActor::SetVisibility,false)
475 operator()(SALOME_Actor* theActor)
477 theActor->SetVisibility(false);
478 // Erase dependent actors
479 vtkActorCollection* aCollection = vtkActorCollection::New();
480 theActor->GetChildActors(aCollection);
481 VTK::ForEach<vtkActor>(aCollection,mySetFunction);
482 aCollection->Delete();
488 To erase all existing VTK presentations
494 using namespace SVTK;
495 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
496 ForEach<SALOME_Actor>(aCopy.GetActors(),
502 To display all existing VTK presentations
508 using namespace SVTK;
509 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
510 ForEach<SALOME_Actor>(aCopy.GetActors(),
511 TSetVisibility<SALOME_Actor>(true));
516 To erase VTK presentation
517 \param theActor - actor
518 \param theIsUpdate - updates current viewer
522 ::Erase(SALOME_Actor* theActor,
525 SVTK::TErase()(theActor);
533 To erase VTK presentation
534 \param theIObject - object
535 \param theIsUpdate - updates current viewer
539 ::Erase(const Handle(SALOME_InteractiveObject)& theIObject,
542 using namespace SVTK;
543 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
544 ForEachIf<SALOME_Actor>(aCopy.GetActors(),
545 TIsSameIObject<SALOME_Actor>(theIObject),
552 To display the VTK presentation
556 ::Display(SALOME_Actor* theActor,
559 GetRenderer()->AddActor(theActor);
560 theActor->SetVisibility(true);
567 To display the VTK presentation
571 ::Display(const Handle(SALOME_InteractiveObject)& theIObject,
574 using namespace SVTK;
575 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
576 ForEachIf<SALOME_Actor>(aCopy.GetActors(),
577 TIsSameIObject<SALOME_Actor>(theIObject),
578 TSetVisibility<SALOME_Actor>(true));
585 To display VTK presentation with defined #SALOME_InteractiveObject and erase all others
589 ::DisplayOnly(const Handle(SALOME_InteractiveObject)& theIObject)
600 SVTK_Renderer* myRenderer;
601 TRemoveAction(SVTK_Renderer* theRenderer):
602 myRenderer(theRenderer)
605 operator()(SALOME_Actor* theActor)
607 myRenderer->RemoveActor(theActor);
613 To remove the VTK presentation
617 ::Remove(const Handle(SALOME_InteractiveObject)& theIObject,
620 using namespace SVTK;
621 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
622 ForEachIf<SALOME_Actor>(aCopy.GetActors(),
623 TIsSameIObject<SALOME_Actor>(theIObject),
624 TRemoveAction(GetRenderer()));
630 To remove the VTK presentation
634 ::Remove(SALOME_Actor* theActor,
637 GetRenderer()->RemoveActor(theActor);
643 To remove all VTK presentations
647 ::RemoveAll(bool theIsUpdate)
649 vtkRenderer* aRenderer = getRenderer();
650 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
651 if(vtkActorCollection* anActors = aCopy.GetActors()){
652 anActors->InitTraversal();
653 while(vtkActor *anAct = anActors->GetNextActor()){
654 if(SALOME_Actor* aSAct = SALOME_Actor::SafeDownCast(anAct)){
655 if(aSAct->hasIO() && aSAct->getIO()->hasEntry())
656 aRenderer->RemoveActor( anAct );
666 \return current transparency
667 \param theIObject - object
671 ::GetTransparency(const Handle(SALOME_InteractiveObject)& theIObject)
673 using namespace SVTK;
674 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
675 SALOME_Actor* anActor =
676 Find<SALOME_Actor>(aCopy.GetActors(),
677 TIsSameIObject<SALOME_Actor>(theIObject));
679 return 1.0 - anActor->GetOpacity();
685 Sets current transparency
686 \param theIObject - object
687 \param theTrans - new transparency
691 ::SetTransparency(const Handle(SALOME_InteractiveObject)& theIObject,
694 double anOpacity = 1.0 - theTrans;
695 using namespace SVTK;
696 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
697 ForEachIf<SALOME_Actor>(aCopy.GetActors(),
698 TIsSameIObject<SALOME_Actor>(theIObject),
699 TSetFunction<SALOME_Actor,double>
700 (&SALOME_Actor::SetOpacity,anOpacity));
705 \param theIObject - object
706 \param theColor - new color
710 ::SetColor(const Handle(SALOME_InteractiveObject)& theIObject,
711 const QColor& theColor)
713 double aColor[3] = {theColor.red()/255., theColor.green()/255., theColor.blue()/255.};
715 using namespace SVTK;
716 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
717 ForEachIf<SALOME_Actor>(aCopy.GetActors(),
718 TIsSameIObject<SALOME_Actor>(theIObject),
719 TSetFunction<SALOME_Actor,const double*>
720 (&SALOME_Actor::SetColor,aColor));
726 \param theIObject - object
730 ::GetColor(const Handle(SALOME_InteractiveObject)& theIObject)
732 using namespace SVTK;
733 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
734 SALOME_Actor* anActor =
735 Find<SALOME_Actor>(aCopy.GetActors(),
736 TIsSameIObject<SALOME_Actor>(theIObject));
739 anActor->GetColor(r,g,b);
740 return QColor(int(r*255),int(g*255),int(b*255));
743 return QColor(0,0,0);
748 \param theIObject - object
749 \param thePropF - property contained new properties of front material
750 \param thePropB - property contained new properties of back material
754 ::SetMaterial(const Handle(SALOME_InteractiveObject)& theIObject,
755 vtkProperty* thePropF, vtkProperty* thePropB)
757 using namespace SVTK;
758 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
759 std::vector<vtkProperty*> aProps;
760 aProps.push_back( thePropF );
761 aProps.push_back( thePropB );
762 ForEachIf<SALOME_Actor>(aCopy.GetActors(),
763 TIsSameIObject<SALOME_Actor>(theIObject),
764 TSetFunction<SALOME_Actor,std::vector<vtkProperty*> >
765 (&SALOME_Actor::SetMaterial,aProps));
769 Get current front material
\r
770 \param theIObject - object
\r
771 \return property contained front material properties of the given object
\r
775 ::GetFrontMaterial(const Handle(SALOME_InteractiveObject)& theIObject)
\r
777 using namespace SVTK;
\r
778 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
\r
779 SALOME_Actor* anActor =
\r
780 Find<SALOME_Actor>(aCopy.GetActors(),
\r
781 TIsSameIObject<SALOME_Actor>(theIObject));
\r
783 return anActor->GetFrontMaterial();
\r
788 Get current back material
\r
789 \param theIObject - object
\r
790 \return property contained back material properties of the given object
\r
794 ::GetBackMaterial(const Handle(SALOME_InteractiveObject)& theIObject)
\r
796 using namespace SVTK;
\r
797 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
\r
798 SALOME_Actor* anActor =
\r
799 Find<SALOME_Actor>(aCopy.GetActors(),
\r
800 TIsSameIObject<SALOME_Actor>(theIObject));
\r
802 return anActor->GetBackMaterial();
807 \Collect objects visible in viewer
808 \param theList - visible objects collection
810 void SVTK_View::GetVisible( SALOME_ListIO& theList )
812 using namespace SVTK;
813 VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
814 ForEach<SALOME_Actor>(aCopy.GetActors(),
815 TCollectIfVisible<SALOME_Actor>(theList));