Salome HOME
f1dbaf002fa7df0a60b92dd39f7d37571cd8f313
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : PrimitiveGUI_SphereDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "PrimitiveGUI_SphereDlg.h"
31
32 #include "QAD_Config.h"
33 #include "QAD_Desktop.h"
34
35 #include "GEOMImpl_Types.hxx"
36
37 #include "utilities.h"
38
39 using namespace std;
40
41 //=================================================================================
42 // class    : PrimitiveGUI_SphereDlg()
43 // purpose  : Constructs a PrimitiveGUI_SphereDlg which is a child of 'parent', with the 
44 //            name 'name' and widget flags set to 'f'.
45 //            The dialog will by default be modeless, unless you set 'modal' to
46 //            TRUE to construct a modal dialog.
47 //=================================================================================
48 PrimitiveGUI_SphereDlg::PrimitiveGUI_SphereDlg(QWidget* parent,  const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
49   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
50 {
51   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_SPHERE_P")));
52   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_SPHERE_DXYZ")));
53   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
54
55   setCaption(tr("GEOM_SPHERE_TITLE"));
56
57   /***************************************************************/
58   GroupConstructors->setTitle(tr("GEOM_SPHERE"));
59   RadioButton1->setPixmap(image0);
60   RadioButton2->setPixmap(image1);
61   RadioButton3->close(TRUE);
62
63   GroupPoints = new DlgRef_1Sel1Spin(this, "GroupPoints");
64   GroupPoints->GroupBox1->setTitle(tr("GEOM_SPHERE_CR"));
65   GroupPoints->TextLabel1->setText(tr("GEOM_CENTER"));
66   GroupPoints->TextLabel2->setText(tr("GEOM_RADIUS"));
67   GroupPoints->PushButton1->setPixmap(image2);
68
69   GroupDimensions = new DlgRef_1Spin(this, "GroupDimensions");
70   GroupDimensions->GroupBox1->setTitle(tr("GEOM_SPHERE_RO"));
71   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS"));
72
73   Layout1->addWidget(GroupPoints, 2, 0);
74   Layout1->addWidget(GroupDimensions, 2, 0);
75   /***************************************************************/
76
77   Init();
78 }
79
80
81 //=================================================================================
82 // function : ~PrimitiveGUI_SphereDlg()
83 // purpose  : Destroys the object and frees any allocated resources
84 //=================================================================================
85 PrimitiveGUI_SphereDlg::~PrimitiveGUI_SphereDlg()
86 {  
87   /* no need to delete child widgets, Qt does it all for us */
88 }
89
90
91 //=================================================================================
92 // function : Init()
93 // purpose  :
94 //=================================================================================
95 void PrimitiveGUI_SphereDlg::Init()
96 {
97   /* init variables */
98   myEditCurrentArgument = GroupPoints->LineEdit1;
99   GroupPoints->LineEdit1->setReadOnly( true );
100   
101   myPoint = GEOM::GEOM_Object::_nil();
102   
103   /* Get setting of step value from file configuration */
104   QString St = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
105   double step = St.toDouble();
106
107   /* min, max, step and decimals for spin boxes */
108   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
109   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
110   GroupPoints->SpinBox_DX->SetValue(100.0);
111   GroupDimensions->SpinBox_DX->SetValue(100.0);
112   
113   /* signals and slots connections */
114   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
115   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
116   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
117
118   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
119   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
120
121   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
122   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
123
124   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
125   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
126   
127   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
128
129   initName(tr("GEOM_SPHERE"));
130   ConstructorsClicked(0);
131 }
132
133
134 //=================================================================================
135 // function : ConstructorsClicked()
136 // purpose  : Radio button management
137 //=================================================================================
138 void PrimitiveGUI_SphereDlg::ConstructorsClicked(int constructorId)
139 {
140   disconnect(mySelection, 0, this, 0);
141   
142   switch (constructorId)
143     {
144     case 0:
145       {
146         globalSelection( GEOM_POINT );
147         GroupDimensions->hide();
148         resize(0, 0);
149         GroupPoints->show();
150
151         myEditCurrentArgument = GroupPoints->LineEdit1;
152         GroupPoints->LineEdit1->setText("");
153         myPoint = GEOM::GEOM_Object::_nil();
154
155         connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
156         break;
157       }
158     case 1:
159       {
160         GroupPoints->hide();
161         resize(0, 0);
162         GroupDimensions->show();
163         
164         break;
165       }
166     }
167   displayPreview();
168 }
169
170
171 //=================================================================================
172 // function : ClickOnOk()
173 // purpose  :
174 //=================================================================================
175 void PrimitiveGUI_SphereDlg::ClickOnOk()
176 {
177   if ( ClickOnApply() )
178     ClickOnCancel();
179 }
180
181
182 //=================================================================================
183 // function : ClickOnApply()
184 // purpose  :
185 //=================================================================================
186 bool PrimitiveGUI_SphereDlg::ClickOnApply()
187 {
188   if ( !onAccept() )
189     return false;
190   
191   initName();
192   ConstructorsClicked( getConstructorId() );
193   return true;
194 }
195
196
197
198 //=======================================================================
199 // function : ClickOnCancel()
200 // purpose  :
201 //=======================================================================
202 void PrimitiveGUI_SphereDlg::ClickOnCancel()
203 {
204   GEOMBase_Skeleton::ClickOnCancel();
205 }
206
207
208 //=================================================================================
209 // function : SelectionIntoArgument()
210 // purpose  : Called when selection as changed or other case
211 //=================================================================================
212 void PrimitiveGUI_SphereDlg::SelectionIntoArgument()
213 {
214   if ( getConstructorId() != 0 )
215     return;
216   
217   myEditCurrentArgument->setText("");
218   
219   if(mySelection->IObjectCount() != 1) 
220     {
221       myPoint = GEOM::GEOM_Object::_nil();
222       return;
223     }
224   
225   /* nbSel == 1 ! */
226   Standard_Boolean testResult = Standard_False;
227   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject( mySelection->firstIObject(), testResult );
228   
229   if (!testResult || CORBA::is_nil( aSelectedObject ))
230     return;
231     
232   myPoint = aSelectedObject;
233   myEditCurrentArgument->setText( GEOMBase::GetName( myPoint ) );
234   
235   displayPreview();
236 }
237
238
239 //=================================================================================
240 // function : LineEditReturnPressed()
241 // purpose  :
242 //=================================================================================
243 void PrimitiveGUI_SphereDlg::LineEditReturnPressed()
244 {
245   QLineEdit* send = (QLineEdit*)sender();
246   if(send == GroupPoints->LineEdit1)
247     {
248       myEditCurrentArgument = send;
249       GEOMBase_Skeleton::LineEditReturnPressed();
250     }
251 }
252
253
254 //=================================================================================
255 // function : SetEditCurrentArgument()
256 // purpose  :
257 //=================================================================================
258 void PrimitiveGUI_SphereDlg::SetEditCurrentArgument()
259 {
260   QPushButton* send = (QPushButton*)sender();
261   
262   if(send == GroupPoints->PushButton1) {
263     GroupPoints->LineEdit1->setFocus();
264     myEditCurrentArgument = GroupPoints->LineEdit1;
265     globalSelection( GEOM_POINT );
266     SelectionIntoArgument();
267   }
268 }
269
270
271 //=================================================================================
272 // function : ActivateThisDialog()
273 // purpose  :
274 //=================================================================================
275 void PrimitiveGUI_SphereDlg::ActivateThisDialog()
276 {
277   GEOMBase_Skeleton::ActivateThisDialog();
278   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
279   
280   ConstructorsClicked( getConstructorId() );
281 }
282
283
284 //=================================================================================
285 // function : DeactivateActiveDialog()
286 // purpose  : public slot to deactivate if active
287 //=================================================================================
288 void PrimitiveGUI_SphereDlg::DeactivateActiveDialog()
289 {
290   GEOMBase_Skeleton::DeactivateActiveDialog();
291 }
292
293
294 //=================================================================================
295 // function : enterEvent()
296 // purpose  :
297 //=================================================================================
298 void PrimitiveGUI_SphereDlg::enterEvent(QEvent* e)
299 {
300   if ( !GroupConstructors->isEnabled() )
301     ActivateThisDialog();
302 }
303
304
305 //=================================================================================
306 // function : ValueChangedInSpinBox()
307 // purpose  :
308 //=================================================================================
309 void PrimitiveGUI_SphereDlg::ValueChangedInSpinBox()
310 {
311   displayPreview();
312 }
313
314
315 //=================================================================================
316 // function : createOperation
317 // purpose  :
318 //=================================================================================
319 GEOM::GEOM_IOperations_ptr PrimitiveGUI_SphereDlg::createOperation()
320 {
321   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
322 }
323
324
325 //=================================================================================
326 // function : isValid
327 // purpose  :
328 //=================================================================================
329 bool PrimitiveGUI_SphereDlg::isValid( QString& msg  )
330 {
331   return getConstructorId() == 0 ? !myPoint->_is_nil() : true;
332 }
333
334 //=================================================================================
335 // function : execute
336 // purpose  :
337 //=================================================================================
338 bool  PrimitiveGUI_SphereDlg::execute( ObjectList& objects )
339 {
340   bool res = false;
341   
342   GEOM::GEOM_Object_var anObj;
343
344   switch ( getConstructorId() ) 
345   {
346   case 0 :
347     {
348       if ( !CORBA::is_nil( myPoint ) ) {
349         anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeSpherePntR( myPoint, getRadius() );
350         res = true;
351       }
352       break;
353     }
354   case 1 :
355     {
356       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeSphereR( getRadius() );
357       res = true;
358       break;
359     }
360   }
361   
362   if ( !anObj->_is_nil() )
363     objects.push_back( anObj._retn() );
364   
365   return res;
366 }
367
368 //=================================================================================
369 // function : closeEvent
370 // purpose  :
371 //=================================================================================
372 void PrimitiveGUI_SphereDlg::closeEvent( QCloseEvent* e )
373 {
374   myGeomGUI->SetState( -1 );
375   GEOMBase_Skeleton::closeEvent( e );
376 }
377
378
379
380 //=================================================================================
381 // function : getRadius()
382 // purpose  :
383 //=================================================================================
384 double PrimitiveGUI_SphereDlg::getRadius() const
385 {
386   int aConstructorId = getConstructorId();
387   if (aConstructorId == 0)
388     return GroupPoints->SpinBox_DX->GetValue();
389   else if (aConstructorId == 1)
390     return GroupDimensions->SpinBox_DX->GetValue();
391   return 0;
392 }