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