Salome HOME
GEOM: It should be forbidden creation of primitives with zero dimensions.
[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.00001, COORD_MAX, aStep, 6 ); // VSR: TODO: DBL_DIGITS_DISPLAY
143   GroupPlane->SpinBox_DX->setValue( aDefaultSize );
144   initSpinBox( GroupPlane->SpinBox_DY, 0.000001, COORD_MAX, aStep, 6 ); // VSR: TODO: DBL_DIGITS_DISPLAY
145   GroupPlane->SpinBox_DY->setValue( aDefaultSize );
146
147   initSpinBox( GroupDimensions->SpinBox_DX, 0.000001, COORD_MAX, aStep, 6 ); // VSR: TODO: DBL_DIGITS_DISPLAY
148   GroupDimensions->SpinBox_DX->setValue( aDefaultSize );
149   initSpinBox( GroupDimensions->SpinBox_DY, 0.000001, COORD_MAX, aStep, 6 ); // 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   erasePreview();
272   switch ( constructorId ) {
273   case 0:
274     {
275       globalSelection(); // close local contexts, if any
276       GroupPlane->hide();
277       GroupDimensions->show();
278       GroupOrientation->show();
279       GroupType->hide();
280       GroupOrientation->RadioButton1->setChecked( true );
281       myOrientationType = 1;
282       break;
283     }
284   case 1:
285     {
286       globalSelection(); // close local contexts, if any
287       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
288       myEditCurrentArgument = GroupPlane->LineEdit1;
289       myEditCurrentArgument->setText("");
290       myEdge = GEOM::GEOM_Object::_nil();
291       myFace = GEOM::GEOM_Object::_nil();
292       GroupType->RadioButton1->setChecked( true );
293       TypeButtonClicked();
294       GroupDimensions->hide();
295       GroupOrientation->hide();
296       GroupType->show();
297       GroupPlane->show();
298       break;
299     }
300   }
301
302   qApp->processEvents();
303   updateGeometry();
304   resize( minimumSize() );
305   SelectionIntoArgument();
306   //displayPreview();
307 }
308
309 //=================================================================================
310 // function : SelectionIntoArgument()
311 // purpose  : Called when selection as changed or other case
312 //=================================================================================
313 void PrimitiveGUI_FaceDlg::SelectionIntoArgument()
314 {
315   if (getConstructorId() == 0) {
316     displayPreview();
317     return;
318   }
319
320   myEditCurrentArgument->setText( "" );
321
322   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
323   SALOME_ListIO aSelList;
324   aSelMgr->selectedObjects(aSelList);
325
326   if ( aSelList.Extent() != 1 ) {
327     if ( myEditCurrentArgument == GroupPlane->LineEdit1 )  myEdge = GEOM::GEOM_Object::_nil();
328     return;
329   }
330
331   // nbSel == 1 
332   Standard_Boolean aRes = Standard_False;
333   TopAbs_ShapeEnum aNeedType = TopAbs_EDGE;
334   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First(), aRes );
335   if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
336     QString aName = GEOMBase::GetName( aSelectedObject );
337     
338     TopoDS_Shape aShape;
339     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
340       if (GroupType->RadioButton2->isChecked())
341         aNeedType = TopAbs_FACE;
342
343       TColStd_IndexedMapOfInteger aMap;
344       aSelMgr->GetIndexes( aSelList.First(), aMap );
345       if ( aMap.Extent() == 1 ) { // Local Selection
346         int anIndex = aMap( 1 );
347         if ( aNeedType == TopAbs_EDGE )
348           aName += QString( ":edge_%1" ).arg( anIndex );
349         else
350           aName += QString( ":face_%1" ).arg( anIndex );
351
352         //Find SubShape Object in Father
353         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
354
355         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
356           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
357           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
358         }
359         else
360           aSelectedObject = aFindedObject; // get Object from study
361       }
362       else { // Global Selection
363         if ( aShape.ShapeType() != aNeedType ) {
364           aSelectedObject = GEOM::GEOM_Object::_nil();
365           aName = "";
366         }
367       }
368     }
369
370     myEditCurrentArgument->setText( aName );
371     if ( myEditCurrentArgument == GroupPlane->LineEdit1 && aNeedType == TopAbs_EDGE )
372       myEdge = aSelectedObject;
373     else if ( myEditCurrentArgument == GroupPlane->LineEdit1 && aNeedType == TopAbs_FACE )
374       myFace = aSelectedObject;
375   }
376   displayPreview();
377 }
378
379
380 //=================================================================================
381 // function : SetEditCurrentArgument()
382 // purpose  :
383 //=================================================================================
384 void PrimitiveGUI_FaceDlg::SetEditCurrentArgument()
385 {
386   QPushButton* send = (QPushButton*)sender();
387
388   globalSelection(); // close previous local contexts
389   if ( send == GroupPlane->PushButton1 ) {
390     myEditCurrentArgument = GroupPlane->LineEdit1;
391     if (GroupType->RadioButton1->isChecked())
392       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
393     else if (GroupType->RadioButton1->isChecked())
394       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
395   }
396
397   myEditCurrentArgument->setFocus();
398   SelectionIntoArgument();
399 }
400
401
402 //=================================================================================
403 // function : LineEditReturnPressed()
404 // purpose  :
405 //=================================================================================
406 void PrimitiveGUI_FaceDlg::LineEditReturnPressed()
407 {
408   QLineEdit* send = (QLineEdit*)sender();
409   if ( send == GroupPlane->LineEdit1 )  myEditCurrentArgument = GroupPlane->LineEdit1;
410   else return;
411   GEOMBase_Skeleton::LineEditReturnPressed();
412 }
413
414
415 //=================================================================================
416 // function : ActivateThisDialog()
417 // purpose  :
418 //=================================================================================
419 void PrimitiveGUI_FaceDlg::ActivateThisDialog()
420 {
421   GEOMBase_Skeleton::ActivateThisDialog();
422   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
423            this, SLOT( SelectionIntoArgument() ) );
424
425   ConstructorsClicked( getConstructorId() );
426 }
427
428 //=================================================================================
429 // function : DeactivateActiveDialog()
430 // purpose  : public slot to deactivate if active
431 //=================================================================================
432 void PrimitiveGUI_FaceDlg::DeactivateActiveDialog()
433 {
434   GEOMBase_Skeleton::DeactivateActiveDialog();
435 }
436
437 //=================================================================================
438 // function : enterEvent()
439 // purpose  :
440 //=================================================================================
441 void PrimitiveGUI_FaceDlg::enterEvent( QEvent* )
442 {
443   if ( !mainFrame()->GroupConstructors->isEnabled() )
444     ActivateThisDialog();
445 }
446
447 //=================================================================================
448 // function : createOperation
449 // purpose  :
450 //=================================================================================
451 GEOM::GEOM_IOperations_ptr PrimitiveGUI_FaceDlg::createOperation()
452 {
453   return myGeomGUI->GetGeomGen()->GetI3DPrimOperations( getStudyId() );
454 }
455
456 //=================================================================================
457 // function : isValid
458 // purpose  :
459 //=================================================================================
460 bool PrimitiveGUI_FaceDlg::isValid( QString& msg )
461 {
462   const int id = getConstructorId();
463   if ( id == 0 )
464     return true;
465   else if ( id == 1 ) {
466     if (GroupType->RadioButton1->isChecked())
467       return !myEdge->_is_nil();
468     else if (GroupType->RadioButton2->isChecked())
469       return !myFace->_is_nil();
470   }
471   return false;
472 }
473
474 //=================================================================================
475 // function : execute
476 // purpose  :
477 //=================================================================================
478 bool PrimitiveGUI_FaceDlg::execute (ObjectList& objects)
479 {
480   bool res = false;
481   GEOM::GEOM_Object_var anObj;
482   switch (getConstructorId()) {
483   case 0:
484     anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->
485       MakeFaceHW(GroupDimensions->SpinBox_DX->value(),
486                  GroupDimensions->SpinBox_DY->value(), myOrientationType);
487     res = true;
488     break;
489   case 1:
490     if (GroupType->RadioButton1->isChecked())
491       anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->
492         MakeFaceObjHW(myEdge, GroupPlane->SpinBox_DX->value(), GroupPlane->SpinBox_DY->value());
493     else if (GroupType->RadioButton2->isChecked())
494       anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->
495         MakeFaceObjHW(myFace, GroupPlane->SpinBox_DX->value(), GroupPlane->SpinBox_DY->value());
496     res = true;
497     break;
498   }
499
500   if (!anObj->_is_nil())
501     objects.push_back(anObj._retn());
502
503   return res;
504 }
505
506 //=================================================================================
507 // function : addSubshapeToStudy
508 // purpose  : virtual method to add new SubObjects if local selection
509 //=================================================================================
510 void PrimitiveGUI_FaceDlg::addSubshapesToStudy()
511 {
512   QMap<QString, GEOM::GEOM_Object_var> objMap;
513   switch ( getConstructorId() ) {
514   case 1 :
515     if ( GroupType->RadioButton1->isChecked() )
516       objMap[GroupPlane->LineEdit1->text()] = myEdge;
517     if ( GroupType->RadioButton2->isChecked() )
518       objMap[GroupPlane->LineEdit1->text()] = myFace;
519     break;
520   }
521   addSubshapesToFather( objMap );
522 }