1 // Copyright (C) 2007-2012 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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : MeasureGUI_CenterMassDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
27 #include "MeasureGUI_CenterMassDlg.h"
28 #include "MeasureGUI_Widgets.h"
33 #include <GeometryGUI.h>
35 #include <SUIT_Session.h>
36 #include <SUIT_ResourceMgr.h>
37 #include <SalomeApp_Application.h>
38 #include <LightApp_SelectionMgr.h>
39 #include <SalomeApp_Tools.h>
41 #include <BRep_Tool.hxx>
42 #include <TopoDS_Vertex.hxx>
46 //=================================================================================
47 // class : MeasureGUI_CenterMassDlg()
48 // purpose : Constructs a MeasureGUI_CenterMassDlg which is a child of 'parent', with the
49 // name 'name' and widget flags set to 'f'.
50 // The dialog will by default be modeless, unless you set 'modal' to
51 // true to construct a modal dialog.
52 //=================================================================================
53 MeasureGUI_CenterMassDlg::MeasureGUI_CenterMassDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
54 : GEOMBase_Skeleton( theGeometryGUI, parent, false )
56 QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_CENTERMASS" ) ) );
57 QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
59 setWindowTitle( tr( "GEOM_CMASS_TITLE" ) );
61 /***************************************************************/
62 mainFrame()->GroupConstructors->setTitle( tr( "GEOM_CMASS" ) );
63 mainFrame()->RadioButton1->setIcon( image0 );
64 mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
65 mainFrame()->RadioButton2->close();
66 mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
67 mainFrame()->RadioButton3->close();
69 myGrp = new MeasureGUI_1Sel3LineEdit( centralWidget() );
70 myGrp->GroupBox1->setTitle( tr( "GEOM_CENTER" ) );
71 myGrp->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
72 myGrp->TextLabel2->setText( tr( "GEOM_X" ) );
73 myGrp->TextLabel3->setText( tr( "GEOM_Y" ) );
74 myGrp->TextLabel4->setText( tr( "GEOM_Z" ) );
75 myGrp->LineEdit2->setReadOnly( true );
76 myGrp->LineEdit3->setReadOnly( true );
77 myGrp->LineEdit4->setReadOnly( true );
78 myGrp->PushButton1->setIcon( image1 );
79 myGrp->LineEdit1->setReadOnly( true );
81 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
82 layout->setMargin( 0 ); layout->setSpacing( 6 );
83 layout->addWidget( myGrp );
85 /***************************************************************/
87 myHelpFileName = "using_measurement_tools_page.html#center_mass_anchor";
94 //=================================================================================
95 // function : ~MeasureGUI_CenterMassDlg()
96 // purpose : Destroys the object and frees any allocated resources
97 //=================================================================================
98 MeasureGUI_CenterMassDlg::~MeasureGUI_CenterMassDlg()
103 //=================================================================================
106 //=================================================================================
107 void MeasureGUI_CenterMassDlg::Init()
110 myEditCurrentArgument = myGrp->LineEdit1;
112 /* signals and slots connections */
113 connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
114 connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
116 connect( myGrp->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
117 connect( myGrp->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
119 connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
120 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
122 initName( tr( "GEOM_POINT") );
124 SelectionIntoArgument();
128 //=================================================================================
129 // function : ClickOnOk()
131 //=================================================================================
132 void MeasureGUI_CenterMassDlg::ClickOnOk()
134 if ( ClickOnApply() )
139 //=================================================================================
140 // function : ClickOnApply()
142 //=================================================================================
143 bool MeasureGUI_CenterMassDlg::ClickOnApply()
151 // myMeasureGUI->MakeCDGAndDisplay( myGeomShape );
155 //=================================================================================
156 // function : SelectionIntoArgument()
157 // purpose : Called when selection as changed or other case
158 //=================================================================================
159 void MeasureGUI_CenterMassDlg::SelectionIntoArgument()
162 myObj = GEOM::GEOM_Object::_nil();
164 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
165 SALOME_ListIO aSelList;
166 aSelMgr->selectedObjects(aSelList);
168 if (aSelList.Extent() != 1) {
173 GEOM::GEOM_Object_var aSelectedObject =
174 GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
176 if ( aSelectedObject->_is_nil() ) {
181 myObj = aSelectedObject;
183 displayPreview(true);
186 //=================================================================================
187 // function : SetEditCurrentArgument()
189 //=================================================================================
190 void MeasureGUI_CenterMassDlg::SetEditCurrentArgument()
192 myGrp->LineEdit1->setFocus();
193 myEditCurrentArgument = myGrp->LineEdit1;
194 SelectionIntoArgument();
198 //=================================================================================
199 // function : LineEditReturnPressed()
201 //=================================================================================
202 void MeasureGUI_CenterMassDlg::LineEditReturnPressed()
204 QLineEdit* send = ( QLineEdit* )sender();
205 if ( send == myGrp->LineEdit1 ) {
206 myEditCurrentArgument = myGrp->LineEdit1;
207 GEOMBase_Skeleton::LineEditReturnPressed();
212 //=================================================================================
213 // function : ActivateThisDialog()
215 //=================================================================================
216 void MeasureGUI_CenterMassDlg::ActivateThisDialog()
218 GEOMBase_Skeleton::ActivateThisDialog();
220 connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
221 SIGNAL(currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
224 displayPreview(true);
227 //=================================================================================
228 // function : processObject()
229 // purpose : Fill dialog fields in accordance with myObj
230 //=================================================================================
231 void MeasureGUI_CenterMassDlg::processObject()
233 if ( myObj->_is_nil() ) {
234 myGrp->LineEdit1->setText( "" );
235 myGrp->LineEdit2->setText( "" );
236 myGrp->LineEdit3->setText( "" );
237 myGrp->LineEdit4->setText( "" );
241 double x = 0, y = 0, z = 0;
243 getParameters( x, y, z );
245 myGrp->LineEdit1->setText( GEOMBase::GetName( myObj ) );
247 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
248 int aPrecision = resMgr->integerValue( "Geometry", "length_precision", 6 );
249 myGrp->LineEdit2->setText( DlgRef::PrintDoubleValue( x, aPrecision ) );
250 myGrp->LineEdit3->setText( DlgRef::PrintDoubleValue( y, aPrecision ) );
251 myGrp->LineEdit4->setText( DlgRef::PrintDoubleValue( z, aPrecision ) );
253 displayPreview(true);
258 //=================================================================================
259 // function : enterEvent()
261 //=================================================================================
262 void MeasureGUI_CenterMassDlg::enterEvent( QEvent* )
264 if ( !mainFrame()->GroupConstructors->isEnabled() )
265 ActivateThisDialog();
268 //=================================================================================
269 // function : createOperation
271 //=================================================================================
272 GEOM::GEOM_IOperations_ptr MeasureGUI_CenterMassDlg::createOperation()
274 return getGeomEngine()->GetIMeasureOperations( getStudyId() );
277 //=================================================================================
278 // function : isValid
280 //=================================================================================
281 bool MeasureGUI_CenterMassDlg::isValid( QString& )
283 return !myObj->_is_nil();
286 //=================================================================================
287 // function : execute
289 //=================================================================================
290 bool MeasureGUI_CenterMassDlg::getParameters( double& theX, double& theY, double& theZ )
292 if ( myObj->_is_nil() )
296 GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
297 GEOM::GEOM_Object_var anObj = anOper->GetCentreOfMass( myObj );
298 if ( !anOper->IsDone() )
302 if ( !GEOMBase::GetShape( anObj, aShape ) ||
304 aShape.ShapeType() != TopAbs_VERTEX )
307 TopoDS_Vertex aVertex = TopoDS::Vertex( aShape );
309 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
317 catch( const SALOME::SALOME_Exception& e ) {
318 SalomeApp_Tools::QtCatchCorbaException( e );
324 //=================================================================================
325 // function : execute
327 //=================================================================================
328 bool MeasureGUI_CenterMassDlg::execute( ObjectList& objects )
330 GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
331 GEOM::GEOM_Object_var anObj = anOper->GetCentreOfMass( myObj );
333 if ( !anObj->_is_nil() )
334 objects.push_back( anObj._retn() );