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