Salome HOME
Merge branch 'fbt/add_header_for_mpi_compilation'
[modules/geom.git] / src / MeasureGUI / MeasureGUI_CenterMassDlg.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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, or (at your option) any later version.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : MeasureGUI_CenterMassDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "MeasureGUI_CenterMassDlg.h"
28 #include "MeasureGUI_Widgets.h"
29
30 #include <DlgRef.h>
31 #include <GEOMBase.h>
32
33 #include <GeometryGUI.h>
34
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>
40
41 #include <BRep_Tool.hxx>
42 #include <TopoDS_Vertex.hxx>
43 #include <TopoDS.hxx>
44 #include <gp_Pnt.hxx>
45
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 )
55 {
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" ) ) );
58
59   setWindowTitle( tr( "GEOM_CMASS_TITLE" ) );
60
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();
68
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 );
80
81   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
82   layout->setMargin( 0 ); layout->setSpacing( 6 );
83   layout->addWidget( myGrp );
84
85   /***************************************************************/
86
87   myHelpFileName = "center_mass_page.html";
88
89   /* Initialisation */
90   Init();
91 }
92
93
94 //=================================================================================
95 // function : ~MeasureGUI_CenterMassDlg()
96 // purpose  : Destroys the object and frees any allocated resources
97 //=================================================================================
98 MeasureGUI_CenterMassDlg::~MeasureGUI_CenterMassDlg()
99 {
100 }
101
102
103 //=================================================================================
104 // function : Init()
105 // purpose  :
106 //=================================================================================
107 void MeasureGUI_CenterMassDlg::Init()
108 {
109   /* init variables */
110   myEditCurrentArgument = myGrp->LineEdit1;
111
112    /* signals and slots connections */
113   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
114   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
115
116   connect( myGrp->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
117   connect( myGrp->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
118
119   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
120            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
121
122   initName( tr( "GEOM_POINT") );
123   activateSelection();
124   SelectionIntoArgument();
125 }
126
127 //=================================================================================
128 // function : activateSelection
129 // purpose  : 
130 //=================================================================================
131 void MeasureGUI_CenterMassDlg::activateSelection()
132 {
133   globalSelection( GEOM_ALLSHAPES );
134   std::list<int> needTypes;
135   needTypes.push_back( TopAbs_SHAPE ), needTypes.push_back( TopAbs_VERTEX ), needTypes.push_back( TopAbs_EDGE ); 
136   needTypes.push_back( TopAbs_WIRE ), needTypes.push_back( TopAbs_FACE ), needTypes.push_back( TopAbs_SHELL );
137   needTypes.push_back( TopAbs_SOLID ), needTypes.push_back( TopAbs_COMPSOLID ), needTypes.push_back( TopAbs_COMPOUND );
138   localSelection(GEOM::GEOM_Object::_nil(), needTypes );
139 }
140
141 //=================================================================================
142 // function : ClickOnOk()
143 // purpose  :
144 //=================================================================================
145 void MeasureGUI_CenterMassDlg::ClickOnOk()
146 {
147   if ( ClickOnApply() )
148     ClickOnCancel();
149 }
150
151
152 //=================================================================================
153 // function : ClickOnApply()
154 // purpose  :
155 //=================================================================================
156 bool MeasureGUI_CenterMassDlg::ClickOnApply()
157 {
158   if ( !onAccept() )
159     return false;
160
161   initName();
162   activateSelection();
163   return true;
164
165 //    myMeasureGUI->MakeCDGAndDisplay( myGeomShape );
166 }
167
168
169 //=================================================================================
170 // function : SelectionIntoArgument()
171 // purpose  : Called when selection as changed or other case
172 //=================================================================================
173 void MeasureGUI_CenterMassDlg::SelectionIntoArgument()
174 {
175   erasePreview();
176
177   myObj.nullify();
178   QList<TopAbs_ShapeEnum> aTypes;
179   aTypes << TopAbs_VERTEX << TopAbs_EDGE << TopAbs_WIRE << TopAbs_FACE << TopAbs_SHELL << TopAbs_SOLID << TopAbs_COMPSOLID << TopAbs_COMPOUND << TopAbs_SHAPE;
180   myObj = getSelected( aTypes );
181
182   if ( !myObj ) {
183     processObject();
184     return;
185   }
186
187   processObject();
188   displayPreview(true);
189 }
190
191 //=================================================================================
192 // function : SetEditCurrentArgument()
193 // purpose  :
194 //=================================================================================
195 void MeasureGUI_CenterMassDlg::SetEditCurrentArgument()
196 {
197   myGrp->LineEdit1->setFocus();
198   myEditCurrentArgument = myGrp->LineEdit1;
199   SelectionIntoArgument();
200 }
201
202
203 //=================================================================================
204 // function : LineEditReturnPressed()
205 // purpose  :
206 //=================================================================================
207 void MeasureGUI_CenterMassDlg::LineEditReturnPressed()
208 {
209   QLineEdit* send = ( QLineEdit* )sender();
210   if ( send == myGrp->LineEdit1 ) {
211     myEditCurrentArgument = myGrp->LineEdit1;
212     GEOMBase_Skeleton::LineEditReturnPressed();
213   }
214 }
215
216
217 //=================================================================================
218 // function : ActivateThisDialog()
219 // purpose  :
220 //=================================================================================
221 void MeasureGUI_CenterMassDlg::ActivateThisDialog()
222 {
223   GEOMBase_Skeleton::ActivateThisDialog();
224
225   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
226            SIGNAL(currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
227
228   activateSelection();
229   displayPreview(true);
230 }
231
232 //=================================================================================
233 // function : processObject()
234 // purpose  : Fill dialog fields in accordance with myObj
235 //=================================================================================
236 void MeasureGUI_CenterMassDlg::processObject()
237 {
238   if ( !myObj ) {
239     myGrp->LineEdit1->setText( "" );
240     myGrp->LineEdit2->setText( "" );
241     myGrp->LineEdit3->setText( "" );
242     myGrp->LineEdit4->setText( "" );
243     erasePreview();
244   }
245   else {
246     double x = 0, y = 0, z = 0;
247     
248     getParameters( x, y, z );
249     
250     myGrp->LineEdit1->setText( GEOMBase::GetName( myObj.get() ) );
251
252     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
253     int aPrecision = resMgr->integerValue( "Geometry", "length_precision", 6 );
254     myGrp->LineEdit2->setText( DlgRef::PrintDoubleValue( x, aPrecision ) );
255     myGrp->LineEdit3->setText( DlgRef::PrintDoubleValue( y, aPrecision ) );
256     myGrp->LineEdit4->setText( DlgRef::PrintDoubleValue( z, aPrecision ) );
257
258     displayPreview(true);
259   }
260 }
261
262
263 //=================================================================================
264 // function : enterEvent()
265 // purpose  :
266 //=================================================================================
267 void MeasureGUI_CenterMassDlg::enterEvent( QEvent* )
268 {
269   if ( !mainFrame()->GroupConstructors->isEnabled() )
270     ActivateThisDialog();
271 }
272
273 //=================================================================================
274 // function : createOperation
275 // purpose  :
276 //=================================================================================
277 GEOM::GEOM_IOperations_ptr MeasureGUI_CenterMassDlg::createOperation()
278 {
279   return getGeomEngine()->GetIMeasureOperations( getStudyId() );
280 }
281
282 //=================================================================================
283 // function : isValid
284 // purpose  :
285 //=================================================================================
286 bool MeasureGUI_CenterMassDlg::isValid( QString& )
287 {
288   return myObj;
289 }
290
291 //=================================================================================
292 // function : execute
293 // purpose  :
294 //=================================================================================
295 bool MeasureGUI_CenterMassDlg::getParameters( double& theX, double& theY, double& theZ )
296 {
297   if ( !myObj )
298     return false;
299   else {
300     try {
301       GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
302       GEOM::GEOM_Object_var anObj = anOper->GetCentreOfMass( myObj.get() );
303       if ( !anOper->IsDone() )
304         return false;
305
306       TopoDS_Shape aShape;
307       if ( !GEOMBase::GetShape( anObj, aShape ) ||
308            aShape.IsNull() ||
309            aShape.ShapeType() != TopAbs_VERTEX )
310         return false;
311
312       TopoDS_Vertex aVertex = TopoDS::Vertex( aShape );
313
314       gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
315
316       theX = aPnt.X();
317       theY = aPnt.Y();
318       theZ = aPnt.Z();
319
320       return true;
321     }
322     catch( const SALOME::SALOME_Exception& e ) {
323       SalomeApp_Tools::QtCatchCorbaException( e );
324       return false;
325     }
326   }
327 }
328
329 //=================================================================================
330 // function : execute
331 // purpose  :
332 //=================================================================================
333 bool MeasureGUI_CenterMassDlg::execute( ObjectList& objects )
334 {
335   GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
336   GEOM::GEOM_Object_var anObj = anOper->GetCentreOfMass( myObj.get() );
337
338   if ( !anObj->_is_nil() )
339     objects.push_back( anObj._retn() );
340
341   return true;
342 }
343
344 //=================================================================================
345 // function : addSubshapeToStudy
346 // purpose  : virtual method to add new SubObjects if local selection
347 //=================================================================================
348 void MeasureGUI_CenterMassDlg::addSubshapesToStudy()
349 {
350   GEOMBase::PublishSubObject( myObj.get() );
351 }