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