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