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