Salome HOME
7fd7c774b8466205619eef0adec4d009af88b1ec
[modules/geom.git] / src / MeasureGUI / MeasureGUI_CenterMassDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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.
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : MeasureGUI_CenterMassDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "MeasureGUI_CenterMassDlg.h"
30 #include "MeasureGUI_1Sel3LineEdit_QTD.h"
31
32 #include "utilities.h"
33 #include "SUIT_Session.h"
34 #include "SalomeApp_Application.h"
35 #include "LightApp_SelectionMgr.h"
36 #include "SalomeApp_Tools.h"
37
38 #include <BRep_Tool.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <TopoDS.hxx>
41 #include <gp_Pnt.hxx>
42
43 #include <qlabel.h>
44
45 //=================================================================================
46 // class    : MeasureGUI_CenterMassDlg()
47 // purpose  : Constructs a MeasureGUI_CenterMassDlg which is a child of 'parent', with the
48 //            name 'name' and widget flags set to 'f'.
49 //            The dialog will by default be modeless, unless you set 'modal' to
50 //            TRUE to construct a modal dialog.
51 //=================================================================================
52 MeasureGUI_CenterMassDlg::MeasureGUI_CenterMassDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
53   : GEOMBase_Skeleton(theGeometryGUI, parent, "MeasureGUI_CenterMassDlg", false,
54                       WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
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   setCaption( tr( "GEOM_CMASS_TITLE" ) );
60
61   /***************************************************************/
62   GroupConstructors->setTitle( tr( "GEOM_CMASS" ) );
63   RadioButton1->setPixmap( image0 );
64   RadioButton2->close( TRUE );
65   RadioButton3->close( TRUE );
66
67   myGrp = new MeasureGUI_1Sel3LineEdit_QTD( this, "myGrp" );
68   myGrp->GroupBox1->setTitle( tr( "GEOM_CENTER" ) );
69   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
70   myGrp->TextLabel2->setText( tr( "GEOM_X" ) );
71   myGrp->TextLabel3->setText( tr( "GEOM_Y" ) );
72   myGrp->TextLabel4->setText( tr( "GEOM_Z" ) );
73   myGrp->LineEdit2->setReadOnly( TRUE );
74   myGrp->LineEdit3->setReadOnly( TRUE );
75   myGrp->LineEdit4->setReadOnly( TRUE );
76   myGrp->PushButton1->setPixmap( image1 );
77   myGrp->LineEdit1->setReadOnly( true );
78
79   Layout1->addWidget( myGrp, 2, 0 );
80   /***************************************************************/
81
82   myHelpFileName = "files/salome2_sp3_measuregui_functions.htm#Center_gravity";
83
84   /* Initialisation */
85   Init();
86 }
87
88
89 //=================================================================================
90 // function : ~MeasureGUI_CenterMassDlg()
91 // purpose  : Destroys the object and frees any allocated resources
92 //=================================================================================
93 MeasureGUI_CenterMassDlg::~MeasureGUI_CenterMassDlg()
94 {
95 }
96
97
98 //=================================================================================
99 // function : Init()
100 // purpose  :
101 //=================================================================================
102 void MeasureGUI_CenterMassDlg::Init()
103 {
104   /* init variables */
105   myEditCurrentArgument = myGrp->LineEdit1;
106
107    /* signals and slots connections */
108   connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
109   connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
110
111   connect( myGrp->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
112   connect( myGrp->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
113
114   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
115           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
116
117   initName( tr( "GEOM_POINT") );
118   globalSelection();
119   SelectionIntoArgument();
120
121   /* displays Dialog */
122   myGrp->show();
123   this->show();
124 }
125
126
127 //=================================================================================
128 // function : ClickOnOk()
129 // purpose  :
130 //=================================================================================
131 void MeasureGUI_CenterMassDlg::ClickOnOk()
132 {
133   if ( ClickOnApply() )
134     ClickOnCancel();
135 }
136
137
138 //=================================================================================
139 // function : ClickOnApply()
140 // purpose  :
141 //=================================================================================
142 bool MeasureGUI_CenterMassDlg::ClickOnApply()
143 {
144   if ( !onAccept() )
145     return false;
146
147   initName();
148   return true;
149
150 //    myMeasureGUI->MakeCDGAndDisplay( myGeomShape );
151 }
152
153
154 //=================================================================================
155 // function : SelectionIntoArgument()
156 // purpose  : Called when selection as changed or other case
157 //=================================================================================
158 void MeasureGUI_CenterMassDlg::SelectionIntoArgument()
159 {
160   erasePreview();
161   myObj = GEOM::GEOM_Object::_nil();
162
163   if ( IObjectCount() != 1 )
164   {
165     processObject();
166     return;
167   }
168
169   Standard_Boolean testResult = Standard_False;
170   GEOM::GEOM_Object_var aSelectedObject =
171     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
172
173   if ( !testResult || aSelectedObject->_is_nil() )
174   {
175     processObject();
176     return;
177   }
178
179   myObj = aSelectedObject;
180   processObject();
181   displayPreview();
182 }
183
184
185 //=================================================================================
186 // function : SetEditCurrentArgument()
187 // purpose  :
188 //=================================================================================
189 void MeasureGUI_CenterMassDlg::SetEditCurrentArgument()
190 {
191   myGrp->LineEdit1->setFocus();
192   myEditCurrentArgument = myGrp->LineEdit1;
193   SelectionIntoArgument();
194 }
195
196
197 //=================================================================================
198 // function : LineEditReturnPressed()
199 // purpose  :
200 //=================================================================================
201 void MeasureGUI_CenterMassDlg::LineEditReturnPressed()
202 {
203   QLineEdit* send = ( QLineEdit* )sender();
204   if ( send == myGrp->LineEdit1 )
205   {
206     myEditCurrentArgument = myGrp->LineEdit1;
207     GEOMBase_Skeleton::LineEditReturnPressed();
208   }
209 }
210
211
212 //=================================================================================
213 // function : ActivateThisDialog()
214 // purpose  :
215 //=================================================================================
216 void MeasureGUI_CenterMassDlg::ActivateThisDialog()
217 {
218   GEOMBase_Skeleton::ActivateThisDialog();
219
220   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
221           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
222
223   globalSelection();
224   displayPreview();
225 }
226
227 //=================================================================================
228 // function : processObject()
229 // purpose  : Fill dialog fields in accordance with myObj
230 //=================================================================================
231 void MeasureGUI_CenterMassDlg::processObject()
232 {
233   if ( myObj->_is_nil() )
234   {
235     myGrp->LineEdit1->setText( "" );
236     myGrp->LineEdit2->setText( "" );
237     myGrp->LineEdit3->setText( "" );
238     myGrp->LineEdit4->setText( "" );
239     erasePreview();
240   }
241   else
242   {
243     double x = 0, y = 0, z = 0;
244     
245     getParameters( x, y, z );
246     
247     myGrp->LineEdit1->setText( GEOMBase::GetName( myObj ) );
248     myGrp->LineEdit2->setText( QString( "%1" ).arg( x ) );
249     myGrp->LineEdit3->setText( QString( "%1" ).arg( y ) );
250     myGrp->LineEdit4->setText( QString( "%1" ).arg( z ) );
251
252     displayPreview();
253   }
254 }
255
256
257 //=================================================================================
258 // function : enterEvent()
259 // purpose  :
260 //=================================================================================
261 void MeasureGUI_CenterMassDlg::enterEvent( QEvent* e )
262 {
263   if ( !GroupConstructors->isEnabled() )
264     ActivateThisDialog();
265 }
266
267 //=================================================================================
268 // function : createOperation
269 // purpose  :
270 //=================================================================================
271 GEOM::GEOM_IOperations_ptr MeasureGUI_CenterMassDlg::createOperation()
272 {
273   return getGeomEngine()->GetIMeasureOperations( getStudyId() );
274 }
275
276 //=================================================================================
277 // function : isValid
278 // purpose  :
279 //=================================================================================
280 bool MeasureGUI_CenterMassDlg::isValid( QString& )
281 {
282   return !myObj->_is_nil();
283 }
284
285 //=================================================================================
286 // function : execute
287 // purpose  :
288 //=================================================================================
289 bool MeasureGUI_CenterMassDlg::getParameters( double& theX, double& theY, double& theZ )
290 {
291   if ( myObj->_is_nil() )
292     return false;
293   else
294   {
295     try
296     {
297       GEOM::GEOM_Object_var anObj;
298       anObj = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetCentreOfMass( myObj );
299       if ( !getOperation()->IsDone() )
300         return false;
301
302       TopoDS_Shape aShape;
303       if ( !GEOMBase::GetShape( anObj, aShape ) ||
304            aShape.IsNull() ||
305            aShape.ShapeType() != TopAbs_VERTEX )
306         return false;
307
308       TopoDS_Vertex aVertex = TopoDS::Vertex( aShape );
309
310       gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
311
312       theX = aPnt.X();
313       theY = aPnt.Y();
314       theZ = aPnt.Z();
315
316       return true;
317     }
318     catch( const SALOME::SALOME_Exception& e )
319     {
320       SalomeApp_Tools::QtCatchCorbaException( e );
321       return false;
322     }
323   }
324 }
325
326 //=================================================================================
327 // function : execute
328 // purpose  :
329 //=================================================================================
330 bool MeasureGUI_CenterMassDlg::execute( ObjectList& objects )
331 {
332   GEOM::GEOM_Object_var anObj;
333
334   anObj = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetCentreOfMass( myObj );
335
336   if ( !anObj->_is_nil() )
337     objects.push_back( anObj._retn() );
338
339   return true;
340 }