Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/geom.git] / src / BasicGUI / BasicGUI_CircleDlg.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   : BasicGUI_CircleDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "BasicGUI_CircleDlg.h"
30
31 #include <gp_Lin.hxx>
32 #include <gp_Circ.hxx>
33 #include <BRepBuilderAPI_MakeEdge.hxx>
34 #include <BRepAdaptor_Curve.hxx>
35 #include "QAD_Config.h"
36
37 #include "utilities.h"
38
39 using namespace std;
40
41 //=================================================================================
42 // class    : BasicGUI_CircleDlg()
43 // purpose  : Constructs a BasicGUI_CircleDlg 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 BasicGUI_CircleDlg::BasicGUI_CircleDlg(QWidget* parent, const char* name, BasicGUI* theBasicGUI, 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_CIRCLE_PV")));
52   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
53
54   setCaption(tr("GEOM_CIRCLE_TITLE"));
55
56   /***************************************************************/
57   GroupConstructors->setTitle(tr("GEOM_CIRCLE"));
58   RadioButton1->setPixmap(image0);
59   RadioButton2->close(TRUE);
60   RadioButton3->close(TRUE);
61
62   GroupPoints = new DlgRef_2Sel1Spin(this, "GroupPoints");
63   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
64   GroupPoints->TextLabel1->setText(tr("GEOM_CENTER_POINT"));
65   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
66   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS"));
67   GroupPoints->PushButton1->setPixmap(image1);
68   GroupPoints->PushButton2->setPixmap(image1);
69
70   Layout1->addWidget(GroupPoints, 1, 0);
71   /***************************************************************/
72
73   /* Initialisations */
74   myBasicGUI = theBasicGUI;
75   Init();
76 }
77
78
79 //=================================================================================
80 // function : ~BasicGUI_CircleDlg()
81 // purpose  : Destroys the object and frees any allocated resources
82 //=================================================================================
83 BasicGUI_CircleDlg::~BasicGUI_CircleDlg()
84 {
85     // no need to delete child widgets, Qt does it all for us
86 }
87
88
89 //=================================================================================
90 // function : Init()
91 // purpose  :
92 //=================================================================================
93 void BasicGUI_CircleDlg::Init()
94 {
95   /* init variables */
96   myEditCurrentArgument = GroupPoints->LineEdit1;
97
98   myRadius = 100.0;
99   myOkPoint1 = myOkDir = false;
100
101   myEdgeFilter = new GEOM_ShapeTypeFilter(TopAbs_EDGE, myGeom);
102   myVertexFilter = new GEOM_ShapeTypeFilter(TopAbs_VERTEX, myGeom);
103   mySelection->AddFilter(myVertexFilter);
104
105   /* Get setting of step value from file configuration */
106   QString St = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
107   step = St.toDouble();
108
109   /* min, max, step and decimals for spin boxes & initial values */
110   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
111   GroupPoints->SpinBox_DX->SetValue(myRadius);
112
113   /* signals and slots connections */
114   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
115   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
116
117   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
118   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
119
120   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
121   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
122
123   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
124   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
125   
126   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
127
128   /* displays Dialog */
129   GroupPoints->show();
130   this->show();
131
132   return;
133 }
134
135
136 //=================================================================================
137 // function : ClickOnOk()
138 // purpose  :
139 //=================================================================================
140 void BasicGUI_CircleDlg::ClickOnOk()
141 {
142   this->ClickOnApply();
143   ClickOnCancel();
144   return;
145 }
146
147 //=================================================================================
148 // function : ClickOnApply()
149 // purpose  :
150 //=================================================================================
151 void BasicGUI_CircleDlg::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(myOkPoint1 && myOkDir)
161     myBasicGUI->MakeCircleAndDisplay(myPoint1, myDir, myRadius);
162   return;
163 }
164
165
166 //=================================================================================
167 // function : SelectionIntoArgument()
168 // purpose  : Called when selection as changed or other case
169 //=================================================================================
170 void BasicGUI_CircleDlg::SelectionIntoArgument()
171 {
172   myGeomBase->EraseSimulationShape(); 
173   mySimulationTopoDs.Nullify();
174   myEditCurrentArgument->setText("");
175   QString aString = ""; /* name of selection */
176   
177   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
178   if (nbSel != 1) {
179     if(myEditCurrentArgument == GroupPoints->LineEdit1)
180       myOkPoint1 = false;
181     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
182       myOkDir = false;
183     return;
184   }
185
186   /* nbSel == 1 */
187   TopoDS_Shape S;  
188   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
189     return;  
190  
191   /*  gp_Pnt : not used */
192   if(myEditCurrentArgument == GroupPoints->LineEdit1 && myGeomBase->VertexToPoint(S, myPoint1)) {
193     GroupPoints->LineEdit1->setText(aString);
194     myOkPoint1 = true;
195   }    
196   else if(myEditCurrentArgument == GroupPoints->LineEdit2) {
197     BRepAdaptor_Curve curv(TopoDS::Edge(S));
198     myDir = curv.Line().Direction();
199     GroupPoints->LineEdit2->setText(aString);
200     myOkDir = true;
201   }
202
203   if(myOkPoint1 && myOkDir)
204     this->MakeCircleSimulationAndDisplay();
205   return;
206 }
207
208
209 //=================================================================================
210 // function : SetEditCurrentArgument()
211 // purpose  :
212 //=================================================================================
213 void BasicGUI_CircleDlg::SetEditCurrentArgument()
214 {
215   QPushButton* send = (QPushButton*)sender();
216   mySelection->ClearFilters();
217
218   if(send == GroupPoints->PushButton1) {
219     GroupPoints->LineEdit1->setFocus();
220     myEditCurrentArgument = GroupPoints->LineEdit1;
221     mySelection->AddFilter(myVertexFilter);
222   }
223   else if(send == GroupPoints->PushButton2) {
224     GroupPoints->LineEdit2->setFocus();
225     myEditCurrentArgument = GroupPoints->LineEdit2;
226     mySelection->AddFilter(myEdgeFilter);
227   }
228   this->SelectionIntoArgument();
229
230   return;
231 }
232
233 //=================================================================================
234 // function : LineEditReturnPressed()
235 // purpose  :
236 //=================================================================================
237 void BasicGUI_CircleDlg::LineEditReturnPressed()
238 {
239   QLineEdit* send = (QLineEdit*)sender();
240   if(send == GroupPoints->LineEdit1)
241     myEditCurrentArgument = GroupPoints->LineEdit1;
242   else if (send == GroupPoints->LineEdit2)
243     myEditCurrentArgument = GroupPoints->LineEdit2;
244   else
245     return;
246
247   GEOMBase_Skeleton::LineEditReturnPressed();
248   return;
249 }
250
251
252 //=================================================================================
253 // function : ActivateThisDialog()
254 // purpose  :
255 //=================================================================================
256 void BasicGUI_CircleDlg::ActivateThisDialog()
257 {
258   GEOMBase_Skeleton::ActivateThisDialog();
259   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
260   GroupPoints->LineEdit1->setFocus();
261   myEditCurrentArgument = GroupPoints->LineEdit1;
262   mySelection->AddFilter(myVertexFilter);
263   if(!mySimulationTopoDs.IsNull())
264     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
265   return;
266 }
267
268
269 //=================================================================================
270 // function : enterEvent()
271 // purpose  :
272 //=================================================================================
273 void BasicGUI_CircleDlg::enterEvent(QEvent* e)
274 {
275   if(GroupConstructors->isEnabled())
276     return;
277   this->ActivateThisDialog();
278   return;
279 }
280
281
282 //=================================================================================
283 // function : ValueChangedInSpinBox()
284 // purpose  :
285 //=================================================================================
286 void BasicGUI_CircleDlg::ValueChangedInSpinBox(double newValue)
287 {
288   myRadius = newValue;
289   if(myOkPoint1 && myOkDir)
290     MakeCircleSimulationAndDisplay();
291   return;
292 }
293
294
295 //=================================================================================
296 // function : MakeCircleSimulationAndDisplay()
297 // purpose  :
298 //=================================================================================
299 void BasicGUI_CircleDlg::MakeCircleSimulationAndDisplay() 
300 {
301   myGeomBase->EraseSimulationShape();
302   mySimulationTopoDs.Nullify();
303
304   try {
305     gp_Ax2 anAxis(myPoint1, myDir);
306     gp_Circ circ(anAxis, myRadius);
307     BRepBuilderAPI_MakeEdge MakeEdge(circ);
308     mySimulationTopoDs = MakeEdge.Shape();
309     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
310   }
311   catch(Standard_Failure) {
312     MESSAGE("Exception catched in MakeCircleSimulationAndDisplay");
313   }
314   return;
315 }