Salome HOME
0023505: Sigsegv with fuse on cylinder and cone
[modules/geom.git] / src / MeasureGUI / MeasureGUI_AngleDlg.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // GEOM GEOMGUI : GUI for Geometry component
21 // File   : MeasureGUI_AngleDlg.cxx
22 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
23
24 #include "MeasureGUI_AngleDlg.h"
25 #include "MeasureGUI_Widgets.h"
26
27 #include <DlgRef.h>
28 #include <GEOMBase.h>
29 #include <GeometryGUI.h>
30
31 #include <Basics_OCCTVersion.hxx>
32
33 #include <SUIT_Session.h>
34 #include <SUIT_Desktop.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_ViewWindow.h>
38 #include <SUIT_ViewManager.h>
39 #include <SOCC_Prs.h>
40 #include <SOCC_ViewModel.h>
41 #include <SalomeApp_Tools.h>
42 #include <SalomeApp_Application.h>
43 #include <LightApp_SelectionMgr.h>
44
45 // OCCT Includes
46 #include <AIS_AngleDimension.hxx>
47 #include <AIS_ListIteratorOfListOfInteractive.hxx>
48 #include <BRep_Tool.hxx>
49 #include <BRepBuilderAPI_MakeEdge.hxx>
50 #include <BRepBuilderAPI_MakeVertex.hxx>
51 #include <TopExp.hxx>
52 #include <TopoDS.hxx>
53 #include <TopoDS_Edge.hxx>
54 #include <Geom_Plane.hxx>
55 #include <gce_MakePln.hxx>
56 #include <Precision.hxx>
57 #include <AIS.hxx>
58 #include <Prs3d_LineAspect.hxx> 
59 #include <IntTools_EdgeEdge.hxx>
60 #include <IntTools_SequenceOfCommonPrts.hxx>
61 #include <IntTools_CommonPrt.hxx>
62
63 // QT Includes
64 #include <qlineedit.h>
65 #include <qlabel.h>
66 #include <qlayout.h>
67 #include <qpushbutton.h>
68 #include <qradiobutton.h>
69 #include <qbuttongroup.h>
70
71 #include "GEOMImpl_Types.hxx"
72
73 #include "utilities.h"
74
75 #include <Standard_Failure.hxx>
76 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
77
78 //=================================================================================
79 // class    : MeasureGUI_AngleDlg()
80 // purpose  : Constructs a MeasureGUI_AngleDlg which is a child of 'parent', with the
81 //            name 'name' and widget flags set to 'f'.
82 //            The dialog will by default be modeless, unless you set 'modal' to
83 //            TRUE to construct a modal dialog.
84 //=================================================================================
85 MeasureGUI_AngleDlg::MeasureGUI_AngleDlg (GeometryGUI* GUI, QWidget* parent)
86   : MeasureGUI_Skeleton(GUI, parent)
87 {
88   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
89   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_ANGLE")));
90   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
91
92   setWindowTitle(tr("GEOM_MEASURE_ANGLE_TITLE"));
93
94   /***************************************************************/
95   mainFrame()->GroupConstructors->setTitle(tr("GEOM_MEASURE_ANGLE_ANGLE"));
96   mainFrame()->RadioButton1->setIcon(image0);
97
98   myGrp = new MeasureGUI_2Sel1LineEdit(centralWidget());
99   myGrp->GroupBox1->setTitle(tr("GEOM_MEASURE_ANGLE_OBJ"));
100   myGrp->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
101   myGrp->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
102   myGrp->TextLabel3->setText(tr(aResMgr->stringValue ( "Geometry", "dimensions_angle_units", "deg" ) == "deg" ? "GEOM_MEASURE_ANGLE_DEG" : "GEOM_MEASURE_ANGLE_RAD"));
103   myGrp->LineEdit3->setReadOnly(true);
104   myGrp->PushButton1->setIcon(image1);
105   myGrp->PushButton2->setIcon(image1);
106   myGrp->LineEdit1->setReadOnly(true);
107   myGrp->LineEdit2->setReadOnly(true);
108
109   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
110   layout->setMargin(0); layout->setSpacing(6);
111   layout->addWidget(myGrp);
112   /***************************************************************/
113
114   // Help page reference
115   myHelpFileName = "angle_page.html";
116
117   // Initialisation
118   Init();
119 }
120
121 //=================================================================================
122 // function : ~MeasureGUI_AngleDlg()
123 // purpose  : Destroys the object and frees any allocated resources
124 //=================================================================================
125 MeasureGUI_AngleDlg::~MeasureGUI_AngleDlg()
126 {
127 }
128
129 //=================================================================================
130 // function : Init()
131 // purpose  :
132 //=================================================================================
133 void MeasureGUI_AngleDlg::Init()
134 {
135   // init variables
136   myGrp->LineEdit1->setText("");
137   myGrp->LineEdit2->setText("");
138   myGrp->LineEdit1->setEnabled(true);
139   myGrp->LineEdit2->setEnabled(false);
140   myObj.nullify();
141   myObj2.nullify();
142
143   mySelBtn   = myGrp->PushButton1;
144   mySelEdit  = myGrp->LineEdit1;
145   mySelBtn2  = myGrp->PushButton2;
146   mySelEdit2 = myGrp->LineEdit2;
147
148   myEditCurrentArgument = mySelEdit;
149
150   // signals and slots connections
151   connect(mySelEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
152   connect(mySelBtn2,  SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
153
154   activateSelection();  
155   MeasureGUI_Skeleton::Init();
156 }
157
158 //=================================================================================
159 // function : SelectionIntoArgument()
160 // purpose  : Called when selection has changed
161 //=================================================================================
162 void MeasureGUI_AngleDlg::SelectionIntoArgument()
163 {
164   GEOM::GeomObjPtr aSelectedObject = getSelected( TopAbs_EDGE );
165
166   // clear selection
167   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
168   myGeomGUI->getApp()->selectionMgr()->clearSelected();
169   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
170           this, SLOT(SelectionIntoArgument()));
171
172   if (myEditCurrentArgument == mySelEdit) {
173     myObj = aSelectedObject;
174     if (myObj && !myObj2)
175       myGrp->PushButton2->click();
176   }
177   else {
178     myObj2 = aSelectedObject;
179     if (myObj2 && !myObj)
180       myGrp->PushButton1->click();
181   }
182
183   processObject();
184 }
185
186 //=================================================================================
187 // function : processObject()
188 // purpose  : Fill dialogs fields in accordance with myObj and myObj2
189 //=================================================================================
190 void MeasureGUI_AngleDlg::processObject()
191 {
192   myGrp->LineEdit1->setText(myObj  ? GEOMBase::GetName(myObj.get()) : "");
193   myGrp->LineEdit2->setText(myObj2 ? GEOMBase::GetName(myObj2.get()) : "");
194
195   double anAngle = 0.;
196   if (getParameters(anAngle)) {
197     // To avoid the using different angle values in viewer and dialog,
198     // the value from presentation is used in both cases (see buildPrs())
199     redisplayPreview();
200   }
201   else {
202     myGrp->LineEdit3->setText("");
203     erasePreview();
204   }
205 }
206
207 //=================================================================================
208 // function : getParameters()
209 // purpose  : Get angle between objects
210 //=================================================================================
211 bool MeasureGUI_AngleDlg::getParameters (double& theAngle)
212 {
213   QString msg;
214   if (isValid(msg)) {
215     GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
216     try {
217       theAngle = anOper->GetAngle(myObj.get(), myObj2.get());
218     }
219     catch(const SALOME::SALOME_Exception& e) {
220       SalomeApp_Tools::QtCatchCorbaException(e);
221       return false;
222     }
223
224     bool isDone = anOper->IsDone();
225     if (!isDone) {
226       CORBA::String_var aMsg = anOper->GetErrorCode();
227       SUIT_MessageBox::warning(this,
228                                QObject::tr("WRN_WARNING"),
229                                QObject::tr(aMsg.in()));
230     }
231     return isDone;
232   }
233
234   return false;
235 }
236
237 //=================================================================================
238 // function : SetEditCurrentArgument()
239 // purpose  :
240 //=================================================================================
241 void MeasureGUI_AngleDlg::SetEditCurrentArgument()
242 {
243   QPushButton* send = (QPushButton*)sender();
244
245   if (send == myGrp->PushButton1) {
246     myEditCurrentArgument = myGrp->LineEdit1;
247
248     myGrp->PushButton2->setDown(false);
249     myGrp->LineEdit2->setEnabled(false);
250   }
251   else {
252     myEditCurrentArgument = myGrp->LineEdit2;
253
254     myGrp->PushButton1->setDown(false);
255     myGrp->LineEdit1->setEnabled(false);
256   }
257
258   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
259   activateSelection();
260   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
261           this, SLOT(SelectionIntoArgument()));
262
263   // enable line edit
264   myEditCurrentArgument->setEnabled(true);
265   myEditCurrentArgument->setFocus();
266   // after setFocus(), because it will be setDown(false) when loses focus
267   send->setDown(true);
268
269   // seems we need it only to avoid preview disappearing, caused by selection mode change
270   redisplayPreview();
271 }
272
273 //=================================================================================
274 // function : LineEditReturnPressed()
275 // purpose  :
276 //=================================================================================
277 void MeasureGUI_AngleDlg::LineEditReturnPressed()
278 {
279   QLineEdit* send = (QLineEdit*)sender();
280
281   if (send == mySelEdit)
282     myEditCurrentArgument = mySelEdit;
283   else
284     myEditCurrentArgument = mySelEdit2;
285
286   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
287   SALOME_ListIO aSelList;
288   aSelMgr->selectedObjects(aSelList);
289
290   if (GEOMBase::SelectionByNameInDialogs(this, mySelEdit->text(), aSelList))
291     mySelEdit->setText(mySelEdit->text());
292 }
293
294 //=================================================================================
295 // function : buildPrs()
296 // purpose  :
297 //=================================================================================
298 SALOME_Prs* MeasureGUI_AngleDlg::buildPrs()
299 {
300   double anAngle = 0.;
301   myGrp->LineEdit3->setText("");
302
303   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
304
305   if (!myObj || !myObj2 || !getParameters(anAngle) ||
306        vw->getViewManager()->getType() != OCCViewer_Viewer::Type())
307     return 0;
308
309   if (anAngle > Precision::Angular()) {
310     try {
311       OCC_CATCH_SIGNALS;
312       TopoDS_Shape S1, S2;
313       if (GEOMBase::GetShape(myObj.get() , S1, TopAbs_EDGE) &&
314         GEOMBase::GetShape(myObj2.get(), S2, TopAbs_EDGE)) {
315         TopoDS_Edge anEdge1 = TopoDS::Edge(S1);
316         TopoDS_Edge anEdge2 = TopoDS::Edge(S2);
317
318         Handle(AIS_AngleDimension) anIO = new AIS_AngleDimension( anEdge1, anEdge2 );
319
320         Handle(Prs3d_DimensionAspect) aDimensionStyle = new Prs3d_DimensionAspect;
321
322         Handle(Geom_Line) geom_lin1,geom_lin2;
323         gp_Pnt ptat11,ptat12,ptat21,ptat22;
324         Standard_Boolean isInfinite1,isInfinite2;
325         Handle(Geom_Curve) extCurv;
326         Standard_Integer extShape;
327         Handle(Geom_Plane) aPlane;
328         if (AIS::ComputeGeometry(anEdge1,
329                                   anEdge2,
330                                   extShape,
331                                   geom_lin1,
332                                   geom_lin2,
333                                   ptat11,
334                                   ptat12,
335                                   ptat21,
336                                   ptat22,
337                                   extCurv,
338                                   isInfinite1,
339                                   isInfinite2,
340                                   aPlane)) {
341           Standard_Real arrSize1 = aDimensionStyle->ArrowAspect()->Length();
342           Standard_Real arrSize2 = aDimensionStyle->ArrowAspect()->Length();
343           if (!isInfinite1) arrSize1 = ptat11.Distance(ptat12)/10.;
344           if (!isInfinite2) arrSize2 = ptat21.Distance(ptat22)/10.;
345           aDimensionStyle->ArrowAspect()->SetLength( Max(arrSize1, arrSize2) );
346         }
347
348         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
349         int w = resMgr->integerValue( "Geometry", "measures_line_width", 1 );
350         QString aUnitsAngle = resMgr->stringValue ( "Geometry", "dimensions_angle_units", "deg" );
351
352         aDimensionStyle->LineAspect()->SetWidth( w );
353         aDimensionStyle->SetTextHorizontalPosition( Prs3d_DTHP_Center );
354         aDimensionStyle->SetTextVerticalPosition( Prs3d_DTVP_Center );
355         aDimensionStyle->MakeText3d( Standard_False );
356         aDimensionStyle->MakeArrows3d( Standard_True );
357
358         anIO->SetDimensionAspect( aDimensionStyle );
359         anIO->SetDisplayUnits( aUnitsAngle.toLatin1().data() );
360         if (aUnitsAngle == "rad")
361           anIO->SetDisplaySpecialSymbol(AIS_DSS_No);
362
363         SOCC_Prs* aPrs =
364           dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
365
366         if (aPrs)
367           aPrs->AddObject(anIO);
368
369         // set angle value into dialog
370         double anAngle = anIO->GetValue();
371         QString anAngleLabel = "GEOM_MEASURE_ANGLE_RAD";
372         if (aUnitsAngle == "deg") {
373           // using degrees instead of radians
374           anAngle *= 180. / M_PI;
375           anAngleLabel = "GEOM_MEASURE_ANGLE_DEG";
376         }
377         myGrp->TextLabel3->setText(tr(anAngleLabel.toLatin1().data()));
378         int aPrecision = resMgr->integerValue( "Geometry", "angle_precision", 6 );
379         myGrp->LineEdit3->setText(DlgRef::PrintDoubleValue(anAngle, aPrecision));
380
381         return aPrs;
382       }
383     }
384     catch(Standard_Failure) {
385     }
386   }
387
388   return 0;
389 }
390
391 //=================================================================================
392 // function : isValid()
393 // purpose  :
394 //=================================================================================
395 bool MeasureGUI_AngleDlg::isValid (QString& msg)
396 {
397   return MeasureGUI_Skeleton::isValid(msg) && myObj2;
398 }
399
400 //=================================================================================
401 // function : activateSelection()
402 // purpose  :
403 //=================================================================================
404 void MeasureGUI_AngleDlg::activateSelection()
405 {
406   globalSelection(GEOM_LINE);
407   localSelection( TopAbs_EDGE );
408 }