1 // GEOM GEOMGUI : GUI for Geometry component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : OperationGUI_ClippingDlg.cxx
25 // Author : Michael Zorin
29 #include "OperationGUI_ClippingDlg.h"
30 #include "DlgRef_SpinBox.h"
32 #include "SUIT_Session.h"
33 #include "SUIT_ViewWindow.h"
34 #include "SVTK_ViewWindow.h"
35 #include "SVTK_ViewModel.h"
37 #include "OCCViewer_ViewWindow.h"
38 #include "OCCViewer_ViewPort3d.h"
40 #include "utilities.h"
41 #include <V3d_View.hxx>
44 //#include <V3d_Plane.hxx>
47 #include <qapplication.h>
48 #include <qgroupbox.h>
50 #include <qpushbutton.h>
51 #include <qcombobox.h>
54 #include "VTKViewer.h"
55 #include <vtkCamera.h>
56 #include <vtkRenderer.h>
58 //=================================================================================
59 // class : OperationGUI_ClippingDlg()
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)
66 setCaption( tr( "Change clipping range" ) );
68 GroupConstructors->close();
69 GroupBoxName->close();
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 );
82 TextLabelNear = new QLabel( GroupArguments, "TextLabelNear" );
83 TextLabelNear->setText( tr( "Near" ) );
84 TextLabelNear->setFixedWidth(74);
85 GroupArgumentsLayout->addWidget( TextLabelNear, 0, 0 );
87 SpinBox_Near = new DlgRef_SpinBox( GroupArguments, "SpinBox_Near");
88 GroupArgumentsLayout->addWidget( SpinBox_Near, 0, 1 );
90 TextLabelFar = new QLabel( GroupArguments, "TextLabelFar" );
91 TextLabelFar->setText( tr( "Far" ) );
92 TextLabelFar->setFixedWidth(74);
93 GroupArgumentsLayout->addWidget( TextLabelFar, 0, 2 );
95 SpinBox_Far = new DlgRef_SpinBox( GroupArguments, "SpinBox_Far");
96 GroupArgumentsLayout->addWidget( SpinBox_Far, 0, 3 );
98 resetButton = new QPushButton( GroupArguments, "resetButton" );
99 resetButton->setText( tr( "Reset" ) );
100 GroupArgumentsLayout->addWidget( resetButton, 0, 4 );
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 );
109 Layout1->addWidget( GroupArguments, 2, 0 );
111 /* Initialisations */
112 SpinBox_Near->RangeStepAndValidator( -999999.999, +999999.999, 10.0, 3 );
113 SpinBox_Far->RangeStepAndValidator( -999999.999, +999999.999, 10.0, 3 );
115 /* signals and slots connections */
116 connect( buttonOk , SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
117 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
119 connect( resetButton, SIGNAL (clicked() ), this, SLOT( onReset() ) ) ;
121 setHelpFileName("none.htm");
126 //=================================================================================
127 // function : ~ OperationGUI_ClippingDlg()
128 // purpose : Destroys the object and frees any allocated resources
129 //=================================================================================
130 OperationGUI_ClippingDlg::~ OperationGUI_ClippingDlg()
132 // no need to delete child widgets, Qt does it all for us
135 //=================================================================================
138 //=================================================================================
139 void OperationGUI_ClippingDlg::Init()
141 SUIT_ViewWindow* anActiveWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
145 if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() )
147 SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
151 TextLabelNear->setText( tr( "Near" ) );
152 TextLabelFar->setText( tr( "Far" ) );
156 vtkRenderer* aRenderer = aVTKFrame->getRenderer();
157 if(!aRenderer) return;
159 vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
160 if( anActiveCamera == NULL ){
161 MESSAGE("Trying to reset clipping range of non-existant camera");
165 // Find the plane equation for the camera view plane
167 anActiveCamera->GetViewPlaneNormal(vn);
169 anActiveCamera->GetPosition(position);
171 vtkFloatingPointType bounds[6];
172 aRenderer->ComputeVisiblePropBounds(bounds);
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;
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]));
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]));
187 vtkFloatingPointType range[2] = {distance - width/2.0, distance + width/2.0};
189 SpinBox_Near->SetValue(range[0]);
190 SpinBox_Far->SetValue(range[1]);
194 else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
196 OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
198 TextLabelNear->setText( tr( "Depth" ) );
199 TextLabelFar->setText( tr( "Thickness" ) );
201 Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)aOCCFrame->getViewPort())->getView();
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);
209 //Handle(V3d_Plane) aPlane = new V3d_Plane( aOCCFrame->getViewer()->getViewer3d() ,0, 0, 1, -100);
210 //aPlane->Display(view3d);
215 //=================================================================================
216 // function : ClickOnApply()
218 //=================================================================================
219 bool OperationGUI_ClippingDlg::ClickOnApply()
221 SUIT_ViewWindow* anActiveWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
225 if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() )
227 SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
231 vtkRenderer* aRenderer = aVTKFrame->getRenderer();
232 if(!aRenderer) return false;
234 vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
235 if( anActiveCamera == NULL ){
236 MESSAGE("Trying to reset clipping range of non-existant camera");
240 vtkFloatingPointType range[2] = { SpinBox_Near->GetValue(), SpinBox_Far->GetValue() };
241 if (range[0] < 0.0) range[0] = 0.0;
242 anActiveCamera->SetClippingRange( range );
246 else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
248 OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
250 Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)aOCCFrame->getViewPort())->getView();
252 double depth = SpinBox_Near->GetValue();
253 double thickness = SpinBox_Far->GetValue();
254 int aType = TypeCB->currentItem();
256 view3d->SetZClippingType(V3d_TypeOfZclipping(aType));
257 view3d->SetZClippingDepth(depth);
258 view3d->SetZClippingWidth(thickness);
264 //=================================================================================
265 // function : ClickOnOk()
267 //=================================================================================
268 void OperationGUI_ClippingDlg::ClickOnOk()
274 //=================================================================================
275 // function : onActivate()
277 //=================================================================================
278 void OperationGUI_ClippingDlg::onActivate()
280 GEOMBase_Skeleton::ActivateThisDialog();
283 //=================================================================================
284 // function : enterEvent()
286 //=================================================================================
287 void OperationGUI_ClippingDlg::enterEvent(QEvent*)
289 this->setEnabled(true);
293 //=================================================================================
294 // function : closeEvent
296 //=================================================================================
297 void OperationGUI_ClippingDlg::closeEvent( QCloseEvent* e )
299 QDialog::closeEvent( e );
302 //=================================================================================
303 // function : onReset()
305 //=================================================================================
306 void OperationGUI_ClippingDlg::onReset()
308 SUIT_ViewWindow* anActiveWindow =
309 SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
313 if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() )
315 SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
319 vtkRenderer* aRenderer = aVTKFrame->getRenderer();
320 if(!aRenderer) return;
322 vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
323 if( anActiveCamera == NULL ){
324 MESSAGE("Trying to reset clipping range of non-existant camera");
328 // Find the plane equation for the camera view plane
330 anActiveCamera->GetViewPlaneNormal(vn);
332 anActiveCamera->GetPosition(position);
334 vtkFloatingPointType bounds[6];
335 aRenderer->ComputeVisiblePropBounds(bounds);
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;
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]));
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]));
350 vtkFloatingPointType range[2] = {distance - width/2.0, distance + width/2.0};
352 SpinBox_Near->SetValue(range[0]);
353 SpinBox_Far->SetValue(range[1]);
357 else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
359 OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
361 TextLabelNear->setText( tr( "Depth" ) );
362 TextLabelFar->setText( tr( "Thickness" ) );
364 Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)aOCCFrame->getViewPort())->getView();
366 view3d->SetZClippingType(V3d_TypeOfZclipping(0));
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);