Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[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
34 #include "SUIT_Session.h"
35 #include "SUIT_ViewWindow.h"
36 #include "SOCC_Prs.h"
37 #include "SOCC_ViewModel.h"
38 #include "SalomeApp_Tools.h"
39
40 #include <Geom_Plane.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <AIS_LengthDimension.hxx>
43 #include <BRepBuilderAPI_MakeEdge.hxx>
44 #include <BRepBuilderAPI_MakeVertex.hxx>
45 #include <AIS_ListIteratorOfListOfInteractive.hxx>
46 #include <gce_MakePln.hxx>
47 #include <Precision.hxx>
48
49 #include "utilities.h"
50
51 #include <qlineedit.h>
52 #include <qlabel.h>
53 #include <qlayout.h>
54 #include <qpushbutton.h>
55 #include <qradiobutton.h>
56 #include <qbuttongroup.h>
57
58 //=================================================================================
59 // class    : MeasureGUI_DistanceDlg()
60 // purpose  : Constructs a MeasureGUI_DistanceDlg which is a child of 'parent', with the
61 //            name 'name' and widget flags set to 'f'.
62 //            The dialog will by default be modeless, unless you set 'modal' to
63 //            TRUE to construct a modal dialog.
64 //=================================================================================
65 MeasureGUI_DistanceDlg::MeasureGUI_DistanceDlg( GeometryGUI* GUI, QWidget* parent )
66 : MeasureGUI_Skeleton( GUI, parent, "MeasureGUI_DistanceDlg" )
67 {
68   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap(
69     "GEOM",tr( "ICON_DLG_MINDIST" ) ) );
70   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap(
71   "GEOM",tr( "ICON_SELECT" ) ) );
72
73   setCaption( tr( "GEOM_MINDIST_TITLE" ) );
74
75   /***************************************************************/
76   
77   GroupConstructors->setTitle( tr( "GEOM_DISTANCE" ) );
78   RadioButton1->setPixmap( image0 );
79
80   myGrp = new MeasureGUI_2Sel1LineEdit_QTD( this, "myGrp" );
81   myGrp->GroupBox1->setTitle( tr( "GEOM_MINDIST_OBJ" ) );
82   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT_I" ).arg( "1" ) );
83   myGrp->TextLabel2->setText( tr( "GEOM_OBJECT_I" ).arg( "2" ) );
84   myGrp->TextLabel3->setText( tr( "GEOM_LENGTH" ) );
85   myGrp->LineEdit3->setReadOnly( TRUE );
86   myGrp->PushButton1->setPixmap( image1 );
87   myGrp->PushButton2->setPixmap( image1 );
88   myGrp->LineEdit1->setReadOnly( true );
89   myGrp->LineEdit2->setReadOnly( true );
90
91   Layout1->addWidget( myGrp, 1, 0 );
92
93   /***************************************************************/
94
95   myHelpFileName = "files/salome2_sp3_measuregui_functions.htm#Min_distance";
96
97   /* Initialisation */
98   Init();
99 }
100
101
102 //=================================================================================
103 // function : ~MeasureGUI_DistanceDlg()
104 // purpose  : Destroys the object and frees any allocated resources
105 //=================================================================================
106 MeasureGUI_DistanceDlg::~MeasureGUI_DistanceDlg()
107 {
108 }
109
110
111 //=================================================================================
112 // function : Init()
113 // purpose  :
114 //=================================================================================
115 void MeasureGUI_DistanceDlg::Init()
116 {
117   mySelBtn   = myGrp->PushButton1;
118   mySelEdit  = myGrp->LineEdit1;
119   mySelBtn2  = myGrp->PushButton2;
120   mySelEdit2 = myGrp->LineEdit2;
121   
122   myEditCurrentArgument = mySelEdit;
123
124   connect( mySelEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
125   connect( mySelBtn2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
126
127   MeasureGUI_Skeleton::Init();
128
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( QString( "%1" ).arg( aDist ) );
167     redisplayPreview();
168   }
169   else
170   {
171     myGrp->LineEdit3->setText( "" );
172     erasePreview();
173   }
174     
175 }
176
177 //=================================================================================
178 // function : getParameters()
179 // purpose  : Get distance between objects
180 //=================================================================================
181 bool MeasureGUI_DistanceDlg::getParameters( double& theDistance,
182                                             gp_Pnt& thePnt1,
183                                             gp_Pnt& thePnt2 )
184 {
185   QString msg;
186   if ( !isValid( msg ) )
187     return false;
188   else
189   {
190     try
191     {
192       double x1, y1, z1, x2, y2, z2;
193       theDistance = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetMinDistance(
194         myObj, myObj2, x1, y1, z1, x2, y2, z2 );
195
196       thePnt1.SetCoord( x1, y1, z1 );
197       thePnt2.SetCoord( x2, y2, z2 );
198     }
199     catch( const SALOME::SALOME_Exception& e )
200     {
201       SalomeApp_Tools::QtCatchCorbaException( e );
202       return false;
203     }
204
205     return getOperation()->IsDone();
206   }
207 }
208
209
210 //=================================================================================
211 // function : SetEditCurrentArgument()
212 // purpose  :
213 //=================================================================================
214 void MeasureGUI_DistanceDlg::SetEditCurrentArgument()
215 {
216   QPushButton* send = ( QPushButton* )sender();
217
218   if( send == mySelBtn )
219   {
220     mySelEdit->setFocus();
221     myEditCurrentArgument = mySelEdit;
222   }
223   else
224   {
225     mySelEdit2->setFocus();
226     myEditCurrentArgument = mySelEdit2;
227   }
228
229   SelectionIntoArgument();
230 }
231
232
233 //=================================================================================
234 // function : LineEditReturnPressed()
235 // purpose  :
236 //=================================================================================
237 void MeasureGUI_DistanceDlg::LineEditReturnPressed()
238 {
239   QLineEdit* send = ( QLineEdit* )sender();
240
241   if( send == mySelEdit )
242     myEditCurrentArgument = mySelEdit;
243   else
244     myEditCurrentArgument = mySelEdit2;
245
246   if ( GEOMBase::SelectionByNameInDialogs( this, mySelEdit->text(), selectedIO() ) )
247     mySelEdit->setText( mySelEdit->text() );
248 }
249
250
251 //=================================================================================
252 // function : buildPrs()
253 // purpose  :
254 //=================================================================================
255 SALOME_Prs* MeasureGUI_DistanceDlg::buildPrs()
256 {
257   double aDist = 0.;
258   gp_Pnt aPnt1( 0, 0, 0 ), aPnt2( 0, 0, 0 );
259   
260   if ( myObj->_is_nil() || myObj2->_is_nil() || !getParameters( aDist, aPnt1, aPnt2 ) ||
261        SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
262        != OCCViewer_Viewer::Type() )
263     return 0;
264   
265   try
266   {
267     if( aDist <= 1.e-9 )
268     {
269       BRepBuilderAPI_MakeVertex aMaker( aPnt1 );
270       return getDisplayer()->BuildPrs( aMaker.Vertex() );
271     }
272     else
273     {
274       BRepBuilderAPI_MakeEdge MakeEdge( aPnt1, aPnt2 );
275       TopoDS_Vertex aVert1 = BRepBuilderAPI_MakeVertex( aPnt1 );
276       TopoDS_Vertex aVert2 = BRepBuilderAPI_MakeVertex( aPnt2 );
277
278       QString aLabel;
279       aLabel.sprintf( "%.1f", aDist );
280
281       gp_Pnt aPnt3( ( aPnt1.X() + aPnt2.X() ) / 2,
282                     ( aPnt1.Y() + aPnt2.Y() ) / 2,
283                     ( aPnt1.Z() + aPnt2.Z() ) / 2 + 100 );
284
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       SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
302       SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>( ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs( 0 ) );
303       
304       //QAD_ViewFrame* vf = GEOM_Displayer::GetActiveView();
305       //OCCViewer_Prs* aPrs = dynamic_cast<OCCViewer_Prs*>( vf->CreatePrs( 0 ) );
306
307       if ( aPrs )
308         aPrs->AddObject( anIO );
309
310       return aPrs;
311     }
312   }
313   catch( Standard_Failure )
314   {
315     return 0;
316   }
317 }
318
319 //=================================================================================
320 // function : isValid()
321 // purpose  :
322 //=================================================================================
323 bool MeasureGUI_DistanceDlg::isValid( QString& msg )
324 {
325   return MeasureGUI_Skeleton::isValid( msg ) && !myObj2->_is_nil();
326 }
327
328
329
330
331
332
333
334
335
336
337
338
339
340