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