Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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 <gp_Lin.hxx>
32 #include <BRepPrimAPI_MakeCylinder.hxx>
33 #include <BRepAdaptor_Curve.hxx>
34 #include <Precision.hxx>
35 #include "QAD_Config.h"
36
37 #include "utilities.h"
38
39 using namespace std;
40
41 //=================================================================================
42 // class    : PrimitiveGUI_CylinderDlg()
43 // purpose  : Constructs a PrimitiveGUI_CylinderDlg which is a child of 'parent', with the 
44 //            name 'name' and widget flags set to 'f'.
45 //            The dialog will by default be modeless, unless you set 'modal' to
46 //            TRUE to construct a modal dialog.
47 //=================================================================================
48 PrimitiveGUI_CylinderDlg::PrimitiveGUI_CylinderDlg(QWidget* parent, const char* name, PrimitiveGUI* thePrimitiveGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
49   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
50 {
51   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_CYLINDER_PV")));
52   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_CYLINDER_DXYZ")));
53   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
54
55   setCaption(tr("GEOM_CYLINDER_TITLE"));
56
57   /***************************************************************/
58   GroupConstructors->setTitle(tr("GEOM_CYLINDER"));
59   RadioButton1->setPixmap(image0);
60   RadioButton2->setPixmap(image1);
61   RadioButton3->close(TRUE);
62
63   GroupPoints = new DlgRef_2Sel2Spin(this, "GroupPoints");
64   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
65   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_POINT"));
66   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
67   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS"));
68   GroupPoints->TextLabel4->setText(tr("GEOM_HEIGHT"));
69   GroupPoints->PushButton1->setPixmap(image2);
70   GroupPoints->PushButton2->setPixmap(image2);
71
72   GroupDimensions = new DlgRef_2Spin(this, "GroupDimensions");
73   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
74   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS"));
75   GroupDimensions->TextLabel2->setText(tr("GEOM_HEIGHT"));
76
77   Layout1->addWidget(GroupPoints, 1, 0);
78   Layout1->addWidget(GroupDimensions, 1, 0);
79   /***************************************************************/
80
81   /* Initialisations */
82   myPrimitiveGUI = thePrimitiveGUI;
83   Init();
84 }
85
86
87 //=================================================================================
88 // function : ~PrimitiveGUI_CylinderDlg()
89 // purpose  : Destroys the object and frees any allocated resources
90 //=================================================================================
91 PrimitiveGUI_CylinderDlg::~PrimitiveGUI_CylinderDlg()
92 {
93     // no need to delete child widgets, Qt does it all for us
94 }
95
96
97 //=================================================================================
98 // function : Init()
99 // purpose  :
100 //=================================================================================
101 void PrimitiveGUI_CylinderDlg::Init()
102 {
103   /* init variables */
104   myConstructorId = 0;
105   myEditCurrentArgument = GroupPoints->LineEdit1;
106
107   myRadius = 100.0;
108   myHeight = 300.0;
109   myOkRadius = myOkHeight = true;
110   myOkPoint1 = myOkDir = false;
111
112   myEdgeFilter = new GEOM_EdgeFilter(StdSelect_Line, myGeom);
113   myVertexFilter = new GEOM_ShapeTypeFilter(TopAbs_VERTEX, myGeom);
114   mySelection->AddFilter(myVertexFilter);
115
116   /* Get setting of step value from file configuration */
117   QString St = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
118   step = St.toDouble();
119
120   /* min, max, step and decimals for spin boxes & initial values */
121   /* First constructor : radius */
122   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
123   /* First constructor : algebric height */
124   GroupPoints->SpinBox_DY->RangeStepAndValidator(-999.999, 999.999, step, 3);
125   /* Second constructor : radius */
126   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
127   /* Second constructor : algebric height */
128   GroupDimensions->SpinBox_DY->RangeStepAndValidator(-999.999, 999.999, step, 3);
129
130   GroupPoints->SpinBox_DX->SetValue(myRadius);
131   GroupPoints->SpinBox_DY->SetValue(myHeight);
132   GroupDimensions->SpinBox_DX->SetValue(myRadius);  
133   GroupDimensions->SpinBox_DY->SetValue(myHeight);  
134
135   /* signals and slots connections */
136   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
137   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
138   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
139
140   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
141   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
142
143   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
144   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
145
146   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
147   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
148   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
149   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
150
151   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
152   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
153   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
154   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DY, SLOT(SetStep(double)));
155   
156   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
157
158   /* displays Dialog */
159   GroupDimensions->hide();
160   GroupPoints->show();
161   this->show();
162
163   return ;
164 }
165
166
167 //=================================================================================
168 // function : ConstructorsClicked()
169 // purpose  : Radio button management
170 //=================================================================================
171 void PrimitiveGUI_CylinderDlg::ConstructorsClicked(int constructorId)
172 {
173   myConstructorId = constructorId;
174   mySelection->ClearFilters();
175   myGeomBase->EraseSimulationShape();
176   disconnect(mySelection, 0, this, 0);
177   myOkHeight = myOkRadius = true;
178   myRadius = 100.0;
179   myHeight = 300.0;
180
181   switch(myConstructorId)
182     { 
183     case 0 :
184       {
185         GroupDimensions->hide();
186         resize(0, 0);
187         GroupPoints->show();
188         myOkPoint1 = myOkDir = false;
189
190         myEditCurrentArgument = GroupPoints->LineEdit1;
191         GroupPoints->LineEdit1->setText(tr(""));
192         GroupPoints->LineEdit2->setText(tr(""));
193         
194         GroupPoints->SpinBox_DX->SetValue(myRadius);
195         GroupPoints->SpinBox_DY->SetValue(myHeight);
196
197         /* filter for next selection */
198         mySelection->AddFilter(myVertexFilter);
199         connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
200         break;
201       }
202     case 1 :
203       { 
204         GroupPoints->hide();
205         resize( 0, 0 );
206         GroupDimensions->show();
207         myOkPoint1 = myOkDir = true;
208
209         GroupDimensions->SpinBox_DX->SetValue(myRadius);
210         GroupDimensions->SpinBox_DY->SetValue(myHeight);
211         myPoint1.SetCoord(0.0, 0.0, 0.0);
212         myDir.SetCoord(0.0, 0.0, 1.0);
213
214         MakeCylinderSimulationAndDisplay();
215         break;
216       }
217     }
218   return ;
219 }
220
221
222 //=================================================================================
223 // function : ClickOnOk()
224 // purpose  :
225 //=================================================================================
226 void PrimitiveGUI_CylinderDlg::ClickOnOk()
227 {
228   this->ClickOnApply();
229   ClickOnCancel();
230   return;
231 }
232
233
234 //=================================================================================
235 // function : ClickOnApply()
236 // purpose  :
237 //=================================================================================
238 void PrimitiveGUI_CylinderDlg::ClickOnApply()
239 {
240   buttonApply->setFocus();
241   QAD_Application::getDesktop()->putInfo(tr(""));
242   if (mySimulationTopoDs.IsNull())
243     return;
244   myGeomBase->EraseSimulationShape();
245   mySimulationTopoDs.Nullify();
246
247   if(myOkPoint1 && myOkDir && myOkRadius && myOkHeight) {
248     gp_Dir aDir = myDir;
249     /* allows user to reverse direction of construction with a negative height */
250     if(this->myHeight < -Precision::Confusion())
251       aDir.Reverse();
252     myPrimitiveGUI->MakeCylinderAndDisplay(myPoint1, aDir, myRadius, fabs(myHeight)); 
253   }
254   return;
255 }
256
257
258 //=================================================================================
259 // function : SelectionIntoArgument()
260 // purpose  : Called when selection as changed or other case
261 //=================================================================================
262 void PrimitiveGUI_CylinderDlg::SelectionIntoArgument()
263 {
264   myGeomBase->EraseSimulationShape(); 
265   myEditCurrentArgument->setText("");
266   QString aString = ""; /* name of selection */
267
268   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
269   if(nbSel != 1) {
270     if(myEditCurrentArgument == GroupPoints->LineEdit1)
271       myOkPoint1 = false;
272     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
273       myOkDir = false;
274     return;
275   }
276
277   /* nbSel == 1 */
278   TopoDS_Shape S;
279   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
280     return;
281  
282   /*  gp_Pnt : not used */
283   if (myEditCurrentArgument == GroupPoints->LineEdit1 && myGeomBase->VertexToPoint(S, myPoint1)) {
284     GroupPoints->LineEdit1->setText(aString);
285     myOkPoint1 = true;
286   }    
287   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
288     BRepAdaptor_Curve curv(TopoDS::Edge(S));
289     myDir = curv.Line().Direction();
290     GroupPoints->LineEdit2->setText(aString);
291     myOkDir = true;
292   }
293   
294   if(myOkPoint1 && myOkDir && myOkRadius && myOkHeight)
295     MakeCylinderSimulationAndDisplay();
296   return;
297 }
298
299
300 //=================================================================================
301 // function : SetEditCurrentArgument()
302 // purpose  :
303 //=================================================================================
304 void PrimitiveGUI_CylinderDlg::SetEditCurrentArgument()
305 {
306   if(myConstructorId != 0)
307     return;
308
309   QPushButton* send = (QPushButton*)sender();
310   mySelection->ClearFilters();
311
312   if(send == GroupPoints->PushButton1) {
313     GroupPoints->LineEdit1->setFocus();
314     myEditCurrentArgument = GroupPoints->LineEdit1;
315     mySelection->AddFilter(myVertexFilter);
316   }
317   else if(send == GroupPoints->PushButton2) {
318     GroupPoints->LineEdit2->setFocus();
319     myEditCurrentArgument = GroupPoints->LineEdit2;
320     mySelection->AddFilter(myEdgeFilter);
321   }
322   this->SelectionIntoArgument();
323
324   return;
325 }
326
327
328 //=================================================================================
329 // function : LineEditReturnPressed()
330 // purpose  :
331 //=================================================================================
332 void PrimitiveGUI_CylinderDlg::LineEditReturnPressed()
333 {  
334   QLineEdit* send = (QLineEdit*)sender();
335   if(send == GroupPoints->LineEdit1)
336     myEditCurrentArgument = GroupPoints->LineEdit1;
337   else if (send == GroupPoints->LineEdit2)
338     myEditCurrentArgument = GroupPoints->LineEdit2;
339   else
340     return;
341
342   GEOMBase_Skeleton::LineEditReturnPressed();
343   return;
344 }
345
346
347 //=================================================================================
348 // function : ActivateThisDialog()
349 // purpose  :
350 //=================================================================================
351 void PrimitiveGUI_CylinderDlg::ActivateThisDialog()
352 {
353   GEOMBase_Skeleton::ActivateThisDialog();
354   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
355   if(myConstructorId == 0) {
356     GroupPoints->LineEdit1->setFocus();
357     myEditCurrentArgument = GroupPoints->LineEdit1;
358     mySelection->AddFilter(myVertexFilter);
359   }
360   if(!mySimulationTopoDs.IsNull())
361     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
362   return;
363 }
364
365
366 //=================================================================================
367 // function : enterEvent()
368 // purpose  :
369 //=================================================================================
370 void PrimitiveGUI_CylinderDlg::enterEvent(QEvent* e)
371 {
372   if (GroupConstructors->isEnabled())
373     return;
374   this->ActivateThisDialog();
375   return;
376 }
377
378
379 //=================================================================================
380 // function : ValueChangedInSpinBox
381 // purpose  :
382 //=================================================================================
383 void PrimitiveGUI_CylinderDlg::ValueChangedInSpinBox( double newValue )
384 {  
385   myGeomBase->EraseSimulationShape();
386   mySimulationTopoDs.Nullify();
387   QObject* send = (QObject*)sender();
388   
389     /* radius */
390   if(send == GroupPoints->SpinBox_DX || send == GroupDimensions->SpinBox_DX) {
391     myRadius = newValue;
392     myOkRadius = true;
393   } /* algebric height */
394   else if(send == GroupPoints->SpinBox_DY || send == GroupDimensions->SpinBox_DY) {
395     myHeight = newValue;
396     myOkHeight = true;
397   }
398
399   if (myOkPoint1 && myOkDir && myOkRadius && myOkHeight)
400     MakeCylinderSimulationAndDisplay();
401   return;
402 }
403
404
405 //=================================================================================
406 // function : MakeCylinderSimulationAndDisplay()
407 // purpose  :
408 //=================================================================================
409 void PrimitiveGUI_CylinderDlg::MakeCylinderSimulationAndDisplay() 
410 {
411   myGeomBase->EraseSimulationShape();
412   mySimulationTopoDs.Nullify();
413   gp_Dir aDir = this->myDir;
414   
415   try {
416     /* allows user to reverse direction of construction with a negative height */
417     if(this->myHeight < -Precision::Confusion())
418       aDir.Reverse();
419
420     gp_Ax2 anAxis(this->myPoint1, aDir);
421
422     mySimulationTopoDs = BRepPrimAPI_MakeCylinder(anAxis, this->myRadius, fabs(myHeight)).Shape();
423     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
424   }
425   catch(Standard_Failure) {
426     MESSAGE("Exception catched in MakeCylinderSimulationAndDisplay");
427   }
428   return;
429 }