Salome HOME
Update from BR_V5_DEV 13Feb2009
[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, Open CASCADE S.A.S.
25 //
26 #include "MeasureGUI_CenterMassDlg.h"
27 #include "MeasureGUI_Widgets.h"
28
29 #include <DlgRef.h>
30 #include <GEOMBase.h>
31
32 #include <GeometryGUI.h>
33
34 #include <SUIT_Session.h>
35 #include <SUIT_ResourceMgr.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 //=================================================================================
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, false )
54 {
55   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_CENTERMASS" ) ) );
56   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
57
58   setWindowTitle( tr( "GEOM_CMASS_TITLE" ) );
59
60   /***************************************************************/
61   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_CMASS" ) );
62   mainFrame()->RadioButton1->setIcon( image0 );
63   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
64   mainFrame()->RadioButton2->close();
65   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
66   mainFrame()->RadioButton3->close();
67
68   myGrp = new MeasureGUI_1Sel3LineEdit( centralWidget() );
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->setIcon( image1 );
78   myGrp->LineEdit1->setReadOnly( true );
79
80   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
81   layout->setMargin( 0 ); layout->setSpacing( 6 );
82   layout->addWidget( myGrp );
83
84   /***************************************************************/
85
86   myHelpFileName = "using_measurement_tools_page.html#center_mass_anchor";
87
88   /* Initialisation */
89   Init();
90 }
91
92
93 //=================================================================================
94 // function : ~MeasureGUI_CenterMassDlg()
95 // purpose  : Destroys the object and frees any allocated resources
96 //=================================================================================
97 MeasureGUI_CenterMassDlg::~MeasureGUI_CenterMassDlg()
98 {
99 }
100
101
102 //=================================================================================
103 // function : Init()
104 // purpose  :
105 //=================================================================================
106 void MeasureGUI_CenterMassDlg::Init()
107 {
108   /* init variables */
109   myEditCurrentArgument = myGrp->LineEdit1;
110
111    /* signals and slots connections */
112   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
113   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
114
115   connect( myGrp->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
116   connect( myGrp->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
117
118   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
119            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
120
121   initName( tr( "GEOM_POINT") );
122   globalSelection();
123   SelectionIntoArgument();
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   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
164   SALOME_ListIO aSelList;
165   aSelMgr->selectedObjects(aSelList);
166
167   if (aSelList.Extent() != 1) {
168     processObject();
169     return;
170   }
171
172   Standard_Boolean testResult = Standard_False;
173   GEOM::GEOM_Object_var aSelectedObject =
174     GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
175
176   if (!testResult || aSelectedObject->_is_nil()) {
177     processObject();
178     return;
179   }
180
181   myObj = aSelectedObject;
182   processObject();
183   displayPreview();
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     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     myGrp->LineEdit1->setText( "" );
235     myGrp->LineEdit2->setText( "" );
236     myGrp->LineEdit3->setText( "" );
237     myGrp->LineEdit4->setText( "" );
238     erasePreview();
239   }
240   else {
241     double x = 0, y = 0, z = 0;
242     
243     getParameters( x, y, z );
244     
245     myGrp->LineEdit1->setText( GEOMBase::GetName( myObj ) );
246     myGrp->LineEdit2->setText( DlgRef::PrintDoubleValue( x ) );
247     myGrp->LineEdit3->setText( DlgRef::PrintDoubleValue( y ) );
248     myGrp->LineEdit4->setText( DlgRef::PrintDoubleValue( z ) );
249
250     displayPreview();
251   }
252 }
253
254
255 //=================================================================================
256 // function : enterEvent()
257 // purpose  :
258 //=================================================================================
259 void MeasureGUI_CenterMassDlg::enterEvent( QEvent* )
260 {
261   if ( !mainFrame()->GroupConstructors->isEnabled() )
262     ActivateThisDialog();
263 }
264
265 //=================================================================================
266 // function : createOperation
267 // purpose  :
268 //=================================================================================
269 GEOM::GEOM_IOperations_ptr MeasureGUI_CenterMassDlg::createOperation()
270 {
271   return getGeomEngine()->GetIMeasureOperations( getStudyId() );
272 }
273
274 //=================================================================================
275 // function : isValid
276 // purpose  :
277 //=================================================================================
278 bool MeasureGUI_CenterMassDlg::isValid( QString& )
279 {
280   return !myObj->_is_nil();
281 }
282
283 //=================================================================================
284 // function : execute
285 // purpose  :
286 //=================================================================================
287 bool MeasureGUI_CenterMassDlg::getParameters( double& theX, double& theY, double& theZ )
288 {
289   if ( myObj->_is_nil() )
290     return false;
291   else {
292     try {
293       GEOM::GEOM_Object_var anObj;
294       anObj = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetCentreOfMass( myObj );
295       if ( !getOperation()->IsDone() )
296         return false;
297
298       TopoDS_Shape aShape;
299       if ( !GEOMBase::GetShape( anObj, aShape ) ||
300            aShape.IsNull() ||
301            aShape.ShapeType() != TopAbs_VERTEX )
302         return false;
303
304       TopoDS_Vertex aVertex = TopoDS::Vertex( aShape );
305
306       gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
307
308       theX = aPnt.X();
309       theY = aPnt.Y();
310       theZ = aPnt.Z();
311
312       return true;
313     }
314     catch( const SALOME::SALOME_Exception& e ) {
315       SalomeApp_Tools::QtCatchCorbaException( e );
316       return false;
317     }
318   }
319 }
320
321 //=================================================================================
322 // function : execute
323 // purpose  :
324 //=================================================================================
325 bool MeasureGUI_CenterMassDlg::execute( ObjectList& objects )
326 {
327   GEOM::GEOM_Object_var anObj;
328
329   anObj = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetCentreOfMass( myObj );
330
331   if ( !anObj->_is_nil() )
332     objects.push_back( anObj._retn() );
333
334   return true;
335 }