Salome HOME
Merge from BR_WIN_INDUS_514 04/10/2010
[modules/geom.git] / src / BasicGUI / BasicGUI_EllipseDlg.cxx
1 //  Copyright (C) 2007-2010  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
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   globalSelection(); // close local contexts, if any
119   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
120   GroupPoints->PushButton1->setDown(true);
121
122   myPoint = myDir = myMajor = GEOM::GEOM_Object::_nil();
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->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
148   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
149   connect( GroupPoints->LineEdit3, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
150
151   connect( GroupPoints->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
152   connect( GroupPoints->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
153
154   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
155   
156   connect( myGeomGUI->getApp()->selectionMgr(), 
157           SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
158
159   initName( tr( "GEOM_ELLIPSE" ) );
160
161   resize( minimumSizeHint() );
162   SelectionIntoArgument();
163   displayPreview();
164 }
165
166 //=================================================================================
167 // function : SetDoubleSpinBoxStep()
168 // purpose  : Double spin box management
169 //=================================================================================
170 void BasicGUI_EllipseDlg::SetDoubleSpinBoxStep( double step )
171 {
172   GroupPoints->SpinBox_DX->setSingleStep(step);
173   GroupPoints->SpinBox_DY->setSingleStep(step);
174 }
175
176
177 //=================================================================================
178 // function : ClickOnOk()
179 // purpose  :
180 //=================================================================================
181 void BasicGUI_EllipseDlg::ClickOnOk()
182 {
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 = myDir = myMajor = GEOM::GEOM_Object::_nil();
201   GroupPoints->LineEdit1->setText( "" );
202   GroupPoints->LineEdit2->setText( "" );
203   GroupPoints->LineEdit3->setText( "" );
204   GroupPoints->PushButton1->setDown(true);
205   GroupPoints->PushButton2->setDown(false);
206   GroupPoints->PushButton3->setDown(false);
207   GroupPoints->LineEdit1->setEnabled( true );
208   GroupPoints->LineEdit2->setEnabled( false );
209   GroupPoints->LineEdit3->setEnabled( false );
210   myEditCurrentArgument = GroupPoints->LineEdit1;
211
212   globalSelection(); // close local contexts, if any
213   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
214   displayPreview();
215   
216   return true;
217 }
218
219 //=================================================================================
220 // function : SelectionIntoArgument()
221 // purpose  : Called when selection as changed or other case
222 //=================================================================================
223 void BasicGUI_EllipseDlg::SelectionIntoArgument()
224 {
225   myEditCurrentArgument->setText( "" );
226
227   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
228   SALOME_ListIO aSelList;
229   aSelMgr->selectedObjects(aSelList);
230
231   if (aSelList.Extent() != 1) {
232     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint = GEOM::GEOM_Object::_nil();
233     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myDir   = GEOM::GEOM_Object::_nil();
234     else if (myEditCurrentArgument == GroupPoints->LineEdit3) myMajor = GEOM::GEOM_Object::_nil();
235     return;
236   }
237
238   Standard_Boolean aRes = Standard_False;
239   Handle(SALOME_InteractiveObject) anIO = aSelList.First();
240   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(anIO, aRes);
241   if (!CORBA::is_nil(aSelectedObject) && aRes) {
242     QString aName = GEOMBase::GetName(aSelectedObject);
243     
244     // Get Selected object if selected subshape
245     TopoDS_Shape aShape;
246
247     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
248     if ( myEditCurrentArgument == GroupPoints->LineEdit2 || myEditCurrentArgument == GroupPoints->LineEdit3 )
249       aNeedType = TopAbs_EDGE;
250     
251     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
252       TColStd_IndexedMapOfInteger aMap;
253       aSelMgr->GetIndexes(anIO, aMap);
254       if (aMap.Extent() == 1) {
255         int anIndex = aMap(1);
256         if (aNeedType == TopAbs_EDGE)
257           aName += QString(":edge_%1").arg(anIndex);
258         else
259           aName += QString(":vertex_%1").arg(anIndex);
260
261         //Find SubShape Object in Father
262         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
263
264                 if (aFindedObject->_is_nil()) { // Object not found in study
265           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
266           aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
267         } 
268         else {
269           aSelectedObject = aFindedObject; // get Object from study
270         }
271       }
272       else { // Global Selection
273         if (aShape.ShapeType() != aNeedType) {
274           aSelectedObject = GEOM::GEOM_Object::_nil();
275           aName = "";
276         }
277       }
278     }
279     
280     myEditCurrentArgument->setText( aName );
281
282     if (!aSelectedObject->_is_nil()) { // clear selection if something selected
283       globalSelection();
284       localSelection( GEOM::GEOM_Object::_nil(), aNeedType );      
285     }
286
287     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
288       myPoint = aSelectedObject;
289       if ( !myPoint->_is_nil() && myDir->_is_nil() )
290         GroupPoints->PushButton2->click();
291     }
292     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
293       myDir   = aSelectedObject;
294       if ( !myDir->_is_nil() && myMajor->_is_nil() )
295         GroupPoints->PushButton3->click();
296     }
297     else if ( myEditCurrentArgument == GroupPoints->LineEdit3 ) {
298       myMajor = aSelectedObject;
299       if ( !myMajor->_is_nil() && myPoint->_is_nil() )
300         GroupPoints->PushButton1->click();
301     }
302   }
303
304   displayPreview();
305 }
306
307
308 //=================================================================================
309 // function : SetEditCurrentArgument()
310 // purpose  :
311 //=================================================================================
312 void BasicGUI_EllipseDlg::SetEditCurrentArgument()
313 {
314   QPushButton* send = (QPushButton*)sender();
315
316   if ( send == GroupPoints->PushButton1 ) {
317     myEditCurrentArgument = GroupPoints->LineEdit1;
318     GroupPoints->PushButton2->setDown(false);
319     GroupPoints->PushButton3->setDown(false);
320     GroupPoints->LineEdit1->setEnabled( true );
321     GroupPoints->LineEdit2->setEnabled( false );
322     GroupPoints->LineEdit3->setEnabled( false );
323   }
324   else if ( send == GroupPoints->PushButton2 ) {
325     myEditCurrentArgument = GroupPoints->LineEdit2;
326     GroupPoints->PushButton1->setDown(false);
327     GroupPoints->PushButton3->setDown(false);
328     GroupPoints->LineEdit1->setEnabled( false );
329     GroupPoints->LineEdit2->setEnabled( true );
330     GroupPoints->LineEdit3->setEnabled( false );
331   }
332   else if ( send == GroupPoints->PushButton3 ) {
333     myEditCurrentArgument = GroupPoints->LineEdit3;
334     GroupPoints->PushButton1->setDown(false);
335     GroupPoints->PushButton2->setDown(false);
336     GroupPoints->LineEdit1->setEnabled( false );
337     GroupPoints->LineEdit2->setEnabled( false );
338     GroupPoints->LineEdit3->setEnabled( true );
339   }
340
341   globalSelection(); // close local contexts, if any
342   TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
343   if ( myEditCurrentArgument == GroupPoints->LineEdit2 || myEditCurrentArgument == GroupPoints->LineEdit3 )
344     aNeedType = TopAbs_EDGE;
345   localSelection( GEOM::GEOM_Object::_nil(), aNeedType );
346   
347   myEditCurrentArgument->setFocus();
348   //SelectionIntoArgument();
349   send->setDown(true);
350   displayPreview();
351 }
352
353 //=================================================================================
354 // function : LineEditReturnPressed()
355 // purpose  :
356 //=================================================================================
357 void BasicGUI_EllipseDlg::LineEditReturnPressed()
358 {
359   QLineEdit* send = (QLineEdit*)sender();
360   if ( send == GroupPoints->LineEdit1 ||
361        send == GroupPoints->LineEdit2 ||
362        send == GroupPoints->LineEdit3 ) {
363     myEditCurrentArgument = send;
364     GEOMBase_Skeleton::LineEditReturnPressed();
365   }
366 }
367
368
369 //=================================================================================
370 // function : ActivateThisDialog()
371 // purpose  :
372 //=================================================================================
373 void BasicGUI_EllipseDlg::ActivateThisDialog()
374 {
375   GEOMBase_Skeleton::ActivateThisDialog();
376   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
377            this, SLOT( SelectionIntoArgument() ) );
378   
379   GroupPoints->LineEdit1->setFocus();
380   myEditCurrentArgument = GroupPoints->LineEdit1;
381
382   GroupPoints->LineEdit1->setText( "" );
383   GroupPoints->LineEdit2->setText( "" );
384   GroupPoints->LineEdit3->setText( "" );
385
386   myPoint = myDir = GEOM::GEOM_Object::_nil();
387   //globalSelection( GEOM_POINT );
388   globalSelection(); // close local contexts, if any
389   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
390 }
391
392 //=================================================================================
393 // function : DeactivateActiveDialog()
394 // purpose  : public slot to deactivate if active
395 //=================================================================================
396 void BasicGUI_EllipseDlg::DeactivateActiveDialog()
397 {
398   // myGeomGUI->SetState( -1 );
399   GEOMBase_Skeleton::DeactivateActiveDialog();
400 }
401
402 //=================================================================================
403 // function : enterEvent()
404 // purpose  :
405 //=================================================================================
406 void BasicGUI_EllipseDlg::enterEvent( QEvent* )
407 {
408   if ( !mainFrame()->GroupConstructors->isEnabled() )
409     ActivateThisDialog();
410 }
411
412 //=================================================================================
413 // function : ValueChangedInSpinBox()
414 // purpose  :
415 //=================================================================================
416 void BasicGUI_EllipseDlg::ValueChangedInSpinBox( double newValue )
417 {
418   displayPreview();
419 }
420
421 //=================================================================================
422 // function : createOperation
423 // purpose  :
424 //=================================================================================
425 GEOM::GEOM_IOperations_ptr BasicGUI_EllipseDlg::createOperation()
426 {
427   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
428 }
429
430 //=================================================================================
431 // function : isValid
432 // purpose  :
433 //=================================================================================
434 bool BasicGUI_EllipseDlg::isValid( QString& msg )
435 {
436   double aMajorR = GroupPoints->SpinBox_DX->value();
437   double aMinorR = GroupPoints->SpinBox_DY->value();
438   if ( aMajorR < aMinorR ) {
439     msg = tr( "GEOM_ELLIPSE_ERROR_1" );
440     return false;
441   }
442   //return !myPoint->_is_nil() && !myDir->_is_nil();
443   //nil point means origin of global CS
444   //nil vector means Z axis
445   bool ok = true;
446   ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
447   ok = GroupPoints->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
448   return ok;
449 }
450
451 //=================================================================================
452 // function : execute
453 // purpose  :
454 //=================================================================================
455 bool BasicGUI_EllipseDlg::execute( ObjectList& objects )
456 {
457   double aMajorR = GroupPoints->SpinBox_DX->value();
458   double aMinorR = GroupPoints->SpinBox_DY->value();
459
460   QStringList aParameters;
461   aParameters<<GroupPoints->SpinBox_DX->text();
462   aParameters<<GroupPoints->SpinBox_DY->text();
463   
464   GEOM::GEOM_ICurvesOperations_var anOper = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() );
465   GEOM::GEOM_Object_var anObj = myMajor->_is_nil() ? 
466     anOper->MakeEllipse   ( myPoint, myDir, aMajorR, aMinorR )         :
467     anOper->MakeEllipseVec( myPoint, myDir, aMajorR, aMinorR, myMajor );
468   if ( !anObj->_is_nil() ) {
469     if ( !IsPreview() )
470       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
471     objects.push_back( anObj._retn() );
472   }
473   return true;
474 }
475
476 //=================================================================================
477 // function : addSubshapeToStudy
478 // purpose  : virtual method to add new SubObjects if local selection
479 //=================================================================================
480 void BasicGUI_EllipseDlg::addSubshapesToStudy()
481 {
482   QMap<QString, GEOM::GEOM_Object_var> objMap;
483
484   if (!CORBA::is_nil(myPoint))
485     objMap[GroupPoints->LineEdit1->text()] = myPoint;
486   if (!CORBA::is_nil(myDir))
487     objMap[GroupPoints->LineEdit2->text()] = myDir;
488   if (!CORBA::is_nil(myMajor))
489     objMap[GroupPoints->LineEdit3->text()] = myMajor;
490
491   addSubshapesToFather( objMap );
492 }