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