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