Salome HOME
NPAL 18378
[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 //
23 //
24 //  File   : PrimitiveGUI_SphereDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "PrimitiveGUI_SphereDlg.h"
30
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "LightApp_SelectionMgr.h"
35
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS.hxx>
38 #include <TopExp.hxx>
39 #include <TColStd_IndexedMapOfInteger.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
41
42 #include <qlabel.h>
43
44 #include "GEOMImpl_Types.hxx"
45
46 #include "utilities.h"
47
48 using namespace std;
49
50 //=================================================================================
51 // class    : PrimitiveGUI_SphereDlg()
52 // purpose  : Constructs a PrimitiveGUI_SphereDlg which is a child of 'parent', with the 
53 //            name 'name' and widget flags set to 'f'.
54 //            The dialog will by default be modeless, unless you set 'modal' to
55 //            TRUE to construct a modal dialog.
56 //=================================================================================
57 PrimitiveGUI_SphereDlg::PrimitiveGUI_SphereDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
58                                                const char* name, bool modal, WFlags fl)
59   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
60                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
61 {
62   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_SPHERE_P")));
63   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_SPHERE_DXYZ")));
64   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
65
66   setCaption(tr("GEOM_SPHERE_TITLE"));
67
68   /***************************************************************/
69   GroupConstructors->setTitle(tr("GEOM_SPHERE"));
70   RadioButton1->setPixmap(image0);
71   RadioButton2->setPixmap(image1);
72   RadioButton3->close(TRUE);
73
74   GroupPoints = new DlgRef_1Sel1Spin(this, "GroupPoints");
75   GroupPoints->GroupBox1->setTitle(tr("GEOM_SPHERE_CR"));
76   GroupPoints->TextLabel1->setText(tr("GEOM_CENTER"));
77   GroupPoints->TextLabel2->setText(tr("GEOM_RADIUS"));
78   GroupPoints->PushButton1->setPixmap(image2);
79
80   GroupDimensions = new DlgRef_1Spin(this, "GroupDimensions");
81   GroupDimensions->GroupBox1->setTitle(tr("GEOM_SPHERE_RO"));
82   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS"));
83
84   Layout1->addWidget(GroupPoints, 2, 0);
85   Layout1->addWidget(GroupDimensions, 2, 0);
86   /***************************************************************/
87
88   setHelpFileName("sphere.htm");
89
90   Init();
91 }
92
93 //=================================================================================
94 // function : ~PrimitiveGUI_SphereDlg()
95 // purpose  : Destroys the object and frees any allocated resources
96 //=================================================================================
97 PrimitiveGUI_SphereDlg::~PrimitiveGUI_SphereDlg()
98 {  
99   /* no need to delete child widgets, Qt does it all for us */
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   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
120   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
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   connect(GroupConstructors, SIGNAL(clicked(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)),
136           GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
137   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
138           GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
139
140   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
141           this, SLOT(SelectionIntoArgument()));
142
143   initName(tr("GEOM_SPHERE"));
144   ConstructorsClicked(0);
145 }
146
147 //=================================================================================
148 // function : ConstructorsClicked()
149 // purpose  : Radio button management
150 //=================================================================================
151 void PrimitiveGUI_SphereDlg::ConstructorsClicked (int constructorId)
152 {
153   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
154
155   switch (constructorId)
156     {
157     case 0:
158       {
159         globalSelection(); // close local contexts, if any
160         localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
161         GroupDimensions->hide();
162         resize(0, 0);
163         GroupPoints->show();
164
165         myEditCurrentArgument = GroupPoints->LineEdit1;
166         GroupPoints->LineEdit1->setText("");
167         myPoint = GEOM::GEOM_Object::_nil();
168
169         connect(myGeomGUI->getApp()->selectionMgr(), 
170                 SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
171         break;
172       }
173     case 1:
174       {
175         GroupPoints->hide();
176         resize(0, 0);
177         GroupDimensions->show();
178         
179         break;
180       }
181     }
182   displayPreview();
183 }
184
185 //=================================================================================
186 // function : ClickOnOk()
187 // purpose  :
188 //=================================================================================
189 void PrimitiveGUI_SphereDlg::ClickOnOk()
190 {
191   if ( ClickOnApply() )
192     ClickOnCancel();
193 }
194
195 //=================================================================================
196 // function : ClickOnApply()
197 // purpose  :
198 //=================================================================================
199 bool PrimitiveGUI_SphereDlg::ClickOnApply()
200 {
201   if ( !onAccept() )
202     return false;
203   
204   initName();
205   ConstructorsClicked( getConstructorId() );
206   return true;
207 }
208
209 //=================================================================================
210 // function : ClickOnCancel()
211 // purpose  :
212 //=================================================================================
213 void PrimitiveGUI_SphereDlg::ClickOnCancel()
214 {
215   GEOMBase_Skeleton::ClickOnCancel();
216 }
217
218 //=================================================================================
219 // function : SelectionIntoArgument()
220 // purpose  : Called when selection as changed or other case
221 //=================================================================================
222 void PrimitiveGUI_SphereDlg::SelectionIntoArgument()
223 {
224   if (getConstructorId() != 0)
225     return;
226
227   myEditCurrentArgument->setText("");
228
229   if (IObjectCount() != 1) 
230   {
231     myPoint = GEOM::GEOM_Object::_nil();
232     return;
233   }
234
235   /* nbSel == 1 ! */
236   Standard_Boolean testResult = Standard_False;
237   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult);
238
239   if (!testResult || CORBA::is_nil(aSelectedObject))
240     return;
241
242   QString aName = GEOMBase::GetName(aSelectedObject);
243   TopoDS_Shape aShape;
244   if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
245   {
246     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
247     TColStd_IndexedMapOfInteger aMap;
248     aSelMgr->GetIndexes(firstIObject(), aMap);
249     if (aMap.Extent() == 1) // Local Selection
250     {
251       int anIndex = aMap( 1 );
252       aName.append( ":vertex_" + QString::number( anIndex ) );
253
254       //Find SubShape Object in Father
255       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
256
257       if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
258         GEOM::GEOM_IShapesOperations_var aShapesOp =
259           getGeomEngine()->GetIShapesOperations( getStudyId() );
260         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
261       }
262       else
263         aSelectedObject = aFindedObject; // get Object from study
264     }
265     else // Global Selection
266     {
267       if (aShape.ShapeType() != TopAbs_VERTEX) {
268         aSelectedObject = GEOM::GEOM_Object::_nil();
269         aName = "";
270       }
271     }
272   }
273
274   myEditCurrentArgument->setText(aName);
275   myPoint = aSelectedObject;
276
277   displayPreview();
278 }
279
280 //=================================================================================
281 // function : LineEditReturnPressed()
282 // purpose  :
283 //=================================================================================
284 void PrimitiveGUI_SphereDlg::LineEditReturnPressed()
285 {
286   QLineEdit* send = (QLineEdit*)sender();
287   if(send == GroupPoints->LineEdit1)
288     {
289       myEditCurrentArgument = send;
290       GEOMBase_Skeleton::LineEditReturnPressed();
291     }
292 }
293
294 //=================================================================================
295 // function : SetEditCurrentArgument()
296 // purpose  :
297 //=================================================================================
298 void PrimitiveGUI_SphereDlg::SetEditCurrentArgument()
299 {
300   QPushButton* send = (QPushButton*)sender();
301   
302   if (send == GroupPoints->PushButton1) {
303     GroupPoints->LineEdit1->setFocus();
304     myEditCurrentArgument = GroupPoints->LineEdit1;
305     globalSelection(); // close local contexts, if any
306     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
307     SelectionIntoArgument();
308   }
309 }
310
311 //=================================================================================
312 // function : ActivateThisDialog()
313 // purpose  :
314 //=================================================================================
315 void PrimitiveGUI_SphereDlg::ActivateThisDialog()
316 {
317   GEOMBase_Skeleton::ActivateThisDialog();
318   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
319           this, SLOT(SelectionIntoArgument()));
320
321   ConstructorsClicked(getConstructorId());
322 }
323
324 //=================================================================================
325 // function : DeactivateActiveDialog()
326 // purpose  : public slot to deactivate if active
327 //=================================================================================
328 void PrimitiveGUI_SphereDlg::DeactivateActiveDialog()
329 {
330   GEOMBase_Skeleton::DeactivateActiveDialog();
331 }
332
333 //=================================================================================
334 // function : enterEvent()
335 // purpose  :
336 //=================================================================================
337 void PrimitiveGUI_SphereDlg::enterEvent(QEvent* e)
338 {
339   if ( !GroupConstructors->isEnabled() )
340     ActivateThisDialog();
341 }
342
343 //=================================================================================
344 // function : ValueChangedInSpinBox()
345 // purpose  :
346 //=================================================================================
347 void PrimitiveGUI_SphereDlg::ValueChangedInSpinBox()
348 {
349   displayPreview();
350 }
351
352 //=================================================================================
353 // function : createOperation
354 // purpose  :
355 //=================================================================================
356 GEOM::GEOM_IOperations_ptr PrimitiveGUI_SphereDlg::createOperation()
357 {
358   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
359 }
360
361 //=================================================================================
362 // function : isValid
363 // purpose  :
364 //=================================================================================
365 bool PrimitiveGUI_SphereDlg::isValid( QString& msg  )
366 {
367   return getConstructorId() == 0 ? !myPoint->_is_nil() : true;
368 }
369
370 //=================================================================================
371 // function : execute
372 // purpose  :
373 //=================================================================================
374 bool PrimitiveGUI_SphereDlg::execute (ObjectList& objects)
375 {
376   bool res = false;
377
378   GEOM::GEOM_Object_var anObj;
379
380   switch (getConstructorId()) 
381   {
382   case 0:
383     {
384       if (!CORBA::is_nil(myPoint)) {
385         anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->
386           MakeSpherePntR(myPoint, getRadius());
387         res = true;
388       }
389       break;
390     }
391   case 1:
392     {
393       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->
394         MakeSphereR(getRadius());
395       res = true;
396       break;
397     }
398   }
399
400   if (!anObj->_is_nil())
401     objects.push_back(anObj._retn());
402
403   return res;
404 }
405
406 //=================================================================================
407 // function : closeEvent
408 // purpose  :
409 //=================================================================================
410 void PrimitiveGUI_SphereDlg::closeEvent( QCloseEvent* e )
411 {
412   GEOMBase_Skeleton::closeEvent( e );
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->GetValue();
424   else if (aConstructorId == 1)
425     return GroupDimensions->SpinBox_DX->GetValue();
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   {
439   case 0:
440     objMap[GroupPoints->LineEdit1->text()] = myPoint;
441     break;
442   case 1:
443     return;
444   }
445  addSubshapesToFather( objMap );
446 }