]> SALOME platform Git repositories - modules/geom.git/blob - src/PrimitiveGUI/PrimitiveGUI_ConeDlg.cxx
Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_17Jan08)
[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 <TopoDS_Shape.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS.hxx>
40 #include <TopExp.hxx>
41 #include <TColStd_IndexedMapOfInteger.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
43
44 #include <GEOMImpl_Types.hxx>
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                                             bool modal, Qt::WindowFlags fl )
55   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
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   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_CONE" ) );
65   mainFrame()->RadioButton1->setIcon( image0 );
66   mainFrame()->RadioButton2->setIcon( image1 );
67   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
68   mainFrame()->RadioButton3->close();
69
70   GroupPoints = new DlgRef_2Sel3Spin( centralWidget() );
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( centralWidget() );
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   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
87   layout->setMargin( 0 ); layout->setSpacing( 6 );
88   layout->addWidget( GroupPoints );
89   layout->addWidget( GroupDimensions );
90   /***************************************************************/
91
92   setHelpFileName( "create_cone_page.html" );
93   
94   Init();
95 }
96
97
98 //=================================================================================
99 // function : ~PrimitiveGUI_ConeDlg()
100 // purpose  : Destroys the object and frees any allocated resources
101 //=================================================================================
102 PrimitiveGUI_ConeDlg::~PrimitiveGUI_ConeDlg()
103 {
104   // no need to delete child widgets, Qt does it all for us
105 }
106
107
108 //=================================================================================
109 // function : Init()
110 // purpose  :
111 //=================================================================================
112 void PrimitiveGUI_ConeDlg::Init()
113 {
114   /* init variables */
115   myEditCurrentArgument = GroupPoints->LineEdit1;
116   GroupPoints->LineEdit1->setReadOnly( true );
117   GroupPoints->LineEdit2->setReadOnly( true );
118   
119   myPoint = myDir = GEOM::GEOM_Object::_nil();
120   
121   /* Get setting of step value from file configuration */
122   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
123   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
124   
125   /* min, max, step and decimals for spin boxes & initial values */
126   initSpinBox( GroupPoints->SpinBox_DX, 0.000, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
127   initSpinBox( GroupPoints->SpinBox_DY, 0.000, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
128   initSpinBox( GroupPoints->SpinBox_DZ, COORD_MIN, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
129   initSpinBox( GroupDimensions->SpinBox_DX, 0.000, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
130   initSpinBox( GroupDimensions->SpinBox_DY, 0.000, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
131   initSpinBox( GroupDimensions->SpinBox_DZ, COORD_MIN, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
132
133   double aRadius1( 100.0 ), aRadius2( 0.0 ), aHeight( 300.0 );
134   GroupPoints->SpinBox_DX->setValue( aRadius1 );
135   GroupPoints->SpinBox_DY->setValue( aRadius2 );
136   GroupPoints->SpinBox_DZ->setValue( aHeight );
137   GroupDimensions->SpinBox_DX->setValue( aRadius1 );
138   GroupDimensions->SpinBox_DY->setValue( aRadius2 );
139   GroupDimensions->SpinBox_DZ->setValue( aHeight );
140
141   /* signals and slots connections */
142   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
143   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
144
145   connect( this,          SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
146
147   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
148   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
149
150   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
151   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
152
153   connect( GroupPoints->SpinBox_DX,     SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
154   connect( GroupPoints->SpinBox_DY,     SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
155   connect( GroupPoints->SpinBox_DZ,     SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
156   connect( GroupDimensions->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
157   connect( GroupDimensions->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
158   connect( GroupDimensions->SpinBox_DZ, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
159
160   // VSR: TODO ->>
161   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupPoints->SpinBox_DX,     SLOT( SetStep( double ) ) );
162   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupPoints->SpinBox_DY,     SLOT( SetStep( double ) ) );
163   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupPoints->SpinBox_DZ,     SLOT( SetStep( double ) ) );
164   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupDimensions->SpinBox_DX, SLOT( SetStep( double ) ) );
165   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupDimensions->SpinBox_DY, SLOT( SetStep( double ) ) );
166   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupDimensions->SpinBox_DZ, SLOT( SetStep( double ) ) );
167   // <<-
168   
169   connect( myGeomGUI->getApp()->selectionMgr(), 
170            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
171   
172   initName( tr( "GEOM_CONE" ) );
173
174   ConstructorsClicked( 0 );
175 }
176
177
178 //=================================================================================
179 // function : ConstructorsClicked()
180 // purpose  : Radio button management
181 //=================================================================================
182 void PrimitiveGUI_ConeDlg::ConstructorsClicked( int constructorId )
183 {
184   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
185   
186   switch( constructorId ) { 
187   case 0 :
188     {
189       //globalSelection( GEOM_POINT );
190       globalSelection(); // to break prvious local selection
191       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
192
193       GroupDimensions->hide();
194       GroupPoints->show();
195       
196       myEditCurrentArgument = GroupPoints->LineEdit1;
197       GroupPoints->LineEdit1->setText( "" );
198       GroupPoints->LineEdit2->setText( "" );
199       myPoint = myDir = GEOM::GEOM_Object::_nil();
200       
201       connect( myGeomGUI->getApp()->selectionMgr(), 
202                SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
203       break;
204     }
205   case 1 :
206     { 
207       globalSelection(); // close local contexts, if any
208
209       GroupPoints->hide();
210       GroupDimensions->show();
211         
212       break;
213     }
214   }
215
216   qApp->processEvents();
217   updateGeometry();
218   resize( minimumSize() );
219
220   displayPreview();
221 }
222
223
224 //=================================================================================
225 // function : ClickOnOk()
226 // purpose  :
227 //=================================================================================
228 void PrimitiveGUI_ConeDlg::ClickOnOk()
229 {
230   if ( ClickOnApply() )
231     ClickOnCancel();
232 }
233
234
235 //=================================================================================
236 // function : ClickOnApply()
237 // purpose  :
238 //=================================================================================
239 bool PrimitiveGUI_ConeDlg::ClickOnApply()
240 {
241   if ( !onAccept() )
242     return false;
243
244   initName();
245   ConstructorsClicked( getConstructorId() );
246   return true;
247 }
248
249
250 //=================================================================================
251 // function : SelectionIntoArgument()
252 // purpose  : Called when selection as changed or other case
253 //=================================================================================
254 void PrimitiveGUI_ConeDlg::SelectionIntoArgument()
255 {
256   if ( getConstructorId() != 0 )
257     return;
258
259   if ( IObjectCount() != 1 ) {
260     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
261       myPoint = GEOM::GEOM_Object::_nil();
262     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
263       myDir = GEOM::GEOM_Object::_nil();
264     return;
265   }
266
267   /* nbSel == 1 */
268   Standard_Boolean testResult = Standard_False;
269   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
270   
271   if ( !testResult || CORBA::is_nil( aSelectedObject ) )
272     return;
273
274   TopoDS_Shape aShape;
275   QString aName = GEOMBase::GetName( aSelectedObject );
276
277   if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
278     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
279     if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
280       aNeedType = TopAbs_EDGE;
281
282     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
283     TColStd_IndexedMapOfInteger aMap;
284     aSelMgr->GetIndexes( firstIObject(), aMap );
285     if ( aMap.Extent() == 1 ) {
286       int anIndex = aMap(1);
287       if ( aNeedType == TopAbs_EDGE )
288         aName.append( ":edge_" + QString::number( anIndex ) );
289       else
290         aName.append( ":vertex_" + QString::number( anIndex ) );
291
292       //Find SubShape Object in Father
293       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
294
295       if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
296         GEOM::GEOM_IShapesOperations_var aShapesOp =
297           getGeomEngine()->GetIShapesOperations( getStudyId() );
298         aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
299       }
300       else {
301         aSelectedObject = aFindedObject; // get Object from study       
302       }
303     }
304     else {
305       if ( aShape.ShapeType() != aNeedType ) {
306         aSelectedObject = GEOM::GEOM_Object::_nil();
307         aName = "";
308       }
309     }
310   }
311
312   myEditCurrentArgument->setText( aName );
313
314   if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
315     myPoint = aSelectedObject;
316   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
317     myDir = aSelectedObject;
318   
319   displayPreview();
320 }
321
322
323 //=================================================================================
324 // function : SetEditCurrentArgument()
325 // purpose  :
326 //=================================================================================
327 void PrimitiveGUI_ConeDlg::SetEditCurrentArgument()
328 {
329   QPushButton* send = (QPushButton*)sender();
330   
331   if ( send == GroupPoints->PushButton1 ) {
332     myEditCurrentArgument = GroupPoints->LineEdit1;
333     globalSelection( GEOM_POINT ); // to break prvious local selection
334     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
335   }
336   else if ( send == GroupPoints->PushButton2 ) {
337     myEditCurrentArgument = GroupPoints->LineEdit2;
338     globalSelection( GEOM_LINE );// to break prvious local selection
339     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
340   }
341   
342   myEditCurrentArgument->setFocus();
343   SelectionIntoArgument();
344 }
345
346
347 //=================================================================================
348 // function : LineEditReturnPressed()
349 // purpose  :
350 //=================================================================================
351 void PrimitiveGUI_ConeDlg::LineEditReturnPressed()
352 {
353   QLineEdit* send = (QLineEdit*)sender();
354   if ( send == GroupPoints->LineEdit1 ||
355        send == GroupPoints->LineEdit2 ) {
356     myEditCurrentArgument = send;
357     GEOMBase_Skeleton::LineEditReturnPressed();
358   }
359 }
360
361
362 //=================================================================================
363 // function : ActivateThisDialog()
364 // purpose  :
365 //=================================================================================
366 void PrimitiveGUI_ConeDlg::ActivateThisDialog()
367 {
368   GEOMBase_Skeleton::ActivateThisDialog();
369   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
370            this, SLOT( SelectionIntoArgument() ) );
371   
372   ConstructorsClicked( getConstructorId() );
373 }
374
375
376 //=================================================================================
377 // function : DeactivateActiveDialog()
378 // purpose  : public slot to deactivate if active
379 //=================================================================================
380 void PrimitiveGUI_ConeDlg::DeactivateActiveDialog()
381 {
382   GEOMBase_Skeleton::DeactivateActiveDialog();
383 }
384
385
386 //=================================================================================
387 // function : enterEvent()
388 // purpose  :
389 //=================================================================================
390 void PrimitiveGUI_ConeDlg::enterEvent( QEvent* )
391 {
392   if ( !mainFrame()->GroupConstructors->isEnabled() )
393     ActivateThisDialog();
394 }
395
396
397 //=================================================================================
398 // function : ValueChangedInSpinBox()
399 // purpose  :
400 //=================================================================================
401 void PrimitiveGUI_ConeDlg::ValueChangedInSpinBox()
402 {
403   displayPreview();
404 }
405
406
407 //=================================================================================
408 // function : createOperation
409 // purpose  :
410 //=================================================================================
411 GEOM::GEOM_IOperations_ptr  PrimitiveGUI_ConeDlg::createOperation()
412 {
413   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
414 }
415
416 //=================================================================================
417 // function : isValid
418 // purpose  :
419 //=================================================================================
420 bool  PrimitiveGUI_ConeDlg::isValid( QString& msg )
421 {
422   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil()) : true;
423 }
424
425 //=================================================================================
426 // function : execute
427 // purpose  :
428 //=================================================================================
429 bool PrimitiveGUI_ConeDlg::execute( ObjectList& objects )
430 {
431   bool res = false;
432   
433   GEOM::GEOM_Object_var anObj;
434
435   switch ( getConstructorId() ) {
436   case 0 :
437     if ( !CORBA::is_nil( myPoint ) && !CORBA::is_nil( myDir ) ) {
438       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeConePntVecR1R2H( myPoint, 
439                                                                                             myDir, 
440                                                                                             getRadius1(),
441                                                                                             getRadius2(),
442                                                                                             getHeight() );
443       res = true;
444     }
445     break;
446   case 1 :
447     anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeConeR1R2H( getRadius1(),
448                                                                                     getRadius2(),
449                                                                                     getHeight() );
450     res = true;
451     break;
452   }
453
454   if ( !anObj->_is_nil() )
455     objects.push_back( anObj._retn() );
456
457   return res;
458 }
459
460
461 //=================================================================================
462 // function : getRadius1()
463 // purpose  :
464 //=================================================================================
465 double PrimitiveGUI_ConeDlg::getRadius1() const
466 {
467   int aConstructorId = getConstructorId();
468   if ( aConstructorId == 0 )
469     return GroupPoints->SpinBox_DX->value();
470   else if ( aConstructorId == 1 )
471     return GroupDimensions->SpinBox_DX->value();
472   return 0;
473 }
474
475
476 //=================================================================================
477 // function : getRadius2()
478 // purpose  :
479 //=================================================================================
480 double PrimitiveGUI_ConeDlg::getRadius2() const
481 {
482   int aConstructorId = getConstructorId();
483   if ( aConstructorId == 0 )
484     return GroupPoints->SpinBox_DY->value();
485   else if ( aConstructorId == 1 )
486     return GroupDimensions->SpinBox_DY->value();
487   return 0;
488 }
489
490
491 //=================================================================================
492 // function : getRadius2()
493 // purpose  :
494 //=================================================================================
495 double PrimitiveGUI_ConeDlg::getHeight() const
496 {
497   int aConstructorId = getConstructorId();
498   if ( aConstructorId == 0 )
499     return GroupPoints->SpinBox_DZ->value();
500   else if ( aConstructorId == 1 )
501     return GroupDimensions->SpinBox_DZ->value();
502   return 0;
503 }
504
505 //=================================================================================
506 // function : addSubshapeToStudy
507 // purpose  : virtual method to add new SubObjects if local selection
508 //=================================================================================
509 void PrimitiveGUI_ConeDlg::addSubshapesToStudy()
510 {
511   QMap<QString, GEOM::GEOM_Object_var> objMap;
512   
513   switch ( getConstructorId() ) {
514   case 0:
515     objMap[GroupPoints->LineEdit1->text()] = myPoint;
516     objMap[GroupPoints->LineEdit2->text()] = myDir;
517     break;
518   case 1:
519     return;
520   }
521   addSubshapesToFather( objMap );
522 }