Salome HOME
Porting to Qt4.
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_CylinderDlg.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_CylinderDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "PrimitiveGUI_CylinderDlg.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_CylinderDlg()
48 // purpose  : Constructs a PrimitiveGUI_CylinderDlg 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_CylinderDlg::PrimitiveGUI_CylinderDlg(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_CYLINDER_PV")));
58   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_CYLINDER_DXYZ")));
59   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
60
61   setWindowTitle(tr("GEOM_CYLINDER_TITLE"));
62
63   /***************************************************************/
64   GroupConstructors->setTitle(tr("GEOM_CYLINDER"));
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"));
75   GroupPoints->TextLabel4->setText(tr("GEOM_HEIGHT"));
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"));
82   GroupDimensions->TextLabel2->setText(tr("GEOM_HEIGHT"));
83
84   gridLayout1->addWidget(GroupPoints, 2, 0);
85   gridLayout1->addWidget(GroupDimensions, 2, 0);
86   /***************************************************************/
87
88   setHelpFileName("cylinder.htm");
89
90   Init();
91 }
92
93
94 //=================================================================================
95 // function : ~PrimitiveGUI_CylinderDlg()
96 // purpose  : Destroys the object and frees any allocated resources
97 //=================================================================================
98 PrimitiveGUI_CylinderDlg::~PrimitiveGUI_CylinderDlg()
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_CylinderDlg::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   /* First constructor : radius */
123   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, 3);
124   /* First constructor : algebric height */
125   GroupPoints->SpinBox_DY->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, 3);
126   /* Second constructor : radius */
127   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, 3);
128   /* Second constructor : algebric height */
129   GroupDimensions->SpinBox_DY->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, 3);
130
131   GroupPoints->SpinBox_DX->SetValue(100.0);
132   GroupPoints->SpinBox_DY->SetValue(300.0);
133   GroupDimensions->SpinBox_DX->SetValue(100.0);  
134   GroupDimensions->SpinBox_DY->SetValue(300.0);  
135
136   /* signals and slots connections */
137   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
138   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
139   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
140
141   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
142   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
143
144   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
145   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
146
147   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
148   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
149   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
150   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
151
152   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
153   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
154   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
155   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DY, SLOT(SetStep(double)));
156   
157   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
158           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
159   
160   initName( tr( "GEOM_CYLINDER" ).toStdString().c_str() );
161   ConstructorsClicked(0);
162 }
163
164
165 //=================================================================================
166 // function : ConstructorsClicked()
167 // purpose  : Radio button management
168 //=================================================================================
169 void PrimitiveGUI_CylinderDlg::ConstructorsClicked(int constructorId)
170 {
171   disconnect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0);
172     
173   switch(constructorId)
174     { 
175     case 0 :
176       {
177         globalSelection( GEOM_POINT );
178
179         GroupDimensions->hide();
180         resize(0, 0);
181         GroupPoints->show();
182         
183         myEditCurrentArgument = GroupPoints->LineEdit1;
184         GroupPoints->LineEdit1->setText(tr(""));
185         GroupPoints->LineEdit2->setText(tr(""));
186         myPoint = myDir = GEOM::GEOM_Object::_nil();
187
188         connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
189                 SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
190         break;
191       }
192     case 1 :
193       { 
194         GroupPoints->hide();
195         resize( 0, 0 );
196         GroupDimensions->show();
197         
198         break;
199       }
200     }
201   displayPreview();
202 }
203
204
205 //=================================================================================
206 // function : ClickOnOk()
207 // purpose  :
208 //=================================================================================
209 void PrimitiveGUI_CylinderDlg::ClickOnOk()
210 {
211   if ( ClickOnApply() )
212     ClickOnCancel();
213 }
214
215
216 //=================================================================================
217 // function : ClickOnApply()
218 // purpose  :
219 //=================================================================================
220 bool PrimitiveGUI_CylinderDlg::ClickOnApply()
221 {
222   if ( !onAccept() )
223     return false;
224
225   initName();
226   ConstructorsClicked( getConstructorId() );
227   return true;
228 }
229
230
231 //=======================================================================
232 // function : ClickOnCancel()
233 // purpose  :
234 //=======================================================================
235 void PrimitiveGUI_CylinderDlg::ClickOnCancel()
236 {
237   GEOMBase_Skeleton::ClickOnCancel();
238 }
239
240
241 //=================================================================================
242 // function : SelectionIntoArgument()
243 // purpose  : Called when selection as changed or other case
244 //=================================================================================
245 void PrimitiveGUI_CylinderDlg::SelectionIntoArgument()
246 {
247   if ( getConstructorId() != 0 )
248     return;
249
250   myEditCurrentArgument->setText("");
251   
252   if(IObjectCount() != 1) 
253     {
254       if(myEditCurrentArgument == GroupPoints->LineEdit1)
255         myPoint = GEOM::GEOM_Object::_nil();
256       else if (myEditCurrentArgument == GroupPoints->LineEdit2)
257         myDir = GEOM::GEOM_Object::_nil();
258       return;
259     }
260   
261   /* nbSel == 1 */
262   Standard_Boolean testResult = Standard_False;
263   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
264     
265   if(!testResult || CORBA::is_nil( aSelectedObject ))
266     return;
267   
268   if (myEditCurrentArgument == GroupPoints->LineEdit1)
269     myPoint = aSelectedObject;
270   else if (myEditCurrentArgument == GroupPoints->LineEdit2)
271     myDir = aSelectedObject;
272     
273   myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
274  
275   displayPreview();
276 }
277
278
279 //=================================================================================
280 // function : SetEditCurrentArgument()
281 // purpose  :
282 //=================================================================================
283 void PrimitiveGUI_CylinderDlg::SetEditCurrentArgument()
284 {
285   QPushButton* send = (QPushButton*)sender();
286   
287   if(send == GroupPoints->PushButton1) {
288     myEditCurrentArgument = GroupPoints->LineEdit1;
289     globalSelection( GEOM_POINT );
290   }
291   else if(send == GroupPoints->PushButton2) {
292     myEditCurrentArgument = GroupPoints->LineEdit2;
293     globalSelection( GEOM_LINE );
294   }
295   
296   myEditCurrentArgument->setFocus();
297   SelectionIntoArgument();
298 }
299
300
301 //=================================================================================
302 // function : LineEditReturnPressed()
303 // purpose  :
304 //=================================================================================
305 void PrimitiveGUI_CylinderDlg::LineEditReturnPressed()
306 {  
307   QLineEdit* send = (QLineEdit*)sender();
308   if(send == GroupPoints->LineEdit1 ||
309      send == GroupPoints->LineEdit2)
310     {
311       myEditCurrentArgument = send;
312       GEOMBase_Skeleton::LineEditReturnPressed();
313     }
314 }
315
316
317 //=================================================================================
318 // function : ActivateThisDialog()
319 // purpose  :
320 //=================================================================================
321 void PrimitiveGUI_CylinderDlg::ActivateThisDialog()
322 {
323   GEOMBase_Skeleton::ActivateThisDialog();
324   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
325           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
326   
327   ConstructorsClicked( getConstructorId() );
328 }
329
330
331 //=================================================================================
332 // function : enterEvent()
333 // purpose  :
334 //=================================================================================
335 void PrimitiveGUI_CylinderDlg::enterEvent(QEvent* e)
336 {
337   if ( !GroupConstructors->isEnabled() )
338     ActivateThisDialog();
339 }
340
341
342 //=================================================================================
343 // function : DeactivateActiveDialog()
344 // purpose  : public slot to deactivate if active
345 //=================================================================================
346 void PrimitiveGUI_CylinderDlg::DeactivateActiveDialog()
347 {
348   GEOMBase_Skeleton::DeactivateActiveDialog();
349 }
350
351
352 //=================================================================================
353 // function : ValueChangedInSpinBox
354 // purpose  :
355 //=================================================================================
356 void PrimitiveGUI_CylinderDlg::ValueChangedInSpinBox( )
357 {  
358   displayPreview();
359 }
360
361
362 //=================================================================================
363 // function : createOperation
364 // purpose  :
365 //=================================================================================
366 GEOM::GEOM_IOperations_ptr PrimitiveGUI_CylinderDlg::createOperation()
367 {
368   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
369 }
370
371
372 //=================================================================================
373 // function : isValid
374 // purpose  :
375 //=================================================================================
376 bool PrimitiveGUI_CylinderDlg::isValid( QString& msg )
377 {
378   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil() ) : true;
379 }
380
381 //=================================================================================
382 // function : execute
383 // purpose  :
384 //=================================================================================
385 bool  PrimitiveGUI_CylinderDlg::execute( ObjectList& objects )
386 {
387   bool res = false;
388   
389   GEOM::GEOM_Object_var anObj;
390   
391   switch ( getConstructorId() ) 
392     {
393     case 0 :
394       {
395         if ( !CORBA::is_nil( myPoint ) && !CORBA::is_nil( myDir )) {
396           anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeCylinderPntVecRH(myPoint, myDir, getRadius(), getHeight());
397           res = true;
398         }
399         break;
400       }
401     case 1 :
402       {
403         anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeCylinderRH(getRadius(), getHeight());
404         res = true;
405
406         break;
407       }
408     }
409   
410   if ( !anObj->_is_nil() )
411     objects.push_back( anObj._retn() );
412   
413   return res;
414 }
415
416 //=================================================================================
417 // function : closeEvent
418 // purpose  :
419 //=================================================================================
420 void  PrimitiveGUI_CylinderDlg::closeEvent( QCloseEvent* e )
421 {
422   GEOMBase_Skeleton::closeEvent( e );
423 }
424
425
426 //=================================================================================
427 // function : getRadius()
428 // purpose  :
429 //=================================================================================
430 double PrimitiveGUI_CylinderDlg::getRadius() const
431 {
432   int aConstructorId = getConstructorId();
433   if (aConstructorId == 0)
434     return GroupPoints->SpinBox_DX->GetValue();
435   else if (aConstructorId == 1)
436     return GroupDimensions->SpinBox_DX->GetValue();
437   return 0;
438 }
439
440
441 //=================================================================================
442 // function : getHeight()
443 // purpose  :
444 //=================================================================================
445 double PrimitiveGUI_CylinderDlg::getHeight() const
446 {
447   int aConstructorId = getConstructorId();
448   if (aConstructorId == 0)
449     return GroupPoints->SpinBox_DY->GetValue();
450   else if (aConstructorId == 1)
451     return GroupDimensions->SpinBox_DY->GetValue();
452   return 0;
453 }