Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_17Jan08)
[modules/geom.git] / src / MeasureGUI / MeasureGUI_AngleDlg.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 //  File   : MeasureGUI_AngleDlg.cxx
23 //  Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
24 //
25
26 #include "MeasureGUI_AngleDlg.h"
27 #include "MeasureGUI_Widgets.h"
28
29 #include <GEOMBase.h>
30 #include <DlgRef.h>
31
32 #include <SUIT_Desktop.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Tools.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_ViewWindow.h>
38 #include <SUIT_ViewManager.h>
39 #include <SOCC_Prs.h>
40 #include <SOCC_ViewModel.h>
41 //#include "SalomeApp_Application.h"
42
43 // OCCT Includes
44 #include <AIS_AngleDimension.hxx>
45 #include <AIS_ListIteratorOfListOfInteractive.hxx>
46 #include <BRep_Tool.hxx>
47 #include <BRepBuilderAPI_MakeEdge.hxx>
48 #include <BRepBuilderAPI_MakeVertex.hxx>
49 #include <TopExp.hxx>
50 #include <TopoDS.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <Geom_Plane.hxx>
53 #include <gce_MakePln.hxx>
54 #include <Precision.hxx>
55
56 // QT Includes
57 #include <qlineedit.h>
58 #include <qlabel.h>
59 #include <qlayout.h>
60 #include <qpushbutton.h>
61 #include <qradiobutton.h>
62 #include <qbuttongroup.h>
63
64 #include "GEOMImpl_Types.hxx"
65
66 #include "utilities.h"
67
68 #include <Standard_Failure.hxx>
69 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
70
71 //=================================================================================
72 // class    : MeasureGUI_AngleDlg()
73 // purpose  : Constructs a MeasureGUI_AngleDlg which is a child of 'parent', with the
74 //            name 'name' and widget flags set to 'f'.
75 //            The dialog will by default be modeless, unless you set 'modal' to
76 //            TRUE to construct a modal dialog.
77 //=================================================================================
78 MeasureGUI_AngleDlg::MeasureGUI_AngleDlg( GeometryGUI* GUI, QWidget* parent )
79   : MeasureGUI_Skeleton( GUI, parent )
80 {
81   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_ANGLE" ) ) );
82   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
83
84   setWindowTitle( tr( "GEOM_MEASURE_ANGLE_TITLE" ) );
85
86   // Widgets
87
88   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_MEASURE_ANGLE_ANGLE" ) );
89   mainFrame()->RadioButton1->setIcon( image0 );
90
91   myGrp = new MeasureGUI_2Sel1LineEdit( centralWidget() );
92   myGrp->GroupBox1->setTitle( tr( "GEOM_MEASURE_ANGLE_OBJ" ) );
93   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT_I" ).arg( "1" ) );
94   myGrp->TextLabel2->setText( tr( "GEOM_OBJECT_I" ).arg( "2" ) );
95   myGrp->TextLabel3->setText( tr( "GEOM_MEASURE_ANGLE_IS" ) );
96   myGrp->LineEdit3->setReadOnly( true );
97   myGrp->PushButton1->setIcon( image1 );
98   myGrp->PushButton2->setIcon( image1 );
99   myGrp->LineEdit1->setReadOnly( true );
100   myGrp->LineEdit2->setReadOnly( true );
101
102   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
103   layout->setMargin( 0 ); layout->setSpacing( 6 );
104   layout->addWidget( myGrp );
105
106   // Help page reference
107   myHelpFileName = "using_measurement_tools_page.html#angle_anchor";
108
109   // Initialisation
110   Init();
111 }
112
113 //=================================================================================
114 // function : ~MeasureGUI_AngleDlg()
115 // purpose  : Destroys the object and frees any allocated resources
116 //=================================================================================
117 MeasureGUI_AngleDlg::~MeasureGUI_AngleDlg()
118 {
119 }
120
121 //=================================================================================
122 // function : Init()
123 // purpose  :
124 //=================================================================================
125 void MeasureGUI_AngleDlg::Init()
126 {
127   mySelBtn   = myGrp->PushButton1;
128   mySelEdit  = myGrp->LineEdit1;
129   mySelBtn2  = myGrp->PushButton2;
130   mySelEdit2 = myGrp->LineEdit2;
131
132   myEditCurrentArgument = mySelEdit;
133
134   connect( mySelEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
135   connect( mySelBtn2,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
136
137   globalSelection( GEOM_LINE );
138   MeasureGUI_Skeleton::Init();
139 }
140
141
142 //=================================================================================
143 // function : SelectionIntoArgument()
144 // purpose  : Called when selection has changed
145 //=================================================================================
146 void MeasureGUI_AngleDlg::SelectionIntoArgument()
147 {
148   Standard_Boolean testResult = Standard_False;
149   GEOM::GEOM_Object_var aSelectedObject =
150     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
151
152   if ( !testResult )
153     aSelectedObject = GEOM::GEOM_Object::_nil();
154
155   if ( myEditCurrentArgument == mySelEdit )
156     myObj = aSelectedObject;
157   else
158     myObj2 = aSelectedObject;
159
160   processObject();
161 }
162
163 //=================================================================================
164 // function : processObject()
165 // purpose  : Fill dialogs fields in accordance with myObj and myObj2
166 //=================================================================================
167 void MeasureGUI_AngleDlg::processObject()
168 {
169   myGrp->LineEdit1->setText( !myObj->_is_nil()  ? GEOMBase::GetName( myObj )  : "" );
170   myGrp->LineEdit2->setText( !myObj2->_is_nil() ? GEOMBase::GetName( myObj2 ) : "" );
171
172   double anAngle = 0.;
173   if ( getParameters( anAngle ) ) {
174     myGrp->LineEdit3->setText( DlgRef::PrintDoubleValue( anAngle ) );
175     redisplayPreview();
176   }
177   else {
178     myGrp->LineEdit3->setText( "" );
179     erasePreview();
180   }
181 }
182
183 //=================================================================================
184 // function : getParameters()
185 // purpose  : Get angle between objects
186 //=================================================================================
187 bool MeasureGUI_AngleDlg::getParameters( double& theAngle )
188 {
189   QString msg;
190   if ( isValid( msg ) ) {
191     try {
192       theAngle = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetAngle( myObj, myObj2 );
193     }
194     catch( const SALOME::SALOME_Exception& e ) {
195       SalomeApp_Tools::QtCatchCorbaException( e );
196       return false;
197     }
198     
199     bool isDone = getOperation()->IsDone();
200     if ( !isDone ) {
201       CORBA::String_var aMsg = getOperation()->GetErrorCode();
202       SUIT_MessageBox::warning( this, 
203                                 QObject::tr( "WRN_WARNING" ),
204                                 QObject::tr( aMsg.in() ) );
205     }
206     return isDone;
207   }
208
209   return false;
210 }
211
212 //=================================================================================
213 // function : SetEditCurrentArgument()
214 // purpose  :
215 //=================================================================================
216 void MeasureGUI_AngleDlg::SetEditCurrentArgument()
217 {
218   QPushButton* send = (QPushButton*)sender();
219
220   if ( send == mySelBtn ) {
221     mySelEdit->setFocus();
222     myEditCurrentArgument = mySelEdit;
223   }
224   else {
225     mySelEdit2->setFocus();
226     myEditCurrentArgument = mySelEdit2;
227   }
228
229   globalSelection( GEOM_LINE );
230   SelectionIntoArgument();
231 }
232
233 //=================================================================================
234 // function : LineEditReturnPressed()
235 // purpose  :
236 //=================================================================================
237 void MeasureGUI_AngleDlg::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 // function : buildPrs()
252 // purpose  :
253 //=================================================================================
254 SALOME_Prs* MeasureGUI_AngleDlg::buildPrs()
255 {
256   double anAngle = 0.;
257
258   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
259
260   if ( myObj->_is_nil() || myObj2->_is_nil() || !getParameters( anAngle ) ||
261        vw->getViewManager()->getType() != OCCViewer_Viewer::Type() )
262     return 0;
263   
264   if ( anAngle > Precision::Angular() ) {
265     try {
266 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
267       OCC_CATCH_SIGNALS;
268 #endif
269       TopoDS_Shape S1, S2;
270       if ( GEOMBase::GetShape( myObj , S1, TopAbs_EDGE ) &&
271            GEOMBase::GetShape( myObj2, S2, TopAbs_EDGE ) ) {
272         TopoDS_Edge anEdge1 = TopoDS::Edge( S1 );
273         TopoDS_Edge anEdge2 = TopoDS::Edge( S2 );
274         
275         // Build a plane for angle dimension presentation {P11, P12, P3}
276         TopoDS_Vertex V11, V12, V21, V22;
277         TopExp::Vertices( anEdge1, V11, V12 );
278         TopExp::Vertices( anEdge2, V21, V22 );
279         
280         gp_Pnt aP11 = BRep_Tool::Pnt( TopExp::FirstVertex( anEdge1 ) );
281         gp_Pnt aP12 = BRep_Tool::Pnt( TopExp::LastVertex ( anEdge1 ) );
282
283         gp_Pnt aP21 = BRep_Tool::Pnt( TopExp::FirstVertex( anEdge2 ) );
284         gp_Pnt aP22 = BRep_Tool::Pnt( TopExp::LastVertex ( anEdge2 ) );
285
286         //        *P3
287         //         \
288         //          \
289         //           *P22
290         //          /
291         //         /
292         // P11    /         P12
293         // *-----/----------*
294         //  \   /
295         //   \ /
296         //    *P21
297         gp_Pnt aP3 ( aP22.XYZ() + aP11.XYZ() - aP21.XYZ() );
298
299         gce_MakePln gce_MP( aP11, aP12, aP3 );
300         Handle(Geom_Plane) aPlane = new Geom_Plane( gce_MP.Value() );
301
302         // Build the angle dimension presentation
303         QString aLabel;
304         aLabel.sprintf( "%.1f", anAngle );
305
306         Handle(AIS_AngleDimension) anIO = new AIS_AngleDimension
307           ( anEdge1, anEdge2, aPlane, anAngle * PI180,
308             TCollection_ExtendedString( (Standard_CString)aLabel.toLatin1().data() ) );
309
310         SOCC_Prs* aPrs =
311           dynamic_cast<SOCC_Prs*>( ( (SOCC_Viewer*)( vw->getViewManager()->getViewModel() ) )->CreatePrs( 0 ) );
312
313         if ( aPrs )
314           aPrs->AddObject( anIO );
315
316         return aPrs;
317       }
318     }
319     catch( Standard_Failure ) {
320     }
321   }
322
323   return 0;
324 }
325
326 //=================================================================================
327 // function : isValid()
328 // purpose  :
329 //=================================================================================
330 bool MeasureGUI_AngleDlg::isValid( QString& msg )
331 {
332   return MeasureGUI_Skeleton::isValid( msg ) && !myObj2->_is_nil();
333 }