Salome HOME
Bug IPAL19097 NPAL16768 is not implemented for "Normal to a face"
[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 // File   : MeasureGUI_CenterMassDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "MeasureGUI_CenterMassDlg.h"
27 #include "MeasureGUI_Widgets.h"
28
29 #include <DlgRef.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_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 //=================================================================================
44 // class    : MeasureGUI_CenterMassDlg()
45 // purpose  : Constructs a MeasureGUI_CenterMassDlg which is a child of 'parent', with the
46 //            name 'name' and widget flags set to 'f'.
47 //            The dialog will by default be modeless, unless you set 'modal' to
48 //            true to construct a modal dialog.
49 //=================================================================================
50 MeasureGUI_CenterMassDlg::MeasureGUI_CenterMassDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
51   : GEOMBase_Skeleton( theGeometryGUI, parent, false )
52 {
53   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_CENTERMASS" ) ) );
54   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
55
56   setWindowTitle( tr( "GEOM_CMASS_TITLE" ) );
57
58   /***************************************************************/
59   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_CMASS" ) );
60   mainFrame()->RadioButton1->setIcon( image0 );
61   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
62   mainFrame()->RadioButton2->close();
63   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
64   mainFrame()->RadioButton3->close();
65
66   myGrp = new MeasureGUI_1Sel3LineEdit( centralWidget() );
67   myGrp->GroupBox1->setTitle( tr( "GEOM_CENTER" ) );
68   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
69   myGrp->TextLabel2->setText( tr( "GEOM_X" ) );
70   myGrp->TextLabel3->setText( tr( "GEOM_Y" ) );
71   myGrp->TextLabel4->setText( tr( "GEOM_Z" ) );
72   myGrp->LineEdit2->setReadOnly( true );
73   myGrp->LineEdit3->setReadOnly( true );
74   myGrp->LineEdit4->setReadOnly( true );
75   myGrp->PushButton1->setIcon( image1 );
76   myGrp->LineEdit1->setReadOnly( true );
77
78   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
79   layout->setMargin( 0 ); layout->setSpacing( 6 );
80   layout->addWidget( myGrp );
81
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
124
125 //=================================================================================
126 // function : ClickOnOk()
127 // purpose  :
128 //=================================================================================
129 void MeasureGUI_CenterMassDlg::ClickOnOk()
130 {
131   if ( ClickOnApply() )
132     ClickOnCancel();
133 }
134
135
136 //=================================================================================
137 // function : ClickOnApply()
138 // purpose  :
139 //=================================================================================
140 bool MeasureGUI_CenterMassDlg::ClickOnApply()
141 {
142   if ( !onAccept() )
143     return false;
144
145   initName();
146   return true;
147
148 //    myMeasureGUI->MakeCDGAndDisplay( myGeomShape );
149 }
150
151
152 //=================================================================================
153 // function : SelectionIntoArgument()
154 // purpose  : Called when selection as changed or other case
155 //=================================================================================
156 void MeasureGUI_CenterMassDlg::SelectionIntoArgument()
157 {
158   erasePreview();
159   myObj = GEOM::GEOM_Object::_nil();
160
161   if ( IObjectCount() != 1 ) {
162     processObject();
163     return;
164   }
165
166   Standard_Boolean testResult = Standard_False;
167   GEOM::GEOM_Object_var aSelectedObject =
168     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
169
170   if ( !testResult || aSelectedObject->_is_nil() ) {
171     processObject();
172     return;
173   }
174
175   myObj = aSelectedObject;
176   processObject();
177   displayPreview();
178 }
179
180
181 //=================================================================================
182 // function : SetEditCurrentArgument()
183 // purpose  :
184 //=================================================================================
185 void MeasureGUI_CenterMassDlg::SetEditCurrentArgument()
186 {
187   myGrp->LineEdit1->setFocus();
188   myEditCurrentArgument = myGrp->LineEdit1;
189   SelectionIntoArgument();
190 }
191
192
193 //=================================================================================
194 // function : LineEditReturnPressed()
195 // purpose  :
196 //=================================================================================
197 void MeasureGUI_CenterMassDlg::LineEditReturnPressed()
198 {
199   QLineEdit* send = ( QLineEdit* )sender();
200   if ( send == myGrp->LineEdit1 ) {
201     myEditCurrentArgument = myGrp->LineEdit1;
202     GEOMBase_Skeleton::LineEditReturnPressed();
203   }
204 }
205
206
207 //=================================================================================
208 // function : ActivateThisDialog()
209 // purpose  :
210 //=================================================================================
211 void MeasureGUI_CenterMassDlg::ActivateThisDialog()
212 {
213   GEOMBase_Skeleton::ActivateThisDialog();
214
215   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
216            SIGNAL(currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
217
218   globalSelection();
219   displayPreview();
220 }
221
222 //=================================================================================
223 // function : processObject()
224 // purpose  : Fill dialog fields in accordance with myObj
225 //=================================================================================
226 void MeasureGUI_CenterMassDlg::processObject()
227 {
228   if ( myObj->_is_nil() ) {
229     myGrp->LineEdit1->setText( "" );
230     myGrp->LineEdit2->setText( "" );
231     myGrp->LineEdit3->setText( "" );
232     myGrp->LineEdit4->setText( "" );
233     erasePreview();
234   }
235   else {
236     double x = 0, y = 0, z = 0;
237     
238     getParameters( x, y, z );
239     
240     myGrp->LineEdit1->setText( GEOMBase::GetName( myObj ) );
241     myGrp->LineEdit2->setText( DlgRef::PrintDoubleValue( x ) );
242     myGrp->LineEdit3->setText( DlgRef::PrintDoubleValue( y ) );
243     myGrp->LineEdit4->setText( DlgRef::PrintDoubleValue( z ) );
244
245     displayPreview();
246   }
247 }
248
249
250 //=================================================================================
251 // function : enterEvent()
252 // purpose  :
253 //=================================================================================
254 void MeasureGUI_CenterMassDlg::enterEvent( QEvent* )
255 {
256   if ( !mainFrame()->GroupConstructors->isEnabled() )
257     ActivateThisDialog();
258 }
259
260 //=================================================================================
261 // function : createOperation
262 // purpose  :
263 //=================================================================================
264 GEOM::GEOM_IOperations_ptr MeasureGUI_CenterMassDlg::createOperation()
265 {
266   return getGeomEngine()->GetIMeasureOperations( getStudyId() );
267 }
268
269 //=================================================================================
270 // function : isValid
271 // purpose  :
272 //=================================================================================
273 bool MeasureGUI_CenterMassDlg::isValid( QString& )
274 {
275   return !myObj->_is_nil();
276 }
277
278 //=================================================================================
279 // function : execute
280 // purpose  :
281 //=================================================================================
282 bool MeasureGUI_CenterMassDlg::getParameters( double& theX, double& theY, double& theZ )
283 {
284   if ( myObj->_is_nil() )
285     return false;
286   else {
287     try {
288       GEOM::GEOM_Object_var anObj;
289       anObj = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetCentreOfMass( myObj );
290       if ( !getOperation()->IsDone() )
291         return false;
292
293       TopoDS_Shape aShape;
294       if ( !GEOMBase::GetShape( anObj, aShape ) ||
295            aShape.IsNull() ||
296            aShape.ShapeType() != TopAbs_VERTEX )
297         return false;
298
299       TopoDS_Vertex aVertex = TopoDS::Vertex( aShape );
300
301       gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
302
303       theX = aPnt.X();
304       theY = aPnt.Y();
305       theZ = aPnt.Z();
306
307       return true;
308     }
309     catch( const SALOME::SALOME_Exception& e ) {
310       SalomeApp_Tools::QtCatchCorbaException( e );
311       return false;
312     }
313   }
314 }
315
316 //=================================================================================
317 // function : execute
318 // purpose  :
319 //=================================================================================
320 bool MeasureGUI_CenterMassDlg::execute( ObjectList& objects )
321 {
322   GEOM::GEOM_Object_var anObj;
323
324   anObj = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetCentreOfMass( myObj );
325
326   if ( !anObj->_is_nil() )
327     objects.push_back( anObj._retn() );
328
329   return true;
330 }