Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[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 //
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 "LightApp_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,
51                                          const char* name, bool modal, WFlags fl)
52   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
53                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
54 {
55   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_ELLIPSE_PV")));
56   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
57
58   setCaption(tr("GEOM_ELLIPSE_TITLE"));
59
60   /***************************************************************/
61   GroupConstructors->setTitle(tr("GEOM_ELLIPSE"));
62   RadioButton1->setPixmap(image0);
63   RadioButton2->close(TRUE);
64   RadioButton3->close(TRUE);
65
66   GroupPoints = new DlgRef_2Sel2Spin(this, "GroupPoints");
67   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
68   GroupPoints->TextLabel1->setText(tr("GEOM_CENTER"));
69   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
70   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS_MAJOR"));
71   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_MINOR"));
72   GroupPoints->PushButton1->setPixmap(image1);
73   GroupPoints->PushButton2->setPixmap(image1);
74
75   GroupPoints->LineEdit1->setReadOnly( true );
76   GroupPoints->LineEdit2->setReadOnly( true );
77
78   Layout1->addWidget(GroupPoints, 2, 0);
79   /***************************************************************/
80
81   setHelpFileName("ellipse.htm");
82
83   Init();
84 }
85
86
87 //=================================================================================
88 // function : ~BasicGUI_EllipseDlg()
89 // purpose  : Destroys the object and frees any allocated resources
90 //=================================================================================
91 BasicGUI_EllipseDlg::~BasicGUI_EllipseDlg()
92 {
93 }
94
95
96 //=================================================================================
97 // function : Init()
98 // purpose  :
99 //=================================================================================
100 void BasicGUI_EllipseDlg::Init()
101 {
102   /* init variables */
103   myEditCurrentArgument = GroupPoints->LineEdit1;
104   globalSelection( GEOM_POINT );
105
106   myPoint = myDir = GEOM::GEOM_Object::_nil();
107
108   // myGeomGUI->SetState( 0 );
109
110   /* Get setting of step value from file configuration */
111   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
112   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
113   
114   double aMajorR( 200. ), aMinorR( 100. );
115
116   /* min, max, step and decimals for spin boxes & initial values */
117   GroupPoints->SpinBox_DX->RangeStepAndValidator( 0.001, 999.999, step, 3 );
118   GroupPoints->SpinBox_DY->RangeStepAndValidator( 0.001, 999.999, step, 3 );
119   GroupPoints->SpinBox_DX->SetValue( aMajorR );
120   GroupPoints->SpinBox_DY->SetValue( aMinorR );
121
122   /* signals and slots connections */
123   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
124   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
125   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
126
127   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
128   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
129
130   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
131   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
132
133   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
134   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
135
136   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
137   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
138
139   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
140   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
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 : ClickOnCancel()
183 // purpose  :
184 //=======================================================================
185 void BasicGUI_EllipseDlg::ClickOnCancel()
186 {
187   GEOMBase_Skeleton::ClickOnCancel();
188 }
189
190 //=================================================================================
191 // function : SelectionIntoArgument()
192 // purpose  : Called when selection as changed or other case
193 //=================================================================================
194 void BasicGUI_EllipseDlg::SelectionIntoArgument()
195 {
196   myEditCurrentArgument->setText("");
197
198   if ( IObjectCount() != 1 )  
199   {
200     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint = GEOM::GEOM_Object::_nil();
201     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myDir   = GEOM::GEOM_Object::_nil();
202     return;
203   }
204
205   Standard_Boolean aRes = Standard_False;
206   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
207   if ( !CORBA::is_nil( aSelectedObject ) && aRes )
208   {  
209     myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
210     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint = aSelectedObject;
211     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myDir   = aSelectedObject;
212   }
213
214   displayPreview();
215 }
216
217
218 //=================================================================================
219 // function : SetEditCurrentArgument()
220 // purpose  :
221 //=================================================================================
222 void BasicGUI_EllipseDlg::SetEditCurrentArgument()
223 {
224   QPushButton* send = (QPushButton*)sender();
225
226   if      ( send == GroupPoints->PushButton1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
227   else if ( send == GroupPoints->PushButton2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
228   
229   myEditCurrentArgument->setFocus();
230   if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
231     globalSelection( GEOM_LINE );
232   else
233     globalSelection( GEOM_POINT );
234   SelectionIntoArgument();
235 }
236
237 //=================================================================================
238 // function : LineEditReturnPressed()
239 // purpose  :
240 //=================================================================================
241 void BasicGUI_EllipseDlg::LineEditReturnPressed()
242 {
243   QLineEdit* send = (QLineEdit*)sender();
244   if ( send == GroupPoints->LineEdit1 ||
245        send == GroupPoints->LineEdit2 )
246   {
247     myEditCurrentArgument = send;
248     GEOMBase_Skeleton::LineEditReturnPressed();
249   }
250 }
251
252
253 //=================================================================================
254 // function : ActivateThisDialog()
255 // purpose  :
256 //=================================================================================
257 void BasicGUI_EllipseDlg::ActivateThisDialog()
258 {
259   GEOMBase_Skeleton::ActivateThisDialog();
260   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
261           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
262   
263   GroupPoints->LineEdit1->setFocus();
264   myEditCurrentArgument = GroupPoints->LineEdit1;
265
266   GroupPoints->LineEdit1->setText( "" );
267   GroupPoints->LineEdit2->setText( "" );
268
269   myPoint = myDir = GEOM::GEOM_Object::_nil();
270   globalSelection( GEOM_POINT );
271 }
272
273 //=================================================================================
274 // function : DeactivateActiveDialog()
275 // purpose  : public slot to deactivate if active
276 //=================================================================================
277 void BasicGUI_EllipseDlg::DeactivateActiveDialog()
278 {
279   // myGeomGUI->SetState( -1 );
280   GEOMBase_Skeleton::DeactivateActiveDialog();
281 }
282
283 //=================================================================================
284 // function : enterEvent()
285 // purpose  :
286 //=================================================================================
287 void BasicGUI_EllipseDlg::enterEvent(QEvent* e)
288 {
289   if ( !GroupConstructors->isEnabled() )
290     ActivateThisDialog();
291 }
292
293 //=================================================================================
294 // function : ValueChangedInSpinBox()
295 // purpose  :
296 //=================================================================================
297 void BasicGUI_EllipseDlg::ValueChangedInSpinBox(double newValue)
298 {
299   displayPreview();
300 }
301
302 //=================================================================================
303 // function : createOperation
304 // purpose  :
305 //=================================================================================
306 GEOM::GEOM_IOperations_ptr BasicGUI_EllipseDlg::createOperation()
307 {
308   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
309 }
310
311 //=================================================================================
312 // function : isValid
313 // purpose  :
314 //=================================================================================
315 bool BasicGUI_EllipseDlg::isValid( QString& msg )
316 {
317   double aMajorR = GroupPoints->SpinBox_DX->GetValue();
318   double aMinorR = GroupPoints->SpinBox_DY->GetValue();
319   if ( aMajorR < aMinorR )
320   {
321         msg = tr( "GEOM_ELLIPSE_ERROR_1" );
322         return false;
323   }
324   return !myPoint->_is_nil() && !myDir->_is_nil();
325 }
326
327 //=================================================================================
328 // function : execute
329 // purpose  :
330 //=================================================================================
331 bool BasicGUI_EllipseDlg::execute( ObjectList& objects )
332 {
333   double aMajorR = GroupPoints->SpinBox_DX->GetValue();
334   double aMinorR = GroupPoints->SpinBox_DY->GetValue();
335   GEOM::GEOM_Object_var anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeEllipse( myPoint, myDir, aMajorR, aMinorR );
336
337   if ( !anObj->_is_nil() )
338     objects.push_back( anObj._retn() );
339
340   return true;
341 }
342
343 //=================================================================================
344 // function : closeEvent
345 // purpose  :
346 //=================================================================================
347 void BasicGUI_EllipseDlg::closeEvent( QCloseEvent* e )
348 {
349   GEOMBase_Skeleton::closeEvent( e );
350 }
351