Salome HOME
*** empty log message ***
[modules/geom.git] / src / OperationGUI / OperationGUI_ClippingDlg.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2003  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 // File   : OperationGUI_ClippingDlg.cxx
23 // Author : Michael Zorin, Open CASCADE S.A.S. (mikhail.zorin@opencascade.com)
24 //
25
26 #include "OperationGUI_ClippingDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_ViewManager.h>
35 #include <SVTK_ViewWindow.h>
36 #include <SVTK_ViewModel.h>
37 #include <OCCViewer_ViewWindow.h>
38 #include <OCCViewer_ViewPort3d.h>
39 #include <VTKViewer.h>
40
41 // OCC Includes
42 #include <V3d_View.hxx>
43 #include <V3d.hxx>
44
45 // VTK Includes
46 #include <vtkCamera.h>
47 #include <vtkRenderer.h>
48
49 //=================================================================================
50 // class    : OperationGUI_ClippingDlg()
51 // purpose  : 
52 //=================================================================================
53 OperationGUI_ClippingDlg::OperationGUI_ClippingDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
54   : GEOMBase_Skeleton( theGeometryGUI, parent, false )
55 {
56   setWindowTitle( tr( "Change clipping range" ) );
57
58   mainFrame()->GroupConstructors->setAttribute( Qt::WA_DeleteOnClose );
59   mainFrame()->GroupConstructors->close();
60   mainFrame()->GroupBoxName->setAttribute( Qt::WA_DeleteOnClose );
61   mainFrame()->GroupBoxName->close();
62
63   /***************************************************************/
64   GroupArguments = new QGroupBox( centralWidget() );
65   QGridLayout* GroupArgumentsLayout = new QGridLayout( GroupArguments );
66   GroupArgumentsLayout->setSpacing( 6 );
67   GroupArgumentsLayout->setMargin( 9 );
68
69   // Controls
70   TextLabelNear = new QLabel( tr( "Near" ), GroupArguments );
71   GroupArgumentsLayout->addWidget( TextLabelNear, 0, 0 );
72
73   SpinBox_Near = new QDoubleSpinBox( GroupArguments );
74   GroupArgumentsLayout->addWidget( SpinBox_Near, 0, 1 );
75
76   TextLabelFar = new QLabel( tr( "Far" ), GroupArguments );
77   GroupArgumentsLayout->addWidget( TextLabelFar, 0, 2 );
78
79   SpinBox_Far = new QDoubleSpinBox( GroupArguments );
80   GroupArgumentsLayout->addWidget( SpinBox_Far, 0, 3 );
81
82   resetButton  = new QPushButton( tr( "Reset" ), GroupArguments );
83   GroupArgumentsLayout->addWidget( resetButton, 0, 4 );
84
85   TypeCB = new QComboBox( GroupArguments );
86   TypeCB->addItem( tr( "OFF" ) );
87   TypeCB->addItem( tr( "BACK" ) );
88   TypeCB->addItem( tr( "FRONT" ) );
89   TypeCB->addItem( tr( "SLICE" ) );
90   GroupArgumentsLayout->addWidget( TypeCB, 1, 0, 1, 5 );
91
92   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
93   layout->setMargin( 0 ); layout->setSpacing( 6 );
94   layout->addWidget( GroupArguments );
95
96   /* Initialisations */
97   initSpinBox( SpinBox_Near, COORD_MIN, COORD_MAX, 10.0, 3 );
98   initSpinBox( SpinBox_Far, COORD_MIN, COORD_MAX, 10.0, 3 );
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 : closeEvent
273 // purpose  :
274 //=================================================================================
275 void OperationGUI_ClippingDlg::closeEvent( QCloseEvent* e )
276 {
277   QDialog::closeEvent( e );
278 }
279
280 //=================================================================================
281 // function : onReset()
282 // purpose  :
283 //=================================================================================
284 void OperationGUI_ClippingDlg::onReset()
285 {
286   SUIT_ViewWindow* anActiveWindow =
287     SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
288   if ( !anActiveWindow )
289     return;
290   
291   if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
292     SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
293     if ( !aVTKFrame )
294       return;
295     
296     vtkRenderer* aRenderer = aVTKFrame->getRenderer();
297     if ( !aRenderer ) return;
298     
299     vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
300     if ( anActiveCamera == NULL ) {
301       return;
302     }
303     
304       // Find the plane equation for the camera view plane
305     double vn[3];
306     anActiveCamera->GetViewPlaneNormal( vn );
307     double  position[3];
308     anActiveCamera->GetPosition( position );
309     
310     vtkFloatingPointType bounds[6];
311     aRenderer->ComputeVisiblePropBounds( bounds );
312     
313     double center[3];
314     center[0] = ( bounds[0] + bounds[1] ) / 2.0;
315     center[1] = ( bounds[2] + bounds[3] ) / 2.0;
316     center[2] = ( bounds[4] + bounds[5] ) / 2.0;
317     
318     double width = sqrt( ( bounds[1]-bounds[0] ) * ( bounds[1]-bounds[0] ) +
319                          ( bounds[3]-bounds[2] ) * ( bounds[3]-bounds[2] ) +
320                          ( bounds[5]-bounds[4] ) * ( bounds[5]-bounds[4] ) );
321     
322     double distance = sqrt( ( position[0]-center[0] ) * ( position[0]-center[0] ) +
323                             ( position[1]-center[1] ) * ( position[1]-center[1] ) +
324                             ( position[2]-center[2] ) * ( position[2]-center[2] ) );
325     
326     vtkFloatingPointType range[2] = { distance - width/2.0, distance + width/2.0 };
327     
328     SpinBox_Near->setValue( range[0] );
329     SpinBox_Far->setValue( range[1] );
330     
331     return;
332   }
333   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
334     OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
335     
336     TextLabelNear->setText( tr( "Depth"  ) );
337     TextLabelFar->setText( tr( "Thickness"  ) );
338     
339     Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
340     
341     view3d->SetZClippingType( V3d_TypeOfZclipping( 0 ) );
342     view3d->ZFitAll();
343     double depth, thickness; 
344     int ztype= view3d->ZClipping( depth, thickness );
345     SpinBox_Near->setValue( 0 );
346     SpinBox_Far->setValue( 1000 );
347     TypeCB->setCurrentIndex( ztype );
348   }
349 }