]> SALOME platform Git repositories - modules/geom.git/blob - src/PrimitiveGUI/PrimitiveGUI_FaceDlg.cxx
Salome HOME
IPAL21695 TC5.1.4: GEOM Dialogs Behavior
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_FaceDlg.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // GEOM GEOMGUI : GUI for Geometry component
21 // File   : PrimitiveGUI_FaceDlg.cxx
22 // Author : Dmitry Matveitchev, OCN.
23 //
24 #include "PrimitiveGUI_FaceDlg.h"
25
26 #include <DlgRef.h>
27 #include <GeometryGUI.h>
28 #include <GEOMBase.h>
29
30 #include <SUIT_ResourceMgr.h>
31 #include <SUIT_Session.h>
32 #include <SalomeApp_Application.h>
33 #include <LightApp_SelectionMgr.h>
34
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS.hxx>
37 #include <TopExp.hxx>
38 #include <TColStd_IndexedMapOfInteger.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40
41 #include <GEOMImpl_Types.hxx>
42
43 //=================================================================================
44 // class    : PrimitiveGUI_FaceDlg()
45 // purpose  : Constructs a PrimitiveGUI_FaceDlg which is a child of 'parent', with the 
46 //            name 'name' and widget flags set to 'f'.
47 //            The dialog will by default be modeless, unless you set 'modal' to
48 //            TRUE to construct a modal dialog.
49 //=================================================================================
50 PrimitiveGUI_FaceDlg::PrimitiveGUI_FaceDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
51                                     bool modal, Qt::WindowFlags fl )
52   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
53 {
54   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_FACE_OBJ_HW")));
55   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
56   QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_FACE_HW")));
57
58   setWindowTitle( tr( "GEOM_RECTANGLE_TITLE" ) );
59  
60   /***************************************************************/
61   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_RECTANGLE" ) );
62   mainFrame()->RadioButton1->setIcon( image2 );
63   mainFrame()->RadioButton2->setIcon( image0 );
64   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
65   mainFrame()->RadioButton3->close();
66
67   GroupPlane = new DlgRef_1Sel2Spin( centralWidget() );
68   GroupPlane->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
69   GroupPlane->TextLabel1->setText( tr( "GEOM_EDGE" ) );
70   GroupPlane->TextLabel2->setText( tr( "GEOM_HEIGHT" ) );
71   GroupPlane->TextLabel3->setText( tr( "GEOM_WIDTH" ) );
72   GroupPlane->PushButton1->setIcon( image1 );
73   GroupPlane->LineEdit1->setReadOnly( true );
74
75   GroupType = new DlgRef_3Radio( centralWidget() );
76   GroupType->GroupBox1->setTitle( tr( "GEOM_OBJECT_TYPE" ) );
77   GroupType->RadioButton1->setText( tr( "GEOM_EDGE" ) );
78   GroupType->RadioButton2->setText( tr( "GEOM_FACE" ) );
79   GroupType->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
80   GroupType->RadioButton3->close();
81
82   GroupDimensions = new DlgRef_2Spin( centralWidget() );
83   GroupDimensions->GroupBox1->setTitle( tr( "GEOM_BOX_OBJ" ) );
84   GroupDimensions->TextLabel1->setText( tr( "GEOM_HEIGHT" ) );
85   GroupDimensions->TextLabel2->setText( tr( "GEOM_WIDTH" ) );
86
87   GroupOrientation = new DlgRef_3Radio( centralWidget() );
88
89   GroupOrientation->GroupBox1->setTitle( tr( "GEOM_ORIENTATION" ) );
90   GroupOrientation->RadioButton1->setText( tr( "GEOM_WPLANE_OXY" ) );
91   GroupOrientation->RadioButton2->setText( tr( "GEOM_WPLANE_OYZ" ) );
92   GroupOrientation->RadioButton3->setText( tr( "GEOM_WPLANE_OZX" ) );
93
94   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
95   layout->setMargin( 0 ); layout->setSpacing( 6 );
96   layout->addWidget( GroupType );
97   layout->addWidget( GroupPlane );
98   layout->addWidget( GroupDimensions );
99   layout->addWidget( GroupOrientation );
100
101   /***************************************************************/
102
103   setHelpFileName( "create_squareface_page.html" );
104
105   Init();
106 }
107
108
109 //=================================================================================
110 // function : ~PrimitiveGUI_FaceDlg()
111 // purpose  : Destroys the object and frees any allocated resources
112 //=================================================================================
113 PrimitiveGUI_FaceDlg::~PrimitiveGUI_FaceDlg()
114 {  
115 }
116
117
118 //=================================================================================
119 // function : Init()
120 // purpose  :
121 //=================================================================================
122 void PrimitiveGUI_FaceDlg::Init()
123 {
124   /* init variables */
125   myEditCurrentArgument = GroupPlane->LineEdit1;
126   GroupType->RadioButton1->setChecked(true);
127   myEdge = GEOM::GEOM_Object::_nil();
128   myFace = GEOM::GEOM_Object::_nil();
129   globalSelection(); // close local contexts, if any
130   //  localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
131
132   myOrientationType = 1;
133
134   /* Get setting of step value from file configuration */
135   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
136   double aStep = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
137
138   double aDefaultSize = 100.0;
139   /* min, max, step and decimals for spin boxes */
140   initSpinBox( GroupPlane->SpinBox_DX, 0.00001, COORD_MAX, aStep, "length_precision" );
141   GroupPlane->SpinBox_DX->setValue( aDefaultSize );
142   initSpinBox( GroupPlane->SpinBox_DY, 0.00001, COORD_MAX, aStep, "length_precision" );
143   GroupPlane->SpinBox_DY->setValue( aDefaultSize );
144
145   initSpinBox( GroupDimensions->SpinBox_DX, 0.00001, COORD_MAX, aStep, "length_precision" );
146   GroupDimensions->SpinBox_DX->setValue( aDefaultSize );
147   initSpinBox( GroupDimensions->SpinBox_DY, 0.00001, COORD_MAX, aStep, "length_precision" );
148   GroupDimensions->SpinBox_DY->setValue( aDefaultSize );
149
150         
151   /* signals and slots connections */
152   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
153   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
154   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
155
156   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
157
158   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
159   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
160
161   connect( GroupPlane->PushButton1,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
162   connect( GroupPlane->LineEdit1,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
163   connect( GroupPlane->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
164   connect( GroupPlane->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
165
166   connect( GroupDimensions->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
167   connect( GroupDimensions->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
168
169   connect( GroupOrientation->RadioButton1, SIGNAL( clicked() ), this, SLOT( RadioButtonClicked() ) );
170   connect( GroupOrientation->RadioButton2, SIGNAL( clicked() ), this, SLOT( RadioButtonClicked() ) );
171   connect( GroupOrientation->RadioButton3, SIGNAL( clicked() ), this, SLOT( RadioButtonClicked() ) );
172
173   connect( GroupType->RadioButton1, SIGNAL( clicked() ), this, SLOT( TypeButtonClicked() ) );
174   connect( GroupType->RadioButton2, SIGNAL( clicked() ), this, SLOT( TypeButtonClicked() ) );
175
176   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
177            this, SLOT( SelectionIntoArgument() ) );
178   
179   initName( tr( "GEOM_FACE" ) );
180
181   ConstructorsClicked( 0 );
182 }
183
184 //=================================================================================
185 // function : SetDoubleSpinBoxStep()
186 // purpose  : Double spin box management
187 //=================================================================================
188 void PrimitiveGUI_FaceDlg::SetDoubleSpinBoxStep( double step )
189 {
190   GroupPlane->SpinBox_DX->setSingleStep(step);
191   GroupPlane->SpinBox_DY->setSingleStep(step);
192   GroupDimensions->SpinBox_DX->setSingleStep(step);
193   GroupDimensions->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( minimumSizeHint() );
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   bool ok = true;
463   if( getConstructorId() == 0 )
464   {
465     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
466     ok = GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
467   }
468   else if( getConstructorId() == 1 )
469   {
470     ok = GroupPlane->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
471     ok = GroupPlane->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
472   }
473
474   const int id = getConstructorId();
475   if ( id == 0 )
476     return ok;
477   else if ( id == 1 ) {
478     if (GroupType->RadioButton1->isChecked())
479       return !myEdge->_is_nil() && ok;
480     else if (GroupType->RadioButton2->isChecked())
481       return !myFace->_is_nil() && ok;
482   }
483   return false;
484 }
485
486 //=================================================================================
487 // function : execute
488 // purpose  :
489 //=================================================================================
490 bool PrimitiveGUI_FaceDlg::execute (ObjectList& objects)
491 {
492   bool res = false;
493   QStringList aParameters;
494   GEOM::GEOM_Object_var anObj;
495
496   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
497
498   switch (getConstructorId()) {
499   case 0:
500     anObj = anOper->MakeFaceHW(GroupDimensions->SpinBox_DX->value(),
501                                GroupDimensions->SpinBox_DY->value(), myOrientationType);
502     if (!anObj->_is_nil() && !IsPreview())
503     {
504       aParameters << GroupDimensions->SpinBox_DX->text();
505       aParameters << GroupDimensions->SpinBox_DY->text();
506       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
507     }
508     res = true;
509     break;
510   case 1:
511     anObj = GroupType->RadioButton1->isChecked() ? 
512       anOper->MakeFaceObjHW(myEdge, GroupPlane->SpinBox_DX->value(), GroupPlane->SpinBox_DY->value()) : 
513       anOper->MakeFaceObjHW(myFace, GroupPlane->SpinBox_DX->value(), GroupPlane->SpinBox_DY->value());
514     if (!anObj->_is_nil() && !IsPreview())
515     {
516       aParameters << GroupPlane->SpinBox_DX->text();
517       aParameters << GroupPlane->SpinBox_DY->text();
518       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
519     }
520     res = true;
521     break;
522   }
523
524   if (!anObj->_is_nil())
525     objects.push_back(anObj._retn());
526
527   return res;
528 }
529
530 //=================================================================================
531 // function : addSubshapeToStudy
532 // purpose  : virtual method to add new SubObjects if local selection
533 //=================================================================================
534 void PrimitiveGUI_FaceDlg::addSubshapesToStudy()
535 {
536   QMap<QString, GEOM::GEOM_Object_var> objMap;
537   switch ( getConstructorId() ) {
538   case 1 :
539     if ( GroupType->RadioButton1->isChecked() )
540       objMap[GroupPlane->LineEdit1->text()] = myEdge;
541     if ( GroupType->RadioButton2->isChecked() )
542       objMap[GroupPlane->LineEdit1->text()] = myFace;
543     break;
544   }
545   addSubshapesToFather( objMap );
546 }