1 // Copyright (C) 2007-2012 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.
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 // GEOM GEOMGUI : GUI for Geometry component
24 // File : OperationGUI_ClippingDlg.cxx
25 // Author : Michael Zorin, Open CASCADE S.A.S. (mikhail.zorin@opencascade.com)
27 #include "OperationGUI_ClippingDlg.h"
30 #include <GeometryGUI.h>
32 #include <SalomeApp_DoubleSpinBox.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_Desktop.h>
36 #include <SUIT_ViewManager.h>
37 #include <SVTK_ViewWindow.h>
38 #include <SVTK_ViewModel.h>
40 #include <OCCViewer_ViewModel.h>
41 #include <OCCViewer_ViewWindow.h>
42 #include <OCCViewer_ViewPort3d.h>
44 #include <VTKViewer.h>
47 #include <V3d_View.hxx>
51 #include <vtkCamera.h>
52 #include <vtkRenderer.h>
54 //=================================================================================
55 // class : OperationGUI_ClippingDlg()
57 //=================================================================================
58 OperationGUI_ClippingDlg::OperationGUI_ClippingDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
59 : GEOMBase_Skeleton( theGeometryGUI, parent, false )
61 setWindowTitle( tr( "Change clipping range" ) );
63 mainFrame()->GroupConstructors->setAttribute( Qt::WA_DeleteOnClose );
64 mainFrame()->GroupConstructors->close();
65 mainFrame()->GroupBoxName->setAttribute( Qt::WA_DeleteOnClose );
66 mainFrame()->GroupBoxName->close();
68 /***************************************************************/
69 GroupArguments = new QGroupBox( centralWidget() );
70 QGridLayout* GroupArgumentsLayout = new QGridLayout( GroupArguments );
71 GroupArgumentsLayout->setSpacing( 6 );
72 GroupArgumentsLayout->setMargin( 9 );
75 TextLabelNear = new QLabel( tr( "Near" ), GroupArguments );
76 GroupArgumentsLayout->addWidget( TextLabelNear, 0, 0 );
78 SpinBox_Near = new SalomeApp_DoubleSpinBox( GroupArguments );
79 initSpinBox( SpinBox_Near, COORD_MIN, COORD_MAX, 10.0, "length_precision" );
80 GroupArgumentsLayout->addWidget( SpinBox_Near, 0, 1 );
82 TextLabelFar = new QLabel( tr( "Far" ), GroupArguments );
83 GroupArgumentsLayout->addWidget( TextLabelFar, 0, 2 );
85 SpinBox_Far = new SalomeApp_DoubleSpinBox( GroupArguments );
86 initSpinBox( SpinBox_Far, COORD_MIN, COORD_MAX, 10.0, "length_precision" );
87 GroupArgumentsLayout->addWidget( SpinBox_Far, 0, 3 );
89 resetButton = new QPushButton( tr( "Reset" ), GroupArguments );
90 GroupArgumentsLayout->addWidget( resetButton, 0, 4 );
92 TypeCB = new QComboBox( GroupArguments );
93 TypeCB->addItem( tr( "OFF" ) );
94 TypeCB->addItem( tr( "BACK" ) );
95 TypeCB->addItem( tr( "FRONT" ) );
96 TypeCB->addItem( tr( "SLICE" ) );
97 GroupArgumentsLayout->addWidget( TypeCB, 1, 0, 1, 5 );
99 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
100 layout->setMargin( 0 ); layout->setSpacing( 6 );
101 layout->addWidget( GroupArguments );
103 /* signals and slots connections */
104 connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
105 connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
107 connect( resetButton, SIGNAL( clicked() ), this, SLOT( onReset() ) ) ;
109 setHelpFileName( "none.htm" );
114 //=================================================================================
115 // function : ~ OperationGUI_ClippingDlg()
116 // purpose : Destroys the object and frees any allocated resources
117 //=================================================================================
118 OperationGUI_ClippingDlg::~ OperationGUI_ClippingDlg()
120 // no need to delete child widgets, Qt does it all for us
123 //=================================================================================
126 //=================================================================================
127 void OperationGUI_ClippingDlg::Init()
129 SUIT_ViewWindow* anActiveWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
130 if ( !anActiveWindow )
133 if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
134 SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
138 TextLabelNear->setText( tr( "Near" ) );
139 TextLabelFar->setText( tr( "Far" ) );
143 vtkRenderer* aRenderer = aVTKFrame->getRenderer();
144 if ( !aRenderer ) return;
146 vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
147 if ( anActiveCamera == NULL ) {
151 // Find the plane equation for the camera view plane
153 anActiveCamera->GetViewPlaneNormal( vn );
155 anActiveCamera->GetPosition( position );
157 vtkFloatingPointType bounds[6];
158 aRenderer->ComputeVisiblePropBounds( bounds );
161 center[0] = ( bounds[0] + bounds[1] ) / 2.0;
162 center[1] = ( bounds[2] + bounds[3] ) / 2.0;
163 center[2] = ( bounds[4] + bounds[5] ) / 2.0;
165 double width = sqrt( ( bounds[1]-bounds[0] ) * ( bounds[1]-bounds[0] ) +
166 ( bounds[3]-bounds[2] ) * ( bounds[3]-bounds[2] ) +
167 ( bounds[5]-bounds[4] ) * ( bounds[5]-bounds[4] ) );
169 double distance = sqrt( ( position[0]-center[0] ) * ( position[0]-center[0] ) +
170 ( position[1]-center[1] ) * ( position[1]-center[1] ) +
171 ( position[2]-center[2] ) * ( position[2]-center[2] ) );
173 vtkFloatingPointType range[2] = { distance - width/2.0, distance + width/2.0 };
175 SpinBox_Near->setValue( range[0] );
176 SpinBox_Far->setValue( range[1] );
180 else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
181 OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
183 TextLabelNear->setText( tr( "Depth" ) );
184 TextLabelFar->setText( tr( "Thickness" ) );
186 Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
188 double depth, thickness;
189 int ztype = view3d->ZClipping( depth, thickness );
190 SpinBox_Near->setValue( depth );
191 SpinBox_Far->setValue( thickness );
192 TypeCB->setCurrentIndex( ztype );
194 //Handle(V3d_Plane) aPlane = new V3d_Plane( aOCCFrame->getViewer()->getViewer3d() ,0, 0, 1, -100);
195 //aPlane->Display(view3d);
200 //=================================================================================
201 // function : ClickOnApply()
203 //=================================================================================
204 bool OperationGUI_ClippingDlg::ClickOnApply()
206 SUIT_ViewWindow* anActiveWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
207 if ( !anActiveWindow )
210 if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
211 SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
215 vtkRenderer* aRenderer = aVTKFrame->getRenderer();
216 if ( !aRenderer ) return false;
218 vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
219 if ( anActiveCamera == NULL ) {
223 vtkFloatingPointType range[2] = { SpinBox_Near->value(), SpinBox_Far->value() };
224 if ( range[0] < 0.0 ) range[0] = 0.0;
225 anActiveCamera->SetClippingRange( range );
229 else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
230 OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
232 Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
234 double depth = SpinBox_Near->value();
235 double thickness = SpinBox_Far->value();
236 int aType = TypeCB->currentIndex();
238 view3d->SetZClippingType( V3d_TypeOfZclipping( aType ) );
239 view3d->SetZClippingDepth( depth );
240 view3d->SetZClippingWidth( thickness );
246 //=================================================================================
247 // function : ClickOnOk()
249 //=================================================================================
250 void OperationGUI_ClippingDlg::ClickOnOk()
252 setIsApplyAndClose( true );
253 if ( ClickOnApply() )
257 //=================================================================================
258 // function : onActivate()
260 //=================================================================================
261 void OperationGUI_ClippingDlg::onActivate()
263 GEOMBase_Skeleton::ActivateThisDialog();
266 //=================================================================================
267 // function : enterEvent()
269 //=================================================================================
270 void OperationGUI_ClippingDlg::enterEvent( QEvent* )
272 this->setEnabled( true );
275 //=================================================================================
276 // function : onReset()
278 //=================================================================================
279 void OperationGUI_ClippingDlg::onReset()
281 SUIT_ViewWindow* anActiveWindow =
282 SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
283 if ( !anActiveWindow )
286 if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
287 SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
291 vtkRenderer* aRenderer = aVTKFrame->getRenderer();
292 if ( !aRenderer ) return;
294 vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
295 if ( anActiveCamera == NULL ) {
299 // Find the plane equation for the camera view plane
301 anActiveCamera->GetViewPlaneNormal( vn );
303 anActiveCamera->GetPosition( position );
305 vtkFloatingPointType bounds[6];
306 aRenderer->ComputeVisiblePropBounds( bounds );
309 center[0] = ( bounds[0] + bounds[1] ) / 2.0;
310 center[1] = ( bounds[2] + bounds[3] ) / 2.0;
311 center[2] = ( bounds[4] + bounds[5] ) / 2.0;
313 double width = sqrt( ( bounds[1]-bounds[0] ) * ( bounds[1]-bounds[0] ) +
314 ( bounds[3]-bounds[2] ) * ( bounds[3]-bounds[2] ) +
315 ( bounds[5]-bounds[4] ) * ( bounds[5]-bounds[4] ) );
317 double distance = sqrt( ( position[0]-center[0] ) * ( position[0]-center[0] ) +
318 ( position[1]-center[1] ) * ( position[1]-center[1] ) +
319 ( position[2]-center[2] ) * ( position[2]-center[2] ) );
321 vtkFloatingPointType range[2] = { distance - width/2.0, distance + width/2.0 };
323 SpinBox_Near->setValue( range[0] );
324 SpinBox_Far->setValue( range[1] );
328 else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
329 OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
331 TextLabelNear->setText( tr( "Depth" ) );
332 TextLabelFar->setText( tr( "Thickness" ) );
334 Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
336 view3d->SetZClippingType( V3d_TypeOfZclipping( 0 ) );
338 double depth, thickness;
339 int ztype= view3d->ZClipping( depth, thickness );
340 SpinBox_Near->setValue( 0 );
341 SpinBox_Far->setValue( 1000 );
342 TypeCB->setCurrentIndex( ztype );