Salome HOME
Merge with version on tag OCC-V2_1_0d
[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 #include "QAD_Desktop.h"
30 #include "QAD_Config.h"
31
32 #include "GEOMImpl_Types.hxx"
33
34 #include "utilities.h"
35
36 using namespace std;
37
38 //=================================================================================
39 // class    : BasicGUI_EllipseDlg()
40 // purpose  : Constructs a BasicGUI_EllipseDlg which is a child of 'parent', with the 
41 //            name 'name' and widget flags set to 'f'.
42 //            The dialog will by default be modeless, unless you set 'modal' to
43 //            TRUE to construct a modal dialog.
44 //=================================================================================
45 BasicGUI_EllipseDlg::BasicGUI_EllipseDlg(QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
46   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
47 {
48   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_ELLIPSE_PV")));
49   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
50
51   setCaption(tr("GEOM_ELLIPSE_TITLE"));
52   
53   /***************************************************************/
54   GroupConstructors->setTitle(tr("GEOM_ELLIPSE"));
55   RadioButton1->setPixmap(image0);
56   RadioButton2->close(TRUE);
57   RadioButton3->close(TRUE);
58
59   GroupPoints = new DlgRef_2Sel2Spin(this, "GroupPoints");
60   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
61   GroupPoints->TextLabel1->setText(tr("GEOM_CENTER"));
62   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
63   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS_MAJOR"));
64   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_MINOR"));
65   GroupPoints->PushButton1->setPixmap(image1);
66   GroupPoints->PushButton2->setPixmap(image1);
67
68   GroupPoints->LineEdit1->setReadOnly( true );
69   GroupPoints->LineEdit2->setReadOnly( true );
70
71   Layout1->addWidget(GroupPoints, 2, 0);
72   /***************************************************************/
73
74   Init();
75 }
76
77
78 //=================================================================================
79 // function : ~BasicGUI_EllipseDlg()
80 // purpose  : Destroys the object and frees any allocated resources
81 //=================================================================================
82 BasicGUI_EllipseDlg::~BasicGUI_EllipseDlg()
83 {
84 }
85
86
87 //=================================================================================
88 // function : Init()
89 // purpose  :
90 //=================================================================================
91 void BasicGUI_EllipseDlg::Init()
92 {
93   /* init variables */
94   myEditCurrentArgument = GroupPoints->LineEdit1;
95   globalSelection( GEOM_POINT );
96
97   myPoint = myDir = GEOM::GEOM_Object::_nil();
98
99   myGeomGUI->SetState( 0 );
100
101   /* Get setting of step value from file configuration */
102   QString St = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
103   double step = St.toDouble();
104   double aMajorR( 200. ), aMinorR( 100. );
105
106   /* min, max, step and decimals for spin boxes & initial values */
107   GroupPoints->SpinBox_DX->RangeStepAndValidator( 0.001, 999.999, step, 3 );
108   GroupPoints->SpinBox_DY->RangeStepAndValidator( 0.001, 999.999, step, 3 );
109   GroupPoints->SpinBox_DX->SetValue( aMajorR );
110   GroupPoints->SpinBox_DY->SetValue( aMinorR );
111
112   /* signals and slots connections */
113   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
114   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
115   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
116
117   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
118   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
119
120   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
121   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
122
123   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
124   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
125
126   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
127   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
128
129   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
130   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
131   
132   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
133
134   initName( tr( "GEOM_ELLIPSE" ) );
135 }
136
137
138 //=================================================================================
139 // function : ClickOnOk()
140 // purpose  :
141 //=================================================================================
142 void BasicGUI_EllipseDlg::ClickOnOk()
143 {
144   if ( ClickOnApply() )
145     ClickOnCancel();
146 }
147
148
149 //=================================================================================
150 // function : ClickOnApply()
151 // purpose  :
152 //=================================================================================
153 bool BasicGUI_EllipseDlg::ClickOnApply()
154 {
155   if ( !onAccept() )
156     return false;
157
158   initName();
159
160   // reset
161   myPoint = myDir = GEOM::GEOM_Object::_nil();
162   GroupPoints->LineEdit1->setText( "" );
163   GroupPoints->LineEdit2->setText( "" );
164   myEditCurrentArgument = GroupPoints->LineEdit1;
165   globalSelection( GEOM_POINT );
166   
167   return true;
168 }
169
170 //=======================================================================
171 // function : ClickOnCancel()
172 // purpose  :
173 //=======================================================================
174 void BasicGUI_EllipseDlg::ClickOnCancel()
175 {
176   GEOMBase_Skeleton::ClickOnCancel();
177 }
178
179 //=================================================================================
180 // function : SelectionIntoArgument()
181 // purpose  : Called when selection as changed or other case
182 //=================================================================================
183 void BasicGUI_EllipseDlg::SelectionIntoArgument()
184 {
185   myEditCurrentArgument->setText("");
186
187   if ( mySelection->IObjectCount() != 1 )  
188   {
189     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint = GEOM::GEOM_Object::_nil();
190     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myDir   = GEOM::GEOM_Object::_nil();
191     return;
192   }
193
194   Standard_Boolean aRes = Standard_False;
195   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( mySelection->firstIObject(), aRes );
196   if ( !CORBA::is_nil( aSelectedObject ) && aRes )
197   {  
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   {
236     myEditCurrentArgument = send;
237     GEOMBase_Skeleton::LineEditReturnPressed();
238   }
239 }
240
241
242 //=================================================================================
243 // function : ActivateThisDialog()
244 // purpose  :
245 //=================================================================================
246 void BasicGUI_EllipseDlg::ActivateThisDialog()
247 {
248   GEOMBase_Skeleton::ActivateThisDialog();
249   connect(mySelection, 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* e)
276 {
277   if ( !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 getGeomEngine()->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->GetValue();
306   double aMinorR = GroupPoints->SpinBox_DY->GetValue();
307   if ( aMajorR < aMinorR )
308   {
309         msg = tr( "GEOM_ELLIPSE_ERROR_1" );
310         return false;
311   }
312   return !myPoint->_is_nil() && !myDir->_is_nil();
313 }
314
315 //=================================================================================
316 // function : execute
317 // purpose  :
318 //=================================================================================
319 bool BasicGUI_EllipseDlg::execute( ObjectList& objects )
320 {
321   double aMajorR = GroupPoints->SpinBox_DX->GetValue();
322   double aMinorR = GroupPoints->SpinBox_DY->GetValue();
323   GEOM::GEOM_Object_var anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeEllipse( myPoint, myDir, aMajorR, aMinorR );
324
325   if ( !anObj->_is_nil() )
326     objects.push_back( anObj._retn() );
327
328   return true;
329 }
330
331 //=================================================================================
332 // function : closeEvent
333 // purpose  :
334 //=================================================================================
335 void BasicGUI_EllipseDlg::closeEvent( QCloseEvent* e )
336 {
337   myGeomGUI->SetState( -1 );
338   GEOMBase_Skeleton::closeEvent( e );
339 }
340