Salome HOME
2ddbc82f04438c4cd74d08a004428bdfa4ed4b0c
[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
31 #include <BRepBuilderAPI_MakeVertex.hxx>
32 #include <BRepGProp.hxx>
33 #include <GProp_GProps.hxx>
34 #include <GProp_PrincipalProps.hxx>
35
36 #include "utilities.h"
37
38 using namespace std;
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, const char* name, MeasureGUI* theMeasureGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
48   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
49 {
50   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_CENTERMASS")));
51   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
52
53   setCaption(tr("GEOM_CMASS_TITLE"));
54
55   /***************************************************************/
56   GroupConstructors->setTitle(tr("GEOM_CMASS"));
57   RadioButton1->setPixmap(image0);
58   RadioButton2->close(TRUE);
59   RadioButton3->close(TRUE);
60
61   GroupC1 = new MeasureGUI_1Sel3LineEdit_QTD(this, "GroupC1");
62   GroupC1->GroupBox1->setTitle(tr("GEOM_CENTER"));
63   GroupC1->TextLabel1->setText(tr("GEOM_OBJECT"));
64   GroupC1->TextLabel2->setText(tr("GEOM_X"));
65   GroupC1->TextLabel3->setText(tr("GEOM_Y"));
66   GroupC1->TextLabel4->setText(tr("GEOM_Z"));
67   GroupC1->LineEdit2->setReadOnly(TRUE);
68   GroupC1->LineEdit3->setReadOnly(TRUE);
69   GroupC1->LineEdit4->setReadOnly(TRUE);
70   GroupC1->PushButton1->setPixmap(image1);
71
72   Layout1->addWidget(GroupC1, 1, 0);
73   /***************************************************************/
74
75   /* Initialisation */
76   myMeasureGUI = theMeasureGUI;
77   Init();
78 }
79
80
81 //=================================================================================
82 // function : ~MeasureGUI_CenterMassDlg()
83 // purpose  : Destroys the object and frees any allocated resources
84 //=================================================================================
85 MeasureGUI_CenterMassDlg::~MeasureGUI_CenterMassDlg()
86 {
87   // no need to delete child widgets, Qt does it all for us
88 }
89
90
91 //=================================================================================
92 // function : Init()
93 // purpose  :
94 //=================================================================================
95 void MeasureGUI_CenterMassDlg::Init()
96 {
97   /* init variables */
98   myEditCurrentArgument = GroupC1->LineEdit1;
99
100   myOkCenterMass = false;
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(GroupC1->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
107   connect(GroupC1->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
108
109   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
110
111   /* displays Dialog */
112   GroupC1->show();
113   this->show();
114
115   return;
116 }
117
118
119 //=================================================================================
120 // function : ClickOnOk()
121 // purpose  :
122 //=================================================================================
123 void MeasureGUI_CenterMassDlg::ClickOnOk()
124 {
125   this->ClickOnApply();
126   ClickOnCancel();
127   return;
128 }
129
130
131 //=================================================================================
132 // function : ClickOnApply()
133 // purpose  :
134 //=================================================================================
135 void MeasureGUI_CenterMassDlg::ClickOnApply()
136 {
137   QAD_Application::getDesktop()->putInfo(tr(""));
138   if (mySimulationTopoDs.IsNull())
139     return;
140   myGeomBase->EraseSimulationShape();
141   mySimulationTopoDs.Nullify();
142
143   if(myOkCenterMass)
144     myMeasureGUI->MakeCDGAndDisplay(myGeomShape);
145   return;
146 }
147
148
149 //=================================================================================
150 // function : SelectionIntoArgument()
151 // purpose  : Called when selection as changed or other case
152 //=================================================================================
153 void MeasureGUI_CenterMassDlg::SelectionIntoArgument()
154 {
155   myGeomBase->EraseSimulationShape();
156   myEditCurrentArgument->setText("");
157   QString aString = "";
158
159   myOkCenterMass = false;
160   GroupC1->LineEdit2->setText("");
161   GroupC1->LineEdit3->setText("");
162   GroupC1->LineEdit4->setText("");
163
164   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
165   if(nbSel != 1)
166     return;
167
168   /*  nbSel == 1  */
169   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject();
170   if(!myGeomBase->GetTopoFromSelection(mySelection, this->myShape))
171     return;  
172
173   Standard_Boolean testResult;
174   myGeomShape = myGeomBase->ConvertIOinGEOMShape(IO, testResult);
175   if(!testResult)
176     return;
177
178   myEditCurrentArgument->setText(aString);
179
180   if(this->CalculateAndDisplayCenterMass())
181     myOkCenterMass = true;
182   return;
183 }
184
185
186 //=================================================================================
187 // function : SetEditCurrentArgument()
188 // purpose  :
189 //=================================================================================
190 void MeasureGUI_CenterMassDlg::SetEditCurrentArgument()
191 {
192   QPushButton* send = (QPushButton*)sender();
193
194   if(send == GroupC1->PushButton1) {
195     GroupC1->LineEdit1->setFocus();
196     myEditCurrentArgument = GroupC1->LineEdit1;
197   }
198
199   this->SelectionIntoArgument();
200   return;
201 }
202
203
204 //=================================================================================
205 // function : LineEditReturnPressed()
206 // purpose  :
207 //=================================================================================
208 void MeasureGUI_CenterMassDlg::LineEditReturnPressed()
209 {
210   QLineEdit* send = (QLineEdit*)sender();
211   if(send == GroupC1->LineEdit1)
212     myEditCurrentArgument = GroupC1->LineEdit1;
213   else
214     return;
215
216   GEOMBase_Skeleton::LineEditReturnPressed();
217   return;
218 }
219
220
221 //=================================================================================
222 // function : ActivateThisDialog()
223 // purpose  :
224 //=================================================================================
225 void MeasureGUI_CenterMassDlg::ActivateThisDialog()
226 {
227   GEOMBase_Skeleton::ActivateThisDialog();
228   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
229   if(!mySimulationTopoDs.IsNull())
230     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
231   return;
232 }
233
234
235 //=================================================================================
236 // function : enterEvent()
237 // purpose  :
238 //=================================================================================
239 void MeasureGUI_CenterMassDlg::enterEvent(QEvent* e)
240 {
241   if(GroupConstructors->isEnabled())
242     return;
243   this->ActivateThisDialog();
244   return;
245 }
246
247
248 //=================================================================================
249 // function : CalculateAndDisplayCenterMass()
250 // purpose  :
251 //=================================================================================
252 bool MeasureGUI_CenterMassDlg::CalculateAndDisplayCenterMass()
253 {
254   myGeomBase->EraseSimulationShape();
255   mySimulationTopoDs.Nullify();
256
257   try {
258     QString resString;     
259     GProp_GProps System;
260
261     if(myShape.ShapeType() == TopAbs_VERTEX)
262       myGeomBase->VertexToPoint(myShape, myCenterMass);
263     else if(myShape.ShapeType() == TopAbs_EDGE || myShape.ShapeType() == TopAbs_WIRE) {
264       BRepGProp::LinearProperties(myShape, System);
265       myCenterMass = System.CentreOfMass();
266     }
267     else if(myShape.ShapeType() == TopAbs_FACE || myShape.ShapeType() == TopAbs_SHELL) {
268       BRepGProp::SurfaceProperties(myShape, System);
269       myCenterMass = System.CentreOfMass();
270     }
271     else {
272       BRepGProp::VolumeProperties(myShape, System);
273       myCenterMass = System.CentreOfMass();
274     }
275     
276     BRepBuilderAPI_MakeVertex V(myCenterMass);
277     mySimulationTopoDs = V.Shape();
278     
279     resString = tr("%1").arg(myCenterMass.X(), 12, 'f', 6);    
280     GroupC1->LineEdit2->setText(resString);
281     
282     resString = tr("%1").arg(myCenterMass.Y(), 12, 'f', 6);    
283     GroupC1->LineEdit3->setText(resString);
284     
285     resString = tr("%1").arg(myCenterMass.Z(), 12, 'f', 6);    
286     GroupC1->LineEdit4->setText(resString);
287     
288     
289     if(!mySimulationTopoDs.IsNull()) {
290       myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
291       return true;
292     }
293   }
294   catch(Standard_Failure) {
295     MESSAGE("Catch intercepted in CalculateAndDisplayCenterMass()");
296   }
297   return false;
298 }
299
300