1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // GEOM GEOMGUI : GUI for Geometry component
23 // File : OperationGUI_ClippingDlg.cxx
24 // Author : Michael Zorin
28 #include "OperationGUI_ClippingDlg.h"
29 #include "DlgRef_SpinBox.h"
31 #include "SUIT_Session.h"
32 #include "SUIT_ViewWindow.h"
33 #include "SVTK_ViewWindow.h"
34 #include "SVTK_ViewModel.h"
36 #include "OCCViewer_ViewWindow.h"
37 #include "OCCViewer_ViewPort3d.h"
39 #include "utilities.h"
40 #include <V3d_View.hxx>
44 #include <qapplication.h>
45 #include <qgroupbox.h>
47 #include <qpushbutton.h>
48 #include <qcombobox.h>
51 #include "VTKViewer.h"
52 #include <vtkCamera.h>
53 #include <vtkRenderer.h>
55 //=================================================================================
56 // class : OperationGUI_ClippingDlg()
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)
63 setCaption( tr( "Change clipping range" ) );
65 GroupConstructors->close();
66 GroupBoxName->close();
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 );
79 TextLabelNear = new QLabel( GroupArguments, "TextLabelNear" );
80 TextLabelNear->setText( tr( "Near" ) );
81 TextLabelNear->setFixedWidth(74);
82 GroupArgumentsLayout->addWidget( TextLabelNear, 0, 0 );
84 SpinBox_Near = new DlgRef_SpinBox( GroupArguments, "SpinBox_Near");
85 GroupArgumentsLayout->addWidget( SpinBox_Near, 0, 1 );
87 TextLabelFar = new QLabel( GroupArguments, "TextLabelFar" );
88 TextLabelFar->setText( tr( "Far" ) );
89 TextLabelFar->setFixedWidth(74);
90 GroupArgumentsLayout->addWidget( TextLabelFar, 0, 2 );
92 SpinBox_Far = new DlgRef_SpinBox( GroupArguments, "SpinBox_Far");
93 GroupArgumentsLayout->addWidget( SpinBox_Far, 0, 3 );
95 resetButton = new QPushButton( GroupArguments, "resetButton" );
96 resetButton->setText( tr( "Reset" ) );
97 GroupArgumentsLayout->addWidget( resetButton, 0, 4 );
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 );
106 Layout1->addWidget( GroupArguments, 2, 0 );
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 );
112 /* signals and slots connections */
113 connect( buttonOk , SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
114 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
116 connect( resetButton, SIGNAL (clicked() ), this, SLOT( onReset() ) ) ;
118 setHelpFileName("none.htm");
123 //=================================================================================
124 // function : ~ OperationGUI_ClippingDlg()
125 // purpose : Destroys the object and frees any allocated resources
126 //=================================================================================
127 OperationGUI_ClippingDlg::~ OperationGUI_ClippingDlg()
129 // no need to delete child widgets, Qt does it all for us
132 //=================================================================================
135 //=================================================================================
136 void OperationGUI_ClippingDlg::Init()
138 SUIT_ViewWindow* anActiveWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
142 if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() )
144 SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
148 TextLabelNear->setText( tr( "Near" ) );
149 TextLabelFar->setText( tr( "Far" ) );
153 vtkRenderer* aRenderer = aVTKFrame->getRenderer();
154 if(!aRenderer) return;
156 vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
157 if( anActiveCamera == NULL ){
158 MESSAGE("Trying to reset clipping range of non-existant camera");
162 // Find the plane equation for the camera view plane
164 anActiveCamera->GetViewPlaneNormal(vn);
166 anActiveCamera->GetPosition(position);
168 vtkFloatingPointType bounds[6];
169 aRenderer->ComputeVisiblePropBounds(bounds);
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;
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]));
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]));
184 vtkFloatingPointType range[2] = {distance - width/2.0, distance + width/2.0};
186 SpinBox_Near->SetValue(range[0]);
187 SpinBox_Far->SetValue(range[1]);
191 else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
193 OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
195 TextLabelNear->setText( tr( "Depth" ) );
196 TextLabelFar->setText( tr( "Thickness" ) );
198 Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)aOCCFrame->getViewPort())->getView();
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);
206 //Handle(V3d_Plane) aPlane = new V3d_Plane( aOCCFrame->getViewer()->getViewer3d() ,0, 0, 1, -100);
207 //aPlane->Display(view3d);
212 //=================================================================================
213 // function : ClickOnApply()
215 //=================================================================================
216 bool OperationGUI_ClippingDlg::ClickOnApply()
218 SUIT_ViewWindow* anActiveWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
222 if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() )
224 SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
228 vtkRenderer* aRenderer = aVTKFrame->getRenderer();
229 if(!aRenderer) return false;
231 vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
232 if( anActiveCamera == NULL ){
233 MESSAGE("Trying to reset clipping range of non-existant camera");
237 vtkFloatingPointType range[2] = { SpinBox_Near->GetValue(), SpinBox_Far->GetValue() };
238 if (range[0] < 0.0) range[0] = 0.0;
239 anActiveCamera->SetClippingRange( range );
243 else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
245 OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
247 Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)aOCCFrame->getViewPort())->getView();
249 double depth = SpinBox_Near->GetValue();
250 double thickness = SpinBox_Far->GetValue();
251 int aType = TypeCB->currentItem();
253 view3d->SetZClippingType(V3d_TypeOfZclipping(aType));
254 view3d->SetZClippingDepth(depth);
255 view3d->SetZClippingWidth(thickness);
261 //=================================================================================
262 // function : ClickOnOk()
264 //=================================================================================
265 void OperationGUI_ClippingDlg::ClickOnOk()
271 //=================================================================================
272 // function : onActivate()
274 //=================================================================================
275 void OperationGUI_ClippingDlg::onActivate()
277 GEOMBase_Skeleton::ActivateThisDialog();
280 //=================================================================================
281 // function : enterEvent()
283 //=================================================================================
284 void OperationGUI_ClippingDlg::enterEvent(QEvent*)
286 this->setEnabled(true);
290 //=================================================================================
291 // function : closeEvent
293 //=================================================================================
294 void OperationGUI_ClippingDlg::closeEvent( QCloseEvent* e )
296 QDialog::closeEvent( e );
299 //=================================================================================
300 // function : onReset()
302 //=================================================================================
303 void OperationGUI_ClippingDlg::onReset()
305 SUIT_ViewWindow* anActiveWindow =
306 SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
310 if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() )
312 SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
316 vtkRenderer* aRenderer = aVTKFrame->getRenderer();
317 if(!aRenderer) return;
319 vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
320 if( anActiveCamera == NULL ){
321 MESSAGE("Trying to reset clipping range of non-existant camera");
325 // Find the plane equation for the camera view plane
327 anActiveCamera->GetViewPlaneNormal(vn);
329 anActiveCamera->GetPosition(position);
331 vtkFloatingPointType bounds[6];
332 aRenderer->ComputeVisiblePropBounds(bounds);
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;
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]));
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]));
347 vtkFloatingPointType range[2] = {distance - width/2.0, distance + width/2.0};
349 SpinBox_Near->SetValue(range[0]);
350 SpinBox_Far->SetValue(range[1]);
354 else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
356 OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
358 TextLabelNear->setText( tr( "Depth" ) );
359 TextLabelFar->setText( tr( "Thickness" ) );
361 Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)aOCCFrame->getViewPort())->getView();
363 view3d->SetZClippingType(V3d_TypeOfZclipping(0));
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);