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