]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_LineDlg.cxx
Salome HOME
test integration for 0019050: EDF 521 GEOM SMESH : Improve selection process in the...
[modules/geom.git] / src / BasicGUI / BasicGUI_LineDlg.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   : BasicGUI_LineDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "BasicGUI_LineDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_Session.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS.hxx>
39 #include <TopExp.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42
43 #include <GEOMImpl_Types.hxx>
44
45 //=================================================================================
46 // class    : BasicGUI_LineDlg()
47 // purpose  : Constructs a BasicGUI_LineDlg 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 BasicGUI_LineDlg::BasicGUI_LineDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
53                                     bool modal, Qt::WindowFlags fl )
54   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
55 {
56   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_LINE_2P" ) ) );
57   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
58   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_LINE_2F" ) ) );
59
60   setWindowTitle( tr( "GEOM_LINE_TITLE" ) );
61  
62   /***************************************************************/
63   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_LINE" ) );
64   mainFrame()->RadioButton1->setIcon( image0 );
65   mainFrame()->RadioButton2->setIcon( image2 );
66   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
67   mainFrame()->RadioButton3->close();
68
69   GroupPoints = new DlgRef_2Sel( centralWidget() );
70
71   GroupPoints->GroupBox1->setTitle( tr( "GEOM_POINTS" ) );
72   GroupPoints->TextLabel1->setText( tr( "GEOM_POINT_I" ).arg( 1 ) );
73   GroupPoints->TextLabel2->setText( tr( "GEOM_POINT_I" ).arg( 2 ) );
74   GroupPoints->PushButton1->setIcon( image1 );
75   GroupPoints->PushButton2->setIcon( image1 );
76   GroupPoints->LineEdit1->setReadOnly( true );
77   GroupPoints->LineEdit2->setReadOnly( true );
78
79   GroupFaces = new DlgRef_2Sel( centralWidget() );
80   GroupFaces->GroupBox1->setTitle( tr( "GEOM_FACES" ) );
81   GroupFaces->TextLabel1->setText( tr( "GEOM_FACE_I" ).arg( "1" ) );
82   GroupFaces->TextLabel2->setText( tr( "GEOM_FACE_I" ).arg( "2" ) );
83   GroupFaces->PushButton1->setIcon( image1 );
84   GroupFaces->PushButton2->setIcon( image1 );
85   GroupFaces->LineEdit1->setReadOnly( true );
86   GroupFaces->LineEdit2->setReadOnly( true );
87
88   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
89   layout->setMargin( 0 ); layout->setSpacing( 6 );
90   layout->addWidget( GroupPoints );
91   layout->addWidget( GroupFaces );
92
93   /***************************************************************/
94
95   setHelpFileName( "create_line_page.html" );
96
97   Init();
98 }
99
100
101 //=================================================================================
102 // function : ~BasicGUI_LineDlg()
103 // purpose  : Destroys the object and frees any allocated resources
104 //=================================================================================
105 BasicGUI_LineDlg::~BasicGUI_LineDlg()
106 {  
107 }
108
109
110 //=================================================================================
111 // function : Init()
112 // purpose  :
113 //=================================================================================
114 void BasicGUI_LineDlg::Init()
115 {
116   /* init variables */
117   myEditCurrentArgument = GroupPoints->LineEdit1;
118   myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
119   globalSelection(); // close local contexts, if any
120   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
121   GroupPoints->PushButton1->setDown(true);
122   
123         
124   /* signals and slots connections */
125   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
126   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
127
128   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
129
130   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
131   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
132
133   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
134   connect( GroupPoints->PushButton2, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
135   connect( GroupFaces->PushButton1,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
136   connect( GroupFaces->PushButton2,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
137
138   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
139   connect( GroupPoints->LineEdit2,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
140   connect( GroupFaces->LineEdit1,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
141   connect( GroupFaces->LineEdit2,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
142
143   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
144            this, SLOT( SelectionIntoArgument() ) );
145   
146   initName( tr( "GEOM_LINE" ) );
147
148   ConstructorsClicked( 0 );
149 }
150
151
152 //=================================================================================
153 // function : ClickOnOk()
154 // purpose  :
155 //=================================================================================
156 void BasicGUI_LineDlg::ClickOnOk()
157 {
158   if ( ClickOnApply() )
159     ClickOnCancel();
160 }
161
162 //=================================================================================
163 // function : ClickOnApply()
164 // purpose  :
165 //=================================================================================
166 bool BasicGUI_LineDlg::ClickOnApply()
167 {
168   if ( !onAccept() )
169     return false;
170   
171   initName();
172
173   myEditCurrentArgument->setText( "" );
174   ConstructorsClicked( getConstructorId() );
175
176   return true;
177 }
178
179 //=================================================================================
180 // function : ConstructorsClicked()
181 // purpose  : Radio button management
182 //=================================================================================
183 void BasicGUI_LineDlg::ConstructorsClicked( int constructorId )
184 {  
185   switch ( constructorId ) {
186   case 0:
187     {
188       globalSelection(); // close local contexts, if any
189       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
190       
191       myEditCurrentArgument = GroupPoints->LineEdit1;
192       myEditCurrentArgument->setText( "" );
193       myPoint1 = GEOM::GEOM_Object::_nil();
194       myPoint2 = GEOM::GEOM_Object::_nil();
195       GroupPoints->PushButton1->setDown(true);
196       GroupPoints->PushButton2->setDown(false);
197       GroupPoints->show();
198       GroupFaces->hide();
199       break;
200     }
201   case 1:
202     {
203       globalSelection(); // close local contexts, if any
204       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
205
206       myEditCurrentArgument = GroupFaces->LineEdit1;
207       myEditCurrentArgument->setText("");
208       myFace1 = GEOM::GEOM_Object::_nil();
209       myFace2 = GEOM::GEOM_Object::_nil();
210       GroupFaces->PushButton1->setDown(true);
211       GroupFaces->PushButton2->setDown(false);
212       GroupPoints->hide();
213       GroupFaces->show();
214       break;
215     }
216   }
217
218   qApp->processEvents();
219   updateGeometry();
220   resize( minimumSize() );
221
222   SelectionIntoArgument();
223 }
224
225 //=================================================================================
226 // function : SelectionIntoArgument()
227 // purpose  : Called when selection as changed or other case
228 //=================================================================================
229 void BasicGUI_LineDlg::SelectionIntoArgument()
230 {
231   myEditCurrentArgument->setText( "" );
232
233   if ( IObjectCount() != 1 ) {
234     //    printf ( "IObjectCount() != 1 \n" );
235     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )      myPoint1 = GEOM::GEOM_Object::_nil();
236     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myPoint2 = GEOM::GEOM_Object::_nil();
237     else if ( myEditCurrentArgument == GroupFaces->LineEdit1 )  myFace1  = GEOM::GEOM_Object::_nil();
238     else if ( myEditCurrentArgument == GroupFaces->LineEdit2 )  myFace2  = GEOM::GEOM_Object::_nil();
239     displayPreview();
240     return;
241   }
242   //  printf ( "IObjectCount() == 1 \n" );
243   // nbSel == 1 
244   Standard_Boolean aRes = Standard_False;
245   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
246   if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
247     QString aName = GEOMBase::GetName( aSelectedObject );
248     
249     TopoDS_Shape aShape;
250     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
251       TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
252       if ( myEditCurrentArgument == GroupFaces->LineEdit1 ||
253            myEditCurrentArgument == GroupFaces->LineEdit2 )
254         aNeedType = TopAbs_FACE;
255
256       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
257       TColStd_IndexedMapOfInteger aMap;
258       aSelMgr->GetIndexes( firstIObject(), aMap );
259       if ( aMap.Extent() == 1 ) { // Local Selection
260         int anIndex = aMap( 1 );
261         if ( aNeedType == TopAbs_FACE )
262           aName += QString( ":face_%1" ).arg( anIndex );
263         else
264           aName += QString( ":vertex_%1" ).arg( anIndex );
265
266         //Find SubShape Object in Father
267         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
268
269         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
270           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
271           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
272         }
273         else {
274           aSelectedObject = aFindedObject; // get Object from study
275         }
276       }
277       else { // Global Selection
278         if ( aShape.ShapeType() != aNeedType ) {
279           aSelectedObject = GEOM::GEOM_Object::_nil();
280           aName = "";
281         }
282       }
283     }
284
285     myEditCurrentArgument->setText( aName );
286
287     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
288       myPoint1 = aSelectedObject;
289       if ( !myPoint1->_is_nil() && myPoint2->_is_nil() ) {
290         globalSelection(); // close local selection to clear it
291         localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
292         GroupPoints->PushButton2->click();      
293       }
294     }
295     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
296       myPoint2 = aSelectedObject;
297       if ( !myPoint2->_is_nil() && myPoint1->_is_nil() ) {
298         globalSelection(); // close local selection to clear it
299         localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
300         GroupPoints->PushButton1->click();      
301       }
302     }
303     else if ( myEditCurrentArgument == GroupFaces->LineEdit1 )  myFace1 = aSelectedObject;
304     else if ( myEditCurrentArgument == GroupFaces->LineEdit2 )  myFace2 = aSelectedObject;
305   }
306   
307   displayPreview();
308 }
309
310
311 //=================================================================================
312 // function : SetEditCurrentArgument()
313 // purpose  :
314 //=================================================================================
315 void BasicGUI_LineDlg::SetEditCurrentArgument()
316 {
317   QPushButton* send = (QPushButton*)sender();
318   if ( send == GroupPoints->PushButton1 ) {
319     myEditCurrentArgument = GroupPoints->LineEdit1;
320     GroupPoints->PushButton2->setDown(false);
321   }
322   else if ( send == GroupPoints->PushButton2 ) {
323     myEditCurrentArgument = GroupPoints->LineEdit2;
324     GroupPoints->PushButton1->setDown(false);
325   }
326   else if ( send == GroupFaces->PushButton1 ) {
327     myEditCurrentArgument = GroupFaces->LineEdit1;
328     GroupFaces->PushButton2->setDown(false);
329   }
330   else if ( send == GroupFaces->PushButton2 ) {
331     myEditCurrentArgument = GroupFaces->LineEdit2;
332     GroupFaces->PushButton1->setDown(false);
333   }
334   myEditCurrentArgument->setFocus();
335   SelectionIntoArgument();
336   send->setDown(true);
337 }
338
339
340 //=================================================================================
341 // function : LineEditReturnPressed()
342 // purpose  :
343 //=================================================================================
344 void BasicGUI_LineDlg::LineEditReturnPressed()
345 {
346   QLineEdit* send = (QLineEdit*)sender();
347   if      ( send == GroupPoints->LineEdit1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
348   else if ( send == GroupPoints->LineEdit2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
349   else if ( send == GroupFaces->LineEdit1 )  myEditCurrentArgument = GroupFaces->LineEdit1;
350   else if ( send == GroupFaces->LineEdit2 )  myEditCurrentArgument = GroupFaces->LineEdit2;
351   else return;
352   GEOMBase_Skeleton::LineEditReturnPressed();
353 }
354
355
356 //=================================================================================
357 // function : ActivateThisDialog()
358 // purpose  :
359 //=================================================================================
360 void BasicGUI_LineDlg::ActivateThisDialog()
361 {
362   GEOMBase_Skeleton::ActivateThisDialog();
363   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
364            this, SLOT( SelectionIntoArgument() ) );
365
366   ConstructorsClicked( getConstructorId() );
367 }
368
369 //=================================================================================
370 // function : DeactivateActiveDialog()
371 // purpose  : public slot to deactivate if active
372 //=================================================================================
373 void BasicGUI_LineDlg::DeactivateActiveDialog()
374 {
375   GEOMBase_Skeleton::DeactivateActiveDialog();
376 }
377
378 //=================================================================================
379 // function : enterEvent()
380 // purpose  :
381 //=================================================================================
382 void BasicGUI_LineDlg::enterEvent( QEvent* )
383 {
384   if ( !mainFrame()->GroupConstructors->isEnabled() )
385     ActivateThisDialog();
386 }
387
388 //=================================================================================
389 // function : createOperation
390 // purpose  :
391 //=================================================================================
392 GEOM::GEOM_IOperations_ptr BasicGUI_LineDlg::createOperation()
393 {
394   return myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
395 }
396
397 //=================================================================================
398 // function : isValid
399 // purpose  :
400 //=================================================================================
401 bool BasicGUI_LineDlg::isValid( QString& msg )
402 {
403   const int id = getConstructorId();
404   if ( id == 0 )
405     return !myPoint1->_is_nil() && !myPoint2->_is_nil();
406   else if ( id == 1 )
407     return !myFace1->_is_nil() && !myFace2->_is_nil();
408   return false;
409 }
410
411 //=================================================================================
412 // function : execute
413 // purpose  :
414 //=================================================================================
415 bool BasicGUI_LineDlg::execute( ObjectList& objects )
416 {
417   bool res = false;
418   GEOM::GEOM_Object_var anObj;
419   switch ( getConstructorId() ) {
420   case 0 :
421     anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeLineTwoPnt( myPoint1, myPoint2 );
422     res = true;
423     break;
424   case 1 :
425     anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeLineTwoFaces( myFace1, myFace2 );
426     res = true;
427     break;
428   }
429   
430   if ( !anObj->_is_nil() )
431     objects.push_back( anObj._retn() );
432
433   return res;
434 }
435
436 //=================================================================================
437 // function : addSubshapeToStudy
438 // purpose  : virtual method to add new SubObjects if local selection
439 //=================================================================================
440 void BasicGUI_LineDlg::addSubshapesToStudy()
441 {
442   QMap<QString, GEOM::GEOM_Object_var> objMap;
443   switch ( getConstructorId() ) {
444   case 0 :
445     objMap[GroupPoints->LineEdit1->text()] = myPoint1;
446     objMap[GroupPoints->LineEdit2->text()] = myPoint2;
447     break;
448   case 1 :
449     objMap[GroupFaces->LineEdit1->text()] = myFace1;
450     objMap[GroupFaces->LineEdit2->text()] = myFace2;
451     break;
452   }
453   addSubshapesToFather( objMap );
454 }