Salome HOME
DCQ : New Architecture
[modules/geom.git] / src / MeasureGUI / MeasureGUI_MaxToleranceDlg.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_MaxToleranceDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "MeasureGUI_MaxToleranceDlg.h"
31
32 #include <TopoDS_Vertex.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopExp_Explorer.hxx>
36 #include <BRep_Tool.hxx>
37
38 //=================================================================================
39 // class    : MeasureGUI_MaxToleranceDlg()
40 // purpose  : Constructs a MeasureGUI_MaxToleranceDlg which is a child of 'parent', with the 
41 //            name 'name' and widget flags set to 'f'.
42 //            The dialog will by default be modeless, unless you set 'modal' to
43 //            TRUE to construct a modal dialog.
44 //=================================================================================
45 MeasureGUI_MaxToleranceDlg::MeasureGUI_MaxToleranceDlg(QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
46   :MeasureGUI_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
47 {
48   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_TOLERANCE")));
49   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
50
51   setCaption(tr("GEOM_TOLERANCE_TITLE"));
52
53   /***************************************************************/
54   GroupConstructors->setTitle(tr("GEOM_TOLERANCE"));
55   RadioButton1->setPixmap(image0);
56
57   GroupC1 = new MeasureGUI_1Sel6LineEdit_QTD(this, "GroupC1");
58   GroupC1->GroupBox1->setTitle(tr("GEOM_TOLERANCE_CONSTR"));
59   GroupC1->TextLabel1->setText(tr("GEOM_OBJECT"));
60   GroupC1->TextLabel2->setText(tr("GEOM_MIN"));
61   GroupC1->TextLabel3->setText(tr("GEOM_MAX"));
62   GroupC1->TextLabel4->setText(tr("GEOM_TOLERANCE_FACE"));
63   GroupC1->TextLabel5->setText(tr("GEOM_TOLERANCE_EDGE"));
64   GroupC1->TextLabel6->setText(tr("GEOM_TOLERANCE_VERTEX"));
65   GroupC1->LineEdit11->setReadOnly(TRUE);
66   GroupC1->LineEdit12->setReadOnly(TRUE);
67   GroupC1->LineEdit21->setReadOnly(TRUE);
68   GroupC1->LineEdit22->setReadOnly(TRUE);
69   GroupC1->LineEdit31->setReadOnly(TRUE);
70   GroupC1->LineEdit32->setReadOnly(TRUE);
71   GroupC1->PushButton1->setPixmap(image1);
72
73   Layout1->addWidget(GroupC1, 1, 0);
74   /***************************************************************/
75
76   /* Initialisation */
77   Init();
78 }
79
80
81 //=================================================================================
82 // function : ~MeasureGUI_MaxToleranceDlg()
83 // purpose  : Destroys the object and frees any allocated resources
84 //=================================================================================
85 MeasureGUI_MaxToleranceDlg::~MeasureGUI_MaxToleranceDlg()
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_MaxToleranceDlg::Init()
96 {
97   /* init variables */
98   myEditCurrentArgument = GroupC1->LineEdit1;
99
100    /* signals and slots connections */
101   connect(GroupC1->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
102   connect(GroupC1->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
103
104   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
105
106   /* displays Dialog */
107   GroupC1->show();
108   this->show();
109
110   return;
111 }
112
113
114 //=================================================================================
115 // function : SelectionIntoArgument()
116 // purpose  : Called when selection as changed or other case
117 //=================================================================================
118 void MeasureGUI_MaxToleranceDlg::SelectionIntoArgument()
119 {
120   myEditCurrentArgument->setText("");
121   QString aString = "";
122
123   GroupC1->LineEdit11->setText("");
124   GroupC1->LineEdit12->setText("");
125   GroupC1->LineEdit21->setText("");
126   GroupC1->LineEdit22->setText("");
127   GroupC1->LineEdit31->setText("");
128   GroupC1->LineEdit32->setText("");
129
130   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
131   if(nbSel != 1)
132     return;
133
134   /*  nbSel == 1  */
135   TopoDS_Shape S;
136   if(!myGeomBase->GetTopoFromSelection(mySelection, S) || S.IsNull())
137     return;
138
139   if(S.IsNull())
140     return;
141
142   GroupC1->LineEdit1->setText(aString);
143
144   this->CalculateMaxTolerance(S);
145   return;
146 }
147
148
149 //=================================================================================
150 // function : SetEditCurrentArgument()
151 // purpose  :
152 //=================================================================================
153 void MeasureGUI_MaxToleranceDlg::SetEditCurrentArgument()
154 {
155   QPushButton* send = (QPushButton*)sender();
156
157   if(send == GroupC1->PushButton1) {
158     GroupC1->LineEdit1->setFocus();
159     myEditCurrentArgument = GroupC1->LineEdit1;
160   }
161
162   this->SelectionIntoArgument();
163   return;
164 }
165
166
167 //=================================================================================
168 // function : LineEditReturnPressed()
169 // purpose  :
170 //=================================================================================
171 void MeasureGUI_MaxToleranceDlg::LineEditReturnPressed()
172 {
173   QLineEdit* send = (QLineEdit*)sender();
174   if(send == GroupC1->LineEdit1)
175     myEditCurrentArgument = GroupC1->LineEdit1;
176   else
177     return;
178
179   MeasureGUI_Skeleton::LineEditReturnPressed();
180   return;
181 }
182
183
184 //=================================================================================
185 // function : ActivateThisDialog()
186 // purpose  :
187 //=================================================================================
188 void MeasureGUI_MaxToleranceDlg::ActivateThisDialog()
189 {
190   MeasureGUI_Skeleton::ActivateThisDialog();
191   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
192   return;
193 }
194
195
196 //=================================================================================
197 // function : enterEvent()
198 // purpose  :
199 //=================================================================================
200 void MeasureGUI_MaxToleranceDlg::enterEvent(QEvent* e)
201 {
202   if(GroupConstructors->isEnabled())
203     return;
204   this->ActivateThisDialog();
205   return;
206 }
207
208
209 //=================================================================================
210 // function : CalculateMaxTolerance()
211 // purpose  :
212 //=================================================================================
213 void MeasureGUI_MaxToleranceDlg::CalculateMaxTolerance(const TopoDS_Shape& S)
214 {
215   GroupC1->LineEdit11->setText("");
216   GroupC1->LineEdit12->setText("");
217   GroupC1->LineEdit21->setText("");
218   GroupC1->LineEdit22->setText("");
219   GroupC1->LineEdit31->setText("");
220   GroupC1->LineEdit32->setText("");
221
222   if(S.IsNull()) 
223     return;
224
225   Standard_Real T, TMF, TME, TMV, TmF, TmE, TmV;
226   Standard_Integer nbF, nbE, nbV;
227   bool m_isFace, m_isEdge, m_isVertex;
228
229   TMF = TME = TMV = -RealLast();
230   TmF = TmE = TmV = RealLast();
231   nbF = nbE = nbV = 0;
232   m_isFace = m_isEdge = m_isVertex = false;
233
234   try {
235     for(TopExp_Explorer ExF(S,TopAbs_FACE); ExF.More(); ExF.Next()) { 
236       m_isFace = true;
237       TopoDS_Face Face=TopoDS::Face(ExF.Current());
238       T=BRep_Tool::Tolerance(Face);
239       if(T>TMF)
240         TMF=T;
241       if(T<TmF)
242         TmF=T;
243       nbF++;
244     }
245     for(TopExp_Explorer ExE(S,TopAbs_EDGE); ExE.More(); ExE.Next()) { 
246       m_isEdge = true;
247       TopoDS_Edge Edge=TopoDS::Edge(ExE.Current());
248       T=BRep_Tool::Tolerance(Edge);
249       if(T>TME)
250         TME=T;
251       if(T<TmE)
252         TmE=T;
253       nbE++;
254     }
255     for(TopExp_Explorer ExV(S,TopAbs_VERTEX); ExV.More(); ExV.Next()) { 
256       m_isVertex = true;
257       TopoDS_Vertex Vertex=TopoDS::Vertex(ExV.Current());
258       T=BRep_Tool::Tolerance(Vertex);
259       if(T>TMV)
260         TMV=T;
261       if(T<TmV)
262         TmV=T;
263       nbV++;
264     }
265
266     if(m_isFace) {
267       GroupC1->LineEdit11->setText(tr("%1").arg(TmF, 5, 'e', 8));
268       GroupC1->LineEdit12->setText(tr("%1").arg(TMF, 5, 'e', 8));
269     }
270     if(m_isEdge) {
271       GroupC1->LineEdit21->setText(tr("%1").arg(TmE, 5, 'e', 8));
272       GroupC1->LineEdit22->setText(tr("%1").arg(TME, 5, 'e', 8));
273       }
274     if(m_isVertex) {
275       GroupC1->LineEdit31->setText(tr("%1").arg(TmV, 5, 'e', 8));
276       GroupC1->LineEdit32->setText(tr("%1").arg(TMV, 5, 'e', 8));
277     }
278   }
279   catch(Standard_Failure) 
280     {
281       MESSAGE("Catch intercepted in CalculateMaxTolerance()");
282     }
283   return;
284 }