Salome HOME
To right initialization of SVTK_RenderWindowInteractor
[modules/gui.git] / src / SVTK / SVTK_ViewWindow.cxx
1 #include "SVTK_Selector.h"
2 #include "SALOME_Actor.h"
3
4 #include <qapplication.h>
5
6 #include <vtkActorCollection.h>
7 #include <vtkRenderWindow.h>
8 #include <vtkRenderer.h>
9 #include <vtkCamera.h>
10
11 #include "QtxAction.h"
12
13 #include "SUIT_Session.h"
14 #include "SUIT_ToolButton.h"
15 #include "SUIT_MessageBox.h"
16
17 #include "SUIT_Tools.h"
18 #include "SUIT_ResourceMgr.h"
19 #include "SUIT_FileDlg.h"
20
21 #include "VTKViewer_Transform.h"
22 #include "VTKViewer_Utilities.h"
23
24 #include "SVTK_Trihedron.h"
25 #include "SVTK_ViewWindow.h"
26 #include "SVTK_ViewModel.h"
27 #include "SVTK_RenderWindow.h"
28 #include "SVTK_RenderWindowInteractor.h"
29 #include "SVTK_InteractorStyle.h"
30
31 #include "SALOME_ListIteratorOfListIO.hxx"
32
33 #include "VTKViewer_Algorithm.h"
34 #include "SVTK_Functor.h"
35
36 //----------------------------------------------------------------------------
37 SVTK_ViewWindow
38 ::SVTK_ViewWindow( SUIT_Desktop* theDesktop, 
39                    SVTK_Viewer* theModel)
40   : SUIT_ViewWindow(theDesktop)
41 {
42   myModel = theModel;
43   mySelector = new SVTK_Selector();
44   connect(this,SIGNAL(selectionChanged()),theModel,SLOT(onSelectionChanged()));
45
46   myTransform = VTKViewer_Transform::New();
47   myTrihedron = SVTK_Trihedron::New();
48   myRenderer  = vtkRenderer::New() ;
49
50   myTrihedron->AddToRender( myRenderer );
51
52   myRenderWindow = new SVTK_RenderWindow( this, "RenderWindow" );
53   setCentralWidget(myRenderWindow);
54   myRenderWindow->setFocusPolicy( StrongFocus );
55   myRenderWindow->setFocus();
56
57   myRenderWindow->getRenderWindow()->AddRenderer( myRenderer );
58
59   myRenderer->GetActiveCamera()->ParallelProjectionOn();
60   myRenderer->LightFollowCameraOn();
61   myRenderer->TwoSidedLightingOn();
62
63   // Set BackgroundColor
64   QString BgrColorRed   = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorRed");
65   QString BgrColorGreen = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorGreen");
66   QString BgrColorBlue  = "0";//SUIT_CONFIG->getSetting("VTKViewer:BackgroundColorBlue");
67
68   if( !BgrColorRed.isEmpty() && !BgrColorGreen.isEmpty() && !BgrColorBlue.isEmpty() ) 
69     myRenderer->SetBackground( BgrColorRed.toInt()/255., BgrColorGreen.toInt()/255., BgrColorBlue.toInt()/255. );
70   else
71     myRenderer->SetBackground( 0, 0, 0 );
72   
73   // Create an interactor.
74   myRWInteractor = SVTK_RenderWindowInteractor::New();
75   myRWInteractor->SetRenderWindow( myRenderWindow->getRenderWindow() );
76   myRWInteractor->setViewWindow( this );
77
78   SVTK_InteractorStyle* RWS = SVTK_InteractorStyle::New();
79   RWS->setGUIWindow( myRenderWindow );
80   RWS->setViewWindow( this );
81
82   myRWInteractor->SetInteractorStyle( RWS ); 
83   myRWInteractor->Initialize();
84
85   RWS->setTriedron( myTrihedron );
86   RWS->FindPokedRenderer( 0, 0 );
87
88   SetSelectionMode(ActorSelection);
89
90   setCentralWidget( myRenderWindow );
91
92   myToolBar = new QToolBar(this);
93   myToolBar->setCloseMode(QDockWindow::Undocked);
94   myToolBar->setLabel(tr("LBL_TOOLBAR_LABEL"));
95
96   createActions();
97   createToolBar();
98
99   connect( myRenderWindow, SIGNAL(KeyPressed( QKeyEvent* )),
100            this,           SLOT(onKeyPressed( QKeyEvent* )) );
101   connect( myRenderWindow, SIGNAL(KeyReleased( QKeyEvent* )),
102            this,           SLOT(onKeyReleased( QKeyEvent* )) );
103   connect( myRenderWindow, SIGNAL(MouseButtonPressed( QMouseEvent* )),
104            this,           SLOT(onMousePressed( QMouseEvent* )) );
105   connect( myRenderWindow, SIGNAL(MouseButtonReleased( QMouseEvent* )),
106            this,           SLOT(onMouseReleased( QMouseEvent* )) );
107   connect( myRenderWindow, SIGNAL(MouseDoubleClicked( QMouseEvent* )),
108            this,           SLOT(onMouseDoubleClicked( QMouseEvent* )) );
109   connect( myRenderWindow, SIGNAL(MouseMove( QMouseEvent* )),
110            this,           SLOT(onMouseMoving( QMouseEvent* )) );
111
112   connect( myRWInteractor, SIGNAL(RenderWindowModified()),
113            myRenderWindow, SLOT(update()) );
114   connect( myRWInteractor, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
115            this,           SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
116
117   onResetView();
118 }
119
120 //----------------------------------------------------------------------------
121 SVTK_ViewWindow
122 ::~SVTK_ViewWindow()
123 {
124   myTransform->Delete();
125   // In order to ensure that the interactor unregisters
126   // this RenderWindow, we assign a NULL RenderWindow to 
127   // it before deleting it.
128   myRWInteractor->SetRenderWindow( NULL );
129   myRWInteractor->Delete();
130   
131   //m_RW->Delete() ;
132   myRenderer->RemoveAllProps();
133   //m_Renderer->Delete() ;
134   myTrihedron->Delete();
135 }
136
137 //----------------------------------------------------------------------------
138 void
139 SVTK_ViewWindow
140 ::activateZoom()
141 {
142   myRWInteractor->GetSInteractorStyle()->startZoom();
143 }
144
145 //----------------------------------------------------------------------------
146 void
147 SVTK_ViewWindow
148 ::activatePanning()
149 {
150   myRWInteractor->GetSInteractorStyle()->startPan();
151 }
152
153 //----------------------------------------------------------------------------
154 void
155 SVTK_ViewWindow
156 ::activateRotation()
157 {
158   myRWInteractor->GetSInteractorStyle()->startRotate();
159 }
160
161 //----------------------------------------------------------------------------
162 void
163 SVTK_ViewWindow
164 ::activateGlobalPanning()
165 {
166   if(myTrihedron->GetVisibleActorCount(myRenderer))
167     myRWInteractor->GetSInteractorStyle()->startGlobalPan();
168 }
169
170 //----------------------------------------------------------------------------
171 void
172 SVTK_ViewWindow
173 ::activateWindowFit()
174 {
175   myRWInteractor->GetSInteractorStyle()->startFitArea();
176 }
177
178 //----------------------------------------------------------------------------
179 void
180 SVTK_ViewWindow
181 ::createActions()
182 {
183   if (!myActionsMap.isEmpty()) return;
184   
185   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
186   
187   QtxAction* aAction;
188
189   // Dump view
190   aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_DUMP" ) ),
191                            tr( "MNU_DUMP_VIEW" ), 0, this);
192   aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
193   connect(aAction, SIGNAL(activated()), this, SLOT(onDumpView()));
194   myActionsMap[ DumpId ] = aAction;
195
196   // FitAll
197   aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITALL" ) ),
198                            tr( "MNU_FITALL" ), 0, this);
199   aAction->setStatusTip(tr("DSC_FITALL"));
200   connect(aAction, SIGNAL(activated()), this, SLOT(onFitAll()));
201   myActionsMap[ FitAllId ] = aAction;
202
203   // FitRect
204   aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITAREA" ) ),
205                            tr( "MNU_FITRECT" ), 0, this);
206   aAction->setStatusTip(tr("DSC_FITRECT"));
207   connect(aAction, SIGNAL(activated()), this, SLOT(activateWindowFit()));
208   myActionsMap[ FitRectId ] = aAction;
209
210   // Zoom
211   aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ZOOM" ) ),
212                            tr( "MNU_ZOOM_VIEW" ), 0, this);
213   aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
214   connect(aAction, SIGNAL(activated()), this, SLOT(activateZoom()));
215   myActionsMap[ ZoomId ] = aAction;
216
217   // Panning
218   aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_PAN" ) ),
219                            tr( "MNU_PAN_VIEW" ), 0, this);
220   aAction->setStatusTip(tr("DSC_PAN_VIEW"));
221   connect(aAction, SIGNAL(activated()), this, SLOT(activatePanning()));
222   myActionsMap[ PanId ] = aAction;
223
224   // Global Panning
225   aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_GLOBALPAN" ) ),
226                            tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
227   aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
228   connect(aAction, SIGNAL(activated()), this, SLOT(activateGlobalPanning()));
229   myActionsMap[ GlobalPanId ] = aAction;
230
231   // Rotation
232   aAction = new QtxAction(tr("MNU_ROTATE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ROTATE" ) ),
233                            tr( "MNU_ROTATE_VIEW" ), 0, this);
234   aAction->setStatusTip(tr("DSC_ROTATE_VIEW"));
235   connect(aAction, SIGNAL(activated()), this, SLOT(activateRotation()));
236   myActionsMap[ RotationId ] = aAction;
237
238   // Projections
239   aAction = new QtxAction(tr("MNU_FRONT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FRONT" ) ),
240                            tr( "MNU_FRONT_VIEW" ), 0, this);
241   aAction->setStatusTip(tr("DSC_FRONT_VIEW"));
242   connect(aAction, SIGNAL(activated()), this, SLOT(onFrontView()));
243   myActionsMap[ FrontId ] = aAction;
244
245   aAction = new QtxAction(tr("MNU_BACK_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BACK" ) ),
246                            tr( "MNU_BACK_VIEW" ), 0, this);
247   aAction->setStatusTip(tr("DSC_BACK_VIEW"));
248   connect(aAction, SIGNAL(activated()), this, SLOT(onBackView()));
249   myActionsMap[ BackId ] = aAction;
250
251   aAction = new QtxAction(tr("MNU_TOP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TOP" ) ),
252                            tr( "MNU_TOP_VIEW" ), 0, this);
253   aAction->setStatusTip(tr("DSC_TOP_VIEW"));
254   connect(aAction, SIGNAL(activated()), this, SLOT(onTopView()));
255   myActionsMap[ TopId ] = aAction;
256
257   aAction = new QtxAction(tr("MNU_BOTTOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BOTTOM" ) ),
258                            tr( "MNU_BOTTOM_VIEW" ), 0, this);
259   aAction->setStatusTip(tr("DSC_BOTTOM_VIEW"));
260   connect(aAction, SIGNAL(activated()), this, SLOT(onBottomView()));
261   myActionsMap[ BottomId ] = aAction;
262
263   aAction = new QtxAction(tr("MNU_LEFT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_LEFT" ) ),
264                            tr( "MNU_LEFT_VIEW" ), 0, this);
265   aAction->setStatusTip(tr("DSC_LEFT_VIEW"));
266   connect(aAction, SIGNAL(activated()), this, SLOT(onLeftView()));
267   myActionsMap[ LeftId ] = aAction;
268
269   aAction = new QtxAction(tr("MNU_RIGHT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RIGHT" ) ),
270                            tr( "MNU_RIGHT_VIEW" ), 0, this);
271   aAction->setStatusTip(tr("DSC_RIGHT_VIEW"));
272   connect(aAction, SIGNAL(activated()), this, SLOT(onRightView()));
273   myActionsMap[ RightId ] = aAction;
274
275   // Reset
276   aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RESET" ) ),
277                            tr( "MNU_RESET_VIEW" ), 0, this);
278   aAction->setStatusTip(tr("DSC_RESET_VIEW"));
279   connect(aAction, SIGNAL(activated()), this, SLOT(onResetView()));
280   myActionsMap[ ResetId ] = aAction;
281 }
282
283 //----------------------------------------------------------------------------
284 void
285 SVTK_ViewWindow
286 ::createToolBar()
287 {
288   myActionsMap[DumpId]->addTo(myToolBar);
289
290   SUIT_ToolButton* aScaleBtn = new SUIT_ToolButton(myToolBar);
291   aScaleBtn->AddAction(myActionsMap[FitAllId]);
292   aScaleBtn->AddAction(myActionsMap[FitRectId]);
293   aScaleBtn->AddAction(myActionsMap[ZoomId]);
294
295   SUIT_ToolButton* aPanningBtn = new SUIT_ToolButton(myToolBar);
296   aPanningBtn->AddAction(myActionsMap[PanId]);
297   aPanningBtn->AddAction(myActionsMap[GlobalPanId]);
298
299   myActionsMap[RotationId]->addTo(myToolBar);
300
301   SUIT_ToolButton* aViewsBtn = new SUIT_ToolButton(myToolBar);
302   aViewsBtn->AddAction(myActionsMap[FrontId]);
303   aViewsBtn->AddAction(myActionsMap[BackId]);
304   aViewsBtn->AddAction(myActionsMap[TopId]);
305   aViewsBtn->AddAction(myActionsMap[BottomId]);
306   aViewsBtn->AddAction(myActionsMap[LeftId]);
307   aViewsBtn->AddAction(myActionsMap[RightId]);
308
309   myActionsMap[ResetId]->addTo(myToolBar);
310 }
311
312 //----------------------------------------------------------------------------
313 void
314 SVTK_ViewWindow
315 ::onFrontView()
316 {
317   vtkCamera* camera = myRenderer->GetActiveCamera();
318   camera->SetPosition(1,0,0);
319   camera->SetViewUp(0,0,1);
320   camera->SetFocalPoint(0,0,0);
321   onFitAll();
322 }
323
324 //----------------------------------------------------------------------------
325 void
326 SVTK_ViewWindow
327 ::onBackView()
328 {
329   vtkCamera* camera = myRenderer->GetActiveCamera();
330   camera->SetPosition(-1,0,0);
331   camera->SetViewUp(0,0,1);
332   camera->SetFocalPoint(0,0,0);
333   onFitAll();
334 }
335
336 //----------------------------------------------------------------------------
337 void
338 SVTK_ViewWindow
339 ::onTopView()
340 {
341   vtkCamera* camera = myRenderer->GetActiveCamera();
342   camera->SetPosition(0,0,1);
343   camera->SetViewUp(0,1,0);
344   camera->SetFocalPoint(0,0,0);
345   onFitAll();
346 }
347
348 //----------------------------------------------------------------------------
349 void
350 SVTK_ViewWindow
351 ::onBottomView()
352 {
353   vtkCamera* camera = myRenderer->GetActiveCamera();
354   camera->SetPosition(0,0,-1);
355   camera->SetViewUp(0,1,0);
356   camera->SetFocalPoint(0,0,0);
357   onFitAll();
358 }
359
360 //----------------------------------------------------------------------------
361 void
362 SVTK_ViewWindow
363 ::onLeftView()
364 {
365   vtkCamera* camera = myRenderer->GetActiveCamera(); 
366   camera->SetPosition(0,-1,0);
367   camera->SetViewUp(0,0,1);
368   camera->SetFocalPoint(0,0,0);
369   onFitAll();
370 }
371
372 //----------------------------------------------------------------------------
373 void
374 SVTK_ViewWindow
375 ::onRightView()
376 {
377   vtkCamera* camera = myRenderer->GetActiveCamera();
378   camera->SetPosition(0,1,0);
379   camera->SetViewUp(0,0,1);
380   camera->SetFocalPoint(0,0,0);
381   onFitAll();
382 }
383
384 //----------------------------------------------------------------------------
385 void
386 SVTK_ViewWindow
387 ::onResetView()
388 {
389   int aTrihedronIsVisible = isTrihedronDisplayed();
390   myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
391   ::ResetCamera(myRenderer,true);  
392   vtkCamera* aCamera = myRenderer->GetActiveCamera();
393   aCamera->SetPosition(1,-1,1);
394   aCamera->SetViewUp(0,0,1);
395   ::ResetCamera(myRenderer,true);  
396   if(aTrihedronIsVisible) myTrihedron->VisibilityOn();
397   else myTrihedron->VisibilityOff();
398   static float aCoeff = 3.0;
399   aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
400   Repaint();
401 }
402
403 //----------------------------------------------------------------------------
404 void
405 SVTK_ViewWindow
406 ::onFitAll()
407 {
408   myRWInteractor->GetSInteractorStyle()->ViewFitAll();
409   Repaint();
410 }
411
412 //----------------------------------------------------------------------------
413 void
414 SVTK_ViewWindow
415 ::onDumpView()
416 {
417   QApplication::setOverrideCursor( Qt::waitCursor );
418   QPixmap px = QPixmap::grabWindow(myRenderWindow->winId());
419   QApplication::restoreOverrideCursor();
420   
421   QString fileName = SUIT_FileDlg::getFileName(this,
422                                               QString::null,
423                                               tr("VTK_IMAGE_FILES"),
424                                               tr("INF_APP_DUMP_VIEW"),
425                                               false);
426   if (!fileName.isNull()) {
427     QApplication::setOverrideCursor( Qt::waitCursor );
428     QString fmt = SUIT_Tools::extension( fileName ).upper();
429     if (fmt.isEmpty())
430       fmt = QString("BMP"); // default format
431     if (fmt == "JPG")
432       fmt = "JPEG";
433     bool bOk = px.save(fileName, fmt.latin1());
434     QApplication::restoreOverrideCursor();
435     if (!bOk) {
436       SUIT_MessageBox::error1(this, tr("ERROR"), tr("ERR_DOC_CANT_SAVE_FILE"), tr("BUT_OK"));
437     }
438   }
439 }
440
441 //----------------------------------------------------------------
442 void
443 SVTK_ViewWindow
444 ::onSelectionChanged()
445 {
446   unHighlightAll();
447
448   const SALOME_ListIO& aListIO = mySelector->StoredIObjects();
449   SALOME_ListIteratorOfListIO anIter(aListIO);
450   for(; anIter.More(); anIter.Next()){
451     highlight(anIter.Value(),true,!anIter.More());
452   }
453
454   emit selectionChanged();
455 }
456
457 //----------------------------------------------------------------
458 void
459 SVTK_ViewWindow
460 ::SetSelectionMode(Selection_Mode theMode)
461 {
462   mySelector->SetSelectionMode(theMode);
463   myRWInteractor->SetSelectionMode(theMode);
464 }
465
466 //----------------------------------------------------------------
467 Selection_Mode
468 SVTK_ViewWindow
469 ::SelectionMode() const
470 {
471   return mySelector->SelectionMode();
472 }
473
474 //----------------------------------------------------------------
475 void 
476 SVTK_ViewWindow
477 ::unHighlightAll() 
478 {
479   myRWInteractor->unHighlightAll();
480 }
481
482 //----------------------------------------------------------------
483 void
484 SVTK_ViewWindow
485 ::highlight( const Handle(SALOME_InteractiveObject)& theIO, 
486              bool theIsHighlight, 
487              bool theIsUpdate ) 
488 {
489   myRWInteractor->highlight(theIO, theIsHighlight, theIsUpdate);
490
491   if(mySelector->HasIndex(theIO) && theIO->hasEntry()){
492     TColStd_IndexedMapOfInteger aMapIndex;
493     mySelector->GetIndex(theIO,aMapIndex);
494     using namespace VTK;
495     const char* anEntry = theIO->getEntry();
496     vtkActorCollection* aCollection = myRenderer->GetActors();
497     if(SALOME_Actor* anActor = Find<SALOME_Actor>(aCollection,TIsSameEntry<SALOME_Actor>(anEntry))){
498       switch (mySelector->SelectionMode()) {
499       case NodeSelection:
500         myRWInteractor->highlightPoint(aMapIndex,anActor,theIsHighlight,theIsUpdate);
501         break;
502       case EdgeOfCellSelection:
503         myRWInteractor->highlightEdge(aMapIndex,anActor,theIsHighlight,theIsUpdate);
504         break;
505       case CellSelection:
506       case EdgeSelection:
507       case FaceSelection:
508       case VolumeSelection:
509         myRWInteractor->highlightCell(aMapIndex,anActor,theIsHighlight,theIsUpdate);
510         break;
511       }
512     }
513   }else{
514     myRWInteractor->unHighlightSubSelection();
515   }
516 }
517
518 //----------------------------------------------------------------
519 bool
520 SVTK_ViewWindow
521 ::isInViewer( const Handle(SALOME_InteractiveObject)& theIO ) 
522 {
523   return myRWInteractor->isInViewer( theIO );
524 }
525
526 //----------------------------------------------------------------
527 bool
528 SVTK_ViewWindow
529 ::isVisible( const Handle(SALOME_InteractiveObject)& theIO ) 
530 {
531   return myRWInteractor->isVisible( theIO );
532 }
533
534 //----------------------------------------------------------------------------
535 void
536 SVTK_ViewWindow
537 ::setBackgroundColor( const QColor& color )
538 {
539   if ( myRenderer )
540     myRenderer->SetBackground( color.red()/255., color.green()/255., color.blue()/255. );
541 }
542
543 //----------------------------------------------------------------------------
544 QColor
545 SVTK_ViewWindow
546 ::backgroundColor() const
547 {
548   float backint[3];
549   if ( myRenderer ) {
550     myRenderer->GetBackground( backint );
551     return QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255));
552   }
553   return SUIT_ViewWindow::backgroundColor();
554 }
555
556 //----------------------------------------------------------------------------
557 void
558 SVTK_ViewWindow
559 ::Repaint(bool theUpdateTrihedron)
560 {
561   if (theUpdateTrihedron) 
562     onAdjustTrihedron();
563   myRenderWindow->update();
564 }
565
566 //----------------------------------------------------------------------------
567 void
568 SVTK_ViewWindow
569 ::GetScale( double theScale[3] ) 
570 {
571   myTransform->GetScale( theScale );
572 }
573
574 //----------------------------------------------------------------------------
575 void
576 SVTK_ViewWindow
577 ::SetScale( double theScale[3] ) 
578 {
579   myTransform->SetScale( theScale[0], theScale[1], theScale[2] );
580   myRWInteractor->Render();
581   Repaint();
582 }
583
584 //----------------------------------------------------------------------------
585 bool
586 SVTK_ViewWindow
587 ::isTrihedronDisplayed()
588 {
589   return myTrihedron->GetVisibility() == VTKViewer_Trihedron::eOn;
590 }
591
592 //----------------------------------------------------------------------------
593 void 
594 SVTK_ViewWindow
595 ::onViewTrihedron()
596 {
597   if(!myTrihedron) 
598     return;
599
600   if(isTrihedronDisplayed())
601     myTrihedron->VisibilityOff();
602   else
603     myTrihedron->VisibilityOn();
604
605   Repaint();
606 }
607
608 //----------------------------------------------------------------------------
609 bool
610 SVTK_ViewWindow
611 ::ComputeTrihedronSize( double& theNewSize, double& theSize )
612 {
613   // calculating diagonal of visible props of the renderer
614   float aBndBox[ 6 ];
615   myTrihedron->VisibilityOff();
616
617   if ( ::ComputeVisiblePropBounds( myRenderer, aBndBox ) == 0 ) {
618     aBndBox[ 1 ] = aBndBox[ 3 ] = aBndBox[ 5 ] = 100;
619     aBndBox[ 0 ] = aBndBox[ 2 ] = aBndBox[ 4 ] = 0;
620   }
621
622   myTrihedron->VisibilityOn();
623   float aLength = 0;
624   static bool aCalcByDiag = false;
625   if ( aCalcByDiag ) {
626     aLength = sqrt( ( aBndBox[1]-aBndBox[0])*(aBndBox[1]-aBndBox[0] )+
627                     ( aBndBox[3]-aBndBox[2])*(aBndBox[3]-aBndBox[2] )+
628                     ( aBndBox[5]-aBndBox[4])*(aBndBox[5]-aBndBox[4] ) );
629   } else {
630     aLength = aBndBox[ 1 ]-aBndBox[ 0 ];
631     aLength = max( ( aBndBox[ 3 ] - aBndBox[ 2 ] ),aLength );
632     aLength = max( ( aBndBox[ 5 ] - aBndBox[ 4 ] ),aLength );
633   }
634
635   static float aSizeInPercents = 105;
636   //QString aSetting = QAD_CONFIG->getSetting( "Viewer:TrihedronSize" );
637   //if ( !aSetting.isEmpty() )
638   //  aSizeInPercents = aSetting.toFloat();
639
640   static float EPS_SIZE = 5.0E-3;
641   theSize = myTrihedron->GetSize();
642   theNewSize = aLength * aSizeInPercents / 100.0;
643
644   // if the new trihedron size have sufficient difference, then apply the value
645   return fabs( theNewSize - theSize) > theSize * EPS_SIZE ||
646          fabs( theNewSize-theSize ) > theNewSize * EPS_SIZE;
647 }
648
649 //----------------------------------------------------------------------------
650 double
651 SVTK_ViewWindow
652 ::GetTrihedronSize() const
653 {
654   return myTrihedron->GetSize();
655 }
656
657 //----------------------------------------------------------------------------
658 void
659 SVTK_ViewWindow
660 ::AdjustTrihedrons( const bool theIsForcedUpdate )
661 {
662   if ( !isTrihedronDisplayed() && !theIsForcedUpdate )
663     return;
664
665   int aVisibleNum = myTrihedron->GetVisibleActorCount( myRenderer );
666   if ( aVisibleNum || theIsForcedUpdate ) {
667     // if the new trihedron size have sufficient difference, then apply the value
668     double aNewSize = 100, anOldSize;
669     if ( ComputeTrihedronSize( aNewSize, anOldSize ) || theIsForcedUpdate ) {
670       myTrihedron->SetSize( aNewSize );
671       // itearte throuh displayed objects and set size if necessary
672       
673       vtkActorCollection* anActors = getRenderer()->GetActors();
674       anActors->InitTraversal();
675       while( vtkActor* anActor = anActors->GetNextActor() ) {
676         if( SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast( anActor ) ) {
677           if ( aSActor->IsResizable() )
678             aSActor->SetSize( 0.5 * aNewSize );
679         }
680       }
681     }
682   }
683
684   ::ResetCameraClippingRange(myRenderer);
685 }
686
687 //----------------------------------------------------------------------------
688 void
689 SVTK_ViewWindow
690 ::onAdjustTrihedron()
691 {   
692   AdjustTrihedrons( false );
693 }
694
695 #define INCREMENT_FOR_OP 10
696
697 //=======================================================================
698 // name    : onPanLeft
699 // Purpose : Performs incremental panning to the left
700 //=======================================================================
701 void
702 SVTK_ViewWindow
703 ::onPanLeft()
704 {
705   myRWInteractor->GetSInteractorStyle()->IncrementalPan( -INCREMENT_FOR_OP, 0 );
706 }
707
708 //=======================================================================
709 // name    : onPanRight
710 // Purpose : Performs incremental panning to the right
711 //=======================================================================
712 void
713 SVTK_ViewWindow
714 ::onPanRight()
715 {
716   myRWInteractor->GetSInteractorStyle()->IncrementalPan( INCREMENT_FOR_OP, 0 );
717 }
718
719 //=======================================================================
720 // name    : onPanUp
721 // Purpose : Performs incremental panning to the top
722 //=======================================================================
723 void
724 SVTK_ViewWindow
725 ::onPanUp()
726 {
727   myRWInteractor->GetSInteractorStyle()->IncrementalPan( 0, INCREMENT_FOR_OP );
728 }
729
730 //=======================================================================
731 // name    : onPanDown
732 // Purpose : Performs incremental panning to the bottom
733 //=======================================================================
734 void
735 SVTK_ViewWindow
736 ::onPanDown()
737 {
738   myRWInteractor->GetSInteractorStyle()->IncrementalPan( 0, -INCREMENT_FOR_OP );
739 }
740
741 //=======================================================================
742 // name    : onZoomIn
743 // Purpose : Performs incremental zooming in
744 //=======================================================================
745 void
746 SVTK_ViewWindow
747 ::onZoomIn()
748 {
749   myRWInteractor->GetSInteractorStyle()->IncrementalZoom( INCREMENT_FOR_OP );
750 }
751
752 //=======================================================================
753 // name    : onZoomOut
754 // Purpose : Performs incremental zooming out
755 //=======================================================================
756 void
757 SVTK_ViewWindow
758 ::onZoomOut()
759 {
760   myRWInteractor->GetSInteractorStyle()->IncrementalZoom( -INCREMENT_FOR_OP );
761 }
762
763 //=======================================================================
764 // name    : onRotateLeft
765 // Purpose : Performs incremental rotating to the left
766 //=======================================================================
767 void
768 SVTK_ViewWindow
769 ::onRotateLeft()
770 {
771   myRWInteractor->GetSInteractorStyle()->IncrementalRotate( -INCREMENT_FOR_OP, 0 );
772 }
773
774 //=======================================================================
775 // name    : onRotateRight
776 // Purpose : Performs incremental rotating to the right
777 //=======================================================================
778 void
779 SVTK_ViewWindow
780 ::onRotateRight()
781 {
782   myRWInteractor->GetSInteractorStyle()->IncrementalRotate( INCREMENT_FOR_OP, 0 );
783 }
784
785 //=======================================================================
786 // name    : onRotateUp
787 // Purpose : Performs incremental rotating to the top
788 //=======================================================================
789 void
790 SVTK_ViewWindow
791 ::onRotateUp()
792 {
793   myRWInteractor->GetSInteractorStyle()->IncrementalRotate( 0, -INCREMENT_FOR_OP );
794 }
795
796 //=======================================================================
797 void
798 SVTK_ViewWindow
799 ::onKeyPressed(QKeyEvent* event)
800 {
801   emit keyPressed( this, event );
802 }
803
804 //=======================================================================
805 void
806 SVTK_ViewWindow
807 ::onKeyReleased(QKeyEvent* event)
808 {
809   emit keyReleased( this, event );
810 }
811
812 //=======================================================================
813 void
814 SVTK_ViewWindow
815 ::onMousePressed(QMouseEvent* event)
816 {
817   emit mousePressed(this, event);
818 }
819
820 //=======================================================================
821 void
822 SVTK_ViewWindow
823 ::onMouseReleased(QMouseEvent* event)
824 {
825   emit mouseReleased( this, event );
826 }
827
828 //=======================================================================
829 void
830 SVTK_ViewWindow
831 ::onMouseMoving(QMouseEvent* event)
832 {
833   emit mouseMoving( this, event );
834 }
835
836 //=======================================================================
837 void
838 SVTK_ViewWindow
839 ::onMouseDoubleClicked( QMouseEvent* event )
840 {
841   emit mouseDoubleClicked( this, event );
842 }
843
844 //=======================================================================
845 // name    : onRotateDown
846 // Purpose : Performs incremental rotating to the bottom
847 //=======================================================================
848 void
849 SVTK_ViewWindow
850 ::onRotateDown()
851 {
852   myRWInteractor->GetSInteractorStyle()->IncrementalRotate( 0, INCREMENT_FOR_OP );
853 }
854
855 //----------------------------------------------------------------------------
856 void
857 SVTK_ViewWindow
858 ::InsertActor( SALOME_Actor* theActor, bool theMoveInternalActors )
859 {
860   theActor->AddToRender(myRenderer);
861   theActor->SetTransform(myTransform);
862   if(theMoveInternalActors) 
863     myRWInteractor->MoveInternalActors();
864 }
865
866 //----------------------------------------------------------------------------
867 void
868 SVTK_ViewWindow
869 ::AddActor( SALOME_Actor* theActor, bool theUpdate /*=false*/ )
870 {
871   InsertActor(theActor);
872   if(theUpdate) 
873     Repaint();
874 }
875
876 //----------------------------------------------------------------------------
877 void
878 SVTK_ViewWindow
879 ::RemoveActor( SALOME_Actor* theActor, bool theUpdate /*=false*/ )
880 {
881   theActor->RemoveFromRender(myRenderer);
882   if(theUpdate) 
883     Repaint();
884 }
885
886 //----------------------------------------------------------------------------
887 void
888 SVTK_ViewWindow
889 ::MoveActor( SALOME_Actor* theActor)
890 {
891   RemoveActor(theActor);
892   InsertActor(theActor,true);
893 }