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