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