Salome HOME
b0e378dca3d08d0ec8633067b16463bbd87f4481
[modules/geom.git] / src / BasicGUI / BasicGUI_EllipseDlg.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : BasicGUI_EllipseDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 //
27 #include "BasicGUI_EllipseDlg.h"
28
29 #include <DlgRef.h>
30
31 #include <GeometryGUI.h>
32 #include <GEOMBase.h>
33
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_Session.h>
36 #include <SalomeApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38
39 #include <TColStd_IndexedMapOfInteger.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS.hxx>
44 #include <TopExp.hxx>
45
46 #include <GEOMImpl_Types.hxx>
47
48 //=================================================================================
49 // class    : BasicGUI_EllipseDlg()
50 // purpose  : Constructs a BasicGUI_EllipseDlg which is a child of 'parent', with the 
51 //            name 'name' and widget flags set to 'f'.
52 //            The dialog will by default be modeless, unless you set 'modal' to
53 //            TRUE to construct a modal dialog.
54 //=================================================================================
55 BasicGUI_EllipseDlg::BasicGUI_EllipseDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
56                                           bool modal, Qt::WindowFlags fl )
57   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
58 {
59   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_ELLIPSE_PV" ) ) );
60   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
61
62   setWindowTitle( tr( "GEOM_ELLIPSE_TITLE" ) );
63
64   /***************************************************************/
65   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_ELLIPSE" ) );
66   mainFrame()->RadioButton1->setIcon( image0 );
67   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
68   mainFrame()->RadioButton2->close();
69   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
70   mainFrame()->RadioButton3->close();
71
72   GroupPoints = new DlgRef_3Sel2Spin( centralWidget() );
73   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
74   GroupPoints->TextLabel1->setText( tr( "%1 (%2)" ).arg( tr( "GEOM_CENTER" ), tr( "ORIGIN_DEFAULT" ) ) );
75   GroupPoints->TextLabel2->setText( tr( "%1 (%2)" ).arg( tr( "GEOM_VECTOR" ), tr( "Z_AXIS_DEFAULT" ) ) );
76   GroupPoints->TextLabel3->setText( tr( "%1 (%2)" ).arg( tr( "GEOM_VECTOR_MAJOR" ), tr( "X_AXIS_DEFAULT" ) ) );
77   GroupPoints->TextLabel4->setText( tr( "GEOM_RADIUS_MAJOR" ) );
78   GroupPoints->TextLabel5->setText( tr( "GEOM_RADIUS_MINOR" ) );
79   GroupPoints->PushButton1->setIcon( image1 );
80   GroupPoints->PushButton2->setIcon( image1 );
81   GroupPoints->PushButton3->setIcon( image1 );
82
83   GroupPoints->LineEdit1->setReadOnly( true );
84   GroupPoints->LineEdit2->setReadOnly( true );
85   GroupPoints->LineEdit3->setReadOnly( true );
86   GroupPoints->LineEdit1->setEnabled( true );
87   GroupPoints->LineEdit2->setEnabled( false );
88   GroupPoints->LineEdit3->setEnabled( false );
89
90   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
91   layout->setMargin( 0 ); layout->setSpacing( 6 );
92   layout->addWidget( GroupPoints );
93   /***************************************************************/
94
95   setHelpFileName( "create_ellipse_page.html" );
96
97   Init();
98 }
99
100
101 //=================================================================================
102 // function : ~BasicGUI_EllipseDlg()
103 // purpose  : Destroys the object and frees any allocated resources
104 //=================================================================================
105 BasicGUI_EllipseDlg::~BasicGUI_EllipseDlg()
106 {
107 }
108
109
110 //=================================================================================
111 // function : Init()
112 // purpose  :
113 //=================================================================================
114 void BasicGUI_EllipseDlg::Init()
115 {
116   /* init variables */
117   myEditCurrentArgument = GroupPoints->LineEdit1;
118   GroupPoints->PushButton1->setDown(true);
119
120   myPoint.nullify();
121   myDir.nullify();
122   myMajor.nullify();
123
124   /* Get setting of step value from file configuration */
125   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
126   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
127   
128   double aMajorR( 200. ), aMinorR( 100. );
129
130   /* min, max, step and decimals for spin boxes & initial values */
131   initSpinBox( GroupPoints->SpinBox_DX, 0.000001, COORD_MAX, step, "length_precision" );
132   initSpinBox( GroupPoints->SpinBox_DY, 0.000001, COORD_MAX, step, "length_precision" );
133   GroupPoints->SpinBox_DX->setValue( aMajorR );
134   GroupPoints->SpinBox_DY->setValue( aMinorR );
135
136   /* signals and slots connections */
137   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
138   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
139
140   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
141   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
142
143   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
144   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
145   connect( GroupPoints->PushButton3, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
146
147   connect( GroupPoints->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
148   connect( GroupPoints->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
149
150   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
151   
152   connect( myGeomGUI->getApp()->selectionMgr(), 
153           SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
154
155   initName( tr( "GEOM_ELLIPSE" ) );
156
157   globalSelection(); // close local contexts, if any
158   localSelection( TopAbs_VERTEX );
159
160   resize( minimumSizeHint() );
161   SelectionIntoArgument();
162   displayPreview(true);
163 }
164
165 //=================================================================================
166 // function : SetDoubleSpinBoxStep()
167 // purpose  : Double spin box management
168 //=================================================================================
169 void BasicGUI_EllipseDlg::SetDoubleSpinBoxStep( double step )
170 {
171   GroupPoints->SpinBox_DX->setSingleStep(step);
172   GroupPoints->SpinBox_DY->setSingleStep(step);
173 }
174
175
176 //=================================================================================
177 // function : ClickOnOk()
178 // purpose  :
179 //=================================================================================
180 void BasicGUI_EllipseDlg::ClickOnOk()
181 {
182   setIsApplyAndClose( true );
183   if ( ClickOnApply() )
184     ClickOnCancel();
185 }
186
187
188 //=================================================================================
189 // function : ClickOnApply()
190 // purpose  :
191 //=================================================================================
192 bool BasicGUI_EllipseDlg::ClickOnApply()
193 {
194   if ( !onAccept() )
195     return false;
196
197   initName();
198
199   // reset
200   myPoint.nullify();
201   myDir.nullify();
202   myMajor.nullify();
203   GroupPoints->LineEdit1->setText( "" );
204   GroupPoints->LineEdit2->setText( "" );
205   GroupPoints->LineEdit3->setText( "" );
206   GroupPoints->PushButton1->setDown(true);
207   GroupPoints->PushButton2->setDown(false);
208   GroupPoints->PushButton3->setDown(false);
209   GroupPoints->LineEdit1->setEnabled( true );
210   GroupPoints->LineEdit2->setEnabled( false );
211   GroupPoints->LineEdit3->setEnabled( false );
212   myEditCurrentArgument = GroupPoints->LineEdit1;
213
214   globalSelection(); // close local contexts, if any
215   localSelection( TopAbs_VERTEX );
216   displayPreview(true);
217   
218   return true;
219 }
220
221 //=================================================================================
222 // function : SelectionIntoArgument()
223 // purpose  : Called when selection as changed or other case
224 //=================================================================================
225 void BasicGUI_EllipseDlg::SelectionIntoArgument()
226 {
227   myEditCurrentArgument->setText( "" );
228
229   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
230   SALOME_ListIO aSelList;
231   aSelMgr->selectedObjects(aSelList);
232
233   if (aSelList.Extent() != 1) {
234     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint.nullify();
235     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myDir.nullify();
236     else if (myEditCurrentArgument == GroupPoints->LineEdit3) myMajor.nullify();
237     return;
238   }
239
240   TopAbs_ShapeEnum aNeedType = ( myEditCurrentArgument == GroupPoints->LineEdit2 || 
241                                  myEditCurrentArgument == GroupPoints->LineEdit3 ) ?
242     TopAbs_EDGE : TopAbs_VERTEX;
243   GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
244   TopoDS_Shape aShape;
245   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
246     QString aName = GEOMBase::GetName( aSelectedObject.get() );
247     myEditCurrentArgument->setText( aName );
248     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
249       myPoint = aSelectedObject;
250       if ( myPoint && !myDir )
251         GroupPoints->PushButton2->click();
252     }
253     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
254       myDir = aSelectedObject;
255       if ( myDir && !myMajor )
256         GroupPoints->PushButton3->click();
257     }
258     else if ( myEditCurrentArgument == GroupPoints->LineEdit3 ) {
259       myMajor = aSelectedObject;
260       if ( myMajor && !myPoint )
261         GroupPoints->PushButton1->click();
262     }
263   }
264
265   displayPreview(true);
266 }
267
268
269 //=================================================================================
270 // function : SetEditCurrentArgument()
271 // purpose  :
272 //=================================================================================
273 void BasicGUI_EllipseDlg::SetEditCurrentArgument()
274 {
275   QPushButton* send = (QPushButton*)sender();
276
277   if ( send == GroupPoints->PushButton1 ) {
278     myEditCurrentArgument = GroupPoints->LineEdit1;
279     GroupPoints->PushButton2->setDown(false);
280     GroupPoints->PushButton3->setDown(false);
281     GroupPoints->LineEdit1->setEnabled( true );
282     GroupPoints->LineEdit2->setEnabled( false );
283     GroupPoints->LineEdit3->setEnabled( false );
284   }
285   else if ( send == GroupPoints->PushButton2 ) {
286     myEditCurrentArgument = GroupPoints->LineEdit2;
287     GroupPoints->PushButton1->setDown(false);
288     GroupPoints->PushButton3->setDown(false);
289     GroupPoints->LineEdit1->setEnabled( false );
290     GroupPoints->LineEdit2->setEnabled( true );
291     GroupPoints->LineEdit3->setEnabled( false );
292   }
293   else if ( send == GroupPoints->PushButton3 ) {
294     myEditCurrentArgument = GroupPoints->LineEdit3;
295     GroupPoints->PushButton1->setDown(false);
296     GroupPoints->PushButton2->setDown(false);
297     GroupPoints->LineEdit1->setEnabled( false );
298     GroupPoints->LineEdit2->setEnabled( false );
299     GroupPoints->LineEdit3->setEnabled( true );
300   }
301
302   TopAbs_ShapeEnum aNeedType = ( myEditCurrentArgument == GroupPoints->LineEdit2 || 
303                                  myEditCurrentArgument == GroupPoints->LineEdit3 ) ? 
304     TopAbs_EDGE : TopAbs_VERTEX;
305   globalSelection(); // close local contexts, if any
306   localSelection( aNeedType );
307   
308   myEditCurrentArgument->setFocus();
309   //SelectionIntoArgument();
310   send->setDown(true);
311   displayPreview(true);
312 }
313
314
315 //=================================================================================
316 // function : ActivateThisDialog()
317 // purpose  :
318 //=================================================================================
319 void BasicGUI_EllipseDlg::ActivateThisDialog()
320 {
321   GEOMBase_Skeleton::ActivateThisDialog();
322   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
323            this, SLOT( SelectionIntoArgument() ) );
324   
325   GroupPoints->LineEdit1->setFocus();
326   myEditCurrentArgument = GroupPoints->LineEdit1;
327
328   GroupPoints->LineEdit1->setText( "" );
329   GroupPoints->LineEdit2->setText( "" );
330   GroupPoints->LineEdit3->setText( "" );
331
332   myPoint.nullify();
333   myDir.nullify();
334   //globalSelection( GEOM_POINT );
335   globalSelection(); // close local contexts, if any
336   localSelection( TopAbs_VERTEX );
337 }
338
339 //=================================================================================
340 // function : DeactivateActiveDialog()
341 // purpose  : public slot to deactivate if active
342 //=================================================================================
343 void BasicGUI_EllipseDlg::DeactivateActiveDialog()
344 {
345   // myGeomGUI->SetState( -1 );
346   GEOMBase_Skeleton::DeactivateActiveDialog();
347 }
348
349 //=================================================================================
350 // function : enterEvent()
351 // purpose  :
352 //=================================================================================
353 void BasicGUI_EllipseDlg::enterEvent( QEvent* )
354 {
355   if ( !mainFrame()->GroupConstructors->isEnabled() )
356     ActivateThisDialog();
357 }
358
359 //=================================================================================
360 // function : ValueChangedInSpinBox()
361 // purpose  :
362 //=================================================================================
363 void BasicGUI_EllipseDlg::ValueChangedInSpinBox( double /*newValue*/ )
364 {
365   displayPreview(true);
366 }
367
368 //=================================================================================
369 // function : createOperation
370 // purpose  :
371 //=================================================================================
372 GEOM::GEOM_IOperations_ptr BasicGUI_EllipseDlg::createOperation()
373 {
374   return myGeomGUI->GetGeomGen()->GetICurvesOperations();
375 }
376
377 //=================================================================================
378 // function : isValid
379 // purpose  :
380 //=================================================================================
381 bool BasicGUI_EllipseDlg::isValid( QString& msg )
382 {
383   // nil point means origin of global CS
384   // nil vector means Z axis
385   bool ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) &&
386             GroupPoints->SpinBox_DY->isValid( msg, !IsPreview() );
387   if ( ok ) {
388     double aMajorR = GroupPoints->SpinBox_DX->value();
389     double aMinorR = GroupPoints->SpinBox_DY->value();
390     if ( aMajorR < aMinorR ) {
391       msg = tr( "GEOM_ELLIPSE_ERROR_1" );
392       ok = false;
393     }
394   }
395   return ok;
396 }
397
398 //=================================================================================
399 // function : execute
400 // purpose  :
401 //=================================================================================
402 bool BasicGUI_EllipseDlg::execute( ObjectList& objects )
403 {
404   double aMajorR = GroupPoints->SpinBox_DX->value();
405   double aMinorR = GroupPoints->SpinBox_DY->value();
406
407   QStringList aParameters;
408   aParameters<<GroupPoints->SpinBox_DX->text();
409   aParameters<<GroupPoints->SpinBox_DY->text();
410   
411   GEOM::GEOM_ICurvesOperations_var anOper = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() );
412   GEOM::GEOM_Object_var anObj = myMajor ? 
413     anOper->MakeEllipseVec( myPoint.get(), myDir.get(), aMajorR, aMinorR, myMajor.get() ) :
414     anOper->MakeEllipse   ( myPoint.get(), myDir.get(), aMajorR, aMinorR );
415   if ( !anObj->_is_nil() ) {
416     if ( !IsPreview() )
417       anObj->SetParameters(aParameters.join(":").toUtf8().constData());
418     objects.push_back( anObj._retn() );
419   }
420   return true;
421 }
422
423 //=================================================================================
424 // function : addSubshapeToStudy
425 // purpose  : virtual method to add new SubObjects if local selection
426 //=================================================================================
427 void BasicGUI_EllipseDlg::addSubshapesToStudy()
428 {
429   GEOMBase::PublishSubObject( myPoint.get() );
430   GEOMBase::PublishSubObject( myDir.get() );
431   GEOMBase::PublishSubObject( myMajor.get() );
432 }
433
434 //=================================================================================
435 // function : getSourceObjects
436 // purpose  : virtual method to get source objects
437 //=================================================================================
438 QList<GEOM::GeomObjPtr> BasicGUI_EllipseDlg::getSourceObjects()
439 {
440   QList<GEOM::GeomObjPtr> res;
441   res << myPoint << myDir << myMajor;
442   return res;
443 }