Salome HOME
027dd0da642ff9aceb3a37ee8628c7a3354e6106
[modules/gui.git] / src / VTKViewer / VTKViewer_ViewModel.cxx
1 // Copyright (C) 2007-2022  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_ViewModel.h"
24 #include "VTKViewer_ViewWindow.h"
25 #include "VTKViewer_ViewManager.h"
26 #include "VTKViewer_RenderWindowInteractor.h"
27
28 #include "QtxBackgroundTool.h"
29 #include "SUIT_ViewWindow.h"
30 #include "SUIT_Desktop.h"
31 #include "SUIT_Session.h"
32
33 #include <QColorDialog>
34 #include <QMenu>
35 #include <QMouseEvent>
36 #include <QToolBar>
37
38 // VSR: Uncomment below line to allow texture background support in VTK viewer
39 #define VTK_ENABLE_TEXTURED_BACKGROUND
40
41 bool _InitializeVtkWarningsCall()
42 {
43   char* isOn = getenv( "VTK_WARNINGS_IS_ON" );  
44   if ( !isOn || strcmp( isOn, "1" ) )
45     vtkObject::GlobalWarningDisplayOff();
46
47   delete isOn;
48   return vtkObject::GetGlobalWarningDisplay();
49 }
50 static bool _InitializeVtkWarnings = _InitializeVtkWarningsCall();
51
52 /*!Constructor.Sets background color to black.*/
53 VTKViewer_Viewer::VTKViewer_Viewer()
54 : SUIT_ViewModel(),
55   myDefaultBackground( Qtx::BackgroundData( Qt::black ) )
56 {
57 }
58
59 /*!Destructor.*/
60 VTKViewer_Viewer::~VTKViewer_Viewer() 
61 {
62 }
63
64 /*! Get data for supported background modes: gradient types, identifiers and supported image formats */
65 QString VTKViewer_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
66 {
67   gradList << tr( "GT_HORIZONTALGRADIENT" )
68            << tr( "GT_VERTICALGRADIENT" )
69            << tr( "GT_FIRSTDIAGONALGRADIENT" )
70            << tr( "GT_SECONDDIAGONALGRADIENT" )
71            << tr( "GT_FIRSTCORNERGRADIENT" )
72            << tr( "GT_SECONDCORNERGRADIENT" )
73            << tr( "GT_THIRDCORNERGRADIENT" )
74            << tr( "GT_FOURTHCORNERGRADIENT" );
75   idList   << HorizontalGradient
76            << VerticalGradient
77            << FirstDiagonalGradient
78            << SecondDiagonalGradient
79            << FirstCornerGradient
80            << SecondCornerGradient
81            << ThirdCornerGradient
82            << FourthCornerGradient;
83 #ifdef VTK_ENABLE_TEXTURED_BACKGROUND
84   txtList  << Qtx::CenterTexture << Qtx::TileTexture << Qtx::StretchTexture;
85 #endif
86   return tr("BG_IMAGE_FILES");
87 }
88
89 /*!Gets background color [obsolete]*/
90 QColor VTKViewer_Viewer::backgroundColor() const
91 {
92   return background().color();
93 }
94
95 /*!Sets background color [obsolete]*/
96 void VTKViewer_Viewer::setBackgroundColor( const QColor& c )
97 {
98   Qtx::BackgroundData bg = background();
99   bg.setColor( c );
100   setBackground( bg );
101 }
102
103 /*!Gets default background data.*/
104 Qtx::BackgroundData VTKViewer_Viewer::background() const
105 {
106   return myDefaultBackground;
107 }
108
109 /*!Sets default background data.*/
110 void VTKViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
111 {
112   myDefaultBackground = theBackground.isValid() ? theBackground : Qtx::BackgroundData( Qt::black );
113 }
114
115 /*!Create new instance of VTKViewer_ViewWindow, sets background color and return pointer to it.*/
116 SUIT_ViewWindow* VTKViewer_Viewer::createView( SUIT_Desktop* theDesktop )
117 {
118   VTKViewer_ViewWindow* vw = new VTKViewer_ViewWindow( theDesktop, this );
119   vw->setBackground( myDefaultBackground );
120   return vw;
121 }
122
123 /*!Sets view manager and connect slots.*/
124 void VTKViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
125 {
126   SUIT_ViewModel::setViewManager(theViewManager);
127   connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
128           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
129
130   connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
131           this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
132
133   connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
134           this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
135 }
136
137 /*!Insert context into popup menu.*/
138 void VTKViewer_Viewer::contextMenuPopup(QMenu* thePopup)
139 {
140   thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
141   thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
142
143   thePopup->addSeparator();
144
145   VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
146   if ( aView && !aView->getToolBar()->isVisible() )
147     thePopup->addAction( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
148 }
149
150 /*!On mouse press event.*/
151 void VTKViewer_Viewer::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
152 {
153   VTKViewer_RenderWindowInteractor* rwi = 0;
154   if ( vw && vw->inherits( "VTKViewer_ViewWindow" ) )
155     rwi = ((VTKViewer_ViewWindow*)vw)->getRWInteractor();
156   if ( !rwi )
157     return;
158
159   switch(event->buttons()) {
160   case Qt::LeftButton:
161     rwi->LeftButtonPressed(event) ;
162     break ;
163   case Qt::MidButton:
164     rwi->MiddleButtonPressed(event) ;
165     break ;
166   case Qt::RightButton:
167     rwi->RightButtonPressed(event) ;
168     break;
169   default:
170     break ;
171   }
172 }
173
174 /*!On mouse move event.*/
175 void VTKViewer_Viewer::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
176 {
177   VTKViewer_RenderWindowInteractor* rwi = 0;
178   if ( vw && vw->inherits( "VTKViewer_ViewWindow" ) )
179     rwi = ((VTKViewer_ViewWindow*)vw)->getRWInteractor();
180   if ( rwi )
181     rwi->MouseMove( event );
182 }
183
184 /*!On mouse release event.*/
185 void VTKViewer_Viewer::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
186 {
187   VTKViewer_RenderWindowInteractor* rwi = 0;
188   if ( vw && vw->inherits( "VTKViewer_ViewWindow" ) )
189     rwi = ((VTKViewer_ViewWindow*)vw)->getRWInteractor();
190   if ( !rwi )
191     return;
192
193   switch(event->buttons()) {
194   case Qt::LeftButton:
195     rwi->LeftButtonReleased(event) ;
196     break ;
197   case Qt::MidButton:
198     rwi->MiddleButtonReleased(event) ;
199     break ;
200   case Qt::RightButton:
201     rwi->RightButtonReleased(event) ;
202     break;
203   default:
204     break ;
205   }
206 }
207
208 /*!Sets flag to enable selection \a isEnable.*/
209 void VTKViewer_Viewer::enableSelection(bool isEnabled)
210 {
211   mySelectionEnabled = isEnabled;
212   //!! To be done for view windows
213 }
214
215 /*!Sets flag to multi selection enable \a isEnable.*/
216 void VTKViewer_Viewer::enableMultiselection(bool isEnable)
217 {
218   myMultiSelectionEnabled = isEnable;
219   //!! To be done for view windows
220 }
221
222 /*!On dump view event.*/
223 void VTKViewer_Viewer::onDumpView()
224 {
225   VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
226   if ( aView )
227     aView->onDumpView();
228 }
229
230 /*!On change back ground color event.*/
231 void VTKViewer_Viewer::onChangeBackground()
232 {
233   VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
234   if ( !aView )
235     return;
236
237   // get supported gradient types
238   QStringList gradList;
239   QIntList    idList, txtList;
240   QString     formats = backgroundData( gradList, idList, txtList );
241
242   // invoke dialog box
243   Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(),  // initial background
244                                                                    aView,                // parent for dialog box
245                                                                    txtList,              // allowed texture modes
246                                                                    true,                 // enable solid color mode
247                                                                    true,                 // enable gradient mode
248                                                                    false,                // disable custom gradient mode
249                                                                    !txtList.isEmpty(),   // enable texture mode
250                                                                    gradList,             // gradient names
251                                                                    idList,               // gradient identifiers
252                                                                    formats );            // image formats
253
254   // set chosen background data to the viewer
255   if ( bgData.isValid() )
256     aView->setBackground( bgData );
257 }
258
259 /*!On show tool bar event.*/
260 void VTKViewer_Viewer::onShowToolbar() {
261   VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
262   if ( aView )
263     aView->getToolBar()->show();    
264 }