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