Salome HOME
6fc293005d19334c908758395155505f43bb890a
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_SphereDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : PrimitiveGUI_SphereDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
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.000001, COORD_MAX, step, 6 ); // VSR: TODO: DBL_DIGITS_DISPLAY
119   initSpinBox( GroupDimensions->SpinBox_DX, 0.000001, COORD_MAX, step, 6 ); // 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   setConstructorId( 1 ); // simplest constructor
143   ConstructorsClicked( 1 );
144 }
145
146 //=================================================================================
147 // function : SetDoubleSpinBoxStep()
148 // purpose  : Double spin box management
149 //=================================================================================
150 void PrimitiveGUI_SphereDlg::SetDoubleSpinBoxStep( double step )
151 {
152   GroupPoints->SpinBox_DX->setSingleStep(step);
153   GroupDimensions->SpinBox_DX->setSingleStep(step);
154 }
155
156 //=================================================================================
157 // function : ConstructorsClicked()
158 // purpose  : Radio button management
159 //=================================================================================
160 void PrimitiveGUI_SphereDlg::ConstructorsClicked( int constructorId )
161 {
162   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
163   
164   switch ( constructorId ) {
165   case 0:
166     {
167       globalSelection(); // close local contexts, if any
168       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
169
170       GroupDimensions->hide();
171       GroupPoints->show();
172       
173       myEditCurrentArgument = GroupPoints->LineEdit1;
174       GroupPoints->LineEdit1->setText( "" );
175       myPoint = GEOM::GEOM_Object::_nil();
176       
177       connect( myGeomGUI->getApp()->selectionMgr(), 
178                SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
179       break;
180     }
181   case 1:
182     {
183       globalSelection(); // close local contexts, if any
184       
185       GroupPoints->hide();
186       GroupDimensions->show();
187       
188       break;
189     }
190   }
191
192   qApp->processEvents();
193   updateGeometry();
194   resize( minimumSizeHint() );
195
196   displayPreview();
197 }
198
199
200 //=================================================================================
201 // function : ClickOnOk()
202 // purpose  :
203 //=================================================================================
204 void PrimitiveGUI_SphereDlg::ClickOnOk()
205 {
206   if ( ClickOnApply() )
207     ClickOnCancel();
208 }
209
210
211 //=================================================================================
212 // function : ClickOnApply()
213 // purpose  :
214 //=================================================================================
215 bool PrimitiveGUI_SphereDlg::ClickOnApply()
216 {
217   if ( !onAccept() )
218     return false;
219   
220   initName();
221   ConstructorsClicked( getConstructorId() );
222   return true;
223 }
224
225
226 //=================================================================================
227 // function : SelectionIntoArgument()
228 // purpose  : Called when selection as changed or other case
229 //=================================================================================
230 void PrimitiveGUI_SphereDlg::SelectionIntoArgument()
231 {
232   if ( getConstructorId() != 0 )
233     return;
234
235   myEditCurrentArgument->setText( "" );
236
237   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
238   SALOME_ListIO aSelList;
239   aSelMgr->selectedObjects(aSelList);
240
241   if (aSelList.Extent() != 1) {
242     myPoint = GEOM::GEOM_Object::_nil();
243     return;
244   }
245
246   /* nbSel == 1 ! */
247   Standard_Boolean testResult = Standard_False;
248   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
249
250   if ( !testResult || CORBA::is_nil( aSelectedObject ) )
251     return;
252  
253   QString aName = GEOMBase::GetName( aSelectedObject );
254   TopoDS_Shape aShape;
255   if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull() ) {
256     TColStd_IndexedMapOfInteger aMap;
257     aSelMgr->GetIndexes(aSelList.First(), aMap);
258     if ( aMap.Extent() == 1 ) { // Local Selection
259       int anIndex = aMap( 1 );
260       aName.append( ":vertex_" + QString::number( anIndex ) );
261
262       //Find SubShape Object in Father
263       GEOM::GEOM_Object_var aFindedObject = findObjectInFather(aSelectedObject, aName );
264
265       if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
266         GEOM::GEOM_IShapesOperations_var aShapesOp =
267           getGeomEngine()->GetIShapesOperations( getStudyId() );
268         aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
269       }
270       else {
271         aSelectedObject = aFindedObject; // get Object from study
272       }
273     }
274     else { // Global Selection
275       if (aShape.ShapeType() != TopAbs_VERTEX) {
276         aSelectedObject = GEOM::GEOM_Object::_nil();
277         aName = "";
278       }
279     }
280   }
281
282   myEditCurrentArgument->setText( aName );
283   myPoint = aSelectedObject;
284
285   displayPreview();
286 }
287
288 //=================================================================================
289 // function : LineEditReturnPressed()
290 // purpose  :
291 //=================================================================================
292 void PrimitiveGUI_SphereDlg::LineEditReturnPressed()
293 {
294   QLineEdit* send = (QLineEdit*)sender();
295   if ( send == GroupPoints->LineEdit1 ) {
296     myEditCurrentArgument = send;
297     GEOMBase_Skeleton::LineEditReturnPressed();
298   }
299 }
300
301
302 //=================================================================================
303 // function : SetEditCurrentArgument()
304 // purpose  :
305 //=================================================================================
306 void PrimitiveGUI_SphereDlg::SetEditCurrentArgument()
307 {
308   QPushButton* send = (QPushButton*)sender();
309   
310   if ( send == GroupPoints->PushButton1 ) {
311     GroupPoints->LineEdit1->setFocus();
312     myEditCurrentArgument = GroupPoints->LineEdit1;
313     globalSelection(); // close local contexts, if any
314     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
315     SelectionIntoArgument();
316   }
317 }
318
319
320 //=================================================================================
321 // function : ActivateThisDialog()
322 // purpose  :
323 //=================================================================================
324 void PrimitiveGUI_SphereDlg::ActivateThisDialog()
325 {
326   GEOMBase_Skeleton::ActivateThisDialog();
327   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
328            this, SLOT( SelectionIntoArgument() ) );
329   
330   ConstructorsClicked( getConstructorId() );
331 }
332
333
334 //=================================================================================
335 // function : DeactivateActiveDialog()
336 // purpose  : public slot to deactivate if active
337 //=================================================================================
338 void PrimitiveGUI_SphereDlg::DeactivateActiveDialog()
339 {
340   GEOMBase_Skeleton::DeactivateActiveDialog();
341 }
342
343
344 //=================================================================================
345 // function : enterEvent()
346 // purpose  :
347 //=================================================================================
348 void PrimitiveGUI_SphereDlg::enterEvent( QEvent* )
349 {
350   if ( !mainFrame()->GroupConstructors->isEnabled() )
351     ActivateThisDialog();
352 }
353
354
355 //=================================================================================
356 // function : ValueChangedInSpinBox()
357 // purpose  :
358 //=================================================================================
359 void PrimitiveGUI_SphereDlg::ValueChangedInSpinBox()
360 {
361   displayPreview();
362 }
363
364
365 //=================================================================================
366 // function : createOperation
367 // purpose  :
368 //=================================================================================
369 GEOM::GEOM_IOperations_ptr PrimitiveGUI_SphereDlg::createOperation()
370 {
371   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
372 }
373
374
375 //=================================================================================
376 // function : isValid
377 // purpose  :
378 //=================================================================================
379 bool PrimitiveGUI_SphereDlg::isValid( QString& msg  )
380 {
381   bool ok = true;
382   if( getConstructorId() == 0 )
383     ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
384   else if( getConstructorId() == 1 )
385     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
386   return getConstructorId() == 0 ? !myPoint->_is_nil() && ok : ok;
387 }
388
389 //=================================================================================
390 // function : execute
391 // purpose  :
392 //=================================================================================
393 bool PrimitiveGUI_SphereDlg::execute( ObjectList& objects )
394 {
395   bool res = false;
396   
397   GEOM::GEOM_Object_var anObj;
398
399   switch ( getConstructorId() ) {
400   case 0 :
401     {
402       if ( !CORBA::is_nil( myPoint ) ) {
403         anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeSpherePntR( myPoint, getRadius() );
404         if (!anObj->_is_nil() && !IsPreview())
405         {
406           QStringList aParameters;
407           aParameters << GroupPoints->SpinBox_DX->text();
408           anObj->SetParameters(GeometryGUI::JoinObjectParameters(aParameters));
409         }
410         res = true;
411       }
412       break;
413     }
414   case 1 :
415     {
416       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeSphereR( getRadius() );
417       if (!anObj->_is_nil() && !IsPreview())
418       {
419         QStringList aParameters;
420         aParameters << GroupDimensions->SpinBox_DX->text();
421         anObj->SetParameters(GeometryGUI::JoinObjectParameters(aParameters));
422       }
423       res = true;
424       break;
425     }
426   }
427   
428   if ( !anObj->_is_nil() )
429     objects.push_back( anObj._retn() );
430   
431   return res;
432 }
433
434
435 //=================================================================================
436 // function : getRadius()
437 // purpose  :
438 //=================================================================================
439 double PrimitiveGUI_SphereDlg::getRadius() const
440 {
441   int aConstructorId = getConstructorId();
442   if ( aConstructorId == 0 )
443     return GroupPoints->SpinBox_DX->value();
444   else if ( aConstructorId == 1 )
445     return GroupDimensions->SpinBox_DX->value();
446   return 0;
447 }
448
449 //=================================================================================
450 // function : addSubshapeToStudy
451 // purpose  : virtual method to add new SubObjects if local selection
452 //=================================================================================
453 void PrimitiveGUI_SphereDlg::addSubshapesToStudy()
454 {
455   QMap<QString, GEOM::GEOM_Object_var> objMap;
456
457   switch ( getConstructorId() ) {
458   case 0:
459     objMap[GroupPoints->LineEdit1->text()] = myPoint;
460     break;
461   case 1:
462     return;
463   }
464   addSubshapesToFather( objMap );
465 }