Salome HOME
DCQ : New Architecture
[modules/geom.git] / src / MeasureGUI / MeasureGUI_InertiaDlg.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_InertiaDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27
28 using namespace std;
29 #include "MeasureGUI_InertiaDlg.h"
30
31 #include <BRepGProp.hxx>
32 #include <GProp_GProps.hxx>
33 #include <GProp_PrincipalProps.hxx>
34
35 //=================================================================================
36 // class    : MeasureGUI_InertiaDlg()
37 // purpose  : Constructs a MeasureGUI_InertiaDlg which is a child of 'parent', with the 
38 //            name 'name' and widget flags set to 'f'.
39 //            The dialog will by default be modeless, unless you set 'modal' to
40 //            TRUE to construct a modal dialog.
41 //=================================================================================
42 MeasureGUI_InertiaDlg::MeasureGUI_InertiaDlg(QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
43   :MeasureGUI_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
44 {
45   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_INERTIA")));
46   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
47     
48   setCaption(tr("GEOM_INERTIA_TITLE"));
49
50   /***************************************************************/
51   GroupConstructors->setTitle(tr("GEOM_INERTIA_CONSTR"));
52   RadioButton1->setPixmap(image0);
53
54   GroupC1 = new MeasureGUI_1Sel12LineEdit_QTD(this, "GroupC1");
55   GroupC1->GroupBox1->setTitle(tr("GEOM_MATRIX"));
56   GroupC1->TextLabel1->setText(tr("GEOM_OBJECT"));
57   GroupC1->TextLabel2->setText(tr("GEOM_INERTIA_I").arg("1"));
58   GroupC1->TextLabel3->setText(tr("GEOM_INERTIA_I").arg("2"));
59   GroupC1->TextLabel4->setText(tr("GEOM_INERTIA_I").arg("3"));
60   GroupC1->TextLabel5->setText(tr("GEOM_INERTIA_IXYZ"));
61   GroupC1->LineEdit11->setReadOnly(TRUE);
62   GroupC1->LineEdit12->setReadOnly(TRUE);
63   GroupC1->LineEdit13->setReadOnly(TRUE);
64   GroupC1->LineEdit21->setReadOnly(TRUE);
65   GroupC1->LineEdit22->setReadOnly(TRUE);
66   GroupC1->LineEdit23->setReadOnly(TRUE);
67   GroupC1->LineEdit31->setReadOnly(TRUE);
68   GroupC1->LineEdit32->setReadOnly(TRUE);
69   GroupC1->LineEdit33->setReadOnly(TRUE);
70   GroupC1->LineEdit41->setReadOnly(TRUE);
71   GroupC1->LineEdit42->setReadOnly(TRUE);
72   GroupC1->LineEdit43->setReadOnly(TRUE);
73   GroupC1->PushButton1->setPixmap(image1);
74
75   Layout1->addWidget(GroupC1, 1, 0);
76   /***************************************************************/
77
78   /* Initialisation */
79   Init();
80 }
81
82
83 //=================================================================================
84 // function : ~MeasureGUI_InertiaDlg()
85 // purpose  : Destroys the object and frees any allocated resources
86 //=================================================================================
87 MeasureGUI_InertiaDlg::~MeasureGUI_InertiaDlg()
88 {
89   // no need to delete child widgets, Qt does it all for us
90 }
91
92
93 //=================================================================================
94 // function : Init()
95 // purpose  :
96 //=================================================================================
97 void MeasureGUI_InertiaDlg::Init()
98 {
99   /* init variables */
100   myEditCurrentArgument = GroupC1->LineEdit1;
101
102    /* signals and slots connections */
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 : SelectionIntoArgument()
118 // purpose  : Called when selection as changed or other case
119 //=================================================================================
120 void MeasureGUI_InertiaDlg::SelectionIntoArgument()
121 {
122   myEditCurrentArgument->setText("");
123   QString aString = "";
124
125   GroupC1->LineEdit11->setText("");
126   GroupC1->LineEdit12->setText("");
127   GroupC1->LineEdit13->setText("");
128   GroupC1->LineEdit21->setText("");
129   GroupC1->LineEdit22->setText("");
130   GroupC1->LineEdit23->setText("");
131   GroupC1->LineEdit31->setText("");
132   GroupC1->LineEdit32->setText("");
133   GroupC1->LineEdit33->setText("");
134   GroupC1->LineEdit41->setText("");
135   GroupC1->LineEdit42->setText("");
136   GroupC1->LineEdit43->setText("");
137
138   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
139   if(nbSel != 1)
140     return;
141
142   /*  nbSel == 1  */
143   TopoDS_Shape S;
144   if(!myGeomBase->GetTopoFromSelection(mySelection, S) || S.IsNull())
145     return;
146
147   GroupC1->LineEdit1->setText(aString);
148
149   this->CalculateAndDisplayInertia(S);
150   return;
151 }
152
153
154 //=================================================================================
155 // function : SetEditCurrentArgument()
156 // purpose  :
157 //=================================================================================
158 void MeasureGUI_InertiaDlg::SetEditCurrentArgument()
159 {
160   QPushButton* send = (QPushButton*)sender();
161
162   if(send == GroupC1->PushButton1) {
163     GroupC1->LineEdit1->setFocus();
164     myEditCurrentArgument = GroupC1->LineEdit1;
165   }
166
167   this->SelectionIntoArgument();
168   return;
169 }
170
171
172 //=================================================================================
173 // function : LineEditReturnPressed()
174 // purpose  :
175 //=================================================================================
176 void MeasureGUI_InertiaDlg::LineEditReturnPressed()
177 {
178   QLineEdit* send = (QLineEdit*)sender();
179   if(send == GroupC1->LineEdit1)
180     myEditCurrentArgument = GroupC1->LineEdit1;
181   else
182     return;
183
184   MeasureGUI_Skeleton::LineEditReturnPressed();
185   return;
186 }
187
188
189 //=================================================================================
190 // function : ActivateThisDialog()
191 // purpose  :
192 //=================================================================================
193 void MeasureGUI_InertiaDlg::ActivateThisDialog()
194 {
195   MeasureGUI_Skeleton::ActivateThisDialog();
196   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
197   return;
198 }
199
200
201 //=================================================================================
202 // function : enterEvent()
203 // purpose  :
204 //=================================================================================
205 void MeasureGUI_InertiaDlg::enterEvent(QEvent* e)
206 {
207   if(GroupConstructors->isEnabled())
208     return;
209   this->ActivateThisDialog();
210   return;
211 }
212
213
214 //=================================================================================
215 // function : CalculateAndDisplayInertia()
216 // purpose  :
217 //=================================================================================
218 void MeasureGUI_InertiaDlg::CalculateAndDisplayInertia(const TopoDS_Shape& S)
219 {
220   GroupC1->LineEdit11->setText("");
221   GroupC1->LineEdit12->setText("");
222   GroupC1->LineEdit13->setText("");
223   GroupC1->LineEdit21->setText("");
224   GroupC1->LineEdit22->setText("");
225   GroupC1->LineEdit23->setText("");
226   GroupC1->LineEdit31->setText("");
227   GroupC1->LineEdit32->setText("");
228   GroupC1->LineEdit33->setText("");
229   GroupC1->LineEdit41->setText("");
230   GroupC1->LineEdit42->setText("");
231   GroupC1->LineEdit43->setText("");
232
233   if(S.IsNull()) 
234     return;
235
236   QString resString;
237   GProp_GProps System;
238
239   try {
240     if(S.ShapeType() == TopAbs_VERTEX || S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE)
241       BRepGProp::LinearProperties(S, System);
242     else if(S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SHELL)
243       BRepGProp::SurfaceProperties(S, System);
244     else
245       BRepGProp::VolumeProperties(S, System);
246
247     gp_Mat I = System.MatrixOfInertia() ;
248     GProp_PrincipalProps Pr = System.PrincipalProperties();
249     Standard_Real Ix,Iy,Iz;
250     Pr.Moments(Ix,Iy,Iz);
251     
252     /* matrix 3x3 */
253     resString = tr("%1").arg(I(1,1), 12, 'f', 6);
254     GroupC1->LineEdit11->setText(resString);
255     resString = tr("%1").arg(I(1,2), 12, 'f', 6);
256     GroupC1->LineEdit12->setText(resString);
257     resString = tr("%1").arg(I(1,3), 12, 'f', 6);
258     GroupC1->LineEdit13->setText(resString);
259
260     resString = tr("%1").arg(I(2,1), 12, 'f', 6);
261     GroupC1->LineEdit21->setText(resString);
262     resString = tr("%1").arg(I(2,2), 12, 'f', 6);
263     GroupC1->LineEdit22->setText(resString);
264     resString = tr("%1").arg(I(2,3), 12, 'f', 6);
265     GroupC1->LineEdit23->setText(resString);
266
267     resString = tr("%1").arg(I(3,1), 12, 'f', 6);
268     GroupC1->LineEdit31->setText(resString);
269     resString = tr("%1").arg(I(3,2), 12, 'f', 6);
270     GroupC1->LineEdit32->setText(resString);
271     resString = tr("%1").arg(I(3,3), 12, 'f', 6);
272     GroupC1->LineEdit33->setText(resString);
273
274     /* moments */
275     resString = tr("%1").arg(Ix, 12, 'f', 6);
276     GroupC1->LineEdit41->setText(resString);
277     resString = tr("%1").arg(Ix, 12, 'f', 6);
278     GroupC1->LineEdit42->setText(resString);
279     resString = tr("%1").arg(Iz, 12, 'f', 6);
280     GroupC1->LineEdit43->setText(resString);
281   }
282   catch(Standard_Failure) {
283     MESSAGE("Catch intercepted in CalculateAndDisplayInertia()");
284   }
285   return;
286 }