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