Back to index

salome-geom  6.5.0
OperationGUI_ClippingDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // GEOM GEOMGUI : GUI for Geometry component
00024 // File   : OperationGUI_ClippingDlg.cxx
00025 // Author : Michael Zorin, Open CASCADE S.A.S. (mikhail.zorin@opencascade.com)
00026 //
00027 #include "OperationGUI_ClippingDlg.h"
00028 
00029 #include <DlgRef.h>
00030 #include <GeometryGUI.h>
00031 #include <GEOMBase.h>
00032 #include <SalomeApp_DoubleSpinBox.h>
00033 
00034 #include <SUIT_Session.h>
00035 #include <SUIT_Desktop.h>
00036 #include <SUIT_ViewManager.h>
00037 #include <SVTK_ViewWindow.h>
00038 #include <SVTK_ViewModel.h>
00039 
00040 #include <OCCViewer_ViewModel.h>
00041 #include <OCCViewer_ViewWindow.h>
00042 #include <OCCViewer_ViewPort3d.h>
00043 
00044 #include <VTKViewer.h>
00045 
00046 // OCC Includes
00047 #include <V3d_View.hxx>
00048 #include <V3d.hxx>
00049 
00050 // VTK Includes
00051 #include <vtkCamera.h>
00052 #include <vtkRenderer.h>
00053 
00054 //=================================================================================
00055 // class    : OperationGUI_ClippingDlg()
00056 // purpose  :
00057 //=================================================================================
00058 OperationGUI_ClippingDlg::OperationGUI_ClippingDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
00059   : GEOMBase_Skeleton( theGeometryGUI, parent, false )
00060 {
00061   setWindowTitle( tr( "Change clipping range" ) );
00062 
00063   mainFrame()->GroupConstructors->setAttribute( Qt::WA_DeleteOnClose );
00064   mainFrame()->GroupConstructors->close();
00065   mainFrame()->GroupBoxName->setAttribute( Qt::WA_DeleteOnClose );
00066   mainFrame()->GroupBoxName->close();
00067 
00068   /***************************************************************/
00069   GroupArguments = new QGroupBox( centralWidget() );
00070   QGridLayout* GroupArgumentsLayout = new QGridLayout( GroupArguments );
00071   GroupArgumentsLayout->setSpacing( 6 );
00072   GroupArgumentsLayout->setMargin( 9 );
00073 
00074   // Controls
00075   TextLabelNear = new QLabel( tr( "Near" ), GroupArguments );
00076   GroupArgumentsLayout->addWidget( TextLabelNear, 0, 0 );
00077 
00078   SpinBox_Near = new SalomeApp_DoubleSpinBox( GroupArguments );
00079   initSpinBox( SpinBox_Near, COORD_MIN, COORD_MAX, 10.0, "length_precision" );
00080   GroupArgumentsLayout->addWidget( SpinBox_Near, 0, 1 );
00081 
00082   TextLabelFar = new QLabel( tr( "Far" ), GroupArguments );
00083   GroupArgumentsLayout->addWidget( TextLabelFar, 0, 2 );
00084 
00085   SpinBox_Far = new SalomeApp_DoubleSpinBox( GroupArguments );
00086   initSpinBox( SpinBox_Far, COORD_MIN, COORD_MAX, 10.0, "length_precision" );
00087   GroupArgumentsLayout->addWidget( SpinBox_Far, 0, 3 );
00088 
00089   resetButton  = new QPushButton( tr( "Reset" ), GroupArguments );
00090   GroupArgumentsLayout->addWidget( resetButton, 0, 4 );
00091 
00092   TypeCB = new QComboBox( GroupArguments );
00093   TypeCB->addItem( tr( "OFF" ) );
00094   TypeCB->addItem( tr( "BACK" ) );
00095   TypeCB->addItem( tr( "FRONT" ) );
00096   TypeCB->addItem( tr( "SLICE" ) );
00097   GroupArgumentsLayout->addWidget( TypeCB, 1, 0, 1, 5 );
00098 
00099   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
00100   layout->setMargin( 0 ); layout->setSpacing( 6 );
00101   layout->addWidget( GroupArguments );
00102 
00103   /* signals and slots connections */
00104   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
00105   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
00106 
00107   connect( resetButton,   SIGNAL( clicked() ), this, SLOT( onReset() ) ) ;
00108 
00109   setHelpFileName( "none.htm" );
00110 
00111   Init();
00112 }
00113 
00114 //=================================================================================
00115 // function : ~ OperationGUI_ClippingDlg()
00116 // purpose  : Destroys the object and frees any allocated resources
00117 //=================================================================================
00118 OperationGUI_ClippingDlg::~ OperationGUI_ClippingDlg()
00119 {
00120   // no need to delete child widgets, Qt does it all for us
00121 }
00122 
00123 //=================================================================================
00124 // function : Init()
00125 // purpose  :
00126 //=================================================================================
00127 void OperationGUI_ClippingDlg::Init()
00128 {
00129   SUIT_ViewWindow* anActiveWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
00130   if ( !anActiveWindow )
00131     return;
00132 
00133   if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
00134     SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
00135     if ( !aVTKFrame )
00136       return;
00137 
00138     TextLabelNear->setText( tr( "Near" ) );
00139     TextLabelFar->setText( tr( "Far" ) );
00140 
00141     TypeCB->hide();
00142 
00143     vtkRenderer* aRenderer = aVTKFrame->getRenderer();
00144     if ( !aRenderer ) return;
00145 
00146     vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
00147     if ( anActiveCamera == NULL ) {
00148       return;
00149     }
00150 
00151       // Find the plane equation for the camera view plane
00152     double vn[3];
00153     anActiveCamera->GetViewPlaneNormal( vn );
00154     double  position[3];
00155     anActiveCamera->GetPosition( position );
00156 
00157     vtkFloatingPointType bounds[6];
00158     aRenderer->ComputeVisiblePropBounds( bounds );
00159 
00160     double center[3];
00161     center[0] = ( bounds[0] + bounds[1] ) / 2.0;
00162     center[1] = ( bounds[2] + bounds[3] ) / 2.0;
00163     center[2] = ( bounds[4] + bounds[5] ) / 2.0;
00164 
00165     double width = sqrt( ( bounds[1]-bounds[0] ) * ( bounds[1]-bounds[0] ) +
00166                          ( bounds[3]-bounds[2] ) * ( bounds[3]-bounds[2] ) +
00167                          ( bounds[5]-bounds[4] ) * ( bounds[5]-bounds[4] ) );
00168 
00169     double distance = sqrt( ( position[0]-center[0] ) * ( position[0]-center[0] ) +
00170                             ( position[1]-center[1] ) * ( position[1]-center[1] ) +
00171                             ( position[2]-center[2] ) * ( position[2]-center[2] ) );
00172 
00173     vtkFloatingPointType range[2] = { distance - width/2.0, distance + width/2.0 };
00174 
00175     SpinBox_Near->setValue( range[0] );
00176     SpinBox_Far->setValue( range[1] );
00177 
00178     return;
00179   }
00180   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
00181     OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
00182 
00183     TextLabelNear->setText( tr( "Depth" ) );
00184     TextLabelFar->setText( tr( "Thickness" ) );
00185 
00186     Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
00187 
00188     double depth, thickness;
00189     int ztype = view3d->ZClipping( depth, thickness );
00190     SpinBox_Near->setValue( depth );
00191     SpinBox_Far->setValue( thickness );
00192     TypeCB->setCurrentIndex( ztype );
00193 
00194     //Handle(V3d_Plane) aPlane = new V3d_Plane( aOCCFrame->getViewer()->getViewer3d() ,0, 0, 1, -100);
00195     //aPlane->Display(view3d);
00196   }
00197 }
00198 
00199 
00200 //=================================================================================
00201 // function : ClickOnApply()
00202 // purpose  :
00203 //=================================================================================
00204 bool OperationGUI_ClippingDlg::ClickOnApply()
00205 {
00206   SUIT_ViewWindow* anActiveWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
00207   if ( !anActiveWindow )
00208     return false;
00209 
00210   if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
00211     SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
00212     if ( !aVTKFrame )
00213       return false;
00214 
00215     vtkRenderer* aRenderer = aVTKFrame->getRenderer();
00216     if ( !aRenderer ) return false;
00217 
00218     vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
00219     if ( anActiveCamera == NULL ) {
00220       return false;
00221     }
00222 
00223     vtkFloatingPointType range[2] = { SpinBox_Near->value(), SpinBox_Far->value() };
00224     if ( range[0] < 0.0 ) range[0] = 0.0;
00225     anActiveCamera->SetClippingRange( range );
00226 
00227     return true;
00228   }
00229   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
00230     OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
00231 
00232     Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
00233 
00234     double depth = SpinBox_Near->value();
00235     double thickness = SpinBox_Far->value();
00236     int aType = TypeCB->currentIndex();
00237 
00238     view3d->SetZClippingType( V3d_TypeOfZclipping( aType ) );
00239     view3d->SetZClippingDepth( depth );
00240     view3d->SetZClippingWidth( thickness );
00241   }
00242 
00243   return true;
00244 }
00245 
00246 //=================================================================================
00247 // function : ClickOnOk()
00248 // purpose  :
00249 //=================================================================================
00250 void OperationGUI_ClippingDlg::ClickOnOk()
00251 {
00252   setIsApplyAndClose( true );
00253   if ( ClickOnApply() )
00254     ClickOnCancel();
00255 }
00256 
00257 //=================================================================================
00258 // function : onActivate()
00259 // purpose  :
00260 //=================================================================================
00261 void OperationGUI_ClippingDlg::onActivate()
00262 {
00263   GEOMBase_Skeleton::ActivateThisDialog();
00264 }
00265 
00266 //=================================================================================
00267 // function : enterEvent()
00268 // purpose  :
00269 //=================================================================================
00270 void OperationGUI_ClippingDlg::enterEvent( QEvent* )
00271 {
00272   this->setEnabled( true );
00273 }
00274 
00275 //=================================================================================
00276 // function : onReset()
00277 // purpose  :
00278 //=================================================================================
00279 void OperationGUI_ClippingDlg::onReset()
00280 {
00281   SUIT_ViewWindow* anActiveWindow =
00282     SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
00283   if ( !anActiveWindow )
00284     return;
00285 
00286   if ( anActiveWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
00287     SVTK_ViewWindow* aVTKFrame = dynamic_cast<SVTK_ViewWindow*>( anActiveWindow );
00288     if ( !aVTKFrame )
00289       return;
00290 
00291     vtkRenderer* aRenderer = aVTKFrame->getRenderer();
00292     if ( !aRenderer ) return;
00293 
00294     vtkCamera* anActiveCamera = aRenderer->GetActiveCamera();
00295     if ( anActiveCamera == NULL ) {
00296       return;
00297     }
00298 
00299       // Find the plane equation for the camera view plane
00300     double vn[3];
00301     anActiveCamera->GetViewPlaneNormal( vn );
00302     double  position[3];
00303     anActiveCamera->GetPosition( position );
00304 
00305     vtkFloatingPointType bounds[6];
00306     aRenderer->ComputeVisiblePropBounds( bounds );
00307 
00308     double center[3];
00309     center[0] = ( bounds[0] + bounds[1] ) / 2.0;
00310     center[1] = ( bounds[2] + bounds[3] ) / 2.0;
00311     center[2] = ( bounds[4] + bounds[5] ) / 2.0;
00312 
00313     double width = sqrt( ( bounds[1]-bounds[0] ) * ( bounds[1]-bounds[0] ) +
00314                          ( bounds[3]-bounds[2] ) * ( bounds[3]-bounds[2] ) +
00315                          ( bounds[5]-bounds[4] ) * ( bounds[5]-bounds[4] ) );
00316 
00317     double distance = sqrt( ( position[0]-center[0] ) * ( position[0]-center[0] ) +
00318                             ( position[1]-center[1] ) * ( position[1]-center[1] ) +
00319                             ( position[2]-center[2] ) * ( position[2]-center[2] ) );
00320 
00321     vtkFloatingPointType range[2] = { distance - width/2.0, distance + width/2.0 };
00322 
00323     SpinBox_Near->setValue( range[0] );
00324     SpinBox_Far->setValue( range[1] );
00325 
00326     return;
00327   }
00328   else if ( anActiveWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
00329     OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anActiveWindow );
00330 
00331     TextLabelNear->setText( tr( "Depth"  ) );
00332     TextLabelFar->setText( tr( "Thickness"  ) );
00333 
00334     Handle(V3d_View) view3d = ( (OCCViewer_ViewPort3d*)aOCCFrame->getViewPort() )->getView();
00335 
00336     view3d->SetZClippingType( V3d_TypeOfZclipping( 0 ) );
00337     view3d->ZFitAll();
00338     double depth, thickness;
00339     int ztype= view3d->ZClipping( depth, thickness );
00340     SpinBox_Near->setValue( 0 );
00341     SpinBox_Far->setValue( 1000 );
00342     TypeCB->setCurrentIndex( ztype );
00343   }
00344 }