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