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