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