Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/geom.git] / src / MeasureGUI / MeasureGUI_DistanceDlg.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_DistanceDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "MeasureGUI_DistanceDlg.h"
30
31 #include "QAD_RightFrame.h"
32 #include "OCCViewer_Viewer3d.h"
33
34 #include <Geom_Plane.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <BRepExtrema_DistShapeShape.hxx>
37 #include <AIS_LengthDimension.hxx>
38 #include <BRepBuilderAPI_MakeEdge.hxx>
39 #include <BRepBuilderAPI_MakeVertex.hxx>
40 #include <AIS_ListIteratorOfListOfInteractive.hxx>
41 #include <gce_MakePln.hxx>
42 #include <Precision.hxx>
43
44 #include "utilities.h"
45
46 using namespace std;
47
48 //=================================================================================
49 // class    : MeasureGUI_DistanceDlg()
50 // purpose  : Constructs a MeasureGUI_DistanceDlg which is a child of 'parent', with the 
51 //            name 'name' and widget flags set to 'f'.
52 //            The dialog will by default be modeless, unless you set 'modal' to
53 //            TRUE to construct a modal dialog.
54 //=================================================================================
55 MeasureGUI_DistanceDlg::MeasureGUI_DistanceDlg(QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
56   :MeasureGUI_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
57 {
58   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_MINDIST")));
59   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
60
61   setCaption(tr("GEOM_MINDIST_TITLE"));
62
63   /***************************************************************/
64   GroupConstructors->setTitle(tr("GEOM_DISTANCE"));
65   RadioButton1->setPixmap(image0);
66
67   GroupC1 = new MeasureGUI_2Sel1LineEdit_QTD(this, "GroupC1");
68   GroupC1->GroupBox1->setTitle(tr("GEOM_MINDIST_OBJ"));
69   GroupC1->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
70   GroupC1->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
71   GroupC1->TextLabel3->setText(tr("GEOM_LENGTH"));
72   GroupC1->LineEdit3->setReadOnly(TRUE);
73   GroupC1->PushButton1->setPixmap(image1);
74   GroupC1->PushButton2->setPixmap(image1);
75
76   Layout1->addWidget(GroupC1, 1, 0);
77   /***************************************************************/
78
79   /* Initialisation */
80   Init();
81 }
82
83
84 //=================================================================================
85 // function : ~MeasureGUI_DistanceDlg()
86 // purpose  : Destroys the object and frees any allocated resources
87 //=================================================================================
88 MeasureGUI_DistanceDlg::~MeasureGUI_DistanceDlg()
89 {  
90   /* no need to delete child widgets, Qt does it all for us */
91 }
92
93
94 //=================================================================================
95 // function : Init()
96 // purpose  :
97 //=================================================================================
98 void MeasureGUI_DistanceDlg::Init()
99 {
100   /* init variables */
101   myEditCurrentArgument = GroupC1->LineEdit1;
102
103   myOkShape1 = myOkShape2 = false;
104
105    /* signals and slots connections */
106   connect(buttonClose, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
107   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
108   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
109
110   connect(GroupC1->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
111   connect(GroupC1->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
112
113   connect(GroupC1->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
114   connect(GroupC1->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
115
116   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
117
118   /* displays Dialog */
119   GroupC1->show();
120   this->show();
121
122   return;
123 }
124
125
126 //=================================================================================
127 // function : ClickOnCancel()
128 // purpose  :
129 //=================================================================================
130 void MeasureGUI_DistanceDlg::ClickOnCancel()
131 {
132   this->EraseDistance();
133   MeasureGUI_Skeleton::ClickOnCancel();
134   return;
135 }
136
137
138 //=================================================================================
139 // function : SelectionIntoArgument()
140 // purpose  : Called when selection has changed
141 //=================================================================================
142 void MeasureGUI_DistanceDlg::SelectionIntoArgument()
143 {
144   myGeomBase->EraseSimulationShape();
145   mySimulationTopoDs.Nullify();
146   this->EraseDistance();
147   myEditCurrentArgument->setText("");
148   QString aString = "";
149
150   GroupC1->LineEdit3->setText("");
151
152   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
153   if(nbSel != 1) {
154     if(myEditCurrentArgument == GroupC1->LineEdit1)
155       myOkShape1 = false;
156     else if(myEditCurrentArgument == GroupC1->LineEdit2)
157       myOkShape2 = false;
158     return;
159   }
160
161   /*  nbSel == 1  */
162   TopoDS_Shape S;
163   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
164     return;
165
166   Standard_Boolean testResult;
167   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject();
168   
169   if(myEditCurrentArgument == GroupC1->LineEdit1) {
170     myGeomShape1 = myGeomBase->ConvertIOinGEOMShape(IO, testResult);
171     if(!testResult)
172       return;
173     myShape1 = S;
174     myEditCurrentArgument->setText(aString);
175     myOkShape1 = true;
176   }    
177   else if(myEditCurrentArgument == GroupC1->LineEdit2) {
178     myGeomShape2 = myGeomBase->ConvertIOinGEOMShape(IO, testResult);
179     if(!testResult)
180       return;
181     myShape2 = S;
182     myEditCurrentArgument->setText(aString);
183     myOkShape2 = true;
184   }
185
186   if(myOkShape1 && myOkShape2)
187     this->MakeDistanceSimulationAndDisplay(myShape1 ,myShape2);
188   return; 
189 }
190
191
192 //=================================================================================
193 // function : SetEditCurrentArgument()
194 // purpose  :
195 //=================================================================================
196 void MeasureGUI_DistanceDlg::SetEditCurrentArgument()
197 {
198   QPushButton* send = (QPushButton*)sender();
199
200   if(send == GroupC1->PushButton1) {
201     GroupC1->LineEdit1->setFocus();
202     myEditCurrentArgument = GroupC1->LineEdit1;
203   }
204   else if(send == GroupC1->PushButton2) {
205     GroupC1->LineEdit2->setFocus();
206     myEditCurrentArgument = GroupC1->LineEdit2;
207   }
208
209   this->SelectionIntoArgument();
210   return;
211 }
212
213
214 //=================================================================================
215 // function : LineEditReturnPressed()
216 // purpose  :
217 //=================================================================================
218 void MeasureGUI_DistanceDlg::LineEditReturnPressed()
219 {
220   QLineEdit* send = (QLineEdit*)sender();
221   if(send == GroupC1->LineEdit1)
222     myEditCurrentArgument = GroupC1->LineEdit1;
223   else if(send == GroupC1->LineEdit2)
224     myEditCurrentArgument = GroupC1->LineEdit2;
225   else
226     return;
227
228   MeasureGUI_Skeleton::LineEditReturnPressed();
229   return;
230 }
231
232
233 //=================================================================================
234 // function : ActivateThisDialog()
235 // purpose  :
236 //=================================================================================
237 void MeasureGUI_DistanceDlg::ActivateThisDialog()
238 {
239   MeasureGUI_Skeleton::ActivateThisDialog();
240   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
241   if(!mySimulationTopoDs.IsNull())
242     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
243   return;
244 }
245
246
247 //=================================================================================
248 // function : enterEvent()
249 // purpose  : when mouse enter onto the QWidget
250 //=================================================================================
251 void MeasureGUI_DistanceDlg::enterEvent(QEvent* e)
252 {
253   if(GroupConstructors->isEnabled())
254     return;
255   this->ActivateThisDialog();
256   return;
257 }
258
259
260 //=================================================================================
261 // function : closeEvent()
262 // purpose  :
263 //=================================================================================
264 void MeasureGUI_DistanceDlg::closeEvent(QCloseEvent* e)
265 {
266   /* same than click on cancel button */
267   this->ClickOnCancel();
268   return;
269 }
270
271
272 //=================================================================================
273 // function : MakeDistanceSimulationAndDisplay()
274 // purpose  :
275 //=================================================================================
276 void MeasureGUI_DistanceDlg::MakeDistanceSimulationAndDisplay(const TopoDS_Shape& S1, const TopoDS_Shape& S2) 
277 {
278   myGeomBase->EraseSimulationShape();
279   mySimulationTopoDs.Nullify();
280   GroupC1->LineEdit3->setText("");
281   
282   BRepExtrema_DistShapeShape dst(S1, S2);
283
284   try {
285     if(dst.IsDone()) {
286       gp_Pnt P1, P2;
287       Standard_Real Dist = 1.e9;
288       for(int i = 1; i <= dst.NbSolution(); i++) {
289         P1 = (dst.PointOnShape1(i));
290         P2 = (dst.PointOnShape2(i));
291
292         Standard_Real MinDist = P1.Distance(P2);
293         if(Dist > MinDist)
294           Dist = MinDist;
295       }
296
297       if(Dist <= 1.e-9) {
298         BRepBuilderAPI_MakeVertex MakeVertex(P1);
299         mySimulationTopoDs = MakeVertex.Vertex();
300         myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
301
302         GroupC1->LineEdit3->setText("0.0");
303       } 
304       else {
305         BRepBuilderAPI_MakeEdge MakeEdge(P1, P2);
306         mySimulationTopoDs = MakeEdge.Edge();
307
308         TopoDS_Vertex V1 = BRepBuilderAPI_MakeVertex(P1);
309         TopoDS_Vertex V2 = BRepBuilderAPI_MakeVertex(P2);
310
311         QString S;
312         S.sprintf("%.1f", Dist);
313
314         gp_Pnt P3;
315         P3.SetX((P1.X() + P2.X()) / 2);
316         P3.SetY((P1.Y() + P2.Y()) / 2);
317         P3.SetZ(((P1.Z() + P2.Z()) / 2) + 100);
318         gp_Vec va(P3, P1);
319         gp_Vec vb(P3, P2);
320         if(va.IsParallel(vb, Precision::Angular())) {
321           P3.SetY(((P1.Y() + P2.Y()) / 2) + 100);
322           P3.SetZ(((P1.Z() + P2.Z()) / 2));
323         }
324         gce_MakePln gce_MP(P1, P2, P3);
325         gp_Pln gp_P = gce_MP.Value();
326         Handle(Geom_Plane) P = new Geom_Plane(gp_P);
327
328         char* aCopyS = CORBA::string_dup(S);
329         Handle(AIS_LengthDimension) Distance = new AIS_LengthDimension(V1, V2, P, Dist, TCollection_ExtendedString(aCopyS));
330         delete(aCopyS);
331
332         GroupC1->LineEdit3->setText(S);
333
334         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
335           OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
336           Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
337           ic->Display(Distance);
338           ic->UpdateCurrentViewer();
339         }
340       }
341     }
342   }
343   catch(Standard_Failure) {
344     MESSAGE("Catch intercepted in MakeDistanceSimulationAndDisplay()");
345   }
346   return;
347 }
348
349
350 //=================================================================================
351 // function : EraseDistance()
352 // purpose  :
353 //=================================================================================
354 void MeasureGUI_DistanceDlg::EraseDistance()
355 {
356   int count = QAD_Application::getDesktop()->getActiveStudy()->getStudyFramesCount();
357   for(int i = 0; i < count; i++) {
358     if (QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i)->getTypeView() == VIEW_OCC ) {
359       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i)->getRightFrame()->getViewFrame())->getViewer();
360       Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
361
362       AIS_ListOfInteractive L;
363       ic->DisplayedObjects(AIS_KOI_Relation, -1, L);
364       AIS_ListIteratorOfListOfInteractive ite(L);
365       while (ite.More()) {
366         ic->Remove(ite.Value());
367         ic->UpdateCurrentViewer();
368         ite.Next();
369       }
370     }
371   }
372   return;
373 }