Salome HOME
80321b426bfae379a0e8e8a973387770466f3d14
[modules/geom.git] / src / BasicGUI / BasicGUI_EllipseDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE 
4 // 
5 //  This library is free software; you can redistribute it and/or 
6 //  modify it under the terms of the GNU Lesser General Public 
7 //  License as published by the Free Software Foundation; either 
8 //  version 2.1 of the License. 
9 // 
10 //  This library is distributed in the hope that it will be useful, 
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13 //  Lesser General Public License for more details. 
14 // 
15 //  You should have received a copy of the GNU Lesser General Public 
16 //  License along with this library; if not, write to the Free Software 
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
18 // 
19 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
20 //
21 //
22 //
23 //  File   : BasicGUI_EllipseDlg.cxx
24 //  Author : Nicolas REJNERI
25 //  Module : GEOM
26 //  $Header$
27
28 #include "BasicGUI_EllipseDlg.h"
29
30 #include <gp_Lin.hxx>
31 #include <gp_Elips.hxx>
32 #include <BRepBuilderAPI_MakeEdge.hxx>
33 #include <BRepAdaptor_Curve.hxx>
34 #include "QAD_Config.h"
35
36 #include "utilities.h"
37
38 using namespace std;
39
40 //=================================================================================
41 // class    : BasicGUI_EllipseDlg()
42 // purpose  : Constructs a BasicGUI_EllipseDlg which is a child of 'parent', with the 
43 //            name 'name' and widget flags set to 'f'.
44 //            The dialog will by default be modeless, unless you set 'modal' to
45 //            TRUE to construct a modal dialog.
46 //=================================================================================
47 BasicGUI_EllipseDlg::BasicGUI_EllipseDlg(QWidget* parent, const char* name, BasicGUI* theBasicGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
48   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
49 {
50   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_ELLIPSE_PV")));
51   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
52
53   setCaption(tr("GEOM_ELLIPSE_TITLE"));
54   
55   /***************************************************************/
56   GroupConstructors->setTitle(tr("GEOM_ELLIPSE"));
57   RadioButton1->setPixmap(image0);
58   RadioButton2->close(TRUE);
59   RadioButton3->close(TRUE);
60
61   GroupPoints = new DlgRef_2Sel2Spin(this, "GroupPoints");
62   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
63   GroupPoints->TextLabel1->setText(tr("GEOM_CENTER"));
64   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
65   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS_MINOR"));
66   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_MAJOR"));
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_EllipseDlg()
81 // purpose  : Destroys the object and frees any allocated resources
82 //=================================================================================
83 BasicGUI_EllipseDlg::~BasicGUI_EllipseDlg()
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_EllipseDlg::Init()
94 {
95   /* init variables */
96   myEditCurrentArgument = GroupPoints->LineEdit1;
97
98   myMajorRadius = 200.0;
99   myMinorRadius = 100.0;
100   myOkPoint = myOkDir = false;
101
102   myEdgeFilter = new GEOM_ShapeTypeFilter(TopAbs_EDGE, myGeom);
103   myVertexFilter = new GEOM_ShapeTypeFilter(TopAbs_VERTEX, myGeom);
104   mySelection->AddFilter(myVertexFilter);
105
106   /* Get setting of step value from file configuration */
107   QString St = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
108   step = St.toDouble();
109
110   /* min, max, step and decimals for spin boxes & initial values */
111   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
112   GroupPoints->SpinBox_DY->RangeStepAndValidator(0.001, 999.999, step, 3);
113   GroupPoints->SpinBox_DX->SetValue(myMajorRadius);
114   GroupPoints->SpinBox_DY->SetValue(myMinorRadius);
115
116   /* signals and slots connections */
117   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
118   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
119
120   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
121   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
122
123   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
124   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
125
126   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
127   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
128
129   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
130   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
131   
132   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
133
134   /* displays Dialog */
135   GroupPoints->show();
136   this->show();
137
138   return;
139 }
140
141
142 //=================================================================================
143 // function : ClickOnOk()
144 // purpose  :
145 //=================================================================================
146 void BasicGUI_EllipseDlg::ClickOnOk()
147 {
148   this->ClickOnApply();
149   ClickOnCancel();
150   return;
151 }
152
153 //=================================================================================
154 // function : ClickOnApply()
155 // purpose  :
156 //=================================================================================
157 void BasicGUI_EllipseDlg::ClickOnApply()
158 {
159   buttonApply->setFocus();
160   QAD_Application::getDesktop()->putInfo(tr(""));
161   if (mySimulationTopoDs.IsNull())
162     return;
163   myGeomBase->EraseSimulationShape();
164   mySimulationTopoDs.Nullify();
165
166   if(myOkPoint && myOkDir)
167     myBasicGUI->MakeEllipseAndDisplay(myPoint, myDir, myMajorRadius, myMinorRadius); 
168 }
169
170
171 //=================================================================================
172 // function : SelectionIntoArgument()
173 // purpose  : Called when selection as changed or other case
174 //=================================================================================
175 void BasicGUI_EllipseDlg::SelectionIntoArgument()
176 {
177   myGeomBase->EraseSimulationShape();
178   mySimulationTopoDs.Nullify();
179   myEditCurrentArgument->setText("");
180   QString aString = ""; /* name of selection */
181   
182   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
183   if (nbSel != 1) {
184     if(myEditCurrentArgument == GroupPoints->LineEdit1)
185       myOkPoint = false;
186     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
187       myOkDir = false;
188     return;
189   }
190
191   /* nbSel == 1 */
192   TopoDS_Shape S;  
193   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
194     return;  
195  
196   /*  gp_Pnt : not used */
197   if(myEditCurrentArgument == GroupPoints->LineEdit1 && myGeomBase->VertexToPoint(S, myPoint)) {
198     GroupPoints->LineEdit1->setText(aString);
199     myOkPoint = true;
200   }    
201   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
202     BRepAdaptor_Curve curv(TopoDS::Edge(S));
203     myDir = curv.Line().Direction();
204     GroupPoints->LineEdit2->setText(aString);
205     myOkDir = true;
206   }
207
208   if(myOkPoint && myOkDir)
209     this->MakeEllipseSimulationAndDisplay();
210   return;
211 }
212
213
214 //=================================================================================
215 // function : SetEditCurrentArgument()
216 // purpose  :
217 //=================================================================================
218 void BasicGUI_EllipseDlg::SetEditCurrentArgument()
219 {
220   QPushButton* send = (QPushButton*)sender();
221   mySelection->ClearFilters();
222
223   if(send == GroupPoints->PushButton1) {
224     GroupPoints->LineEdit1->setFocus();
225     myEditCurrentArgument = GroupPoints->LineEdit1;
226     mySelection->AddFilter(myVertexFilter);
227   }
228   else if(send == GroupPoints->PushButton2) {
229     GroupPoints->LineEdit2->setFocus();
230     myEditCurrentArgument = GroupPoints->LineEdit2;
231     mySelection->AddFilter(myEdgeFilter);
232   }
233   this->SelectionIntoArgument();
234
235   return;
236 }
237
238 //=================================================================================
239 // function : LineEditReturnPressed()
240 // purpose  :
241 //=================================================================================
242 void BasicGUI_EllipseDlg::LineEditReturnPressed()
243 {
244   QLineEdit* send = (QLineEdit*)sender();
245   if(send == GroupPoints->LineEdit1)
246     myEditCurrentArgument = GroupPoints->LineEdit1;
247   else if (send == GroupPoints->LineEdit2)
248     myEditCurrentArgument = GroupPoints->LineEdit2;
249   else
250     return;
251
252   GEOMBase_Skeleton::LineEditReturnPressed();
253   return;
254 }
255
256
257 //=================================================================================
258 // function : ActivateThisDialog()
259 // purpose  :
260 //=================================================================================
261 void BasicGUI_EllipseDlg::ActivateThisDialog()
262 {
263   GEOMBase_Skeleton::ActivateThisDialog();
264   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
265   GroupPoints->LineEdit1->setFocus();
266   myEditCurrentArgument = GroupPoints->LineEdit1;
267   mySelection->AddFilter(myVertexFilter);
268   if(!mySimulationTopoDs.IsNull())
269     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
270   return;
271 }
272
273
274 //=================================================================================
275 // function : enterEvent()
276 // purpose  :
277 //=================================================================================
278 void BasicGUI_EllipseDlg::enterEvent(QEvent* e)
279 {
280   if (GroupConstructors->isEnabled())
281     return;
282   this->ActivateThisDialog();
283   return;
284 }
285
286
287 //=================================================================================
288 // function : ValueChangedInSpinBox()
289 // purpose  :
290 //=================================================================================
291 void BasicGUI_EllipseDlg::ValueChangedInSpinBox(double newValue)
292 {
293   QObject* send = (QObject*)sender();
294
295   if(send == GroupPoints->SpinBox_DX )
296     myMajorRadius = newValue;
297   else if(send == GroupPoints->SpinBox_DY)
298     myMinorRadius = newValue;
299
300   if (myOkPoint && myOkDir)
301     MakeEllipseSimulationAndDisplay();
302   return;
303 }
304
305
306 //=================================================================================
307 // function : MakeEllipseSimulationAndDisplay()
308 // purpose  :
309 //=================================================================================
310 void BasicGUI_EllipseDlg::MakeEllipseSimulationAndDisplay() 
311 {
312   myGeomBase->EraseSimulationShape();
313   mySimulationTopoDs.Nullify();
314
315   if(myMajorRadius < myMinorRadius)
316     return;
317
318   try {
319     gp_Ax2 anAxis(myPoint, myDir);
320     gp_Elips ellipse(anAxis, myMajorRadius, myMinorRadius);
321     BRepBuilderAPI_MakeEdge MakeEdge(ellipse);
322     mySimulationTopoDs = MakeEdge.Shape();
323     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
324   }
325   catch(Standard_Failure) {
326     MESSAGE("Exception catched in MakeEllipseSimulationAndDisplay");
327   }
328   return;
329 }