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