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