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