]> SALOME platform Git repositories - modules/gui.git/blob - src/SVTK/SVTK_ViewWindow.cxx
Salome HOME
dace6f30e893fc531d3506b8731c9b06d4e9e68e
[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
19 #include "VTKViewer_Transform.h"
20 #include "VTKViewer_Utilities.h"
21
22 #include "SVTK_Trihedron.h"
23 #include "SVTK_ViewWindow.h"
24 #include "SVTK_ViewModel.h"
25 #include "SVTK_RenderWindow.h"
26 #include "SVTK_RenderWindowInteractor.h"
27 #include "SVTK_InteractorStyle.h"
28
29 #include "SALOME_ListIteratorOfListIO.hxx"
30
31 #include "SVTK_SelectorDef.h"
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_SelectorDef();
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   // onViewTrihedron: Shows - Hides Trihedron
283   aAction = new QtxAction(tr("MNU_SHOW_TRIHEDRON"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TRIHEDRON" ) ),
284                            tr( "MNU_SHOW_TRIHEDRON" ), 0, this);
285   aAction->setStatusTip(tr("DSC_SHOW_TRIHEDRON"));
286   connect(aAction, SIGNAL(activated()), this, SLOT(onViewTrihedron()));
287   myActionsMap[ ViewTrihedronId ] = aAction;
288 }
289
290 //----------------------------------------------------------------------------
291 void
292 SVTK_ViewWindow
293 ::createToolBar()
294 {
295   myActionsMap[DumpId]->addTo(myToolBar);
296   myActionsMap[ViewTrihedronId]->addTo(myToolBar);
297
298   SUIT_ToolButton* aScaleBtn = new SUIT_ToolButton(myToolBar);
299   aScaleBtn->AddAction(myActionsMap[FitAllId]);
300   aScaleBtn->AddAction(myActionsMap[FitRectId]);
301   aScaleBtn->AddAction(myActionsMap[ZoomId]);
302
303   SUIT_ToolButton* aPanningBtn = new SUIT_ToolButton(myToolBar);
304   aPanningBtn->AddAction(myActionsMap[PanId]);
305   aPanningBtn->AddAction(myActionsMap[GlobalPanId]);
306
307   myActionsMap[RotationId]->addTo(myToolBar);
308
309   SUIT_ToolButton* aViewsBtn = new SUIT_ToolButton(myToolBar);
310   aViewsBtn->AddAction(myActionsMap[FrontId]);
311   aViewsBtn->AddAction(myActionsMap[BackId]);
312   aViewsBtn->AddAction(myActionsMap[TopId]);
313   aViewsBtn->AddAction(myActionsMap[BottomId]);
314   aViewsBtn->AddAction(myActionsMap[LeftId]);
315   aViewsBtn->AddAction(myActionsMap[RightId]);
316
317   myActionsMap[ResetId]->addTo(myToolBar);
318 }
319
320 //----------------------------------------------------------------------------
321 void
322 SVTK_ViewWindow
323 ::onFrontView()
324 {
325   vtkCamera* camera = myRenderer->GetActiveCamera();
326   camera->SetPosition(1,0,0);
327   camera->SetViewUp(0,0,1);
328   camera->SetFocalPoint(0,0,0);
329   onFitAll();
330 }
331
332 //----------------------------------------------------------------------------
333 void
334 SVTK_ViewWindow
335 ::onBackView()
336 {
337   vtkCamera* camera = myRenderer->GetActiveCamera();
338   camera->SetPosition(-1,0,0);
339   camera->SetViewUp(0,0,1);
340   camera->SetFocalPoint(0,0,0);
341   onFitAll();
342 }
343
344 //----------------------------------------------------------------------------
345 void
346 SVTK_ViewWindow
347 ::onTopView()
348 {
349   vtkCamera* camera = myRenderer->GetActiveCamera();
350   camera->SetPosition(0,0,1);
351   camera->SetViewUp(0,1,0);
352   camera->SetFocalPoint(0,0,0);
353   onFitAll();
354 }
355
356 //----------------------------------------------------------------------------
357 void
358 SVTK_ViewWindow
359 ::onBottomView()
360 {
361   vtkCamera* camera = myRenderer->GetActiveCamera();
362   camera->SetPosition(0,0,-1);
363   camera->SetViewUp(0,1,0);
364   camera->SetFocalPoint(0,0,0);
365   onFitAll();
366 }
367
368 //----------------------------------------------------------------------------
369 void
370 SVTK_ViewWindow
371 ::onLeftView()
372 {
373   vtkCamera* camera = myRenderer->GetActiveCamera(); 
374   camera->SetPosition(0,-1,0);
375   camera->SetViewUp(0,0,1);
376   camera->SetFocalPoint(0,0,0);
377   onFitAll();
378 }
379
380 //----------------------------------------------------------------------------
381 void
382 SVTK_ViewWindow
383 ::onRightView()
384 {
385   vtkCamera* camera = myRenderer->GetActiveCamera();
386   camera->SetPosition(0,1,0);
387   camera->SetViewUp(0,0,1);
388   camera->SetFocalPoint(0,0,0);
389   onFitAll();
390 }
391
392 //----------------------------------------------------------------------------
393 void
394 SVTK_ViewWindow
395 ::onResetView()
396 {
397   int aTrihedronIsVisible = isTrihedronDisplayed();
398   myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
399   ::ResetCamera(myRenderer,true);  
400   vtkCamera* aCamera = myRenderer->GetActiveCamera();
401   aCamera->SetPosition(1,-1,1);
402   aCamera->SetViewUp(0,0,1);
403   ::ResetCamera(myRenderer,true);  
404   if(aTrihedronIsVisible) myTrihedron->VisibilityOn();
405   else myTrihedron->VisibilityOff();
406   static float aCoeff = 3.0;
407   aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
408   Repaint();
409 }
410
411 //----------------------------------------------------------------------------
412 void
413 SVTK_ViewWindow
414 ::onFitAll()
415 {
416   myRWInteractor->GetSInteractorStyle()->ViewFitAll();
417   Repaint();
418 }
419
420 //----------------------------------------------------------------------------
421 void
422 SVTK_ViewWindow
423 ::onDumpView()
424 {
425   QApplication::setOverrideCursor( Qt::waitCursor );
426   QPixmap px = QPixmap::grabWindow(myRenderWindow->winId());
427   QApplication::restoreOverrideCursor();
428   
429   SUIT_Application* app = getViewManager()->study()->application();
430
431   QString aFileName = app->getFileName( false, QString::null, tr("VTK_IMAGE_FILES"), tr("INF_APP_DUMP_VIEW"), 0 );
432
433   if ( !aFileName.isNull() ) {
434     QApplication::setOverrideCursor( Qt::waitCursor );
435     QString fmt = SUIT_Tools::extension( aFileName ).upper();
436     if (fmt.isEmpty())
437       fmt = QString("BMP"); // default format
438     if (fmt == "JPG")
439       fmt = "JPEG";
440     bool bOk = px.save(aFileName, fmt.latin1());
441     QApplication::restoreOverrideCursor();
442     if (!bOk) {
443       SUIT_MessageBox::error1(this, tr("ERROR"), tr("ERR_DOC_CANT_SAVE_FILE"), tr("BUT_OK"));
444     }
445   }
446 }
447
448 //----------------------------------------------------------------
449 void
450 SVTK_ViewWindow
451 ::onSelectionChanged()
452 {
453   unHighlightAll();
454
455   const SALOME_ListIO& aListIO = mySelector->StoredIObjects();
456   SALOME_ListIteratorOfListIO anIter(aListIO);
457   for(; anIter.More(); anIter.Next()){
458     highlight(anIter.Value(),true,!anIter.More());
459   }
460
461   emit selectionChanged();
462 }
463
464 //----------------------------------------------------------------
465 void
466 SVTK_ViewWindow
467 ::SetSelectionMode(Selection_Mode theMode)
468 {
469   mySelector->SetSelectionMode(theMode);
470   myRWInteractor->SetSelectionMode(theMode);
471 }
472
473 //----------------------------------------------------------------
474 Selection_Mode
475 SVTK_ViewWindow
476 ::SelectionMode() const
477 {
478   return mySelector->SelectionMode();
479 }
480
481 //----------------------------------------------------------------
482 void 
483 SVTK_ViewWindow
484 ::unHighlightAll() 
485 {
486   myRWInteractor->unHighlightAll();
487 }
488
489 //----------------------------------------------------------------
490 void
491 SVTK_ViewWindow
492 ::highlight( const Handle(SALOME_InteractiveObject)& theIO, 
493              bool theIsHighlight, 
494              bool theIsUpdate ) 
495 {
496   myRWInteractor->highlight(theIO, theIsHighlight, theIsUpdate);
497
498   if(mySelector->HasIndex(theIO) && theIO->hasEntry()){
499     TColStd_IndexedMapOfInteger aMapIndex;
500     mySelector->GetIndex(theIO,aMapIndex);
501     using namespace VTK;
502     const char* anEntry = theIO->getEntry();
503     vtkActorCollection* aCollection = myRenderer->GetActors();
504     if(SALOME_Actor* anActor = Find<SALOME_Actor>(aCollection,TIsSameEntry<SALOME_Actor>(anEntry))){
505       switch (mySelector->SelectionMode()) {
506       case NodeSelection:
507         myRWInteractor->highlightPoint(aMapIndex,anActor,theIsHighlight,theIsUpdate);
508         break;
509       case EdgeOfCellSelection:
510         myRWInteractor->highlightEdge(aMapIndex,anActor,theIsHighlight,theIsUpdate);
511         break;
512       case CellSelection:
513       case EdgeSelection:
514       case FaceSelection:
515       case VolumeSelection:
516         myRWInteractor->highlightCell(aMapIndex,anActor,theIsHighlight,theIsUpdate);
517         break;
518       }
519     }
520   }else{
521     myRWInteractor->unHighlightSubSelection();
522   }
523 }
524
525 //----------------------------------------------------------------
526 bool
527 SVTK_ViewWindow
528 ::isInViewer( const Handle(SALOME_InteractiveObject)& theIO ) 
529 {
530   return myRWInteractor->isInViewer( theIO );
531 }
532
533 //----------------------------------------------------------------
534 bool
535 SVTK_ViewWindow
536 ::isVisible( const Handle(SALOME_InteractiveObject)& theIO ) 
537 {
538   return myRWInteractor->isVisible( theIO );
539 }
540
541 //----------------------------------------------------------------------------
542 void
543 SVTK_ViewWindow
544 ::setBackgroundColor( const QColor& color )
545 {
546   if ( myRenderer )
547     myRenderer->SetBackground( color.red()/255., color.green()/255., color.blue()/255. );
548 }
549
550 //----------------------------------------------------------------------------
551 QColor
552 SVTK_ViewWindow
553 ::backgroundColor() const
554 {
555   float backint[3];
556   if ( myRenderer ) {
557     myRenderer->GetBackground( backint );
558     return QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255));
559   }
560   return SUIT_ViewWindow::backgroundColor();
561 }
562
563 //----------------------------------------------------------------------------
564 void
565 SVTK_ViewWindow
566 ::Repaint(bool theUpdateTrihedron)
567 {
568   if (theUpdateTrihedron) 
569     onAdjustTrihedron();
570   myRenderWindow->update();
571 }
572
573 //----------------------------------------------------------------------------
574 void
575 SVTK_ViewWindow
576 ::GetScale( double theScale[3] ) 
577 {
578   myTransform->GetScale( theScale );
579 }
580
581 //----------------------------------------------------------------------------
582 void
583 SVTK_ViewWindow
584 ::SetScale( double theScale[3] ) 
585 {
586   myTransform->SetScale( theScale[0], theScale[1], theScale[2] );
587   myRWInteractor->Render();
588   Repaint();
589 }
590
591 //----------------------------------------------------------------------------
592 bool
593 SVTK_ViewWindow
594 ::isTrihedronDisplayed()
595 {
596   return myTrihedron->GetVisibility() == VTKViewer_Trihedron::eOn;
597 }
598
599 //----------------------------------------------------------------------------
600 void 
601 SVTK_ViewWindow
602 ::onViewTrihedron()
603 {
604   if(!myTrihedron) 
605     return;
606
607   if(isTrihedronDisplayed())
608     myTrihedron->VisibilityOff();
609   else
610     myTrihedron->VisibilityOn();
611
612   Repaint();
613 }
614
615 //----------------------------------------------------------------------------
616 bool
617 SVTK_ViewWindow
618 ::ComputeTrihedronSize( double& theNewSize, double& theSize )
619 {
620   // calculating diagonal of visible props of the renderer
621   float aBndBox[ 6 ];
622   myTrihedron->VisibilityOff();
623
624   if ( ::ComputeVisiblePropBounds( myRenderer, aBndBox ) == 0 ) {
625     aBndBox[ 1 ] = aBndBox[ 3 ] = aBndBox[ 5 ] = 100;
626     aBndBox[ 0 ] = aBndBox[ 2 ] = aBndBox[ 4 ] = 0;
627   }
628
629   myTrihedron->VisibilityOn();
630   float aLength = 0;
631   static bool aCalcByDiag = false;
632   if ( aCalcByDiag ) {
633     aLength = sqrt( ( aBndBox[1]-aBndBox[0])*(aBndBox[1]-aBndBox[0] )+
634                     ( aBndBox[3]-aBndBox[2])*(aBndBox[3]-aBndBox[2] )+
635                     ( aBndBox[5]-aBndBox[4])*(aBndBox[5]-aBndBox[4] ) );
636   } else {
637     aLength = aBndBox[ 1 ]-aBndBox[ 0 ];
638     aLength = max( ( aBndBox[ 3 ] - aBndBox[ 2 ] ),aLength );
639     aLength = max( ( aBndBox[ 5 ] - aBndBox[ 4 ] ),aLength );
640   }
641
642   static float aSizeInPercents = 105;
643   //QString aSetting = QAD_CONFIG->getSetting( "Viewer:TrihedronSize" );
644   //if ( !aSetting.isEmpty() )
645   //  aSizeInPercents = aSetting.toFloat();
646
647   static float EPS_SIZE = 5.0E-3;
648   theSize = myTrihedron->GetSize();
649   theNewSize = aLength * aSizeInPercents / 100.0;
650
651   // if the new trihedron size have sufficient difference, then apply the value
652   return fabs( theNewSize - theSize) > theSize * EPS_SIZE ||
653          fabs( theNewSize-theSize ) > theNewSize * EPS_SIZE;
654 }
655
656 //----------------------------------------------------------------------------
657 double
658 SVTK_ViewWindow
659 ::GetTrihedronSize() const
660 {
661   return myTrihedron->GetSize();
662 }
663
664 //----------------------------------------------------------------------------
665 void
666 SVTK_ViewWindow
667 ::AdjustTrihedrons( const bool theIsForcedUpdate )
668 {
669   if ( !isTrihedronDisplayed() && !theIsForcedUpdate )
670     return;
671
672   int aVisibleNum = myTrihedron->GetVisibleActorCount( myRenderer );
673   if ( aVisibleNum || theIsForcedUpdate ) {
674     // if the new trihedron size have sufficient difference, then apply the value
675     double aNewSize = 100, anOldSize;
676     if ( ComputeTrihedronSize( aNewSize, anOldSize ) || theIsForcedUpdate ) {
677       myTrihedron->SetSize( aNewSize );
678       // itearte throuh displayed objects and set size if necessary
679       
680       vtkActorCollection* anActors = getRenderer()->GetActors();
681       anActors->InitTraversal();
682       while( vtkActor* anActor = anActors->GetNextActor() ) {
683         if( SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast( anActor ) ) {
684           if ( aSActor->IsResizable() )
685             aSActor->SetSize( 0.5 * aNewSize );
686         }
687       }
688     }
689   }
690
691   ::ResetCameraClippingRange(myRenderer);
692 }
693
694 //----------------------------------------------------------------------------
695 void
696 SVTK_ViewWindow
697 ::onAdjustTrihedron()
698 {   
699   AdjustTrihedrons( false );
700 }
701
702 #define INCREMENT_FOR_OP 10
703
704 //=======================================================================
705 // name    : onPanLeft
706 // Purpose : Performs incremental panning to the left
707 //=======================================================================
708 void
709 SVTK_ViewWindow
710 ::onPanLeft()
711 {
712   myRWInteractor->GetSInteractorStyle()->IncrementalPan( -INCREMENT_FOR_OP, 0 );
713 }
714
715 //=======================================================================
716 // name    : onPanRight
717 // Purpose : Performs incremental panning to the right
718 //=======================================================================
719 void
720 SVTK_ViewWindow
721 ::onPanRight()
722 {
723   myRWInteractor->GetSInteractorStyle()->IncrementalPan( INCREMENT_FOR_OP, 0 );
724 }
725
726 //=======================================================================
727 // name    : onPanUp
728 // Purpose : Performs incremental panning to the top
729 //=======================================================================
730 void
731 SVTK_ViewWindow
732 ::onPanUp()
733 {
734   myRWInteractor->GetSInteractorStyle()->IncrementalPan( 0, INCREMENT_FOR_OP );
735 }
736
737 //=======================================================================
738 // name    : onPanDown
739 // Purpose : Performs incremental panning to the bottom
740 //=======================================================================
741 void
742 SVTK_ViewWindow
743 ::onPanDown()
744 {
745   myRWInteractor->GetSInteractorStyle()->IncrementalPan( 0, -INCREMENT_FOR_OP );
746 }
747
748 //=======================================================================
749 // name    : onZoomIn
750 // Purpose : Performs incremental zooming in
751 //=======================================================================
752 void
753 SVTK_ViewWindow
754 ::onZoomIn()
755 {
756   myRWInteractor->GetSInteractorStyle()->IncrementalZoom( INCREMENT_FOR_OP );
757 }
758
759 //=======================================================================
760 // name    : onZoomOut
761 // Purpose : Performs incremental zooming out
762 //=======================================================================
763 void
764 SVTK_ViewWindow
765 ::onZoomOut()
766 {
767   myRWInteractor->GetSInteractorStyle()->IncrementalZoom( -INCREMENT_FOR_OP );
768 }
769
770 //=======================================================================
771 // name    : onRotateLeft
772 // Purpose : Performs incremental rotating to the left
773 //=======================================================================
774 void
775 SVTK_ViewWindow
776 ::onRotateLeft()
777 {
778   myRWInteractor->GetSInteractorStyle()->IncrementalRotate( -INCREMENT_FOR_OP, 0 );
779 }
780
781 //=======================================================================
782 // name    : onRotateRight
783 // Purpose : Performs incremental rotating to the right
784 //=======================================================================
785 void
786 SVTK_ViewWindow
787 ::onRotateRight()
788 {
789   myRWInteractor->GetSInteractorStyle()->IncrementalRotate( INCREMENT_FOR_OP, 0 );
790 }
791
792 //=======================================================================
793 // name    : onRotateUp
794 // Purpose : Performs incremental rotating to the top
795 //=======================================================================
796 void
797 SVTK_ViewWindow
798 ::onRotateUp()
799 {
800   myRWInteractor->GetSInteractorStyle()->IncrementalRotate( 0, -INCREMENT_FOR_OP );
801 }
802
803 //=======================================================================
804 void
805 SVTK_ViewWindow
806 ::onKeyPressed(QKeyEvent* event)
807 {
808   emit keyPressed( this, event );
809 }
810
811 //=======================================================================
812 void
813 SVTK_ViewWindow
814 ::onKeyReleased(QKeyEvent* event)
815 {
816   emit keyReleased( this, event );
817 }
818
819 //=======================================================================
820 void
821 SVTK_ViewWindow
822 ::onMousePressed(QMouseEvent* event)
823 {
824   emit mousePressed(this, event);
825 }
826
827 //=======================================================================
828 void
829 SVTK_ViewWindow
830 ::onMouseReleased(QMouseEvent* event)
831 {
832   emit mouseReleased( this, event );
833 }
834
835 //=======================================================================
836 void
837 SVTK_ViewWindow
838 ::onMouseMoving(QMouseEvent* event)
839 {
840   emit mouseMoving( this, event );
841 }
842
843 //=======================================================================
844 void
845 SVTK_ViewWindow
846 ::onMouseDoubleClicked( QMouseEvent* event )
847 {
848   emit mouseDoubleClicked( this, event );
849 }
850
851 //=======================================================================
852 // name    : onRotateDown
853 // Purpose : Performs incremental rotating to the bottom
854 //=======================================================================
855 void
856 SVTK_ViewWindow
857 ::onRotateDown()
858 {
859   myRWInteractor->GetSInteractorStyle()->IncrementalRotate( 0, INCREMENT_FOR_OP );
860 }
861
862 //----------------------------------------------------------------------------
863 void
864 SVTK_ViewWindow
865 ::InsertActor( SALOME_Actor* theActor, bool theMoveInternalActors )
866 {
867   theActor->AddToRender(myRenderer);
868   theActor->SetTransform(myTransform);
869   if(theMoveInternalActors) 
870     myRWInteractor->MoveInternalActors();
871 }
872
873 //----------------------------------------------------------------------------
874 void
875 SVTK_ViewWindow
876 ::AddActor( SALOME_Actor* theActor, bool theUpdate /*=false*/ )
877 {
878   InsertActor(theActor);
879   if(theUpdate) 
880     Repaint();
881 }
882
883 //----------------------------------------------------------------------------
884 void
885 SVTK_ViewWindow
886 ::RemoveActor( SALOME_Actor* theActor, bool theUpdate /*=false*/ )
887 {
888   theActor->RemoveFromRender(myRenderer);
889   if(theUpdate) 
890     Repaint();
891 }
892
893 //----------------------------------------------------------------------------
894 void
895 SVTK_ViewWindow
896 ::MoveActor( SALOME_Actor* theActor)
897 {
898   RemoveActor(theActor);
899   InsertActor(theActor,true);
900 }