Salome HOME
0016756: EDF 545 GEOM : Extrusion, give the coordinates of the vector as input
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_CylinderDlg.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_CylinderDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "PrimitiveGUI_CylinderDlg.h"
27
28 #include <DlgRef.h>
29
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_Session.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS.hxx>
41 #include <TopExp.hxx>
42 #include <TColStd_IndexedMapOfInteger.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
44
45 #include <GEOMImpl_Types.hxx>
46
47 //=================================================================================
48 // class    : PrimitiveGUI_CylinderDlg()
49 // purpose  : Constructs a PrimitiveGUI_CylinderDlg which is a child of 'parent', with the 
50 //            name 'name' and widget flags set to 'f'.
51 //            The dialog will by default be modeless, unless you set 'modal' to
52 //            TRUE to construct a modal dialog.
53 //=================================================================================
54 PrimitiveGUI_CylinderDlg::PrimitiveGUI_CylinderDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
55                                                     bool modal, Qt::WindowFlags fl )
56   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
57 {
58   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_CYLINDER_PV" ) ) );
59   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_CYLINDER_DXYZ" ) ) );
60   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
61
62   setWindowTitle( tr( "GEOM_CYLINDER_TITLE" ) );
63
64   /***************************************************************/
65   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_CYLINDER" ) );
66   mainFrame()->RadioButton1->setIcon( image0 );
67   mainFrame()->RadioButton2->setIcon( image1 );
68   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
69   mainFrame()->RadioButton3->close();
70
71   GroupPoints = new DlgRef_2Sel2Spin( centralWidget() );
72   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
73   GroupPoints->TextLabel1->setText( tr( "GEOM_BASE_POINT" ) );
74   GroupPoints->TextLabel2->setText( tr( "GEOM_VECTOR" ) );
75   GroupPoints->TextLabel3->setText( tr( "GEOM_RADIUS" ) );
76   GroupPoints->TextLabel4->setText( tr( "GEOM_HEIGHT" ) );
77   GroupPoints->PushButton1->setIcon( image2 );
78   GroupPoints->PushButton2->setIcon( image2 );
79
80   GroupDimensions = new DlgRef_2Spin( centralWidget() );
81   GroupDimensions->GroupBox1->setTitle( tr( "GEOM_BOX_OBJ" ) );
82   GroupDimensions->TextLabel1->setText( tr( "GEOM_RADIUS" ) );
83   GroupDimensions->TextLabel2->setText( tr( "GEOM_HEIGHT" ) );
84
85   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
86   layout->setMargin( 0 ); layout->setSpacing( 6 );
87   layout->addWidget( GroupPoints );
88   layout->addWidget( GroupDimensions );
89   /***************************************************************/
90
91   setHelpFileName( "create_cylinder_page.html" );
92
93   Init();
94 }
95
96
97 //=================================================================================
98 // function : ~PrimitiveGUI_CylinderDlg()
99 // purpose  : Destroys the object and frees any allocated resources
100 //=================================================================================
101 PrimitiveGUI_CylinderDlg::~PrimitiveGUI_CylinderDlg()
102 {
103   // no need to delete child widgets, Qt does it all for us
104 }
105
106
107 //=================================================================================
108 // function : Init()
109 // purpose  :
110 //=================================================================================
111 void PrimitiveGUI_CylinderDlg::Init()
112 {
113   /* init variables */
114   myEditCurrentArgument = GroupPoints->LineEdit1;
115   GroupPoints->LineEdit1->setReadOnly( true );
116   GroupPoints->LineEdit2->setReadOnly( true );
117
118   myPoint = myDir = GEOM::GEOM_Object::_nil();
119   
120   /* Get setting of step value from file configuration */
121   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
122   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
123
124   /* min, max, step and decimals for spin boxes & initial values */
125   /* First constructor : radius */
126   initSpinBox( GroupPoints->SpinBox_DX, 0.001, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
127   /* First constructor : algebric height */
128   initSpinBox( GroupPoints->SpinBox_DY, COORD_MIN, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
129   /* Second constructor : radius */
130   initSpinBox( GroupDimensions->SpinBox_DX, 0.001, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
131   /* Second constructor : algebric height */
132   initSpinBox( GroupDimensions->SpinBox_DY, COORD_MIN, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
133
134   GroupPoints->SpinBox_DX->setValue( 100.0 );
135   GroupPoints->SpinBox_DY->setValue( 300.0 );
136   GroupDimensions->SpinBox_DX->setValue( 100.0 );
137   GroupDimensions->SpinBox_DY->setValue( 300.0 );
138
139   /* signals and slots connections */
140   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
141   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
142
143   connect( this,          SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
144
145   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
146   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
147
148   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
149   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
150
151   connect( GroupPoints->SpinBox_DX,     SIGNAL(valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
152   connect( GroupPoints->SpinBox_DY,     SIGNAL(valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
153   connect( GroupDimensions->SpinBox_DX, SIGNAL(valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
154   connect( GroupDimensions->SpinBox_DY, SIGNAL(valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
155
156   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
157   
158   connect( myGeomGUI->getApp()->selectionMgr(), 
159            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
160   
161   initName( tr( "GEOM_CYLINDER" ) );
162   ConstructorsClicked( 0 );
163 }
164
165
166 //=================================================================================
167 // function : SetDoubleSpinBoxStep()
168 // purpose  : Double spin box management
169 //=================================================================================
170 void PrimitiveGUI_CylinderDlg::SetDoubleSpinBoxStep( double step )
171 {
172   GroupPoints->SpinBox_DX->setSingleStep(step);
173   GroupPoints->SpinBox_DY->setSingleStep(step);
174   GroupDimensions->SpinBox_DX->setSingleStep(step);
175   GroupDimensions->SpinBox_DY->setSingleStep(step);
176 }
177
178
179 //=================================================================================
180 // function : ConstructorsClicked()
181 // purpose  : Radio button management
182 //=================================================================================
183 void PrimitiveGUI_CylinderDlg::ConstructorsClicked( int constructorId )
184 {
185   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
186     
187   switch( constructorId ) { 
188   case 0 :
189     {
190       globalSelection( GEOM_POINT ); // to break previous local selection
191       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
192       
193       GroupDimensions->hide();
194       GroupPoints->show();
195       
196       myEditCurrentArgument = GroupPoints->LineEdit1;
197       GroupPoints->LineEdit1->setText( "" );
198       GroupPoints->LineEdit2->setText( "" );
199       myPoint = myDir = GEOM::GEOM_Object::_nil();
200       
201       connect( myGeomGUI->getApp()->selectionMgr(), 
202                SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
203       break;
204     }
205   case 1 :
206     { 
207       globalSelection(); // close local contexts, if any
208       
209       GroupPoints->hide();
210       GroupDimensions->show();
211       break;
212     }
213   }
214
215   qApp->processEvents();
216   updateGeometry();
217   resize( minimumSize() );
218
219   displayPreview();
220 }
221
222
223 //=================================================================================
224 // function : ClickOnOk()
225 // purpose  :
226 //=================================================================================
227 void PrimitiveGUI_CylinderDlg::ClickOnOk()
228 {
229   if ( ClickOnApply() )
230     ClickOnCancel();
231 }
232
233
234 //=================================================================================
235 // function : ClickOnApply()
236 // purpose  :
237 //=================================================================================
238 bool PrimitiveGUI_CylinderDlg::ClickOnApply()
239 {
240   if ( !onAccept() )
241     return false;
242
243   initName();
244   ConstructorsClicked( getConstructorId() );
245   return true;
246 }
247
248
249 //=================================================================================
250 // function : SelectionIntoArgument()
251 // purpose  : Called when selection as changed or other case
252 //=================================================================================
253 void PrimitiveGUI_CylinderDlg::SelectionIntoArgument()
254 {
255   if ( getConstructorId() != 0 )
256     return;
257
258   myEditCurrentArgument->setText( "" );
259   
260   if ( IObjectCount() != 1 ) {
261     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
262       myPoint = GEOM::GEOM_Object::_nil();
263     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
264       myDir = GEOM::GEOM_Object::_nil();
265     return;
266   }
267   
268   /* nbSel == 1 */
269   Standard_Boolean testResult = Standard_False;
270   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
271     
272   if ( !testResult || CORBA::is_nil( aSelectedObject ) )
273     return;
274   
275
276   QString aName = GEOMBase::GetName(aSelectedObject);
277   TopoDS_Shape aShape;
278   if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
279     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
280     if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
281       aNeedType = TopAbs_EDGE;
282
283     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
284     TColStd_IndexedMapOfInteger aMap;
285     aSelMgr->GetIndexes( firstIObject(), aMap );
286     if ( aMap.Extent() == 1 ) { // Local Selection
287       int anIndex = aMap( 1 );
288       if ( aNeedType == TopAbs_EDGE )
289         aName.append( ":edge_" + QString::number( anIndex ) );
290       else
291         aName.append( ":vertex_" + QString::number( anIndex ) );
292
293       //Find SubShape Object in Father
294       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
295
296       if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
297         GEOM::GEOM_IShapesOperations_var aShapesOp =
298           getGeomEngine()->GetIShapesOperations( getStudyId() );
299         aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
300       }
301       else {
302         aSelectedObject = aFindedObject; // get Object from study
303       }
304     }
305     else { // Global Selection
306       if ( aShape.ShapeType() != aNeedType ) {
307         aSelectedObject = GEOM::GEOM_Object::_nil();
308         aName = "";
309       }
310     }
311   }
312
313   myEditCurrentArgument->setText(aName);
314
315   if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
316     myPoint = aSelectedObject;
317   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
318     myDir = aSelectedObject;
319     
320   displayPreview();
321 }
322
323
324 //=================================================================================
325 // function : SetEditCurrentArgument()
326 // purpose  :
327 //=================================================================================
328 void PrimitiveGUI_CylinderDlg::SetEditCurrentArgument()
329 {
330   QPushButton* send = (QPushButton*)sender();
331   
332   if ( send == GroupPoints->PushButton1 ) {
333     myEditCurrentArgument = GroupPoints->LineEdit1;
334     globalSelection( GEOM_POINT ); // to break previous local selection
335     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
336   }
337   else if ( send == GroupPoints->PushButton2 ) {
338     myEditCurrentArgument = GroupPoints->LineEdit2;
339     globalSelection( GEOM_LINE );  // to break previous local selection
340     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
341   }
342   
343   myEditCurrentArgument->setFocus();
344   SelectionIntoArgument();
345 }
346
347
348 //=================================================================================
349 // function : LineEditReturnPressed()
350 // purpose  :
351 //=================================================================================
352 void PrimitiveGUI_CylinderDlg::LineEditReturnPressed()
353 {  
354   QLineEdit* send = (QLineEdit*)sender();
355   if ( send == GroupPoints->LineEdit1 ||
356        send == GroupPoints->LineEdit2 ) {
357     myEditCurrentArgument = send;
358     GEOMBase_Skeleton::LineEditReturnPressed();
359   }
360 }
361
362
363 //=================================================================================
364 // function : ActivateThisDialog()
365 // purpose  :
366 //=================================================================================
367 void PrimitiveGUI_CylinderDlg::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 //=================================================================================
378 // function : enterEvent()
379 // purpose  :
380 //=================================================================================
381 void PrimitiveGUI_CylinderDlg::enterEvent( QEvent* )
382 {
383   if ( !mainFrame()->GroupConstructors->isEnabled() )
384     ActivateThisDialog();
385 }
386
387
388 //=================================================================================
389 // function : DeactivateActiveDialog()
390 // purpose  : public slot to deactivate if active
391 //=================================================================================
392 void PrimitiveGUI_CylinderDlg::DeactivateActiveDialog()
393 {
394   GEOMBase_Skeleton::DeactivateActiveDialog();
395 }
396
397
398 //=================================================================================
399 // function : ValueChangedInSpinBox
400 // purpose  :
401 //=================================================================================
402 void PrimitiveGUI_CylinderDlg::ValueChangedInSpinBox( )
403 {  
404   displayPreview();
405 }
406
407
408 //=================================================================================
409 // function : createOperation
410 // purpose  :
411 //=================================================================================
412 GEOM::GEOM_IOperations_ptr PrimitiveGUI_CylinderDlg::createOperation()
413 {
414   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
415 }
416
417
418 //=================================================================================
419 // function : isValid
420 // purpose  :
421 //=================================================================================
422 bool PrimitiveGUI_CylinderDlg::isValid( QString& msg )
423 {
424   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil() ) : true;
425 }
426
427 //=================================================================================
428 // function : execute
429 // purpose  :
430 //=================================================================================
431 bool PrimitiveGUI_CylinderDlg::execute( ObjectList& objects )
432 {
433   bool res = false;
434   
435   GEOM::GEOM_Object_var anObj;
436   
437   switch ( getConstructorId() ) {
438   case 0 :
439     if ( !CORBA::is_nil( myPoint ) && !CORBA::is_nil( myDir ) ) {
440       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeCylinderPntVecRH( myPoint, myDir, getRadius(), getHeight() );
441       res = true;
442     }
443     break;
444   case 1 :
445     anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeCylinderRH( getRadius(), getHeight() );
446     res = true;
447     
448     break;
449   }
450   
451   if ( !anObj->_is_nil() )
452     objects.push_back( anObj._retn() );
453   
454   return res;
455 }
456
457
458 //=================================================================================
459 // function : getRadius()
460 // purpose  :
461 //=================================================================================
462 double PrimitiveGUI_CylinderDlg::getRadius() const
463 {
464   int aConstructorId = getConstructorId();
465   if ( aConstructorId == 0 )
466     return GroupPoints->SpinBox_DX->value();
467   else if ( aConstructorId == 1 )
468     return GroupDimensions->SpinBox_DX->value();
469   return 0;
470 }
471
472
473 //=================================================================================
474 // function : getHeight()
475 // purpose  :
476 //=================================================================================
477 double PrimitiveGUI_CylinderDlg::getHeight() const
478 {
479   int aConstructorId = getConstructorId();
480   if ( aConstructorId == 0 )
481     return GroupPoints->SpinBox_DY->value();
482   else if ( aConstructorId == 1 )
483     return GroupDimensions->SpinBox_DY->value();
484   return 0;
485 }
486
487 //=================================================================================
488 // function : addSubshapeToStudy
489 // purpose  : virtual method to add new SubObjects if local selection
490 //=================================================================================
491 void PrimitiveGUI_CylinderDlg::addSubshapesToStudy()
492 {
493   QMap<QString, GEOM::GEOM_Object_var> objMap;
494
495   switch ( getConstructorId() ) {
496   case 0:
497     objMap[GroupPoints->LineEdit1->text()] = myPoint;
498     objMap[GroupPoints->LineEdit2->text()] = myDir;
499     break;
500   case 1:
501     return;
502   }
503   addSubshapesToFather( objMap );
504 }