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