Salome HOME
Issue 0021046: EDF 1610 GUI: To be able to change the width of the lines: small changes.
[modules/geom.git] / src / MeasureGUI / MeasureGUI_AngleDlg.cxx
1 // Copyright (C) 2007-2011  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.
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 // GEOM GEOMGUI : GUI for Geometry component
20 // File   : MeasureGUI_AngleDlg.cxx
21 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
22
23 #include "MeasureGUI_AngleDlg.h"
24 #include "MeasureGUI_Widgets.h"
25
26 #include <DlgRef.h>
27 #include <GEOMBase.h>
28 #include <GeometryGUI.h>
29
30 #include <Basics_OCCTVersion.hxx>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_ViewWindow.h>
37 #include <SUIT_ViewManager.h>
38 #include <SOCC_Prs.h>
39 #include <SOCC_ViewModel.h>
40 #include <SalomeApp_Tools.h>
41 #include <SalomeApp_Application.h>
42 #include <LightApp_SelectionMgr.h>
43
44 // OCCT Includes
45 #include <AIS_AngleDimension.hxx>
46 #include <AIS_ListIteratorOfListOfInteractive.hxx>
47 #include <BRep_Tool.hxx>
48 #include <BRepBuilderAPI_MakeEdge.hxx>
49 #include <BRepBuilderAPI_MakeVertex.hxx>
50 #include <TopExp.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <Geom_Plane.hxx>
54 #include <gce_MakePln.hxx>
55 #include <Precision.hxx>
56 #include <AIS.hxx>
57 #include <AIS_Drawer.hxx>
58 #include <Prs3d_AngleAspect.hxx>
59 #include <Prs3d_LineAspect.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("GEOM_MEASURE_ANGLE_IS"));
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 = "using_measurement_tools_page.html#angle_anchor";
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   myObj = myObj2 = GEOM::GEOM_Object::_nil();
137
138   mySelBtn   = myGrp->PushButton1;
139   mySelEdit  = myGrp->LineEdit1;
140   mySelBtn2  = myGrp->PushButton2;
141   mySelEdit2 = myGrp->LineEdit2;
142
143   myEditCurrentArgument = mySelEdit;
144
145   // signals and slots connections
146   connect(mySelEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
147   connect(mySelBtn2,  SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
148
149   globalSelection(GEOM_LINE);
150   MeasureGUI_Skeleton::Init();
151 }
152
153 //=================================================================================
154 // function : SelectionIntoArgument()
155 // purpose  : Called when selection has changed
156 //=================================================================================
157 void MeasureGUI_AngleDlg::SelectionIntoArgument()
158 {
159   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
160   SALOME_ListIO aSelList;
161   aSelMgr->selectedObjects(aSelList);
162
163   GEOM::GEOM_Object_var aSelectedObject = GEOM::GEOM_Object::_nil();
164
165   if (aSelList.Extent() > 0) {
166     aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
167   }
168
169   // clear selection
170   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
171   myGeomGUI->getApp()->selectionMgr()->clearSelected();
172   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
173           this, SLOT(SelectionIntoArgument()));
174
175   if (myEditCurrentArgument == mySelEdit) {
176     myObj = aSelectedObject;
177     if (!myObj->_is_nil() && myObj2->_is_nil())
178       myGrp->PushButton2->click();
179   }
180   else {
181     myObj2 = aSelectedObject;
182     if (!myObj2->_is_nil() && myObj->_is_nil())
183       myGrp->PushButton1->click();
184   }
185
186   processObject();
187 }
188
189 //=================================================================================
190 // function : processObject()
191 // purpose  : Fill dialogs fields in accordance with myObj and myObj2
192 //=================================================================================
193 void MeasureGUI_AngleDlg::processObject()
194 {
195   myGrp->LineEdit1->setText(!myObj->_is_nil()  ? GEOMBase::GetName(myObj ) : "");
196   myGrp->LineEdit2->setText(!myObj2->_is_nil() ? GEOMBase::GetName(myObj2) : "");
197
198   double anAngle = 0.;
199   if (getParameters(anAngle)) {
200     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
201     int aPrecision = resMgr->integerValue( "Geometry", "angle_precision", 6 );
202     myGrp->LineEdit3->setText(DlgRef::PrintDoubleValue(anAngle, aPrecision));
203     redisplayPreview();
204   }
205   else {
206     myGrp->LineEdit3->setText("");
207     erasePreview();
208   }
209 }
210
211 //=================================================================================
212 // function : getParameters()
213 // purpose  : Get angle between objects
214 //=================================================================================
215 bool MeasureGUI_AngleDlg::getParameters (double& theAngle)
216 {
217   QString msg;
218   if (isValid(msg)) {
219     GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
220     try {
221       theAngle = anOper->GetAngle(myObj, myObj2);
222     }
223     catch(const SALOME::SALOME_Exception& e) {
224       SalomeApp_Tools::QtCatchCorbaException(e);
225       return false;
226     }
227
228     bool isDone = anOper->IsDone();
229     if (!isDone) {
230       CORBA::String_var aMsg = anOper->GetErrorCode();
231       SUIT_MessageBox::warning(this,
232                                QObject::tr("WRN_WARNING"),
233                                QObject::tr(aMsg.in()));
234     }
235     return isDone;
236   }
237
238   return false;
239 }
240
241 //=================================================================================
242 // function : SetEditCurrentArgument()
243 // purpose  :
244 //=================================================================================
245 void MeasureGUI_AngleDlg::SetEditCurrentArgument()
246 {
247   QPushButton* send = (QPushButton*)sender();
248
249   if (send == myGrp->PushButton1) {
250     myEditCurrentArgument = myGrp->LineEdit1;
251
252     myGrp->PushButton2->setDown(false);
253     myGrp->LineEdit2->setEnabled(false);
254   }
255   else {
256     myEditCurrentArgument = myGrp->LineEdit2;
257
258     myGrp->PushButton1->setDown(false);
259     myGrp->LineEdit1->setEnabled(false);
260   }
261
262   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
263   globalSelection(GEOM_LINE);
264   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
265           this, SLOT(SelectionIntoArgument()));
266
267   // enable line edit
268   myEditCurrentArgument->setEnabled(true);
269   myEditCurrentArgument->setFocus();
270   // after setFocus(), because it will be setDown(false) when loses focus
271   send->setDown(true);
272
273   // seems we need it only to avoid preview disappearing, caused by selection mode change
274   redisplayPreview();
275 }
276
277 //=================================================================================
278 // function : LineEditReturnPressed()
279 // purpose  :
280 //=================================================================================
281 void MeasureGUI_AngleDlg::LineEditReturnPressed()
282 {
283   QLineEdit* send = (QLineEdit*)sender();
284
285   if (send == mySelEdit)
286     myEditCurrentArgument = mySelEdit;
287   else
288     myEditCurrentArgument = mySelEdit2;
289
290   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
291   SALOME_ListIO aSelList;
292   aSelMgr->selectedObjects(aSelList);
293
294   if (GEOMBase::SelectionByNameInDialogs(this, mySelEdit->text(), aSelList))
295     mySelEdit->setText(mySelEdit->text());
296 }
297
298 //=================================================================================
299 // function : buildPrs()
300 // purpose  :
301 //=================================================================================
302 SALOME_Prs* MeasureGUI_AngleDlg::buildPrs()
303 {
304   double anAngle = 0.;
305
306   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
307
308   if (myObj->_is_nil() || myObj2->_is_nil() || !getParameters(anAngle) ||
309        vw->getViewManager()->getType() != OCCViewer_Viewer::Type())
310     return 0;
311
312   if (anAngle > Precision::Angular()) {
313     try {
314 #if OCC_VERSION_LARGE > 0x06010000
315       OCC_CATCH_SIGNALS;
316 #endif
317       TopoDS_Shape S1, S2;
318       if (GEOMBase::GetShape(myObj , S1, TopAbs_EDGE) &&
319            GEOMBase::GetShape(myObj2, S2, TopAbs_EDGE)) {
320         TopoDS_Edge anEdge1 = TopoDS::Edge(S1);
321         TopoDS_Edge anEdge2 = TopoDS::Edge(S2);
322
323         // Build a plane for angle dimension presentation {P11, P12, P3}
324         TopoDS_Vertex V11, V12, V21, V22;
325         TopExp::Vertices(anEdge1, V11, V12);
326         TopExp::Vertices(anEdge2, V21, V22);
327
328         gp_Pnt aP11 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge1));
329         gp_Pnt aP12 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge1));
330
331         gp_Pnt aP21 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge2));
332         gp_Pnt aP22 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge2));
333
334         //        *P3
335         //         \
336         //          \
337         //           *P22
338         //          /
339         //         /
340         // P11    /         P12
341         // *-----/----------*
342         //  \   /
343         //   \ /
344         //    *P21
345         gp_Pnt aP3 (aP22.XYZ() + aP11.XYZ() - aP21.XYZ());
346
347         gce_MakePln gce_MP(aP11, aP12, aP3);
348         Handle(Geom_Plane) aPlane = new Geom_Plane(gce_MP.Value());
349
350         // Build the angle dimension presentation
351         QString aLabel;
352         aLabel.sprintf("%.1f", anAngle);
353
354         Handle(AIS_AngleDimension) anIO = new AIS_AngleDimension
355           (anEdge1, anEdge2, aPlane, anAngle * M_PI / 180.,
356            TCollection_ExtendedString((Standard_CString)aLabel.toLatin1().data()));
357         Handle(Geom_Line) geom_lin1,geom_lin2;
358         gp_Pnt ptat11,ptat12,ptat21,ptat22;
359         Standard_Boolean isInfinite1,isInfinite2;
360         Handle(Geom_Curve) extCurv;
361         Standard_Integer extShape;
362         if (AIS::ComputeGeometry(anEdge1,
363                                   anEdge2,
364                                   extShape,
365                                   geom_lin1,
366                                   geom_lin2,
367                                   ptat11,
368                                   ptat12,
369                                   ptat21,
370                                   ptat22,
371                                   extCurv,
372                                   isInfinite1,
373                                   isInfinite2,
374                                   aPlane)) {
375           Standard_Real arrSize1 = anIO->ArrowSize();
376           Standard_Real arrSize2 = anIO->ArrowSize();
377           if (!isInfinite1) arrSize1 = ptat11.Distance(ptat12)/10.;
378           if (!isInfinite2) arrSize2 = ptat21.Distance(ptat22)/10.;
379           Standard_Real arrowSize = Max(arrSize1,arrSize2);
380           anIO->SetArrowSize(arrowSize);
381         }
382
383         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
384         int w = resMgr->integerValue("Geometry", "measures_line_width", 1);
385   Handle(Prs3d_AngleAspect) asp = new Prs3d_AngleAspect();
386   asp->LineAspect()->SetWidth(w);
387         anIO->Attributes()->SetAngleAspect(asp);
388
389         SOCC_Prs* aPrs =
390           dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
391
392         if (aPrs)
393           aPrs->AddObject(anIO);
394
395         return aPrs;
396       }
397     }
398     catch(Standard_Failure) {
399     }
400   }
401
402   return 0;
403 }
404
405 //=================================================================================
406 // function : isValid()
407 // purpose  :
408 //=================================================================================
409 bool MeasureGUI_AngleDlg::isValid (QString& msg)
410 {
411   return MeasureGUI_Skeleton::isValid(msg) && !myObj2->_is_nil();
412 }