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