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