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