Salome HOME
SMH: Merged GEOM (NEWGUI, HEAD, POLYWORK)
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : OperationGUI_ClippingDlg.cxx
25 //  Author : Michael Zorin
26 //  Module : GEOM
27 //  $Header: 
28
29 #include "OperationGUI_ClippingDlg.h"
30 #include "DlgRef_SpinBox.h"
31
32 #include "SUIT_Session.h"
33 #include "SUIT_ViewWindow.h"
34 #include "VTKViewer_ViewWindow.h"
35 #include "VTKViewer_ViewModel.h"
36 #include "OCCViewer_ViewWindow.h"
37 #include "OCCViewer_ViewPort3d.h"
38
39 #include "utilities.h"
40 #include <V3d_View.hxx>
41 #include <V3d.hxx>
42
43 //#include <V3d_Plane.hxx>
44
45 // QT Includes
46 #include <qapplication.h>
47 #include <qgroupbox.h>
48 #include <qlabel.h>
49 #include <qpushbutton.h>
50 #include <qcombobox.h>
51
52 // VTK Includes
53 #include <vtkCamera.h>
54 #include <vtkRenderer.h>
55
56 //=================================================================================
57 // class    : OperationGUI_ClippingDlg()
58 // purpose  : 
59 //=================================================================================
60 OperationGUI_ClippingDlg::OperationGUI_ClippingDlg( QWidget* parent, const char* name, bool modal, WFlags fl )
61   : GEOMBase_Skeleton( parent, "OperationGUI_ClippingDlg", false,
62     WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
63 {
64   setCaption( tr( "Change clipping range" ) );
65   
66   GroupConstructors->close();
67   GroupBoxName->close();
68
69   /***************************************************************/
70   GroupArguments = new QGroupBox( this, "GroupArguments" );
71   GroupArguments->setColumnLayout(0, Qt::Vertical );
72   GroupArguments->layout()->setSpacing( 0 );
73   GroupArguments->layout()->setMargin( 0 );
74   QGridLayout* GroupArgumentsLayout = new QGridLayout( GroupArguments->layout() );
75   GroupArgumentsLayout->setAlignment( Qt::AlignTop );
76   GroupArgumentsLayout->setSpacing( 6 );
77   GroupArgumentsLayout->setMargin( 11 );
78   
79   // Controls
80   TextLabelNear = new QLabel( GroupArguments, "TextLabelNear" );
81   TextLabelNear->setText( tr( "Near"  ) );
82   TextLabelNear->setFixedWidth(74);
83   GroupArgumentsLayout->addWidget( TextLabelNear, 0, 0 );
84
85   SpinBox_Near = new DlgRef_SpinBox( GroupArguments, "SpinBox_Near");
86   GroupArgumentsLayout->addWidget( SpinBox_Near, 0, 1 );
87
88   TextLabelFar = new QLabel( GroupArguments, "TextLabelFar" );
89   TextLabelFar->setText( tr( "Far"  ) );
90   TextLabelFar->setFixedWidth(74);
91   GroupArgumentsLayout->addWidget( TextLabelFar, 0, 2 );
92   
93   SpinBox_Far = new DlgRef_SpinBox( GroupArguments, "SpinBox_Far");
94   GroupArgumentsLayout->addWidget( SpinBox_Far, 0, 3 );
95   
96   resetButton  = new QPushButton( GroupArguments, "resetButton" );
97   resetButton->setText( tr( "Reset"  ) );
98   GroupArgumentsLayout->addWidget( resetButton, 0, 4 );
99
100   TypeCB = new QComboBox( GroupArguments, "TypeCB" );
101   TypeCB->insertItem(tr("OFF"));
102   TypeCB->insertItem(tr("BACK"));
103   TypeCB->insertItem(tr("FRONT"));
104   TypeCB->insertItem(tr("SLICE"));
105   GroupArgumentsLayout->addMultiCellWidget( TypeCB, 1, 1, 0, 4 );
106   
107   Layout1->addWidget( GroupArguments, 2, 0 );
108   
109   /* Initialisations */
110   SpinBox_Near->RangeStepAndValidator( -999999.999, +999999.999, 10.0, 3 );
111   SpinBox_Far->RangeStepAndValidator( -999999.999, +999999.999, 10.0, 3 );
112   
113   //GeometryGUI* aGeomGUI = GeometryGUI::GetGeomGUI();
114   
115   /* signals and slots connections */
116   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
117   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
118   connect( buttonApply, SIGNAL( clicked() ),  this, SLOT( ClickOnApply() ) );
119   
120   connect( resetButton, SIGNAL (clicked() ), this, SLOT( onReset() ) ) ;
121   
122   //  connect( aGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( onDeactivate() ) ) ;
123   //connect( aGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
124
125   Init();
126 }
127
128 //=================================================================================
129 // function : ~ OperationGUI_ClippingDlg()
130 // purpose  : Destroys the object and frees any allocated resources
131 //=================================================================================
132 OperationGUI_ClippingDlg::~ OperationGUI_ClippingDlg()
133 {
134   // no need to delete child widgets, Qt does it all for us
135 }
136
137
138 //=================================================================================
139 // function : Init()
140 // purpose  :
141 //=================================================================================
142 void OperationGUI_ClippingDlg::Init()
143 {
144   SUIT_ViewWindow* anActiveWindow =  SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
145   if (!anActiveWindow)
146     return;
147   
148   if ( anActiveWindow->getViewManager()->getType() == VTKViewer_Viewer::Type() )
149     {
150       VTKViewer_ViewWindow* aVTKFrame = dynamic_cast<VTKViewer_ViewWindow*>( anActiveWindow );
151       
152       TextLabelNear->setText( tr( "Near"  ) );
153       TextLabelFar->setText( tr( "Far"  ) );
154
155       TypeCB->hide();
156
157       vtkRenderer* aRenderer = aVTKFrame->getRenderer();
158       if(!aRenderer) return;
159       
160       vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
161       if( anActiveCamera == NULL ){
162         MESSAGE("Trying to reset clipping range of non-existant camera");
163         return;
164       }
165       
166       // Find the plane equation for the camera view plane
167       double vn[3];
168       anActiveCamera->GetViewPlaneNormal(vn);
169       double  position[3];
170       anActiveCamera->GetPosition(position);
171       
172       float bounds[6];
173       aRenderer->ComputeVisiblePropBounds(bounds);
174       
175       double center[3];
176       center[0] = (bounds[0] + bounds[1])/2.0;
177       center[1] = (bounds[2] + bounds[3])/2.0;
178       center[2] = (bounds[4] + bounds[5])/2.0;
179       
180       double width = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
181                           (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
182                           (bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
183       
184       double distance = sqrt((position[0]-center[0])*(position[0]-center[0]) +
185                              (position[1]-center[1])*(position[1]-center[1]) +
186                              (position[2]-center[2])*(position[2]-center[2]));
187       
188       float range[2] = {distance - width/2.0, distance + width/2.0};
189       
190       SpinBox_Near->SetValue(range[0]);
191       SpinBox_Far->SetValue(range[1]);
192       
193       return;
194     }
195   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
196     {
197       OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
198
199       TextLabelNear->setText( tr( "Depth"  ) );
200       TextLabelFar->setText( tr( "Thickness"  ) );
201
202       Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)aOCCFrame->getViewPort())->getView();
203
204       double depth, thickness; 
205       int ztype= view3d->ZClipping(depth, thickness);
206       SpinBox_Near->SetValue(depth);
207       SpinBox_Far->SetValue(thickness);
208       TypeCB->setCurrentItem(ztype);
209
210       //Handle(V3d_Plane) aPlane = new V3d_Plane( aOCCFrame->getViewer()->getViewer3d() ,0, 0, 1, -100);
211       //aPlane->Display(view3d);
212     }
213 }
214
215
216 //=================================================================================
217 // function : ClickOnApply()
218 // purpose  :
219 //=================================================================================
220 bool OperationGUI_ClippingDlg::ClickOnApply()
221 {
222   SUIT_ViewWindow* anActiveWindow =  SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
223   if (!anActiveWindow)
224     return false;
225   
226   if ( anActiveWindow->getViewManager()->getType() == VTKViewer_Viewer::Type() )
227     {
228       VTKViewer_ViewWindow* aVTKFrame = dynamic_cast<VTKViewer_ViewWindow*>( anActiveWindow );
229       
230       vtkRenderer* aRenderer = aVTKFrame->getRenderer();
231       if(!aRenderer) return false;
232       
233       vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
234       if( anActiveCamera == NULL ){
235         MESSAGE("Trying to reset clipping range of non-existant camera");
236         return false;
237       }
238       
239       float range[2] = { SpinBox_Near->GetValue(), SpinBox_Far->GetValue() };
240       if (range[0] < 0.0) range[0] = 0.0;
241       anActiveCamera->SetClippingRange( range );
242       
243       return true;
244     }
245   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
246     {
247       OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
248
249       Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)aOCCFrame->getViewPort())->getView();
250       
251       double depth = SpinBox_Near->GetValue();
252       double thickness = SpinBox_Far->GetValue();
253       int aType = TypeCB->currentItem();
254       
255       view3d->SetZClippingType(V3d_TypeOfZclipping(aType));
256       view3d->SetZClippingDepth(depth);
257       view3d->SetZClippingWidth(thickness);
258     }
259
260   return true;
261 }
262
263 //=================================================================================
264 // function : ClickOnOk()
265 // purpose  :
266 //=================================================================================
267 void OperationGUI_ClippingDlg::ClickOnOk()
268 {
269   ClickOnApply();
270   ClickOnCancel();
271 }
272
273         
274 //=================================================================================
275 // function : ClickOnCancel()
276 // purpose  :
277 //=================================================================================
278 void OperationGUI_ClippingDlg::ClickOnCancel()
279 {
280   GEOMBase_Skeleton::ClickOnCancel();
281 }
282
283
284 //=================================================================================
285 // function : onActivate()
286 // purpose  :
287 //=================================================================================
288 void OperationGUI_ClippingDlg::onActivate()
289 {
290   GEOMBase_Skeleton::ActivateThisDialog();
291 }
292
293
294 //=================================================================================
295 // function : onDeactivate()
296 // purpose  : public slot to deactivate if active
297 //=================================================================================
298 void OperationGUI_ClippingDlg::DeactivateActiveDialog()
299 {
300   return;
301 }
302
303 //=================================================================================
304 // function : enterEvent()
305 // purpose  :
306 //=================================================================================
307 void OperationGUI_ClippingDlg::enterEvent(QEvent* e)
308 {
309   this->setEnabled(true);
310   return;
311 }
312
313
314 //=================================================================================
315 // function : closeEvent
316 // purpose  :
317 //=================================================================================
318 void OperationGUI_ClippingDlg::closeEvent( QCloseEvent* e )
319 {
320   QDialog::closeEvent( e );
321 }
322
323
324 //=================================================================================
325 // function : onReset()
326 // purpose  :
327 //=================================================================================
328 void OperationGUI_ClippingDlg::onReset()
329 {
330   SUIT_ViewWindow* anActiveWindow =  SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
331   if (!anActiveWindow)
332     return;
333   
334   if ( anActiveWindow->getViewManager()->getType() == VTKViewer_Viewer::Type() )
335     {
336       VTKViewer_ViewWindow* aVTKFrame = dynamic_cast<VTKViewer_ViewWindow*>( anActiveWindow );
337
338       vtkRenderer* aRenderer = aVTKFrame->getRenderer();
339       if(!aRenderer) return;
340       
341       vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
342       if( anActiveCamera == NULL ){
343         MESSAGE("Trying to reset clipping range of non-existant camera");
344         return;
345       }
346       
347       // Find the plane equation for the camera view plane
348       double vn[3];
349       anActiveCamera->GetViewPlaneNormal(vn);
350       double  position[3];
351       anActiveCamera->GetPosition(position);
352       
353       float bounds[6];
354       aRenderer->ComputeVisiblePropBounds(bounds);
355       
356       double center[3];
357       center[0] = (bounds[0] + bounds[1])/2.0;
358       center[1] = (bounds[2] + bounds[3])/2.0;
359       center[2] = (bounds[4] + bounds[5])/2.0;
360       
361       double width = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
362                           (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
363                           (bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
364       
365       double distance = sqrt((position[0]-center[0])*(position[0]-center[0]) +
366                              (position[1]-center[1])*(position[1]-center[1]) +
367                              (position[2]-center[2])*(position[2]-center[2]));
368       
369       float range[2] = {distance - width/2.0, distance + width/2.0};
370       
371       SpinBox_Near->SetValue(range[0]);
372       SpinBox_Far->SetValue(range[1]);
373       
374       return;
375     }
376   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
377     {
378       OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
379
380       TextLabelNear->setText( tr( "Depth"  ) );
381       TextLabelFar->setText( tr( "Thickness"  ) );
382       
383       Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)aOCCFrame->getViewPort())->getView();
384       
385       view3d->SetZClippingType(V3d_TypeOfZclipping(0));
386       view3d->ZFitAll();
387       double depth, thickness; 
388       int ztype= view3d->ZClipping(depth, thickness);
389       SpinBox_Near->SetValue(0);
390       SpinBox_Far->SetValue(1000);
391       TypeCB->setCurrentItem(ztype);
392     }
393
394
395
396   return;
397 }