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