Salome HOME
SMH: Merged GEOM (NEWGUI, HEAD, POLYWORK)
[modules/geom.git] / src / BasicGUI / BasicGUI_CircleDlg.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_CircleDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "BasicGUI_CircleDlg.h"
30
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "SalomeApp_SelectionMgr.h"
35
36 #include <qlabel.h>
37
38 #include "GEOMImpl_Types.hxx"
39
40 #include "utilities.h"
41
42 //=================================================================================
43 // class    : BasicGUI_CircleDlg()
44 // purpose  : Constructs a BasicGUI_CircleDlg 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_CircleDlg::BasicGUI_CircleDlg(GeometryGUI* theGeometryGUI, QWidget* parent, const char* name, bool modal, WFlags fl)
50   :GEOMBase_Skeleton(parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
51    myGeometryGUI(theGeometryGUI)
52 {
53   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_CIRCLE_PV")));
54   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_CIRCLE_PNTS")));
55   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
56
57   setCaption(tr("GEOM_CIRCLE_TITLE"));
58
59   /***************************************************************/
60   GroupConstructors->setTitle(tr("GEOM_CIRCLE"));
61   RadioButton1->setPixmap(image0);
62   RadioButton2->setPixmap(image2);
63   RadioButton3->close(TRUE);
64
65   GroupPntVecR = new DlgRef_2Sel1Spin(this, "GroupPntVecR");
66   GroupPntVecR->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
67   
68   GroupPntVecR->TextLabel1->setText(tr("GEOM_CENTER_POINT"));
69   GroupPntVecR->TextLabel2->setText(tr("GEOM_VECTOR"));
70   GroupPntVecR->TextLabel3->setText(tr("GEOM_RADIUS"));
71   GroupPntVecR->PushButton1->setPixmap(image1);
72   GroupPntVecR->PushButton2->setPixmap(image1);
73
74   GroupPntVecR->LineEdit1->setReadOnly( true );
75   GroupPntVecR->LineEdit2->setReadOnly( true );
76
77   Group3Pnts = new DlgRef_3Sel_QTD(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->PushButton1->setPixmap(image1);
83   Group3Pnts->PushButton2->setPixmap(image1);
84   Group3Pnts->PushButton3->setPixmap(image1);
85
86   Group3Pnts->LineEdit1->setReadOnly( true );
87   Group3Pnts->LineEdit2->setReadOnly( true );
88   Group3Pnts->LineEdit3->setReadOnly( true );
89
90   Layout1->addWidget( GroupPntVecR, 2, 0 );
91   Layout1->addWidget( Group3Pnts, 2, 0 );
92   /***************************************************************/
93
94   Init();
95 }
96
97
98 //=================================================================================
99 // function : ~BasicGUI_CircleDlg()
100 // purpose  : Destroys the object and frees any allocated resources
101 //=================================================================================
102 BasicGUI_CircleDlg::~BasicGUI_CircleDlg()
103 {
104 }
105
106
107 //=================================================================================
108 // function : Init()
109 // purpose  :
110 //=================================================================================
111 void BasicGUI_CircleDlg::Init()
112 {
113   /* init variables */
114   myEditCurrentArgument = GroupPntVecR->LineEdit1;
115
116   myPoint = myDir = myPoint1 = myPoint2 = myPoint3 = GEOM::GEOM_Object::_nil();
117
118   // myGeometryGUI->SetState( 0 );
119
120   /* Get setting of step value from file configuration */
121   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
122   double aStep = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
123
124   /* min, max, step and decimals for spin boxes & initial values */
125   GroupPntVecR->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, aStep, 3);
126   GroupPntVecR->SpinBox_DX->SetValue( 100 );
127
128   /* signals and slots connections */
129   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
130   connect(myGeometryGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
131   connect(myGeometryGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
132   
133   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
134   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
135   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
136
137   connect(GroupPntVecR->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
138   connect(GroupPntVecR->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
139   connect(Group3Pnts->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
140   connect(Group3Pnts->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
141   connect(Group3Pnts->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
142
143   connect(GroupPntVecR->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
144   connect(GroupPntVecR->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
145
146   connect(GroupPntVecR->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
147   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPntVecR->SpinBox_DX, SLOT(SetStep(double)));
148   
149   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
150           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
151
152   initName( tr( "GEOM_CIRCLE" ) );
153
154   Group3Pnts->hide();
155   ConstructorsClicked( 0 );
156 }
157
158 //=================================================================================
159 // function : ConstructorsClicked()
160 // purpose  : Radio button management
161 //=================================================================================
162 void BasicGUI_CircleDlg::ConstructorsClicked( int constructorId )
163 {
164   disconnect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0 );
165   myPoint = myDir = myPoint1 = myPoint2 = myPoint3 = GEOM::GEOM_Object::_nil();
166
167   switch ( constructorId )
168   {
169     case 0:
170       {
171         Group3Pnts->hide();
172         resize(0, 0);
173         GroupPntVecR->show();
174         
175         myEditCurrentArgument = GroupPntVecR->LineEdit1;
176         GroupPntVecR->LineEdit1->setText("");
177         GroupPntVecR->LineEdit2->setText("");
178         break;
179       }
180   case 1:
181     {
182       GroupPntVecR->hide();
183       resize( 0, 0 );
184       Group3Pnts->show();
185       
186       myEditCurrentArgument = Group3Pnts->LineEdit1;
187       Group3Pnts->LineEdit1->setText("");
188       Group3Pnts->LineEdit2->setText("");
189       Group3Pnts->LineEdit3->setText("");
190       break;
191       }
192   }
193   
194   myEditCurrentArgument->setFocus();
195   globalSelection( GEOM_POINT );
196   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
197           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
198 }
199
200 //=================================================================================
201 // function : ClickOnOk()
202 // purpose  :
203 //=================================================================================
204 void BasicGUI_CircleDlg::ClickOnOk()
205 {
206   if ( ClickOnApply() )
207     ClickOnCancel();
208 }
209
210 //=================================================================================
211 // function : ClickOnApply()
212 // purpose  :
213 //=================================================================================
214 bool BasicGUI_CircleDlg::ClickOnApply()
215 {
216   if ( !onAccept() )
217     return false;
218
219   initName();
220   ConstructorsClicked( getConstructorId() );
221   return true;
222 }
223
224 //=======================================================================
225 // function : ClickOnCancel()
226 // purpose  :
227 //=======================================================================
228 void BasicGUI_CircleDlg::ClickOnCancel()
229 {
230   GEOMBase_Skeleton::ClickOnCancel();
231 }
232
233 //=================================================================================
234 // function : SelectionIntoArgument()
235 // purpose  : Called when selection as changed or other case
236 //=================================================================================
237 void BasicGUI_CircleDlg::SelectionIntoArgument()
238 {
239   myEditCurrentArgument->setText("");
240   
241   if ( IObjectCount() != 1 )  
242   {
243     if      ( myEditCurrentArgument == GroupPntVecR->LineEdit1 ) myPoint  = GEOM::GEOM_Object::_nil();
244     else if ( myEditCurrentArgument == GroupPntVecR->LineEdit2 ) myDir    = GEOM::GEOM_Object::_nil();
245     else if ( myEditCurrentArgument == Group3Pnts->LineEdit1 )   myPoint1 = GEOM::GEOM_Object::_nil();
246     else if ( myEditCurrentArgument == Group3Pnts->LineEdit2 )   myPoint2 = GEOM::GEOM_Object::_nil();
247     else if ( myEditCurrentArgument == Group3Pnts->LineEdit3 )   myPoint3 = GEOM::GEOM_Object::_nil();
248     return;
249   }
250
251   // nbSel == 1
252   Standard_Boolean aRes = Standard_False;
253   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
254   if ( !CORBA::is_nil( aSelectedObject ) && aRes )
255   {  
256     myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
257     if      ( myEditCurrentArgument == GroupPntVecR->LineEdit1 ) myPoint  = aSelectedObject;
258     else if ( myEditCurrentArgument == GroupPntVecR->LineEdit2 ) myDir    = aSelectedObject;
259     else if ( myEditCurrentArgument == Group3Pnts->LineEdit1 )   myPoint1 = aSelectedObject;
260     else if ( myEditCurrentArgument == Group3Pnts->LineEdit2 )   myPoint2 = aSelectedObject;
261     else if ( myEditCurrentArgument == Group3Pnts->LineEdit3 )   myPoint3 = aSelectedObject;
262   }
263
264   displayPreview();
265 }
266
267
268 //=================================================================================
269 // function : SetEditCurrentArgument()
270 // purpose  :
271 //=================================================================================
272 void BasicGUI_CircleDlg::SetEditCurrentArgument()
273 {
274   QPushButton* send = (QPushButton*)sender();
275
276   if ( send == GroupPntVecR->PushButton1 )      myEditCurrentArgument = GroupPntVecR->LineEdit1;
277   else if ( send == GroupPntVecR->PushButton2 ) myEditCurrentArgument = GroupPntVecR->LineEdit2;
278   else if ( send == Group3Pnts->PushButton1 )   myEditCurrentArgument = Group3Pnts->LineEdit1;
279   else if ( send == Group3Pnts->PushButton2 )   myEditCurrentArgument = Group3Pnts->LineEdit2;
280   else if ( send == Group3Pnts->PushButton3 )   myEditCurrentArgument = Group3Pnts->LineEdit3;
281   
282   myEditCurrentArgument->setFocus();
283   
284   if ( myEditCurrentArgument == GroupPntVecR->LineEdit2 ) 
285     globalSelection( GEOM_LINE );
286   else
287     globalSelection( GEOM_POINT );
288   SelectionIntoArgument();
289 }
290
291 //=================================================================================
292 // function : LineEditReturnPressed()
293 // purpose  :
294 //=================================================================================
295 void BasicGUI_CircleDlg::LineEditReturnPressed()
296 {
297   QLineEdit* send = (QLineEdit*)sender();
298   if ( send == GroupPntVecR->LineEdit1 ||
299        send == GroupPntVecR->LineEdit2 ||
300        send == Group3Pnts->LineEdit1 ||
301        send == Group3Pnts->LineEdit2 ||
302        send == Group3Pnts->LineEdit3 )
303   {
304     myEditCurrentArgument = send;
305     GEOMBase_Skeleton::LineEditReturnPressed();
306   }
307 }
308
309
310 //=================================================================================
311 // function : ActivateThisDialog()
312 // purpose  :
313 //=================================================================================
314 void BasicGUI_CircleDlg::ActivateThisDialog()
315 {
316   GEOMBase_Skeleton::ActivateThisDialog();
317   globalSelection( GEOM_POINT );
318   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
319           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
320
321   // myGeometryGUI->SetState( 0 );
322
323   ConstructorsClicked( getConstructorId() );
324 }
325
326
327 //=================================================================================
328 // function : enterEvent()
329 // purpose  :
330 //=================================================================================
331 void BasicGUI_CircleDlg::enterEvent(QEvent* e)
332 {
333   if ( !GroupConstructors->isEnabled() )
334     ActivateThisDialog();
335 }
336
337 //=================================================================================
338 // function : DeactivateActiveDialog()
339 // purpose  : public slot to deactivate if active
340 //=================================================================================
341 void BasicGUI_CircleDlg::DeactivateActiveDialog()
342 {
343   // myGeometryGUI->SetState( -1 );
344   GEOMBase_Skeleton::DeactivateActiveDialog();
345 }
346
347 //=================================================================================
348 // function : ValueChangedInSpinBox()
349 // purpose  :
350 //=================================================================================
351 void BasicGUI_CircleDlg::ValueChangedInSpinBox()
352 {
353   displayPreview();
354 }
355
356 //=================================================================================
357 // function : getRadius()
358 // purpose  :
359 //=================================================================================
360 double BasicGUI_CircleDlg::getRadius() const
361 {
362   return GroupPntVecR->SpinBox_DX->GetValue();
363 }
364
365 //=================================================================================
366 // function : createOperation
367 // purpose  :
368 //=================================================================================
369 GEOM::GEOM_IOperations_ptr BasicGUI_CircleDlg::createOperation()
370 {
371   return myGeometryGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
372 }
373
374 //=================================================================================
375 // function : isEqual
376 // purpose  : it may also be needed to check for min distance between gp_Pnt-s...
377 //=================================================================================
378 static bool isEqual( const GEOM::GEOM_Object_var& thePnt1, const GEOM::GEOM_Object_var& thePnt2 )
379 {
380   return thePnt1->_is_equivalent( thePnt2 );
381 }
382
383 //=================================================================================
384 // function : isValid
385 // purpose  :
386 //=================================================================================
387 bool BasicGUI_CircleDlg::isValid( QString& msg )
388 {
389   const int id = getConstructorId();
390   if ( id == 0 )
391     return !myPoint->_is_nil() && !myDir->_is_nil() && getRadius() > 0;
392   else if ( id == 1 )
393     return !myPoint1->_is_nil() && !myPoint2->_is_nil() && !myPoint3->_is_nil() &&
394       !isEqual( myPoint1, myPoint2 ) && !isEqual( myPoint1, myPoint3 ) && !isEqual( myPoint2, myPoint3 );
395   return false;
396 }
397
398 //=================================================================================
399 // function : execute
400 // purpose  :
401 //=================================================================================
402 bool BasicGUI_CircleDlg::execute( ObjectList& objects )
403 {
404   bool res = false;
405   
406   GEOM::GEOM_Object_var anObj;
407   
408   switch ( getConstructorId() )
409   {
410   case 0 :
411     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeCirclePntVecR( myPoint, myDir, getRadius() );
412     res = true;
413     break;
414   case 1 :
415     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeCircleThreePnt( myPoint1, myPoint2, myPoint3 );
416     res = true;
417     break;
418   }
419   
420   if ( !anObj->_is_nil() )
421     objects.push_back( anObj._retn() );
422   
423   return res;
424 }
425
426 //=================================================================================
427 // function : closeEvent
428 // purpose  :
429 //=================================================================================
430 void BasicGUI_CircleDlg::closeEvent( QCloseEvent* e )
431 {
432   // myGeometryGUI->SetState( -1 );
433   GEOMBase_Skeleton::closeEvent( e );
434 }
435