Salome HOME
05e6c7b6c4aeef236c24e38b586fe05b5f01e859
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_TorusDlg.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_TorusDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "PrimitiveGUI_TorusDlg.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_TorusDlg()
46 // purpose  : Constructs a PrimitiveGUI_TorusDlg 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_TorusDlg::PrimitiveGUI_TorusDlg(GeometryGUI* theGeometryGUI, QWidget* parent, const char* name, bool modal, WFlags fl)
52   :GEOMBase_Skeleton(parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu), myGeometryGUI(theGeometryGUI)
53 {
54   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_TORUS_PV")));
55   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_TORUS_DXYZ")));
56   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
57     
58   setCaption(tr("GEOM_TORUS_TITLE"));
59
60   /***************************************************************/
61   GroupConstructors->setTitle(tr("GEOM_TORUS"));
62   RadioButton1->setPixmap(image0);
63   RadioButton2->setPixmap(image1);
64   RadioButton3->close(TRUE);
65
66   GroupPoints = new DlgRef_2Sel2Spin(this, "GroupPoints");
67   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
68   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_POINT"));
69   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
70   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS_I").arg("1"));
71   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_I").arg("2"));
72   GroupPoints->PushButton1->setPixmap(image2);
73   GroupPoints->PushButton2->setPixmap(image2);
74
75   GroupDimensions = new DlgRef_2Spin(this, "GroupDimensions");
76   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
77   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS_I").arg("1"));
78   GroupDimensions->TextLabel2->setText(tr("GEOM_RADIUS_I").arg("2"));
79
80   Layout1->addWidget(GroupPoints, 2, 0);
81   Layout1->addWidget(GroupDimensions, 2, 0);
82   /***************************************************************/
83   
84   Init();
85 }
86
87
88 //=================================================================================
89 // function : ~PrimitiveGUI_TorusDlg()
90 // purpose  : Destroys the object and frees any allocated resources
91 //=================================================================================
92 PrimitiveGUI_TorusDlg::~PrimitiveGUI_TorusDlg()
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_TorusDlg::Init()
103 {
104   /* init variables */
105   myEditCurrentArgument = GroupPoints->LineEdit1;
106   GroupPoints->LineEdit1->setReadOnly( true );
107   GroupPoints->LineEdit2->setReadOnly( true );
108  
109   myPoint = myDir = GEOM::GEOM_Object::_nil();
110
111   /* Get setting of step value from file configuration */
112   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
113   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
114
115   /* min, max, step and decimals for spin boxes & initial values */
116   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
117   GroupPoints->SpinBox_DY->RangeStepAndValidator(0.001, 999.999, step, 3);
118   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
119   GroupDimensions->SpinBox_DY->RangeStepAndValidator(0.001, 999.999, step, 3);
120
121   GroupPoints->SpinBox_DX->SetValue(300.0);
122   GroupPoints->SpinBox_DY->SetValue(100.0);
123   GroupDimensions->SpinBox_DX->SetValue(300.0);
124   GroupDimensions->SpinBox_DY->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->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
133
134   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
135   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
136
137   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
138   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
139   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
140   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
141
142   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
143   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
144   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
145   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DY, SLOT(SetStep(double)));
146   
147   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
148           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
149   
150   initName( tr( "GEOM_TORUS" ) );
151   ConstructorsClicked(0);
152 }
153
154
155
156 //=================================================================================
157 // function : ConstructorsClicked()
158 // purpose  : Radio button management
159 //=================================================================================
160 void PrimitiveGUI_TorusDlg::ConstructorsClicked(int constructorId)
161 {
162   disconnect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0);
163   
164   switch(constructorId)
165     { 
166     case 0 :
167       {
168         globalSelection( GEOM_LINE );
169
170         GroupDimensions->hide();
171         resize(0, 0);
172         GroupPoints->show();
173         
174         myEditCurrentArgument = GroupPoints->LineEdit1;
175         GroupPoints->LineEdit1->setText(tr(""));
176         GroupPoints->LineEdit2->setText(tr(""));
177         myPoint = myDir = GEOM::GEOM_Object::_nil();
178         
179         connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
180                 SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
181         break ;
182       }
183     case 1 :
184       {
185         GroupPoints->hide();
186         resize( 0, 0 );
187         GroupDimensions->show();
188         
189         break ;
190       }
191     }
192   displayPreview();
193 }
194
195
196 //=================================================================================
197 // function : ClickOnOk()
198 // purpose  :
199 //=================================================================================
200 void PrimitiveGUI_TorusDlg::ClickOnOk()
201 {
202   if ( ClickOnApply() )
203     ClickOnCancel();
204 }
205
206
207 //=================================================================================
208 // function : ClickOnApply()
209 // purpose  :
210 //=================================================================================
211 bool PrimitiveGUI_TorusDlg::ClickOnApply()
212 {
213   if ( !onAccept() )
214     return false;
215
216   initName();
217   ConstructorsClicked( getConstructorId() );
218   return true;
219 }
220
221
222 //=======================================================================
223 // function : ClickOnCancel()
224 // purpose  :
225 //=======================================================================
226 void PrimitiveGUI_TorusDlg::ClickOnCancel()
227 {
228   GEOMBase_Skeleton::ClickOnCancel();
229 }
230
231
232 //=================================================================================
233 // function : SelectionIntoArgument()
234 // purpose  : Called when selection as changed or other case
235 //=================================================================================
236 void PrimitiveGUI_TorusDlg::SelectionIntoArgument()
237 {
238   if ( getConstructorId() != 0 )
239     return;
240   
241   myEditCurrentArgument->setText("");
242   
243   if(IObjectCount() != 1)
244     {
245       if(myEditCurrentArgument == GroupPoints->LineEdit1)
246         myPoint = GEOM::GEOM_Object::_nil();
247       else if(myEditCurrentArgument == GroupPoints->LineEdit2)
248         myDir = GEOM::GEOM_Object::_nil();
249       return;
250     }
251   
252   
253   /* nbSel == 1 */
254   Standard_Boolean testResult = Standard_False;
255   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
256   
257   if(!testResult || CORBA::is_nil( aSelectedObject ))
258     return;
259   
260   if(myEditCurrentArgument == GroupPoints->LineEdit1)
261     myPoint = aSelectedObject;
262   else if(myEditCurrentArgument == GroupPoints->LineEdit2)
263     myDir = aSelectedObject;
264     
265   myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
266   displayPreview();
267 }
268
269
270 //=================================================================================
271 // function : SetEditCurrentArgument()
272 // purpose  :
273 //=================================================================================
274 void PrimitiveGUI_TorusDlg::SetEditCurrentArgument()
275 {
276   QPushButton* send = (QPushButton*)sender();
277   
278   if(send == GroupPoints->PushButton1) {
279     myEditCurrentArgument = GroupPoints->LineEdit1;
280     globalSelection( GEOM_POINT );
281   }
282   else if(send == GroupPoints->PushButton2) {
283     myEditCurrentArgument = GroupPoints->LineEdit2;
284     globalSelection( GEOM_LINE );
285   }
286   
287   myEditCurrentArgument->setFocus();
288   SelectionIntoArgument();
289 }
290
291
292 //=================================================================================
293 // function : LineEditReturnPressed()
294 // purpose  :
295 //=================================================================================
296 void PrimitiveGUI_TorusDlg::LineEditReturnPressed()
297 {  
298   QLineEdit* send = (QLineEdit*)sender();
299   if(send == GroupPoints->LineEdit1 ||
300      send == GroupPoints->LineEdit2)
301     {
302       myEditCurrentArgument = send;
303       GEOMBase_Skeleton::LineEditReturnPressed();
304     }
305 }
306
307
308 //=================================================================================
309 // function : ActivateThisDialog()
310 // purpose  :
311 //=================================================================================
312 void PrimitiveGUI_TorusDlg::ActivateThisDialog()
313 {
314   GEOMBase_Skeleton::ActivateThisDialog();
315   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
316           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
317  
318   ConstructorsClicked( getConstructorId() );
319 }
320
321
322 //=================================================================================
323 // function : enterEvent()
324 // purpose  :
325 //=================================================================================
326 void PrimitiveGUI_TorusDlg::enterEvent(QEvent* e)
327 {
328   if ( !GroupConstructors->isEnabled() )
329     ActivateThisDialog();
330 }
331
332
333 //=================================================================================
334 // function : DeactivateActiveDialog()
335 // purpose  : public slot to deactivate if active
336 //=================================================================================
337 void PrimitiveGUI_TorusDlg::DeactivateActiveDialog()
338 {
339   GEOMBase_Skeleton::DeactivateActiveDialog();
340 }
341
342
343 //=================================================================================
344 // function : ValueChangedInSpinBox
345 // purpose  :
346 //=================================================================================
347 void PrimitiveGUI_TorusDlg::ValueChangedInSpinBox()
348 {
349   displayPreview();
350 }
351
352
353 //=================================================================================
354 // function : createOperation
355 // purpose  :
356 //=================================================================================
357 GEOM::GEOM_IOperations_ptr PrimitiveGUI_TorusDlg::createOperation()
358 {
359   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
360 }
361
362
363 //=================================================================================
364 // function : isValid
365 // purpose  :
366 //=================================================================================
367 bool PrimitiveGUI_TorusDlg::isValid( QString& msg )
368 {
369   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil()) : true;
370 }
371
372
373 //=================================================================================
374 // function : execute
375 // purpose  :
376 //=================================================================================
377 bool PrimitiveGUI_TorusDlg::execute( ObjectList& objects )
378 {
379   bool res = false;
380   
381   GEOM::GEOM_Object_var anObj;
382
383   switch ( getConstructorId() ) 
384   {
385   case 0 :
386   {
387     if (!CORBA::is_nil( myPoint ) && !CORBA::is_nil( myDir )){
388       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeTorusPntVecRR(myPoint, myDir, getRadius1(), getRadius2());
389       res = true;
390     }
391     break;
392   }
393   case 1 :
394     {
395       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeTorusRR(getRadius1(), getRadius2());
396       res = true;
397       break;
398     }
399   }
400
401   if ( !anObj->_is_nil() )
402     objects.push_back( anObj._retn() );
403
404   return res;
405 }
406
407
408 //=================================================================================
409 // function : closeEvent
410 // purpose  :
411 //=================================================================================
412 void PrimitiveGUI_TorusDlg::closeEvent( QCloseEvent* e )
413 {
414   GEOMBase_Skeleton::closeEvent( e );
415 }
416
417
418 //=================================================================================
419 // function : getRadius1()
420 // purpose  :
421 //=================================================================================
422 double PrimitiveGUI_TorusDlg::getRadius1() const
423 {
424   int aConstructorId = getConstructorId();
425   if (aConstructorId == 0)
426     return GroupPoints->SpinBox_DX->GetValue();
427   else if (aConstructorId == 1)
428     return GroupDimensions->SpinBox_DX->GetValue();
429   return 0;
430 }
431
432
433 //=================================================================================
434 // function : getRadius2()
435 // purpose  :
436 //=================================================================================
437 double PrimitiveGUI_TorusDlg::getRadius2() const
438 {
439   int aConstructorId = getConstructorId();
440   if (aConstructorId == 0)
441     return GroupPoints->SpinBox_DY->GetValue();
442   else if (aConstructorId == 1)
443     return GroupDimensions->SpinBox_DY->GetValue();
444   return 0;
445 }