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