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