1 // GEOM GEOMGUI : GUI for Geometry component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : MeasureGUI_DistanceDlg.cxx
23 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 #include "MeasureGUI_DistanceDlg.h"
27 #include "MeasureGUI_Widgets.h"
31 #include <SUIT_Session.h>
32 #include <SUIT_Desktop.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_ViewWindow.h>
35 #include <SUIT_ViewManager.h>
37 #include <SOCC_ViewModel.h>
38 #include <SalomeApp_Tools.h>
40 #include <Geom_Plane.hxx>
41 #include <AIS_LengthDimension.hxx>
42 #include <BRepBuilderAPI_MakeEdge.hxx>
43 #include <BRepBuilderAPI_MakeVertex.hxx>
44 #include <gce_MakePln.hxx>
45 #include <Precision.hxx>
47 //=================================================================================
48 // class : MeasureGUI_DistanceDlg()
49 // purpose : Constructs a MeasureGUI_DistanceDlg which is a child of 'parent', with the
50 // name 'name' and widget flags set to 'f'.
51 // The dialog will by default be modeless, unless you set 'modal' to
52 // true to construct a modal dialog.
53 //=================================================================================
54 MeasureGUI_DistanceDlg::MeasureGUI_DistanceDlg( GeometryGUI* GUI, QWidget* parent )
55 : MeasureGUI_Skeleton( GUI, parent )
57 QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap(
58 "GEOM", tr( "ICON_DLG_MINDIST" ) ) );
59 QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap(
60 "GEOM", tr( "ICON_SELECT" ) ) );
62 setWindowTitle( tr( "GEOM_MINDIST_TITLE" ) );
64 /***************************************************************/
66 mainFrame()->GroupConstructors->setTitle( tr( "GEOM_DISTANCE" ) );
67 mainFrame()->RadioButton1->setIcon( image0 );
69 myGrp = new MeasureGUI_2Sel1LineEdit( centralWidget() );
70 myGrp->GroupBox1->setTitle( tr( "GEOM_MINDIST_OBJ" ) );
71 myGrp->TextLabel1->setText( tr( "GEOM_OBJECT_I" ).arg( "1" ) );
72 myGrp->TextLabel2->setText( tr( "GEOM_OBJECT_I" ).arg( "2" ) );
73 myGrp->TextLabel3->setText( tr( "GEOM_LENGTH" ) );
74 myGrp->LineEdit3->setReadOnly( true );
75 myGrp->PushButton1->setIcon( image1 );
76 myGrp->PushButton2->setIcon( image1 );
77 myGrp->LineEdit1->setReadOnly( true );
78 myGrp->LineEdit2->setReadOnly( true );
80 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
81 layout->setMargin( 0 ); layout->setSpacing( 6 );
82 layout->addWidget( myGrp );
84 /***************************************************************/
86 myHelpFileName = "files/salome2_sp3_measuregui_functions.htm#Min_distance";
93 //=================================================================================
94 // function : ~MeasureGUI_DistanceDlg()
95 // purpose : Destroys the object and frees any allocated resources
96 //=================================================================================
97 MeasureGUI_DistanceDlg::~MeasureGUI_DistanceDlg()
102 //=================================================================================
105 //=================================================================================
106 void MeasureGUI_DistanceDlg::Init()
108 mySelBtn = myGrp->PushButton1;
109 mySelEdit = myGrp->LineEdit1;
110 mySelBtn2 = myGrp->PushButton2;
111 mySelEdit2 = myGrp->LineEdit2;
113 myEditCurrentArgument = mySelEdit;
115 connect( mySelEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
116 connect( mySelBtn2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
118 MeasureGUI_Skeleton::Init();
123 //=================================================================================
124 // function : SelectionIntoArgument()
125 // purpose : Called when selection has changed
126 //=================================================================================
127 void MeasureGUI_DistanceDlg::SelectionIntoArgument()
129 Standard_Boolean testResult = Standard_False;
130 GEOM::GEOM_Object_var aSelectedObject =
131 GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
134 aSelectedObject = GEOM::GEOM_Object::_nil();
136 if ( myEditCurrentArgument == mySelEdit )
137 myObj = aSelectedObject;
139 myObj2 = aSelectedObject;
144 //=================================================================================
145 // function : processObject()
146 // purpose : Fill dialogs fileds in accordance with myObj and myObj2
147 //=================================================================================
148 void MeasureGUI_DistanceDlg::processObject()
150 myGrp->LineEdit1->setText( !myObj->_is_nil() ? GEOMBase::GetName( myObj ) : "" );
151 myGrp->LineEdit2->setText( !myObj2->_is_nil() ? GEOMBase::GetName( myObj2 ) : "" );
155 if ( getParameters( aDist, aPnt1, aPnt2 ) ) {
156 myGrp->LineEdit3->setText( QString( "%1" ).arg( aDist ) );
160 myGrp->LineEdit3->setText( "" );
166 //=================================================================================
167 // function : getParameters()
168 // purpose : Get distance between objects
169 //=================================================================================
170 bool MeasureGUI_DistanceDlg::getParameters( double& theDistance,
175 if ( !isValid( msg ) )
179 double x1, y1, z1, x2, y2, z2;
180 theDistance = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetMinDistance(
181 myObj, myObj2, x1, y1, z1, x2, y2, z2 );
183 thePnt1.SetCoord( x1, y1, z1 );
184 thePnt2.SetCoord( x2, y2, z2 );
186 catch( const SALOME::SALOME_Exception& e ) {
187 SalomeApp_Tools::QtCatchCorbaException( e );
191 return getOperation()->IsDone();
196 //=================================================================================
197 // function : SetEditCurrentArgument()
199 //=================================================================================
200 void MeasureGUI_DistanceDlg::SetEditCurrentArgument()
202 QPushButton* send = ( QPushButton* )sender();
204 if ( send == mySelBtn ) {
205 mySelEdit->setFocus();
206 myEditCurrentArgument = mySelEdit;
209 mySelEdit2->setFocus();
210 myEditCurrentArgument = mySelEdit2;
213 SelectionIntoArgument();
217 //=================================================================================
218 // function : LineEditReturnPressed()
220 //=================================================================================
221 void MeasureGUI_DistanceDlg::LineEditReturnPressed()
223 QLineEdit* send = ( QLineEdit* )sender();
225 if ( send == mySelEdit )
226 myEditCurrentArgument = mySelEdit;
228 myEditCurrentArgument = mySelEdit2;
230 if ( GEOMBase::SelectionByNameInDialogs( this, mySelEdit->text(), selectedIO() ) )
231 mySelEdit->setText( mySelEdit->text() );
235 //=================================================================================
236 // function : buildPrs()
238 //=================================================================================
239 SALOME_Prs* MeasureGUI_DistanceDlg::buildPrs()
242 gp_Pnt aPnt1( 0, 0, 0 ), aPnt2( 0, 0, 0 );
244 if ( myObj->_is_nil() || myObj2->_is_nil() || !getParameters( aDist, aPnt1, aPnt2 ) ||
245 SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType()
246 != OCCViewer_Viewer::Type() )
251 if( aDist <= 1.e-9 ) {
252 BRepBuilderAPI_MakeVertex aMaker( aPnt1 );
253 return getDisplayer()->BuildPrs( aMaker.Vertex() );
256 BRepBuilderAPI_MakeEdge MakeEdge( aPnt1, aPnt2 );
257 TopoDS_Vertex aVert1 = BRepBuilderAPI_MakeVertex( aPnt1 );
258 TopoDS_Vertex aVert2 = BRepBuilderAPI_MakeVertex( aPnt2 );
261 aLabel.sprintf( "%.1f", aDist );
263 gp_Pnt aPnt3( ( aPnt1.X() + aPnt2.X() ) / 2,
264 ( aPnt1.Y() + aPnt2.Y() ) / 2,
265 ( aPnt1.Z() + aPnt2.Z() ) / 2 + 100 );
268 gp_Vec va( aPnt3, aPnt1 );
269 gp_Vec vb( aPnt3, aPnt2 );
271 if ( va.IsParallel( vb, Precision::Angular() ) ) {
272 aPnt3.SetY( ( aPnt1.Y() + aPnt2.Y() ) / 2 + 100 );
273 aPnt3.SetZ( ( aPnt1.Z() + aPnt2.Z() ) / 2 );
276 gce_MakePln gce_MP( aPnt1, aPnt2, aPnt3 );
277 Handle( Geom_Plane ) P = new Geom_Plane( gce_MP.Value() );
279 Handle( AIS_LengthDimension ) anIO = new AIS_LengthDimension(
280 aVert1, aVert2, P, aDist, TCollection_ExtendedString( (Standard_CString)aLabel.toLatin1().constData() ) );
282 SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
283 SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>( ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs( 0 ) );
285 //QAD_ViewFrame* vf = GEOM_Displayer::GetActiveView();
286 //OCCViewer_Prs* aPrs = dynamic_cast<OCCViewer_Prs*>( vf->CreatePrs( 0 ) );
289 aPrs->AddObject( anIO );
294 catch( Standard_Failure ) {
299 //=================================================================================
300 // function : isValid()
302 //=================================================================================
303 bool MeasureGUI_DistanceDlg::isValid( QString& msg )
305 return MeasureGUI_Skeleton::isValid( msg ) && !myObj2->_is_nil();