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