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