]> SALOME platform Git repositories - modules/geom.git/blob - src/OperationGUI/OperationGUI_ClippingDlg.cxx
Salome HOME
191eb88c2b10fb3fc0ac59122e4b1767d880036b
[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 "SVTK_ViewWindow.h"
35 #include "SVTK_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() == SVTK_Viewer::Type() )
149     {
150       SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
151       if( !aVTKFrame )
152         return;
153       
154       TextLabelNear->setText( tr( "Near"  ) );
155       TextLabelFar->setText( tr( "Far"  ) );
156
157       TypeCB->hide();
158
159       vtkRenderer* aRenderer = aVTKFrame->getRenderer();
160       if(!aRenderer) return;
161       
162       vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
163       if( anActiveCamera == NULL ){
164         MESSAGE("Trying to reset clipping range of non-existant camera");
165         return;
166       }
167       
168       // Find the plane equation for the camera view plane
169       double vn[3];
170       anActiveCamera->GetViewPlaneNormal(vn);
171       double  position[3];
172       anActiveCamera->GetPosition(position);
173       
174       float bounds[6];
175       aRenderer->ComputeVisiblePropBounds(bounds);
176       
177       double center[3];
178       center[0] = (bounds[0] + bounds[1])/2.0;
179       center[1] = (bounds[2] + bounds[3])/2.0;
180       center[2] = (bounds[4] + bounds[5])/2.0;
181       
182       double width = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
183                           (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
184                           (bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
185       
186       double distance = sqrt((position[0]-center[0])*(position[0]-center[0]) +
187                              (position[1]-center[1])*(position[1]-center[1]) +
188                              (position[2]-center[2])*(position[2]-center[2]));
189       
190       float range[2] = {distance - width/2.0, distance + width/2.0};
191       
192       SpinBox_Near->SetValue(range[0]);
193       SpinBox_Far->SetValue(range[1]);
194       
195       return;
196     }
197   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
198     {
199       OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
200
201       TextLabelNear->setText( tr( "Depth"  ) );
202       TextLabelFar->setText( tr( "Thickness"  ) );
203
204       Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)aOCCFrame->getViewPort())->getView();
205
206       double depth, thickness; 
207       int ztype= view3d->ZClipping(depth, thickness);
208       SpinBox_Near->SetValue(depth);
209       SpinBox_Far->SetValue(thickness);
210       TypeCB->setCurrentItem(ztype);
211
212       //Handle(V3d_Plane) aPlane = new V3d_Plane( aOCCFrame->getViewer()->getViewer3d() ,0, 0, 1, -100);
213       //aPlane->Display(view3d);
214     }
215 }
216
217
218 //=================================================================================
219 // function : ClickOnApply()
220 // purpose  :
221 //=================================================================================
222 bool OperationGUI_ClippingDlg::ClickOnApply()
223 {
224   SUIT_ViewWindow* anActiveWindow =  SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
225   if (!anActiveWindow)
226     return false;
227   
228   if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() )
229     {
230       SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
231       if( !aVTKFrame )
232         return false;
233       
234       vtkRenderer* aRenderer = aVTKFrame->getRenderer();
235       if(!aRenderer) return false;
236       
237       vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
238       if( anActiveCamera == NULL ){
239         MESSAGE("Trying to reset clipping range of non-existant camera");
240         return false;
241       }
242       
243       float range[2] = { SpinBox_Near->GetValue(), SpinBox_Far->GetValue() };
244       if (range[0] < 0.0) range[0] = 0.0;
245       anActiveCamera->SetClippingRange( range );
246       
247       return true;
248     }
249   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
250     {
251       OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
252
253       Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)aOCCFrame->getViewPort())->getView();
254       
255       double depth = SpinBox_Near->GetValue();
256       double thickness = SpinBox_Far->GetValue();
257       int aType = TypeCB->currentItem();
258       
259       view3d->SetZClippingType(V3d_TypeOfZclipping(aType));
260       view3d->SetZClippingDepth(depth);
261       view3d->SetZClippingWidth(thickness);
262     }
263
264   return true;
265 }
266
267 //=================================================================================
268 // function : ClickOnOk()
269 // purpose  :
270 //=================================================================================
271 void OperationGUI_ClippingDlg::ClickOnOk()
272 {
273   ClickOnApply();
274   ClickOnCancel();
275 }
276
277         
278 //=================================================================================
279 // function : ClickOnCancel()
280 // purpose  :
281 //=================================================================================
282 void OperationGUI_ClippingDlg::ClickOnCancel()
283 {
284   GEOMBase_Skeleton::ClickOnCancel();
285 }
286
287
288 //=================================================================================
289 // function : onActivate()
290 // purpose  :
291 //=================================================================================
292 void OperationGUI_ClippingDlg::onActivate()
293 {
294   GEOMBase_Skeleton::ActivateThisDialog();
295 }
296
297
298 //=================================================================================
299 // function : onDeactivate()
300 // purpose  : public slot to deactivate if active
301 //=================================================================================
302 void OperationGUI_ClippingDlg::DeactivateActiveDialog()
303 {
304   return;
305 }
306
307 //=================================================================================
308 // function : enterEvent()
309 // purpose  :
310 //=================================================================================
311 void OperationGUI_ClippingDlg::enterEvent(QEvent* e)
312 {
313   this->setEnabled(true);
314   return;
315 }
316
317
318 //=================================================================================
319 // function : closeEvent
320 // purpose  :
321 //=================================================================================
322 void OperationGUI_ClippingDlg::closeEvent( QCloseEvent* e )
323 {
324   QDialog::closeEvent( e );
325 }
326
327
328 //=================================================================================
329 // function : onReset()
330 // purpose  :
331 //=================================================================================
332 void OperationGUI_ClippingDlg::onReset()
333 {
334   SUIT_ViewWindow* anActiveWindow =  SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
335   if (!anActiveWindow)
336     return;
337   
338   if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() )
339     {
340       SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
341       if( !aVTKFrame )
342         return;
343
344       vtkRenderer* aRenderer = aVTKFrame->getRenderer();
345       if(!aRenderer) return;
346       
347       vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
348       if( anActiveCamera == NULL ){
349         MESSAGE("Trying to reset clipping range of non-existant camera");
350         return;
351       }
352       
353       // Find the plane equation for the camera view plane
354       double vn[3];
355       anActiveCamera->GetViewPlaneNormal(vn);
356       double  position[3];
357       anActiveCamera->GetPosition(position);
358       
359       float bounds[6];
360       aRenderer->ComputeVisiblePropBounds(bounds);
361       
362       double center[3];
363       center[0] = (bounds[0] + bounds[1])/2.0;
364       center[1] = (bounds[2] + bounds[3])/2.0;
365       center[2] = (bounds[4] + bounds[5])/2.0;
366       
367       double width = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
368                           (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
369                           (bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
370       
371       double distance = sqrt((position[0]-center[0])*(position[0]-center[0]) +
372                              (position[1]-center[1])*(position[1]-center[1]) +
373                              (position[2]-center[2])*(position[2]-center[2]));
374       
375       float range[2] = {distance - width/2.0, distance + width/2.0};
376       
377       SpinBox_Near->SetValue(range[0]);
378       SpinBox_Far->SetValue(range[1]);
379       
380       return;
381     }
382   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
383     {
384       OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
385
386       TextLabelNear->setText( tr( "Depth"  ) );
387       TextLabelFar->setText( tr( "Thickness"  ) );
388       
389       Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)aOCCFrame->getViewPort())->getView();
390       
391       view3d->SetZClippingType(V3d_TypeOfZclipping(0));
392       view3d->ZFitAll();
393       double depth, thickness; 
394       int ztype= view3d->ZClipping(depth, thickness);
395       SpinBox_Near->SetValue(0);
396       SpinBox_Far->SetValue(1000);
397       TypeCB->setCurrentItem(ztype);
398     }
399
400
401
402   return;
403 }