Salome HOME
Merge remote-tracking branch 'origin/vsr/fix_single_study_pb' into pre/fix_single_study
[modules/gui.git] / src / VTKViewer / VTKViewer_ViewWindow.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "VTKViewer_ViewWindow.h"
24 #include "VTKViewer_ViewModel.h"
25 #include "VTKViewer_RenderWindow.h"
26 #include "VTKViewer_RenderWindowInteractor.h"
27 #include "VTKViewer_InteractorStyle.h"
28 #include "VTKViewer_Trihedron.h"
29 #include "VTKViewer_Transform.h"
30 #include "VTKViewer_Utilities.h"
31 #include "VTKViewer_Texture.h"
32 #include "VTKViewer_OpenGLRenderer.h"
33
34 #include <SUIT_Session.h>
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_Tools.h>
37 #include <SUIT_ResourceMgr.h>
38
39 #include <QFileInfo>
40 #include <QImage>
41
42 #include <vtkCamera.h>
43 #include <vtkJPEGReader.h>
44 #include <vtkBMPReader.h>
45 #include <vtkTIFFReader.h>
46 #include <vtkPNGReader.h>
47 #include <vtkMetaImageReader.h>
48 #include <vtkImageMapToColors.h>
49 #include <vtkTexture.h>
50
51 #include <QtxToolBar.h>
52 #include <QtxMultiAction.h>
53
54 /*! Construction*/
55 VTKViewer_ViewWindow::VTKViewer_ViewWindow( SUIT_Desktop* theDesktop, 
56                                             VTKViewer_Viewer* theModel,
57                                             VTKViewer_InteractorStyle* iStyle,
58                                             VTKViewer_RenderWindowInteractor* rw )
59 : SUIT_ViewWindow( theDesktop )
60 {
61   myModel = theModel;
62
63   myTrihedron = VTKViewer_Trihedron::New();
64   myTransform = VTKViewer_Transform::New();
65   myRenderer  = VTKViewer_OpenGLRenderer::New() ;
66
67   myTrihedron->AddToRender( myRenderer );
68
69   myRenderWindow = new VTKViewer_RenderWindow( this, "RenderWindow" );
70   setCentralWidget(myRenderWindow);
71   myRenderWindow->setFocusPolicy( Qt::StrongFocus );
72   myRenderWindow->setFocus();
73
74   myRenderWindow->getRenderWindow()->AddRenderer( myRenderer );
75
76   myRenderer->GetActiveCamera()->ParallelProjectionOn();
77   myRenderer->LightFollowCameraOn();
78   myRenderer->TwoSidedLightingOn();
79
80   // Create an interactor.
81   myRWInteractor = rw ? rw : VTKViewer_RenderWindowInteractor::New();
82   myRWInteractor->SetRenderWindow( myRenderWindow->getRenderWindow() );
83
84   VTKViewer_InteractorStyle* RWS = iStyle ? iStyle : VTKViewer_InteractorStyle::New();
85   RWS->setGUIWindow( myRenderWindow );
86   myRWInteractor->SetInteractorStyle( RWS ); 
87
88   myRWInteractor->Initialize();
89   RWS->setTriedron( myTrihedron );
90   RWS->FindPokedRenderer( 0, 0 );
91
92   setCentralWidget( myRenderWindow );
93
94   myToolBar = new QtxToolBar( true, tr("LBL_TOOLBAR_LABEL"), this );
95   myToolBar->setFloatable( false );
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   connect( myRWInteractor, SIGNAL(RenderWindowModified()),
113            myRenderWindow, SLOT(update()) );
114
115   connect( myRenderWindow, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
116            this,           SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
117
118   connect( myRWInteractor, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
119            this,           SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
120
121
122   // set default background
123   setBackground( Qtx::BackgroundData( Qt::black ) );
124   // reset view
125   onResetView();
126 }
127
128 /*!Destructor.*/
129 VTKViewer_ViewWindow::~VTKViewer_ViewWindow()
130 {
131   myTransform->Delete();
132   // In order to ensure that the interactor unregisters
133   // this RenderWindow, we assign a NULL RenderWindow to 
134   // it before deleting it.
135   myRWInteractor->SetRenderWindow( NULL );
136   myRWInteractor->Delete();
137   
138   //m_RW->Delete() ;
139   myRenderer->RemoveAllViewProps();
140   //m_Renderer->Delete() ;
141   myTrihedron->Delete();
142 }
143
144 /*!Checks: is trihedron displayed.*/
145 bool VTKViewer_ViewWindow::isTrihedronDisplayed(){
146   return myTrihedron->GetVisibility() == VTKViewer_Trihedron::eOn;
147 }
148
149 /*!Activates 'zooming' transformation*/
150 void VTKViewer_ViewWindow::activateZoom()
151 {
152   myRWInteractor->GetInteractorStyle()->startZoom();
153 }
154
155 /*!Activates 'panning' transformation*/
156 void VTKViewer_ViewWindow::activatePanning()
157 {
158   myRWInteractor->GetInteractorStyle()->startPan();
159 }
160
161 /*!Activates 'rotation' transformation*/
162 void VTKViewer_ViewWindow::activateRotation()
163 {
164   myRWInteractor->GetInteractorStyle()->startRotate();
165 }
166
167 /*!Activate global panning.*/
168 void VTKViewer_ViewWindow::activateGlobalPanning()
169 {
170   //if(myTrihedron->GetVisibleActorCount(myRenderer))
171   myRWInteractor->GetInteractorStyle()->startGlobalPan();
172 }
173
174 /*!Activates 'fit area' transformation*/
175 void VTKViewer_ViewWindow::activateWindowFit()
176 {
177   myRWInteractor->GetInteractorStyle()->startFitArea();
178 }
179
180 /*!Create actions:*/
181 void VTKViewer_ViewWindow::createActions()
182 {
183   if (!myActionsMap.isEmpty()) return;
184   
185   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
186   
187   QtxAction* aAction;
188
189   //! \li 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   //! \li 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   //! \li 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   //! \li 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   //! \li 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   //! \li 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   //! \li 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   //! \li 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   //! \li Back view
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   //! \li Top view
253   aAction = new QtxAction(tr("MNU_TOP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TOP" ) ),
254                            tr( "MNU_TOP_VIEW" ), 0, this);
255   aAction->setStatusTip(tr("DSC_TOP_VIEW"));
256   connect(aAction, SIGNAL(activated()), this, SLOT(onTopView()));
257   myActionsMap[ TopId ] = aAction;
258
259   //! \li Bottom view
260   aAction = new QtxAction(tr("MNU_BOTTOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BOTTOM" ) ),
261                            tr( "MNU_BOTTOM_VIEW" ), 0, this);
262   aAction->setStatusTip(tr("DSC_BOTTOM_VIEW"));
263   connect(aAction, SIGNAL(activated()), this, SLOT(onBottomView()));
264   myActionsMap[ BottomId ] = aAction;
265
266   //! \li Left view
267   aAction = new QtxAction(tr("MNU_LEFT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_LEFT" ) ),
268                            tr( "MNU_LEFT_VIEW" ), 0, this);
269   aAction->setStatusTip(tr("DSC_LEFT_VIEW"));
270   connect(aAction, SIGNAL(activated()), this, SLOT(onLeftView()));
271   myActionsMap[ LeftId ] = aAction;
272
273   //! \li Right view
274   aAction = new QtxAction(tr("MNU_RIGHT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RIGHT" ) ),
275                            tr( "MNU_RIGHT_VIEW" ), 0, this);
276   aAction->setStatusTip(tr("DSC_RIGHT_VIEW"));
277   connect(aAction, SIGNAL(activated()), this, SLOT(onRightView()));
278   myActionsMap[ RightId ] = aAction;
279
280   // \li Rotate anticlockwise
281   aAction = new QtxAction(tr("MNU_ANTICLOCKWISE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ANTICLOCKWISE" ) ),
282                           tr( "MNU_ANTICLOCKWISE_VIEW" ), 0, this);
283   aAction->setStatusTip(tr("DSC_ANTICLOCKWISE_VIEW"));
284   connect(aAction, SIGNAL(triggered()), this, SLOT(onAntiClockWiseView()));
285   myActionsMap[ AntiClockWiseId ] = aAction;
286
287   // \li Rotate clockwise
288   aAction = new QtxAction(tr("MNU_CLOCKWISE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_CLOCKWISE" ) ),
289                           tr( "MNU_CLOCKWISE_VIEW" ), 0, this);
290   aAction->setStatusTip(tr("DSC_CLOCKWISE_VIEW"));
291   connect(aAction, SIGNAL(triggered()), this, SLOT(onClockWiseView()));
292   myActionsMap[ ClockWiseId ] = aAction;
293
294   //! \li Reset
295   aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RESET" ) ),
296                            tr( "MNU_RESET_VIEW" ), 0, this);
297   aAction->setStatusTip(tr("DSC_RESET_VIEW"));
298   connect(aAction, SIGNAL(activated()), this, SLOT(onResetView()));
299   myActionsMap[ ResetId ] = aAction;
300
301   //! \li Trihedron shown
302   aAction = new QtxAction(tr("MNU_SHOW_TRIHEDRON"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TRIHEDRON" ) ),
303                            tr( "MNU_SHOW_TRIHEDRON" ), 0, this);
304   aAction->setStatusTip(tr("DSC_SHOW_TRIHEDRON"));
305   connect(aAction, SIGNAL(activated()), this, SLOT(onTrihedronShow()));
306   myActionsMap[ TrihedronShowId ] = aAction;
307 }
308
309 /*!Create tool bar.*/
310 void VTKViewer_ViewWindow::createToolBar()
311 {
312   myToolBar->addAction( myActionsMap[DumpId] );
313   myToolBar->addAction( myActionsMap[TrihedronShowId] );
314
315   QtxMultiAction* aScaleAction = new QtxMultiAction( this );
316   aScaleAction->insertAction( myActionsMap[FitAllId] );
317   aScaleAction->insertAction( myActionsMap[FitRectId] );
318   aScaleAction->insertAction( myActionsMap[ZoomId] );
319   myToolBar->addAction( aScaleAction );
320
321   QtxMultiAction* aPanningAction = new QtxMultiAction( this );
322   aPanningAction->insertAction( myActionsMap[PanId] );
323   aPanningAction->insertAction( myActionsMap[GlobalPanId] );
324   myToolBar->addAction( aPanningAction );
325
326   myToolBar->addAction( myActionsMap[RotationId] );
327
328   QtxMultiAction* aViewsAction = new QtxMultiAction(myToolBar);
329   aViewsAction->insertAction( myActionsMap[FrontId] );
330   aViewsAction->insertAction( myActionsMap[BackId] );
331   aViewsAction->insertAction( myActionsMap[TopId] );
332   aViewsAction->insertAction( myActionsMap[BottomId] );
333   aViewsAction->insertAction( myActionsMap[LeftId] );
334   aViewsAction->insertAction( myActionsMap[RightId] );
335   myToolBar->addAction( aViewsAction );
336
337   myToolBar->addAction( myActionsMap[AntiClockWiseId] );
338   myToolBar->addAction( myActionsMap[ClockWiseId] );
339
340   myToolBar->addAction( myActionsMap[ResetId] );
341 }
342
343 /*!On front view event.*/
344 void VTKViewer_ViewWindow::onFrontView()
345 {
346   vtkCamera* camera = myRenderer->GetActiveCamera();
347   camera->SetPosition(1,0,0);
348   camera->SetViewUp(0,0,1);
349   camera->SetFocalPoint(0,0,0);
350   onFitAll();
351 }
352
353 /*!On back view slot.*/
354 void VTKViewer_ViewWindow::onBackView()
355 {
356   vtkCamera* camera = myRenderer->GetActiveCamera();
357   camera->SetPosition(-1,0,0);
358   camera->SetViewUp(0,0,1);
359   camera->SetFocalPoint(0,0,0);
360   onFitAll();
361 }
362
363 /*!On back view slot.*/
364 void VTKViewer_ViewWindow::onTopView()
365 {
366   vtkCamera* camera = myRenderer->GetActiveCamera();
367   camera->SetPosition(0,0,1);
368   camera->SetViewUp(0,1,0);
369   camera->SetFocalPoint(0,0,0);
370   onFitAll();
371 }
372
373 /*!On bottom view slot.*/
374 void VTKViewer_ViewWindow::onBottomView()
375 {
376   vtkCamera* camera = myRenderer->GetActiveCamera();
377   camera->SetPosition(0,0,-1);
378   camera->SetViewUp(0,1,0);
379   camera->SetFocalPoint(0,0,0);
380   onFitAll();
381 }
382
383 /*!On left view slot.*/
384 void VTKViewer_ViewWindow::onLeftView()
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 /*!On right view slot.*/
394 void VTKViewer_ViewWindow::onRightView()
395 {
396   vtkCamera* camera = myRenderer->GetActiveCamera();
397   camera->SetPosition(0,1,0);
398   camera->SetViewUp(0,0,1);
399   camera->SetFocalPoint(0,0,0);
400   onFitAll();
401 }
402
403 /*!
404   \brief Rotate view 90 degrees clockwise
405 */
406 void VTKViewer_ViewWindow::onClockWiseView()
407 {
408   vtkCamera* aCamera = myRenderer->GetActiveCamera(); 
409   aCamera->Roll(-90);
410   aCamera->OrthogonalizeViewUp();
411   Repaint();
412 }
413
414 /*!
415   \brief Rotate view 90 degrees conterclockwise
416 */
417 void VTKViewer_ViewWindow::onAntiClockWiseView()
418 {
419   vtkCamera* aCamera = myRenderer->GetActiveCamera(); 
420   aCamera->Roll(90);
421   aCamera->OrthogonalizeViewUp();
422   Repaint();
423 }
424
425 /*!On reset view slot.*/
426 void VTKViewer_ViewWindow::onResetView()
427 {
428   int aTriedronIsVisible = isTrihedronDisplayed();
429   myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
430   ::ResetCamera(myRenderer,true);  
431   vtkCamera* aCamera = myRenderer->GetActiveCamera();
432   aCamera->SetPosition(1,-1,1);
433   aCamera->SetViewUp(0,0,1);
434   ::ResetCamera(myRenderer,true);  
435   if(aTriedronIsVisible) myTrihedron->VisibilityOn();
436   else myTrihedron->VisibilityOff();
437   static double aCoeff = 3.0;
438   aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
439   Repaint();
440 }
441
442 /*!On fit all slot.*/
443 void VTKViewer_ViewWindow::onFitAll()
444 {
445   myRWInteractor->GetInteractorStyle()->ViewFitAll();
446   Repaint();
447 }
448
449 /*!Set background color of the viewport [obsolete]*/
450 void VTKViewer_ViewWindow::setBackgroundColor( const QColor& c )
451 {
452   Qtx::BackgroundData bg = background();
453   bg.setColor( c );
454   setBackground( bg );
455 }
456
457 /*!Returns background color of the viewport [obsolete]*/
458 QColor VTKViewer_ViewWindow::backgroundColor() const
459 {
460   return background().color();
461 }
462
463 /*!Set background of the viewport*/
464 void VTKViewer_ViewWindow::setBackground( const Qtx::BackgroundData& bgData )
465 {
466   bool ok = false;
467  
468   if ( bgData.isValid() ) {
469     switch ( bgData.mode() ) {
470     case Qtx::ColorBackground:
471       {
472         QColor c = bgData.color();
473         if ( c.isValid() ) {
474           // show solid-colored background
475           getRenderer()->SetTexturedBackground( false );  // cancel texture mode
476           getRenderer()->SetGradientBackground( false );  // cancel gradient mode
477           getRenderer()->SetBackground( c.red()/255.0,
478                                         c.green()/255.0,
479                                         c.blue()/255.0 ); // set background color
480           ok = true;
481         }
482         break;
483       }
484     case Qtx::SimpleGradientBackground:
485       {
486         QColor c1, c2;
487         int type = bgData.gradient( c1, c2 );
488         if ( c1.isValid() )
489         {
490           if ( !c2.isValid() )
491             c2 = c1;
492
493           // show two-color gradient background
494           getRenderer()->SetTexturedBackground( false );    // cancel texture mode
495           getRenderer()->SetGradientBackground( true );     // switch to gradient mode
496
497           VTKViewer_OpenGLRenderer* aRenderer =
498             VTKViewer_OpenGLRenderer::SafeDownCast( getRenderer() );
499           if( aRenderer )
500           {
501             aRenderer->SetGradientType( type );
502             aRenderer->SetBackground( c1.redF(), c1.greenF(), c1.blueF() );
503             aRenderer->SetBackground2( c2.redF(), c2.greenF(), c2.blueF() );
504             ok = true;
505           }
506         }
507         break;
508       }
509     case Qtx::CustomGradientBackground:
510       {
511         // NOT IMPLEMENTED YET
512         getRenderer()->SetTexturedBackground( false );  // cancel texture mode
513         getRenderer()->SetGradientBackground( false );  // cancel gradient mode
514         // .........
515         break;
516       }
517     default:
518       break;
519     }
520     if ( bgData.isTextureShown() ) {
521       QString fileName;
522       int textureMode = bgData.texture( fileName );
523       QFileInfo fi( fileName );
524       if ( !fileName.isEmpty() && fi.exists() ) {
525         // read texture from file
526         QString extension = fi.suffix().toLower();
527         vtkImageReader2* aReader = 0;
528         if ( extension == "jpg" || extension == "jpeg" )
529           aReader = vtkJPEGReader::New();
530         else if ( extension == "bmp" )
531           aReader = vtkBMPReader::New();
532         else if ( extension == "tif" || extension == "tiff" )
533           aReader = vtkTIFFReader::New();
534         else if ( extension == "png" )
535           aReader = vtkPNGReader::New();
536         else if ( extension == "mhd" || extension == "mha" )
537           aReader = vtkMetaImageReader::New();           
538         if ( aReader ) {
539           // create texture
540           aReader->SetFileName( fi.absoluteFilePath().toLatin1().constData() );
541           aReader->Update();
542           
543           VTKViewer_Texture* aTexture = VTKViewer_Texture::New();
544           vtkImageMapToColors* aMap = 0;
545           vtkAlgorithmOutput* anOutput;
546           /*
547           // special processing for BMP reader
548           vtkBMPReader* aBMPReader = (vtkBMPReader*)aReader;
549           if ( aBMPReader ) {
550             // Special processing for BMP file
551             aBMPReader->SetAllow8BitBMP(1);
552             
553             aMap = vtkImageMapToColors::New();
554             aMap->SetInputConnection( aBMPReader->GetOutputPort() );
555             aMap->SetLookupTable( (vtkScalarsToColors*)aBMPReader->GetLookupTable() );
556             aMap->SetOutputFormatToRGB();
557             
558             anOutput = aMap->GetOutputPort();
559           }
560           else {
561           }
562           */
563           anOutput = aReader->GetOutputPort( 0 );
564           aTexture->SetInputConnection( anOutput );
565           // set texture mode
566           // VSR: Currently, VTK only supports Stretch mode, so below code will give
567           // the same results for all modes
568           switch ( textureMode ) {
569           case Qtx::TileTexture:
570             aTexture->RepeatOn();
571             aTexture->EdgeClampOff();
572             aTexture->InterpolateOff();
573             break;
574           case Qtx::StretchTexture:
575             aTexture->RepeatOff();
576             aTexture->EdgeClampOff();
577             aTexture->InterpolateOn();
578             break;
579           case Qtx::CenterTexture:
580           default:
581             aTexture->RepeatOff();
582             aTexture->EdgeClampOn();
583             aTexture->InterpolateOff();
584             break;
585           }
586           // show textured background
587           getRenderer()->SetTexturedBackground( true );     // switch to texture mode
588           getRenderer()->SetBackgroundTexture( aTexture );  // set texture image
589           
590           // clean-up resources
591           if ( aMap )
592             aMap->Delete();
593           aReader->Delete();
594           aTexture->Delete();
595           ok = true;
596         }
597       }
598     }
599   }
600
601   if ( ok )
602     myBackground = bgData;
603 }
604
605 /*!Returns background of the viewport*/
606 Qtx::BackgroundData VTKViewer_ViewWindow::background() const
607 {
608   return myBackground;
609 }
610
611 /*!Repaint window. If \a theUpdateTrihedron is true - recalculate trihedron.*/
612 void VTKViewer_ViewWindow::Repaint(bool theUpdateTrihedron)
613 {
614   if (theUpdateTrihedron) onAdjustTrihedron();
615   myRenderWindow->update();
616 }
617
618 /*!Get scale of transformation filter.*/
619 void VTKViewer_ViewWindow::GetScale( double theScale[3] ) {
620   myTransform->GetScale( theScale );
621 }
622
623 /*!Set scale of transformation filter and repaint window.*/
624 void VTKViewer_ViewWindow::SetScale( double theScale[3] ) {
625   myTransform->SetMatrixScale( theScale[0], theScale[1], theScale[2] );
626   myRWInteractor->Render();
627   Repaint();
628 }
629
630 /*!Calculation trihedron size.*/
631 void VTKViewer_ViewWindow::onAdjustTrihedron(){   
632   if( !isTrihedronDisplayed() ) 
633     return;
634   int aVisibleNum = myTrihedron->GetVisibleActorCount(myRenderer);
635   if(aVisibleNum){
636     // calculating diagonal of visible props of the renderer
637     double bnd[6];
638     myTrihedron->VisibilityOff();
639     ::ComputeVisiblePropBounds(myRenderer,bnd);
640     myTrihedron->VisibilityOn();
641     double aLength = 0;
642     static bool CalcByDiag = false;
643     if(CalcByDiag){
644       aLength = sqrt((bnd[1]-bnd[0])*(bnd[1]-bnd[0])+
645                      (bnd[3]-bnd[2])*(bnd[3]-bnd[2])+
646                      (bnd[5]-bnd[4])*(bnd[5]-bnd[4]));
647     }else{
648       aLength = bnd[1]-bnd[0];
649       aLength = qMax((bnd[3]-bnd[2]),aLength);
650       aLength = qMax((bnd[5]-bnd[4]),aLength);
651     }
652    
653     static double aSizeInPercents = 105;
654     QString aSetting;// = SUIT_CONFIG->getSetting("Viewer:TrihedronSize");
655     if(!aSetting.isEmpty()) aSizeInPercents = aSetting.toFloat();
656
657     static double EPS_SIZE = 5.0E-3;
658     double aSize = myTrihedron->GetSize();
659     double aNewSize = aLength*aSizeInPercents/100.0;
660     // if the new trihedron size have sufficient difference, then apply the value
661     if(fabs(aNewSize-aSize) > aSize*EPS_SIZE || fabs(aNewSize-aSize) > aNewSize*EPS_SIZE){
662       myTrihedron->SetSize(aNewSize);
663     }
664   }
665   ::ResetCameraClippingRange(myRenderer);
666 }
667
668 /*!Emit key pressed.*/
669 void VTKViewer_ViewWindow::onKeyPressed(QKeyEvent* event)
670 {
671   emit keyPressed( this, event );
672 }
673
674 /*!Emit key released.*/
675 void VTKViewer_ViewWindow::onKeyReleased(QKeyEvent* event)
676 {
677   emit keyReleased( this, event );
678 }
679
680 /*!Emit key pressed.*/
681 void VTKViewer_ViewWindow::onMousePressed(QMouseEvent* event)
682 {
683   emit mousePressed(this, event);
684 }
685
686 /*!Emit mouse released.*/
687 void VTKViewer_ViewWindow::onMouseReleased(QMouseEvent* event)
688 {
689   emit mouseReleased( this, event );
690 }
691
692 /*!Emit mouse moving.*/
693 void VTKViewer_ViewWindow::onMouseMoving(QMouseEvent* event)
694 {
695   emit mouseMoving( this, event );
696 }
697
698 /*!Emit mouse double clicked.*/
699 void VTKViewer_ViewWindow::onMouseDoubleClicked( QMouseEvent* event )
700 {
701   emit mouseDoubleClicked( this, event );
702 }
703
704 /*!Insert actor to renderer and transformation filter.
705  *Move Internal actors, if \a theMoveInternalActors is true.
706  */
707 void VTKViewer_ViewWindow::InsertActor( VTKViewer_Actor* theActor, bool theMoveInternalActors ){
708   theActor->AddToRender(myRenderer);
709   theActor->SetTransform(myTransform);
710   if(theMoveInternalActors) 
711     myRWInteractor->MoveInternalActors();
712 }
713
714 /*!Add actor.Repaint window if \a theUpdate is true.
715  *@see InsertActor( VTKViewer_Actor* theActor, bool theMoveInternalActors )
716  */
717 void VTKViewer_ViewWindow::AddActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
718   InsertActor(theActor);
719   if(theUpdate) 
720     Repaint();
721 }
722
723 /*!Remove \a theActor from renderer and pepaint, if \a theUpdate is true.*/
724 void VTKViewer_ViewWindow::RemoveActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
725   theActor->RemoveFromRender(myRenderer);
726   if(theUpdate) 
727     Repaint();
728 }
729
730 /*!@see RemoveActor() and InsertActor().*/
731 void VTKViewer_ViewWindow::MoveActor( VTKViewer_Actor* theActor)
732 {
733   RemoveActor(theActor);
734   InsertActor(theActor,true);
735 }
736
737 /*!On trihedron show slot.*/
738 void VTKViewer_ViewWindow::onTrihedronShow()
739 {
740   if (isTrihedronDisplayed())
741     myTrihedron->VisibilityOff();
742   else
743     myTrihedron->VisibilityOn();
744   myRenderWindow->update();
745 }
746
747 /*!Dump view.*/
748 QImage VTKViewer_ViewWindow::dumpView()
749 {
750   QPixmap px = QPixmap::grabWindow( myRenderWindow->winId() );
751   return px.toImage();
752 }
753
754 /*! The method returns the visual parameters of this view as a formated string
755  */
756 QString VTKViewer_ViewWindow::getVisualParameters()
757 {
758   double pos[3], focalPnt[3], viewUp[3], parScale, scale[3];
759
760   vtkCamera* camera = myRenderer->GetActiveCamera();
761   camera->GetPosition( pos );
762   camera->GetFocalPoint( focalPnt );
763   camera->GetViewUp( viewUp );
764   parScale = camera->GetParallelScale();
765   GetScale( scale );
766
767   QString retStr;
768   retStr.sprintf( "%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e", 
769                   pos[0], pos[1], pos[2], focalPnt[0], focalPnt[1], focalPnt[2], viewUp[0], viewUp[1], 
770                   viewUp[2], parScale, scale[0], scale[1], scale[2] );
771   return retStr;
772 }
773
774 /*! The method restors visual parameters of this view from a formated string
775  */
776 void VTKViewer_ViewWindow::setVisualParameters( const QString& parameters )
777 {
778   QStringList paramsLst = parameters.split( '*' );
779   if ( paramsLst.size() == 13 ) {
780     double pos[3], focalPnt[3], viewUp[3], parScale, scale[3];
781     pos[0] = paramsLst[0].toDouble();
782     pos[1] = paramsLst[1].toDouble();
783     pos[2] = paramsLst[2].toDouble();
784     focalPnt[0] = paramsLst[3].toDouble();
785     focalPnt[1] = paramsLst[4].toDouble();
786     focalPnt[2] = paramsLst[5].toDouble();
787     viewUp[0] = paramsLst[6].toDouble();
788     viewUp[1] = paramsLst[7].toDouble();
789     viewUp[2] = paramsLst[8].toDouble();
790     parScale = paramsLst[9].toDouble();
791     scale[0] = paramsLst[10].toDouble();
792     scale[1] = paramsLst[11].toDouble();
793     scale[2] = paramsLst[12].toDouble();
794
795     vtkCamera* camera = myRenderer->GetActiveCamera();
796     camera->SetPosition( pos );
797     camera->SetFocalPoint( focalPnt );
798     camera->SetViewUp( viewUp );
799     camera->SetParallelScale( parScale );
800     myTransform->SetMatrixScale( scale[0], scale[1], scale[2] );
801     myRWInteractor->Render();
802   }
803 }