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