]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_EllipseDlg.cxx
Salome HOME
Bug 0020374: Partition operation leads to missing volume. A fix by PKV.
[modules/geom.git] / src / BasicGUI / BasicGUI_EllipseDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  GEOM GEOMGUI : GUI for Geometry component
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_3Sel2Spin(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("%1 (%2)").arg(tr("GEOM_VECTOR_MAJOR")).arg(tr("GEOM_OPTIONAL")));
78   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_MAJOR"));
79   GroupPoints->TextLabel5->setText(tr("GEOM_RADIUS_MINOR"));
80   GroupPoints->PushButton1->setPixmap(image1);
81   GroupPoints->PushButton2->setPixmap(image1);
82   GroupPoints->PushButton3->setPixmap(image1);
83
84   GroupPoints->LineEdit1->setReadOnly( true );
85   GroupPoints->LineEdit2->setReadOnly( true );
86   GroupPoints->LineEdit3->setReadOnly( true );
87
88   Layout1->addWidget(GroupPoints, 2, 0);
89   /***************************************************************/
90
91   setHelpFileName("create_ellipse_page.html");
92
93   Init();
94 }
95
96
97 //=================================================================================
98 // function : ~BasicGUI_EllipseDlg()
99 // purpose  : Destroys the object and frees any allocated resources
100 //=================================================================================
101 BasicGUI_EllipseDlg::~BasicGUI_EllipseDlg()
102 {
103 }
104
105
106 //=================================================================================
107 // function : Init()
108 // purpose  :
109 //=================================================================================
110 void BasicGUI_EllipseDlg::Init()
111 {
112   /* init variables */
113   myEditCurrentArgument = GroupPoints->LineEdit1;
114   globalSelection(); // close local contexts, if any
115   localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
116
117   myPoint = myDir = myMajor = GEOM::GEOM_Object::_nil();
118
119   /* Get setting of step value from file configuration */
120   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
121   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
122   
123   double aMajorR( 200. ), aMinorR( 100. );
124
125   /* min, max, step and decimals for spin boxes & initial values */
126   GroupPoints->SpinBox_DX->RangeStepAndValidator( 0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY );
127   GroupPoints->SpinBox_DY->RangeStepAndValidator( 0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY );
128   GroupPoints->SpinBox_DX->SetValue( aMajorR );
129   GroupPoints->SpinBox_DY->SetValue( aMinorR );
130
131   /* signals and slots connections */
132   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
133   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
134   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
135
136   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
137   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
138
139   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
140   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
141   connect(GroupPoints->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
142
143   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
144   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
145   connect(GroupPoints->LineEdit3, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
146
147   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
148   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
149
150   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
151           GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
152   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
153           GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
154   
155   connect(myGeomGUI->getApp()->selectionMgr(), 
156           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
157
158   initName( tr( "GEOM_ELLIPSE" ) );
159 }
160
161 //=================================================================================
162 // function : ClickOnOk()
163 // purpose  :
164 //=================================================================================
165 void BasicGUI_EllipseDlg::ClickOnOk()
166 {
167   if ( ClickOnApply() )
168     ClickOnCancel();
169 }
170
171 //=================================================================================
172 // function : ClickOnApply()
173 // purpose  :
174 //=================================================================================
175 bool BasicGUI_EllipseDlg::ClickOnApply()
176 {
177   if ( !onAccept() )
178     return false;
179
180   initName();
181
182   // reset
183   myPoint = myDir = myMajor = GEOM::GEOM_Object::_nil();
184   GroupPoints->LineEdit1->setText( "" );
185   GroupPoints->LineEdit2->setText( "" );
186   GroupPoints->LineEdit3->setText( "" );
187   myEditCurrentArgument = GroupPoints->LineEdit1;
188   //globalSelection(GEOM_POINT);
189   globalSelection(); // close local contexts, if any
190   localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
191
192   return true;
193 }
194
195 //=======================================================================
196 // function : ClickOnCancel()
197 // purpose  :
198 //=======================================================================
199 void BasicGUI_EllipseDlg::ClickOnCancel()
200 {
201   GEOMBase_Skeleton::ClickOnCancel();
202 }
203
204 //=================================================================================
205 // function : SelectionIntoArgument()
206 // purpose  : Called when selection as changed or other case
207 //=================================================================================
208 void BasicGUI_EllipseDlg::SelectionIntoArgument()
209 {
210   myEditCurrentArgument->setText("");
211
212   if ( IObjectCount() != 1 )  
213   {
214     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint = GEOM::GEOM_Object::_nil();
215     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myDir   = GEOM::GEOM_Object::_nil();
216     else if ( myEditCurrentArgument == GroupPoints->LineEdit3 ) myMajor = GEOM::GEOM_Object::_nil();
217     return;
218   }
219
220   Standard_Boolean aRes = Standard_False;
221   Handle(SALOME_InteractiveObject) anIO = firstIObject();
222   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), aRes);
223   if (!CORBA::is_nil(aSelectedObject) && aRes)
224   {  
225     QString aName = GEOMBase::GetName(aSelectedObject);
226
227     // Get Selected object if selected subshape
228     TopoDS_Shape aShape;
229
230     if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
231     {
232       TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
233       if (myEditCurrentArgument == GroupPoints->LineEdit2 || myEditCurrentArgument == GroupPoints->LineEdit3)
234         aNeedType = TopAbs_EDGE;
235
236       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
237       TColStd_IndexedMapOfInteger aMap;
238       aSelMgr->GetIndexes(anIO, aMap);
239       if (aMap.Extent() == 1)
240       {
241         int anIndex = aMap(1);
242         if (aNeedType == TopAbs_EDGE)
243           aName += QString(":edge_%1").arg(anIndex);
244         else
245           aName += QString(":vertex_%1").arg(anIndex);
246
247         //Find SubShape Object in Father
248         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
249
250         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
251         GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
252         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
253         } else {
254           aSelectedObject = aFindedObject; // get Object from study
255         }
256       }
257       else // Global Selection
258       {
259         if (aShape.ShapeType() != aNeedType) {
260           aSelectedObject = GEOM::GEOM_Object::_nil();
261           aName = "";
262         }
263       }
264     }
265
266     myEditCurrentArgument->setText(aName);
267
268     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint = aSelectedObject;
269     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myDir   = aSelectedObject;
270     else if ( myEditCurrentArgument == GroupPoints->LineEdit3 ) myMajor = aSelectedObject;
271   }
272
273   displayPreview();
274 }
275
276
277 //=================================================================================
278 // function : SetEditCurrentArgument()
279 // purpose  :
280 //=================================================================================
281 void BasicGUI_EllipseDlg::SetEditCurrentArgument()
282 {
283   QPushButton* send = (QPushButton*)sender();
284   globalSelection( GEOM_POINT );
285
286   if      ( send == GroupPoints->PushButton1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
287   else if ( send == GroupPoints->PushButton2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
288   else if ( send == GroupPoints->PushButton3 ) myEditCurrentArgument = GroupPoints->LineEdit3;
289   
290   myEditCurrentArgument->setFocus();
291   globalSelection(); // close local contexts, if any
292   if ( myEditCurrentArgument == GroupPoints->LineEdit2 || myEditCurrentArgument == GroupPoints->LineEdit3 )
293     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
294   else
295     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
296   SelectionIntoArgument();
297 }
298
299 //=================================================================================
300 // function : LineEditReturnPressed()
301 // purpose  :
302 //=================================================================================
303 void BasicGUI_EllipseDlg::LineEditReturnPressed()
304 {
305   QLineEdit* send = (QLineEdit*)sender();
306   if ( send == GroupPoints->LineEdit1 ||
307        send == GroupPoints->LineEdit2 ||
308        send == GroupPoints->LineEdit3 )
309   {
310     myEditCurrentArgument = send;
311     GEOMBase_Skeleton::LineEditReturnPressed();
312   }
313 }
314
315
316 //=================================================================================
317 // function : ActivateThisDialog()
318 // purpose  :
319 //=================================================================================
320 void BasicGUI_EllipseDlg::ActivateThisDialog()
321 {
322   GEOMBase_Skeleton::ActivateThisDialog();
323   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
324           this, SLOT(SelectionIntoArgument()));
325
326   GroupPoints->LineEdit1->setFocus();
327   myEditCurrentArgument = GroupPoints->LineEdit1;
328
329   GroupPoints->LineEdit1->setText( "" );
330   GroupPoints->LineEdit2->setText( "" );
331   GroupPoints->LineEdit3->setText( "" );
332
333   myPoint = myDir = GEOM::GEOM_Object::_nil();
334   //globalSelection( GEOM_POINT );
335   globalSelection(); // close local contexts, if any
336   localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
337 }
338
339 //=================================================================================
340 // function : DeactivateActiveDialog()
341 // purpose  : public slot to deactivate if active
342 //=================================================================================
343 void BasicGUI_EllipseDlg::DeactivateActiveDialog()
344 {
345   // myGeomGUI->SetState( -1 );
346   GEOMBase_Skeleton::DeactivateActiveDialog();
347 }
348
349 //=================================================================================
350 // function : enterEvent()
351 // purpose  :
352 //=================================================================================
353 void BasicGUI_EllipseDlg::enterEvent(QEvent* e)
354 {
355   if ( !GroupConstructors->isEnabled() )
356     ActivateThisDialog();
357 }
358
359 //=================================================================================
360 // function : ValueChangedInSpinBox()
361 // purpose  :
362 //=================================================================================
363 void BasicGUI_EllipseDlg::ValueChangedInSpinBox(double newValue)
364 {
365   displayPreview();
366 }
367
368 //=================================================================================
369 // function : createOperation
370 // purpose  :
371 //=================================================================================
372 GEOM::GEOM_IOperations_ptr BasicGUI_EllipseDlg::createOperation()
373 {
374   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
375 }
376
377 //=================================================================================
378 // function : isValid
379 // purpose  :
380 //=================================================================================
381 bool BasicGUI_EllipseDlg::isValid( QString& msg )
382 {
383   double aMajorR = GroupPoints->SpinBox_DX->GetValue();
384   double aMinorR = GroupPoints->SpinBox_DY->GetValue();
385   if ( aMajorR < aMinorR )
386   {
387         msg = tr( "GEOM_ELLIPSE_ERROR_1" );
388         return false;
389   }
390   return !myPoint->_is_nil() && !myDir->_is_nil();
391 }
392
393 //=================================================================================
394 // function : execute
395 // purpose  :
396 //=================================================================================
397 bool BasicGUI_EllipseDlg::execute( ObjectList& objects )
398 {
399   double aMajorR = GroupPoints->SpinBox_DX->GetValue();
400   double aMinorR = GroupPoints->SpinBox_DY->GetValue();
401
402   GEOM::GEOM_Object_var anObj = myMajor->_is_nil() ? 
403     GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeEllipse   ( myPoint, myDir, aMajorR, aMinorR )         :
404     GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeEllipseVec( myPoint, myDir, aMajorR, aMinorR, myMajor );
405
406   if ( !anObj->_is_nil() )
407     objects.push_back( anObj._retn() );
408
409   return true;
410 }
411
412 //=================================================================================
413 // function : closeEvent
414 // purpose  :
415 //=================================================================================
416 void BasicGUI_EllipseDlg::closeEvent( QCloseEvent* e )
417 {
418   GEOMBase_Skeleton::closeEvent( e );
419 }
420
421 //=================================================================================
422 // function : addSubshapeToStudy
423 // purpose  : virtual method to add new SubObjects if local selection
424 //=================================================================================
425 void BasicGUI_EllipseDlg::addSubshapesToStudy()
426 {
427   QMap<QString, GEOM::GEOM_Object_var> objMap;
428
429   objMap[GroupPoints->LineEdit1->text()] = myPoint;
430   objMap[GroupPoints->LineEdit2->text()] = myDir;
431   if (!CORBA::is_nil(myMajor))
432     objMap[GroupPoints->LineEdit3->text()] = myMajor;
433
434   addSubshapesToFather( objMap );
435 }