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