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