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