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