]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_LineDlg.cxx
Salome HOME
Test Integration: 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       GroupPoints->LineEdit2->setText( "" );
194       myPoint1 = GEOM::GEOM_Object::_nil();
195       myPoint2 = GEOM::GEOM_Object::_nil();
196       GroupPoints->PushButton1->setDown(true);
197       GroupPoints->PushButton2->setDown(false);
198       GroupPoints->show();
199       GroupFaces->hide();
200       break;
201     }
202   case 1:
203     {
204       globalSelection(); // close local contexts, if any
205       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
206
207       myEditCurrentArgument = GroupFaces->LineEdit1;
208       myEditCurrentArgument->setText("");
209       myFace1 = GEOM::GEOM_Object::_nil();
210       myFace2 = GEOM::GEOM_Object::_nil();
211       GroupFaces->PushButton1->setDown(true);
212       GroupFaces->PushButton2->setDown(false);
213       GroupPoints->hide();
214       GroupFaces->show();
215       break;
216     }
217   }
218
219   qApp->processEvents();
220   updateGeometry();
221   resize( minimumSize() );
222
223   SelectionIntoArgument();
224 }
225
226 //=================================================================================
227 // function : SelectionIntoArgument()
228 // purpose  : Called when selection as changed or other case
229 //=================================================================================
230 void BasicGUI_LineDlg::SelectionIntoArgument()
231 {
232   myEditCurrentArgument->setText( "" );
233
234   if ( IObjectCount() != 1 ) {
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
243   Standard_Boolean aRes = Standard_False;
244   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
245   if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
246     QString aName = GEOMBase::GetName( aSelectedObject );
247     
248     TopoDS_Shape aShape;
249     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
250       TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
251       if ( myEditCurrentArgument == GroupFaces->LineEdit1 ||
252            myEditCurrentArgument == GroupFaces->LineEdit2 )
253         aNeedType = TopAbs_FACE;
254
255       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
256       TColStd_IndexedMapOfInteger aMap;
257       aSelMgr->GetIndexes( firstIObject(), aMap );
258       if ( aMap.Extent() == 1 ) { // Local Selection
259         int anIndex = aMap( 1 );
260         if ( aNeedType == TopAbs_FACE )
261           aName += QString( ":face_%1" ).arg( anIndex );
262         else
263           aName += QString( ":vertex_%1" ).arg( anIndex );
264
265         //Find SubShape Object in Father
266         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
267
268         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
269           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
270           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
271         }
272         else {
273           aSelectedObject = aFindedObject; // get Object from study
274         }
275       }
276       else { // Global Selection
277         if ( aShape.ShapeType() != aNeedType ) {
278           aSelectedObject = GEOM::GEOM_Object::_nil();
279           aName = "";
280         }
281       }
282     }
283
284     myEditCurrentArgument->setText( aName );
285
286     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
287       myPoint1 = aSelectedObject;
288       if ( !myPoint1->_is_nil() && myPoint2->_is_nil() ) {
289         globalSelection(); // close local selection to clear it
290         localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
291         GroupPoints->PushButton2->click();      
292       }
293     }
294     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
295       myPoint2 = aSelectedObject;
296       if ( !myPoint2->_is_nil() && myPoint1->_is_nil() ) {
297         globalSelection(); // close local selection to clear it
298         localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
299         GroupPoints->PushButton1->click();      
300       }
301     }
302     else if ( myEditCurrentArgument == GroupFaces->LineEdit1 )  myFace1 = aSelectedObject;
303     else if ( myEditCurrentArgument == GroupFaces->LineEdit2 )  myFace2 = aSelectedObject;
304   }
305   
306   displayPreview();
307 }
308
309
310 //=================================================================================
311 // function : SetEditCurrentArgument()
312 // purpose  :
313 //=================================================================================
314 void BasicGUI_LineDlg::SetEditCurrentArgument()
315 {
316   if ( IObjectCount() != 0 ) {
317     globalSelection(); // close local selection to clear it
318     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
319   }
320   QPushButton* send = (QPushButton*)sender();
321   if ( send == GroupPoints->PushButton1 ) {
322     myEditCurrentArgument = GroupPoints->LineEdit1;
323     GroupPoints->PushButton2->setDown(false);
324   }
325   else if ( send == GroupPoints->PushButton2 ) {
326     myEditCurrentArgument = GroupPoints->LineEdit2;
327     GroupPoints->PushButton1->setDown(false);
328   }
329   else if ( send == GroupFaces->PushButton1 ) {
330     myEditCurrentArgument = GroupFaces->LineEdit1;
331     GroupFaces->PushButton2->setDown(false);
332   }
333   else if ( send == GroupFaces->PushButton2 ) {
334     myEditCurrentArgument = GroupFaces->LineEdit2;
335     GroupFaces->PushButton1->setDown(false);
336   }
337   myEditCurrentArgument->setFocus();
338   //  SelectionIntoArgument();
339   send->setDown(true);
340   displayPreview();
341 }
342
343
344 //=================================================================================
345 // function : LineEditReturnPressed()
346 // purpose  :
347 //=================================================================================
348 void BasicGUI_LineDlg::LineEditReturnPressed()
349 {
350   QLineEdit* send = (QLineEdit*)sender();
351   if      ( send == GroupPoints->LineEdit1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
352   else if ( send == GroupPoints->LineEdit2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
353   else if ( send == GroupFaces->LineEdit1 )  myEditCurrentArgument = GroupFaces->LineEdit1;
354   else if ( send == GroupFaces->LineEdit2 )  myEditCurrentArgument = GroupFaces->LineEdit2;
355   else return;
356   GEOMBase_Skeleton::LineEditReturnPressed();
357 }
358
359
360 //=================================================================================
361 // function : ActivateThisDialog()
362 // purpose  :
363 //=================================================================================
364 void BasicGUI_LineDlg::ActivateThisDialog()
365 {
366   GEOMBase_Skeleton::ActivateThisDialog();
367   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
368            this, SLOT( SelectionIntoArgument() ) );
369
370   ConstructorsClicked( getConstructorId() );
371 }
372
373 //=================================================================================
374 // function : DeactivateActiveDialog()
375 // purpose  : public slot to deactivate if active
376 //=================================================================================
377 void BasicGUI_LineDlg::DeactivateActiveDialog()
378 {
379   GEOMBase_Skeleton::DeactivateActiveDialog();
380 }
381
382 //=================================================================================
383 // function : enterEvent()
384 // purpose  :
385 //=================================================================================
386 void BasicGUI_LineDlg::enterEvent( QEvent* )
387 {
388   if ( !mainFrame()->GroupConstructors->isEnabled() )
389     ActivateThisDialog();
390 }
391
392 //=================================================================================
393 // function : createOperation
394 // purpose  :
395 //=================================================================================
396 GEOM::GEOM_IOperations_ptr BasicGUI_LineDlg::createOperation()
397 {
398   return myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
399 }
400
401 //=================================================================================
402 // function : isValid
403 // purpose  :
404 //=================================================================================
405 bool BasicGUI_LineDlg::isValid( QString& msg )
406 {
407   const int id = getConstructorId();
408   if ( id == 0 )
409     return !myPoint1->_is_nil() && !myPoint2->_is_nil();
410   else if ( id == 1 )
411     return !myFace1->_is_nil() && !myFace2->_is_nil();
412   return false;
413 }
414
415 //=================================================================================
416 // function : execute
417 // purpose  :
418 //=================================================================================
419 bool BasicGUI_LineDlg::execute( ObjectList& objects )
420 {
421   bool res = false;
422   GEOM::GEOM_Object_var anObj;
423   switch ( getConstructorId() ) {
424   case 0 :
425     anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeLineTwoPnt( myPoint1, myPoint2 );
426     res = true;
427     break;
428   case 1 :
429     anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeLineTwoFaces( myFace1, myFace2 );
430     res = true;
431     break;
432   }
433   
434   if ( !anObj->_is_nil() )
435     objects.push_back( anObj._retn() );
436
437   return res;
438 }
439
440 //=================================================================================
441 // function : addSubshapeToStudy
442 // purpose  : virtual method to add new SubObjects if local selection
443 //=================================================================================
444 void BasicGUI_LineDlg::addSubshapesToStudy()
445 {
446   QMap<QString, GEOM::GEOM_Object_var> objMap;
447   switch ( getConstructorId() ) {
448   case 0 :
449     objMap[GroupPoints->LineEdit1->text()] = myPoint1;
450     objMap[GroupPoints->LineEdit2->text()] = myPoint2;
451     break;
452   case 1 :
453     objMap[GroupFaces->LineEdit1->text()] = myFace1;
454     objMap[GroupFaces->LineEdit2->text()] = myFace2;
455     break;
456   }
457   addSubshapesToFather( objMap );
458 }