Salome HOME
Fix for bug 0019870(Import/Export should show current directory).
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_CylinderDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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 //
23 //
24 //  File   : PrimitiveGUI_CylinderDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "PrimitiveGUI_CylinderDlg.h"
30
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "LightApp_SelectionMgr.h"
35
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS.hxx>
39 #include <TopExp.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42
43 #include <qlabel.h>
44
45 #include "GEOMImpl_Types.hxx"
46
47 #include "utilities.h"
48
49 using namespace std;
50
51 //=================================================================================
52 // class    : PrimitiveGUI_CylinderDlg()
53 // purpose  : Constructs a PrimitiveGUI_CylinderDlg which is a child of 'parent', with the 
54 //            name 'name' and widget flags set to 'f'.
55 //            The dialog will by default be modeless, unless you set 'modal' to
56 //            TRUE to construct a modal dialog.
57 //=================================================================================
58 PrimitiveGUI_CylinderDlg::PrimitiveGUI_CylinderDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
59                                                    const char* name, bool modal, WFlags fl)
60   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
61                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
62 {
63   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_CYLINDER_PV")));
64   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_CYLINDER_DXYZ")));
65   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
66
67   setCaption(tr("GEOM_CYLINDER_TITLE"));
68
69   /***************************************************************/
70   GroupConstructors->setTitle(tr("GEOM_CYLINDER"));
71   RadioButton1->setPixmap(image0);
72   RadioButton2->setPixmap(image1);
73   RadioButton3->close(TRUE);
74
75   GroupPoints = new DlgRef_2Sel2Spin(this, "GroupPoints");
76   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
77   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_POINT"));
78   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
79   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS"));
80   GroupPoints->TextLabel4->setText(tr("GEOM_HEIGHT"));
81   GroupPoints->PushButton1->setPixmap(image2);
82   GroupPoints->PushButton2->setPixmap(image2);
83
84   GroupDimensions = new DlgRef_2Spin(this, "GroupDimensions");
85   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
86   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS"));
87   GroupDimensions->TextLabel2->setText(tr("GEOM_HEIGHT"));
88
89   Layout1->addWidget(GroupPoints, 2, 0);
90   Layout1->addWidget(GroupDimensions, 2, 0);
91   /***************************************************************/
92
93   setHelpFileName("create_cylinder_page.html");
94
95   Init();
96 }
97
98
99 //=================================================================================
100 // function : ~PrimitiveGUI_CylinderDlg()
101 // purpose  : Destroys the object and frees any allocated resources
102 //=================================================================================
103 PrimitiveGUI_CylinderDlg::~PrimitiveGUI_CylinderDlg()
104 {
105     // no need to delete child widgets, Qt does it all for us
106 }
107
108
109 //=================================================================================
110 // function : Init()
111 // purpose  :
112 //=================================================================================
113 void PrimitiveGUI_CylinderDlg::Init()
114 {
115   /* init variables */
116   myEditCurrentArgument = GroupPoints->LineEdit1;
117   GroupPoints->LineEdit1->setReadOnly( true );
118   GroupPoints->LineEdit2->setReadOnly( true );
119
120   myPoint = myDir = GEOM::GEOM_Object::_nil();
121   
122   /* Get setting of step value from file configuration */
123   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
124   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
125
126   /* min, max, step and decimals for spin boxes & initial values */
127   /* First constructor : radius */
128   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
129   /* First constructor : algebric height */
130   GroupPoints->SpinBox_DY->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY);
131   /* Second constructor : radius */
132   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
133   /* Second constructor : algebric height */
134   GroupDimensions->SpinBox_DY->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY);
135
136   GroupPoints->SpinBox_DX->SetValue(100.0);
137   GroupPoints->SpinBox_DY->SetValue(300.0);
138   GroupDimensions->SpinBox_DX->SetValue(100.0);  
139   GroupDimensions->SpinBox_DY->SetValue(300.0);  
140
141   /* signals and slots connections */
142   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
143   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
144   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
145
146   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
147   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
148
149   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
150   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
151
152   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
153   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
154   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
155   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
156
157   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
158   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
159   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
160   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DY, SLOT(SetStep(double)));
161   
162   connect(myGeomGUI->getApp()->selectionMgr(), 
163           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
164   
165   initName( tr( "GEOM_CYLINDER" ) );
166   ConstructorsClicked(0);
167 }
168
169
170 //=================================================================================
171 // function : ConstructorsClicked()
172 // purpose  : Radio button management
173 //=================================================================================
174 void PrimitiveGUI_CylinderDlg::ConstructorsClicked(int constructorId)
175 {
176   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
177     
178   switch (constructorId)
179     { 
180     case 0 :
181       {
182         globalSelection( GEOM_POINT ); // to break previous local selection
183         localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
184
185         GroupDimensions->hide();
186         resize(0, 0);
187         GroupPoints->show();
188         
189         myEditCurrentArgument = GroupPoints->LineEdit1;
190         GroupPoints->LineEdit1->setText(tr(""));
191         GroupPoints->LineEdit2->setText(tr(""));
192         myPoint = myDir = GEOM::GEOM_Object::_nil();
193
194         connect(myGeomGUI->getApp()->selectionMgr(), 
195                 SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
196         break;
197       }
198     case 1 :
199       { 
200         GroupPoints->hide();
201         globalSelection(); // close local contexts, if any
202         resize( 0, 0 );
203         GroupDimensions->show();
204         
205         break;
206       }
207     }
208   displayPreview();
209 }
210
211
212 //=================================================================================
213 // function : ClickOnOk()
214 // purpose  :
215 //=================================================================================
216 void PrimitiveGUI_CylinderDlg::ClickOnOk()
217 {
218   if ( ClickOnApply() )
219     ClickOnCancel();
220 }
221
222
223 //=================================================================================
224 // function : ClickOnApply()
225 // purpose  :
226 //=================================================================================
227 bool PrimitiveGUI_CylinderDlg::ClickOnApply()
228 {
229   if ( !onAccept() )
230     return false;
231
232   initName();
233   ConstructorsClicked( getConstructorId() );
234   return true;
235 }
236
237
238 //=======================================================================
239 // function : ClickOnCancel()
240 // purpose  :
241 //=======================================================================
242 void PrimitiveGUI_CylinderDlg::ClickOnCancel()
243 {
244   GEOMBase_Skeleton::ClickOnCancel();
245 }
246
247
248 //=================================================================================
249 // function : SelectionIntoArgument()
250 // purpose  : Called when selection as changed or other case
251 //=================================================================================
252 void PrimitiveGUI_CylinderDlg::SelectionIntoArgument()
253 {
254   if (getConstructorId() != 0)
255     return;
256
257   myEditCurrentArgument->setText("");
258
259   if (IObjectCount() != 1) 
260   {
261     if (myEditCurrentArgument == GroupPoints->LineEdit1)
262       myPoint = GEOM::GEOM_Object::_nil();
263     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
264       myDir = GEOM::GEOM_Object::_nil();
265     return;
266   }
267
268   /* nbSel == 1 */
269   Standard_Boolean testResult = Standard_False;
270   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult);
271
272   if (!testResult || CORBA::is_nil(aSelectedObject))
273     return;
274
275   QString aName = GEOMBase::GetName(aSelectedObject);
276   TopoDS_Shape aShape;
277   if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
278   {
279     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
280     if (myEditCurrentArgument == GroupPoints->LineEdit2)
281       aNeedType = TopAbs_EDGE;
282
283     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
284     TColStd_IndexedMapOfInteger aMap;
285     aSelMgr->GetIndexes(firstIObject(), aMap);
286     if (aMap.Extent() == 1) // Local Selection
287     {
288       int anIndex = aMap(1);
289       if (aNeedType == TopAbs_EDGE)
290         aName.append(":edge_" + QString::number(anIndex));
291       else
292         aName.append(":vertex_" + QString::number(anIndex));
293
294       //Find SubShape Object in Father
295       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
296
297       if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
298         GEOM::GEOM_IShapesOperations_var aShapesOp =
299           getGeomEngine()->GetIShapesOperations(getStudyId());
300         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
301       }
302       else
303         aSelectedObject = aFindedObject; // get Object from study
304     }
305     else // Global Selection
306     {
307       if (aShape.ShapeType() != aNeedType) {
308         aSelectedObject = GEOM::GEOM_Object::_nil();
309         aName = "";
310       }
311     }
312   }
313
314   myEditCurrentArgument->setText(aName);
315
316   if (myEditCurrentArgument == GroupPoints->LineEdit1)
317     myPoint = aSelectedObject;
318   else if (myEditCurrentArgument == GroupPoints->LineEdit2)
319     myDir = aSelectedObject;
320   displayPreview();
321 }
322
323 //=================================================================================
324 // function : SetEditCurrentArgument()
325 // purpose  :
326 //=================================================================================
327 void PrimitiveGUI_CylinderDlg::SetEditCurrentArgument()
328 {
329   QPushButton* send = (QPushButton*)sender();
330   
331   if (send == GroupPoints->PushButton1) {
332     myEditCurrentArgument = GroupPoints->LineEdit1;
333     globalSelection( GEOM_POINT ); // to break previous local selection
334     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
335   }
336   else if (send == GroupPoints->PushButton2) {
337     myEditCurrentArgument = GroupPoints->LineEdit2;
338     globalSelection( GEOM_LINE );  // to break previous local selection
339     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
340   }
341   
342   myEditCurrentArgument->setFocus();
343   SelectionIntoArgument();
344 }
345
346
347 //=================================================================================
348 // function : LineEditReturnPressed()
349 // purpose  :
350 //=================================================================================
351 void PrimitiveGUI_CylinderDlg::LineEditReturnPressed()
352 {  
353   QLineEdit* send = (QLineEdit*)sender();
354   if(send == GroupPoints->LineEdit1 ||
355      send == GroupPoints->LineEdit2)
356     {
357       myEditCurrentArgument = send;
358       GEOMBase_Skeleton::LineEditReturnPressed();
359     }
360 }
361
362 //=================================================================================
363 // function : ActivateThisDialog()
364 // purpose  :
365 //=================================================================================
366 void PrimitiveGUI_CylinderDlg::ActivateThisDialog()
367 {
368   GEOMBase_Skeleton::ActivateThisDialog();
369   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
370           this, SLOT(SelectionIntoArgument()));
371
372   ConstructorsClicked(getConstructorId());
373 }
374
375 //=================================================================================
376 // function : enterEvent()
377 // purpose  :
378 //=================================================================================
379 void PrimitiveGUI_CylinderDlg::enterEvent(QEvent* e)
380 {
381   if ( !GroupConstructors->isEnabled() )
382     ActivateThisDialog();
383 }
384
385
386 //=================================================================================
387 // function : DeactivateActiveDialog()
388 // purpose  : public slot to deactivate if active
389 //=================================================================================
390 void PrimitiveGUI_CylinderDlg::DeactivateActiveDialog()
391 {
392   GEOMBase_Skeleton::DeactivateActiveDialog();
393 }
394
395
396 //=================================================================================
397 // function : ValueChangedInSpinBox
398 // purpose  :
399 //=================================================================================
400 void PrimitiveGUI_CylinderDlg::ValueChangedInSpinBox( )
401 {  
402   displayPreview();
403 }
404
405
406 //=================================================================================
407 // function : createOperation
408 // purpose  :
409 //=================================================================================
410 GEOM::GEOM_IOperations_ptr PrimitiveGUI_CylinderDlg::createOperation()
411 {
412   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
413 }
414
415
416 //=================================================================================
417 // function : isValid
418 // purpose  :
419 //=================================================================================
420 bool PrimitiveGUI_CylinderDlg::isValid( QString& msg )
421 {
422   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil() ) : true;
423 }
424
425 //=================================================================================
426 // function : execute
427 // purpose  :
428 //=================================================================================
429 bool  PrimitiveGUI_CylinderDlg::execute( ObjectList& objects )
430 {
431   bool res = false;
432   
433   GEOM::GEOM_Object_var anObj;
434   
435   switch ( getConstructorId() ) 
436     {
437     case 0 :
438       {
439         if ( !CORBA::is_nil( myPoint ) && !CORBA::is_nil( myDir )) {
440           anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeCylinderPntVecRH(myPoint, myDir, getRadius(), getHeight());
441           res = true;
442         }
443         break;
444       }
445     case 1 :
446       {
447         anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeCylinderRH(getRadius(), getHeight());
448         res = true;
449
450         break;
451       }
452     }
453   
454   if ( !anObj->_is_nil() )
455     objects.push_back( anObj._retn() );
456   
457   return res;
458 }
459
460 //=================================================================================
461 // function : closeEvent
462 // purpose  :
463 //=================================================================================
464 void  PrimitiveGUI_CylinderDlg::closeEvent( QCloseEvent* e )
465 {
466   GEOMBase_Skeleton::closeEvent( e );
467 }
468
469
470 //=================================================================================
471 // function : getRadius()
472 // purpose  :
473 //=================================================================================
474 double PrimitiveGUI_CylinderDlg::getRadius() const
475 {
476   int aConstructorId = getConstructorId();
477   if (aConstructorId == 0)
478     return GroupPoints->SpinBox_DX->GetValue();
479   else if (aConstructorId == 1)
480     return GroupDimensions->SpinBox_DX->GetValue();
481   return 0;
482 }
483
484 //=================================================================================
485 // function : getHeight()
486 // purpose  :
487 //=================================================================================
488 double PrimitiveGUI_CylinderDlg::getHeight() const
489 {
490   int aConstructorId = getConstructorId();
491   if (aConstructorId == 0)
492     return GroupPoints->SpinBox_DY->GetValue();
493   else if (aConstructorId == 1)
494     return GroupDimensions->SpinBox_DY->GetValue();
495   return 0;
496 }
497
498 //=================================================================================
499 // function : addSubshapeToStudy
500 // purpose  : virtual method to add new SubObjects if local selection
501 //=================================================================================
502 void PrimitiveGUI_CylinderDlg::addSubshapesToStudy()
503 {
504   QMap<QString, GEOM::GEOM_Object_var> objMap;
505
506 switch (getConstructorId())
507   {
508   case 0:
509     objMap[GroupPoints->LineEdit1->text()] = myPoint;
510     objMap[GroupPoints->LineEdit2->text()] = myDir;
511     break;
512   case 1:
513     return;
514   }
515  addSubshapesToFather( objMap );
516 }