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