Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/geom.git] / src / MeasureGUI / MeasureGUI_DistanceDlg.cxx
1 // Copyright (C) 2007-2012  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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : MeasureGUI_DistanceDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 //
27 #include "MeasureGUI_DistanceDlg.h"
28 #include "MeasureGUI_Widgets.h"
29
30 #include <DlgRef.h>
31 #include <GEOMBase.h>
32 #include <GeometryGUI.h>
33
34 #include <SUIT_Session.h>
35 #include <SUIT_Desktop.h>
36 #include <SUIT_ResourceMgr.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 <Geom_Plane.hxx>
47 #include <AIS_LengthDimension.hxx>
48 #include <AIS_Drawer.hxx>
49 #include <Prs3d_LengthAspect.hxx>
50 #include <Prs3d_LineAspect.hxx>
51 #include <BRepBuilderAPI_MakeEdge.hxx>
52 #include <BRepBuilderAPI_MakeVertex.hxx>
53 #include <gce_MakePln.hxx>
54 #include <Precision.hxx>
55
56 //=================================================================================
57 // class    : MeasureGUI_DistanceDlg()
58 // purpose  : Constructs a MeasureGUI_DistanceDlg which is a child of 'parent', with the
59 //            name 'name' and widget flags set to 'f'.
60 //            The dialog will by default be modeless, unless you set 'modal' to
61 //            true to construct a modal dialog.
62 //=================================================================================
63 MeasureGUI_DistanceDlg::MeasureGUI_DistanceDlg (GeometryGUI* GUI, QWidget* parent)
64   : MeasureGUI_Skeleton(GUI, parent)
65 {
66   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
67   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_MINDIST")));
68   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
69
70   setWindowTitle(tr("GEOM_MINDIST_TITLE"));
71
72   /***************************************************************/
73   mainFrame()->GroupConstructors->setTitle(tr("GEOM_DISTANCE"));
74   mainFrame()->RadioButton1->setIcon(image0);
75
76   myGrp = new MeasureGUI_2Sel4LineEdit(centralWidget());
77   myGrp->GroupBox1->setTitle(tr("GEOM_MINDIST_OBJ"));
78   myGrp->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
79   myGrp->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
80   myGrp->TextLabel3->setText(tr("GEOM_LENGTH"));
81   myGrp->TextLabel4->setText(tr("GEOM_DX"));
82   myGrp->TextLabel5->setText(tr("GEOM_DY"));
83   myGrp->TextLabel6->setText(tr("GEOM_DZ"));
84   myGrp->LineEdit3->setReadOnly(true);
85   myGrp->PushButton1->setIcon(image1);
86   myGrp->PushButton2->setIcon(image1);
87   myGrp->LineEdit1->setReadOnly(true);
88   myGrp->LineEdit2->setReadOnly(true);
89   myGrp->LineEdit4->setReadOnly(true);
90   myGrp->LineEdit5->setReadOnly(true);
91   myGrp->LineEdit6->setReadOnly(true);
92
93   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
94   layout->setMargin(0); layout->setSpacing(6);
95   layout->addWidget(myGrp);
96   /***************************************************************/
97
98   myHelpFileName = "using_measurement_tools_page.html#min_distance_anchor";
99
100   // Initialisation
101   Init();
102 }
103
104 //=================================================================================
105 // function : ~MeasureGUI_DistanceDlg()
106 // purpose  : Destroys the object and frees any allocated resources
107 //=================================================================================
108 MeasureGUI_DistanceDlg::~MeasureGUI_DistanceDlg()
109 {
110 }
111
112 //=================================================================================
113 // function : Init()
114 // purpose  :
115 //=================================================================================
116 void MeasureGUI_DistanceDlg::Init()
117 {
118   // init variables
119   myGrp->LineEdit1->setText("");
120   myGrp->LineEdit2->setText("");
121   myObj = myObj2 = GEOM::GEOM_Object::_nil();
122
123   mySelBtn   = myGrp->PushButton1;
124   mySelEdit  = myGrp->LineEdit1;
125
126   myEditCurrentArgument = myGrp->LineEdit1;
127
128   // signals and slots connections
129   connect(myGrp->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
130   connect(myGrp->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
131
132   MeasureGUI_Skeleton::Init();
133 }
134
135 //=================================================================================
136 // function : SelectionIntoArgument()
137 // purpose  : Called when selection has changed
138 //=================================================================================
139 void MeasureGUI_DistanceDlg::SelectionIntoArgument()
140 {
141   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
142   SALOME_ListIO aSelList;
143   aSelMgr->selectedObjects(aSelList);
144
145   GEOM::GEOM_Object_var aSelectedObject = GEOM::GEOM_Object::_nil();
146
147   if (aSelList.Extent() > 0) {
148     aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
149   }
150
151   // clear selection
152   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
153   myGeomGUI->getApp()->selectionMgr()->clearSelected();
154   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
155           this, SLOT(SelectionIntoArgument()));
156
157   if (myEditCurrentArgument == myGrp->LineEdit1) {
158     myObj = aSelectedObject;
159     if (!myObj->_is_nil() && myObj2->_is_nil())
160       myGrp->PushButton2->click();
161   }
162   else {
163     myObj2 = aSelectedObject;
164     if (!myObj2->_is_nil() && myObj->_is_nil())
165       myGrp->PushButton1->click();
166   }
167
168   processObject();
169 }
170
171 //=================================================================================
172 // function : processObject()
173 // purpose  : Fill dialogs fields in accordance with myObj and myObj2
174 //=================================================================================
175 void MeasureGUI_DistanceDlg::processObject()
176 {
177   myGrp->LineEdit1->setText(!myObj->_is_nil()  ? GEOMBase::GetName(myObj ) : "");
178   myGrp->LineEdit2->setText(!myObj2->_is_nil() ? GEOMBase::GetName(myObj2) : "");
179
180   gp_Pnt aPnt1, aPnt2;
181   double aDist = 0.;
182   if (getParameters(aDist, aPnt1, aPnt2)) {
183     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
184     int aPrecision = resMgr->integerValue( "Geometry", "length_precision", 6 );
185
186     myGrp->LineEdit3->setText(DlgRef::PrintDoubleValue(aDist, aPrecision));
187
188     gp_XYZ aVec = aPnt2.XYZ() - aPnt1.XYZ();
189     myGrp->LineEdit4->setText(DlgRef::PrintDoubleValue(aVec.X(), aPrecision));
190     myGrp->LineEdit5->setText(DlgRef::PrintDoubleValue(aVec.Y(), aPrecision));
191     myGrp->LineEdit6->setText(DlgRef::PrintDoubleValue(aVec.Z(), aPrecision));
192
193     redisplayPreview();
194   }
195   else {
196     myGrp->LineEdit3->setText("");
197     myGrp->LineEdit4->setText("");
198     myGrp->LineEdit5->setText("");
199     myGrp->LineEdit6->setText("");
200     erasePreview();
201   }
202 }
203
204 //=================================================================================
205 // function : getParameters()
206 // purpose  : Get distance between objects
207 //=================================================================================
208 bool MeasureGUI_DistanceDlg::getParameters (double& theDistance,
209                                             gp_Pnt& thePnt1,
210                                             gp_Pnt& thePnt2)
211 {
212   QString msg;
213   if (isValid(msg)) {
214     GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
215
216     try {
217       double x1, y1, z1, x2, y2, z2;
218       theDistance = anOper->GetMinDistance(myObj, myObj2, x1, y1, z1, x2, y2, z2);
219
220       thePnt1.SetCoord(x1, y1, z1);
221       thePnt2.SetCoord(x2, y2, z2);
222     }
223     catch(const SALOME::SALOME_Exception& e) {
224       SalomeApp_Tools::QtCatchCorbaException(e);
225       return false;
226     }
227
228     return anOper->IsDone();
229   }
230
231   return false;
232 }
233
234 //=================================================================================
235 // function : SetEditCurrentArgument()
236 // purpose  :
237 //=================================================================================
238 void MeasureGUI_DistanceDlg::SetEditCurrentArgument()
239 {
240   QPushButton* send = (QPushButton*)sender();
241
242   if (send == myGrp->PushButton1) {
243     myEditCurrentArgument = myGrp->LineEdit1;
244
245     myGrp->PushButton2->setDown(false);
246     myGrp->LineEdit2->setEnabled(false);
247   }
248   else {
249     myEditCurrentArgument = myGrp->LineEdit2;
250
251     myGrp->PushButton1->setDown(false);
252     myGrp->LineEdit1->setEnabled(false);
253   }
254
255   // enable line edit
256   myEditCurrentArgument->setEnabled(true);
257   myEditCurrentArgument->setFocus();
258   // after setFocus(), because it will be setDown(false) when loses focus
259   send->setDown(true);
260
261   // seems we need it only to avoid preview disappearing, caused by selection mode change
262   redisplayPreview();
263 }
264
265 //=================================================================================
266 // function : LineEditReturnPressed()
267 // purpose  :
268 //=================================================================================
269 void MeasureGUI_DistanceDlg::LineEditReturnPressed()
270 {
271   QLineEdit* send = (QLineEdit*)sender();
272
273   if (send == myGrp->LineEdit1 || send == myGrp->LineEdit2) {
274     myEditCurrentArgument = send;
275
276     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
277     SALOME_ListIO aSelList;
278     aSelMgr->selectedObjects(aSelList);
279
280     if (GEOMBase::SelectionByNameInDialogs(this, myGrp->LineEdit1->text(), aSelList))
281       myGrp->LineEdit1->setText(myGrp->LineEdit1->text());
282   }
283 }
284
285 //=================================================================================
286 // function : buildPrs()
287 // purpose  :
288 //=================================================================================
289 SALOME_Prs* MeasureGUI_DistanceDlg::buildPrs()
290 {
291   double aDist = 0.;
292   gp_Pnt aPnt1(0, 0, 0), aPnt2(0, 0, 0);
293
294   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
295
296   if (myObj->_is_nil() || myObj2->_is_nil() ||
297        !getParameters(aDist, aPnt1, aPnt2) ||
298        vw->getViewManager()->getType() != OCCViewer_Viewer::Type())
299     return 0;
300
301   try
302   {
303     if (aDist <= 1.e-9) {
304       BRepBuilderAPI_MakeVertex aMaker(aPnt1);
305       return getDisplayer()->BuildPrs(aMaker.Vertex());
306     }
307     else {
308       BRepBuilderAPI_MakeEdge MakeEdge(aPnt1, aPnt2);
309       TopoDS_Vertex aVert1 = BRepBuilderAPI_MakeVertex(aPnt1);
310       TopoDS_Vertex aVert2 = BRepBuilderAPI_MakeVertex(aPnt2);
311
312       QString aLabel;
313       aLabel.sprintf("%.1f", aDist);
314
315       gp_Pnt aPnt3((aPnt1.X() + aPnt2.X()) / 2,
316                     (aPnt1.Y() + aPnt2.Y()) / 2,
317                     (aPnt1.Z() + aPnt2.Z()) / 2);
318
319       gp_Vec va(aPnt3, aPnt1);
320       gp_Vec vb(aPnt3, aPnt2);
321
322       if (va.IsParallel(vb, Precision::Angular())) {
323         aPnt3.SetY((aPnt1.Y() + aPnt2.Y()) / 2 + 100);
324         aPnt3.SetZ((aPnt1.Z() + aPnt2.Z()) / 2);
325       }
326
327       gce_MakePln gce_MP(aPnt1, aPnt2, aPnt3);
328       Handle(Geom_Plane) P = new Geom_Plane(gce_MP.Value());
329
330       Handle(AIS_LengthDimension) anIO = new AIS_LengthDimension(
331         aVert1, aVert2, P, aDist, TCollection_ExtendedString((Standard_CString)aLabel.toLatin1().constData()));
332       anIO->SetArrowSize(aDist/20);
333       
334       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
335       int w = resMgr->integerValue("Geometry", "measures_line_width", 1);
336       Handle(Prs3d_LengthAspect) asp = new Prs3d_LengthAspect();
337       asp->LineAspect()->SetWidth(w);
338       anIO->Attributes()->SetLengthAspect(asp);
339
340       SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
341
342       if (aPrs)
343         aPrs->AddObject(anIO);
344
345       return aPrs;
346     }
347   }
348   catch(Standard_Failure) {
349     return 0;
350   }
351 }
352
353 //=================================================================================
354 // function : isValid()
355 // purpose  :
356 //=================================================================================
357 bool MeasureGUI_DistanceDlg::isValid (QString& msg)
358 {
359   return MeasureGUI_Skeleton::isValid(msg) && !myObj2->_is_nil();
360 }