Salome HOME
*** empty log message ***
[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 <GEOM_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 <GEOMImpl_Types.hxx>
38
39 //=================================================================================
40 // class    : BasicGUI_EllipseDlg()
41 // purpose  : Constructs a BasicGUI_EllipseDlg which is a child of 'parent', with the 
42 //            name 'name' and widget flags set to 'f'.
43 //            The dialog will by default be modeless, unless you set 'modal' to
44 //            TRUE to construct a modal dialog.
45 //=================================================================================
46 BasicGUI_EllipseDlg::BasicGUI_EllipseDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
47                                           bool modal, Qt::WindowFlags fl )
48   : GEOMBase_Skeleton( theGeometryGUI, parent, "BasicGUI_EllipseDlg", modal, fl )
49 {
50   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_ELLIPSE_PV" ) ) );
51   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
52
53   setWindowTitle( tr( "GEOM_ELLIPSE_TITLE" ) );
54
55   /***************************************************************/
56   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_ELLIPSE" ) );
57   mainFrame()->RadioButton1->setIcon( image0 );
58   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
59   mainFrame()->RadioButton2->close();
60   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
61   mainFrame()->RadioButton3->close();
62
63   GroupPoints = new DlgRef_2Sel2Spin( centralWidget() );
64   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
65   GroupPoints->TextLabel1->setText( tr( "GEOM_CENTER" ) );
66   GroupPoints->TextLabel2->setText( tr( "GEOM_VECTOR" ) );
67   GroupPoints->TextLabel3->setText( tr( "GEOM_RADIUS_MAJOR" ) );
68   GroupPoints->TextLabel4->setText( tr( "GEOM_RADIUS_MINOR" ) );
69   GroupPoints->PushButton1->setIcon( image1 );
70   GroupPoints->PushButton2->setIcon( image1 );
71
72   GroupPoints->LineEdit1->setReadOnly( true );
73   GroupPoints->LineEdit2->setReadOnly( true );
74
75   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
76   layout->setMargin( 0 ); layout->setSpacing( 6 );
77   layout->addWidget( GroupPoints );
78   /***************************************************************/
79
80   setHelpFileName( "ellipse.htm" );
81
82   Init();
83 }
84
85
86 //=================================================================================
87 // function : ~BasicGUI_EllipseDlg()
88 // purpose  : Destroys the object and frees any allocated resources
89 //=================================================================================
90 BasicGUI_EllipseDlg::~BasicGUI_EllipseDlg()
91 {
92 }
93
94
95 //=================================================================================
96 // function : Init()
97 // purpose  :
98 //=================================================================================
99 void BasicGUI_EllipseDlg::Init()
100 {
101   /* init variables */
102   myEditCurrentArgument = GroupPoints->LineEdit1;
103   globalSelection( GEOM_POINT );
104
105   myPoint = myDir = GEOM::GEOM_Object::_nil();
106
107   // myGeomGUI->SetState( 0 );
108
109   /* Get setting of step value from file configuration */
110   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
111   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
112   
113   double aMajorR( 200. ), aMinorR( 100. );
114
115   /* min, max, step and decimals for spin boxes & initial values */
116   initSpinBox( GroupPoints->SpinBox_DX, 0.001, COORD_MAX, step, 3 );
117   initSpinBox( GroupPoints->SpinBox_DY, 0.001, COORD_MAX, step, 3 );
118   GroupPoints->SpinBox_DX->setValue( aMajorR );
119   GroupPoints->SpinBox_DY->setValue( aMinorR );
120
121   /* signals and slots connections */
122   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
123   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
124
125   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
126   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
127
128   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
129   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
130
131   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
132   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
133
134   connect( GroupPoints->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
135   connect( GroupPoints->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
136
137   // VSR: TODO ->>
138   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupPoints->SpinBox_DX, SLOT( SetStep( double ) ) );
139   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupPoints->SpinBox_DY, SLOT( SetStep( double ) ) );
140   // <<-
141   
142   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
143           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
144
145   initName( tr( "GEOM_ELLIPSE" ) );
146 }
147
148
149 //=================================================================================
150 // function : ClickOnOk()
151 // purpose  :
152 //=================================================================================
153 void BasicGUI_EllipseDlg::ClickOnOk()
154 {
155   if ( ClickOnApply() )
156     ClickOnCancel();
157 }
158
159
160 //=================================================================================
161 // function : ClickOnApply()
162 // purpose  :
163 //=================================================================================
164 bool BasicGUI_EllipseDlg::ClickOnApply()
165 {
166   if ( !onAccept() )
167     return false;
168
169   initName();
170
171   // reset
172   myPoint = myDir = GEOM::GEOM_Object::_nil();
173   GroupPoints->LineEdit1->setText( "" );
174   GroupPoints->LineEdit2->setText( "" );
175   myEditCurrentArgument = GroupPoints->LineEdit1;
176   globalSelection( GEOM_POINT );
177   
178   return true;
179 }
180
181 //=================================================================================
182 // function : SelectionIntoArgument()
183 // purpose  : Called when selection as changed or other case
184 //=================================================================================
185 void BasicGUI_EllipseDlg::SelectionIntoArgument()
186 {
187   myEditCurrentArgument->setText( "" );
188
189   if ( IObjectCount() != 1 ) {
190     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint = GEOM::GEOM_Object::_nil();
191     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myDir   = GEOM::GEOM_Object::_nil();
192     return;
193   }
194
195   Standard_Boolean aRes = Standard_False;
196   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
197   if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {  
198     myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
199     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint = aSelectedObject;
200     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myDir   = aSelectedObject;
201   }
202
203   displayPreview();
204 }
205
206
207 //=================================================================================
208 // function : SetEditCurrentArgument()
209 // purpose  :
210 //=================================================================================
211 void BasicGUI_EllipseDlg::SetEditCurrentArgument()
212 {
213   QPushButton* send = (QPushButton*)sender();
214
215   if      ( send == GroupPoints->PushButton1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
216   else if ( send == GroupPoints->PushButton2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
217   
218   myEditCurrentArgument->setFocus();
219   if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
220     globalSelection( GEOM_LINE );
221   else
222     globalSelection( GEOM_POINT );
223   SelectionIntoArgument();
224 }
225
226 //=================================================================================
227 // function : LineEditReturnPressed()
228 // purpose  :
229 //=================================================================================
230 void BasicGUI_EllipseDlg::LineEditReturnPressed()
231 {
232   QLineEdit* send = (QLineEdit*)sender();
233   if ( send == GroupPoints->LineEdit1 ||
234        send == GroupPoints->LineEdit2 ) {
235     myEditCurrentArgument = send;
236     GEOMBase_Skeleton::LineEditReturnPressed();
237   }
238 }
239
240
241 //=================================================================================
242 // function : ActivateThisDialog()
243 // purpose  :
244 //=================================================================================
245 void BasicGUI_EllipseDlg::ActivateThisDialog()
246 {
247   GEOMBase_Skeleton::ActivateThisDialog();
248   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
249            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
250   
251   GroupPoints->LineEdit1->setFocus();
252   myEditCurrentArgument = GroupPoints->LineEdit1;
253
254   GroupPoints->LineEdit1->setText( "" );
255   GroupPoints->LineEdit2->setText( "" );
256
257   myPoint = myDir = GEOM::GEOM_Object::_nil();
258   globalSelection( GEOM_POINT );
259 }
260
261 //=================================================================================
262 // function : DeactivateActiveDialog()
263 // purpose  : public slot to deactivate if active
264 //=================================================================================
265 void BasicGUI_EllipseDlg::DeactivateActiveDialog()
266 {
267   // myGeomGUI->SetState( -1 );
268   GEOMBase_Skeleton::DeactivateActiveDialog();
269 }
270
271 //=================================================================================
272 // function : enterEvent()
273 // purpose  :
274 //=================================================================================
275 void BasicGUI_EllipseDlg::enterEvent( QEvent* )
276 {
277   if ( !mainFrame()->GroupConstructors->isEnabled() )
278     ActivateThisDialog();
279 }
280
281 //=================================================================================
282 // function : ValueChangedInSpinBox()
283 // purpose  :
284 //=================================================================================
285 void BasicGUI_EllipseDlg::ValueChangedInSpinBox( double newValue )
286 {
287   displayPreview();
288 }
289
290 //=================================================================================
291 // function : createOperation
292 // purpose  :
293 //=================================================================================
294 GEOM::GEOM_IOperations_ptr BasicGUI_EllipseDlg::createOperation()
295 {
296   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
297 }
298
299 //=================================================================================
300 // function : isValid
301 // purpose  :
302 //=================================================================================
303 bool BasicGUI_EllipseDlg::isValid( QString& msg )
304 {
305   double aMajorR = GroupPoints->SpinBox_DX->value();
306   double aMinorR = GroupPoints->SpinBox_DY->value();
307   if ( aMajorR < aMinorR ) {
308     msg = tr( "GEOM_ELLIPSE_ERROR_1" );
309     return false;
310   }
311   return !myPoint->_is_nil() && !myDir->_is_nil();
312 }
313
314 //=================================================================================
315 // function : execute
316 // purpose  :
317 //=================================================================================
318 bool BasicGUI_EllipseDlg::execute( ObjectList& objects )
319 {
320   double aMajorR = GroupPoints->SpinBox_DX->value();
321   double aMinorR = GroupPoints->SpinBox_DY->value();
322   GEOM::GEOM_Object_var anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeEllipse( myPoint, myDir, aMajorR, aMinorR );
323
324   if ( !anObj->_is_nil() )
325     objects.push_back( anObj._retn() );
326
327   return true;
328 }
329
330 //=================================================================================
331 // function : closeEvent
332 // purpose  :
333 //=================================================================================
334 void BasicGUI_EllipseDlg::closeEvent( QCloseEvent* e )
335 {
336   GEOMBase_Skeleton::closeEvent( e );
337 }