Back to index

salome-geom  6.5.0
MeasureGUI_CenterMassDlg.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   : MeasureGUI_CenterMassDlg.cxx
00025 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
00026 //
00027 #include "MeasureGUI_CenterMassDlg.h"
00028 #include "MeasureGUI_Widgets.h"
00029 
00030 #include <DlgRef.h>
00031 #include <GEOMBase.h>
00032 
00033 #include <GeometryGUI.h>
00034 
00035 #include <SUIT_Session.h>
00036 #include <SUIT_ResourceMgr.h>
00037 #include <SalomeApp_Application.h>
00038 #include <LightApp_SelectionMgr.h>
00039 #include <SalomeApp_Tools.h>
00040 
00041 #include <BRep_Tool.hxx>
00042 #include <TopoDS_Vertex.hxx>
00043 #include <TopoDS.hxx>
00044 #include <gp_Pnt.hxx>
00045 
00046 //=================================================================================
00047 // class    : MeasureGUI_CenterMassDlg()
00048 // purpose  : Constructs a MeasureGUI_CenterMassDlg which is a child of 'parent', with the
00049 //            name 'name' and widget flags set to 'f'.
00050 //            The dialog will by default be modeless, unless you set 'modal' to
00051 //            true to construct a modal dialog.
00052 //=================================================================================
00053 MeasureGUI_CenterMassDlg::MeasureGUI_CenterMassDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
00054   : GEOMBase_Skeleton( theGeometryGUI, parent, false )
00055 {
00056   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_CENTERMASS" ) ) );
00057   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
00058 
00059   setWindowTitle( tr( "GEOM_CMASS_TITLE" ) );
00060 
00061   /***************************************************************/
00062   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_CMASS" ) );
00063   mainFrame()->RadioButton1->setIcon( image0 );
00064   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
00065   mainFrame()->RadioButton2->close();
00066   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
00067   mainFrame()->RadioButton3->close();
00068 
00069   myGrp = new MeasureGUI_1Sel3LineEdit( centralWidget() );
00070   myGrp->GroupBox1->setTitle( tr( "GEOM_CENTER" ) );
00071   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
00072   myGrp->TextLabel2->setText( tr( "GEOM_X" ) );
00073   myGrp->TextLabel3->setText( tr( "GEOM_Y" ) );
00074   myGrp->TextLabel4->setText( tr( "GEOM_Z" ) );
00075   myGrp->LineEdit2->setReadOnly( true );
00076   myGrp->LineEdit3->setReadOnly( true );
00077   myGrp->LineEdit4->setReadOnly( true );
00078   myGrp->PushButton1->setIcon( image1 );
00079   myGrp->LineEdit1->setReadOnly( true );
00080 
00081   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
00082   layout->setMargin( 0 ); layout->setSpacing( 6 );
00083   layout->addWidget( myGrp );
00084 
00085   /***************************************************************/
00086 
00087   myHelpFileName = "using_measurement_tools_page.html#center_mass_anchor";
00088 
00089   /* Initialisation */
00090   Init();
00091 }
00092 
00093 
00094 //=================================================================================
00095 // function : ~MeasureGUI_CenterMassDlg()
00096 // purpose  : Destroys the object and frees any allocated resources
00097 //=================================================================================
00098 MeasureGUI_CenterMassDlg::~MeasureGUI_CenterMassDlg()
00099 {
00100 }
00101 
00102 
00103 //=================================================================================
00104 // function : Init()
00105 // purpose  :
00106 //=================================================================================
00107 void MeasureGUI_CenterMassDlg::Init()
00108 {
00109   /* init variables */
00110   myEditCurrentArgument = myGrp->LineEdit1;
00111 
00112    /* signals and slots connections */
00113   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
00114   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
00115 
00116   connect( myGrp->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
00117   connect( myGrp->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
00118 
00119   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
00120            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
00121 
00122   initName( tr( "GEOM_POINT") );
00123   globalSelection();
00124   SelectionIntoArgument();
00125 }
00126 
00127 
00128 //=================================================================================
00129 // function : ClickOnOk()
00130 // purpose  :
00131 //=================================================================================
00132 void MeasureGUI_CenterMassDlg::ClickOnOk()
00133 {
00134   if ( ClickOnApply() )
00135     ClickOnCancel();
00136 }
00137 
00138 
00139 //=================================================================================
00140 // function : ClickOnApply()
00141 // purpose  :
00142 //=================================================================================
00143 bool MeasureGUI_CenterMassDlg::ClickOnApply()
00144 {
00145   if ( !onAccept() )
00146     return false;
00147 
00148   initName();
00149   return true;
00150 
00151 //    myMeasureGUI->MakeCDGAndDisplay( myGeomShape );
00152 }
00153 
00154 
00155 //=================================================================================
00156 // function : SelectionIntoArgument()
00157 // purpose  : Called when selection as changed or other case
00158 //=================================================================================
00159 void MeasureGUI_CenterMassDlg::SelectionIntoArgument()
00160 {
00161   erasePreview();
00162   myObj = GEOM::GEOM_Object::_nil();
00163 
00164   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
00165   SALOME_ListIO aSelList;
00166   aSelMgr->selectedObjects(aSelList);
00167 
00168   if (aSelList.Extent() != 1) {
00169     processObject();
00170     return;
00171   }
00172 
00173   GEOM::GEOM_Object_var aSelectedObject =
00174     GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
00175 
00176   if ( aSelectedObject->_is_nil() ) {
00177     processObject();
00178     return;
00179   }
00180 
00181   myObj = aSelectedObject;
00182   processObject();
00183   displayPreview(true);
00184 }
00185 
00186 //=================================================================================
00187 // function : SetEditCurrentArgument()
00188 // purpose  :
00189 //=================================================================================
00190 void MeasureGUI_CenterMassDlg::SetEditCurrentArgument()
00191 {
00192   myGrp->LineEdit1->setFocus();
00193   myEditCurrentArgument = myGrp->LineEdit1;
00194   SelectionIntoArgument();
00195 }
00196 
00197 
00198 //=================================================================================
00199 // function : LineEditReturnPressed()
00200 // purpose  :
00201 //=================================================================================
00202 void MeasureGUI_CenterMassDlg::LineEditReturnPressed()
00203 {
00204   QLineEdit* send = ( QLineEdit* )sender();
00205   if ( send == myGrp->LineEdit1 ) {
00206     myEditCurrentArgument = myGrp->LineEdit1;
00207     GEOMBase_Skeleton::LineEditReturnPressed();
00208   }
00209 }
00210 
00211 
00212 //=================================================================================
00213 // function : ActivateThisDialog()
00214 // purpose  :
00215 //=================================================================================
00216 void MeasureGUI_CenterMassDlg::ActivateThisDialog()
00217 {
00218   GEOMBase_Skeleton::ActivateThisDialog();
00219 
00220   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
00221            SIGNAL(currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
00222 
00223   globalSelection();
00224   displayPreview(true);
00225 }
00226 
00227 //=================================================================================
00228 // function : processObject()
00229 // purpose  : Fill dialog fields in accordance with myObj
00230 //=================================================================================
00231 void MeasureGUI_CenterMassDlg::processObject()
00232 {
00233   if ( myObj->_is_nil() ) {
00234     myGrp->LineEdit1->setText( "" );
00235     myGrp->LineEdit2->setText( "" );
00236     myGrp->LineEdit3->setText( "" );
00237     myGrp->LineEdit4->setText( "" );
00238     erasePreview();
00239   }
00240   else {
00241     double x = 0, y = 0, z = 0;
00242     
00243     getParameters( x, y, z );
00244     
00245     myGrp->LineEdit1->setText( GEOMBase::GetName( myObj ) );
00246 
00247     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
00248     int aPrecision = resMgr->integerValue( "Geometry", "length_precision", 6 );
00249     myGrp->LineEdit2->setText( DlgRef::PrintDoubleValue( x, aPrecision ) );
00250     myGrp->LineEdit3->setText( DlgRef::PrintDoubleValue( y, aPrecision ) );
00251     myGrp->LineEdit4->setText( DlgRef::PrintDoubleValue( z, aPrecision ) );
00252 
00253     displayPreview(true);
00254   }
00255 }
00256 
00257 
00258 //=================================================================================
00259 // function : enterEvent()
00260 // purpose  :
00261 //=================================================================================
00262 void MeasureGUI_CenterMassDlg::enterEvent( QEvent* )
00263 {
00264   if ( !mainFrame()->GroupConstructors->isEnabled() )
00265     ActivateThisDialog();
00266 }
00267 
00268 //=================================================================================
00269 // function : createOperation
00270 // purpose  :
00271 //=================================================================================
00272 GEOM::GEOM_IOperations_ptr MeasureGUI_CenterMassDlg::createOperation()
00273 {
00274   return getGeomEngine()->GetIMeasureOperations( getStudyId() );
00275 }
00276 
00277 //=================================================================================
00278 // function : isValid
00279 // purpose  :
00280 //=================================================================================
00281 bool MeasureGUI_CenterMassDlg::isValid( QString& )
00282 {
00283   return !myObj->_is_nil();
00284 }
00285 
00286 //=================================================================================
00287 // function : execute
00288 // purpose  :
00289 //=================================================================================
00290 bool MeasureGUI_CenterMassDlg::getParameters( double& theX, double& theY, double& theZ )
00291 {
00292   if ( myObj->_is_nil() )
00293     return false;
00294   else {
00295     try {
00296       GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
00297       GEOM::GEOM_Object_var anObj = anOper->GetCentreOfMass( myObj );
00298       if ( !anOper->IsDone() )
00299         return false;
00300 
00301       TopoDS_Shape aShape;
00302       if ( !GEOMBase::GetShape( anObj, aShape ) ||
00303            aShape.IsNull() ||
00304            aShape.ShapeType() != TopAbs_VERTEX )
00305         return false;
00306 
00307       TopoDS_Vertex aVertex = TopoDS::Vertex( aShape );
00308 
00309       gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
00310 
00311       theX = aPnt.X();
00312       theY = aPnt.Y();
00313       theZ = aPnt.Z();
00314 
00315       return true;
00316     }
00317     catch( const SALOME::SALOME_Exception& e ) {
00318       SalomeApp_Tools::QtCatchCorbaException( e );
00319       return false;
00320     }
00321   }
00322 }
00323 
00324 //=================================================================================
00325 // function : execute
00326 // purpose  :
00327 //=================================================================================
00328 bool MeasureGUI_CenterMassDlg::execute( ObjectList& objects )
00329 {
00330   GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
00331   GEOM::GEOM_Object_var anObj = anOper->GetCentreOfMass( myObj );
00332 
00333   if ( !anObj->_is_nil() )
00334     objects.push_back( anObj._retn() );
00335 
00336   return true;
00337 }