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