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