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