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