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