Salome HOME
bdf686812c348586ccaa6b197a0b2f53996f8d51
[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   ConstructorsClicked( 0 );
168 }
169
170
171 //=================================================================================
172 // function : SetDoubleSpinBoxStep()
173 // purpose  : Double spin box management
174 //=================================================================================
175 void PrimitiveGUI_ConeDlg::SetDoubleSpinBoxStep( double step )
176 {
177   GroupPoints->SpinBox_DX->setSingleStep(step);
178   GroupPoints->SpinBox_DY->setSingleStep(step);
179   GroupPoints->SpinBox_DZ->setSingleStep(step);
180   GroupDimensions->SpinBox_DX->setSingleStep(step);
181   GroupDimensions->SpinBox_DY->setSingleStep(step);
182   GroupDimensions->SpinBox_DZ->setSingleStep(step);
183 }
184
185 //=================================================================================
186 // function : ConstructorsClicked()
187 // purpose  : Radio button management
188 //=================================================================================
189 void PrimitiveGUI_ConeDlg::ConstructorsClicked( int constructorId )
190 {
191   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
192   
193   switch( constructorId ) { 
194   case 0 :
195     {
196       //globalSelection( GEOM_POINT );
197       globalSelection(); // to break prvious local selection
198       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
199
200       GroupDimensions->hide();
201       GroupPoints->show();
202       
203       myEditCurrentArgument = GroupPoints->LineEdit1;
204       GroupPoints->LineEdit1->setText( "" );
205       GroupPoints->LineEdit2->setText( "" );
206       myPoint = myDir = GEOM::GEOM_Object::_nil();
207       
208       connect( myGeomGUI->getApp()->selectionMgr(), 
209                SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
210       break;
211     }
212   case 1 :
213     { 
214       globalSelection(); // close local contexts, if any
215
216       GroupPoints->hide();
217       GroupDimensions->show();
218         
219       break;
220     }
221   }
222
223   qApp->processEvents();
224   updateGeometry();
225   resize( minimumSize() );
226
227   displayPreview();
228 }
229
230
231 //=================================================================================
232 // function : ClickOnOk()
233 // purpose  :
234 //=================================================================================
235 void PrimitiveGUI_ConeDlg::ClickOnOk()
236 {
237   if ( ClickOnApply() )
238     ClickOnCancel();
239 }
240
241
242 //=================================================================================
243 // function : ClickOnApply()
244 // purpose  :
245 //=================================================================================
246 bool PrimitiveGUI_ConeDlg::ClickOnApply()
247 {
248   if ( !onAccept() )
249     return false;
250
251   initName();
252   ConstructorsClicked( getConstructorId() );
253   return true;
254 }
255
256
257 //=================================================================================
258 // function : SelectionIntoArgument()
259 // purpose  : Called when selection as changed or other case
260 //=================================================================================
261 void PrimitiveGUI_ConeDlg::SelectionIntoArgument()
262 {
263   if ( getConstructorId() != 0 )
264     return;
265
266   if ( IObjectCount() != 1 ) {
267     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
268       myPoint = GEOM::GEOM_Object::_nil();
269     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
270       myDir = GEOM::GEOM_Object::_nil();
271     return;
272   }
273
274   /* nbSel == 1 */
275   Standard_Boolean testResult = Standard_False;
276   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
277   
278   if ( !testResult || CORBA::is_nil( aSelectedObject ) )
279     return;
280
281   TopoDS_Shape aShape;
282   QString aName = GEOMBase::GetName( aSelectedObject );
283
284   if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
285     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
286     if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
287       aNeedType = TopAbs_EDGE;
288
289     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
290     TColStd_IndexedMapOfInteger aMap;
291     aSelMgr->GetIndexes( firstIObject(), aMap );
292     if ( aMap.Extent() == 1 ) {
293       int anIndex = aMap(1);
294       if ( aNeedType == TopAbs_EDGE )
295         aName.append( ":edge_" + QString::number( anIndex ) );
296       else
297         aName.append( ":vertex_" + QString::number( anIndex ) );
298
299       //Find SubShape Object in Father
300       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
301
302       if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
303         GEOM::GEOM_IShapesOperations_var aShapesOp =
304           getGeomEngine()->GetIShapesOperations( getStudyId() );
305         aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
306       }
307       else {
308         aSelectedObject = aFindedObject; // get Object from study       
309       }
310     }
311     else {
312       if ( aShape.ShapeType() != aNeedType ) {
313         aSelectedObject = GEOM::GEOM_Object::_nil();
314         aName = "";
315       }
316     }
317   }
318
319   myEditCurrentArgument->setText( aName );
320
321   if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
322     myPoint = aSelectedObject;
323   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
324     myDir = aSelectedObject;
325   
326   displayPreview();
327 }
328
329
330 //=================================================================================
331 // function : SetEditCurrentArgument()
332 // purpose  :
333 //=================================================================================
334 void PrimitiveGUI_ConeDlg::SetEditCurrentArgument()
335 {
336   QPushButton* send = (QPushButton*)sender();
337   
338   if ( send == GroupPoints->PushButton1 ) {
339     myEditCurrentArgument = GroupPoints->LineEdit1;
340     globalSelection( GEOM_POINT ); // to break prvious local selection
341     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
342   }
343   else if ( send == GroupPoints->PushButton2 ) {
344     myEditCurrentArgument = GroupPoints->LineEdit2;
345     globalSelection( GEOM_LINE );// to break prvious local selection
346     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
347   }
348   
349   myEditCurrentArgument->setFocus();
350   SelectionIntoArgument();
351 }
352
353
354 //=================================================================================
355 // function : LineEditReturnPressed()
356 // purpose  :
357 //=================================================================================
358 void PrimitiveGUI_ConeDlg::LineEditReturnPressed()
359 {
360   QLineEdit* send = (QLineEdit*)sender();
361   if ( send == GroupPoints->LineEdit1 ||
362        send == GroupPoints->LineEdit2 ) {
363     myEditCurrentArgument = send;
364     GEOMBase_Skeleton::LineEditReturnPressed();
365   }
366 }
367
368
369 //=================================================================================
370 // function : ActivateThisDialog()
371 // purpose  :
372 //=================================================================================
373 void PrimitiveGUI_ConeDlg::ActivateThisDialog()
374 {
375   GEOMBase_Skeleton::ActivateThisDialog();
376   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
377            this, SLOT( SelectionIntoArgument() ) );
378   
379   ConstructorsClicked( getConstructorId() );
380 }
381
382
383 //=================================================================================
384 // function : DeactivateActiveDialog()
385 // purpose  : public slot to deactivate if active
386 //=================================================================================
387 void PrimitiveGUI_ConeDlg::DeactivateActiveDialog()
388 {
389   GEOMBase_Skeleton::DeactivateActiveDialog();
390 }
391
392
393 //=================================================================================
394 // function : enterEvent()
395 // purpose  :
396 //=================================================================================
397 void PrimitiveGUI_ConeDlg::enterEvent( QEvent* )
398 {
399   if ( !mainFrame()->GroupConstructors->isEnabled() )
400     ActivateThisDialog();
401 }
402
403
404 //=================================================================================
405 // function : ValueChangedInSpinBox()
406 // purpose  :
407 //=================================================================================
408 void PrimitiveGUI_ConeDlg::ValueChangedInSpinBox()
409 {
410   displayPreview();
411 }
412
413
414 //=================================================================================
415 // function : createOperation
416 // purpose  :
417 //=================================================================================
418 GEOM::GEOM_IOperations_ptr  PrimitiveGUI_ConeDlg::createOperation()
419 {
420   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
421 }
422
423 //=================================================================================
424 // function : isValid
425 // purpose  :
426 //=================================================================================
427 bool  PrimitiveGUI_ConeDlg::isValid( QString& msg )
428 {
429   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil()) : true;
430 }
431
432 //=================================================================================
433 // function : execute
434 // purpose  :
435 //=================================================================================
436 bool PrimitiveGUI_ConeDlg::execute( ObjectList& objects )
437 {
438   bool res = false;
439   
440   GEOM::GEOM_Object_var anObj;
441
442   switch ( getConstructorId() ) {
443   case 0 :
444     if ( !CORBA::is_nil( myPoint ) && !CORBA::is_nil( myDir ) ) {
445       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeConePntVecR1R2H( myPoint, 
446                                                                                             myDir, 
447                                                                                             getRadius1(),
448                                                                                             getRadius2(),
449                                                                                             getHeight() );
450       res = true;
451     }
452     break;
453   case 1 :
454     anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeConeR1R2H( getRadius1(),
455                                                                                     getRadius2(),
456                                                                                     getHeight() );
457     res = true;
458     break;
459   }
460
461   if ( !anObj->_is_nil() )
462     objects.push_back( anObj._retn() );
463
464   return res;
465 }
466
467
468 //=================================================================================
469 // function : getRadius1()
470 // purpose  :
471 //=================================================================================
472 double PrimitiveGUI_ConeDlg::getRadius1() const
473 {
474   int aConstructorId = getConstructorId();
475   if ( aConstructorId == 0 )
476     return GroupPoints->SpinBox_DX->value();
477   else if ( aConstructorId == 1 )
478     return GroupDimensions->SpinBox_DX->value();
479   return 0;
480 }
481
482
483 //=================================================================================
484 // function : getRadius2()
485 // purpose  :
486 //=================================================================================
487 double PrimitiveGUI_ConeDlg::getRadius2() const
488 {
489   int aConstructorId = getConstructorId();
490   if ( aConstructorId == 0 )
491     return GroupPoints->SpinBox_DY->value();
492   else if ( aConstructorId == 1 )
493     return GroupDimensions->SpinBox_DY->value();
494   return 0;
495 }
496
497
498 //=================================================================================
499 // function : getRadius2()
500 // purpose  :
501 //=================================================================================
502 double PrimitiveGUI_ConeDlg::getHeight() const
503 {
504   int aConstructorId = getConstructorId();
505   if ( aConstructorId == 0 )
506     return GroupPoints->SpinBox_DZ->value();
507   else if ( aConstructorId == 1 )
508     return GroupDimensions->SpinBox_DZ->value();
509   return 0;
510 }
511
512 //=================================================================================
513 // function : addSubshapeToStudy
514 // purpose  : virtual method to add new SubObjects if local selection
515 //=================================================================================
516 void PrimitiveGUI_ConeDlg::addSubshapesToStudy()
517 {
518   QMap<QString, GEOM::GEOM_Object_var> objMap;
519   
520   switch ( getConstructorId() ) {
521   case 0:
522     objMap[GroupPoints->LineEdit1->text()] = myPoint;
523     objMap[GroupPoints->LineEdit2->text()] = myDir;
524     break;
525   case 1:
526     return;
527   }
528   addSubshapesToFather( objMap );
529 }