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