Salome HOME
NPAL18363: EDF635: We can now select any object in all window.
[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( "cone.htm" );
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(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->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(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0);
184   
185   switch(constructorId)
186     { 
187     case 0 :
188       {
189         //      globalSelection( GEOM_POINT );
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(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
201                 SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
202         break;
203       }
204     case 1 :
205       { 
206         GroupPoints->hide();
207         resize( 0, 0 );
208         GroupDimensions->show();
209
210         break;
211       }
212     }
213   displayPreview();
214 }
215
216
217 //=================================================================================
218 // function : ClickOnOk()
219 // purpose  :
220 //=================================================================================
221 void PrimitiveGUI_ConeDlg::ClickOnOk()
222 {
223   if ( ClickOnApply() )
224     ClickOnCancel();
225 }
226
227
228 //=================================================================================
229 // function : ClickOnApply()
230 // purpose  :
231 //=================================================================================
232 bool PrimitiveGUI_ConeDlg::ClickOnApply()
233 {
234   if ( !onAccept() )
235     return false;
236
237   initName();
238   ConstructorsClicked( getConstructorId() );
239   return true;
240 }
241
242
243 //=======================================================================
244 // function : ClickOnCancel()
245 // purpose  :
246 //=======================================================================
247 void PrimitiveGUI_ConeDlg::ClickOnCancel()
248 {
249   GEOMBase_Skeleton::ClickOnCancel();
250 }
251
252
253 //=================================================================================
254 // function : SelectionIntoArgument()
255 // purpose  : Called when selection as changed or other case
256 //=================================================================================
257 void PrimitiveGUI_ConeDlg::SelectionIntoArgument()
258 {
259   if ( getConstructorId() != 0 )
260     return;
261
262   if(IObjectCount() != 1)
263     {
264       if(myEditCurrentArgument == GroupPoints->LineEdit1)
265         myPoint = GEOM::GEOM_Object::_nil();
266       else if (myEditCurrentArgument == GroupPoints->LineEdit2)
267         myDir = GEOM::GEOM_Object::_nil();
268       return;
269     }
270
271   /* nbSel == 1 */
272   Standard_Boolean testResult = Standard_False;
273   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
274   
275   if(!testResult || CORBA::is_nil( aSelectedObject ))
276     return;
277
278   TopoDS_Shape aShape;
279   myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
280   if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
281     {
282       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
283       TColStd_IndexedMapOfInteger aMap;
284       aSelMgr->GetIndexes( firstIObject(), aMap );
285       if ( aMap.Extent() == 1 )
286         {
287           GEOM::GEOM_IShapesOperations_var aShapesOp =
288             getGeomEngine()->GetIShapesOperations( getStudyId() );
289           int anIndex = aMap( 1 );
290           TopTools_IndexedMapOfShape aShapes;
291           TopExp::MapShapes( aShape, aShapes );
292           aShape = aShapes.FindKey( anIndex );
293           aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
294           aSelMgr->clearSelected();
295         }
296     }
297   if (myEditCurrentArgument == GroupPoints->LineEdit1)
298     myPoint = aSelectedObject;
299   else if (myEditCurrentArgument == GroupPoints->LineEdit2) 
300     myDir = aSelectedObject;
301  
302   displayPreview();
303 }
304
305
306 //=================================================================================
307 // function : SetEditCurrentArgument()
308 // purpose  :
309 //=================================================================================
310 void PrimitiveGUI_ConeDlg::SetEditCurrentArgument()
311 {
312   QPushButton* send = (QPushButton*)sender();
313   
314   if(send == GroupPoints->PushButton1) {
315     myEditCurrentArgument = GroupPoints->LineEdit1;
316     //    globalSelection( GEOM_POINT );
317     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
318   }
319   else if(send == GroupPoints->PushButton2) {
320     myEditCurrentArgument = GroupPoints->LineEdit2;
321     //    globalSelection( GEOM_LINE );
322     GEOM::GEOM_Object_var anObj;
323     localSelection( anObj, TopAbs_EDGE );
324   }
325   
326   myEditCurrentArgument->setFocus();
327   SelectionIntoArgument();
328 }
329
330
331 //=================================================================================
332 // function : LineEditReturnPressed()
333 // purpose  :
334 //=================================================================================
335 void PrimitiveGUI_ConeDlg::LineEditReturnPressed()
336 {
337   QLineEdit* send = (QLineEdit*)sender();
338   if(send == GroupPoints->LineEdit1 ||
339      send == GroupPoints->LineEdit2)
340     {
341       myEditCurrentArgument = send;
342       GEOMBase_Skeleton::LineEditReturnPressed();
343     }
344 }
345
346
347 //=================================================================================
348 // function : ActivateThisDialog()
349 // purpose  :
350 //=================================================================================
351 void PrimitiveGUI_ConeDlg::ActivateThisDialog()
352 {
353   GEOMBase_Skeleton::ActivateThisDialog();
354   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
355           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
356   
357   ConstructorsClicked( getConstructorId() );
358 }
359
360
361 //=================================================================================
362 // function : DeactivateActiveDialog()
363 // purpose  : public slot to deactivate if active
364 //=================================================================================
365 void PrimitiveGUI_ConeDlg::DeactivateActiveDialog()
366 {
367   GEOMBase_Skeleton::DeactivateActiveDialog();
368 }
369
370
371 //=================================================================================
372 // function : enterEvent()
373 // purpose  :
374 //=================================================================================
375 void PrimitiveGUI_ConeDlg::enterEvent(QEvent* e)
376 {
377   if ( !GroupConstructors->isEnabled() )
378     ActivateThisDialog();
379 }
380
381
382 //=================================================================================
383 // function : ValueChangedInSpinBox()
384 // purpose  :
385 //=================================================================================
386 void PrimitiveGUI_ConeDlg::ValueChangedInSpinBox()
387 {
388   displayPreview();
389 }
390
391
392 //=================================================================================
393 // function : createOperation
394 // purpose  :
395 //=================================================================================
396 GEOM::GEOM_IOperations_ptr  PrimitiveGUI_ConeDlg::createOperation()
397 {
398   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
399 }
400
401 //=================================================================================
402 // function : isValid
403 // purpose  :
404 //=================================================================================
405 bool  PrimitiveGUI_ConeDlg::isValid( QString& msg )
406 {
407   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil()) : true;
408 }
409
410 //=================================================================================
411 // function : execute
412 // purpose  :
413 //=================================================================================
414 bool PrimitiveGUI_ConeDlg::execute( ObjectList& objects )
415 {
416   bool res = false;
417   
418   GEOM::GEOM_Object_var anObj;
419
420   switch ( getConstructorId() ) 
421   {
422   case 0 :
423   {
424     if ( !CORBA::is_nil( myPoint ) && !CORBA::is_nil( myDir ) ){
425       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeConePntVecR1R2H( myPoint, 
426                                                                                             myDir, 
427                                                                                             getRadius1(),
428                                                                                             getRadius2(),
429                                                                                             getHeight());
430       res = true;
431     }
432     break;
433   }
434   case 1 :
435     {
436       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeConeR1R2H( getRadius1(),
437                                                                                       getRadius2(),
438                                                                                       getHeight());
439       res = true;
440       break;
441     }
442   }
443
444   if ( !anObj->_is_nil() )
445     objects.push_back( anObj._retn() );
446
447   return res;
448 }
449
450 //=================================================================================
451 // function : closeEvent
452 // purpose  :
453 //=================================================================================
454 void PrimitiveGUI_ConeDlg::closeEvent( QCloseEvent* e )
455 {
456   // myGeomGUI->SetState( -1 );
457   GEOMBase_Skeleton::closeEvent( e );
458 }
459
460
461 //=================================================================================
462 // function : getRadius1()
463 // purpose  :
464 //=================================================================================
465 double PrimitiveGUI_ConeDlg::getRadius1() const
466 {
467   int aConstructorId = getConstructorId();
468   if (aConstructorId == 0)
469     return GroupPoints->SpinBox_DX->GetValue();
470   else if (aConstructorId == 1)
471     return GroupDimensions->SpinBox_DX->GetValue();
472   return 0;
473 }
474
475
476 //=================================================================================
477 // function : getRadius2()
478 // purpose  :
479 //=================================================================================
480 double PrimitiveGUI_ConeDlg::getRadius2() const
481 {
482   int aConstructorId = getConstructorId();
483   if (aConstructorId == 0)
484     return GroupPoints->SpinBox_DY->GetValue();
485   else if (aConstructorId == 1)
486     return GroupDimensions->SpinBox_DY->GetValue();
487   return 0;
488 }
489
490
491 //=================================================================================
492 // function : getRadius2()
493 // purpose  :
494 //=================================================================================
495 double PrimitiveGUI_ConeDlg::getHeight() const
496 {
497   int aConstructorId = getConstructorId();
498   if (aConstructorId == 0)
499     return GroupPoints->SpinBox_DZ->GetValue();
500   else if (aConstructorId == 1)
501     return GroupDimensions->SpinBox_DZ->GetValue();
502   return 0;
503 }