]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_EllipseDlg.cxx
Salome HOME
0020044: EDF 866 GEOM: Extrusion along a path : impossible to select a Wire
[modules/geom.git] / src / BasicGUI / BasicGUI_EllipseDlg.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2003  OPEN CASCADE 
4 // 
5 // This library is free software; you can redistribute it and/or 
6 // modify it under the terms of the GNU Lesser General Public 
7 // License as published by the Free Software Foundation; either 
8 // version 2.1 of the License. 
9 // 
10 // This library is distributed in the hope that it will be useful, 
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13 // Lesser General Public License for more details. 
14 // 
15 // You should have received a copy of the GNU Lesser General Public 
16 // License along with this library; if not, write to the Free Software 
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
18 // 
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 //
21 // File   : BasicGUI_EllipseDlg.cxx
22 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
23 //
24
25 #include "BasicGUI_EllipseDlg.h"
26
27 #include <DlgRef.h>
28
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_Session.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <TColStd_IndexedMapOfInteger.hxx>
38 #include <TopTools_IndexedMapOfShape.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS.hxx>
42 #include <TopExp.hxx>
43
44 #include <GEOMImpl_Types.hxx>
45
46 //=================================================================================
47 // class    : BasicGUI_EllipseDlg()
48 // purpose  : Constructs a BasicGUI_EllipseDlg which is a child of 'parent', with the 
49 //            name 'name' and widget flags set to 'f'.
50 //            The dialog will by default be modeless, unless you set 'modal' to
51 //            TRUE to construct a modal dialog.
52 //=================================================================================
53 BasicGUI_EllipseDlg::BasicGUI_EllipseDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
54                                           bool modal, Qt::WindowFlags fl )
55   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
56 {
57   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_ELLIPSE_PV" ) ) );
58   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
59
60   setWindowTitle( tr( "GEOM_ELLIPSE_TITLE" ) );
61
62   /***************************************************************/
63   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_ELLIPSE" ) );
64   mainFrame()->RadioButton1->setIcon( image0 );
65   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
66   mainFrame()->RadioButton2->close();
67   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
68   mainFrame()->RadioButton3->close();
69
70   GroupPoints = new DlgRef_2Sel2Spin( centralWidget() );
71   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
72   GroupPoints->TextLabel1->setText( tr( "GEOM_CENTER" ) + " (Origin by default)" );
73   GroupPoints->TextLabel2->setText( tr( "GEOM_VECTOR" ) + " (Z axis by default)" );
74   GroupPoints->TextLabel3->setText( tr( "GEOM_RADIUS_MAJOR" ) );
75   GroupPoints->TextLabel4->setText( tr( "GEOM_RADIUS_MINOR" ) );
76   GroupPoints->PushButton1->setIcon( image1 );
77   GroupPoints->PushButton2->setIcon( image1 );
78
79   GroupPoints->LineEdit1->setReadOnly( true );
80   GroupPoints->LineEdit2->setReadOnly( true );
81   GroupPoints->LineEdit1->setEnabled( true );
82   GroupPoints->LineEdit2->setEnabled( false );
83
84   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
85   layout->setMargin( 0 ); layout->setSpacing( 6 );
86   layout->addWidget( GroupPoints );
87   /***************************************************************/
88
89   setHelpFileName( "create_ellipse_page.html" );
90
91   Init();
92 }
93
94
95 //=================================================================================
96 // function : ~BasicGUI_EllipseDlg()
97 // purpose  : Destroys the object and frees any allocated resources
98 //=================================================================================
99 BasicGUI_EllipseDlg::~BasicGUI_EllipseDlg()
100 {
101 }
102
103
104 //=================================================================================
105 // function : Init()
106 // purpose  :
107 //=================================================================================
108 void BasicGUI_EllipseDlg::Init()
109 {
110   /* init variables */
111   myEditCurrentArgument = GroupPoints->LineEdit1;
112   globalSelection(); // close local contexts, if any
113   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
114   GroupPoints->PushButton1->setDown(true);
115
116   myPoint = myDir = GEOM::GEOM_Object::_nil();
117
118   /* Get setting of step value from file configuration */
119   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
120   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
121   
122   double aMajorR( 200. ), aMinorR( 100. );
123
124   /* min, max, step and decimals for spin boxes & initial values */
125   initSpinBox( GroupPoints->SpinBox_DX, 0.000001, COORD_MAX, step, 6 ); // VSR: TODO: DBL_DIGITS_DISPLAY
126   initSpinBox( GroupPoints->SpinBox_DY, 0.000001, COORD_MAX, step, 6 ); // VSR: TODO: DBL_DIGITS_DISPLAY
127   GroupPoints->SpinBox_DX->setValue( aMajorR );
128   GroupPoints->SpinBox_DY->setValue( aMinorR );
129
130   /* signals and slots connections */
131   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
132   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
133
134   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
135   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
136
137   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
138   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
139
140   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
141   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
142
143   connect( GroupPoints->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
144   connect( GroupPoints->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
145
146   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
147   
148   connect( myGeomGUI->getApp()->selectionMgr(), 
149           SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
150
151   initName( tr( "GEOM_ELLIPSE" ) );
152
153   displayPreview();
154 }
155
156 //=================================================================================
157 // function : SetDoubleSpinBoxStep()
158 // purpose  : Double spin box management
159 //=================================================================================
160 void BasicGUI_EllipseDlg::SetDoubleSpinBoxStep( double step )
161 {
162   GroupPoints->SpinBox_DX->setSingleStep(step);
163   GroupPoints->SpinBox_DY->setSingleStep(step);
164 }
165
166
167 //=================================================================================
168 // function : ClickOnOk()
169 // purpose  :
170 //=================================================================================
171 void BasicGUI_EllipseDlg::ClickOnOk()
172 {
173   if ( ClickOnApply() )
174     ClickOnCancel();
175 }
176
177
178 //=================================================================================
179 // function : ClickOnApply()
180 // purpose  :
181 //=================================================================================
182 bool BasicGUI_EllipseDlg::ClickOnApply()
183 {
184   if ( !onAccept() )
185     return false;
186
187   initName();
188
189   // reset
190   myPoint = myDir = GEOM::GEOM_Object::_nil();
191   GroupPoints->LineEdit1->setText( "" );
192   GroupPoints->LineEdit2->setText( "" );
193   GroupPoints->PushButton1->setDown(true);
194   GroupPoints->PushButton2->setDown(false);
195   GroupPoints->LineEdit1->setEnabled( true );
196   GroupPoints->LineEdit2->setEnabled( false );
197   myEditCurrentArgument = GroupPoints->LineEdit1;
198
199   globalSelection(); // close local contexts, if any
200   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
201   displayPreview();
202   
203   return true;
204 }
205
206 //=================================================================================
207 // function : SelectionIntoArgument()
208 // purpose  : Called when selection as changed or other case
209 //=================================================================================
210 void BasicGUI_EllipseDlg::SelectionIntoArgument()
211 {
212   myEditCurrentArgument->setText( "" );
213
214   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
215   SALOME_ListIO aSelList;
216   aSelMgr->selectedObjects(aSelList);
217
218   if (aSelList.Extent() != 1) {
219     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint = GEOM::GEOM_Object::_nil();
220     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myDir   = GEOM::GEOM_Object::_nil();
221     return;
222   }
223
224   Standard_Boolean aRes = Standard_False;
225   Handle(SALOME_InteractiveObject) anIO = aSelList.First();
226   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(anIO, aRes);
227   if (!CORBA::is_nil(aSelectedObject) && aRes) {
228     QString aName = GEOMBase::GetName(aSelectedObject);
229     
230     // Get Selected object if selected subshape
231     TopoDS_Shape aShape;
232
233     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
234     if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
235       aNeedType = TopAbs_EDGE;
236     
237     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
238       TColStd_IndexedMapOfInteger aMap;
239       aSelMgr->GetIndexes(anIO, aMap);
240       if (aMap.Extent() == 1) {
241         int anIndex = aMap(1);
242         if (aNeedType == TopAbs_EDGE)
243           aName += QString(":edge_%1").arg(anIndex);
244         else
245           aName += QString(":vertex_%1").arg(anIndex);
246
247         //Find SubShape Object in Father
248         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
249
250         if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
251           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
252           aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
253         } 
254         else {
255           aSelectedObject = aFindedObject; // get Object from study
256         }
257       }
258       else { // Global Selection
259         if (aShape.ShapeType() != aNeedType) {
260           aSelectedObject = GEOM::GEOM_Object::_nil();
261           aName = "";
262         }
263       }
264     }
265     
266     myEditCurrentArgument->setText( aName );
267
268     if (!aSelectedObject->_is_nil()) { // clear selection if something selected
269       globalSelection();
270       localSelection( GEOM::GEOM_Object::_nil(), aNeedType );      
271     }
272
273     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
274       myPoint = aSelectedObject;
275       if ( !myPoint->_is_nil() && myDir->_is_nil() )
276         GroupPoints->PushButton2->click();
277     }
278     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
279       myDir   = aSelectedObject;
280       if ( !myDir->_is_nil() && myPoint->_is_nil() )
281         GroupPoints->PushButton1->click();
282     }
283   }
284
285   displayPreview();
286 }
287
288
289 //=================================================================================
290 // function : SetEditCurrentArgument()
291 // purpose  :
292 //=================================================================================
293 void BasicGUI_EllipseDlg::SetEditCurrentArgument()
294 {
295   QPushButton* send = (QPushButton*)sender();
296
297   if ( send == GroupPoints->PushButton1 ) {
298     myEditCurrentArgument = GroupPoints->LineEdit1;
299     GroupPoints->PushButton2->setDown(false);
300     GroupPoints->LineEdit1->setEnabled( true );
301     GroupPoints->LineEdit2->setEnabled( false );
302   }
303   else if ( send == GroupPoints->PushButton2 ) {
304     myEditCurrentArgument = GroupPoints->LineEdit2;
305     GroupPoints->PushButton1->setDown(false);
306     GroupPoints->LineEdit1->setEnabled( false );
307     GroupPoints->LineEdit2->setEnabled( true );
308   }
309
310   globalSelection(); // close local contexts, if any
311   TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
312   if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
313     aNeedType = TopAbs_EDGE;
314   localSelection( GEOM::GEOM_Object::_nil(), aNeedType );
315   
316   myEditCurrentArgument->setFocus();
317   //SelectionIntoArgument();
318   send->setDown(true);
319   displayPreview();
320 }
321
322 //=================================================================================
323 // function : LineEditReturnPressed()
324 // purpose  :
325 //=================================================================================
326 void BasicGUI_EllipseDlg::LineEditReturnPressed()
327 {
328   QLineEdit* send = (QLineEdit*)sender();
329   if ( send == GroupPoints->LineEdit1 ||
330        send == GroupPoints->LineEdit2 ) {
331     myEditCurrentArgument = send;
332     GEOMBase_Skeleton::LineEditReturnPressed();
333   }
334 }
335
336
337 //=================================================================================
338 // function : ActivateThisDialog()
339 // purpose  :
340 //=================================================================================
341 void BasicGUI_EllipseDlg::ActivateThisDialog()
342 {
343   GEOMBase_Skeleton::ActivateThisDialog();
344   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
345            this, SLOT( SelectionIntoArgument() ) );
346   
347   GroupPoints->LineEdit1->setFocus();
348   myEditCurrentArgument = GroupPoints->LineEdit1;
349
350   GroupPoints->LineEdit1->setText( "" );
351   GroupPoints->LineEdit2->setText( "" );
352
353   myPoint = myDir = GEOM::GEOM_Object::_nil();
354   //globalSelection( GEOM_POINT );
355   globalSelection(); // close local contexts, if any
356   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
357 }
358
359 //=================================================================================
360 // function : DeactivateActiveDialog()
361 // purpose  : public slot to deactivate if active
362 //=================================================================================
363 void BasicGUI_EllipseDlg::DeactivateActiveDialog()
364 {
365   // myGeomGUI->SetState( -1 );
366   GEOMBase_Skeleton::DeactivateActiveDialog();
367 }
368
369 //=================================================================================
370 // function : enterEvent()
371 // purpose  :
372 //=================================================================================
373 void BasicGUI_EllipseDlg::enterEvent( QEvent* )
374 {
375   if ( !mainFrame()->GroupConstructors->isEnabled() )
376     ActivateThisDialog();
377 }
378
379 //=================================================================================
380 // function : ValueChangedInSpinBox()
381 // purpose  :
382 //=================================================================================
383 void BasicGUI_EllipseDlg::ValueChangedInSpinBox( double newValue )
384 {
385   displayPreview();
386 }
387
388 //=================================================================================
389 // function : createOperation
390 // purpose  :
391 //=================================================================================
392 GEOM::GEOM_IOperations_ptr BasicGUI_EllipseDlg::createOperation()
393 {
394   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
395 }
396
397 //=================================================================================
398 // function : isValid
399 // purpose  :
400 //=================================================================================
401 bool BasicGUI_EllipseDlg::isValid( QString& msg )
402 {
403   double aMajorR = GroupPoints->SpinBox_DX->value();
404   double aMinorR = GroupPoints->SpinBox_DY->value();
405   if ( aMajorR < aMinorR ) {
406     msg = tr( "GEOM_ELLIPSE_ERROR_1" );
407     return false;
408   }
409   //return !myPoint->_is_nil() && !myDir->_is_nil();
410   //nil point means origin of global CS
411   //nil vector means Z axis
412   return true;
413 }
414
415 //=================================================================================
416 // function : execute
417 // purpose  :
418 //=================================================================================
419 bool BasicGUI_EllipseDlg::execute( ObjectList& objects )
420 {
421   double aMajorR = GroupPoints->SpinBox_DX->value();
422   double aMinorR = GroupPoints->SpinBox_DY->value();
423   GEOM::GEOM_Object_var anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeEllipse( myPoint, myDir, aMajorR, aMinorR );
424
425   if ( !anObj->_is_nil() )
426     objects.push_back( anObj._retn() );
427
428   return true;
429 }
430
431 //=================================================================================
432 // function : addSubshapeToStudy
433 // purpose  : virtual method to add new SubObjects if local selection
434 //=================================================================================
435 void BasicGUI_EllipseDlg::addSubshapesToStudy()
436 {
437   QMap<QString, GEOM::GEOM_Object_var> objMap;
438
439   if (!CORBA::is_nil(myPoint))
440     objMap[GroupPoints->LineEdit1->text()] = myPoint;
441   if (!CORBA::is_nil(myDir))
442     objMap[GroupPoints->LineEdit2->text()] = myDir;
443
444   addSubshapesToFather( objMap );
445 }