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