Salome HOME
0019050: EDF 521 (For GEOM/BasicGUI): Improve selection process in the dialogs
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_FaceDlg.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   : PrimitiveGUI_FaceDlg.cxx
23 // Author : Dmitry Matveitchev, OCN.
24 //
25
26 #include "PrimitiveGUI_FaceDlg.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    : PrimitiveGUI_FaceDlg()
47 // purpose  : Constructs a PrimitiveGUI_FaceDlg 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 PrimitiveGUI_FaceDlg::PrimitiveGUI_FaceDlg( 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_FACE_OBJ_HW" ) ) );
57   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
58   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_FACE_HW" ) ) );
59
60   setWindowTitle( tr( "GEOM_FACE_TITLE" ) );
61  
62   /***************************************************************/
63   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_FACE" ) );
64   mainFrame()->RadioButton1->setIcon( image2 );
65   mainFrame()->RadioButton2->setIcon( image0 );
66   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
67   mainFrame()->RadioButton3->close();
68
69   GroupPlane = new DlgRef_1Sel2Spin( centralWidget() );
70   GroupPlane->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
71   GroupPlane->TextLabel1->setText( tr( "GEOM_EDGE" ) );
72   GroupPlane->TextLabel2->setText( tr( "GEOM_HEIGHT" ) );
73   GroupPlane->TextLabel3->setText( tr( "GEOM_WIDTH" ) );
74   GroupPlane->PushButton1->setIcon( image1 );
75   GroupPlane->LineEdit1->setReadOnly( true );
76
77   GroupType = new DlgRef_3Radio( centralWidget() );
78   GroupType->GroupBox1->setTitle( tr( "GEOM_OBJECT_TYPE" ) );
79   GroupType->RadioButton1->setText( tr( "GEOM_EDGE" ) );
80   GroupType->RadioButton2->setText( tr( "GEOM_FACE" ) );
81   GroupType->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
82   GroupType->RadioButton3->close();
83
84   GroupDimensions = new DlgRef_2Spin( centralWidget() );
85   GroupDimensions->GroupBox1->setTitle( tr( "GEOM_BOX_OBJ" ) );
86   GroupDimensions->TextLabel1->setText( tr( "GEOM_HEIGHT" ) );
87   GroupDimensions->TextLabel2->setText( tr( "GEOM_WIDTH" ) );
88
89   GroupOrientation = new DlgRef_3Radio( centralWidget() );
90
91   GroupOrientation->GroupBox1->setTitle( tr( "GEOM_ORIENTATION" ) );
92   GroupOrientation->RadioButton1->setText( tr( "GEOM_WPLANE_OXY" ) );
93   GroupOrientation->RadioButton2->setText( tr( "GEOM_WPLANE_OYZ" ) );
94   GroupOrientation->RadioButton3->setText( tr( "GEOM_WPLANE_OZX" ) );
95
96   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
97   layout->setMargin( 0 ); layout->setSpacing( 6 );
98   layout->addWidget( GroupType );
99   layout->addWidget( GroupPlane );
100   layout->addWidget( GroupDimensions );
101   layout->addWidget( GroupOrientation );
102
103   /***************************************************************/
104
105   setHelpFileName( "create_squareface_page.html" );
106
107   Init();
108 }
109
110
111 //=================================================================================
112 // function : ~PrimitiveGUI_FaceDlg()
113 // purpose  : Destroys the object and frees any allocated resources
114 //=================================================================================
115 PrimitiveGUI_FaceDlg::~PrimitiveGUI_FaceDlg()
116 {  
117 }
118
119
120 //=================================================================================
121 // function : Init()
122 // purpose  :
123 //=================================================================================
124 void PrimitiveGUI_FaceDlg::Init()
125 {
126   /* init variables */
127   myEditCurrentArgument = GroupPlane->LineEdit1;
128   GroupType->RadioButton1->setChecked(true);
129   myEdge = GEOM::GEOM_Object::_nil();
130   myFace = GEOM::GEOM_Object::_nil();
131   globalSelection(); // close local contexts, if any
132   //  localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
133
134   myOrientationType = 1;
135
136   /* Get setting of step value from file configuration */
137   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
138   double aStep = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
139
140   double aDefaultSize = 100.0;
141   /* min, max, step and decimals for spin boxes */
142   initSpinBox( GroupPlane->SpinBox_DX, 0.001, COORD_MAX, aStep, 3 ); // VSR:TODO : DBL_DIGITS_DISPLAY
143   GroupPlane->SpinBox_DX->setValue( aDefaultSize );
144   initSpinBox( GroupPlane->SpinBox_DY, 0.001, COORD_MAX, aStep, 3 ); // VSR:TODO : DBL_DIGITS_DISPLAY
145   GroupPlane->SpinBox_DY->setValue( aDefaultSize );
146
147   initSpinBox( GroupDimensions->SpinBox_DX, 0.001, COORD_MAX, aStep, 3 ); // VSR:TODO : DBL_DIGITS_DISPLAY
148   GroupDimensions->SpinBox_DX->setValue( aDefaultSize );
149   initSpinBox( GroupDimensions->SpinBox_DY, 0.001, COORD_MAX, aStep, 3 ); // VSR:TODO : DBL_DIGITS_DISPLAY
150   GroupDimensions->SpinBox_DY->setValue( aDefaultSize );
151
152         
153   /* signals and slots connections */
154   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
155   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
156   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
157
158   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
159
160   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
161   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
162
163   connect( GroupPlane->PushButton1,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
164   connect( GroupPlane->LineEdit1,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
165   connect( GroupPlane->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
166   connect( GroupPlane->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
167
168   connect( GroupDimensions->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
169   connect( GroupDimensions->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
170
171   connect( GroupOrientation->RadioButton1, SIGNAL( clicked() ), this, SLOT( RadioButtonClicked() ) );
172   connect( GroupOrientation->RadioButton2, SIGNAL( clicked() ), this, SLOT( RadioButtonClicked() ) );
173   connect( GroupOrientation->RadioButton3, SIGNAL( clicked() ), this, SLOT( RadioButtonClicked() ) );
174
175   connect( GroupType->RadioButton1, SIGNAL( clicked() ), this, SLOT( TypeButtonClicked() ) );
176   connect( GroupType->RadioButton2, SIGNAL( clicked() ), this, SLOT( TypeButtonClicked() ) );
177
178   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
179            this, SLOT( SelectionIntoArgument() ) );
180   
181   initName( tr( "GEOM_FACE" ) );
182
183   ConstructorsClicked( 0 );
184 }
185
186 //=================================================================================
187 // function : SetDoubleSpinBoxStep()
188 // purpose  : Double spin box management
189 //=================================================================================
190 void PrimitiveGUI_FaceDlg::SetDoubleSpinBoxStep( double step )
191 {
192   GroupPlane->SpinBox_DX->setSingleStep(step);
193   GroupPlane->SpinBox_DY->setSingleStep(step);
194 }
195
196 //=================================================================================
197 // function : ValueChangedInSpinBox()
198 // purpose  :
199 //=================================================================================
200 void PrimitiveGUI_FaceDlg::ValueChangedInSpinBox( double newValue )
201 {
202   displayPreview();
203 }
204
205 //=================================================================================
206 // function : RadioBittonClicked()
207 // purpose  : Radio button management
208 //=================================================================================
209 void PrimitiveGUI_FaceDlg::RadioButtonClicked()
210 {
211   if ( GroupOrientation->RadioButton1->isChecked() )
212     myOrientationType = 1;
213   else if ( GroupOrientation->RadioButton2->isChecked() )
214     myOrientationType = 2;
215   else if ( GroupOrientation->RadioButton3->isChecked() )
216     myOrientationType = 3;
217   displayPreview();
218 }
219
220 //=================================================================================
221 // function : TypeBittonClicked()
222 // purpose  : Radio button management
223 //=================================================================================
224 void PrimitiveGUI_FaceDlg::TypeButtonClicked()
225 {
226   if ( GroupType->RadioButton1->isChecked() ) {
227     globalSelection(); // close local contexts, if any
228     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
229     GroupPlane->TextLabel1->setText( tr( "GEOM_EDGE" ) );
230   }
231   else if ( GroupType->RadioButton2->isChecked() ) {
232     globalSelection(); // close local contexts, if any
233     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
234     GroupPlane->TextLabel1->setText( tr( "GEOM_FACE" ) );
235   }
236 }
237
238 //=================================================================================
239 // function : ClickOnOk()
240 // purpose  :
241 //=================================================================================
242 void PrimitiveGUI_FaceDlg::ClickOnOk()
243 {
244   if ( ClickOnApply() )
245     ClickOnCancel();
246 }
247
248 //=================================================================================
249 // function : ClickOnApply()
250 // purpose  :
251 //=================================================================================
252 bool PrimitiveGUI_FaceDlg::ClickOnApply()
253 {
254   if ( !onAccept() )
255     return false;
256   
257   initName();
258
259   myEditCurrentArgument->setText( "" );
260   ConstructorsClicked( getConstructorId() );
261
262   return true;
263 }
264
265 //=================================================================================
266 // function : ConstructorsClicked()
267 // purpose  : Radio button management
268 //=================================================================================
269 void PrimitiveGUI_FaceDlg::ConstructorsClicked( int constructorId )
270 {  
271   switch ( constructorId ) {
272   case 0:
273     {
274       globalSelection(); // close local contexts, if any
275       GroupPlane->hide();
276       GroupDimensions->show();
277       GroupOrientation->show();
278       GroupType->hide();
279       GroupOrientation->RadioButton1->setChecked( true );
280       myOrientationType = 1;
281       break;
282     }
283   case 1:
284     {
285       globalSelection(); // close local contexts, if any
286       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
287       myEditCurrentArgument = GroupPlane->LineEdit1;
288       myEditCurrentArgument->setText("");
289       myEdge = GEOM::GEOM_Object::_nil();
290       myFace = GEOM::GEOM_Object::_nil();
291       GroupType->RadioButton1->setChecked( true );
292       TypeButtonClicked();
293       GroupDimensions->hide();
294       GroupOrientation->hide();
295       GroupType->show();
296       GroupPlane->show();
297       break;
298     }
299   }
300
301   qApp->processEvents();
302   updateGeometry();
303   resize( minimumSize() );
304   SelectionIntoArgument();
305   displayPreview();
306 }
307
308 //=================================================================================
309 // function : SelectionIntoArgument()
310 // purpose  : Called when selection as changed or other case
311 //=================================================================================
312 void PrimitiveGUI_FaceDlg::SelectionIntoArgument()
313 {
314   if (getConstructorId() == 0) {
315     displayPreview();
316     return;
317   }
318
319   myEditCurrentArgument->setText( "" );
320
321   if ( IObjectCount() != 1 ) {
322     if ( myEditCurrentArgument == GroupPlane->LineEdit1 )  myEdge = GEOM::GEOM_Object::_nil();
323     return;
324   }
325
326   // nbSel == 1 
327   Standard_Boolean aRes = Standard_False;
328   TopAbs_ShapeEnum aNeedType = TopAbs_EDGE;
329   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
330   if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
331     QString aName = GEOMBase::GetName( aSelectedObject );
332     
333     TopoDS_Shape aShape;
334     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
335       if (GroupType->RadioButton2->isChecked())
336         aNeedType = TopAbs_FACE;
337
338       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
339       TColStd_IndexedMapOfInteger aMap;
340       aSelMgr->GetIndexes( firstIObject(), aMap );
341       if ( aMap.Extent() == 1 ) { // Local Selection
342         int anIndex = aMap( 1 );
343         if ( aNeedType == TopAbs_EDGE )
344           aName += QString( ":edge_%1" ).arg( anIndex );
345         else
346           aName += QString( ":face_%1" ).arg( anIndex );
347
348         //Find SubShape Object in Father
349         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
350
351         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
352           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
353           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
354         }
355         else
356           aSelectedObject = aFindedObject; // get Object from study
357       }
358       else { // Global Selection
359         if ( aShape.ShapeType() != aNeedType ) {
360           aSelectedObject = GEOM::GEOM_Object::_nil();
361           aName = "";
362         }
363       }
364     }
365
366     myEditCurrentArgument->setText( aName );
367     if ( myEditCurrentArgument == GroupPlane->LineEdit1 && aNeedType == TopAbs_EDGE )
368       myEdge = aSelectedObject;
369     else if ( myEditCurrentArgument == GroupPlane->LineEdit1 && aNeedType == TopAbs_FACE )
370       myFace = aSelectedObject;
371   }
372   displayPreview();
373 }
374
375
376 //=================================================================================
377 // function : SetEditCurrentArgument()
378 // purpose  :
379 //=================================================================================
380 void PrimitiveGUI_FaceDlg::SetEditCurrentArgument()
381 {
382   QPushButton* send = (QPushButton*)sender();
383
384   globalSelection(); // close previous local contexts
385   if ( send == GroupPlane->PushButton1 ) {
386     myEditCurrentArgument = GroupPlane->LineEdit1;
387     if (GroupType->RadioButton1->isChecked())
388       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
389     else if (GroupType->RadioButton1->isChecked())
390       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
391   }
392
393   myEditCurrentArgument->setFocus();
394   SelectionIntoArgument();
395 }
396
397
398 //=================================================================================
399 // function : LineEditReturnPressed()
400 // purpose  :
401 //=================================================================================
402 void PrimitiveGUI_FaceDlg::LineEditReturnPressed()
403 {
404   QLineEdit* send = (QLineEdit*)sender();
405   if ( send == GroupPlane->LineEdit1 )  myEditCurrentArgument = GroupPlane->LineEdit1;
406   else return;
407   GEOMBase_Skeleton::LineEditReturnPressed();
408 }
409
410
411 //=================================================================================
412 // function : ActivateThisDialog()
413 // purpose  :
414 //=================================================================================
415 void PrimitiveGUI_FaceDlg::ActivateThisDialog()
416 {
417   GEOMBase_Skeleton::ActivateThisDialog();
418   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
419            this, SLOT( SelectionIntoArgument() ) );
420
421   ConstructorsClicked( getConstructorId() );
422 }
423
424 //=================================================================================
425 // function : DeactivateActiveDialog()
426 // purpose  : public slot to deactivate if active
427 //=================================================================================
428 void PrimitiveGUI_FaceDlg::DeactivateActiveDialog()
429 {
430   GEOMBase_Skeleton::DeactivateActiveDialog();
431 }
432
433 //=================================================================================
434 // function : enterEvent()
435 // purpose  :
436 //=================================================================================
437 void PrimitiveGUI_FaceDlg::enterEvent( QEvent* )
438 {
439   if ( !mainFrame()->GroupConstructors->isEnabled() )
440     ActivateThisDialog();
441 }
442
443 //=================================================================================
444 // function : createOperation
445 // purpose  :
446 //=================================================================================
447 GEOM::GEOM_IOperations_ptr PrimitiveGUI_FaceDlg::createOperation()
448 {
449   return myGeomGUI->GetGeomGen()->GetI3DPrimOperations( getStudyId() );
450 }
451
452 //=================================================================================
453 // function : isValid
454 // purpose  :
455 //=================================================================================
456 bool PrimitiveGUI_FaceDlg::isValid( QString& msg )
457 {
458   const int id = getConstructorId();
459   if ( id == 0 )
460     return true;
461   else if ( id == 1 ) {
462     if (GroupType->RadioButton1->isChecked())
463       return !myEdge->_is_nil();
464     else if (GroupType->RadioButton2->isChecked())
465       return !myFace->_is_nil();
466   }
467   return false;
468 }
469
470 //=================================================================================
471 // function : execute
472 // purpose  :
473 //=================================================================================
474 bool PrimitiveGUI_FaceDlg::execute( ObjectList& objects )
475 {
476   bool res = false;
477   GEOM::GEOM_Object_var anObj;
478   switch ( getConstructorId() ) {
479   case 0 :
480     anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeFaceHW( GroupDimensions->SpinBox_DX->value(), GroupDimensions->SpinBox_DY->value(), myOrientationType );
481     res = true;
482     break;
483   case 1 :
484     if ( GroupType->RadioButton1->isChecked() )
485       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeFaceObjHW( myEdge, GroupPlane->SpinBox_DX->value(), GroupPlane->SpinBox_DY->value() );
486     else if (GroupType->RadioButton2->isChecked())
487       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeFaceObjHW( myFace, GroupPlane->SpinBox_DX->value(), GroupPlane->SpinBox_DY->value() );
488     res = true;
489     break;
490   }
491   
492   if ( !anObj->_is_nil() )
493     objects.push_back( anObj._retn() );
494
495   return res;
496 }
497
498 //=================================================================================
499 // function : addSubshapeToStudy
500 // purpose  : virtual method to add new SubObjects if local selection
501 //=================================================================================
502 void PrimitiveGUI_FaceDlg::addSubshapesToStudy()
503 {
504   QMap<QString, GEOM::GEOM_Object_var> objMap;
505   switch ( getConstructorId() ) {
506   case 1 :
507     if ( GroupType->RadioButton1->isChecked() )
508       objMap[GroupPlane->LineEdit1->text()] = myEdge;
509     if ( GroupType->RadioButton2->isChecked() )
510       objMap[GroupPlane->LineEdit1->text()] = myFace;
511     break;
512   }
513   addSubshapesToFather( objMap );
514 }