Salome HOME
be5213ff74dc20bab5f3e3c1063423832a606a20
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_SphereDlg.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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.nullify();
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
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( TopAbs_VERTEX );
169
170       GroupDimensions->hide();
171       GroupPoints->show();
172       
173       myEditCurrentArgument = GroupPoints->LineEdit1;
174       GroupPoints->LineEdit1->setText( "" );
175       myPoint.nullify();
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   SelectionIntoArgument();
196
197   displayPreview(true);
198 }
199
200
201 //=================================================================================
202 // function : ClickOnOk()
203 // purpose  :
204 //=================================================================================
205 void PrimitiveGUI_SphereDlg::ClickOnOk()
206 {
207   setIsApplyAndClose( true );
208   if ( ClickOnApply() )
209     ClickOnCancel();
210 }
211
212
213 //=================================================================================
214 // function : ClickOnApply()
215 // purpose  :
216 //=================================================================================
217 bool PrimitiveGUI_SphereDlg::ClickOnApply()
218 {
219   if ( !onAccept() )
220     return false;
221   
222   initName();
223   ConstructorsClicked( getConstructorId() );
224   return true;
225 }
226
227
228 //=================================================================================
229 // function : SelectionIntoArgument()
230 // purpose  : Called when selection as changed or other case
231 //=================================================================================
232 void PrimitiveGUI_SphereDlg::SelectionIntoArgument()
233 {
234   if ( getConstructorId() != 0 )
235     return;
236
237   myEditCurrentArgument->setText( "" );
238
239   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
240   SALOME_ListIO aSelList;
241   aSelMgr->selectedObjects(aSelList);
242
243   if (aSelList.Extent() != 1) {
244     myPoint.nullify();
245     return;
246   }
247
248   GEOM::GeomObjPtr aSelectedObject = getSelected( TopAbs_VERTEX );
249   TopoDS_Shape aShape;
250   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
251     QString aName = GEOMBase::GetName( aSelectedObject.get() );
252     myEditCurrentArgument->setText( aName );
253     myPoint = aSelectedObject;
254   }
255
256   displayPreview(true);
257 }
258
259 //=================================================================================
260 // function : SetEditCurrentArgument()
261 // purpose  :
262 //=================================================================================
263 void PrimitiveGUI_SphereDlg::SetEditCurrentArgument()
264 {
265   QPushButton* send = (QPushButton*)sender();
266   
267   if ( send == GroupPoints->PushButton1 ) {
268     GroupPoints->LineEdit1->setFocus();
269     myEditCurrentArgument = GroupPoints->LineEdit1;
270     globalSelection(); // close local contexts, if any
271     localSelection( TopAbs_VERTEX );
272     SelectionIntoArgument();
273   }
274 }
275
276
277 //=================================================================================
278 // function : ActivateThisDialog()
279 // purpose  :
280 //=================================================================================
281 void PrimitiveGUI_SphereDlg::ActivateThisDialog()
282 {
283   GEOMBase_Skeleton::ActivateThisDialog();
284   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
285            this, SLOT( SelectionIntoArgument() ) );
286   
287   ConstructorsClicked( getConstructorId() );
288 }
289
290
291 //=================================================================================
292 // function : DeactivateActiveDialog()
293 // purpose  : public slot to deactivate if active
294 //=================================================================================
295 void PrimitiveGUI_SphereDlg::DeactivateActiveDialog()
296 {
297   GEOMBase_Skeleton::DeactivateActiveDialog();
298 }
299
300
301 //=================================================================================
302 // function : enterEvent()
303 // purpose  :
304 //=================================================================================
305 void PrimitiveGUI_SphereDlg::enterEvent( QEvent* )
306 {
307   if ( !mainFrame()->GroupConstructors->isEnabled() )
308     ActivateThisDialog();
309 }
310
311
312 //=================================================================================
313 // function : ValueChangedInSpinBox()
314 // purpose  :
315 //=================================================================================
316 void PrimitiveGUI_SphereDlg::ValueChangedInSpinBox()
317 {
318   displayPreview(true);
319 }
320
321
322 //=================================================================================
323 // function : createOperation
324 // purpose  :
325 //=================================================================================
326 GEOM::GEOM_IOperations_ptr PrimitiveGUI_SphereDlg::createOperation()
327 {
328   return getGeomEngine()->GetI3DPrimOperations();
329 }
330
331
332 //=================================================================================
333 // function : isValid
334 // purpose  :
335 //=================================================================================
336 bool PrimitiveGUI_SphereDlg::isValid( QString& msg  )
337 {
338   bool ok = false;
339   if( getConstructorId() == 0 )
340     ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) && myPoint;
341   else if( getConstructorId() == 1 )
342     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() );
343   return ok;
344 }
345
346 //=================================================================================
347 // function : execute
348 // purpose  :
349 //=================================================================================
350 bool PrimitiveGUI_SphereDlg::execute( ObjectList& objects )
351 {
352   bool res = false;
353   
354   GEOM::GEOM_Object_var anObj;
355
356   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
357
358   switch ( getConstructorId() ) {
359   case 0 :
360     {
361       if ( myPoint ) {
362         anObj = anOper->MakeSpherePntR( myPoint.get(), getRadius() );
363         if (!anObj->_is_nil() && !IsPreview())
364         {
365           QStringList aParameters;
366           aParameters << GroupPoints->SpinBox_DX->text();
367           anObj->SetParameters(aParameters.join(":").toUtf8().constData());
368         }
369         res = true;
370       }
371       break;
372     }
373   case 1 :
374     {
375       anObj = anOper->MakeSphereR( getRadius() );
376       if (!anObj->_is_nil() && !IsPreview())
377       {
378         QStringList aParameters;
379         aParameters << GroupDimensions->SpinBox_DX->text();
380         anObj->SetParameters(aParameters.join(":").toUtf8().constData());
381       }
382       res = true;
383       break;
384     }
385   }
386   
387   if ( !anObj->_is_nil() )
388     objects.push_back( anObj._retn() );
389   
390   return res;
391 }
392
393
394 //=================================================================================
395 // function : getRadius()
396 // purpose  :
397 //=================================================================================
398 double PrimitiveGUI_SphereDlg::getRadius() const
399 {
400   int aConstructorId = getConstructorId();
401   if ( aConstructorId == 0 )
402     return GroupPoints->SpinBox_DX->value();
403   else if ( aConstructorId == 1 )
404     return GroupDimensions->SpinBox_DX->value();
405   return 0;
406 }
407
408 //=================================================================================
409 // function : addSubshapeToStudy
410 // purpose  : virtual method to add new SubObjects if local selection
411 //=================================================================================
412 void PrimitiveGUI_SphereDlg::addSubshapesToStudy()
413 {
414   if ( getConstructorId() == 0 ) {
415     GEOMBase::PublishSubObject( myPoint.get() );
416   }
417 }
418
419 //=================================================================================
420 // function : getSourceObjects
421 // purpose  : virtual method to get source objects
422 //=================================================================================
423 QList<GEOM::GeomObjPtr> PrimitiveGUI_SphereDlg::getSourceObjects()
424 {
425   QList<GEOM::GeomObjPtr> res;
426   res << myPoint;
427   return res;
428 }