1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "VTKViewer_ViewModel.h"
24 #include "VTKViewer_ViewWindow.h"
25 #include "VTKViewer_ViewManager.h"
26 #include "VTKViewer_RenderWindowInteractor.h"
28 #include "QtxBackgroundTool.h"
29 #include "SUIT_ViewWindow.h"
30 #include "SUIT_Desktop.h"
31 #include "SUIT_Session.h"
33 #include <QColorDialog>
35 #include <QMouseEvent>
38 // VSR: Uncomment below line to allow texture background support in VTK viewer
39 #define VTK_ENABLE_TEXTURED_BACKGROUND
41 bool _InitializeVtkWarningsCall()
43 char* isOn = getenv( "VTK_WARNINGS_IS_ON" );
44 if ( !isOn || strcmp( isOn, "1" ) )
45 vtkObject::GlobalWarningDisplayOff();
48 return vtkObject::GetGlobalWarningDisplay();
50 static bool _InitializeVtkWarnings = _InitializeVtkWarningsCall();
52 /*!Constructor.Sets background color to black.*/
53 VTKViewer_Viewer::VTKViewer_Viewer()
55 myDefaultBackground( Qtx::BackgroundData( Qt::black ) )
60 VTKViewer_Viewer::~VTKViewer_Viewer()
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 )
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
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;
86 return tr("BG_IMAGE_FILES");
89 /*!Gets background color [obsolete]*/
90 QColor VTKViewer_Viewer::backgroundColor() const
92 return background().color();
95 /*!Sets background color [obsolete]*/
96 void VTKViewer_Viewer::setBackgroundColor( const QColor& c )
98 Qtx::BackgroundData bg = background();
103 /*!Gets default background data.*/
104 Qtx::BackgroundData VTKViewer_Viewer::background() const
106 return myDefaultBackground;
109 /*!Sets default background data.*/
110 void VTKViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
112 myDefaultBackground = theBackground.isValid() ? theBackground : Qtx::BackgroundData( Qt::black );
115 /*!Create new instance of VTKViewer_ViewWindow, sets background color and return pointer to it.*/
116 SUIT_ViewWindow* VTKViewer_Viewer::createView( SUIT_Desktop* theDesktop )
118 VTKViewer_ViewWindow* vw = new VTKViewer_ViewWindow( theDesktop, this );
119 vw->setBackground( myDefaultBackground );
123 /*!Sets view manager and connect slots.*/
124 void VTKViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
126 SUIT_ViewModel::setViewManager(theViewManager);
127 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
128 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
130 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
131 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
133 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
134 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
137 /*!Insert context into popup menu.*/
138 void VTKViewer_Viewer::contextMenuPopup(QMenu* thePopup)
140 thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
141 thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
143 thePopup->addSeparator();
145 VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
146 if ( aView && !aView->getToolBar()->isVisible() )
147 thePopup->addAction( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
150 /*!On mouse press event.*/
151 void VTKViewer_Viewer::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
153 VTKViewer_RenderWindowInteractor* rwi = 0;
154 if ( vw && vw->inherits( "VTKViewer_ViewWindow" ) )
155 rwi = ((VTKViewer_ViewWindow*)vw)->getRWInteractor();
159 switch(event->buttons()) {
161 rwi->LeftButtonPressed(event) ;
164 rwi->MiddleButtonPressed(event) ;
166 case Qt::RightButton:
167 rwi->RightButtonPressed(event) ;
174 /*!On mouse move event.*/
175 void VTKViewer_Viewer::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
177 VTKViewer_RenderWindowInteractor* rwi = 0;
178 if ( vw && vw->inherits( "VTKViewer_ViewWindow" ) )
179 rwi = ((VTKViewer_ViewWindow*)vw)->getRWInteractor();
181 rwi->MouseMove( event );
184 /*!On mouse release event.*/
185 void VTKViewer_Viewer::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
187 VTKViewer_RenderWindowInteractor* rwi = 0;
188 if ( vw && vw->inherits( "VTKViewer_ViewWindow" ) )
189 rwi = ((VTKViewer_ViewWindow*)vw)->getRWInteractor();
193 switch(event->buttons()) {
195 rwi->LeftButtonReleased(event) ;
198 rwi->MiddleButtonReleased(event) ;
200 case Qt::RightButton:
201 rwi->RightButtonReleased(event) ;
208 /*!Sets flag to enable selection \a isEnable.*/
209 void VTKViewer_Viewer::enableSelection(bool isEnabled)
211 mySelectionEnabled = isEnabled;
212 //!! To be done for view windows
215 /*!Sets flag to multi selection enable \a isEnable.*/
216 void VTKViewer_Viewer::enableMultiselection(bool isEnable)
218 myMultiSelectionEnabled = isEnable;
219 //!! To be done for view windows
222 /*!On dump view event.*/
223 void VTKViewer_Viewer::onDumpView()
225 VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
230 /*!On change back ground color event.*/
231 void VTKViewer_Viewer::onChangeBackground()
233 VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
237 // get supported gradient types
238 QStringList gradList;
239 QIntList idList, txtList;
240 QString formats = backgroundData( gradList, idList, txtList );
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
254 // set chosen background data to the viewer
255 if ( bgData.isValid() )
256 aView->setBackground( bgData );
259 /*!On show tool bar event.*/
260 void VTKViewer_Viewer::onShowToolbar() {
261 VTKViewer_ViewWindow* aView = (VTKViewer_ViewWindow*)(myViewManager->getActiveView());
263 aView->getToolBar()->show();