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