]> SALOME platform Git repositories - modules/geom.git/blob - src/PrimitiveGUI/PrimitiveGUI_DiskDlg.cxx
Salome HOME
0016757: EDF 546 GEOM : Add Face and Disc in geom primitives
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_DiskDlg.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_DiskDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "PrimitiveGUI_DiskDlg.h"
27
28 #include <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 <TColStd_IndexedMapOfInteger.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS.hxx>
43 #include <TopExp.hxx>
44
45 //=================================================================================
46 // class    : PrimitiveGUI_DiskDlg()
47 // purpose  : Constructs a PrimitiveGUI_DiskDlg which is a child of 'parent', with the 
48 //            name 'name' and widget flags set to 'f'.
49 //            The dialog will by default be modeless, unless you set 'modal' to
50 //            TRUE to construct a modal dialog.
51 //=================================================================================
52 PrimitiveGUI_DiskDlg::PrimitiveGUI_DiskDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
53                                         bool modal, Qt::WindowFlags fl )
54   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
55 {
56   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_DISK_PNT_VEC_R" ) ) );
57   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
58   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_DISK_THREE_POINTS" ) ) );
59
60   setWindowTitle( tr( "GEOM_DISK_TITLE" ) );
61
62   /***************************************************************/
63   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_DISK" ) );
64   mainFrame()->RadioButton1->setIcon( image0 );
65   mainFrame()->RadioButton2->setIcon( image2 );
66   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
67   mainFrame()->RadioButton3->close();
68
69   GroupPntVecR = new DlgRef_2Sel1Spin( centralWidget() );
70
71   GroupPntVecR->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
72   GroupPntVecR->TextLabel1->setText( tr( "GEOM_CENTER_POINT" ) );
73   GroupPntVecR->TextLabel2->setText( tr( "GEOM_VECTOR" ) );
74   GroupPntVecR->TextLabel3->setText( tr( "GEOM_RADIUS" ) );
75   GroupPntVecR->PushButton1->setIcon( image1 );
76   GroupPntVecR->PushButton2->setIcon( image1 );
77
78   GroupPntVecR->LineEdit1->setReadOnly( true );
79   GroupPntVecR->LineEdit2->setReadOnly( true );
80
81   Group3Pnts = new DlgRef_3Sel( centralWidget() );
82
83   Group3Pnts->GroupBox1->setTitle( tr( "GEOM_3_POINTS" ) );
84   Group3Pnts->TextLabel1->setText( tr( "GEOM_POINT1" ) );
85   Group3Pnts->TextLabel2->setText( tr( "GEOM_POINT2" ) );
86   Group3Pnts->TextLabel3->setText( tr( "GEOM_POINT3" ) );
87   Group3Pnts->PushButton1->setIcon( image1 );
88   Group3Pnts->PushButton2->setIcon( image1 );
89   Group3Pnts->PushButton3->setIcon( image1 );
90
91   Group3Pnts->LineEdit1->setReadOnly( true );
92   Group3Pnts->LineEdit2->setReadOnly( true );
93   Group3Pnts->LineEdit3->setReadOnly( true );
94
95   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
96   layout->setMargin( 0 ); layout->setSpacing( 6 );
97   layout->addWidget( GroupPntVecR );
98   layout->addWidget( Group3Pnts );
99   /***************************************************************/
100
101   setHelpFileName( "create_disk_page.html" );
102
103   Init();
104 }
105
106
107 //=================================================================================
108 // function : ~PrimitiveGUI_DiskDlg()
109 // purpose  : Destroys the object and frees any allocated resources
110 //=================================================================================
111 PrimitiveGUI_DiskDlg::~PrimitiveGUI_DiskDlg()
112 {
113 }
114
115
116 //=================================================================================
117 // function : Init()
118 // purpose  :
119 //=================================================================================
120 void PrimitiveGUI_DiskDlg::Init()
121 {
122   /* init variables */
123   myEditCurrentArgument = GroupPntVecR->LineEdit1;
124
125   myPoint = myDir = myPoint1 = myPoint2 = myPoint3 = GEOM::GEOM_Object::_nil();
126
127   /* Get setting of step value from file configuration */
128   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
129   double aStep = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
130
131   /* min, max, step and decimals for spin boxes & initial values */
132   initSpinBox( GroupPntVecR->SpinBox_DX, 0.001, COORD_MAX, aStep, 3 ); // VSR:TODO : DBL_DIGITS_DISPLAY
133   GroupPntVecR->SpinBox_DX->setValue( 100 );
134
135   /* signals and slots connections */
136   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
137   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
138   
139   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
140   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
141
142   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
143
144   connect( GroupPntVecR->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
145   connect( GroupPntVecR->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
146   connect( GroupPntVecR->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
147   connect( GroupPntVecR->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
148
149   connect( Group3Pnts->PushButton1,   SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
150   connect( Group3Pnts->PushButton2,   SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
151   connect( Group3Pnts->PushButton3,   SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
152   connect( Group3Pnts->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
153   connect( Group3Pnts->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
154   connect( Group3Pnts->LineEdit3, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
155
156   connect( GroupPntVecR->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT(ValueChangedInSpinBox() ) );
157   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
158
159   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
160            this, SLOT( SelectionIntoArgument() ) );
161
162   initName( tr( "GEOM_DISK" ) );
163
164   ConstructorsClicked( 0 );
165 }
166
167 //=================================================================================
168 // function : SetDoubleSpinBoxStep()
169 // purpose  : Double spin box management
170 //=================================================================================
171 void PrimitiveGUI_DiskDlg::SetDoubleSpinBoxStep( double step )
172 {
173   GroupPntVecR->SpinBox_DX->setSingleStep(step);
174 }
175
176 //=================================================================================
177 // function : ConstructorsClicked()
178 // purpose  : Radio button management
179 //=================================================================================
180 void PrimitiveGUI_DiskDlg::ConstructorsClicked( int constructorId )
181 {
182   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
183
184   myPoint = myDir = myPoint1 = myPoint2 = myPoint3 = GEOM::GEOM_Object::_nil();
185
186   switch ( constructorId ) {
187   case 0:
188     {
189       Group3Pnts->hide();
190       GroupPntVecR->show();
191       
192       myEditCurrentArgument = GroupPntVecR->LineEdit1;
193       GroupPntVecR->LineEdit1->setText( "" );
194       GroupPntVecR->LineEdit2->setText( "" );
195       break;
196     }
197   case 1:
198     {
199       GroupPntVecR->hide();
200       Group3Pnts->show();
201       
202       myEditCurrentArgument = Group3Pnts->LineEdit1;
203       Group3Pnts->LineEdit1->setText( "" );
204       Group3Pnts->LineEdit2->setText( "" );
205       Group3Pnts->LineEdit3->setText( "" );
206       break;
207     }
208   }
209   
210   qApp->processEvents();
211   updateGeometry();
212   resize( minimumSize() );
213
214   myEditCurrentArgument->setFocus();
215   globalSelection(); // close local contexts, if any
216   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
217
218   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged() ),
219            this, SLOT( SelectionIntoArgument() ) );
220 }
221
222 //=================================================================================
223 // function : ClickOnOk()
224 // purpose  :
225 //=================================================================================
226 void PrimitiveGUI_DiskDlg::ClickOnOk()
227 {
228   if ( ClickOnApply() )
229     ClickOnCancel();
230 }
231
232 //=================================================================================
233 // function : ClickOnApply()
234 // purpose  :
235 //=================================================================================
236 bool PrimitiveGUI_DiskDlg::ClickOnApply()
237 {
238   if ( !onAccept() )
239     return false;
240
241   initName();
242   ConstructorsClicked( getConstructorId() );
243   return true;
244 }
245
246 //=================================================================================
247 // function : SelectionIntoArgument()
248 // purpose  : Called when selection as changed or other case
249 //=================================================================================
250 void PrimitiveGUI_DiskDlg::SelectionIntoArgument()
251 {
252   myEditCurrentArgument->setText( "" );
253   
254   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
255   SALOME_ListIO aList;
256   aSelMgr->selectedObjects( aList );
257
258   if ( aList.Extent() != 1 ) {
259     if      ( myEditCurrentArgument == GroupPntVecR->LineEdit1 )     myPoint  = GEOM::GEOM_Object::_nil();
260     else if ( myEditCurrentArgument == GroupPntVecR->LineEdit2 )     myDir    = GEOM::GEOM_Object::_nil();
261     else if ( myEditCurrentArgument == Group3Pnts->LineEdit1 )       myPoint1 = GEOM::GEOM_Object::_nil();
262     else if ( myEditCurrentArgument == Group3Pnts->LineEdit2 )       myPoint2 = GEOM::GEOM_Object::_nil();
263     else if ( myEditCurrentArgument == Group3Pnts->LineEdit3 )       myPoint3 = GEOM::GEOM_Object::_nil();
264     return;
265   }
266
267   // nbSel == 1
268   Handle(SALOME_InteractiveObject) anIO = aList.First();
269
270   Standard_Boolean aRes = Standard_False;
271   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(anIO, aRes);
272   if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
273     QString aName = GEOMBase::GetName( aSelectedObject );
274
275     // If selected Vertex or Edge on the some Shape Get selection Subshape
276     TopoDS_Shape aShape;
277     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
278       TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
279       if ( myEditCurrentArgument == GroupPntVecR->LineEdit2 )
280         aNeedType = TopAbs_EDGE;
281
282       TColStd_IndexedMapOfInteger aMap;
283       aSelMgr->GetIndexes(anIO, aMap);
284       if ( aMap.Extent() == 1 ) { // Local Selection
285         int anIndex = aMap(1);
286         if ( aNeedType == TopAbs_EDGE )
287           aName += QString( ":edge_%1" ).arg( anIndex );
288         else
289           aName += QString( ":vertex_%1" ).arg( anIndex );
290
291         //Find SubShape Object in Father
292         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
293
294         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
295           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
296           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
297         }
298         else {
299           aSelectedObject = aFindedObject; // get Object from study
300         }
301       }
302       else { // Global Selection
303         if ( aShape.ShapeType() != aNeedType ) {
304           aSelectedObject = GEOM::GEOM_Object::_nil();
305           aName = "";
306         }
307       }
308     }
309     
310     myEditCurrentArgument->setText( aName );
311
312     if      ( myEditCurrentArgument == GroupPntVecR->LineEdit1 )     myPoint  = aSelectedObject;
313     else if ( myEditCurrentArgument == GroupPntVecR->LineEdit2 )     myDir    = aSelectedObject;
314     else if ( myEditCurrentArgument == Group3Pnts->LineEdit1 )       myPoint1 = aSelectedObject;
315     else if ( myEditCurrentArgument == Group3Pnts->LineEdit2 )       myPoint2 = aSelectedObject;
316     else if ( myEditCurrentArgument == Group3Pnts->LineEdit3 )       myPoint3 = aSelectedObject;
317   }
318
319   displayPreview();
320 }
321
322
323 //=================================================================================
324 // function : SetEditCurrentArgument()
325 // purpose  :
326 //=================================================================================
327 void PrimitiveGUI_DiskDlg::SetEditCurrentArgument()
328 {
329   QPushButton* send = (QPushButton*)sender();
330
331   if ( send == GroupPntVecR->PushButton1 )          myEditCurrentArgument = GroupPntVecR->LineEdit1;
332   else if ( send == GroupPntVecR->PushButton2 )     myEditCurrentArgument = GroupPntVecR->LineEdit2;
333   else if ( send == Group3Pnts->PushButton1 )       myEditCurrentArgument = Group3Pnts->LineEdit1;
334   else if ( send == Group3Pnts->PushButton2 )       myEditCurrentArgument = Group3Pnts->LineEdit2;
335   else if ( send == Group3Pnts->PushButton3 )       myEditCurrentArgument = Group3Pnts->LineEdit3;
336   
337   myEditCurrentArgument->setFocus();
338
339   if ( myEditCurrentArgument == GroupPntVecR->LineEdit2 ) {
340     globalSelection(); // close local contexts, if any
341     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
342   }
343   else {
344     globalSelection(); // close local contexts, if any
345     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
346   }
347
348   SelectionIntoArgument();
349 }
350
351 //=================================================================================
352 // function : LineEditReturnPressed()
353 // purpose  :
354 //=================================================================================
355 void PrimitiveGUI_DiskDlg::LineEditReturnPressed()
356 {
357   QLineEdit* send = (QLineEdit*)sender();
358   if ( send == GroupPntVecR->LineEdit1 ||
359        send == GroupPntVecR->LineEdit2 ||
360        send == Group3Pnts->LineEdit1 ||
361        send == Group3Pnts->LineEdit2 ||
362        send == Group3Pnts->LineEdit3 ) {
363     myEditCurrentArgument = send;
364     GEOMBase_Skeleton::LineEditReturnPressed();
365   }
366 }
367
368
369 //=================================================================================
370 // function : ActivateThisDialog()
371 // purpose  :
372 //=================================================================================
373 void PrimitiveGUI_DiskDlg::ActivateThisDialog()
374 {
375   GEOMBase_Skeleton::ActivateThisDialog();
376
377   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
378            this, SLOT( SelectionIntoArgument() ) );
379
380   ConstructorsClicked( getConstructorId() );
381 }
382
383
384 //=================================================================================
385 // function : enterEvent()
386 // purpose  :
387 //=================================================================================
388 void PrimitiveGUI_DiskDlg::enterEvent( QEvent* )
389 {
390   if ( !mainFrame()->GroupConstructors->isEnabled() )
391     ActivateThisDialog();
392 }
393
394 //=================================================================================
395 // function : DeactivateActiveDialog()
396 // purpose  : public slot to deactivate if active
397 //=================================================================================
398 void PrimitiveGUI_DiskDlg::DeactivateActiveDialog()
399 {
400   // myGeomGUI->SetState( -1 );
401   GEOMBase_Skeleton::DeactivateActiveDialog();
402 }
403
404 //=================================================================================
405 // function : ValueChangedInSpinBox()
406 // purpose  :
407 //=================================================================================
408 void PrimitiveGUI_DiskDlg::ValueChangedInSpinBox()
409 {
410   displayPreview();
411 }
412
413 //=================================================================================
414 // function : getRadius()
415 // purpose  :
416 //=================================================================================
417 double PrimitiveGUI_DiskDlg::getRadius() const
418 {
419   double r = 0.;
420   switch ( getConstructorId() ) {
421   case 0:
422     r = GroupPntVecR->SpinBox_DX->value(); break;
423   }
424   return r;
425 }
426
427 //=================================================================================
428 // function : createOperation
429 // purpose  :
430 //=================================================================================
431 GEOM::GEOM_IOperations_ptr PrimitiveGUI_DiskDlg::createOperation()
432 {
433   return myGeomGUI->GetGeomGen()->GetI3DPrimOperations( getStudyId() );
434 }
435
436 //=================================================================================
437 // function : isEqual
438 // purpose  : it may also be needed to check for min distance between gp_Pnt-s...
439 //=================================================================================
440 static bool isEqual( const GEOM::GEOM_Object_var& thePnt1, const GEOM::GEOM_Object_var& thePnt2 )
441 {
442   return thePnt1->_is_equivalent( thePnt2 );
443 }
444
445 //=================================================================================
446 // function : isValid
447 // purpose  :
448 //=================================================================================
449 bool PrimitiveGUI_DiskDlg::isValid( QString& msg )
450 {
451   const int id = getConstructorId();
452   if ( id == 0 )
453     return !myPoint->_is_nil() && !myDir->_is_nil() && getRadius() > 0;
454   else if ( id == 1 )
455     return !myPoint1->_is_nil() && !myPoint2->_is_nil() && !myPoint3->_is_nil() &&
456       !isEqual( myPoint1, myPoint2 ) && !isEqual( myPoint1, myPoint3 ) && !isEqual( myPoint2, myPoint3 );
457   return false;
458 }
459
460 //=================================================================================
461 // function : execute
462 // purpose  :
463 //=================================================================================
464 bool PrimitiveGUI_DiskDlg::execute( ObjectList& objects )
465 {
466   bool res = false;
467   
468   GEOM::GEOM_Object_var anObj;
469   
470   switch ( getConstructorId() ) {
471   case 0 :
472     anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeDiskPntVecR( myPoint, myDir, getRadius() );
473     res = true;
474     break;
475   case 1 :
476     anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeDiskThreePnt( myPoint1, myPoint2, myPoint3 );
477     res = true;
478     break;
479   }
480   
481   if ( !anObj->_is_nil() )
482     objects.push_back( anObj._retn() );
483   else {
484     MESSAGE( "Execute Object is NULL!" );
485   }
486   
487   return res;
488 }
489
490 //=================================================================================
491 // function : addSubshapeToStudy
492 // purpose  : virtual method to add new SubObjects if local selection
493 //=================================================================================
494 void PrimitiveGUI_DiskDlg::addSubshapesToStudy()
495 {
496   QMap<QString, GEOM::GEOM_Object_var> objMap;
497
498   switch ( getConstructorId() ) {
499   case 0:
500     objMap[GroupPntVecR->LineEdit1->text()] = myPoint;
501     objMap[GroupPntVecR->LineEdit2->text()] = myDir;
502     break;
503   case 1:
504     objMap[Group3Pnts->LineEdit1->text()] = myPoint1;
505     objMap[Group3Pnts->LineEdit2->text()] = myPoint2;
506     objMap[Group3Pnts->LineEdit3->text()] = myPoint3;
507     break;
508   }
509   addSubshapesToFather( objMap );
510 }