Salome HOME
9b9eb6c07b7f2059c473b7060964722298eab5d7
[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_2Sel2Spin( centralWidget() );
72   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
73   GroupPoints->TextLabel1->setText( tr( "GEOM_CENTER" ) + " (Origin by default)" );
74   GroupPoints->TextLabel2->setText( tr( "GEOM_VECTOR" ) + " (Z axis by default)" );
75   GroupPoints->TextLabel3->setText( tr( "GEOM_RADIUS_MAJOR" ) );
76   GroupPoints->TextLabel4->setText( tr( "GEOM_RADIUS_MINOR" ) );
77   GroupPoints->PushButton1->setIcon( image1 );
78   GroupPoints->PushButton2->setIcon( image1 );
79
80   GroupPoints->LineEdit1->setReadOnly( true );
81   GroupPoints->LineEdit2->setReadOnly( true );
82   GroupPoints->LineEdit1->setEnabled( true );
83   GroupPoints->LineEdit2->setEnabled( false );
84
85   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
86   layout->setMargin( 0 ); layout->setSpacing( 6 );
87   layout->addWidget( GroupPoints );
88   /***************************************************************/
89
90   setHelpFileName( "create_ellipse_page.html" );
91
92   Init();
93 }
94
95
96 //=================================================================================
97 // function : ~BasicGUI_EllipseDlg()
98 // purpose  : Destroys the object and frees any allocated resources
99 //=================================================================================
100 BasicGUI_EllipseDlg::~BasicGUI_EllipseDlg()
101 {
102 }
103
104
105 //=================================================================================
106 // function : Init()
107 // purpose  :
108 //=================================================================================
109 void BasicGUI_EllipseDlg::Init()
110 {
111   /* init variables */
112   myEditCurrentArgument = GroupPoints->LineEdit1;
113   globalSelection(); // close local contexts, if any
114   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
115   GroupPoints->PushButton1->setDown(true);
116
117   myPoint = myDir = GEOM::GEOM_Object::_nil();
118
119   /* Get setting of step value from file configuration */
120   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
121   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
122   
123   double aMajorR( 200. ), aMinorR( 100. );
124
125   /* min, max, step and decimals for spin boxes & initial values */
126   initSpinBox( GroupPoints->SpinBox_DX, 0.000001, COORD_MAX, step, 6 ); // VSR: TODO: DBL_DIGITS_DISPLAY
127   initSpinBox( GroupPoints->SpinBox_DY, 0.000001, COORD_MAX, step, 6 ); // VSR: TODO: DBL_DIGITS_DISPLAY
128   GroupPoints->SpinBox_DX->setValue( aMajorR );
129   GroupPoints->SpinBox_DY->setValue( aMinorR );
130
131   /* signals and slots connections */
132   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
133   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
134
135   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
136   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
137
138   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
139   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
140
141   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
142   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
143
144   connect( GroupPoints->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
145   connect( GroupPoints->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
146
147   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
148   
149   connect( myGeomGUI->getApp()->selectionMgr(), 
150           SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
151
152   initName( tr( "GEOM_ELLIPSE" ) );
153
154   displayPreview();
155 }
156
157 //=================================================================================
158 // function : SetDoubleSpinBoxStep()
159 // purpose  : Double spin box management
160 //=================================================================================
161 void BasicGUI_EllipseDlg::SetDoubleSpinBoxStep( double step )
162 {
163   GroupPoints->SpinBox_DX->setSingleStep(step);
164   GroupPoints->SpinBox_DY->setSingleStep(step);
165 }
166
167
168 //=================================================================================
169 // function : ClickOnOk()
170 // purpose  :
171 //=================================================================================
172 void BasicGUI_EllipseDlg::ClickOnOk()
173 {
174   if ( ClickOnApply() )
175     ClickOnCancel();
176 }
177
178
179 //=================================================================================
180 // function : ClickOnApply()
181 // purpose  :
182 //=================================================================================
183 bool BasicGUI_EllipseDlg::ClickOnApply()
184 {
185   if ( !onAccept() )
186     return false;
187
188   initName();
189
190   // reset
191   myPoint = myDir = GEOM::GEOM_Object::_nil();
192   GroupPoints->LineEdit1->setText( "" );
193   GroupPoints->LineEdit2->setText( "" );
194   GroupPoints->PushButton1->setDown(true);
195   GroupPoints->PushButton2->setDown(false);
196   GroupPoints->LineEdit1->setEnabled( true );
197   GroupPoints->LineEdit2->setEnabled( false );
198   myEditCurrentArgument = GroupPoints->LineEdit1;
199
200   globalSelection(); // close local contexts, if any
201   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
202   displayPreview();
203   
204   return true;
205 }
206
207 //=================================================================================
208 // function : SelectionIntoArgument()
209 // purpose  : Called when selection as changed or other case
210 //=================================================================================
211 void BasicGUI_EllipseDlg::SelectionIntoArgument()
212 {
213   myEditCurrentArgument->setText( "" );
214
215   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
216   SALOME_ListIO aSelList;
217   aSelMgr->selectedObjects(aSelList);
218
219   if (aSelList.Extent() != 1) {
220     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint = GEOM::GEOM_Object::_nil();
221     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myDir   = GEOM::GEOM_Object::_nil();
222     return;
223   }
224
225   Standard_Boolean aRes = Standard_False;
226   Handle(SALOME_InteractiveObject) anIO = aSelList.First();
227   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(anIO, aRes);
228   if (!CORBA::is_nil(aSelectedObject) && aRes) {
229     QString aName = GEOMBase::GetName(aSelectedObject);
230     
231     // Get Selected object if selected subshape
232     TopoDS_Shape aShape;
233
234     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
235     if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
236       aNeedType = TopAbs_EDGE;
237     
238     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
239       TColStd_IndexedMapOfInteger aMap;
240       aSelMgr->GetIndexes(anIO, aMap);
241       if (aMap.Extent() == 1) {
242         int anIndex = aMap(1);
243         if (aNeedType == TopAbs_EDGE)
244           aName += QString(":edge_%1").arg(anIndex);
245         else
246           aName += QString(":vertex_%1").arg(anIndex);
247
248         //Find SubShape Object in Father
249         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
250
251         if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
252           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
253           aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
254         } 
255         else {
256           aSelectedObject = aFindedObject; // get Object from study
257         }
258       }
259       else { // Global Selection
260         if (aShape.ShapeType() != aNeedType) {
261           aSelectedObject = GEOM::GEOM_Object::_nil();
262           aName = "";
263         }
264       }
265     }
266     
267     myEditCurrentArgument->setText( aName );
268
269     if (!aSelectedObject->_is_nil()) { // clear selection if something selected
270       globalSelection();
271       localSelection( GEOM::GEOM_Object::_nil(), aNeedType );      
272     }
273
274     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
275       myPoint = aSelectedObject;
276       if ( !myPoint->_is_nil() && myDir->_is_nil() )
277         GroupPoints->PushButton2->click();
278     }
279     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
280       myDir   = aSelectedObject;
281       if ( !myDir->_is_nil() && myPoint->_is_nil() )
282         GroupPoints->PushButton1->click();
283     }
284   }
285
286   displayPreview();
287 }
288
289
290 //=================================================================================
291 // function : SetEditCurrentArgument()
292 // purpose  :
293 //=================================================================================
294 void BasicGUI_EllipseDlg::SetEditCurrentArgument()
295 {
296   QPushButton* send = (QPushButton*)sender();
297
298   if ( send == GroupPoints->PushButton1 ) {
299     myEditCurrentArgument = GroupPoints->LineEdit1;
300     GroupPoints->PushButton2->setDown(false);
301     GroupPoints->LineEdit1->setEnabled( true );
302     GroupPoints->LineEdit2->setEnabled( false );
303   }
304   else if ( send == GroupPoints->PushButton2 ) {
305     myEditCurrentArgument = GroupPoints->LineEdit2;
306     GroupPoints->PushButton1->setDown(false);
307     GroupPoints->LineEdit1->setEnabled( false );
308     GroupPoints->LineEdit2->setEnabled( true );
309   }
310
311   globalSelection(); // close local contexts, if any
312   TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
313   if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
314     aNeedType = TopAbs_EDGE;
315   localSelection( GEOM::GEOM_Object::_nil(), aNeedType );
316   
317   myEditCurrentArgument->setFocus();
318   //SelectionIntoArgument();
319   send->setDown(true);
320   displayPreview();
321 }
322
323 //=================================================================================
324 // function : LineEditReturnPressed()
325 // purpose  :
326 //=================================================================================
327 void BasicGUI_EllipseDlg::LineEditReturnPressed()
328 {
329   QLineEdit* send = (QLineEdit*)sender();
330   if ( send == GroupPoints->LineEdit1 ||
331        send == GroupPoints->LineEdit2 ) {
332     myEditCurrentArgument = send;
333     GEOMBase_Skeleton::LineEditReturnPressed();
334   }
335 }
336
337
338 //=================================================================================
339 // function : ActivateThisDialog()
340 // purpose  :
341 //=================================================================================
342 void BasicGUI_EllipseDlg::ActivateThisDialog()
343 {
344   GEOMBase_Skeleton::ActivateThisDialog();
345   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
346            this, SLOT( SelectionIntoArgument() ) );
347   
348   GroupPoints->LineEdit1->setFocus();
349   myEditCurrentArgument = GroupPoints->LineEdit1;
350
351   GroupPoints->LineEdit1->setText( "" );
352   GroupPoints->LineEdit2->setText( "" );
353
354   myPoint = myDir = GEOM::GEOM_Object::_nil();
355   //globalSelection( GEOM_POINT );
356   globalSelection(); // close local contexts, if any
357   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
358 }
359
360 //=================================================================================
361 // function : DeactivateActiveDialog()
362 // purpose  : public slot to deactivate if active
363 //=================================================================================
364 void BasicGUI_EllipseDlg::DeactivateActiveDialog()
365 {
366   // myGeomGUI->SetState( -1 );
367   GEOMBase_Skeleton::DeactivateActiveDialog();
368 }
369
370 //=================================================================================
371 // function : enterEvent()
372 // purpose  :
373 //=================================================================================
374 void BasicGUI_EllipseDlg::enterEvent( QEvent* )
375 {
376   if ( !mainFrame()->GroupConstructors->isEnabled() )
377     ActivateThisDialog();
378 }
379
380 //=================================================================================
381 // function : ValueChangedInSpinBox()
382 // purpose  :
383 //=================================================================================
384 void BasicGUI_EllipseDlg::ValueChangedInSpinBox( double newValue )
385 {
386   displayPreview();
387 }
388
389 //=================================================================================
390 // function : createOperation
391 // purpose  :
392 //=================================================================================
393 GEOM::GEOM_IOperations_ptr BasicGUI_EllipseDlg::createOperation()
394 {
395   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
396 }
397
398 //=================================================================================
399 // function : isValid
400 // purpose  :
401 //=================================================================================
402 bool BasicGUI_EllipseDlg::isValid( QString& msg )
403 {
404   double aMajorR = GroupPoints->SpinBox_DX->value();
405   double aMinorR = GroupPoints->SpinBox_DY->value();
406   if ( aMajorR < aMinorR ) {
407     msg = tr( "GEOM_ELLIPSE_ERROR_1" );
408     return false;
409   }
410   //return !myPoint->_is_nil() && !myDir->_is_nil();
411   //nil point means origin of global CS
412   //nil vector means Z axis
413   bool ok = true;
414   ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
415   ok = GroupPoints->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
416   return ok;
417 }
418
419 //=================================================================================
420 // function : execute
421 // purpose  :
422 //=================================================================================
423 bool BasicGUI_EllipseDlg::execute( ObjectList& objects )
424 {
425   double aMajorR = GroupPoints->SpinBox_DX->value();
426   double aMinorR = GroupPoints->SpinBox_DY->value();
427
428   QStringList aParameters;
429   aParameters<<GroupPoints->SpinBox_DX->text();
430   aParameters<<GroupPoints->SpinBox_DY->text();
431   
432   GEOM::GEOM_Object_var anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeEllipse( myPoint, myDir, aMajorR, aMinorR );
433   if ( !anObj->_is_nil() ) {
434     if ( !IsPreview() )
435       anObj->SetParameters(GeometryGUI::JoinObjectParameters(aParameters));
436     objects.push_back( anObj._retn() );
437   }
438   return true;
439 }
440
441 //=================================================================================
442 // function : addSubshapeToStudy
443 // purpose  : virtual method to add new SubObjects if local selection
444 //=================================================================================
445 void BasicGUI_EllipseDlg::addSubshapesToStudy()
446 {
447   QMap<QString, GEOM::GEOM_Object_var> objMap;
448
449   if (!CORBA::is_nil(myPoint))
450     objMap[GroupPoints->LineEdit1->text()] = myPoint;
451   if (!CORBA::is_nil(myDir))
452     objMap[GroupPoints->LineEdit2->text()] = myDir;
453
454   addSubshapesToFather( objMap );
455 }