Salome HOME
0020230: Memory performance improvement.
[modules/geom.git] / src / MeasureGUI / MeasureGUI_AngleDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // GEOM GEOMGUI : GUI for Geometry component
23 //  File   : MeasureGUI_AngleDlg.cxx
24 //  Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
25 //
26 #include "MeasureGUI_AngleDlg.h"
27 #include "MeasureGUI_Widgets.h"
28
29 #include <DlgRef.h>
30 #include <GEOMBase.h>
31 #include <GeometryGUI.h>
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
59 // QT Includes
60 #include <qlineedit.h>
61 #include <qlabel.h>
62 #include <qlayout.h>
63 #include <qpushbutton.h>
64 #include <qradiobutton.h>
65 #include <qbuttongroup.h>
66
67 #include "GEOMImpl_Types.hxx"
68
69 #include "utilities.h"
70
71 #include <Standard_Failure.hxx>
72 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
73
74 //=================================================================================
75 // class    : MeasureGUI_AngleDlg()
76 // purpose  : Constructs a MeasureGUI_AngleDlg which is a child of 'parent', with the
77 //            name 'name' and widget flags set to 'f'.
78 //            The dialog will by default be modeless, unless you set 'modal' to
79 //            TRUE to construct a modal dialog.
80 //=================================================================================
81 MeasureGUI_AngleDlg::MeasureGUI_AngleDlg (GeometryGUI* GUI, QWidget* parent)
82   : MeasureGUI_Skeleton(GUI, parent)
83 {
84   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
85   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_ANGLE")));
86   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
87
88   setWindowTitle(tr("GEOM_MEASURE_ANGLE_TITLE"));
89
90   /***************************************************************/
91   mainFrame()->GroupConstructors->setTitle(tr("GEOM_MEASURE_ANGLE_ANGLE"));
92   mainFrame()->RadioButton1->setIcon(image0);
93
94   myGrp = new MeasureGUI_2Sel1LineEdit(centralWidget());
95   myGrp->GroupBox1->setTitle(tr("GEOM_MEASURE_ANGLE_OBJ"));
96   myGrp->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
97   myGrp->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
98   myGrp->TextLabel3->setText(tr("GEOM_MEASURE_ANGLE_IS"));
99   myGrp->LineEdit3->setReadOnly(true);
100   myGrp->PushButton1->setIcon(image1);
101   myGrp->PushButton2->setIcon(image1);
102   myGrp->LineEdit1->setReadOnly(true);
103   myGrp->LineEdit2->setReadOnly(true);
104
105   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
106   layout->setMargin(0); layout->setSpacing(6);
107   layout->addWidget(myGrp);
108   /***************************************************************/
109
110   // Help page reference
111   myHelpFileName = "using_measurement_tools_page.html#angle_anchor";
112
113   // Initialisation
114   Init();
115 }
116
117 //=================================================================================
118 // function : ~MeasureGUI_AngleDlg()
119 // purpose  : Destroys the object and frees any allocated resources
120 //=================================================================================
121 MeasureGUI_AngleDlg::~MeasureGUI_AngleDlg()
122 {
123 }
124
125 //=================================================================================
126 // function : Init()
127 // purpose  :
128 //=================================================================================
129 void MeasureGUI_AngleDlg::Init()
130 {
131   // init variables
132   myGrp->LineEdit1->setText("");
133   myGrp->LineEdit2->setText("");
134   myObj = myObj2 = GEOM::GEOM_Object::_nil();
135
136   mySelBtn   = myGrp->PushButton1;
137   mySelEdit  = myGrp->LineEdit1;
138   mySelBtn2  = myGrp->PushButton2;
139   mySelEdit2 = myGrp->LineEdit2;
140
141   myEditCurrentArgument = mySelEdit;
142
143   // signals and slots connections
144   connect(mySelEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
145   connect(mySelBtn2,  SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
146
147   globalSelection(GEOM_LINE);
148   MeasureGUI_Skeleton::Init();
149 }
150
151 //=================================================================================
152 // function : SelectionIntoArgument()
153 // purpose  : Called when selection has changed
154 //=================================================================================
155 void MeasureGUI_AngleDlg::SelectionIntoArgument()
156 {
157   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
158   SALOME_ListIO aSelList;
159   aSelMgr->selectedObjects(aSelList);
160
161   GEOM::GEOM_Object_var aSelectedObject = GEOM::GEOM_Object::_nil();
162
163   if (aSelList.Extent() > 0) {
164     Standard_Boolean testResult = Standard_False;
165     aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
166     if (!testResult)
167       aSelectedObject = GEOM::GEOM_Object::_nil();
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     myGrp->LineEdit3->setText(DlgRef::PrintDoubleValue(anAngle));
202     redisplayPreview();
203   }
204   else {
205     myGrp->LineEdit3->setText("");
206     erasePreview();
207   }
208 }
209
210 //=================================================================================
211 // function : getParameters()
212 // purpose  : Get angle between objects
213 //=================================================================================
214 bool MeasureGUI_AngleDlg::getParameters (double& theAngle)
215 {
216   QString msg;
217   if (isValid(msg)) {
218     try {
219       theAngle = GEOM::GEOM_IMeasureOperations::_narrow(getOperation())->GetAngle(myObj, myObj2);
220     }
221     catch(const SALOME::SALOME_Exception& e) {
222       SalomeApp_Tools::QtCatchCorbaException(e);
223       return false;
224     }
225
226     bool isDone = getOperation()->IsDone();
227     if (!isDone) {
228       CORBA::String_var aMsg = getOperation()->GetErrorCode();
229       SUIT_MessageBox::warning(this,
230                                QObject::tr("WRN_WARNING"),
231                                QObject::tr(aMsg.in()));
232     }
233     return isDone;
234   }
235
236   return false;
237 }
238
239 //=================================================================================
240 // function : SetEditCurrentArgument()
241 // purpose  :
242 //=================================================================================
243 void MeasureGUI_AngleDlg::SetEditCurrentArgument()
244 {
245   QPushButton* send = (QPushButton*)sender();
246
247   if (send == myGrp->PushButton1) {
248     myEditCurrentArgument = myGrp->LineEdit1;
249
250     myGrp->PushButton2->setDown(false);
251     myGrp->LineEdit2->setEnabled(false);
252   }
253   else {
254     myEditCurrentArgument = myGrp->LineEdit2;
255
256     myGrp->PushButton1->setDown(false);
257     myGrp->LineEdit1->setEnabled(false);
258   }
259
260   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
261   globalSelection(GEOM_LINE);
262   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
263           this, SLOT(SelectionIntoArgument()));
264
265   // enable line edit
266   myEditCurrentArgument->setEnabled(true);
267   myEditCurrentArgument->setFocus();
268   // after setFocus(), because it will be setDown(false) when loses focus
269   send->setDown(true);
270
271   // seems we need it only to avoid preview disappearing, caused by selection mode change
272   redisplayPreview();
273 }
274
275 //=================================================================================
276 // function : LineEditReturnPressed()
277 // purpose  :
278 //=================================================================================
279 void MeasureGUI_AngleDlg::LineEditReturnPressed()
280 {
281   QLineEdit* send = (QLineEdit*)sender();
282
283   if (send == mySelEdit)
284     myEditCurrentArgument = mySelEdit;
285   else
286     myEditCurrentArgument = mySelEdit2;
287
288   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
289   SALOME_ListIO aSelList;
290   aSelMgr->selectedObjects(aSelList);
291
292   if (GEOMBase::SelectionByNameInDialogs(this, mySelEdit->text(), aSelList))
293     mySelEdit->setText(mySelEdit->text());
294 }
295
296 //=================================================================================
297 // function : buildPrs()
298 // purpose  :
299 //=================================================================================
300 SALOME_Prs* MeasureGUI_AngleDlg::buildPrs()
301 {
302   double anAngle = 0.;
303
304   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
305
306   if (myObj->_is_nil() || myObj2->_is_nil() || !getParameters(anAngle) ||
307        vw->getViewManager()->getType() != OCCViewer_Viewer::Type())
308     return 0;
309
310   if (anAngle > Precision::Angular()) {
311     try {
312 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
313       OCC_CATCH_SIGNALS;
314 #endif
315       TopoDS_Shape S1, S2;
316       if (GEOMBase::GetShape(myObj , S1, TopAbs_EDGE) &&
317            GEOMBase::GetShape(myObj2, S2, TopAbs_EDGE)) {
318         TopoDS_Edge anEdge1 = TopoDS::Edge(S1);
319         TopoDS_Edge anEdge2 = TopoDS::Edge(S2);
320
321         // Build a plane for angle dimension presentation {P11, P12, P3}
322         TopoDS_Vertex V11, V12, V21, V22;
323         TopExp::Vertices(anEdge1, V11, V12);
324         TopExp::Vertices(anEdge2, V21, V22);
325
326         gp_Pnt aP11 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge1));
327         gp_Pnt aP12 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge1));
328
329         gp_Pnt aP21 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge2));
330         gp_Pnt aP22 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge2));
331
332         //        *P3
333         //         \
334         //          \
335         //           *P22
336         //          /
337         //         /
338         // P11    /         P12
339         // *-----/----------*
340         //  \   /
341         //   \ /
342         //    *P21
343         gp_Pnt aP3 (aP22.XYZ() + aP11.XYZ() - aP21.XYZ());
344
345         gce_MakePln gce_MP(aP11, aP12, aP3);
346         Handle(Geom_Plane) aPlane = new Geom_Plane(gce_MP.Value());
347
348         // Build the angle dimension presentation
349         QString aLabel;
350         aLabel.sprintf("%.1f", anAngle);
351
352         Handle(AIS_AngleDimension) anIO = new AIS_AngleDimension
353           (anEdge1, anEdge2, aPlane, anAngle * PI180,
354             TCollection_ExtendedString((Standard_CString)aLabel.toLatin1().data()));
355         Handle(Geom_Line) geom_lin1,geom_lin2;
356         gp_Pnt ptat11,ptat12,ptat21,ptat22;
357         Standard_Boolean isInfinite1,isInfinite2;
358         Handle(Geom_Curve) extCurv;
359         Standard_Integer extShape;
360         if (AIS::ComputeGeometry(anEdge1,
361                                   anEdge2,
362                                   extShape,
363                                   geom_lin1,
364                                   geom_lin2,
365                                   ptat11,
366                                   ptat12,
367                                   ptat21,
368                                   ptat22,
369                                   extCurv,
370                                   isInfinite1,
371                                   isInfinite2,
372                                   aPlane)) {
373           Standard_Real arrSize1 = anIO->ArrowSize();
374           Standard_Real arrSize2 = anIO->ArrowSize();
375           if (!isInfinite1) arrSize1 = ptat11.Distance(ptat12)/10.;
376           if (!isInfinite2) arrSize2 = ptat21.Distance(ptat22)/10.;
377           Standard_Real arrowSize = Max(arrSize1,arrSize2);
378           anIO->SetArrowSize(arrowSize);
379         }
380
381         SOCC_Prs* aPrs =
382           dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
383
384         if (aPrs)
385           aPrs->AddObject(anIO);
386
387         return aPrs;
388       }
389     }
390     catch(Standard_Failure) {
391     }
392   }
393
394   return 0;
395 }
396
397 //=================================================================================
398 // function : isValid()
399 // purpose  :
400 //=================================================================================
401 bool MeasureGUI_AngleDlg::isValid (QString& msg)
402 {
403   return MeasureGUI_Skeleton::isValid(msg) && !myObj2->_is_nil();
404 }