]> SALOME platform Git repositories - modules/geom.git/blob - src/PrimitiveGUI/PrimitiveGUI_SphereDlg.cxx
Salome HOME
Bug 0020413: Dump file has many GetMainShape instructions.
[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
25 //  Module : GEOM
26 //  $Header$
27 //
28 #include "PrimitiveGUI_SphereDlg.h"
29
30 #include "SUIT_Desktop.h"
31 #include "SUIT_Session.h"
32 #include "SalomeApp_Application.h"
33 #include "LightApp_SelectionMgr.h"
34
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS.hxx>
37 #include <TopExp.hxx>
38 #include <TColStd_IndexedMapOfInteger.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40
41 #include <qlabel.h>
42
43 #include "GEOMImpl_Types.hxx"
44
45 #include "utilities.h"
46
47 using namespace std;
48
49 //=================================================================================
50 // class    : PrimitiveGUI_SphereDlg()
51 // purpose  : Constructs a PrimitiveGUI_SphereDlg which is a child of 'parent', with the 
52 //            name 'name' and widget flags set to 'f'.
53 //            The dialog will by default be modeless, unless you set 'modal' to
54 //            TRUE to construct a modal dialog.
55 //=================================================================================
56 PrimitiveGUI_SphereDlg::PrimitiveGUI_SphereDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
57                                                const char* name, bool modal, WFlags fl)
58   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
59                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
60 {
61   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_SPHERE_P")));
62   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_SPHERE_DXYZ")));
63   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
64
65   setCaption(tr("GEOM_SPHERE_TITLE"));
66
67   /***************************************************************/
68   GroupConstructors->setTitle(tr("GEOM_SPHERE"));
69   RadioButton1->setPixmap(image0);
70   RadioButton2->setPixmap(image1);
71   RadioButton3->close(TRUE);
72
73   GroupPoints = new DlgRef_1Sel1Spin(this, "GroupPoints");
74   GroupPoints->GroupBox1->setTitle(tr("GEOM_SPHERE_CR"));
75   GroupPoints->TextLabel1->setText(tr("GEOM_CENTER"));
76   GroupPoints->TextLabel2->setText(tr("GEOM_RADIUS"));
77   GroupPoints->PushButton1->setPixmap(image2);
78
79   GroupDimensions = new DlgRef_1Spin(this, "GroupDimensions");
80   GroupDimensions->GroupBox1->setTitle(tr("GEOM_SPHERE_RO"));
81   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS"));
82
83   Layout1->addWidget(GroupPoints, 2, 0);
84   Layout1->addWidget(GroupDimensions, 2, 0);
85   /***************************************************************/
86
87   setHelpFileName("create_sphere_page.html");
88
89   Init();
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 // 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   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
119   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, 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   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
127
128   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
129   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
130
131   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
132   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
133
134   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
135           GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
136   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
137           GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
138
139   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
140           this, SLOT(SelectionIntoArgument()));
141
142   initName(tr("GEOM_SPHERE"));
143   ConstructorsClicked(0);
144 }
145
146 //=================================================================================
147 // function : ConstructorsClicked()
148 // purpose  : Radio button management
149 //=================================================================================
150 void PrimitiveGUI_SphereDlg::ConstructorsClicked (int constructorId)
151 {
152   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
153
154   switch (constructorId)
155     {
156     case 0:
157       {
158         globalSelection(); // close local contexts, if any
159         localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
160         GroupDimensions->hide();
161         resize(0, 0);
162         GroupPoints->show();
163
164         myEditCurrentArgument = GroupPoints->LineEdit1;
165         GroupPoints->LineEdit1->setText("");
166         myPoint = GEOM::GEOM_Object::_nil();
167
168         connect(myGeomGUI->getApp()->selectionMgr(), 
169                 SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
170         break;
171       }
172     case 1:
173       {
174         GroupPoints->hide();
175         globalSelection(); // close local contexts, if any
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 = 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 }