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