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