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