Salome HOME
DCQ : New Architecture
[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   QAD_Application::getDesktop()->putInfo(tr(""));
209   if(mySimulationTopoDs.IsNull())
210     return;
211   myGeomBase->EraseSimulationShape();
212   mySimulationTopoDs.Nullify();
213
214   if(myOkPoint1)
215     myPrimitiveGUI->MakeSphereAndDisplay(myPoint1, myRadius);
216   return;
217 }
218
219
220 //=================================================================================
221 // function : SelectionIntoArgument()
222 // purpose  : Called when selection as changed or other case
223 //=================================================================================
224 void PrimitiveGUI_SphereDlg::SelectionIntoArgument()
225 {
226   myGeomBase->EraseSimulationShape();
227   myEditCurrentArgument->setText("");
228   QString aString = ""; /* name of selection */
229
230   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
231   if(nbSel != 1) {
232     if(myEditCurrentArgument == GroupPoints->LineEdit1) {
233       GroupPoints->LineEdit1->setText("");
234       myOkPoint1 = false;
235     }
236     return;
237   }
238   
239   /* nbSel == 1 ! */
240   TopoDS_Shape S; 
241   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
242     return;
243     
244   /* Constructor 1 treatment */
245   if(myEditCurrentArgument == GroupPoints->LineEdit1 && myGeomBase->VertexToPoint(S, myPoint1)) {
246     GroupPoints->LineEdit1->setText(aString);
247     myOkPoint1 = true;
248   }
249   
250   if(myOkPoint1) {
251     mySimulationTopoDs = BRepPrimAPI_MakeSphere(myPoint1, myRadius).Shape();
252     myGeomBase->DisplaySimulationShape(mySimulationTopoDs); 
253   }
254   return ;
255 }
256
257
258 //=================================================================================
259 // function : LineEditReturnPressed()
260 // purpose  :
261 //=================================================================================
262 void PrimitiveGUI_SphereDlg::LineEditReturnPressed()
263 {
264   QLineEdit* send = (QLineEdit*)sender();
265   if(send == GroupPoints->LineEdit1)
266     myEditCurrentArgument = GroupPoints->LineEdit1;
267   else
268     return;
269
270   GEOMBase_Skeleton::LineEditReturnPressed();
271   return;
272 }
273
274
275 //=================================================================================
276 // function : SetEditCurrentArgument()
277 // purpose  :
278 //=================================================================================
279 void PrimitiveGUI_SphereDlg::SetEditCurrentArgument()
280 {
281   if(myConstructorId != 0)
282     return;
283
284   QPushButton* send = (QPushButton*)sender();
285
286   if(send == GroupPoints->PushButton1) {
287     GroupPoints->LineEdit1->setFocus();
288     myEditCurrentArgument = GroupPoints->LineEdit1;
289     mySelection->AddFilter(myVertexFilter);
290     this->SelectionIntoArgument();
291   }
292   return;
293 }
294
295
296 //=================================================================================
297 // function : ActivateThisDialog()
298 // purpose  :
299 //=================================================================================
300 void PrimitiveGUI_SphereDlg::ActivateThisDialog()
301 {
302   GEOMBase_Skeleton::ActivateThisDialog();
303   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
304   if(myConstructorId == 0)
305     mySelection->AddFilter(myVertexFilter);
306   if(!mySimulationTopoDs.IsNull())
307     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
308   return;
309 }
310
311
312 //=================================================================================
313 // function : enterEvent()
314 // purpose  :
315 //=================================================================================
316 void PrimitiveGUI_SphereDlg::enterEvent(QEvent* e)
317 {
318   if(GroupConstructors->isEnabled())
319     return;
320   this->ActivateThisDialog();
321   return;
322 }
323
324
325 //=================================================================================
326 // function : ValueChangedInSpinBox()
327 // purpose  :
328 //=================================================================================
329 void PrimitiveGUI_SphereDlg::ValueChangedInSpinBox(double newValue)
330 {
331   myGeomBase->EraseSimulationShape();
332   mySimulationTopoDs.Nullify();
333   myRadius = newValue;
334   
335   if(myOkPoint1) {
336     mySimulationTopoDs = BRepPrimAPI_MakeSphere(myPoint1, myRadius).Shape();
337     myGeomBase->DisplaySimulationShape(mySimulationTopoDs); 
338   }
339   return;
340 }