Salome HOME
NPAL 16768 Select Vertex from Vertex on Shape
[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 //=================================================================================
95 // function : ~PrimitiveGUI_SphereDlg()
96 // purpose  : Destroys the object and frees any allocated resources
97 //=================================================================================
98 PrimitiveGUI_SphereDlg::~PrimitiveGUI_SphereDlg()
99 {  
100   /* no need to delete child widgets, Qt does it all for us */
101 }
102
103
104 //=================================================================================
105 // function : Init()
106 // purpose  :
107 //=================================================================================
108 void PrimitiveGUI_SphereDlg::Init()
109 {
110   /* init variables */
111   myEditCurrentArgument = GroupPoints->LineEdit1;
112   GroupPoints->LineEdit1->setReadOnly( true );
113   
114   myPoint = GEOM::GEOM_Object::_nil();
115   
116   /* Get setting of step value from file configuration */
117   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
118   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
119
120   /* min, max, step and decimals for spin boxes */
121   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
122   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
123   GroupPoints->SpinBox_DX->SetValue(100.0);
124   GroupDimensions->SpinBox_DX->SetValue(100.0);
125   
126   /* signals and slots connections */
127   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
128   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
129   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
130
131   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
132   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
133
134   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
135   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
136
137   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
138   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
139
140   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
141           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
142
143   initName(tr("GEOM_SPHERE"));
144   ConstructorsClicked(0);
145 }
146
147
148 //=================================================================================
149 // function : ConstructorsClicked()
150 // purpose  : Radio button management
151 //=================================================================================
152 void PrimitiveGUI_SphereDlg::ConstructorsClicked(int constructorId)
153 {
154   disconnect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0);
155   
156   switch (constructorId)
157     {
158     case 0:
159       {
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(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->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 //=================================================================================
187 // function : ClickOnOk()
188 // purpose  :
189 //=================================================================================
190 void PrimitiveGUI_SphereDlg::ClickOnOk()
191 {
192   if ( ClickOnApply() )
193     ClickOnCancel();
194 }
195
196
197 //=================================================================================
198 // function : ClickOnApply()
199 // purpose  :
200 //=================================================================================
201 bool PrimitiveGUI_SphereDlg::ClickOnApply()
202 {
203   if ( !onAccept() )
204     return false;
205   
206   initName();
207   ConstructorsClicked( getConstructorId() );
208   return true;
209 }
210
211
212
213 //=======================================================================
214 // function : ClickOnCancel()
215 // purpose  :
216 //=======================================================================
217 void PrimitiveGUI_SphereDlg::ClickOnCancel()
218 {
219   GEOMBase_Skeleton::ClickOnCancel();
220 }
221
222
223 //=================================================================================
224 // function : SelectionIntoArgument()
225 // purpose  : Called when selection as changed or other case
226 //=================================================================================
227 void PrimitiveGUI_SphereDlg::SelectionIntoArgument()
228 {
229   if ( getConstructorId() != 0 )
230     return;
231   
232   myEditCurrentArgument->setText("");
233   
234   if(IObjectCount() != 1) 
235     {
236       myPoint = GEOM::GEOM_Object::_nil();
237       return;
238     }
239   
240   /* nbSel == 1 ! */
241   Standard_Boolean testResult = Standard_False;
242   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
243   
244   if (!testResult || CORBA::is_nil( aSelectedObject ))
245     return;
246
247   myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
248   TopoDS_Shape aShape;
249   if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
250     {
251       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
252       TColStd_IndexedMapOfInteger aMap;
253       aSelMgr->GetIndexes( firstIObject(), aMap );
254       if ( aMap.Extent() == 1 )
255         {
256           GEOM::GEOM_IShapesOperations_var aShapesOp =
257             getGeomEngine()->GetIShapesOperations( getStudyId() );
258           int anIndex = aMap( 1 );
259           TopTools_IndexedMapOfShape aShapes;
260           TopExp::MapShapes( aShape, aShapes );
261           aShape = aShapes.FindKey( anIndex );
262           aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
263           aSelMgr->clearSelected();
264         }
265     }
266   myPoint = aSelectedObject;
267  
268   displayPreview();
269 }
270
271
272 //=================================================================================
273 // function : LineEditReturnPressed()
274 // purpose  :
275 //=================================================================================
276 void PrimitiveGUI_SphereDlg::LineEditReturnPressed()
277 {
278   QLineEdit* send = (QLineEdit*)sender();
279   if(send == GroupPoints->LineEdit1)
280     {
281       myEditCurrentArgument = send;
282       GEOMBase_Skeleton::LineEditReturnPressed();
283     }
284 }
285
286
287 //=================================================================================
288 // function : SetEditCurrentArgument()
289 // purpose  :
290 //=================================================================================
291 void PrimitiveGUI_SphereDlg::SetEditCurrentArgument()
292 {
293   QPushButton* send = (QPushButton*)sender();
294   
295   if(send == GroupPoints->PushButton1) {
296     GroupPoints->LineEdit1->setFocus();
297     myEditCurrentArgument = GroupPoints->LineEdit1;
298     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
299     SelectionIntoArgument();
300   }
301 }
302
303
304 //=================================================================================
305 // function : ActivateThisDialog()
306 // purpose  :
307 //=================================================================================
308 void PrimitiveGUI_SphereDlg::ActivateThisDialog()
309 {
310   GEOMBase_Skeleton::ActivateThisDialog();
311   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
312           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
313   
314   ConstructorsClicked( getConstructorId() );
315 }
316
317
318 //=================================================================================
319 // function : DeactivateActiveDialog()
320 // purpose  : public slot to deactivate if active
321 //=================================================================================
322 void PrimitiveGUI_SphereDlg::DeactivateActiveDialog()
323 {
324   GEOMBase_Skeleton::DeactivateActiveDialog();
325 }
326
327
328 //=================================================================================
329 // function : enterEvent()
330 // purpose  :
331 //=================================================================================
332 void PrimitiveGUI_SphereDlg::enterEvent(QEvent* e)
333 {
334   if ( !GroupConstructors->isEnabled() )
335     ActivateThisDialog();
336 }
337
338
339 //=================================================================================
340 // function : ValueChangedInSpinBox()
341 // purpose  :
342 //=================================================================================
343 void PrimitiveGUI_SphereDlg::ValueChangedInSpinBox()
344 {
345   displayPreview();
346 }
347
348
349 //=================================================================================
350 // function : createOperation
351 // purpose  :
352 //=================================================================================
353 GEOM::GEOM_IOperations_ptr PrimitiveGUI_SphereDlg::createOperation()
354 {
355   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
356 }
357
358
359 //=================================================================================
360 // function : isValid
361 // purpose  :
362 //=================================================================================
363 bool PrimitiveGUI_SphereDlg::isValid( QString& msg  )
364 {
365   return getConstructorId() == 0 ? !myPoint->_is_nil() : true;
366 }
367
368 //=================================================================================
369 // function : execute
370 // purpose  :
371 //=================================================================================
372 bool  PrimitiveGUI_SphereDlg::execute( ObjectList& objects )
373 {
374   bool res = false;
375   
376   GEOM::GEOM_Object_var anObj;
377
378   switch ( getConstructorId() ) 
379   {
380   case 0 :
381     {
382       if ( !CORBA::is_nil( myPoint ) ) {
383         anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeSpherePntR( myPoint, getRadius() );
384         res = true;
385       }
386       break;
387     }
388   case 1 :
389     {
390       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeSphereR( getRadius() );
391       res = true;
392       break;
393     }
394   }
395   
396   if ( !anObj->_is_nil() )
397     objects.push_back( anObj._retn() );
398   
399   return res;
400 }
401
402 //=================================================================================
403 // function : closeEvent
404 // purpose  :
405 //=================================================================================
406 void PrimitiveGUI_SphereDlg::closeEvent( QCloseEvent* e )
407 {
408   //myGeomGUI->SetState( -1 );
409   GEOMBase_Skeleton::closeEvent( e );
410 }
411
412
413
414 //=================================================================================
415 // function : getRadius()
416 // purpose  :
417 //=================================================================================
418 double PrimitiveGUI_SphereDlg::getRadius() const
419 {
420   int aConstructorId = getConstructorId();
421   if (aConstructorId == 0)
422     return GroupPoints->SpinBox_DX->GetValue();
423   else if (aConstructorId == 1)
424     return GroupDimensions->SpinBox_DX->GetValue();
425   return 0;
426 }