Salome HOME
b24298447ba6957013f283e4dfdba933e80436aa
[modules/geom.git] / src / OperationGUI / OperationGUI_ClippingDlg.cxx
1 //  Copyright (C) 2007-2008  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.
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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : OperationGUI_ClippingDlg.cxx
24 // Author : Michael Zorin, Open CASCADE S.A.S. (mikhail.zorin@opencascade.com)
25 //
26 #include "OperationGUI_ClippingDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31 #include <QtxDoubleSpinBox.h>
32
33 #include <SUIT_Session.h>
34 #include <SUIT_Desktop.h>
35 #include <SUIT_ViewManager.h>
36 #include <SVTK_ViewWindow.h>
37 #include <SVTK_ViewModel.h>
38
39 #include <OCCViewer_ViewModel.h>
40 #include <OCCViewer_ViewWindow.h>
41 #include <OCCViewer_ViewPort3d.h>
42
43 #include <VTKViewer.h>
44
45 // OCC Includes
46 #include <V3d_View.hxx>
47 #include <V3d.hxx>
48
49 // VTK Includes
50 #include <vtkCamera.h>
51 #include <vtkRenderer.h>
52
53 //=================================================================================
54 // class    : OperationGUI_ClippingDlg()
55 // purpose  :
56 //=================================================================================
57 OperationGUI_ClippingDlg::OperationGUI_ClippingDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
58   : GEOMBase_Skeleton( theGeometryGUI, parent, false )
59 {
60   setWindowTitle( tr( "Change clipping range" ) );
61
62   mainFrame()->GroupConstructors->setAttribute( Qt::WA_DeleteOnClose );
63   mainFrame()->GroupConstructors->close();
64   mainFrame()->GroupBoxName->setAttribute( Qt::WA_DeleteOnClose );
65   mainFrame()->GroupBoxName->close();
66
67   /***************************************************************/
68   GroupArguments = new QGroupBox( centralWidget() );
69   QGridLayout* GroupArgumentsLayout = new QGridLayout( GroupArguments );
70   GroupArgumentsLayout->setSpacing( 6 );
71   GroupArgumentsLayout->setMargin( 9 );
72
73   // Controls
74   TextLabelNear = new QLabel( tr( "Near" ), GroupArguments );
75   GroupArgumentsLayout->addWidget( TextLabelNear, 0, 0 );
76
77   SpinBox_Near = new SalomeApp_DoubleSpinBox( COORD_MIN, COORD_MAX, 10.0, 3, 10, GroupArguments );
78   GroupArgumentsLayout->addWidget( SpinBox_Near, 0, 1 );
79
80   TextLabelFar = new QLabel( tr( "Far" ), GroupArguments );
81   GroupArgumentsLayout->addWidget( TextLabelFar, 0, 2 );
82
83   SpinBox_Far = new SalomeApp_DoubleSpinBox( COORD_MIN, COORD_MAX, 10.0, 3, 10, GroupArguments );
84   GroupArgumentsLayout->addWidget( SpinBox_Far, 0, 3 );
85
86   resetButton  = new QPushButton( tr( "Reset" ), GroupArguments );
87   GroupArgumentsLayout->addWidget( resetButton, 0, 4 );
88
89   TypeCB = new QComboBox( GroupArguments );
90   TypeCB->addItem( tr( "OFF" ) );
91   TypeCB->addItem( tr( "BACK" ) );
92   TypeCB->addItem( tr( "FRONT" ) );
93   TypeCB->addItem( tr( "SLICE" ) );
94   GroupArgumentsLayout->addWidget( TypeCB, 1, 0, 1, 5 );
95
96   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
97   layout->setMargin( 0 ); layout->setSpacing( 6 );
98   layout->addWidget( GroupArguments );
99
100   /* signals and slots connections */
101   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
102   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
103
104   connect( resetButton,   SIGNAL( clicked() ), this, SLOT( onReset() ) ) ;
105
106   setHelpFileName( "none.htm" );
107
108   Init();
109 }
110
111 //=================================================================================
112 // function : ~ OperationGUI_ClippingDlg()
113 // purpose  : Destroys the object and frees any allocated resources
114 //=================================================================================
115 OperationGUI_ClippingDlg::~ OperationGUI_ClippingDlg()
116 {
117   // no need to delete child widgets, Qt does it all for us
118 }
119
120 //=================================================================================
121 // function : Init()
122 // purpose  :
123 //=================================================================================
124 void OperationGUI_ClippingDlg::Init()
125 {
126   SUIT_ViewWindow* anActiveWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
127   if ( !anActiveWindow )
128     return;
129
130   if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
131     SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
132     if ( !aVTKFrame )
133       return;
134
135     TextLabelNear->setText( tr( "Near" ) );
136     TextLabelFar->setText( tr( "Far" ) );
137
138     TypeCB->hide();
139
140     vtkRenderer* aRenderer = aVTKFrame->getRenderer();
141     if ( !aRenderer ) return;
142
143     vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
144     if ( anActiveCamera == NULL ) {
145       return;
146     }
147
148       // Find the plane equation for the camera view plane
149     double vn[3];
150     anActiveCamera->GetViewPlaneNormal( vn );
151     double  position[3];
152     anActiveCamera->GetPosition( position );
153
154     vtkFloatingPointType bounds[6];
155     aRenderer->ComputeVisiblePropBounds( bounds );
156
157     double center[3];
158     center[0] = ( bounds[0] + bounds[1] ) / 2.0;
159     center[1] = ( bounds[2] + bounds[3] ) / 2.0;
160     center[2] = ( bounds[4] + bounds[5] ) / 2.0;
161
162     double width = sqrt( ( bounds[1]-bounds[0] ) * ( bounds[1]-bounds[0] ) +
163                          ( bounds[3]-bounds[2] ) * ( bounds[3]-bounds[2] ) +
164                          ( bounds[5]-bounds[4] ) * ( bounds[5]-bounds[4] ) );
165
166     double distance = sqrt( ( position[0]-center[0] ) * ( position[0]-center[0] ) +
167                             ( position[1]-center[1] ) * ( position[1]-center[1] ) +
168                             ( position[2]-center[2] ) * ( position[2]-center[2] ) );
169
170     vtkFloatingPointType range[2] = { distance - width/2.0, distance + width/2.0 };
171
172     SpinBox_Near->setValue( range[0] );
173     SpinBox_Far->setValue( range[1] );
174
175     return;
176   }
177   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
178     OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
179
180     TextLabelNear->setText( tr( "Depth" ) );
181     TextLabelFar->setText( tr( "Thickness" ) );
182
183     Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
184
185     double depth, thickness;
186     int ztype = view3d->ZClipping( depth, thickness );
187     SpinBox_Near->setValue( depth );
188     SpinBox_Far->setValue( thickness );
189     TypeCB->setCurrentIndex( ztype );
190
191     //Handle(V3d_Plane) aPlane = new V3d_Plane( aOCCFrame->getViewer()->getViewer3d() ,0, 0, 1, -100);
192     //aPlane->Display(view3d);
193   }
194 }
195
196
197 //=================================================================================
198 // function : ClickOnApply()
199 // purpose  :
200 //=================================================================================
201 bool OperationGUI_ClippingDlg::ClickOnApply()
202 {
203   SUIT_ViewWindow* anActiveWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
204   if ( !anActiveWindow )
205     return false;
206
207   if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
208     SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
209     if ( !aVTKFrame )
210       return false;
211
212     vtkRenderer* aRenderer = aVTKFrame->getRenderer();
213     if ( !aRenderer ) return false;
214
215     vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
216     if ( anActiveCamera == NULL ) {
217       return false;
218     }
219
220     vtkFloatingPointType range[2] = { SpinBox_Near->value(), SpinBox_Far->value() };
221     if ( range[0] < 0.0 ) range[0] = 0.0;
222     anActiveCamera->SetClippingRange( range );
223
224     return true;
225   }
226   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
227     OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
228
229     Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
230
231     double depth = SpinBox_Near->value();
232     double thickness = SpinBox_Far->value();
233     int aType = TypeCB->currentIndex();
234
235     view3d->SetZClippingType( V3d_TypeOfZclipping( aType ) );
236     view3d->SetZClippingDepth( depth );
237     view3d->SetZClippingWidth( thickness );
238   }
239
240   return true;
241 }
242
243 //=================================================================================
244 // function : ClickOnOk()
245 // purpose  :
246 //=================================================================================
247 void OperationGUI_ClippingDlg::ClickOnOk()
248 {
249   if ( ClickOnApply() )
250     ClickOnCancel();
251 }
252
253 //=================================================================================
254 // function : onActivate()
255 // purpose  :
256 //=================================================================================
257 void OperationGUI_ClippingDlg::onActivate()
258 {
259   GEOMBase_Skeleton::ActivateThisDialog();
260 }
261
262 //=================================================================================
263 // function : enterEvent()
264 // purpose  :
265 //=================================================================================
266 void OperationGUI_ClippingDlg::enterEvent( QEvent* )
267 {
268   this->setEnabled( true );
269 }
270
271 //=================================================================================
272 // function : onReset()
273 // purpose  :
274 //=================================================================================
275 void OperationGUI_ClippingDlg::onReset()
276 {
277   SUIT_ViewWindow* anActiveWindow =
278     SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
279   if ( !anActiveWindow )
280     return;
281
282   if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
283     SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
284     if ( !aVTKFrame )
285       return;
286
287     vtkRenderer* aRenderer = aVTKFrame->getRenderer();
288     if ( !aRenderer ) return;
289
290     vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
291     if ( anActiveCamera == NULL ) {
292       return;
293     }
294
295       // Find the plane equation for the camera view plane
296     double vn[3];
297     anActiveCamera->GetViewPlaneNormal( vn );
298     double  position[3];
299     anActiveCamera->GetPosition( position );
300
301     vtkFloatingPointType bounds[6];
302     aRenderer->ComputeVisiblePropBounds( bounds );
303
304     double center[3];
305     center[0] = ( bounds[0] + bounds[1] ) / 2.0;
306     center[1] = ( bounds[2] + bounds[3] ) / 2.0;
307     center[2] = ( bounds[4] + bounds[5] ) / 2.0;
308
309     double width = sqrt( ( bounds[1]-bounds[0] ) * ( bounds[1]-bounds[0] ) +
310                          ( bounds[3]-bounds[2] ) * ( bounds[3]-bounds[2] ) +
311                          ( bounds[5]-bounds[4] ) * ( bounds[5]-bounds[4] ) );
312
313     double distance = sqrt( ( position[0]-center[0] ) * ( position[0]-center[0] ) +
314                             ( position[1]-center[1] ) * ( position[1]-center[1] ) +
315                             ( position[2]-center[2] ) * ( position[2]-center[2] ) );
316
317     vtkFloatingPointType range[2] = { distance - width/2.0, distance + width/2.0 };
318
319     SpinBox_Near->setValue( range[0] );
320     SpinBox_Far->setValue( range[1] );
321
322     return;
323   }
324   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
325     OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
326
327     TextLabelNear->setText( tr( "Depth"  ) );
328     TextLabelFar->setText( tr( "Thickness"  ) );
329
330     Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
331
332     view3d->SetZClippingType( V3d_TypeOfZclipping( 0 ) );
333     view3d->ZFitAll();
334     double depth, thickness;
335     int ztype= view3d->ZClipping( depth, thickness );
336     SpinBox_Near->setValue( 0 );
337     SpinBox_Far->setValue( 1000 );
338     TypeCB->setCurrentIndex( ztype );
339   }
340 }