Salome HOME
Join modifications from V3_2_0_maintainance (V3_2_6pre4 - T32x_16Aug2007_16h00m)
[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_2Sel1LineEdit_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_2Sel1LineEdit_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->LineEdit3->setReadOnly( TRUE );
87   myGrp->PushButton1->setPixmap( image1 );
88   myGrp->PushButton2->setPixmap( image1 );
89   myGrp->LineEdit1->setReadOnly( true );
90   myGrp->LineEdit2->setReadOnly( true );
91
92   Layout1->addWidget( myGrp, 1, 0 );
93
94   /***************************************************************/
95
96   myHelpFileName = "files/salome2_sp3_measuregui_functions.htm#Min_distance";
97
98   /* Initialisation */
99   Init();
100 }
101
102
103 //=================================================================================
104 // function : ~MeasureGUI_DistanceDlg()
105 // purpose  : Destroys the object and frees any allocated resources
106 //=================================================================================
107 MeasureGUI_DistanceDlg::~MeasureGUI_DistanceDlg()
108 {
109 }
110
111
112 //=================================================================================
113 // function : Init()
114 // purpose  :
115 //=================================================================================
116 void MeasureGUI_DistanceDlg::Init()
117 {
118   mySelBtn   = myGrp->PushButton1;
119   mySelEdit  = myGrp->LineEdit1;
120   mySelBtn2  = myGrp->PushButton2;
121   mySelEdit2 = myGrp->LineEdit2;
122
123   myEditCurrentArgument = mySelEdit;
124
125   connect( mySelEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
126   connect( mySelBtn2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
127
128   MeasureGUI_Skeleton::Init();
129 }
130
131
132 //=================================================================================
133 // function : SelectionIntoArgument()
134 // purpose  : Called when selection has changed
135 //=================================================================================
136 void MeasureGUI_DistanceDlg::SelectionIntoArgument()
137 {
138   Standard_Boolean testResult = Standard_False;
139   GEOM::GEOM_Object_var aSelectedObject =
140     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
141
142   if ( !testResult )
143     aSelectedObject = GEOM::GEOM_Object::_nil();
144
145   if ( myEditCurrentArgument == mySelEdit )
146       myObj = aSelectedObject;
147     else
148       myObj2 = aSelectedObject;
149
150   processObject();
151 }
152
153 //=================================================================================
154 // function : processObject()
155 // purpose  : Fill dialogs fileds in accordance with myObj and myObj2
156 //=================================================================================
157 void MeasureGUI_DistanceDlg::processObject()
158 {
159   myGrp->LineEdit1->setText( !myObj->_is_nil()  ? GEOMBase::GetName( myObj  ) : "" );
160   myGrp->LineEdit2->setText( !myObj2->_is_nil() ? GEOMBase::GetName( myObj2 ) : "" );
161
162   gp_Pnt aPnt1, aPnt2;
163   double aDist = 0.;
164   if ( getParameters( aDist, aPnt1, aPnt2 ) )
165   {
166     myGrp->LineEdit3->setText( DlgRef_SpinBox::PrintDoubleValue( aDist ) );
167     redisplayPreview();
168   }
169   else
170   {
171     myGrp->LineEdit3->setText( "" );
172     erasePreview();
173   }
174 }
175
176 //=================================================================================
177 // function : getParameters()
178 // purpose  : Get distance between objects
179 //=================================================================================
180 bool MeasureGUI_DistanceDlg::getParameters( double& theDistance,
181                                             gp_Pnt& thePnt1,
182                                             gp_Pnt& thePnt2 )
183 {
184   QString msg;
185   if ( !isValid( msg ) )
186     return false;
187   else
188   {
189     try
190     {
191       double x1, y1, z1, x2, y2, z2;
192       theDistance = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetMinDistance(
193         myObj, myObj2, x1, y1, z1, x2, y2, z2 );
194
195       thePnt1.SetCoord( x1, y1, z1 );
196       thePnt2.SetCoord( x2, y2, z2 );
197     }
198     catch( const SALOME::SALOME_Exception& e )
199     {
200       SalomeApp_Tools::QtCatchCorbaException( e );
201       return false;
202     }
203
204     return getOperation()->IsDone();
205   }
206 }
207
208
209 //=================================================================================
210 // function : SetEditCurrentArgument()
211 // purpose  :
212 //=================================================================================
213 void MeasureGUI_DistanceDlg::SetEditCurrentArgument()
214 {
215   QPushButton* send = ( QPushButton* )sender();
216
217   if( send == mySelBtn )
218   {
219     mySelEdit->setFocus();
220     myEditCurrentArgument = mySelEdit;
221   }
222   else
223   {
224     mySelEdit2->setFocus();
225     myEditCurrentArgument = mySelEdit2;
226   }
227
228   SelectionIntoArgument();
229 }
230
231
232 //=================================================================================
233 // function : LineEditReturnPressed()
234 // purpose  :
235 //=================================================================================
236 void MeasureGUI_DistanceDlg::LineEditReturnPressed()
237 {
238   QLineEdit* send = ( QLineEdit* )sender();
239
240   if( send == mySelEdit )
241     myEditCurrentArgument = mySelEdit;
242   else
243     myEditCurrentArgument = mySelEdit2;
244
245   if ( GEOMBase::SelectionByNameInDialogs( this, mySelEdit->text(), selectedIO() ) )
246     mySelEdit->setText( mySelEdit->text() );
247 }
248
249
250 //=================================================================================
251 // function : buildPrs()
252 // purpose  :
253 //=================================================================================
254 SALOME_Prs* MeasureGUI_DistanceDlg::buildPrs()
255 {
256   double aDist = 0.;
257   gp_Pnt aPnt1( 0, 0, 0 ), aPnt2( 0, 0, 0 );
258
259   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
260
261   if ( myObj->_is_nil() || myObj2->_is_nil() ||
262        !getParameters( aDist, aPnt1, aPnt2 ) ||
263        vw->getViewManager()->getType() != OCCViewer_Viewer::Type() )
264     return 0;
265
266   try
267   {
268     if( aDist <= 1.e-9 )
269     {
270       BRepBuilderAPI_MakeVertex aMaker( aPnt1 );
271       return getDisplayer()->BuildPrs( aMaker.Vertex() );
272     }
273     else
274     {
275       BRepBuilderAPI_MakeEdge MakeEdge( aPnt1, aPnt2 );
276       TopoDS_Vertex aVert1 = BRepBuilderAPI_MakeVertex( aPnt1 );
277       TopoDS_Vertex aVert2 = BRepBuilderAPI_MakeVertex( aPnt2 );
278
279       QString aLabel;
280       aLabel.sprintf( "%.1f", aDist );
281
282       gp_Pnt aPnt3( ( aPnt1.X() + aPnt2.X() ) / 2,
283                     ( aPnt1.Y() + aPnt2.Y() ) / 2,
284                     ( aPnt1.Z() + aPnt2.Z() ) / 2 + 100 );
285
286       gp_Vec va( aPnt3, aPnt1 );
287       gp_Vec vb( aPnt3, aPnt2 );
288
289       if ( va.IsParallel( vb, Precision::Angular() ) )
290       {
291         aPnt3.SetY( ( aPnt1.Y() + aPnt2.Y() ) / 2 + 100 );
292         aPnt3.SetZ( ( aPnt1.Z() + aPnt2.Z() ) / 2 );
293       }
294
295       gce_MakePln gce_MP( aPnt1, aPnt2, aPnt3 );
296       Handle( Geom_Plane ) P = new Geom_Plane( gce_MP.Value() );
297
298       Handle( AIS_LengthDimension ) anIO = new AIS_LengthDimension
299         (aVert1, aVert2, P, aDist, TCollection_ExtendedString((Standard_CString)aLabel.latin1()));
300
301       SOCC_Prs* aPrs =
302         dynamic_cast<SOCC_Prs*>( ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs( 0 ) );
303
304       if ( aPrs )
305         aPrs->AddObject( anIO );
306
307       return aPrs;
308     }
309   }
310   catch( Standard_Failure )
311   {
312     return 0;
313   }
314 }
315
316 //=================================================================================
317 // function : isValid()
318 // purpose  :
319 //=================================================================================
320 bool MeasureGUI_DistanceDlg::isValid( QString& msg )
321 {
322   return MeasureGUI_Skeleton::isValid( msg ) && !myObj2->_is_nil();
323 }