Salome HOME
Copyrights update
[modules/gui.git] / src / SVTK / SVTK_ViewWindow.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "SALOME_Actor.h"
20
21 #include <qapplication.h>
22
23 #include <vtkTextProperty.h>
24 #include <vtkActorCollection.h>
25 #include <vtkRenderWindow.h>
26 #include <vtkRenderer.h>
27 #include <vtkCamera.h>
28
29 #include "QtxAction.h"
30
31 #include "SUIT_Session.h"
32 #include "SUIT_ToolButton.h"
33 #include "SUIT_MessageBox.h"
34 #include "SUIT_Accel.h"
35
36 #include "SUIT_Tools.h"
37 #include "SUIT_ResourceMgr.h"
38
39 #include "VTKViewer_Transform.h"
40 #include "VTKViewer_Utilities.h"
41
42 #include "SVTK_Trihedron.h"
43 #include "SVTK_CubeAxesActor2D.h"
44 #include "SVTK_ViewWindow.h"
45 #include "SVTK_ViewModel.h"
46 #include "SVTK_RenderWindow.h"
47 #include "SVTK_RenderWindowInteractor.h"
48 #include "SVTK_InteractorStyle.h"
49
50 #include "SALOME_ListIteratorOfListIO.hxx"
51
52 #include "SVTK_SelectorDef.h"
53
54 #include "VTKViewer_Algorithm.h"
55 #include "SVTK_Functor.h"
56
57 //----------------------------------------------------------------------------
58 SVTK_ViewWindow
59 ::SVTK_ViewWindow( SUIT_Desktop* theDesktop, 
60                    SVTK_Viewer* theModel)
61   : SUIT_ViewWindow( theDesktop ),
62     myTrihedronSize( 100 )
63 {
64   myModel = theModel;
65   mySelector = new SVTK_SelectorDef();
66   connect(this,SIGNAL(selectionChanged()),theModel,SLOT(onSelectionChanged()));
67
68   myTransform = VTKViewer_Transform::New();
69   myTrihedron = SVTK_Trihedron::New();
70   myCubeAxes  = SVTK_CubeAxesActor2D::New();
71   myRenderer  = vtkRenderer::New() ;
72
73   myTrihedron->AddToRender( myRenderer );
74   myRenderer->AddProp(myCubeAxes);
75
76   myRenderWindow = new SVTK_RenderWindow( this, "RenderWindow" );
77   setCentralWidget(myRenderWindow);
78   myRenderWindow->setFocusPolicy( StrongFocus );
79   myRenderWindow->setFocus();
80
81   myRenderWindow->getRenderWindow()->AddRenderer( myRenderer );
82
83   myRenderer->GetActiveCamera()->ParallelProjectionOn();
84   myRenderer->LightFollowCameraOn();
85   myRenderer->TwoSidedLightingOn();
86
87   // Set BackgroundColor
88   QString BgrColorRed   = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorRed");
89   QString BgrColorGreen = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorGreen");
90   QString BgrColorBlue  = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorBlue");
91
92   if( !BgrColorRed.isEmpty() && !BgrColorGreen.isEmpty() && !BgrColorBlue.isEmpty() ) 
93     myRenderer->SetBackground( BgrColorRed.toInt()/255., BgrColorGreen.toInt()/255., BgrColorBlue.toInt()/255. );
94   else
95     myRenderer->SetBackground( 0, 0, 0 );
96   
97   // Create an interactor.
98   myRWInteractor = SVTK_RenderWindowInteractor::New();
99   myRWInteractor->SetRenderWindow( myRenderWindow->getRenderWindow() );
100   myRWInteractor->setViewWindow( this );
101
102   SVTK_InteractorStyle* RWS = SVTK_InteractorStyle::New();
103   RWS->setGUIWindow( myRenderWindow );
104   RWS->setViewWindow( this );
105
106   myRWInteractor->SetInteractorStyle( RWS ); 
107   myRWInteractor->Initialize();
108
109   //merge with V2_2_0_VISU_improvements:RWS->setTriedron( myTrihedron );
110   RWS->FindPokedRenderer( 0, 0 );
111
112   SetSelectionMode(ActorSelection);
113
114   vtkTextProperty* tprop = vtkTextProperty::New();
115   tprop->SetColor(1, 1, 1);
116   tprop->ShadowOn();
117   
118   float bnd[6];
119   bnd[0] = bnd[2] = bnd[4] = 0;
120   bnd[1] = bnd[3] = bnd[5] = myTrihedron->GetSize();
121   myCubeAxes->SetLabelFormat("%6.4g");
122   myCubeAxes->SetBounds(bnd);
123   myCubeAxes->SetCamera(myRenderer->GetActiveCamera());
124   myCubeAxes->SetFlyModeToOuterEdges(); // ENK remarks: it must bee
125   myCubeAxes->SetFontFactor(0.8);
126   myCubeAxes->SetAxisTitleTextProperty(tprop);
127   myCubeAxes->SetAxisLabelTextProperty(tprop);
128   myCubeAxes->SetCornerOffset(0);
129   myCubeAxes->SetScaling(0);
130   myCubeAxes->SetNumberOfLabels(5);
131   myCubeAxes->VisibilityOff();
132   myCubeAxes->SetTransform(myTransform);
133   tprop->Delete();
134   
135   setCentralWidget( myRenderWindow );
136
137   myToolBar = new QToolBar(this);
138   myToolBar->setCloseMode(QDockWindow::Undocked);
139   myToolBar->setLabel(tr("LBL_TOOLBAR_LABEL"));
140
141   createActions();
142   createToolBar();
143
144   connect( myRenderWindow, SIGNAL(KeyPressed( QKeyEvent* )),
145            this,           SLOT(onKeyPressed( QKeyEvent* )) );
146   connect( myRenderWindow, SIGNAL(KeyReleased( QKeyEvent* )),
147            this,           SLOT(onKeyReleased( QKeyEvent* )) );
148   connect( myRenderWindow, SIGNAL(MouseButtonPressed( QMouseEvent* )),
149            this,           SLOT(onMousePressed( QMouseEvent* )) );
150   connect( myRenderWindow, SIGNAL(MouseButtonReleased( QMouseEvent* )),
151            this,           SLOT(onMouseReleased( QMouseEvent* )) );
152   connect( myRenderWindow, SIGNAL(MouseDoubleClicked( QMouseEvent* )),
153            this,           SLOT(onMouseDoubleClicked( QMouseEvent* )) );
154   connect( myRenderWindow, SIGNAL(MouseMove( QMouseEvent* )),
155            this,           SLOT(onMouseMoving( QMouseEvent* )) );
156
157   connect( myRWInteractor, SIGNAL(RenderWindowModified()),
158            myRenderWindow, SLOT(update()) );
159   connect( myRWInteractor, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
160            this,           SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
161
162   onResetView();
163 }
164
165 //----------------------------------------------------------------------------
166 SVTK_ViewWindow
167 ::~SVTK_ViewWindow()
168 {
169   myTransform->Delete();
170   // In order to ensure that the interactor unregisters
171   // this RenderWindow, we assign a NULL RenderWindow to 
172   // it before deleting it.
173   myRWInteractor->SetRenderWindow( NULL );
174   myRWInteractor->Delete();
175   
176   //m_RW->Delete() ;
177   myRenderer->RemoveAllProps();
178   //m_Renderer->Delete();
179   myTrihedron->Delete();
180   myCubeAxes->Delete();
181 }
182
183 //----------------------------------------------------------------------------
184 void
185 SVTK_ViewWindow
186 ::activateZoom()
187 {
188   myRWInteractor->GetSInteractorStyle()->startZoom();
189 }
190
191 //----------------------------------------------------------------------------
192 void
193 SVTK_ViewWindow
194 ::activatePanning()
195 {
196   myRWInteractor->GetSInteractorStyle()->startPan();
197 }
198
199 //----------------------------------------------------------------------------
200 void
201 SVTK_ViewWindow
202 ::activateRotation()
203 {
204   myRWInteractor->GetSInteractorStyle()->startRotate();
205 }
206
207 //----------------------------------------------------------------------------
208 void
209 SVTK_ViewWindow
210 ::activateGlobalPanning()
211 {
212   if(myTrihedron->GetVisibleActorCount(myRenderer))
213     myRWInteractor->GetSInteractorStyle()->startGlobalPan();
214 }
215
216 //----------------------------------------------------------------------------
217 void
218 SVTK_ViewWindow
219 ::activateWindowFit()
220 {
221   myRWInteractor->GetSInteractorStyle()->startFitArea();
222 }
223
224 //----------------------------------------------------------------------------
225 #if defined(WIN32) && !defined(_DEBUG)
226 #pragma optimize( "", off )
227 #endif
228 void
229 SVTK_ViewWindow
230 ::createActions()
231 {
232   if (!myActionsMap.isEmpty()) return;
233   
234   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
235   
236   QtxAction* aAction;
237
238   // Dump view
239   aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_DUMP" ) ),
240                            tr( "MNU_DUMP_VIEW" ), 0, this);
241   aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
242   connect(aAction, SIGNAL(activated()), this, SLOT(onDumpView()));
243   myActionsMap[ DumpId ] = aAction;
244
245   // FitAll
246   aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITALL" ) ),
247                            tr( "MNU_FITALL" ), 0, this);
248   aAction->setStatusTip(tr("DSC_FITALL"));
249   connect(aAction, SIGNAL(activated()), this, SLOT(onFitAll()));
250   myActionsMap[ FitAllId ] = aAction;
251
252   // FitRect
253   aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITAREA" ) ),
254                            tr( "MNU_FITRECT" ), 0, this);
255   aAction->setStatusTip(tr("DSC_FITRECT"));
256   connect(aAction, SIGNAL(activated()), this, SLOT(activateWindowFit()));
257   myActionsMap[ FitRectId ] = aAction;
258
259   // Zoom
260   aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ZOOM" ) ),
261                            tr( "MNU_ZOOM_VIEW" ), 0, this);
262   aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
263   connect(aAction, SIGNAL(activated()), this, SLOT(activateZoom()));
264   myActionsMap[ ZoomId ] = aAction;
265
266   // Panning
267   aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_PAN" ) ),
268                            tr( "MNU_PAN_VIEW" ), 0, this);
269   aAction->setStatusTip(tr("DSC_PAN_VIEW"));
270   connect(aAction, SIGNAL(activated()), this, SLOT(activatePanning()));
271   myActionsMap[ PanId ] = aAction;
272
273   // Global Panning
274   aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_GLOBALPAN" ) ),
275                            tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
276   aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
277   connect(aAction, SIGNAL(activated()), this, SLOT(activateGlobalPanning()));
278   myActionsMap[ GlobalPanId ] = aAction;
279
280   // Rotation
281   aAction = new QtxAction(tr("MNU_ROTATE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ROTATE" ) ),
282                            tr( "MNU_ROTATE_VIEW" ), 0, this);
283   aAction->setStatusTip(tr("DSC_ROTATE_VIEW"));
284   connect(aAction, SIGNAL(activated()), this, SLOT(activateRotation()));
285   myActionsMap[ RotationId ] = aAction;
286
287   // Projections
288   aAction = new QtxAction(tr("MNU_FRONT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FRONT" ) ),
289                            tr( "MNU_FRONT_VIEW" ), 0, this);
290   aAction->setStatusTip(tr("DSC_FRONT_VIEW"));
291   connect(aAction, SIGNAL(activated()), this, SLOT(onFrontView()));
292   myActionsMap[ FrontId ] = aAction;
293
294   aAction = new QtxAction(tr("MNU_BACK_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BACK" ) ),
295                            tr( "MNU_BACK_VIEW" ), 0, this);
296   aAction->setStatusTip(tr("DSC_BACK_VIEW"));
297   connect(aAction, SIGNAL(activated()), this, SLOT(onBackView()));
298   myActionsMap[ BackId ] = aAction;
299
300   aAction = new QtxAction(tr("MNU_TOP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TOP" ) ),
301                            tr( "MNU_TOP_VIEW" ), 0, this);
302   aAction->setStatusTip(tr("DSC_TOP_VIEW"));
303   connect(aAction, SIGNAL(activated()), this, SLOT(onTopView()));
304   myActionsMap[ TopId ] = aAction;
305
306   aAction = new QtxAction(tr("MNU_BOTTOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BOTTOM" ) ),
307                            tr( "MNU_BOTTOM_VIEW" ), 0, this);
308   aAction->setStatusTip(tr("DSC_BOTTOM_VIEW"));
309   connect(aAction, SIGNAL(activated()), this, SLOT(onBottomView()));
310   myActionsMap[ BottomId ] = aAction;
311
312   aAction = new QtxAction(tr("MNU_LEFT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_LEFT" ) ),
313                            tr( "MNU_LEFT_VIEW" ), 0, this);
314   aAction->setStatusTip(tr("DSC_LEFT_VIEW"));
315   connect(aAction, SIGNAL(activated()), this, SLOT(onLeftView()));
316   myActionsMap[ LeftId ] = aAction;
317
318   aAction = new QtxAction(tr("MNU_RIGHT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RIGHT" ) ),
319                            tr( "MNU_RIGHT_VIEW" ), 0, this);
320   aAction->setStatusTip(tr("DSC_RIGHT_VIEW"));
321   connect(aAction, SIGNAL(activated()), this, SLOT(onRightView()));
322   myActionsMap[ RightId ] = aAction;
323
324   // Reset
325   aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RESET" ) ),
326                            tr( "MNU_RESET_VIEW" ), 0, this);
327   aAction->setStatusTip(tr("DSC_RESET_VIEW"));
328   connect(aAction, SIGNAL(activated()), this, SLOT(onResetView()));
329   myActionsMap[ ResetId ] = aAction;
330
331   // onViewTrihedron: Shows - Hides Trihedron
332   aAction = new QtxAction(tr("MNU_SHOW_TRIHEDRON"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TRIHEDRON" ) ),
333                            tr( "MNU_SHOW_TRIHEDRON" ), 0, this);
334   aAction->setStatusTip(tr("DSC_SHOW_TRIHEDRON"));
335   connect(aAction, SIGNAL(activated()), this, SLOT(onViewTrihedron()));
336   myActionsMap[ ViewTrihedronId ] = aAction;
337 }
338 #if defined(WIN32) && !defined(_DEBUG)
339 #pragma optimize( "", on )
340 #endif
341
342 //----------------------------------------------------------------------------
343 void
344 SVTK_ViewWindow
345 ::createToolBar()
346 {
347   myActionsMap[DumpId]->addTo(myToolBar);
348   myActionsMap[ViewTrihedronId]->addTo(myToolBar);
349
350   SUIT_ToolButton* aScaleBtn = new SUIT_ToolButton(myToolBar);
351   aScaleBtn->AddAction(myActionsMap[FitAllId]);
352   aScaleBtn->AddAction(myActionsMap[FitRectId]);
353   aScaleBtn->AddAction(myActionsMap[ZoomId]);
354
355   SUIT_ToolButton* aPanningBtn = new SUIT_ToolButton(myToolBar);
356   aPanningBtn->AddAction(myActionsMap[PanId]);
357   aPanningBtn->AddAction(myActionsMap[GlobalPanId]);
358
359   myActionsMap[RotationId]->addTo(myToolBar);
360
361   SUIT_ToolButton* aViewsBtn = new SUIT_ToolButton(myToolBar);
362   aViewsBtn->AddAction(myActionsMap[FrontId]);
363   aViewsBtn->AddAction(myActionsMap[BackId]);
364   aViewsBtn->AddAction(myActionsMap[TopId]);
365   aViewsBtn->AddAction(myActionsMap[BottomId]);
366   aViewsBtn->AddAction(myActionsMap[LeftId]);
367   aViewsBtn->AddAction(myActionsMap[RightId]);
368
369   myActionsMap[ResetId]->addTo(myToolBar);
370 }
371
372 //----------------------------------------------------------------------------
373 void
374 SVTK_ViewWindow
375 ::onFrontView()
376 {
377   vtkCamera* camera = myRenderer->GetActiveCamera();
378   camera->SetPosition(1,0,0);
379   camera->SetViewUp(0,0,1);
380   camera->SetFocalPoint(0,0,0);
381   onFitAll();
382 }
383
384 //----------------------------------------------------------------------------
385 void
386 SVTK_ViewWindow
387 ::onBackView()
388 {
389   vtkCamera* camera = myRenderer->GetActiveCamera();
390   camera->SetPosition(-1,0,0);
391   camera->SetViewUp(0,0,1);
392   camera->SetFocalPoint(0,0,0);
393   onFitAll();
394 }
395
396 //----------------------------------------------------------------------------
397 void
398 SVTK_ViewWindow
399 ::onTopView()
400 {
401   vtkCamera* camera = myRenderer->GetActiveCamera();
402   camera->SetPosition(0,0,1);
403   camera->SetViewUp(0,1,0);
404   camera->SetFocalPoint(0,0,0);
405   onFitAll();
406 }
407
408 //----------------------------------------------------------------------------
409 void
410 SVTK_ViewWindow
411 ::onBottomView()
412 {
413   vtkCamera* camera = myRenderer->GetActiveCamera();
414   camera->SetPosition(0,0,-1);
415   camera->SetViewUp(0,1,0);
416   camera->SetFocalPoint(0,0,0);
417   onFitAll();
418 }
419
420 //----------------------------------------------------------------------------
421 void
422 SVTK_ViewWindow
423 ::onLeftView()
424 {
425   vtkCamera* camera = myRenderer->GetActiveCamera(); 
426   camera->SetPosition(0,-1,0);
427   camera->SetViewUp(0,0,1);
428   camera->SetFocalPoint(0,0,0);
429   onFitAll();
430 }
431
432 //----------------------------------------------------------------------------
433 void
434 SVTK_ViewWindow
435 ::onRightView()
436 {
437   vtkCamera* camera = myRenderer->GetActiveCamera();
438   camera->SetPosition(0,1,0);
439   camera->SetViewUp(0,0,1);
440   camera->SetFocalPoint(0,0,0);
441   onFitAll();
442 }
443
444 //----------------------------------------------------------------------------
445 void
446 SVTK_ViewWindow
447 ::onResetView()
448 {
449   int aTrihedronIsVisible = isTrihedronDisplayed();
450   int aCubeAxesIsVisible  = isCubeAxesDisplayed();
451
452   myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
453   myCubeAxes->SetVisibility(0);
454
455   ::ResetCamera(myRenderer,true);  
456   vtkCamera* aCamera = myRenderer->GetActiveCamera();
457   aCamera->SetPosition(1,-1,1);
458   aCamera->SetViewUp(0,0,1);
459   ::ResetCamera(myRenderer,true);  
460
461   if (aTrihedronIsVisible) myTrihedron->VisibilityOn();
462   else myTrihedron->VisibilityOff();
463
464   if (aCubeAxesIsVisible) myCubeAxes->VisibilityOn();
465   else myCubeAxes->VisibilityOff();
466
467   static float aCoeff = 3.0;
468   aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
469   Repaint();
470 }
471
472 //----------------------------------------------------------------------------
473 void
474 SVTK_ViewWindow
475 ::onFitAll()
476 {
477   int aTrihedronWasVisible = false;
478   int aCubeAxesWasVisible = false;
479   if (myTrihedron) {
480     aTrihedronWasVisible = isTrihedronDisplayed();
481     if (aTrihedronWasVisible)
482       myTrihedron->VisibilityOff();
483   }
484
485   if (myCubeAxes) {
486     aCubeAxesWasVisible = isCubeAxesDisplayed();
487     if (aCubeAxesWasVisible)
488       myCubeAxes->VisibilityOff();
489   }
490
491   if (myTrihedron->GetVisibleActorCount(myRenderer)) {
492     myTrihedron->VisibilityOff();
493     myCubeAxes->VisibilityOff();
494     ::ResetCamera(myRenderer);
495   } else {
496     myTrihedron->SetVisibility(VTKViewer_Trihedron::eOnlyLineOn);
497     myCubeAxes->SetVisibility(2);
498     ::ResetCamera(myRenderer,true);
499   }
500
501   if (aTrihedronWasVisible)
502     myTrihedron->VisibilityOn();
503   else
504     myTrihedron->VisibilityOff();
505
506   if (aCubeAxesWasVisible)
507     myCubeAxes->VisibilityOn();
508   else
509     myCubeAxes->VisibilityOff();
510
511   ::ResetCameraClippingRange(myRenderer);
512
513   Repaint();
514 }
515
516 //----------------------------------------------------------------
517 void
518 SVTK_ViewWindow
519 ::onSelectionChanged()
520 {
521   unHighlightAll();
522
523   const SALOME_ListIO& aListIO = mySelector->StoredIObjects();
524   SALOME_ListIteratorOfListIO anIter(aListIO);
525   for(; anIter.More(); anIter.Next()){
526     highlight(anIter.Value(),true,!anIter.More());
527   }
528
529   emit selectionChanged();
530 }
531
532 //----------------------------------------------------------------
533 void
534 SVTK_ViewWindow
535 ::SetSelectionMode(Selection_Mode theMode)
536 {
537   mySelector->SetSelectionMode(theMode);
538   myRWInteractor->SetSelectionMode(theMode);
539 }
540
541 //----------------------------------------------------------------
542 Selection_Mode
543 SVTK_ViewWindow
544 ::SelectionMode() const
545 {
546   return mySelector->SelectionMode();
547 }
548
549 //----------------------------------------------------------------
550 void 
551 SVTK_ViewWindow
552 ::unHighlightAll() 
553 {
554   myRWInteractor->unHighlightAll();
555 }
556
557 //----------------------------------------------------------------
558 void
559 SVTK_ViewWindow
560 ::highlight( const Handle(SALOME_InteractiveObject)& theIO, 
561              bool theIsHighlight, 
562              bool theIsUpdate ) 
563 {
564   myRWInteractor->highlight(theIO, theIsHighlight, theIsUpdate);
565
566   if(mySelector->HasIndex(theIO) && theIO->hasEntry()){
567     TColStd_IndexedMapOfInteger aMapIndex;
568     mySelector->GetIndex(theIO,aMapIndex);
569     using namespace VTK;
570     const char* anEntry = theIO->getEntry();
571     vtkActorCollection* aCollection = myRenderer->GetActors();
572     if(SALOME_Actor* anActor = Find<SALOME_Actor>(aCollection,TIsSameEntry<SALOME_Actor>(anEntry))){
573       switch (mySelector->SelectionMode()) {
574       case NodeSelection:
575         myRWInteractor->highlightPoint(aMapIndex,anActor,theIsHighlight,theIsUpdate);
576         break;
577       case EdgeOfCellSelection:
578         myRWInteractor->highlightEdge(aMapIndex,anActor,theIsHighlight,theIsUpdate);
579         break;
580       case CellSelection:
581       case EdgeSelection:
582       case FaceSelection:
583       case VolumeSelection:
584         myRWInteractor->highlightCell(aMapIndex,anActor,theIsHighlight,theIsUpdate);
585         break;
586       }
587     }
588   }else{
589     myRWInteractor->unHighlightSubSelection();
590   }
591 }
592
593 //----------------------------------------------------------------
594 bool
595 SVTK_ViewWindow
596 ::isInViewer( const Handle(SALOME_InteractiveObject)& theIO ) 
597 {
598   return myRWInteractor->isInViewer( theIO );
599 }
600
601 //----------------------------------------------------------------
602 bool
603 SVTK_ViewWindow
604 ::isVisible( const Handle(SALOME_InteractiveObject)& theIO ) 
605 {
606   return myRWInteractor->isVisible( theIO );
607 }
608
609 //----------------------------------------------------------------------------
610 void
611 SVTK_ViewWindow
612 ::setBackgroundColor( const QColor& color )
613 {
614   if ( myRenderer )
615     myRenderer->SetBackground( color.red()/255., color.green()/255., color.blue()/255. );
616 }
617
618 //----------------------------------------------------------------------------
619 QColor
620 SVTK_ViewWindow
621 ::backgroundColor() const
622 {
623   float backint[3];
624   if ( myRenderer ) {
625     myRenderer->GetBackground( backint );
626     return QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255));
627   }
628   return SUIT_ViewWindow::backgroundColor();
629 }
630
631 //----------------------------------------------------------------------------
632 void
633 SVTK_ViewWindow
634 ::Repaint(bool theUpdateTrihedron)
635 {
636   if (theUpdateTrihedron) 
637     onAdjustTrihedron();
638   myRenderWindow->update();
639 }
640
641 //----------------------------------------------------------------------------
642 void
643 SVTK_ViewWindow
644 ::GetScale( double theScale[3] ) 
645 {
646   myTransform->GetMatrixScale( theScale );
647 }
648
649 //----------------------------------------------------------------------------
650 void
651 SVTK_ViewWindow
652 ::SetScale( double theScale[3] ) 
653 {
654   myTransform->SetMatrixScale( theScale[0], theScale[1], theScale[2] );
655   myRWInteractor->Render();
656   Repaint();
657 }
658
659 //----------------------------------------------------------------------------
660 bool
661 SVTK_ViewWindow
662 ::isTrihedronDisplayed()
663 {
664   return myTrihedron->GetVisibility() == VTKViewer_Trihedron::eOn;
665 }
666
667 bool
668 SVTK_ViewWindow
669 ::isCubeAxesDisplayed()
670 {
671   return myCubeAxes->GetVisibility() == 1;
672 }
673
674 //----------------------------------------------------------------------------
675 void 
676 SVTK_ViewWindow
677 ::onViewTrihedron()
678 {
679   if(!myTrihedron) 
680     return;
681
682   if(isTrihedronDisplayed())
683     myTrihedron->VisibilityOff();
684   else
685     myTrihedron->VisibilityOn();
686
687   Repaint();
688 }
689
690 void
691 SVTK_ViewWindow
692 ::onViewCubeAxes()
693 {
694   if(!myCubeAxes)
695     return;
696
697   if(isCubeAxesDisplayed())
698     myCubeAxes->VisibilityOff();
699   else
700     myCubeAxes->VisibilityOn();
701
702   Repaint();
703 }
704
705 //----------------------------------------------------------------------------
706 /*bool
707 SVTK_ViewWindow
708 ::ComputeTrihedronSize( double& theNewSize, double& theSize )
709 {
710   // calculating diagonal of visible props of the renderer
711   float aBndBox[ 6 ];
712   myTrihedron->VisibilityOff();
713
714   if ( ::ComputeVisiblePropBounds( myRenderer, aBndBox ) == 0 ) {
715     aBndBox[ 1 ] = aBndBox[ 3 ] = aBndBox[ 5 ] = 100;
716     aBndBox[ 0 ] = aBndBox[ 2 ] = aBndBox[ 4 ] = 0;
717   }
718
719   myTrihedron->VisibilityOn();
720   float aLength = 0;
721   static bool aCalcByDiag = false;
722   if ( aCalcByDiag ) {
723     aLength = sqrt( ( aBndBox[1]-aBndBox[0])*(aBndBox[1]-aBndBox[0] )+
724                     ( aBndBox[3]-aBndBox[2])*(aBndBox[3]-aBndBox[2] )+
725                     ( aBndBox[5]-aBndBox[4])*(aBndBox[5]-aBndBox[4] ) );
726   } else {
727     aLength = aBndBox[ 1 ]-aBndBox[ 0 ];
728     aLength = max( ( aBndBox[ 3 ] - aBndBox[ 2 ] ),aLength );
729     aLength = max( ( aBndBox[ 5 ] - aBndBox[ 4 ] ),aLength );
730   }
731
732   float aSizeInPercents = myTrihedronSize;
733
734   static float EPS_SIZE = 5.0E-3;
735   theSize = myTrihedron->GetSize();
736   theNewSize = aLength * aSizeInPercents / 100.0;
737
738   // if the new trihedron size have sufficient difference, then apply the value
739   return fabs( theNewSize - theSize) > theSize * EPS_SIZE ||
740          fabs( theNewSize-theSize ) > theNewSize * EPS_SIZE;
741 }*/
742
743 //----------------------------------------------------------------------------
744 int SVTK_ViewWindow::GetTrihedronSize() const
745 {
746   return myTrihedronSize;
747 }
748
749 void SVTK_ViewWindow::SetTrihedronSize( const int sz )
750 {
751   if ( myTrihedronSize == sz )
752     return;
753
754   myTrihedronSize = sz;
755   AdjustTrihedrons( true );
756 }
757
758 /*! If parameter theIsForcedUpdate is true, recalculate parameters for
759  *  trihedron and cube axes, even if trihedron and cube axes is invisible.
760  */
761 void
762 SVTK_ViewWindow
763 ::AdjustTrihedrons(const bool theIsForcedUpdate)
764 {
765   if ((!isCubeAxesDisplayed() && !isTrihedronDisplayed()) && !theIsForcedUpdate)
766     return;
767
768   float bnd[ 6 ];
769   float newbnd[6];
770   newbnd[ 0 ] = newbnd[ 2 ] = newbnd[ 4 ] = VTK_LARGE_FLOAT;
771   newbnd[ 1 ] = newbnd[ 3 ] = newbnd[ 5 ] = -VTK_LARGE_FLOAT;
772
773   myCubeAxes->GetBounds(bnd);
774
775   int aVisibleNum = myTrihedron->GetVisibleActorCount( myRenderer );
776   //if (aVisibleNum || theIsForcedUpdate) {
777   if (aVisibleNum) {
778     // if the new trihedron size have sufficient difference, then apply the value
779     double aNewSize = 100, anOldSize=myTrihedron->GetSize();
780     bool aTDisplayed = isTrihedronDisplayed();
781     bool aCDisplayed = isCubeAxesDisplayed();
782     if(aTDisplayed) myTrihedron->VisibilityOff();
783     if(aCDisplayed) myCubeAxes->VisibilityOff();
784
785     SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
786     QString aSetting = aResMgr->stringValue("Viewer", "TrihedronSize", "105");
787     static float aSizeInPercents = aSetting.toFloat();
788
789     //bool isComputeTrihedronSize =
790       ::ComputeTrihedronSize(myRenderer, aNewSize, anOldSize, aSizeInPercents);
791
792     myTrihedron->SetSize( aNewSize );
793
794     // iterate through displayed objects and set size if necessary
795     vtkActorCollection* anActors = getRenderer()->GetActors();
796     anActors->InitTraversal();
797     while (vtkActor* anActor = anActors->GetNextActor())
798     {
799       if (SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast( anActor ))
800       {
801         if (aSActor->IsResizable())
802           aSActor->SetSize( 0.5 * aNewSize );
803         if (aSActor->GetVisibility() && !aSActor->IsInfinitive()) {
804           float *abounds = aSActor->GetBounds();
805           if (abounds[0] > -VTK_LARGE_FLOAT && abounds[1] < VTK_LARGE_FLOAT &&
806               abounds[2] > -VTK_LARGE_FLOAT && abounds[3] < VTK_LARGE_FLOAT &&
807               abounds[4] > -VTK_LARGE_FLOAT && abounds[5] < VTK_LARGE_FLOAT)
808             for (int i = 0; i < 5; i = i + 2) {
809               if (abounds[i] < newbnd[i]) newbnd[i] = abounds[i];
810               if (abounds[i+1] > newbnd[i+1]) newbnd[i+1] = abounds[i+1];
811             }
812         }
813       }
814     }
815     if (aTDisplayed) myTrihedron->VisibilityOn();
816     if (aCDisplayed) myCubeAxes->VisibilityOn();
817     
818   } else {
819      double aSize = myTrihedron->GetSize();
820      newbnd[0] = newbnd[2] = newbnd[4] = 0;
821      newbnd[1] = newbnd[3] = newbnd[5] = aSize;
822   }
823   
824   if (newbnd[0] < VTK_LARGE_FLOAT && newbnd[2] < VTK_LARGE_FLOAT && newbnd[4] < VTK_LARGE_FLOAT &&
825       newbnd[1] >-VTK_LARGE_FLOAT && newbnd[3] >-VTK_LARGE_FLOAT && newbnd[5] >-VTK_LARGE_FLOAT) {
826     for(int i=0;i<6;i++) bnd[i] = newbnd[i];
827     myCubeAxes->SetBounds(bnd);
828   }
829   
830   myCubeAxes->SetBounds(bnd);
831
832   ::ResetCameraClippingRange(myRenderer);
833 }
834
835 //----------------------------------------------------------------------------
836 void
837 SVTK_ViewWindow
838 ::onAdjustTrihedron()
839 {   
840   AdjustTrihedrons( false );
841 }
842
843 void
844 SVTK_ViewWindow
845 ::onAdjustCubeAxes()
846 {   
847   AdjustTrihedrons(false);
848 }
849
850 #define INCREMENT_FOR_OP 10
851
852 //=======================================================================
853 // name    : onPanLeft
854 // Purpose : Performs incremental panning to the left
855 //=======================================================================
856 void
857 SVTK_ViewWindow
858 ::onPanLeft()
859 {
860   myRWInteractor->GetSInteractorStyle()->IncrementalPan( -INCREMENT_FOR_OP, 0 );
861 }
862
863 //=======================================================================
864 // name    : onPanRight
865 // Purpose : Performs incremental panning to the right
866 //=======================================================================
867 void
868 SVTK_ViewWindow
869 ::onPanRight()
870 {
871   myRWInteractor->GetSInteractorStyle()->IncrementalPan( INCREMENT_FOR_OP, 0 );
872 }
873
874 //=======================================================================
875 // name    : onPanUp
876 // Purpose : Performs incremental panning to the top
877 //=======================================================================
878 void
879 SVTK_ViewWindow
880 ::onPanUp()
881 {
882   myRWInteractor->GetSInteractorStyle()->IncrementalPan( 0, INCREMENT_FOR_OP );
883 }
884
885 //=======================================================================
886 // name    : onPanDown
887 // Purpose : Performs incremental panning to the bottom
888 //=======================================================================
889 void
890 SVTK_ViewWindow
891 ::onPanDown()
892 {
893   myRWInteractor->GetSInteractorStyle()->IncrementalPan( 0, -INCREMENT_FOR_OP );
894 }
895
896 //=======================================================================
897 // name    : onZoomIn
898 // Purpose : Performs incremental zooming in
899 //=======================================================================
900 void
901 SVTK_ViewWindow
902 ::onZoomIn()
903 {
904   myRWInteractor->GetSInteractorStyle()->IncrementalZoom( INCREMENT_FOR_OP );
905 }
906
907 //=======================================================================
908 // name    : onZoomOut
909 // Purpose : Performs incremental zooming out
910 //=======================================================================
911 void
912 SVTK_ViewWindow
913 ::onZoomOut()
914 {
915   myRWInteractor->GetSInteractorStyle()->IncrementalZoom( -INCREMENT_FOR_OP );
916 }
917
918 //=======================================================================
919 // name    : onRotateLeft
920 // Purpose : Performs incremental rotating to the left
921 //=======================================================================
922 void
923 SVTK_ViewWindow
924 ::onRotateLeft()
925 {
926   myRWInteractor->GetSInteractorStyle()->IncrementalRotate( -INCREMENT_FOR_OP, 0 );
927 }
928
929 //=======================================================================
930 // name    : onRotateRight
931 // Purpose : Performs incremental rotating to the right
932 //=======================================================================
933 void
934 SVTK_ViewWindow
935 ::onRotateRight()
936 {
937   myRWInteractor->GetSInteractorStyle()->IncrementalRotate( INCREMENT_FOR_OP, 0 );
938 }
939
940 //=======================================================================
941 // name    : onRotateUp
942 // Purpose : Performs incremental rotating to the top
943 //=======================================================================
944 void
945 SVTK_ViewWindow
946 ::onRotateUp()
947 {
948   myRWInteractor->GetSInteractorStyle()->IncrementalRotate( 0, -INCREMENT_FOR_OP );
949 }
950
951 //=======================================================================
952 void
953 SVTK_ViewWindow
954 ::onKeyPressed(QKeyEvent* event)
955 {
956   emit keyPressed( this, event );
957 }
958
959 //=======================================================================
960 void
961 SVTK_ViewWindow
962 ::onKeyReleased(QKeyEvent* event)
963 {
964   emit keyReleased( this, event );
965 }
966
967 //=======================================================================
968 void
969 SVTK_ViewWindow
970 ::onMousePressed(QMouseEvent* event)
971 {
972   emit mousePressed(this, event);
973 }
974
975 //=======================================================================
976 void
977 SVTK_ViewWindow
978 ::onMouseReleased(QMouseEvent* event)
979 {
980   emit mouseReleased( this, event );
981 }
982
983 //=======================================================================
984 void
985 SVTK_ViewWindow
986 ::onMouseMoving(QMouseEvent* event)
987 {
988   emit mouseMoving( this, event );
989 }
990
991 //=======================================================================
992 void
993 SVTK_ViewWindow
994 ::onMouseDoubleClicked( QMouseEvent* event )
995 {
996   emit mouseDoubleClicked( this, event );
997 }
998
999 //=======================================================================
1000 // name    : onRotateDown
1001 // Purpose : Performs incremental rotating to the bottom
1002 //=======================================================================
1003 void
1004 SVTK_ViewWindow
1005 ::onRotateDown()
1006 {
1007   myRWInteractor->GetSInteractorStyle()->IncrementalRotate( 0, INCREMENT_FOR_OP );
1008 }
1009
1010 //----------------------------------------------------------------------------
1011 void
1012 SVTK_ViewWindow
1013 ::InsertActor( SALOME_Actor* theActor, bool theMoveInternalActors )
1014 {
1015   theActor->AddToRender(myRenderer);
1016   theActor->SetTransform(myTransform);
1017   if(theMoveInternalActors) 
1018     myRWInteractor->MoveInternalActors();
1019 }
1020
1021 //----------------------------------------------------------------------------
1022 void
1023 SVTK_ViewWindow
1024 ::AddActor( SALOME_Actor* theActor, bool theUpdate /*=false*/ )
1025 {
1026   InsertActor(theActor);
1027   if(theUpdate) 
1028     Repaint();
1029 }
1030
1031 //----------------------------------------------------------------------------
1032 void
1033 SVTK_ViewWindow
1034 ::RemoveActor( SALOME_Actor* theActor, bool theUpdate /*=false*/ )
1035 {
1036   theActor->RemoveFromRender(myRenderer);
1037   if(theUpdate) 
1038     Repaint();
1039 }
1040
1041 //----------------------------------------------------------------------------
1042 void
1043 SVTK_ViewWindow
1044 ::MoveActor( SALOME_Actor* theActor)
1045 {
1046   RemoveActor(theActor);
1047   InsertActor(theActor,true);
1048 }
1049
1050 //----------------------------------------------------------------------------
1051 QImage
1052 SVTK_ViewWindow
1053 ::dumpView()
1054 {
1055   QPixmap px = QPixmap::grabWindow( myRenderWindow->winId() );
1056   return px.convertToImage();
1057 }
1058
1059 //----------------------------------------------------------------------------
1060 void 
1061 SVTK_ViewWindow
1062 ::action( const int theAction  )
1063 {
1064   switch ( theAction ) {
1065   case SUIT_Accel::PanLeft     : onPanLeft();     break;
1066   case SUIT_Accel::PanRight    : onPanRight();    break;
1067   case SUIT_Accel::PanUp       : onPanUp();       break;
1068   case SUIT_Accel::PanDown     : onPanDown();     break;
1069   case SUIT_Accel::ZoomIn      : onZoomIn();      break;
1070   case SUIT_Accel::ZoomOut     : onZoomOut();     break;
1071   case SUIT_Accel::ZoomFit     : onFitAll();      break;
1072   case SUIT_Accel::RotateLeft  : onRotateLeft();  break;
1073   case SUIT_Accel::RotateRight : onRotateRight(); break;
1074   case SUIT_Accel::RotateUp    : onRotateUp();    break;
1075   case SUIT_Accel::RotateDown  : onRotateDown();  break;
1076   }   
1077 }