]> SALOME platform Git repositories - modules/gui.git/blob - src/SVTK/SVTK_View.cxx
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / SVTK / SVTK_View.cxx
1 //  SALOME VTKViewer : build VTK viewer into Salome desktop
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : 
25 //  Author : 
26 //  Module : SALOME
27 //  $Header$
28
29 #include "SALOME_Actor.h"
30
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"
36
37 #include "VTKViewer_Algorithm.h"
38 #include "SVTK_Functor.h"
39
40 #include <vtkActorCollection.h>
41 #include <vtkRenderer.h>
42
43 /*!
44   Constructor
45 */
46 SVTK_SignalHandler
47 ::SVTK_SignalHandler(SVTK_MainWindow* theMainWindow):
48   QObject(theMainWindow),
49   myMainWindow(theMainWindow)
50 {
51   SVTK_RenderWindowInteractor* anInteractor = theMainWindow->GetInteractor();
52
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()));
69 }
70
71 /*!
72   Destructor
73 */
74 SVTK_SignalHandler
75 ::~SVTK_SignalHandler()
76 {
77 }
78
79 /*!
80   \return corresponding svtk main window
81 */
82 SVTK_MainWindow*
83 SVTK_SignalHandler
84 ::GetMainWindow()
85 {
86   return myMainWindow;
87 }
88
89
90 /*!
91   Redirect the request to #SVTK_MainWindow::Repaint (just for flexibility)
92 */
93 void
94 SVTK_SignalHandler
95 ::Repaint(bool theUpdateTrihedron)
96 {
97   myMainWindow->Repaint(theUpdateTrihedron);
98 }
99
100 /*!
101   Redirect the request to #SVTK_MainWindow::GetRenderer (just for flexibility)
102 */
103 SVTK_Renderer* 
104 SVTK_SignalHandler
105 ::GetRenderer()
106 {
107   return myMainWindow->GetRenderer();
108 }
109
110 /*!
111   Redirect the request to #SVTK_MainWindow::getRenderer (just for flexibility)
112 */
113 vtkRenderer* 
114 SVTK_SignalHandler
115 ::getRenderer()
116 {
117   return myMainWindow->getRenderer();
118 }
119
120 namespace SVTK
121 {
122   struct THighlightAction
123   {
124     bool myIsHighlight;
125     THighlightAction( bool theIsHighlight ):
126       myIsHighlight( theIsHighlight )
127     {}
128     
129     void
130     operator()( SALOME_Actor* theActor) 
131     {
132       if(theActor->GetMapper() && theActor->hasIO()){
133         theActor->Highlight( myIsHighlight );
134       }
135     }
136   };
137 }
138
139 /*!
140   SLOT: called on selection change
141 */
142 void
143 SVTK_SignalHandler
144 ::onSelectionChanged()
145 {
146   vtkActorCollection* anActors = myMainWindow->getRenderer()->GetActors();
147
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));
158   }
159
160   myMainWindow->Repaint(false);
161 }
162
163 /*!
164   Constructor
165 */
166 SVTK_View
167 ::SVTK_View(SVTK_MainWindow* theMainWindow) :
168   SVTK_SignalHandler(theMainWindow)
169 {
170 }
171
172 /*!
173   Destructor
174 */
175 SVTK_View
176 ::~SVTK_View()
177 {
178 }
179
180 /*!
181   Unhilights all objects in viewer
182 */
183 void 
184 SVTK_View
185 ::unHighlightAll() 
186 {
187   using namespace SVTK;
188   ForEach<SALOME_Actor>(getRenderer()->GetActors(),
189                         THighlightAction( false ));
190   Repaint();
191 }
192
193 /*!
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
198 */
199 void
200 SVTK_View
201 ::highlight( const Handle(SALOME_InteractiveObject)& theIO, 
202              bool theIsHighlight, 
203              bool theIsUpdate ) 
204 {
205   using namespace SVTK;
206   ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
207                           TIsSameIObject<SALOME_Actor>( theIO ),
208                           THighlightAction(theIsHighlight));
209   Repaint();
210 }
211
212 /*!
213   Display object
214   \param theEntry - entry that corresponds to intractive objects
215 */
216 Handle(SALOME_InteractiveObject)
217 SVTK_View
218 ::FindIObject(const char* theEntry) 
219 {
220   using namespace SVTK;
221   SALOME_Actor* anActor = 
222     Find<SALOME_Actor>(getRenderer()->GetActors(),
223                        TIsSameEntry<SALOME_Actor>(theEntry));
224   if(anActor != NULL)
225     return anActor->getIO();
226  
227   return NULL;
228 }
229
230 /*!
231    Redirect the request to #SVTK_Renderer::SetPreselectionProp
232 */
233 void
234 SVTK_View
235 ::SetSelectionProp(const double& theRed, 
236                    const double& theGreen, 
237                    const double& theBlue, 
238                    const int& theWidth) 
239 {
240   GetRenderer()->SetSelectionProp(theRed,theGreen,theBlue,theWidth);
241 }
242
243 /*!
244   Redirect the request to #SVTK_Renderer::SetPreselectionProp
245 */
246 void
247 SVTK_View
248 ::SetPreselectionProp(const double& theRed, 
249                       const double& theGreen, 
250                       const double& theBlue, 
251                       const int& theWidth) 
252 {
253   GetRenderer()->SetPreselectionProp(theRed,theGreen,theBlue,theWidth);
254 }
255
256 /*!
257   Redirect the request to #SVTK_Renderer::SetPreselectionProp
258 */
259 void
260 SVTK_View
261 ::SetSelectionTolerance(const double& theTolNodes, 
262                         const double& theTolCell,
263                         const double& theTolObjects)
264 {
265   GetRenderer()->SetSelectionTolerance(theTolNodes,theTolCell, theTolObjects);
266 }
267
268 /*!
269   \return true if object is in viewer or in collector
270   \param theIO - object to be checked
271 */
272 bool
273 SVTK_View
274 ::isInViewer(const Handle(SALOME_InteractiveObject)& theIObject)
275 {
276   using namespace SVTK;
277   SALOME_Actor* anActor = 
278     Find<SALOME_Actor>(getRenderer()->GetActors(),
279                        TIsSameIObject<SALOME_Actor>(theIObject));
280   return anActor != NULL;
281 }
282
283 /*!
284   \return true if object is displayed in viewer
285   \param theIO - object to be checked
286 */
287 bool
288 SVTK_View
289 ::isVisible(const Handle(SALOME_InteractiveObject)& theIObject)
290 {
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();
296 }
297
298 /*!
299   Changes name of object
300   \param theIObject - object to be renamed
301   \param theName - new name
302 */
303 void
304 SVTK_View
305 ::rename(const Handle(SALOME_InteractiveObject)& theIObject, 
306          const QString& theName)
307 {
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()));
313 }
314
315 /*!
316   \return current display mode (obsolete)
317 */
318 int
319 SVTK_View
320 ::GetDisplayMode() 
321 {
322   return myDisplayMode; 
323 }
324
325 /*!
326   Set current display mode
327   \param theMode - new display mode
328 */
329 void
330 SVTK_View
331 ::SetDisplayMode(int theMode)
332 {
333   if(theMode == 0) 
334     ChangeRepresentationToWireframe();
335   else 
336     ChangeRepresentationToSurface();
337   myDisplayMode = theMode;
338 }
339
340 /*!
341   Set current display mode
342   \param theIObject - object
343   \param theMode - new display mode
344 */
345 void
346 SVTK_View
347 ::SetDisplayMode(const Handle(SALOME_InteractiveObject)& theIObject, 
348                  int theMode)
349 {
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));
355 }
356
357 /*!
358   Change all actors to wireframe
359 */
360 void
361 SVTK_View
362 ::ChangeRepresentationToWireframe()
363 {
364   ChangeRepresentationToWireframe(getRenderer()->GetActors());
365 }
366
367 /*!
368   Change all actors to shading
369 */
370 void
371 SVTK_View
372 ::ChangeRepresentationToSurface()
373 {
374   ChangeRepresentationToSurface(getRenderer()->GetActors());
375 }
376
377 /*!
378   Change to wireframe a list of vtkactor
379   theCollection - list of vtkactor
380 */
381 void
382 SVTK_View
383 ::ChangeRepresentationToWireframe(vtkActorCollection* theCollection)
384 {
385   using namespace SVTK;
386   ForEach<SALOME_Actor>(theCollection,
387                         TSetFunction<SALOME_Actor,int>
388                         (&SALOME_Actor::setDisplayMode,0));
389   Repaint();
390 }
391
392 /*!
393   Change to shading a list of vtkactor
394   theCollection - list of vtkactor
395 */
396 void
397 SVTK_View
398 ::ChangeRepresentationToSurface(vtkActorCollection* theCollection)
399 {
400   using namespace SVTK;
401   ForEach<SALOME_Actor>(theCollection,
402                         TSetFunction<SALOME_Actor,int>
403                         (&SALOME_Actor::setDisplayMode,1));
404   Repaint();
405 }
406
407 namespace SVTK
408 {
409   struct TErase
410   {
411     VTK::TSetFunction<vtkActor,int> mySetFunction;
412     TErase():
413       mySetFunction(&vtkActor::SetVisibility,false)
414     {}
415     void
416     operator()(SALOME_Actor* theActor)
417     {
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();
424     }
425   };
426 }
427
428 /*!
429   To erase all existing VTK presentations
430 */
431 void
432 SVTK_View
433 ::EraseAll()
434 {   
435   using namespace SVTK;
436   ForEach<SALOME_Actor>(getRenderer()->GetActors(),
437                         TErase());
438   Repaint();
439 }
440
441 /*!
442   To display all existing VTK presentations
443 */
444 void
445 SVTK_View
446 ::DisplayAll()
447
448   using namespace SVTK;
449   ForEach<SALOME_Actor>(getRenderer()->GetActors(),
450                         TSetVisibility<SALOME_Actor>(true));
451   Repaint();
452 }
453
454 /*!
455   To erase VTK presentation
456   \param theActor - actor
457   \param theIsUpdate - updates current viewer
458 */
459 void
460 SVTK_View
461 ::Erase(SALOME_Actor* theActor, 
462         bool theIsUpdate)
463 {
464   SVTK::TErase()(theActor);
465
466   if(theIsUpdate)
467     Repaint();
468 }
469
470
471 /*!
472   To erase VTK presentation
473   \param theIObject - object
474   \param theIsUpdate - updates current viewer
475 */
476 void
477 SVTK_View
478 ::Erase(const Handle(SALOME_InteractiveObject)& theIObject, 
479         bool theIsUpdate)
480 {
481   using namespace SVTK;
482   ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
483                           TIsSameIObject<SALOME_Actor>(theIObject),
484                           TErase());
485   if(theIsUpdate)
486     Repaint();
487 }
488
489 /*!
490   To display the VTK presentation
491 */
492 void
493 SVTK_View
494 ::Display(SALOME_Actor* theActor, 
495           bool theIsUpdate)
496 {
497   GetRenderer()->AddActor(theActor);
498   theActor->SetVisibility(true);
499
500   if(theIsUpdate)
501     Repaint();
502 }
503
504 /*!
505   To display the VTK presentation
506 */
507 void
508 SVTK_View
509 ::Display(const Handle(SALOME_InteractiveObject)& theIObject, 
510           bool theIsUpdate)
511 {
512   using namespace SVTK;
513   ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
514                           TIsSameIObject<SALOME_Actor>(theIObject),
515                           TSetVisibility<SALOME_Actor>(true));
516
517   if(theIsUpdate)
518     Repaint();
519 }
520
521 /*!
522   To display VTK presentation with defined #SALOME_InteractiveObject and erase all others
523 */
524 void
525 SVTK_View
526 ::DisplayOnly(const Handle(SALOME_InteractiveObject)& theIObject)
527 {
528   EraseAll();
529   Display(theIObject);
530 }
531
532
533 namespace SVTK
534 {
535   struct TRemoveAction
536   {
537     SVTK_Renderer* myRenderer;
538     TRemoveAction(SVTK_Renderer* theRenderer): 
539       myRenderer(theRenderer)
540     {}
541     void
542     operator()(SALOME_Actor* theActor)
543     {
544       myRenderer->RemoveActor(theActor);
545     }
546   };
547 }
548
549 /*!
550   To remove the VTK presentation
551 */
552 void
553 SVTK_View
554 ::Remove(const Handle(SALOME_InteractiveObject)& theIObject, 
555          bool theIsUpdate)
556 {
557   using namespace SVTK;
558   ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
559                           TIsSameIObject<SALOME_Actor>(theIObject),
560                           TRemoveAction(GetRenderer()));
561   if(theIsUpdate)
562     Repaint();
563 }
564
565 /*!
566   To remove the VTK presentation
567 */
568 void
569 SVTK_View
570 ::Remove(SALOME_Actor* theActor, 
571          bool theIsUpdate)
572 {
573   GetRenderer()->RemoveActor(theActor);
574   if(theIsUpdate)
575     Repaint();
576 }
577
578 /*!
579   To remove all VTK presentations
580 */
581 void
582 SVTK_View
583 ::RemoveAll(bool theIsUpdate)
584 {
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 );
592       }
593     }
594
595     if(theIsUpdate)
596       Repaint();
597   }
598 }
599
600 /*!
601   \return current transparency
602   \param theIObject - object
603 */
604 float
605 SVTK_View
606 ::GetTransparency(const Handle(SALOME_InteractiveObject)& theIObject) 
607 {
608   using namespace SVTK;
609   SALOME_Actor* anActor = 
610     Find<SALOME_Actor>(getRenderer()->GetActors(),
611                        TIsSameIObject<SALOME_Actor>(theIObject));
612   if(anActor)
613     return 1.0 - anActor->GetOpacity();
614   return -1.0;
615 }
616
617
618 /*!
619   Sets current transparency
620   \param theIObject - object
621   \param theTrans - new transparency
622 */
623 void
624 SVTK_View
625 ::SetTransparency(const Handle(SALOME_InteractiveObject)& theIObject, 
626                   float theTrans)
627 {
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));
634 }
635
636 /*!
637   Change color
638   \param theIObject - object
639   \param theColor - new color
640 */
641 void
642 SVTK_View
643 ::SetColor(const Handle(SALOME_InteractiveObject)& theIObject,
644            const QColor& theColor) 
645 {
646   vtkFloatingPointType aColor[3] = {theColor.red()/255., theColor.green()/255., theColor.blue()/255.};
647
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));
653 }
654
655
656 /*!
657   \return object color
658   \param theIObject - object
659 */
660 QColor
661 SVTK_View
662 ::GetColor(const Handle(SALOME_InteractiveObject)& theIObject) 
663 {
664   using namespace SVTK;
665   SALOME_Actor* anActor = 
666     Find<SALOME_Actor>(getRenderer()->GetActors(),
667                        TIsSameIObject<SALOME_Actor>(theIObject));
668   if(anActor){
669     vtkFloatingPointType r,g,b;
670     anActor->GetColor(r,g,b);
671     return QColor(int(r*255),int(g*255),int(b*255));
672   }
673
674   return QColor(0,0,0);
675 }
676