Salome HOME
Issue 0020580: get rid of QtxDoubleSpinBox class and use SalomeApp_DoubleSpinBox...
[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 <SalomeApp_DoubleSpinBox.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( GroupArguments );
78   initSpinBox( SpinBox_Near, COORD_MIN, COORD_MAX, 10.0, "length_precision" );
79   GroupArgumentsLayout->addWidget( SpinBox_Near, 0, 1 );
80
81   TextLabelFar = new QLabel( tr( "Far" ), GroupArguments );
82   GroupArgumentsLayout->addWidget( TextLabelFar, 0, 2 );
83
84   SpinBox_Far = new SalomeApp_DoubleSpinBox( GroupArguments );
85   initSpinBox( SpinBox_Far, COORD_MIN, COORD_MAX, 10.0, "length_precision" );
86   GroupArgumentsLayout->addWidget( SpinBox_Far, 0, 3 );
87
88   resetButton  = new QPushButton( tr( "Reset" ), GroupArguments );
89   GroupArgumentsLayout->addWidget( resetButton, 0, 4 );
90
91   TypeCB = new QComboBox( GroupArguments );
92   TypeCB->addItem( tr( "OFF" ) );
93   TypeCB->addItem( tr( "BACK" ) );
94   TypeCB->addItem( tr( "FRONT" ) );
95   TypeCB->addItem( tr( "SLICE" ) );
96   GroupArgumentsLayout->addWidget( TypeCB, 1, 0, 1, 5 );
97
98   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
99   layout->setMargin( 0 ); layout->setSpacing( 6 );
100   layout->addWidget( GroupArguments );
101
102   /* signals and slots connections */
103   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
104   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
105
106   connect( resetButton,   SIGNAL( clicked() ), this, SLOT( onReset() ) ) ;
107
108   setHelpFileName( "none.htm" );
109
110   Init();
111 }
112
113 //=================================================================================
114 // function : ~ OperationGUI_ClippingDlg()
115 // purpose  : Destroys the object and frees any allocated resources
116 //=================================================================================
117 OperationGUI_ClippingDlg::~ OperationGUI_ClippingDlg()
118 {
119   // no need to delete child widgets, Qt does it all for us
120 }
121
122 //=================================================================================
123 // function : Init()
124 // purpose  :
125 //=================================================================================
126 void OperationGUI_ClippingDlg::Init()
127 {
128   SUIT_ViewWindow* anActiveWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
129   if ( !anActiveWindow )
130     return;
131
132   if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
133     SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
134     if ( !aVTKFrame )
135       return;
136
137     TextLabelNear->setText( tr( "Near" ) );
138     TextLabelFar->setText( tr( "Far" ) );
139
140     TypeCB->hide();
141
142     vtkRenderer* aRenderer = aVTKFrame->getRenderer();
143     if ( !aRenderer ) return;
144
145     vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
146     if ( anActiveCamera == NULL ) {
147       return;
148     }
149
150       // Find the plane equation for the camera view plane
151     double vn[3];
152     anActiveCamera->GetViewPlaneNormal( vn );
153     double  position[3];
154     anActiveCamera->GetPosition( position );
155
156     vtkFloatingPointType bounds[6];
157     aRenderer->ComputeVisiblePropBounds( bounds );
158
159     double center[3];
160     center[0] = ( bounds[0] + bounds[1] ) / 2.0;
161     center[1] = ( bounds[2] + bounds[3] ) / 2.0;
162     center[2] = ( bounds[4] + bounds[5] ) / 2.0;
163
164     double width = sqrt( ( bounds[1]-bounds[0] ) * ( bounds[1]-bounds[0] ) +
165                          ( bounds[3]-bounds[2] ) * ( bounds[3]-bounds[2] ) +
166                          ( bounds[5]-bounds[4] ) * ( bounds[5]-bounds[4] ) );
167
168     double distance = sqrt( ( position[0]-center[0] ) * ( position[0]-center[0] ) +
169                             ( position[1]-center[1] ) * ( position[1]-center[1] ) +
170                             ( position[2]-center[2] ) * ( position[2]-center[2] ) );
171
172     vtkFloatingPointType range[2] = { distance - width/2.0, distance + width/2.0 };
173
174     SpinBox_Near->setValue( range[0] );
175     SpinBox_Far->setValue( range[1] );
176
177     return;
178   }
179   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
180     OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
181
182     TextLabelNear->setText( tr( "Depth" ) );
183     TextLabelFar->setText( tr( "Thickness" ) );
184
185     Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
186
187     double depth, thickness;
188     int ztype = view3d->ZClipping( depth, thickness );
189     SpinBox_Near->setValue( depth );
190     SpinBox_Far->setValue( thickness );
191     TypeCB->setCurrentIndex( ztype );
192
193     //Handle(V3d_Plane) aPlane = new V3d_Plane( aOCCFrame->getViewer()->getViewer3d() ,0, 0, 1, -100);
194     //aPlane->Display(view3d);
195   }
196 }
197
198
199 //=================================================================================
200 // function : ClickOnApply()
201 // purpose  :
202 //=================================================================================
203 bool OperationGUI_ClippingDlg::ClickOnApply()
204 {
205   SUIT_ViewWindow* anActiveWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
206   if ( !anActiveWindow )
207     return false;
208
209   if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
210     SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
211     if ( !aVTKFrame )
212       return false;
213
214     vtkRenderer* aRenderer = aVTKFrame->getRenderer();
215     if ( !aRenderer ) return false;
216
217     vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
218     if ( anActiveCamera == NULL ) {
219       return false;
220     }
221
222     vtkFloatingPointType range[2] = { SpinBox_Near->value(), SpinBox_Far->value() };
223     if ( range[0] < 0.0 ) range[0] = 0.0;
224     anActiveCamera->SetClippingRange( range );
225
226     return true;
227   }
228   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
229     OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
230
231     Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
232
233     double depth = SpinBox_Near->value();
234     double thickness = SpinBox_Far->value();
235     int aType = TypeCB->currentIndex();
236
237     view3d->SetZClippingType( V3d_TypeOfZclipping( aType ) );
238     view3d->SetZClippingDepth( depth );
239     view3d->SetZClippingWidth( thickness );
240   }
241
242   return true;
243 }
244
245 //=================================================================================
246 // function : ClickOnOk()
247 // purpose  :
248 //=================================================================================
249 void OperationGUI_ClippingDlg::ClickOnOk()
250 {
251   if ( ClickOnApply() )
252     ClickOnCancel();
253 }
254
255 //=================================================================================
256 // function : onActivate()
257 // purpose  :
258 //=================================================================================
259 void OperationGUI_ClippingDlg::onActivate()
260 {
261   GEOMBase_Skeleton::ActivateThisDialog();
262 }
263
264 //=================================================================================
265 // function : enterEvent()
266 // purpose  :
267 //=================================================================================
268 void OperationGUI_ClippingDlg::enterEvent( QEvent* )
269 {
270   this->setEnabled( true );
271 }
272
273 //=================================================================================
274 // function : onReset()
275 // purpose  :
276 //=================================================================================
277 void OperationGUI_ClippingDlg::onReset()
278 {
279   SUIT_ViewWindow* anActiveWindow =
280     SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
281   if ( !anActiveWindow )
282     return;
283
284   if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
285     SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
286     if ( !aVTKFrame )
287       return;
288
289     vtkRenderer* aRenderer = aVTKFrame->getRenderer();
290     if ( !aRenderer ) return;
291
292     vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
293     if ( anActiveCamera == NULL ) {
294       return;
295     }
296
297       // Find the plane equation for the camera view plane
298     double vn[3];
299     anActiveCamera->GetViewPlaneNormal( vn );
300     double  position[3];
301     anActiveCamera->GetPosition( position );
302
303     vtkFloatingPointType bounds[6];
304     aRenderer->ComputeVisiblePropBounds( bounds );
305
306     double center[3];
307     center[0] = ( bounds[0] + bounds[1] ) / 2.0;
308     center[1] = ( bounds[2] + bounds[3] ) / 2.0;
309     center[2] = ( bounds[4] + bounds[5] ) / 2.0;
310
311     double width = sqrt( ( bounds[1]-bounds[0] ) * ( bounds[1]-bounds[0] ) +
312                          ( bounds[3]-bounds[2] ) * ( bounds[3]-bounds[2] ) +
313                          ( bounds[5]-bounds[4] ) * ( bounds[5]-bounds[4] ) );
314
315     double distance = sqrt( ( position[0]-center[0] ) * ( position[0]-center[0] ) +
316                             ( position[1]-center[1] ) * ( position[1]-center[1] ) +
317                             ( position[2]-center[2] ) * ( position[2]-center[2] ) );
318
319     vtkFloatingPointType range[2] = { distance - width/2.0, distance + width/2.0 };
320
321     SpinBox_Near->setValue( range[0] );
322     SpinBox_Far->setValue( range[1] );
323
324     return;
325   }
326   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
327     OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
328
329     TextLabelNear->setText( tr( "Depth"  ) );
330     TextLabelFar->setText( tr( "Thickness"  ) );
331
332     Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
333
334     view3d->SetZClippingType( V3d_TypeOfZclipping( 0 ) );
335     view3d->ZFitAll();
336     double depth, thickness;
337     int ztype= view3d->ZClipping( depth, thickness );
338     SpinBox_Near->setValue( 0 );
339     SpinBox_Far->setValue( 1000 );
340     TypeCB->setCurrentIndex( ztype );
341   }
342 }