]> SALOME platform Git repositories - modules/geom.git/blob - src/MeasureGUI/MeasureGUI_CenterMassDlg.cxx
Salome HOME
SMH: Merged GEOM (NEWGUI, HEAD, POLYWORK)
[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 "SalomeApp_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( QWidget* parent )
53 : GEOMBase_Skeleton( 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   
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   /* Initialisation */
84   Init();
85 }
86
87
88 //=================================================================================
89 // function : ~MeasureGUI_CenterMassDlg()
90 // purpose  : Destroys the object and frees any allocated resources
91 //=================================================================================
92 MeasureGUI_CenterMassDlg::~MeasureGUI_CenterMassDlg()
93 {
94 }
95
96
97 //=================================================================================
98 // function : Init()
99 // purpose  :
100 //=================================================================================
101 void MeasureGUI_CenterMassDlg::Init()
102 {
103   /* init variables */
104   myEditCurrentArgument = myGrp->LineEdit1;
105
106    /* signals and slots connections */
107   connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
108   connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
109
110   connect( myGrp->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
111   connect( myGrp->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
112
113   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
114           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
115
116   initName( tr( "GEOM_POINT") );
117   globalSelection();
118   SelectionIntoArgument();
119
120   /* displays Dialog */
121   myGrp->show();
122   this->show();
123 }
124
125
126 //=================================================================================
127 // function : ClickOnOk()
128 // purpose  :
129 //=================================================================================
130 void MeasureGUI_CenterMassDlg::ClickOnOk()
131 {
132   if ( ClickOnApply() )
133     ClickOnCancel();
134 }
135
136
137 //=================================================================================
138 // function : ClickOnApply()
139 // purpose  :
140 //=================================================================================
141 bool MeasureGUI_CenterMassDlg::ClickOnApply()
142 {
143   if ( !onAccept() )
144     return false;
145
146   initName();
147   return true;
148
149 //    myMeasureGUI->MakeCDGAndDisplay( myGeomShape );
150 }
151
152
153 //=================================================================================
154 // function : SelectionIntoArgument()
155 // purpose  : Called when selection as changed or other case
156 //=================================================================================
157 void MeasureGUI_CenterMassDlg::SelectionIntoArgument()
158 {
159   erasePreview();
160   myObj = GEOM::GEOM_Object::_nil();
161
162   if ( IObjectCount() != 1 )
163   {
164     processObject();
165     return;
166   }
167
168   Standard_Boolean testResult = Standard_False;
169   GEOM::GEOM_Object_var aSelectedObject =
170     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
171
172   if ( !testResult || aSelectedObject->_is_nil() )
173   {
174     processObject();
175     return;
176   }
177
178   myObj = aSelectedObject;
179   processObject();
180   displayPreview();
181 }
182
183
184 //=================================================================================
185 // function : SetEditCurrentArgument()
186 // purpose  :
187 //=================================================================================
188 void MeasureGUI_CenterMassDlg::SetEditCurrentArgument()
189 {
190   myGrp->LineEdit1->setFocus();
191   myEditCurrentArgument = myGrp->LineEdit1;
192   SelectionIntoArgument();
193 }
194
195
196 //=================================================================================
197 // function : LineEditReturnPressed()
198 // purpose  :
199 //=================================================================================
200 void MeasureGUI_CenterMassDlg::LineEditReturnPressed()
201 {
202   QLineEdit* send = ( QLineEdit* )sender();
203   if ( send == myGrp->LineEdit1 )
204   {
205     myEditCurrentArgument = myGrp->LineEdit1;
206     GEOMBase_Skeleton::LineEditReturnPressed();
207   }
208 }
209
210
211 //=================================================================================
212 // function : ActivateThisDialog()
213 // purpose  :
214 //=================================================================================
215 void MeasureGUI_CenterMassDlg::ActivateThisDialog()
216 {
217   GEOMBase_Skeleton::ActivateThisDialog();
218
219   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
220           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
221
222   globalSelection();
223   displayPreview();
224 }
225
226 //=================================================================================
227 // function : processObject()
228 // purpose  : Fill dialog fields in accordance with myObj
229 //=================================================================================
230 void MeasureGUI_CenterMassDlg::processObject()
231 {
232   if ( myObj->_is_nil() )
233   {
234     myGrp->LineEdit1->setText( "" );
235     myGrp->LineEdit2->setText( "" );
236     myGrp->LineEdit3->setText( "" );
237     myGrp->LineEdit4->setText( "" );
238     erasePreview();
239   }
240   else
241   {
242     double x = 0, y = 0, z = 0;
243     
244     getParameters( x, y, z );
245     
246     myGrp->LineEdit1->setText( GEOMBase::GetName( myObj ) );
247     myGrp->LineEdit2->setText( QString( "%1" ).arg( x ) );
248     myGrp->LineEdit3->setText( QString( "%1" ).arg( y ) );
249     myGrp->LineEdit4->setText( QString( "%1" ).arg( z ) );
250
251     displayPreview();
252   }
253 }
254
255
256 //=================================================================================
257 // function : enterEvent()
258 // purpose  :
259 //=================================================================================
260 void MeasureGUI_CenterMassDlg::enterEvent( QEvent* e )
261 {
262   if ( !GroupConstructors->isEnabled() )
263     ActivateThisDialog();
264 }
265
266 //=================================================================================
267 // function : createOperation
268 // purpose  :
269 //=================================================================================
270 GEOM::GEOM_IOperations_ptr MeasureGUI_CenterMassDlg::createOperation()
271 {
272   return getGeomEngine()->GetIMeasureOperations( getStudyId() );
273 }
274
275 //=================================================================================
276 // function : isValid
277 // purpose  :
278 //=================================================================================
279 bool MeasureGUI_CenterMassDlg::isValid( QString& )
280 {
281   return !myObj->_is_nil();
282 }
283
284 //=================================================================================
285 // function : execute
286 // purpose  :
287 //=================================================================================
288 bool MeasureGUI_CenterMassDlg::getParameters( double& theX, double& theY, double& theZ )
289 {
290   if ( myObj->_is_nil() )
291     return false;
292   else
293   {
294     try
295     {
296       GEOM::GEOM_Object_var anObj;
297       anObj = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetCentreOfMass( myObj );
298       if ( !getOperation()->IsDone() )
299         return false;
300
301       TopoDS_Shape aShape;
302       if ( !GEOMBase::GetShape( anObj, aShape ) ||
303            aShape.IsNull() ||
304            aShape.ShapeType() != TopAbs_VERTEX )
305         return false;
306
307       TopoDS_Vertex aVertex = TopoDS::Vertex( aShape );
308
309       gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
310
311       theX = aPnt.X();
312       theY = aPnt.Y();
313       theZ = aPnt.Z();
314
315       return true;
316     }
317     catch( const SALOME::SALOME_Exception& e )
318     {
319       SalomeApp_Tools::QtCatchCorbaException( e );
320       return false;
321     }
322   }
323 }
324
325 //=================================================================================
326 // function : execute
327 // purpose  :
328 //=================================================================================
329 bool MeasureGUI_CenterMassDlg::execute( ObjectList& objects )
330 {
331   GEOM::GEOM_Object_var anObj;
332
333   anObj = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetCentreOfMass( myObj );
334
335   if ( !anObj->_is_nil() )
336     objects.push_back( anObj._retn() );
337
338   return true;
339 }
340
341