Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_SphereDlg.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_SphereDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "PrimitiveGUI_SphereDlg.h"
31
32 #include <BRepPrimAPI_MakeSphere.hxx>
33 #include "QAD_Config.h"
34
35 //=================================================================================
36 // class    : PrimitiveGUI_SphereDlg()
37 // purpose  : Constructs a PrimitiveGUI_SphereDlg which is a child of 'parent', with the 
38 //            name 'name' and widget flags set to 'f'.
39 //            The dialog will by default be modeless, unless you set 'modal' to
40 //            TRUE to construct a modal dialog.
41 //=================================================================================
42 PrimitiveGUI_SphereDlg::PrimitiveGUI_SphereDlg(QWidget* parent,  const char* name, PrimitiveGUI* thePrimitiveGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
43   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
44 {
45   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_SPHERE_P")));
46   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_SPHERE_DXYZ")));
47   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
48
49   setCaption(tr("GEOM_SPHERE_TITLE"));
50
51   /***************************************************************/
52   GroupConstructors->setTitle(tr("GEOM_SPHERE"));
53   RadioButton1->setPixmap(image0);
54   RadioButton2->setPixmap(image1);
55   RadioButton3->close(TRUE);
56
57   GroupPoints = new DlgRef_1Sel1Spin(this, "GroupPoints");
58   GroupPoints->GroupBox1->setTitle(tr("GEOM_SPHERE_CR"));
59   GroupPoints->TextLabel1->setText(tr("GEOM_CENTER"));
60   GroupPoints->TextLabel2->setText(tr("GEOM_RADIUS"));
61   GroupPoints->PushButton1->setPixmap(image2);
62
63   GroupDimensions = new DlgRef_1Spin(this, "GroupDimensions");
64   GroupDimensions->GroupBox1->setTitle(tr("GEOM_SPHERE_RO"));
65   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS"));
66
67   Layout1->addWidget(GroupPoints, 1, 0);
68   Layout1->addWidget(GroupDimensions, 1, 0);
69   /***************************************************************/
70
71   /* Initialisations */
72   myPrimitiveGUI = thePrimitiveGUI;
73   Init();
74 }
75
76
77 //=================================================================================
78 // function : ~PrimitiveGUI_SphereDlg()
79 // purpose  : Destroys the object and frees any allocated resources
80 //=================================================================================
81 PrimitiveGUI_SphereDlg::~PrimitiveGUI_SphereDlg()
82 {  
83   /* no need to delete child widgets, Qt does it all for us */
84 }
85
86
87 //=================================================================================
88 // function : Init()
89 // purpose  :
90 //=================================================================================
91 void PrimitiveGUI_SphereDlg::Init()
92 {
93   /* init variables */
94   myConstructorId = 0;
95   myEditCurrentArgument = GroupPoints->LineEdit1;
96
97   myPoint1.SetCoord(0.0, 0.0, 0.0);
98   myRadius = 100.0;
99   myOkPoint1 = false;
100
101   /*  Vertices Filter for all arguments */
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 */
110   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
111   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
112   GroupPoints->SpinBox_DX->SetValue(myRadius);
113   GroupDimensions->SpinBox_DX->SetValue(myRadius);
114   
115   /* signals and slots connections */
116   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
117   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
118   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
119
120   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
121   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
122
123   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
124   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
125
126   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
127   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
128   
129   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
130
131   /* displays Dialog */
132   GroupDimensions->hide();
133   GroupPoints->show();
134   this->show();
135
136   return;
137 }
138
139
140 //=================================================================================
141 // function : ConstructorsClicked()
142 // purpose  : Radio button management
143 //=================================================================================
144 void PrimitiveGUI_SphereDlg::ConstructorsClicked(int constructorId)
145 {
146   myConstructorId = constructorId;
147   mySelection->ClearFilters();
148   myGeomBase->EraseSimulationShape();
149   disconnect(mySelection, 0, this, 0);
150   myRadius = 100.0;
151
152   switch (constructorId)
153     {
154     case 0:
155       {
156         GroupDimensions->hide();
157         resize(0, 0);
158         GroupPoints->show();
159
160         myEditCurrentArgument = GroupPoints->LineEdit1;
161         GroupPoints->LineEdit1->setText("");
162
163         GroupPoints->SpinBox_DX->SetValue(myRadius);
164         myOkPoint1 = false;
165
166         /* filter for next selections */
167         mySelection->AddFilter(myVertexFilter);
168         connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
169         break;
170       }
171     case 1:
172       {
173         GroupPoints->hide();
174         resize(0, 0);
175         GroupDimensions->show();
176
177         GroupDimensions->SpinBox_DX->SetValue(myRadius);
178         myPoint1.SetCoord(0.0, 0.0, 0.0); /* at origin */
179         myOkPoint1 = true;
180
181         mySimulationTopoDs = BRepPrimAPI_MakeSphere(myPoint1, myRadius).Shape();
182         myGeomBase->DisplaySimulationShape(mySimulationTopoDs); 
183         break;
184       }
185     }
186  return ;
187 }
188
189
190 //=================================================================================
191 // function : ClickOnOk()
192 // purpose  :
193 //=================================================================================
194 void PrimitiveGUI_SphereDlg::ClickOnOk()
195 {
196   this->ClickOnApply();
197   ClickOnCancel();
198   return;
199 }
200
201
202 //=================================================================================
203 // function : ClickOnApply()
204 // purpose  :
205 //=================================================================================
206 void PrimitiveGUI_SphereDlg::ClickOnApply()
207 {
208   buttonApply->setFocus();
209   QAD_Application::getDesktop()->putInfo(tr(""));
210   if(mySimulationTopoDs.IsNull())
211     return;
212   myGeomBase->EraseSimulationShape();
213   mySimulationTopoDs.Nullify();
214
215   if(myOkPoint1)
216     myPrimitiveGUI->MakeSphereAndDisplay(myPoint1, myRadius);
217   return;
218 }
219
220
221 //=================================================================================
222 // function : SelectionIntoArgument()
223 // purpose  : Called when selection as changed or other case
224 //=================================================================================
225 void PrimitiveGUI_SphereDlg::SelectionIntoArgument()
226 {
227   myGeomBase->EraseSimulationShape();
228   myEditCurrentArgument->setText("");
229   QString aString = ""; /* name of selection */
230
231   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
232   if(nbSel != 1) {
233     if(myEditCurrentArgument == GroupPoints->LineEdit1) {
234       GroupPoints->LineEdit1->setText("");
235       myOkPoint1 = false;
236     }
237     return;
238   }
239   
240   /* nbSel == 1 ! */
241   TopoDS_Shape S; 
242   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
243     return;
244     
245   /* Constructor 1 treatment */
246   if(myEditCurrentArgument == GroupPoints->LineEdit1 && myGeomBase->VertexToPoint(S, myPoint1)) {
247     GroupPoints->LineEdit1->setText(aString);
248     myOkPoint1 = true;
249   }
250   
251   if(myOkPoint1) {
252     mySimulationTopoDs = BRepPrimAPI_MakeSphere(myPoint1, myRadius).Shape();
253     myGeomBase->DisplaySimulationShape(mySimulationTopoDs); 
254   }
255   return ;
256 }
257
258
259 //=================================================================================
260 // function : LineEditReturnPressed()
261 // purpose  :
262 //=================================================================================
263 void PrimitiveGUI_SphereDlg::LineEditReturnPressed()
264 {
265   QLineEdit* send = (QLineEdit*)sender();
266   if(send == GroupPoints->LineEdit1)
267     myEditCurrentArgument = GroupPoints->LineEdit1;
268   else
269     return;
270
271   GEOMBase_Skeleton::LineEditReturnPressed();
272   return;
273 }
274
275
276 //=================================================================================
277 // function : SetEditCurrentArgument()
278 // purpose  :
279 //=================================================================================
280 void PrimitiveGUI_SphereDlg::SetEditCurrentArgument()
281 {
282   if(myConstructorId != 0)
283     return;
284
285   QPushButton* send = (QPushButton*)sender();
286
287   if(send == GroupPoints->PushButton1) {
288     GroupPoints->LineEdit1->setFocus();
289     myEditCurrentArgument = GroupPoints->LineEdit1;
290     mySelection->AddFilter(myVertexFilter);
291     this->SelectionIntoArgument();
292   }
293   return;
294 }
295
296
297 //=================================================================================
298 // function : ActivateThisDialog()
299 // purpose  :
300 //=================================================================================
301 void PrimitiveGUI_SphereDlg::ActivateThisDialog()
302 {
303   GEOMBase_Skeleton::ActivateThisDialog();
304   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
305   if(myConstructorId == 0)
306     mySelection->AddFilter(myVertexFilter);
307   if(!mySimulationTopoDs.IsNull())
308     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
309   return;
310 }
311
312
313 //=================================================================================
314 // function : enterEvent()
315 // purpose  :
316 //=================================================================================
317 void PrimitiveGUI_SphereDlg::enterEvent(QEvent* e)
318 {
319   if(GroupConstructors->isEnabled())
320     return;
321   this->ActivateThisDialog();
322   return;
323 }
324
325
326 //=================================================================================
327 // function : ValueChangedInSpinBox()
328 // purpose  :
329 //=================================================================================
330 void PrimitiveGUI_SphereDlg::ValueChangedInSpinBox(double newValue)
331 {
332   myGeomBase->EraseSimulationShape();
333   mySimulationTopoDs.Nullify();
334   myRadius = newValue;
335   
336   if(myOkPoint1) {
337     mySimulationTopoDs = BRepPrimAPI_MakeSphere(myPoint1, myRadius).Shape();
338     myGeomBase->DisplaySimulationShape(mySimulationTopoDs); 
339   }
340   return;
341 }