Salome HOME
Bug IPAL19097 NPAL16768 is not implemented for "Normal to a face"
[modules/geom.git] / src / MeasureGUI / MeasureGUI_NormaleDlg.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_NormaleDlg.cxx
23 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
24 //
25
26 #include "MeasureGUI_NormaleDlg.h"
27
28 #include <DlgRef.h>
29 #include <GEOMBase.h>
30 #include <GeometryGUI.h>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <GEOMImpl_Types.hxx>
38
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS.hxx>
41 #include <TopExp.hxx>
42 #include <TColStd_IndexedMapOfInteger.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
44
45 //=================================================================================
46 // class    : MeasureGUI_NormaleDlg()
47 // purpose  : Constructs a MeasureGUI_NormaleDlg which is a child of 'parent', with the
48 //            name 'name' and widget flags set to 'f'.
49 //            The dialog will by default be modeless, unless you set 'modal' to
50 //            TRUE to construct a modal dialog.
51 //=================================================================================
52 MeasureGUI_NormaleDlg::MeasureGUI_NormaleDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
53   : GEOMBase_Skeleton( theGeometryGUI, parent, false )
54 {
55   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
56   QPixmap image0( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_NORMALE" ) ) );
57   QPixmap image1( aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
58
59   setWindowTitle( tr( "GEOM_NORMALE_TITLE" ) );
60
61   /***************************************************************/
62   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_NORMALE" ) );
63   mainFrame()->RadioButton1->setIcon( image0 );
64   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
65   mainFrame()->RadioButton2->close();
66   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
67   mainFrame()->RadioButton3->close();
68
69   GroupArgs = new DlgRef_2Sel ( centralWidget() );
70   GroupArgs->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
71
72   GroupArgs->TextLabel1->setText( tr( "GEOM_FACE" ) );
73   GroupArgs->TextLabel2->setText( tr( "GEOM_POINT" ) );
74
75   GroupArgs->PushButton1->setIcon( image1 );
76   GroupArgs->PushButton2->setIcon( image1 );
77
78   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
79   layout->setMargin( 0 ); layout->setSpacing( 6 );
80   layout->addWidget( GroupArgs );
81
82   /***************************************************************/
83
84   setHelpFileName("using_measurement_tools_page.html#normale_anchor");
85
86   Init();
87 }
88
89 //=================================================================================
90 // function : ~MeasureGUI_NormaleDlg()
91 // purpose  : Destroys the object and frees any allocated resources
92 //=================================================================================
93 MeasureGUI_NormaleDlg::~MeasureGUI_NormaleDlg()
94 {
95 }
96
97 //=================================================================================
98 // function : Init()
99 // purpose  :
100 //=================================================================================
101 void MeasureGUI_NormaleDlg::Init()
102 {
103   /* init variables */
104   GroupArgs->LineEdit1->setReadOnly( true );
105   GroupArgs->LineEdit2->setReadOnly( true );
106
107   myFace = GEOM::GEOM_Object::_nil();
108   myPoint = GEOM::GEOM_Object::_nil();
109
110   myEditCurrentArgument = GroupArgs->LineEdit1;
111   globalSelection( GEOM_FACE ); // to close previous local selection
112   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
113
114   /* signals and slots connections */
115   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
116   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
117
118   connect( GroupArgs->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
119   connect( GroupArgs->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
120
121   connect( GroupArgs->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
122   connect( GroupArgs->LineEdit2,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
123
124   connect( myGeomGUI->getApp()->selectionMgr(),
125            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
126
127   initName(tr("GEOM_VECTOR_NORMALE"));
128
129   SelectionIntoArgument();
130 }
131
132 //=================================================================================
133 // function : ClickOnOk()
134 // purpose  :
135 //=================================================================================
136 void MeasureGUI_NormaleDlg::ClickOnOk()
137 {
138   if ( ClickOnApply() )
139     ClickOnCancel();
140 }
141
142 //=================================================================================
143 // function : ClickOnApply()
144 // purpose  :
145 //=================================================================================
146 bool MeasureGUI_NormaleDlg::ClickOnApply()
147 {
148   if ( !onAccept() )
149     return false;
150
151   initName();
152   return true;
153 }
154
155 //=================================================================================
156 // function : SelectionIntoArgument()
157 // purpose  : Called when selection as changed or other case
158 //=================================================================================
159 void MeasureGUI_NormaleDlg::SelectionIntoArgument()
160 {
161   erasePreview();
162   myEditCurrentArgument->setText( "" );
163
164   if ( myEditCurrentArgument == GroupArgs->LineEdit1 ) {
165     myFace = GEOM::GEOM_Object::_nil();
166   }
167   else if ( myEditCurrentArgument == GroupArgs->LineEdit2 ) {
168     myPoint = GEOM::GEOM_Object::_nil();
169   }
170   
171   if ( IObjectCount() != 1 )
172     return;
173
174   // nbSel == 1
175   Standard_Boolean testResult = Standard_False;
176   GEOM::GEOM_Object_var aSelectedObject =
177     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
178
179   if ( !testResult )
180     return;
181
182   QString aName = GEOMBase::GetName( aSelectedObject );
183
184   if ( myEditCurrentArgument == GroupArgs->LineEdit1 ) {
185     TopoDS_Shape aShape;
186     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
187     {
188       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
189       TColStd_IndexedMapOfInteger aMap;
190       aSelMgr->GetIndexes( firstIObject(), aMap );
191       if ( aMap.Extent() == 1 ) // Local Selection
192       {
193         GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
194         int anIndex = aMap( 1 );
195         aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
196         aName += QString( ":face_%1" ).arg( anIndex );
197       }
198       else // Global Selection
199       {
200         if ( aShape.ShapeType() != TopAbs_FACE ) {
201           aSelectedObject = GEOM::GEOM_Object::_nil();
202           aName = "";
203         }
204       }
205     }
206     myFace = aSelectedObject;
207   }
208   else if ( myEditCurrentArgument == GroupArgs->LineEdit2 ) {
209     TopoDS_Shape aShape;
210     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
211     {
212       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
213       TColStd_IndexedMapOfInteger aMap;
214       aSelMgr->GetIndexes( firstIObject(), aMap );
215       if ( aMap.Extent() == 1 ) // Local Selection
216       {
217         GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
218         int anIndex = aMap( 1 );
219         aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
220         aName += QString( ":vertex_%1" ).arg( anIndex );
221       }
222       else // Global Selection
223       {
224         if ( aShape.ShapeType() != TopAbs_VERTEX ) {
225           aSelectedObject = GEOM::GEOM_Object::_nil();
226           aName = "";
227         }
228       }
229     }
230     myPoint = aSelectedObject;
231   }
232
233   myEditCurrentArgument->setText( aName );
234
235   displayPreview();
236 }
237
238 //=================================================================================
239 // function : LineEditReturnPressed()
240 // purpose  :
241 //=================================================================================
242 void MeasureGUI_NormaleDlg::LineEditReturnPressed()
243 {
244   QLineEdit* send = (QLineEdit*)sender();
245   if ( send == GroupArgs->LineEdit1 ||
246        send == GroupArgs->LineEdit2 ) {
247     myEditCurrentArgument = send;
248     GEOMBase_Skeleton::LineEditReturnPressed();
249   }
250 }
251
252 //=================================================================================
253 // function : SetEditCurrentArgument()
254 // purpose  :
255 //=================================================================================
256 void MeasureGUI_NormaleDlg::SetEditCurrentArgument()
257 {
258   QPushButton* send = (QPushButton*)sender();
259   globalSelection( GEOM_FACE );
260
261   if ( send == GroupArgs->PushButton1 ) {
262     myEditCurrentArgument = GroupArgs->LineEdit1;
263     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
264   }
265   else if ( send == GroupArgs->PushButton2 ) {
266     myEditCurrentArgument = GroupArgs->LineEdit2;
267     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
268   }
269
270   myEditCurrentArgument->setFocus();
271   SelectionIntoArgument();
272 }
273
274 //=================================================================================
275 // function : ActivateThisDialog()
276 // purpose  :
277 //=================================================================================
278 void MeasureGUI_NormaleDlg::ActivateThisDialog()
279 {
280   GEOMBase_Skeleton::ActivateThisDialog();
281
282   SelectionIntoArgument();
283 }
284
285 //=================================================================================
286 // function : enterEvent()
287 // purpose  :
288 //=================================================================================
289 void MeasureGUI_NormaleDlg::enterEvent( QEvent* e )
290 {
291   if ( !mainFrame()->GroupConstructors->isEnabled() )
292     ActivateThisDialog();
293 }
294
295 //=================================================================================
296 // function : createOperation
297 // purpose  :
298 //=================================================================================
299 GEOM::GEOM_IOperations_ptr MeasureGUI_NormaleDlg::createOperation()
300 {
301   return getGeomEngine()->GetIMeasureOperations( getStudyId() );
302 }
303
304 //=================================================================================
305 // function : isValid
306 // purpose  :
307 //=================================================================================
308 bool MeasureGUI_NormaleDlg::isValid( QString& )
309 {
310   //return !CORBA::is_nil(myFace) && !CORBA::is_nil(myPoint);
311   return !CORBA::is_nil( myFace );
312 }
313
314 //=================================================================================
315 // function : execute
316 // purpose  :
317 //=================================================================================
318 bool MeasureGUI_NormaleDlg::execute( ObjectList& objects )
319 {
320   GEOM::GEOM_Object_var anObj =
321     GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetNormal( myFace, myPoint );
322
323   if ( !anObj->_is_nil() )
324     objects.push_back( anObj._retn() );
325
326   return true;
327 }