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