Back to index

salome-gui  6.5.0
SVTK_View.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME VTKViewer : build VTK viewer into Salome desktop
00024 //  File   : 
00025 //  Author : 
00026 
00027 #include "SALOME_Actor.h"
00028 
00029 #include "SVTK_View.h"
00030 #include "SVTK_Renderer.h"
00031 #include "SVTK_ViewWindow.h"
00032 #include "SVTK_RenderWindowInteractor.h"
00033 #include "SALOME_ListIteratorOfListIO.hxx"
00034 
00035 #include "VTKViewer_Algorithm.h"
00036 #include "SVTK_Functor.h"
00037 
00038 #include <vtkActorCollection.h>
00039 #include <vtkRenderer.h>
00040 #include <vtkProperty.h>
00041 
00045 SVTK_SignalHandler
00046 ::SVTK_SignalHandler(SVTK_ViewWindow* theMainWindow):
00047   QObject(theMainWindow),
00048   myMainWindow(theMainWindow)
00049 {
00050   SVTK_RenderWindowInteractor* anInteractor = theMainWindow->GetInteractor();
00051 
00052   connect(anInteractor,SIGNAL(KeyPressed(QKeyEvent*)),
00053           this,SIGNAL(KeyPressed(QKeyEvent*)) );
00054   connect(anInteractor,SIGNAL(KeyReleased(QKeyEvent*)),
00055           this,SIGNAL(KeyReleased(QKeyEvent*)));
00056   connect(anInteractor,SIGNAL(MouseButtonPressed(QMouseEvent*)),
00057           this,SIGNAL(MouseButtonPressed(QMouseEvent*)));
00058   connect(anInteractor,SIGNAL(MouseButtonReleased(QMouseEvent*)),
00059           this,SIGNAL(MouseButtonReleased(QMouseEvent*)));
00060   connect(anInteractor,SIGNAL(MouseDoubleClicked(QMouseEvent*)),
00061           this,SIGNAL(MouseDoubleClicked(QMouseEvent*)));
00062   connect(anInteractor,SIGNAL(MouseMove(QMouseEvent*)),
00063           this,SIGNAL(MouseMove(QMouseEvent*)));
00064   connect(anInteractor,SIGNAL(contextMenuRequested(QContextMenuEvent*)),
00065           this,SIGNAL(contextMenuRequested(QContextMenuEvent*)));
00066   connect(anInteractor,SIGNAL(selectionChanged()),
00067           this,SIGNAL(selectionChanged()));
00068 }
00069 
00073 SVTK_SignalHandler
00074 ::~SVTK_SignalHandler()
00075 {
00076 }
00077 
00081 SVTK_ViewWindow*
00082 SVTK_SignalHandler
00083 ::GetMainWindow()
00084 {
00085   return myMainWindow;
00086 }
00087 
00088 
00092 void
00093 SVTK_SignalHandler
00094 ::Repaint(bool theUpdateTrihedron)
00095 {
00096   myMainWindow->Repaint(theUpdateTrihedron);
00097 }
00098 
00102 SVTK_Renderer* 
00103 SVTK_SignalHandler
00104 ::GetRenderer()
00105 {
00106   return myMainWindow->GetRenderer();
00107 }
00108 
00112 vtkRenderer* 
00113 SVTK_SignalHandler
00114 ::getRenderer()
00115 {
00116   return myMainWindow->getRenderer();
00117 }
00118 
00119 namespace SVTK
00120 {
00121   struct THighlightAction
00122   {
00123     bool myIsHighlight;
00124     THighlightAction( bool theIsHighlight ):
00125       myIsHighlight( theIsHighlight )
00126     {}
00127     
00128     void
00129     operator()( SALOME_Actor* theActor) 
00130     {
00131       if(theActor->GetMapper() && theActor->hasIO()){
00132         theActor->Highlight( myIsHighlight );
00133       }
00134     }
00135   };
00136 }
00137 
00141 void
00142 SVTK_SignalHandler
00143 ::onSelectionChanged()
00144 {
00145   VTK::ActorCollectionCopy aCopy(myMainWindow->getRenderer()->GetActors());
00146   vtkActorCollection* anActors = aCopy.GetActors();
00147 
00148   using namespace SVTK;
00149   ForEach<SALOME_Actor>(anActors,
00150                         THighlightAction( false ));
00151   SVTK_Selector* aSelector = myMainWindow->GetSelector();
00152   const SALOME_ListIO& aListIO = aSelector->StoredIObjects();
00153   SALOME_ListIteratorOfListIO anIter(aListIO);
00154   for(; anIter.More(); anIter.Next()){
00155     ForEachIf<SALOME_Actor>(anActors,
00156                             TIsSameIObject<SALOME_Actor>(anIter.Value()),
00157                             THighlightAction(true));
00158   }
00159 
00160   myMainWindow->Repaint(false);
00161 }
00162 
00166 SVTK_View
00167 ::SVTK_View(SVTK_ViewWindow* theMainWindow) :
00168   SVTK_SignalHandler(theMainWindow)
00169 {
00170 }
00171 
00175 SVTK_View
00176 ::~SVTK_View()
00177 {
00178 }
00179 
00183 void 
00184 SVTK_View
00185 ::unHighlightAll() 
00186 {
00187   using namespace SVTK;
00188   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00189   ForEach<SALOME_Actor>(aCopy.GetActors(),
00190                         THighlightAction( false ));
00191   Repaint();
00192 }
00193 
00200 void
00201 SVTK_View
00202 ::highlight( const Handle(SALOME_InteractiveObject)& theIO, 
00203              bool theIsHighlight, 
00204              bool theIsUpdate ) 
00205 {
00206   using namespace SVTK;
00207   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00208   ForEachIf<SALOME_Actor>(aCopy.GetActors(),
00209                           TIsSameIObject<SALOME_Actor>( theIO ),
00210                           THighlightAction(theIsHighlight));
00211   Repaint();
00212 }
00213 
00218 Handle(SALOME_InteractiveObject)
00219 SVTK_View
00220 ::FindIObject(const char* theEntry) 
00221 {
00222   using namespace SVTK;
00223   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00224   SALOME_Actor* anActor = 
00225     Find<SALOME_Actor>(aCopy.GetActors(),
00226                        TIsSameEntry<SALOME_Actor>(theEntry));
00227   if(anActor != NULL)
00228     return anActor->getIO();
00229  
00230   return NULL;
00231 }
00232 
00236 void
00237 SVTK_View
00238 ::SetSelectionProp(const double& theRed, 
00239                    const double& theGreen, 
00240                    const double& theBlue, 
00241                    const int& theWidth) 
00242 {
00243   GetRenderer()->SetSelectionProp(theRed,theGreen,theBlue,theWidth);
00244 }
00245 
00249 void
00250 SVTK_View
00251 ::SetPreselectionProp(const double& theRed, 
00252                       const double& theGreen, 
00253                       const double& theBlue, 
00254                       const int& theWidth) 
00255 {
00256   GetRenderer()->SetPreselectionProp(theRed,theGreen,theBlue,theWidth);
00257 }
00258 
00262 void
00263 SVTK_View
00264 ::SetSelectionTolerance(const double& theTolNodes, 
00265                         const double& theTolCell,
00266                         const double& theTolObjects)
00267 {
00268   GetRenderer()->SetSelectionTolerance(theTolNodes, theTolCell, theTolObjects);
00269 }
00270 
00275 bool
00276 SVTK_View
00277 ::isInViewer(const Handle(SALOME_InteractiveObject)& theIObject)
00278 {
00279   using namespace SVTK;
00280   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00281   SALOME_Actor* anActor = 
00282     Find<SALOME_Actor>(aCopy.GetActors(),
00283                        TIsSameIObject<SALOME_Actor>(theIObject));
00284   return anActor != NULL;
00285 }
00286 
00287 namespace SVTK
00288 {
00289   struct TIsActorVisibleAction
00290   {
00291     bool& myResult;
00292     TIsActorVisibleAction(bool& theResult): 
00293       myResult(theResult)
00294     {
00295       myResult = false;
00296     }
00297     void
00298     operator()(SALOME_Actor* theActor)
00299     {
00300       if( !myResult )
00301         myResult = theActor->GetVisibility();
00302     }
00303   };
00304 }
00305 
00310 bool
00311 SVTK_View
00312 ::isVisible(const Handle(SALOME_InteractiveObject)& theIObject)
00313 {
00314   using namespace SVTK;
00315   bool aResult;
00316   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00317   ForEachIf<SALOME_Actor>(aCopy.GetActors(),
00318                           TIsSameIObject<SALOME_Actor>(theIObject),
00319                           TIsActorVisibleAction(aResult));
00320   return aResult;
00321 }
00322 
00328 void
00329 SVTK_View
00330 ::rename(const Handle(SALOME_InteractiveObject)& theIObject, 
00331          const QString& theName)
00332 {
00333   using namespace SVTK;
00334   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00335   ForEachIf<SALOME_Actor>(aCopy.GetActors(),
00336                           TIsSameIObject<SALOME_Actor>(theIObject),
00337                           TSetFunction<SALOME_Actor,const char*,const char*>
00338                           (&SALOME_Actor::setName,theName.toLatin1().data()));
00339 }
00340 
00344 int
00345 SVTK_View
00346 ::GetDisplayMode() 
00347 {
00348   return myDisplayMode; 
00349 }
00350 
00355 void
00356 SVTK_View
00357 ::SetDisplayMode(int theMode)
00358 {
00359   if(theMode == 0) 
00360     ChangeRepresentationToWireframe();
00361   else if (theMode == 1)
00362     ChangeRepresentationToSurface();
00363   else if (theMode == 2) {
00364     ChangeRepresentationToSurfaceWithEdges();
00365     theMode++;
00366   }
00367   myDisplayMode = theMode;
00368 }
00369 
00375 void
00376 SVTK_View
00377 ::SetDisplayMode(const Handle(SALOME_InteractiveObject)& theIObject, 
00378                  int theMode)
00379 {
00380   using namespace SVTK;
00381   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00382   ForEachIf<SALOME_Actor>(aCopy.GetActors(),
00383                           TIsSameIObject<SALOME_Actor>(theIObject),
00384                           TSetFunction<SALOME_Actor,int>
00385                           (&SALOME_Actor::setDisplayMode,theMode));
00386 }
00387 
00391 void
00392 SVTK_View
00393 ::ChangeRepresentationToWireframe()
00394 {
00395   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00396   ChangeRepresentationToWireframe(aCopy.GetActors());
00397 }
00398 
00402 void
00403 SVTK_View
00404 ::ChangeRepresentationToSurface()
00405 {
00406   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00407   ChangeRepresentationToSurface(aCopy.GetActors());
00408 }
00409 
00413 void
00414 SVTK_View
00415 ::ChangeRepresentationToSurfaceWithEdges()
00416 {
00417   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00418   ChangeRepresentationToSurfaceWithEdges(aCopy.GetActors());
00419 }
00420 
00425 void
00426 SVTK_View
00427 ::ChangeRepresentationToWireframe(vtkActorCollection* theCollection)
00428 {
00429   using namespace SVTK;
00430   ForEach<SALOME_Actor>(theCollection,
00431                         TSetFunction<SALOME_Actor,int>
00432                         (&SALOME_Actor::setDisplayMode,0));
00433   Repaint();
00434 }
00435 
00440 void
00441 SVTK_View
00442 ::ChangeRepresentationToSurface(vtkActorCollection* theCollection)
00443 {
00444   using namespace SVTK;
00445   ForEach<SALOME_Actor>(theCollection,
00446                         TSetFunction<SALOME_Actor,int>
00447                         (&SALOME_Actor::setDisplayMode,1));
00448   Repaint();
00449 }
00450 
00455 void
00456 SVTK_View
00457 ::ChangeRepresentationToSurfaceWithEdges(vtkActorCollection* theCollection)
00458 {
00459   using namespace SVTK;
00460   ForEach<SALOME_Actor>(theCollection,
00461                         TSetFunction<SALOME_Actor,int>
00462                         (&SALOME_Actor::setDisplayMode,3));
00463   Repaint();
00464 }
00465 
00466 namespace SVTK
00467 {
00468   struct TErase
00469   {
00470     VTK::TSetFunction<vtkActor,int> mySetFunction;
00471     TErase():
00472       mySetFunction(&vtkActor::SetVisibility,false)
00473     {}
00474     void
00475     operator()(SALOME_Actor* theActor)
00476     {
00477       theActor->SetVisibility(false);
00478       // Erase dependent actors
00479       vtkActorCollection* aCollection = vtkActorCollection::New(); 
00480       theActor->GetChildActors(aCollection);
00481       VTK::ForEach<vtkActor>(aCollection,mySetFunction);
00482       aCollection->Delete();
00483     }
00484   };
00485 }
00486 
00490 void
00491 SVTK_View
00492 ::EraseAll()
00493 {   
00494   using namespace SVTK;
00495   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00496   ForEach<SALOME_Actor>(aCopy.GetActors(),
00497                         TErase());
00498   Repaint();
00499 }
00500 
00504 void
00505 SVTK_View
00506 ::DisplayAll()
00507 { 
00508   using namespace SVTK;
00509   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00510   ForEach<SALOME_Actor>(aCopy.GetActors(),
00511                         TSetVisibility<SALOME_Actor>(true));
00512   Repaint();
00513 }
00514 
00520 void
00521 SVTK_View
00522 ::Erase(SALOME_Actor* theActor, 
00523         bool theIsUpdate)
00524 {
00525   SVTK::TErase()(theActor);
00526 
00527   if(theIsUpdate)
00528     Repaint();
00529 }
00530 
00531 
00537 void
00538 SVTK_View
00539 ::Erase(const Handle(SALOME_InteractiveObject)& theIObject, 
00540         bool theIsUpdate)
00541 {
00542   using namespace SVTK;
00543   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00544   ForEachIf<SALOME_Actor>(aCopy.GetActors(),
00545                           TIsSameIObject<SALOME_Actor>(theIObject),
00546                           TErase());
00547   if(theIsUpdate)
00548     Repaint();
00549 }
00550 
00554 void
00555 SVTK_View
00556 ::Display(SALOME_Actor* theActor, 
00557           bool theIsUpdate)
00558 {
00559   GetRenderer()->AddActor(theActor);
00560   theActor->SetVisibility(true);
00561 
00562   if(theIsUpdate)
00563     Repaint();
00564 }
00565 
00569 void
00570 SVTK_View
00571 ::Display(const Handle(SALOME_InteractiveObject)& theIObject, 
00572           bool theIsUpdate)
00573 {
00574   using namespace SVTK;
00575   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00576   ForEachIf<SALOME_Actor>(aCopy.GetActors(),
00577                           TIsSameIObject<SALOME_Actor>(theIObject),
00578                           TSetVisibility<SALOME_Actor>(true));
00579 
00580   if(theIsUpdate)
00581     Repaint();
00582 }
00583 
00587 void
00588 SVTK_View
00589 ::DisplayOnly(const Handle(SALOME_InteractiveObject)& theIObject)
00590 {
00591   EraseAll();
00592   Display(theIObject);
00593 }
00594 
00595 
00596 namespace SVTK
00597 {
00598   struct TRemoveAction
00599   {
00600     SVTK_Renderer* myRenderer;
00601     TRemoveAction(SVTK_Renderer* theRenderer): 
00602       myRenderer(theRenderer)
00603     {}
00604     void
00605     operator()(SALOME_Actor* theActor)
00606     {
00607       myRenderer->RemoveActor(theActor);
00608     }
00609   };
00610 }
00611 
00615 void
00616 SVTK_View
00617 ::Remove(const Handle(SALOME_InteractiveObject)& theIObject, 
00618          bool theIsUpdate)
00619 {
00620   using namespace SVTK;
00621   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00622   ForEachIf<SALOME_Actor>(aCopy.GetActors(),
00623                           TIsSameIObject<SALOME_Actor>(theIObject),
00624                           TRemoveAction(GetRenderer()));
00625   if(theIsUpdate)
00626     Repaint();
00627 }
00628 
00632 void
00633 SVTK_View
00634 ::Remove(SALOME_Actor* theActor, 
00635          bool theIsUpdate)
00636 {
00637   GetRenderer()->RemoveActor(theActor);
00638   if(theIsUpdate)
00639     Repaint();
00640 }
00641 
00645 void
00646 SVTK_View
00647 ::RemoveAll(bool theIsUpdate)
00648 {
00649   vtkRenderer* aRenderer = getRenderer();
00650   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00651   if(vtkActorCollection* anActors = aCopy.GetActors()){
00652     anActors->InitTraversal();
00653     while(vtkActor *anAct = anActors->GetNextActor()){
00654       if(SALOME_Actor* aSAct = SALOME_Actor::SafeDownCast(anAct)){
00655         if(aSAct->hasIO() && aSAct->getIO()->hasEntry())
00656           aRenderer->RemoveActor( anAct );
00657       }
00658     }
00659 
00660     if(theIsUpdate)
00661       Repaint();
00662   }
00663 }
00664 
00669 float
00670 SVTK_View
00671 ::GetTransparency(const Handle(SALOME_InteractiveObject)& theIObject) 
00672 {
00673   using namespace SVTK;
00674   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00675   SALOME_Actor* anActor = 
00676     Find<SALOME_Actor>(aCopy.GetActors(),
00677                        TIsSameIObject<SALOME_Actor>(theIObject));
00678   if(anActor)
00679     return 1.0 - anActor->GetOpacity();
00680   return -1.0;
00681 }
00682 
00683 
00689 void
00690 SVTK_View
00691 ::SetTransparency(const Handle(SALOME_InteractiveObject)& theIObject, 
00692                   float theTrans)
00693 {
00694   vtkFloatingPointType anOpacity = 1.0 - theTrans;
00695   using namespace SVTK;
00696   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00697   ForEachIf<SALOME_Actor>(aCopy.GetActors(),
00698                           TIsSameIObject<SALOME_Actor>(theIObject),
00699                           TSetFunction<SALOME_Actor,vtkFloatingPointType>
00700                           (&SALOME_Actor::SetOpacity,anOpacity));
00701 }
00702 
00708 void
00709 SVTK_View
00710 ::SetColor(const Handle(SALOME_InteractiveObject)& theIObject,
00711            const QColor& theColor) 
00712 {
00713   vtkFloatingPointType aColor[3] = {theColor.red()/255., theColor.green()/255., theColor.blue()/255.};
00714 
00715   using namespace SVTK;
00716   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00717   ForEachIf<SALOME_Actor>(aCopy.GetActors(),
00718                           TIsSameIObject<SALOME_Actor>(theIObject),
00719                           TSetFunction<SALOME_Actor,const vtkFloatingPointType*>
00720                           (&SALOME_Actor::SetColor,aColor));
00721 }
00722 
00723 
00728 QColor
00729 SVTK_View
00730 ::GetColor(const Handle(SALOME_InteractiveObject)& theIObject) 
00731 {
00732   using namespace SVTK;
00733   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00734   SALOME_Actor* anActor = 
00735     Find<SALOME_Actor>(aCopy.GetActors(),
00736                        TIsSameIObject<SALOME_Actor>(theIObject));
00737   if(anActor){
00738     vtkFloatingPointType r,g,b;
00739     anActor->GetColor(r,g,b);
00740     return QColor(int(r*255),int(g*255),int(b*255));
00741   }
00742 
00743   return QColor(0,0,0);
00744 }
00745 
00751 void
00752 SVTK_View
00753 ::SetMaterial(const Handle(SALOME_InteractiveObject)& theIObject,
00754              vtkProperty* thePropF)
00755 {
00756   using namespace SVTK;
00757   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00758   std::vector<vtkProperty*> aProps;
00759   aProps.push_back( thePropF );
00760   ForEachIf<SALOME_Actor>(aCopy.GetActors(),
00761                           TIsSameIObject<SALOME_Actor>(theIObject),
00762                           TSetFunction<SALOME_Actor,std::vector<vtkProperty*> >
00763                           (&SALOME_Actor::SetMaterial,aProps));
00764 }
00765 
00771 vtkProperty* 
00772 SVTK_View
00773 ::GetMaterial(const Handle(SALOME_InteractiveObject)& theIObject)
00774 {
00775   using namespace SVTK;
00776   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00777   SALOME_Actor* anActor = 
00778     Find<SALOME_Actor>(aCopy.GetActors(),
00779                        TIsSameIObject<SALOME_Actor>(theIObject));
00780   if(anActor)
00781     return anActor->GetMaterial();
00782   return NULL;
00783 }
00784 
00789 void SVTK_View::GetVisible( SALOME_ListIO& theList )
00790 {
00791   using namespace SVTK;
00792   VTK::ActorCollectionCopy aCopy(getRenderer()->GetActors());
00793   ForEach<SALOME_Actor>(aCopy.GetActors(),
00794                         TCollectIfVisible<SALOME_Actor>(theList));
00795 }