Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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 <TColStd_IndexedMapOfInteger.hxx>
36 #include <TopTools_IndexedMapOfShape.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS.hxx>
40 #include <TopExp.hxx>
41
42 #include <qlabel.h>
43
44 #include "GEOMImpl_Types.hxx"
45
46 #include "utilities.h"
47
48 using namespace std;
49
50 //=================================================================================
51 // class    : BasicGUI_EllipseDlg()
52 // purpose  : Constructs a BasicGUI_EllipseDlg which is a child of 'parent', with the 
53 //            name 'name' and widget flags set to 'f'.
54 //            The dialog will by default be modeless, unless you set 'modal' to
55 //            TRUE to construct a modal dialog.
56 //=================================================================================
57 BasicGUI_EllipseDlg::BasicGUI_EllipseDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
58                                          const char* name, bool modal, WFlags fl)
59   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
60                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
61 {
62   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_ELLIPSE_PV")));
63   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
64
65   setCaption(tr("GEOM_ELLIPSE_TITLE"));
66
67   /***************************************************************/
68   GroupConstructors->setTitle(tr("GEOM_ELLIPSE"));
69   RadioButton1->setPixmap(image0);
70   RadioButton2->close(TRUE);
71   RadioButton3->close(TRUE);
72
73   GroupPoints = new DlgRef_2Sel2Spin(this, "GroupPoints");
74   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
75   GroupPoints->TextLabel1->setText(tr("GEOM_CENTER"));
76   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
77   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS_MAJOR"));
78   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_MINOR"));
79   GroupPoints->PushButton1->setPixmap(image1);
80   GroupPoints->PushButton2->setPixmap(image1);
81
82   GroupPoints->LineEdit1->setReadOnly( true );
83   GroupPoints->LineEdit2->setReadOnly( true );
84
85   Layout1->addWidget(GroupPoints, 2, 0);
86   /***************************************************************/
87
88   setHelpFileName("create_ellipse_page.html");
89
90   Init();
91 }
92
93
94 //=================================================================================
95 // function : ~BasicGUI_EllipseDlg()
96 // purpose  : Destroys the object and frees any allocated resources
97 //=================================================================================
98 BasicGUI_EllipseDlg::~BasicGUI_EllipseDlg()
99 {
100 }
101
102
103 //=================================================================================
104 // function : Init()
105 // purpose  :
106 //=================================================================================
107 void BasicGUI_EllipseDlg::Init()
108 {
109   /* init variables */
110   myEditCurrentArgument = GroupPoints->LineEdit1;
111   globalSelection(); // close local contexts, if any
112   localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
113
114   myPoint = myDir = GEOM::GEOM_Object::_nil();
115
116   /* Get setting of step value from file configuration */
117   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
118   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
119   
120   double aMajorR( 200. ), aMinorR( 100. );
121
122   /* min, max, step and decimals for spin boxes & initial values */
123   GroupPoints->SpinBox_DX->RangeStepAndValidator( 0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY );
124   GroupPoints->SpinBox_DY->RangeStepAndValidator( 0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY );
125   GroupPoints->SpinBox_DX->SetValue( aMajorR );
126   GroupPoints->SpinBox_DY->SetValue( aMinorR );
127
128   /* signals and slots connections */
129   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
130   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
131   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
132
133   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
134   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
135
136   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
137   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
138
139   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
140   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
141
142   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
143   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
144
145   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
146           GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
147   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
148           GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
149   
150   connect(myGeomGUI->getApp()->selectionMgr(), 
151           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
152
153   initName( tr( "GEOM_ELLIPSE" ) );
154 }
155
156 //=================================================================================
157 // function : ClickOnOk()
158 // purpose  :
159 //=================================================================================
160 void BasicGUI_EllipseDlg::ClickOnOk()
161 {
162   if ( ClickOnApply() )
163     ClickOnCancel();
164 }
165
166 //=================================================================================
167 // function : ClickOnApply()
168 // purpose  :
169 //=================================================================================
170 bool BasicGUI_EllipseDlg::ClickOnApply()
171 {
172   if ( !onAccept() )
173     return false;
174
175   initName();
176
177   // reset
178   myPoint = myDir = GEOM::GEOM_Object::_nil();
179   GroupPoints->LineEdit1->setText( "" );
180   GroupPoints->LineEdit2->setText( "" );
181   myEditCurrentArgument = GroupPoints->LineEdit1;
182   //globalSelection(GEOM_POINT);
183   globalSelection(); // close local contexts, if any
184   localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
185
186   return true;
187 }
188
189 //=======================================================================
190 // function : ClickOnCancel()
191 // purpose  :
192 //=======================================================================
193 void BasicGUI_EllipseDlg::ClickOnCancel()
194 {
195   GEOMBase_Skeleton::ClickOnCancel();
196 }
197
198 //=================================================================================
199 // function : SelectionIntoArgument()
200 // purpose  : Called when selection as changed or other case
201 //=================================================================================
202 void BasicGUI_EllipseDlg::SelectionIntoArgument()
203 {
204   myEditCurrentArgument->setText("");
205
206   if ( IObjectCount() != 1 )  
207   {
208     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint = GEOM::GEOM_Object::_nil();
209     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myDir   = GEOM::GEOM_Object::_nil();
210     return;
211   }
212
213   Standard_Boolean aRes = Standard_False;
214   Handle(SALOME_InteractiveObject) anIO = firstIObject();
215   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), aRes);
216   if (!CORBA::is_nil(aSelectedObject) && aRes)
217   {  
218     QString aName = GEOMBase::GetName(aSelectedObject);
219
220     // Get Selected object if selected subshape
221     TopoDS_Shape aShape;
222
223     if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
224     {
225       TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
226       if (myEditCurrentArgument == GroupPoints->LineEdit2)
227         aNeedType = TopAbs_EDGE;
228
229       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
230       TColStd_IndexedMapOfInteger aMap;
231       aSelMgr->GetIndexes(anIO, aMap);
232       if (aMap.Extent() == 1)
233       {
234         int anIndex = aMap(1);
235         if (aNeedType == TopAbs_EDGE)
236           aName += QString(":edge_%1").arg(anIndex);
237         else
238           aName += QString(":vertex_%1").arg(anIndex);
239
240         //Find SubShape Object in Father
241         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
242
243         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
244         GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
245         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
246         } else {
247           aSelectedObject = aFindedObject; // get Object from study
248         }
249       }
250       else // Global Selection
251       {
252         if (aShape.ShapeType() != aNeedType) {
253           aSelectedObject = GEOM::GEOM_Object::_nil();
254           aName = "";
255         }
256       }
257     }
258
259     myEditCurrentArgument->setText(aName);
260
261     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint = aSelectedObject;
262     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myDir   = aSelectedObject;
263   }
264
265   displayPreview();
266 }
267
268
269 //=================================================================================
270 // function : SetEditCurrentArgument()
271 // purpose  :
272 //=================================================================================
273 void BasicGUI_EllipseDlg::SetEditCurrentArgument()
274 {
275   QPushButton* send = (QPushButton*)sender();
276   globalSelection( GEOM_POINT );
277
278   if      ( send == GroupPoints->PushButton1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
279   else if ( send == GroupPoints->PushButton2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
280   
281   myEditCurrentArgument->setFocus();
282   globalSelection(); // close local contexts, if any
283   if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
284     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
285   else
286     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
287   SelectionIntoArgument();
288 }
289
290 //=================================================================================
291 // function : LineEditReturnPressed()
292 // purpose  :
293 //=================================================================================
294 void BasicGUI_EllipseDlg::LineEditReturnPressed()
295 {
296   QLineEdit* send = (QLineEdit*)sender();
297   if ( send == GroupPoints->LineEdit1 ||
298        send == GroupPoints->LineEdit2 )
299   {
300     myEditCurrentArgument = send;
301     GEOMBase_Skeleton::LineEditReturnPressed();
302   }
303 }
304
305
306 //=================================================================================
307 // function : ActivateThisDialog()
308 // purpose  :
309 //=================================================================================
310 void BasicGUI_EllipseDlg::ActivateThisDialog()
311 {
312   GEOMBase_Skeleton::ActivateThisDialog();
313   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
314           this, SLOT(SelectionIntoArgument()));
315
316   GroupPoints->LineEdit1->setFocus();
317   myEditCurrentArgument = GroupPoints->LineEdit1;
318
319   GroupPoints->LineEdit1->setText( "" );
320   GroupPoints->LineEdit2->setText( "" );
321
322   myPoint = myDir = GEOM::GEOM_Object::_nil();
323   //globalSelection( GEOM_POINT );
324   globalSelection(); // close local contexts, if any
325   localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
326 }
327
328 //=================================================================================
329 // function : DeactivateActiveDialog()
330 // purpose  : public slot to deactivate if active
331 //=================================================================================
332 void BasicGUI_EllipseDlg::DeactivateActiveDialog()
333 {
334   // myGeomGUI->SetState( -1 );
335   GEOMBase_Skeleton::DeactivateActiveDialog();
336 }
337
338 //=================================================================================
339 // function : enterEvent()
340 // purpose  :
341 //=================================================================================
342 void BasicGUI_EllipseDlg::enterEvent(QEvent* e)
343 {
344   if ( !GroupConstructors->isEnabled() )
345     ActivateThisDialog();
346 }
347
348 //=================================================================================
349 // function : ValueChangedInSpinBox()
350 // purpose  :
351 //=================================================================================
352 void BasicGUI_EllipseDlg::ValueChangedInSpinBox(double newValue)
353 {
354   displayPreview();
355 }
356
357 //=================================================================================
358 // function : createOperation
359 // purpose  :
360 //=================================================================================
361 GEOM::GEOM_IOperations_ptr BasicGUI_EllipseDlg::createOperation()
362 {
363   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
364 }
365
366 //=================================================================================
367 // function : isValid
368 // purpose  :
369 //=================================================================================
370 bool BasicGUI_EllipseDlg::isValid( QString& msg )
371 {
372   double aMajorR = GroupPoints->SpinBox_DX->GetValue();
373   double aMinorR = GroupPoints->SpinBox_DY->GetValue();
374   if ( aMajorR < aMinorR )
375   {
376         msg = tr( "GEOM_ELLIPSE_ERROR_1" );
377         return false;
378   }
379   return !myPoint->_is_nil() && !myDir->_is_nil();
380 }
381
382 //=================================================================================
383 // function : execute
384 // purpose  :
385 //=================================================================================
386 bool BasicGUI_EllipseDlg::execute( ObjectList& objects )
387 {
388   double aMajorR = GroupPoints->SpinBox_DX->GetValue();
389   double aMinorR = GroupPoints->SpinBox_DY->GetValue();
390   GEOM::GEOM_Object_var anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeEllipse( myPoint, myDir, aMajorR, aMinorR );
391
392   if ( !anObj->_is_nil() )
393     objects.push_back( anObj._retn() );
394
395   return true;
396 }
397
398 //=================================================================================
399 // function : closeEvent
400 // purpose  :
401 //=================================================================================
402 void BasicGUI_EllipseDlg::closeEvent( QCloseEvent* e )
403 {
404   GEOMBase_Skeleton::closeEvent( e );
405 }
406
407 //=================================================================================
408 // function : addSubshapeToStudy
409 // purpose  : virtual method to add new SubObjects if local selection
410 //=================================================================================
411 void BasicGUI_EllipseDlg::addSubshapesToStudy()
412 {
413   QMap<QString, GEOM::GEOM_Object_var> objMap;
414
415   objMap[GroupPoints->LineEdit1->text()] = myPoint;
416   objMap[GroupPoints->LineEdit2->text()] = myDir;
417
418   addSubshapesToFather( objMap );
419 }