Salome HOME
Merge from SKV_CCreator_from_HYDRO branch
[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->setObjectName( "VTKViewerViewOperations" );
96   myToolBar->setFloatable( false );
97
98   createActions();
99   createToolBar();
100
101   connect( myRenderWindow, SIGNAL(KeyPressed( QKeyEvent* )),
102            this,           SLOT(onKeyPressed( QKeyEvent* )) );
103   connect( myRenderWindow, SIGNAL(KeyReleased( QKeyEvent* )),
104            this,           SLOT(onKeyReleased( QKeyEvent* )) );
105   connect( myRenderWindow, SIGNAL(MouseButtonPressed( QMouseEvent* )),
106            this,           SLOT(onMousePressed( QMouseEvent* )) );
107   connect( myRenderWindow, SIGNAL(MouseButtonReleased( QMouseEvent* )),
108            this,           SLOT(onMouseReleased( QMouseEvent* )) );
109   connect( myRenderWindow, SIGNAL(MouseDoubleClicked( QMouseEvent* )),
110            this,           SLOT(onMouseDoubleClicked( QMouseEvent* )) );
111   connect( myRenderWindow, SIGNAL(MouseMove( QMouseEvent* )),
112            this,           SLOT(onMouseMoving( QMouseEvent* )) );
113   connect( myRWInteractor, SIGNAL(RenderWindowModified()),
114            myRenderWindow, SLOT(update()) );
115
116   connect( myRenderWindow, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
117            this,           SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
118
119   connect( myRWInteractor, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
120            this,           SIGNAL(contextMenuRequested( QContextMenuEvent * )) );
121
122
123   // set default background
124   setBackground( Qtx::BackgroundData( Qt::black ) );
125   // reset view
126   onResetView();
127 }
128
129 /*!Destructor.*/
130 VTKViewer_ViewWindow::~VTKViewer_ViewWindow()
131 {
132   myTransform->Delete();
133   // In order to ensure that the interactor unregisters
134   // this RenderWindow, we assign a NULL RenderWindow to 
135   // it before deleting it.
136   myRWInteractor->SetRenderWindow( NULL );
137   myRWInteractor->Delete();
138   
139   //m_RW->Delete() ;
140   myRenderer->RemoveAllViewProps();
141   //m_Renderer->Delete() ;
142   myTrihedron->Delete();
143 }
144
145 /*!Checks: is trihedron displayed.*/
146 bool VTKViewer_ViewWindow::isTrihedronDisplayed(){
147   return myTrihedron->GetVisibility() == VTKViewer_Trihedron::eOn;
148 }
149
150 /*!Activates 'zooming' transformation*/
151 void VTKViewer_ViewWindow::activateZoom()
152 {
153   myRWInteractor->GetInteractorStyle()->startZoom();
154 }
155
156 /*!Activates 'panning' transformation*/
157 void VTKViewer_ViewWindow::activatePanning()
158 {
159   myRWInteractor->GetInteractorStyle()->startPan();
160 }
161
162 /*!Activates 'rotation' transformation*/
163 void VTKViewer_ViewWindow::activateRotation()
164 {
165   myRWInteractor->GetInteractorStyle()->startRotate();
166 }
167
168 /*!Activate global panning.*/
169 void VTKViewer_ViewWindow::activateGlobalPanning()
170 {
171   //if(myTrihedron->GetVisibleActorCount(myRenderer))
172   myRWInteractor->GetInteractorStyle()->startGlobalPan();
173 }
174
175 /*!Activates 'fit area' transformation*/
176 void VTKViewer_ViewWindow::activateWindowFit()
177 {
178   myRWInteractor->GetInteractorStyle()->startFitArea();
179 }
180
181 /*!Create actions:*/
182 void VTKViewer_ViewWindow::createActions()
183 {
184   if (!myActionsMap.isEmpty()) return;
185   
186   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
187   
188   QtxAction* aAction;
189
190   //! \li Dump view
191   aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_DUMP" ) ),
192                            tr( "MNU_DUMP_VIEW" ), 0, this);
193   aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
194   connect(aAction, SIGNAL(activated()), this, SLOT(onDumpView()));
195   myActionsMap[ DumpId ] = aAction;
196
197   //! \li FitAll
198   aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITALL" ) ),
199                            tr( "MNU_FITALL" ), 0, this);
200   aAction->setStatusTip(tr("DSC_FITALL"));
201   connect(aAction, SIGNAL(activated()), this, SLOT(onFitAll()));
202   myActionsMap[ FitAllId ] = aAction;
203
204   //! \li FitRect
205   aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FITAREA" ) ),
206                            tr( "MNU_FITRECT" ), 0, this);
207   aAction->setStatusTip(tr("DSC_FITRECT"));
208   connect(aAction, SIGNAL(activated()), this, SLOT(activateWindowFit()));
209   myActionsMap[ FitRectId ] = aAction;
210
211   //! \li Zoom
212   aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ZOOM" ) ),
213                            tr( "MNU_ZOOM_VIEW" ), 0, this);
214   aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
215   connect(aAction, SIGNAL(activated()), this, SLOT(activateZoom()));
216   myActionsMap[ ZoomId ] = aAction;
217
218   //! \li Panning
219   aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_PAN" ) ),
220                            tr( "MNU_PAN_VIEW" ), 0, this);
221   aAction->setStatusTip(tr("DSC_PAN_VIEW"));
222   connect(aAction, SIGNAL(activated()), this, SLOT(activatePanning()));
223   myActionsMap[ PanId ] = aAction;
224
225   //! \li Global Panning
226   aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_GLOBALPAN" ) ),
227                            tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
228   aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
229   connect(aAction, SIGNAL(activated()), this, SLOT(activateGlobalPanning()));
230   myActionsMap[ GlobalPanId ] = aAction;
231
232   //! \li Rotation
233   aAction = new QtxAction(tr("MNU_ROTATE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ROTATE" ) ),
234                            tr( "MNU_ROTATE_VIEW" ), 0, this);
235   aAction->setStatusTip(tr("DSC_ROTATE_VIEW"));
236   connect(aAction, SIGNAL(activated()), this, SLOT(activateRotation()));
237   myActionsMap[ RotationId ] = aAction;
238
239   //! \li Projections
240   aAction = new QtxAction(tr("MNU_FRONT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_FRONT" ) ),
241                            tr( "MNU_FRONT_VIEW" ), 0, this);
242   aAction->setStatusTip(tr("DSC_FRONT_VIEW"));
243   connect(aAction, SIGNAL(activated()), this, SLOT(onFrontView()));
244   myActionsMap[ FrontId ] = aAction;
245
246   //! \li Back view
247   aAction = new QtxAction(tr("MNU_BACK_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BACK" ) ),
248                            tr( "MNU_BACK_VIEW" ), 0, this);
249   aAction->setStatusTip(tr("DSC_BACK_VIEW"));
250   connect(aAction, SIGNAL(activated()), this, SLOT(onBackView()));
251   myActionsMap[ BackId ] = aAction;
252
253   //! \li Top view
254   aAction = new QtxAction(tr("MNU_TOP_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TOP" ) ),
255                            tr( "MNU_TOP_VIEW" ), 0, this);
256   aAction->setStatusTip(tr("DSC_TOP_VIEW"));
257   connect(aAction, SIGNAL(activated()), this, SLOT(onTopView()));
258   myActionsMap[ TopId ] = aAction;
259
260   //! \li Bottom view
261   aAction = new QtxAction(tr("MNU_BOTTOM_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_BOTTOM" ) ),
262                            tr( "MNU_BOTTOM_VIEW" ), 0, this);
263   aAction->setStatusTip(tr("DSC_BOTTOM_VIEW"));
264   connect(aAction, SIGNAL(activated()), this, SLOT(onBottomView()));
265   myActionsMap[ BottomId ] = aAction;
266
267   //! \li Left view
268   aAction = new QtxAction(tr("MNU_LEFT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_LEFT" ) ),
269                            tr( "MNU_LEFT_VIEW" ), 0, this);
270   aAction->setStatusTip(tr("DSC_LEFT_VIEW"));
271   connect(aAction, SIGNAL(activated()), this, SLOT(onLeftView()));
272   myActionsMap[ LeftId ] = aAction;
273
274   //! \li Right view
275   aAction = new QtxAction(tr("MNU_RIGHT_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RIGHT" ) ),
276                            tr( "MNU_RIGHT_VIEW" ), 0, this);
277   aAction->setStatusTip(tr("DSC_RIGHT_VIEW"));
278   connect(aAction, SIGNAL(activated()), this, SLOT(onRightView()));
279   myActionsMap[ RightId ] = aAction;
280
281   // \li Rotate anticlockwise
282   aAction = new QtxAction(tr("MNU_ANTICLOCKWISE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_ANTICLOCKWISE" ) ),
283                           tr( "MNU_ANTICLOCKWISE_VIEW" ), 0, this);
284   aAction->setStatusTip(tr("DSC_ANTICLOCKWISE_VIEW"));
285   connect(aAction, SIGNAL(triggered()), this, SLOT(onAntiClockWiseView()));
286   myActionsMap[ AntiClockWiseId ] = aAction;
287
288   // \li Rotate clockwise
289   aAction = new QtxAction(tr("MNU_CLOCKWISE_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_CLOCKWISE" ) ),
290                           tr( "MNU_CLOCKWISE_VIEW" ), 0, this);
291   aAction->setStatusTip(tr("DSC_CLOCKWISE_VIEW"));
292   connect(aAction, SIGNAL(triggered()), this, SLOT(onClockWiseView()));
293   myActionsMap[ ClockWiseId ] = aAction;
294
295   //! \li Reset
296   aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_RESET" ) ),
297                            tr( "MNU_RESET_VIEW" ), 0, this);
298   aAction->setStatusTip(tr("DSC_RESET_VIEW"));
299   connect(aAction, SIGNAL(activated()), this, SLOT(onResetView()));
300   myActionsMap[ ResetId ] = aAction;
301
302   //! \li Trihedron shown
303   aAction = new QtxAction(tr("MNU_SHOW_TRIHEDRON"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TRIHEDRON" ) ),
304                            tr( "MNU_SHOW_TRIHEDRON" ), 0, this);
305   aAction->setStatusTip(tr("DSC_SHOW_TRIHEDRON"));
306   connect(aAction, SIGNAL(activated()), this, SLOT(onTrihedronShow()));
307   myActionsMap[ TrihedronShowId ] = aAction;
308 }
309
310 /*!Create tool bar.*/
311 void VTKViewer_ViewWindow::createToolBar()
312 {
313   myToolBar->addAction( myActionsMap[DumpId] );
314   myToolBar->addAction( myActionsMap[TrihedronShowId] );
315
316   QtxMultiAction* aScaleAction = new QtxMultiAction( this );
317   aScaleAction->insertAction( myActionsMap[FitAllId] );
318   aScaleAction->insertAction( myActionsMap[FitRectId] );
319   aScaleAction->insertAction( myActionsMap[ZoomId] );
320   myToolBar->addAction( aScaleAction );
321
322   QtxMultiAction* aPanningAction = new QtxMultiAction( this );
323   aPanningAction->insertAction( myActionsMap[PanId] );
324   aPanningAction->insertAction( myActionsMap[GlobalPanId] );
325   myToolBar->addAction( aPanningAction );
326
327   myToolBar->addAction( myActionsMap[RotationId] );
328
329   QtxMultiAction* aViewsAction = new QtxMultiAction(myToolBar);
330   aViewsAction->insertAction( myActionsMap[FrontId] );
331   aViewsAction->insertAction( myActionsMap[BackId] );
332   aViewsAction->insertAction( myActionsMap[TopId] );
333   aViewsAction->insertAction( myActionsMap[BottomId] );
334   aViewsAction->insertAction( myActionsMap[LeftId] );
335   aViewsAction->insertAction( myActionsMap[RightId] );
336   myToolBar->addAction( aViewsAction );
337
338   myToolBar->addAction( myActionsMap[AntiClockWiseId] );
339   myToolBar->addAction( myActionsMap[ClockWiseId] );
340
341   myToolBar->addAction( myActionsMap[ResetId] );
342 }
343
344 /*!On front view event.*/
345 void VTKViewer_ViewWindow::onFrontView()
346 {
347   vtkCamera* camera = myRenderer->GetActiveCamera();
348   camera->SetPosition(1,0,0);
349   camera->SetViewUp(0,0,1);
350   camera->SetFocalPoint(0,0,0);
351   onFitAll();
352 }
353
354 /*!On back view slot.*/
355 void VTKViewer_ViewWindow::onBackView()
356 {
357   vtkCamera* camera = myRenderer->GetActiveCamera();
358   camera->SetPosition(-1,0,0);
359   camera->SetViewUp(0,0,1);
360   camera->SetFocalPoint(0,0,0);
361   onFitAll();
362 }
363
364 /*!On back view slot.*/
365 void VTKViewer_ViewWindow::onTopView()
366 {
367   vtkCamera* camera = myRenderer->GetActiveCamera();
368   camera->SetPosition(0,0,1);
369   camera->SetViewUp(0,1,0);
370   camera->SetFocalPoint(0,0,0);
371   onFitAll();
372 }
373
374 /*!On bottom view slot.*/
375 void VTKViewer_ViewWindow::onBottomView()
376 {
377   vtkCamera* camera = myRenderer->GetActiveCamera();
378   camera->SetPosition(0,0,-1);
379   camera->SetViewUp(0,1,0);
380   camera->SetFocalPoint(0,0,0);
381   onFitAll();
382 }
383
384 /*!On left view slot.*/
385 void VTKViewer_ViewWindow::onLeftView()
386 {
387   vtkCamera* camera = myRenderer->GetActiveCamera(); 
388   camera->SetPosition(0,-1,0);
389   camera->SetViewUp(0,0,1);
390   camera->SetFocalPoint(0,0,0);
391   onFitAll();
392 }
393
394 /*!On right view slot.*/
395 void VTKViewer_ViewWindow::onRightView()
396 {
397   vtkCamera* camera = myRenderer->GetActiveCamera();
398   camera->SetPosition(0,1,0);
399   camera->SetViewUp(0,0,1);
400   camera->SetFocalPoint(0,0,0);
401   onFitAll();
402 }
403
404 /*!
405   \brief Rotate view 90 degrees clockwise
406 */
407 void VTKViewer_ViewWindow::onClockWiseView()
408 {
409   vtkCamera* aCamera = myRenderer->GetActiveCamera(); 
410   aCamera->Roll(-90);
411   aCamera->OrthogonalizeViewUp();
412   Repaint();
413 }
414
415 /*!
416   \brief Rotate view 90 degrees conterclockwise
417 */
418 void VTKViewer_ViewWindow::onAntiClockWiseView()
419 {
420   vtkCamera* aCamera = myRenderer->GetActiveCamera(); 
421   aCamera->Roll(90);
422   aCamera->OrthogonalizeViewUp();
423   Repaint();
424 }
425
426 /*!On reset view slot.*/
427 void VTKViewer_ViewWindow::onResetView()
428 {
429   int aTriedronIsVisible = isTrihedronDisplayed();
430   myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
431   ::ResetCamera(myRenderer,true);  
432   vtkCamera* aCamera = myRenderer->GetActiveCamera();
433   aCamera->SetPosition(1,-1,1);
434   aCamera->SetViewUp(0,0,1);
435   ::ResetCamera(myRenderer,true);  
436   if(aTriedronIsVisible) myTrihedron->VisibilityOn();
437   else myTrihedron->VisibilityOff();
438   static double aCoeff = 3.0;
439   aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
440   Repaint();
441 }
442
443 /*!On fit all slot.*/
444 void VTKViewer_ViewWindow::onFitAll()
445 {
446   myRWInteractor->GetInteractorStyle()->ViewFitAll();
447   Repaint();
448 }
449
450 /*!Set background color of the viewport [obsolete]*/
451 void VTKViewer_ViewWindow::setBackgroundColor( const QColor& c )
452 {
453   Qtx::BackgroundData bg = background();
454   bg.setColor( c );
455   setBackground( bg );
456 }
457
458 /*!Returns background color of the viewport [obsolete]*/
459 QColor VTKViewer_ViewWindow::backgroundColor() const
460 {
461   return background().color();
462 }
463
464 /*!Set background of the viewport*/
465 void VTKViewer_ViewWindow::setBackground( const Qtx::BackgroundData& bgData )
466 {
467   bool ok = false;
468  
469   if ( bgData.isValid() ) {
470     switch ( bgData.mode() ) {
471     case Qtx::ColorBackground:
472       {
473         QColor c = bgData.color();
474         if ( c.isValid() ) {
475           // show solid-colored background
476           getRenderer()->SetTexturedBackground( false );  // cancel texture mode
477           getRenderer()->SetGradientBackground( false );  // cancel gradient mode
478           getRenderer()->SetBackground( c.red()/255.0,
479                                         c.green()/255.0,
480                                         c.blue()/255.0 ); // set background color
481           ok = true;
482         }
483         break;
484       }
485     case Qtx::SimpleGradientBackground:
486       {
487         QColor c1, c2;
488         int type = bgData.gradient( c1, c2 );
489         if ( c1.isValid() )
490         {
491           if ( !c2.isValid() )
492             c2 = c1;
493
494           // show two-color gradient background
495           getRenderer()->SetTexturedBackground( false );    // cancel texture mode
496           getRenderer()->SetGradientBackground( true );     // switch to gradient mode
497
498           VTKViewer_OpenGLRenderer* aRenderer =
499             VTKViewer_OpenGLRenderer::SafeDownCast( getRenderer() );
500           if( aRenderer )
501           {
502             aRenderer->SetGradientType( type );
503             aRenderer->SetBackground( c1.redF(), c1.greenF(), c1.blueF() );
504             aRenderer->SetBackground2( c2.redF(), c2.greenF(), c2.blueF() );
505             ok = true;
506           }
507         }
508         break;
509       }
510     case Qtx::CustomGradientBackground:
511       {
512         // NOT IMPLEMENTED YET
513         getRenderer()->SetTexturedBackground( false );  // cancel texture mode
514         getRenderer()->SetGradientBackground( false );  // cancel gradient mode
515         // .........
516         break;
517       }
518     default:
519       break;
520     }
521     if ( bgData.isTextureShown() ) {
522       QString fileName;
523       int textureMode = bgData.texture( fileName );
524       QFileInfo fi( fileName );
525       if ( !fileName.isEmpty() && fi.exists() ) {
526         // read texture from file
527         QString extension = fi.suffix().toLower();
528         vtkImageReader2* aReader = 0;
529         if ( extension == "jpg" || extension == "jpeg" )
530           aReader = vtkJPEGReader::New();
531         else if ( extension == "bmp" )
532           aReader = vtkBMPReader::New();
533         else if ( extension == "tif" || extension == "tiff" )
534           aReader = vtkTIFFReader::New();
535         else if ( extension == "png" )
536           aReader = vtkPNGReader::New();
537         else if ( extension == "mhd" || extension == "mha" )
538           aReader = vtkMetaImageReader::New();           
539         if ( aReader ) {
540           // create texture
541           aReader->SetFileName( fi.absoluteFilePath().toLatin1().constData() );
542           aReader->Update();
543           
544           VTKViewer_Texture* aTexture = VTKViewer_Texture::New();
545           vtkImageMapToColors* aMap = 0;
546           vtkAlgorithmOutput* anOutput;
547           /*
548           // special processing for BMP reader
549           vtkBMPReader* aBMPReader = (vtkBMPReader*)aReader;
550           if ( aBMPReader ) {
551             // Special processing for BMP file
552             aBMPReader->SetAllow8BitBMP(1);
553             
554             aMap = vtkImageMapToColors::New();
555             aMap->SetInputConnection( aBMPReader->GetOutputPort() );
556             aMap->SetLookupTable( (vtkScalarsToColors*)aBMPReader->GetLookupTable() );
557             aMap->SetOutputFormatToRGB();
558             
559             anOutput = aMap->GetOutputPort();
560           }
561           else {
562           }
563           */
564           anOutput = aReader->GetOutputPort( 0 );
565           aTexture->SetInputConnection( anOutput );
566           // set texture mode
567           // VSR: Currently, VTK only supports Stretch mode, so below code will give
568           // the same results for all modes
569           switch ( textureMode ) {
570           case Qtx::TileTexture:
571             aTexture->RepeatOn();
572             aTexture->EdgeClampOff();
573             aTexture->InterpolateOff();
574             break;
575           case Qtx::StretchTexture:
576             aTexture->RepeatOff();
577             aTexture->EdgeClampOff();
578             aTexture->InterpolateOn();
579             break;
580           case Qtx::CenterTexture:
581           default:
582             aTexture->RepeatOff();
583             aTexture->EdgeClampOn();
584             aTexture->InterpolateOff();
585             break;
586           }
587           // show textured background
588           getRenderer()->SetTexturedBackground( true );     // switch to texture mode
589           getRenderer()->SetBackgroundTexture( aTexture );  // set texture image
590           
591           // clean-up resources
592           if ( aMap )
593             aMap->Delete();
594           aReader->Delete();
595           aTexture->Delete();
596           ok = true;
597         }
598       }
599     }
600   }
601
602   if ( ok )
603     myBackground = bgData;
604 }
605
606 /*!Returns background of the viewport*/
607 Qtx::BackgroundData VTKViewer_ViewWindow::background() const
608 {
609   return myBackground;
610 }
611
612 /*!Repaint window. If \a theUpdateTrihedron is true - recalculate trihedron.*/
613 void VTKViewer_ViewWindow::Repaint(bool theUpdateTrihedron)
614 {
615   if (theUpdateTrihedron) onAdjustTrihedron();
616   myRenderWindow->update();
617 }
618
619 /*!Get scale of transformation filter.*/
620 void VTKViewer_ViewWindow::GetScale( double theScale[3] ) {
621   myTransform->GetScale( theScale );
622 }
623
624 /*!Set scale of transformation filter and repaint window.*/
625 void VTKViewer_ViewWindow::SetScale( double theScale[3] ) {
626   myTransform->SetMatrixScale( theScale[0], theScale[1], theScale[2] );
627   myRWInteractor->Render();
628   Repaint();
629 }
630
631 /*!Calculation trihedron size.*/
632 void VTKViewer_ViewWindow::onAdjustTrihedron(){   
633   if( !isTrihedronDisplayed() ) 
634     return;
635   int aVisibleNum = myTrihedron->GetVisibleActorCount(myRenderer);
636   if(aVisibleNum){
637     // calculating diagonal of visible props of the renderer
638     double bnd[6];
639     myTrihedron->VisibilityOff();
640     ::ComputeVisiblePropBounds(myRenderer,bnd);
641     myTrihedron->VisibilityOn();
642     double aLength = 0;
643     static bool CalcByDiag = false;
644     if(CalcByDiag){
645       aLength = sqrt((bnd[1]-bnd[0])*(bnd[1]-bnd[0])+
646                      (bnd[3]-bnd[2])*(bnd[3]-bnd[2])+
647                      (bnd[5]-bnd[4])*(bnd[5]-bnd[4]));
648     }else{
649       aLength = bnd[1]-bnd[0];
650       aLength = qMax((bnd[3]-bnd[2]),aLength);
651       aLength = qMax((bnd[5]-bnd[4]),aLength);
652     }
653    
654     static double aSizeInPercents = 105;
655     QString aSetting;// = SUIT_CONFIG->getSetting("Viewer:TrihedronSize");
656     if(!aSetting.isEmpty()) aSizeInPercents = aSetting.toFloat();
657
658     static double EPS_SIZE = 5.0E-3;
659     double aSize = myTrihedron->GetSize();
660     double aNewSize = aLength*aSizeInPercents/100.0;
661     // if the new trihedron size have sufficient difference, then apply the value
662     if(fabs(aNewSize-aSize) > aSize*EPS_SIZE || fabs(aNewSize-aSize) > aNewSize*EPS_SIZE){
663       myTrihedron->SetSize(aNewSize);
664     }
665   }
666   ::ResetCameraClippingRange(myRenderer);
667 }
668
669 /*!Emit key pressed.*/
670 void VTKViewer_ViewWindow::onKeyPressed(QKeyEvent* event)
671 {
672   emit keyPressed( this, event );
673 }
674
675 /*!Emit key released.*/
676 void VTKViewer_ViewWindow::onKeyReleased(QKeyEvent* event)
677 {
678   emit keyReleased( this, event );
679 }
680
681 /*!Emit key pressed.*/
682 void VTKViewer_ViewWindow::onMousePressed(QMouseEvent* event)
683 {
684   emit mousePressed(this, event);
685 }
686
687 /*!Emit mouse released.*/
688 void VTKViewer_ViewWindow::onMouseReleased(QMouseEvent* event)
689 {
690   emit mouseReleased( this, event );
691 }
692
693 /*!Emit mouse moving.*/
694 void VTKViewer_ViewWindow::onMouseMoving(QMouseEvent* event)
695 {
696   emit mouseMoving( this, event );
697 }
698
699 /*!Emit mouse double clicked.*/
700 void VTKViewer_ViewWindow::onMouseDoubleClicked( QMouseEvent* event )
701 {
702   emit mouseDoubleClicked( this, event );
703 }
704
705 /*!Insert actor to renderer and transformation filter.
706  *Move Internal actors, if \a theMoveInternalActors is true.
707  */
708 void VTKViewer_ViewWindow::InsertActor( VTKViewer_Actor* theActor, bool theMoveInternalActors ){
709   theActor->AddToRender(myRenderer);
710   theActor->SetTransform(myTransform);
711   if(theMoveInternalActors) 
712     myRWInteractor->MoveInternalActors();
713 }
714
715 /*!Add actor.Repaint window if \a theUpdate is true.
716  *@see InsertActor( VTKViewer_Actor* theActor, bool theMoveInternalActors )
717  */
718 void VTKViewer_ViewWindow::AddActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
719   InsertActor(theActor);
720   if(theUpdate) 
721     Repaint();
722 }
723
724 /*!Remove \a theActor from renderer and pepaint, if \a theUpdate is true.*/
725 void VTKViewer_ViewWindow::RemoveActor( VTKViewer_Actor* theActor, bool theUpdate /*=false*/ ){
726   theActor->RemoveFromRender(myRenderer);
727   if(theUpdate) 
728     Repaint();
729 }
730
731 /*!@see RemoveActor() and InsertActor().*/
732 void VTKViewer_ViewWindow::MoveActor( VTKViewer_Actor* theActor)
733 {
734   RemoveActor(theActor);
735   InsertActor(theActor,true);
736 }
737
738 /*!On trihedron show slot.*/
739 void VTKViewer_ViewWindow::onTrihedronShow()
740 {
741   if (isTrihedronDisplayed())
742     myTrihedron->VisibilityOff();
743   else
744     myTrihedron->VisibilityOn();
745   myRenderWindow->update();
746 }
747
748 /*!Dump view.*/
749 QImage VTKViewer_ViewWindow::dumpView()
750 {
751   QPixmap px = QPixmap::grabWindow( myRenderWindow->winId() );
752   return px.toImage();
753 }
754
755 /*! The method returns the visual parameters of this view as a formated string
756  */
757 QString VTKViewer_ViewWindow::getVisualParameters()
758 {
759   double pos[3], focalPnt[3], viewUp[3], parScale, scale[3];
760
761   vtkCamera* camera = myRenderer->GetActiveCamera();
762   camera->GetPosition( pos );
763   camera->GetFocalPoint( focalPnt );
764   camera->GetViewUp( viewUp );
765   parScale = camera->GetParallelScale();
766   GetScale( scale );
767
768   QString retStr;
769   retStr.sprintf( "%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e*%.12e", 
770                   pos[0], pos[1], pos[2], focalPnt[0], focalPnt[1], focalPnt[2], viewUp[0], viewUp[1], 
771                   viewUp[2], parScale, scale[0], scale[1], scale[2] );
772   return retStr;
773 }
774
775 /*! The method restors visual parameters of this view from a formated string
776  */
777 void VTKViewer_ViewWindow::setVisualParameters( const QString& parameters )
778 {
779   QStringList paramsLst = parameters.split( '*' );
780   if ( paramsLst.size() == 13 ) {
781     double pos[3], focalPnt[3], viewUp[3], parScale, scale[3];
782     pos[0] = paramsLst[0].toDouble();
783     pos[1] = paramsLst[1].toDouble();
784     pos[2] = paramsLst[2].toDouble();
785     focalPnt[0] = paramsLst[3].toDouble();
786     focalPnt[1] = paramsLst[4].toDouble();
787     focalPnt[2] = paramsLst[5].toDouble();
788     viewUp[0] = paramsLst[6].toDouble();
789     viewUp[1] = paramsLst[7].toDouble();
790     viewUp[2] = paramsLst[8].toDouble();
791     parScale = paramsLst[9].toDouble();
792     scale[0] = paramsLst[10].toDouble();
793     scale[1] = paramsLst[11].toDouble();
794     scale[2] = paramsLst[12].toDouble();
795
796     vtkCamera* camera = myRenderer->GetActiveCamera();
797     camera->SetPosition( pos );
798     camera->SetFocalPoint( focalPnt );
799     camera->SetViewUp( viewUp );
800     camera->SetParallelScale( parScale );
801     myTransform->SetMatrixScale( scale[0], scale[1], scale[2] );
802     myRWInteractor->Render();
803   }
804 }