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