Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/geom.git] / src / GenerationGUI / GenerationGUI_RevolDlg.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   : GenerationGUI_RevolDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "GenerationGUI_RevolDlg.h"
30
31 #include <gp_Lin.hxx>
32 #include <BRepAdaptor_Curve.hxx>
33 #include <BRepPrimAPI_MakeRevol.hxx>
34 #include "QAD_Config.h"
35 #include <TopExp_Explorer.hxx>
36 #include <Standard_ErrorHandler.hxx>
37
38 #include "utilities.h"
39
40 using namespace std;
41
42 //=================================================================================
43 // class    : GenerationGUI_RevolDlg()
44 // purpose  : Constructs a GenerationGUI_RevolDlg which is a child of 'parent', with the 
45 //            name 'name' and widget flags set to 'f'.
46 //            The dialog will by default be modeless, unless you set 'modal' to
47 //            TRUE to construct a modal dialog.
48 //=================================================================================
49 GenerationGUI_RevolDlg::GenerationGUI_RevolDlg(QWidget* parent, const char* name, GenerationGUI* theGenerationGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
50   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
51 {
52   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_REVOL")));
53   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
54
55   setCaption(tr("GEOM_REVOLUTION_TITLE"));
56
57   /***************************************************************/
58   GroupConstructors->setTitle(tr("GEOM_REVOLUTION"));
59   RadioButton1->setPixmap(image0);
60   RadioButton2->close(TRUE);
61   RadioButton3->close(TRUE);
62
63   GroupPoints = new DlgRef_2Sel1Spin1Check(this, "GroupPoints");
64   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
65   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECT"));
66   GroupPoints->TextLabel2->setText(tr("GEOM_AXIS"));
67   GroupPoints->TextLabel3->setText(tr("GEOM_ANGLE"));
68   GroupPoints->CheckButton1->setText(tr("GEOM_REVERSE"));
69   GroupPoints->PushButton1->setPixmap(image1);
70   GroupPoints->PushButton2->setPixmap(image1);
71
72   Layout1->addWidget(GroupPoints, 1, 0);
73   /***************************************************************/
74
75   /* Initialisations */
76   myGenerationGUI = theGenerationGUI;
77   Init();
78 }
79
80
81 //=================================================================================
82 // function : ~GenerationGUI_RevolDlg()
83 // purpose  : Destroys the object and frees any allocated resources
84 //=================================================================================
85 GenerationGUI_RevolDlg::~GenerationGUI_RevolDlg()
86 {
87   // no need to delete child widgets, Qt does it all for us
88 }
89
90
91 //=================================================================================
92 // function : Init()
93 // purpose  :
94 //=================================================================================
95 void GenerationGUI_RevolDlg::Init()
96 {
97   /* init variables */
98   myEditCurrentArgument = GroupPoints->LineEdit1;
99
100   myAngle = 45.0;
101   myOkBase = myOkAxis = false;
102
103   myEdgeFilter = new GEOM_ShapeTypeFilter(TopAbs_EDGE, myGeom);
104
105   double SpecificStep = 5;
106   /* min, max, step and decimals for spin boxes & initial values */
107   GroupPoints->SpinBox_DX->RangeStepAndValidator(-999.999, 999.999, SpecificStep, 3);
108   GroupPoints->SpinBox_DX->SetValue(myAngle);
109
110   /* signals and slots connections */
111   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
112   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
113
114   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
115   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
116
117   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
118   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
119
120   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
121   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
122
123   connect(GroupPoints->CheckButton1, SIGNAL(stateChanged(int)), this, SLOT(ReverseAngle(int)));
124   
125   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
126
127   /* displays Dialog */
128   GroupPoints->show();
129   this->show();
130
131   return;
132 }
133
134
135 //=================================================================================
136 // function : ClickOnOk()
137 // purpose  :
138 //=================================================================================
139 void GenerationGUI_RevolDlg::ClickOnOk()
140 {
141   this->ClickOnApply();
142   ClickOnCancel();
143   return;
144 }
145
146
147 //=================================================================================
148 // function : ClickOnApply()
149 // purpose  :
150 //=================================================================================
151 void GenerationGUI_RevolDlg::ClickOnApply()
152 {
153   buttonApply->setFocus();
154   QAD_Application::getDesktop()->putInfo(tr(""));
155   if (mySimulationTopoDs.IsNull())
156     return;
157   myGeomBase->EraseSimulationShape();
158   mySimulationTopoDs.Nullify();
159
160   if(myOkBase && myOkAxis)
161     myGenerationGUI->MakeRevolutionAndDisplay(myGeomShape, myLoc, myDir, myAngle*PI180); 
162   return;
163 }
164
165 //=======================================================================
166 //function : isAcceptableBase
167 //purpose  : return true if theBase can be used as algo argument
168 //=======================================================================
169
170 static bool isAcceptableBase(const TopoDS_Shape& theBase)
171 {
172   switch ( theBase.ShapeType() ) {
173   case TopAbs_VERTEX:
174   case TopAbs_EDGE:
175   case TopAbs_WIRE:
176   case TopAbs_FACE:
177   case TopAbs_SHELL:
178     return true;
179   case TopAbs_SOLID:
180   case TopAbs_COMPSOLID:
181     return false;
182   case TopAbs_COMPOUND: {
183     TopExp_Explorer exp( theBase, TopAbs_SOLID);
184     return !exp.More();
185   }
186   default:
187     return false;
188   }
189   return false;
190 }
191
192 //=================================================================================
193 // function : SelectionIntoArgument()
194 // purpose  : Called when selection as changed or other case
195 //=================================================================================
196 void GenerationGUI_RevolDlg::SelectionIntoArgument()
197 {
198   myGeomBase->EraseSimulationShape();
199   mySimulationTopoDs.Nullify();
200   myEditCurrentArgument->setText("");
201   QString aString = ""; /* name of selection */
202   
203   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
204   if(nbSel != 1) {
205     if(myEditCurrentArgument == GroupPoints->LineEdit1)
206       myOkBase = false;
207     else if(myEditCurrentArgument == GroupPoints->LineEdit2)
208       myOkAxis = false;
209     return;
210   }
211   
212   // nbSel == 1
213   TopoDS_Shape S; 
214   Standard_Boolean testResult;
215   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject();
216   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
217     return;
218     
219   if(myEditCurrentArgument == GroupPoints->LineEdit1) {
220     myGeomShape = myGeomBase->ConvertIOinGEOMShape(IO, testResult);
221     if(!testResult)
222       return;
223     if( !isAcceptableBase( S ))
224       return;
225     myEditCurrentArgument->setText(aString);
226     myOkBase = true;
227     myBase = S;
228   }
229   else if(myEditCurrentArgument == GroupPoints->LineEdit2) {
230     BRepAdaptor_Curve curv(TopoDS::Edge(S));
231     myDir = curv.Line().Direction();
232     myLoc = curv.Line().Location();
233     myEditCurrentArgument->setText(aString);
234     myOkAxis = true;
235   }
236
237   if(myOkBase && myOkAxis)
238     this->MakeRevolutionSimulationAndDisplay();
239   return; 
240 }
241
242
243 //=================================================================================
244 // function : SetEditCurrentArgument()
245 // purpose  :
246 //=================================================================================
247 void GenerationGUI_RevolDlg::SetEditCurrentArgument()
248 {
249   QPushButton* send = (QPushButton*)sender();
250   mySelection->ClearFilters();
251
252   if(send == GroupPoints->PushButton1) {
253     GroupPoints->LineEdit1->setFocus();
254     myEditCurrentArgument = GroupPoints->LineEdit1;
255   }
256   else if(send == GroupPoints->PushButton2) {
257     GroupPoints->LineEdit2->setFocus();
258     myEditCurrentArgument = GroupPoints->LineEdit2;
259     mySelection->AddFilter(myEdgeFilter);
260   }
261   this->SelectionIntoArgument();
262
263   return;
264 }
265
266
267 //=================================================================================
268 // function : LineEditReturnPressed()
269 // purpose  :
270 //=================================================================================
271 void GenerationGUI_RevolDlg::LineEditReturnPressed()
272 {  
273   QLineEdit* send = (QLineEdit*)sender();
274   if(send == GroupPoints->LineEdit1)
275     myEditCurrentArgument = GroupPoints->LineEdit1;
276   else if (send == GroupPoints->LineEdit2)
277     myEditCurrentArgument = GroupPoints->LineEdit2;
278   else
279     return;
280
281   GEOMBase_Skeleton::LineEditReturnPressed();
282   return;
283 }
284
285
286 //=================================================================================
287 // function : ActivateThisDialog()
288 // purpose  :
289 //=================================================================================
290 void GenerationGUI_RevolDlg::ActivateThisDialog()
291 {
292   GEOMBase_Skeleton::ActivateThisDialog();
293   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
294   GroupPoints->LineEdit1->setFocus();
295   myEditCurrentArgument = GroupPoints->LineEdit1;
296   if(!mySimulationTopoDs.IsNull())
297     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
298   return;
299 }
300
301
302 //=================================================================================
303 // function : enterEvent()
304 // purpose  :
305 //=================================================================================
306 void GenerationGUI_RevolDlg::enterEvent(QEvent* e)
307 {
308   if (GroupConstructors->isEnabled())
309     return;
310   this->ActivateThisDialog();
311   return;
312 }
313
314
315 //=================================================================================
316 // function : ValueChangedInSpinBox()
317 // purpose  :
318 //=================================================================================
319 void GenerationGUI_RevolDlg::ValueChangedInSpinBox(double newValue)
320 {  
321   myAngle = newValue;
322   if(myOkBase && myOkAxis)
323     this->MakeRevolutionSimulationAndDisplay();
324   return;
325 }
326
327
328 //=================================================================================
329 // function : ReverseAngle()
330 // purpose  : 'state' not used here
331 //=================================================================================
332 void GenerationGUI_RevolDlg::ReverseAngle(int state)
333 {
334   myAngle = -myAngle;
335   GroupPoints->SpinBox_DX->SetValue(myAngle);
336   if(myOkBase && myOkAxis)
337     this->MakeRevolutionSimulationAndDisplay();
338   return;
339 }
340
341
342 //=================================================================================
343 // function : MakeRevolutionSimulationAndDisplay()
344 // purpose  :
345 //=================================================================================
346 void GenerationGUI_RevolDlg::MakeRevolutionSimulationAndDisplay() 
347 {
348   myGeomBase->EraseSimulationShape();
349   mySimulationTopoDs.Nullify();
350
351   if (!isAcceptableBase( myBase ))
352     return;
353
354   try {
355     gp_Ax1 AX(myLoc, myDir);
356     mySimulationTopoDs = BRepPrimAPI_MakeRevol(myBase, AX, myAngle*PI180);
357     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
358   }
359   catch(Standard_Failure) {
360     MESSAGE("Exception catched in MakeRevolutionSimulationAndDisplay");
361     return;
362   }
363   return;
364 }