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