Salome HOME
e67d4ae54b19085b9f14831a9259dbcfb291a136
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_TorusDlg.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_TorusDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "PrimitiveGUI_TorusDlg.h"
31
32 #include <gp_Lin.hxx>
33 #include <BRepPrimAPI_MakeTorus.hxx>
34 #include <BRepAdaptor_Curve.hxx>
35 #include "QAD_Config.h"
36
37 //=================================================================================
38 // class    : PrimitiveGUI_TorusDlg()
39 // purpose  : Constructs a PrimitiveGUI_TorusDlg which is a child of 'parent', with the 
40 //            name 'name' and widget flags set to 'f'.
41 //            The dialog will by default be modeless, unless you set 'modal' to
42 //            TRUE to construct a modal dialog.
43 //=================================================================================
44 PrimitiveGUI_TorusDlg::PrimitiveGUI_TorusDlg(QWidget* parent, const char* name, PrimitiveGUI* thePrimitiveGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
45   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
46 {
47   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_TORUS_PV")));
48   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_TORUS_DXYZ")));
49   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
50     
51   setCaption(tr("GEOM_TORUS_TITLE"));
52
53   /***************************************************************/
54   GroupConstructors->setTitle(tr("GEOM_TORUS"));
55   RadioButton1->setPixmap(image0);
56   RadioButton2->setPixmap(image1);
57   RadioButton3->close(TRUE);
58
59   GroupPoints = new DlgRef_2Sel2Spin(this, "GroupPoints");
60   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
61   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_POINT"));
62   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
63   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS_I").arg("1"));
64   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_I").arg("2"));
65   GroupPoints->PushButton1->setPixmap(image2);
66   GroupPoints->PushButton2->setPixmap(image2);
67
68   GroupDimensions = new DlgRef_2Spin(this, "GroupDimensions");
69   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
70   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS_I").arg("1"));
71   GroupDimensions->TextLabel2->setText(tr("GEOM_RADIUS_I").arg("2"));
72
73   Layout1->addWidget(GroupPoints, 1, 0);
74   Layout1->addWidget(GroupDimensions, 1, 0);
75   /***************************************************************/
76
77   /* Initialisations */
78   myPrimitiveGUI = thePrimitiveGUI;
79   Init();
80 }
81
82
83 //=================================================================================
84 // function : ~PrimitiveGUI_TorusDlg()
85 // purpose  : Destroys the object and frees any allocated resources
86 //=================================================================================
87 PrimitiveGUI_TorusDlg::~PrimitiveGUI_TorusDlg()
88 {
89     // no need to delete child widgets, Qt does it all for us
90 }
91
92
93 //=================================================================================
94 // function : Init()
95 // purpose  :
96 //=================================================================================
97 void PrimitiveGUI_TorusDlg::Init()
98 {
99   /* init variables */
100   myConstructorId = 0;
101   myEditCurrentArgument = GroupPoints->LineEdit1;
102
103   myRadius1 = 300.0;
104   myRadius2 = 100.0;
105   myOkRadius1 = myOkRadius2 = true;
106   myOkPoint1 = myOkDir = false;
107
108   myEdgeFilter = new GEOM_EdgeFilter(StdSelect_Line, myGeom);
109   myVertexFilter = new GEOM_ShapeTypeFilter(TopAbs_VERTEX, myGeom);
110   mySelection->AddFilter(myVertexFilter);
111
112   /* Get setting of step value from file configuration */
113   QString St = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
114   step = St.toDouble();
115
116   /* min, max, step and decimals for spin boxes & initial values */
117   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
118   GroupPoints->SpinBox_DY->RangeStepAndValidator(0.001, 999.999, step, 3);
119   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
120   GroupDimensions->SpinBox_DY->RangeStepAndValidator(0.001, 999.999, step, 3);
121
122   GroupPoints->SpinBox_DX->SetValue(myRadius1);
123   GroupPoints->SpinBox_DY->SetValue(myRadius2);
124   GroupDimensions->SpinBox_DX->SetValue(myRadius1);
125   GroupDimensions->SpinBox_DY->SetValue(myRadius2);
126
127   /* signals and slots connections */
128   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
129   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
130   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
131
132   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
133   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
134
135   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
136   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
137
138   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
139   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
140   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
141   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
142
143   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
144   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
145   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
146   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DY, SLOT(SetStep(double)));
147   
148   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
149
150   /* displays Dialog */
151   GroupDimensions->hide();
152   GroupPoints->show();
153   this->show();
154
155   return;
156 }
157
158
159
160 //=================================================================================
161 // function : ConstructorsClicked()
162 // purpose  : Radio button management
163 //=================================================================================
164 void PrimitiveGUI_TorusDlg::ConstructorsClicked(int constructorId)
165 {
166   myConstructorId = constructorId;
167   mySelection->ClearFilters();
168   myGeomBase->EraseSimulationShape();
169   disconnect(mySelection, 0, this, 0);
170   myOkRadius1 = myOkRadius2 = true;
171   myRadius1 = 300.0;
172   myRadius2 = 100.0;
173
174   switch(myConstructorId)
175     { 
176     case 0 :
177       {
178         GroupDimensions->hide();
179         resize(0, 0);
180         GroupPoints->show();
181         myOkPoint1 = myOkDir = false;
182
183         myEditCurrentArgument = GroupPoints->LineEdit1;
184         GroupPoints->LineEdit1->setText(tr(""));
185         GroupPoints->LineEdit2->setText(tr(""));
186         
187         GroupPoints->SpinBox_DX->SetValue(myRadius1);
188         GroupPoints->SpinBox_DY->SetValue(myRadius2);
189
190         /* filter for next selection */
191         mySelection->AddFilter(myVertexFilter);
192         connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
193         break ;
194       }
195     case 1 :
196       {
197         GroupPoints->hide();
198         resize( 0, 0 );
199         GroupDimensions->show();
200         myOkPoint1 = myOkDir = true;
201
202         GroupDimensions->SpinBox_DX->SetValue(myRadius1);
203         GroupDimensions->SpinBox_DY->SetValue(myRadius2);
204         myPoint1.SetCoord(0.0, 0.0, 0.0);
205         myDir.SetCoord(0.0, 0.0, 1.0);
206
207         MakeTorusSimulationAndDisplay();
208         break ;
209       }
210     }
211   return ;
212 }
213
214
215 //=================================================================================
216 // function : ClickOnOk()
217 // purpose  :
218 //=================================================================================
219 void PrimitiveGUI_TorusDlg::ClickOnOk()
220 {
221   this->ClickOnApply();
222   ClickOnCancel();
223   return;
224 }
225
226
227 //=================================================================================
228 // function : ClickOnApply()
229 // purpose  :
230 //=================================================================================
231 void PrimitiveGUI_TorusDlg::ClickOnApply()
232 {
233   QAD_Application::getDesktop()->putInfo(tr(""));
234   if (mySimulationTopoDs.IsNull())
235     return;
236   myGeomBase->EraseSimulationShape();
237   mySimulationTopoDs.Nullify();
238
239   if(myOkPoint1 && myOkDir && myOkRadius1 && myOkRadius2 )        
240     myPrimitiveGUI->MakeTorusAndDisplay(myPoint1, myDir, myRadius1, myRadius2); 
241   return ;
242 }
243
244
245 //=================================================================================
246 // function : SelectionIntoArgument()
247 // purpose  : Called when selection as changed or other case
248 //=================================================================================
249 void PrimitiveGUI_TorusDlg::SelectionIntoArgument()
250 {
251   myGeomBase->EraseSimulationShape(); 
252   myEditCurrentArgument->setText("");
253   QString aString = ""; /* name of selection */
254   
255   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
256   if(nbSel != 1) {
257     if(myEditCurrentArgument == GroupPoints->LineEdit1)
258       myOkPoint1 = false;
259     else if(myEditCurrentArgument == GroupPoints->LineEdit2)
260       myOkDir = false;
261     return;
262   }
263
264   /* nbSel == 1 */
265   TopoDS_Shape S;  
266   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
267     return;  
268  
269   /*  gp_Pnt : not used */
270   if(myEditCurrentArgument == GroupPoints->LineEdit1 && myGeomBase->VertexToPoint(S, myPoint1)) {
271     GroupPoints->LineEdit1->setText(aString);
272     myOkPoint1 = true;
273   }    
274   else if(myEditCurrentArgument == GroupPoints->LineEdit2) {
275     BRepAdaptor_Curve curv(TopoDS::Edge(S));
276     myDir = curv.Line().Direction();
277     GroupPoints->LineEdit2->setText(aString);
278     myOkDir = true;
279   }
280
281   if(myOkPoint1 && myOkDir && myOkRadius1 && myOkRadius2)
282     MakeTorusSimulationAndDisplay();
283   return;
284 }
285
286
287 //=================================================================================
288 // function : SetEditCurrentArgument()
289 // purpose  :
290 //=================================================================================
291 void PrimitiveGUI_TorusDlg::SetEditCurrentArgument()
292 {
293   if(myConstructorId != 0)
294     return;
295
296   QPushButton* send = (QPushButton*)sender();
297   mySelection->ClearFilters();
298
299   if(send == GroupPoints->PushButton1) {
300     GroupPoints->LineEdit1->setFocus();
301     myEditCurrentArgument = GroupPoints->LineEdit1;
302     mySelection->AddFilter(myVertexFilter);
303   }
304   else if(send == GroupPoints->PushButton2) {
305     GroupPoints->LineEdit2->setFocus();
306     myEditCurrentArgument = GroupPoints->LineEdit2;
307     mySelection->AddFilter(myEdgeFilter);
308   }
309   this->SelectionIntoArgument();
310
311   return;
312 }
313
314
315 //=================================================================================
316 // function : LineEditReturnPressed()
317 // purpose  :
318 //=================================================================================
319 void PrimitiveGUI_TorusDlg::LineEditReturnPressed()
320 {  
321   QLineEdit* send = (QLineEdit*)sender();
322   if(send == GroupPoints->LineEdit1)
323     myEditCurrentArgument = GroupPoints->LineEdit1;
324   else if (send == GroupPoints->LineEdit2)
325     myEditCurrentArgument = GroupPoints->LineEdit2;
326   else
327     return;
328
329   GEOMBase_Skeleton::LineEditReturnPressed();
330   return;
331 }
332
333
334 //=================================================================================
335 // function : ActivateThisDialog()
336 // purpose  :
337 //=================================================================================
338 void PrimitiveGUI_TorusDlg::ActivateThisDialog()
339 {
340   GEOMBase_Skeleton::ActivateThisDialog();
341   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
342   if(myConstructorId == 1) {
343     GroupPoints->LineEdit1->setFocus();
344     myEditCurrentArgument = GroupPoints->LineEdit1;
345     mySelection->AddFilter(myVertexFilter);
346   }
347   if(!mySimulationTopoDs.IsNull())
348     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
349   return;
350 }
351
352
353 //=================================================================================
354 // function : enterEvent()
355 // purpose  :
356 //=================================================================================
357 void PrimitiveGUI_TorusDlg::enterEvent(QEvent* e)
358 {
359   if (GroupConstructors->isEnabled())
360     return;
361   this->ActivateThisDialog();
362   return;
363 }
364
365
366 //=================================================================================
367 // function : ValueChangedInSpinBox
368 // purpose  :
369 //=================================================================================
370 void PrimitiveGUI_TorusDlg::ValueChangedInSpinBox(double newValue)
371 {
372   myGeomBase->EraseSimulationShape();
373   mySimulationTopoDs.Nullify();
374   QObject* send = (QObject*)sender();
375   
376   if(send == GroupPoints->SpinBox_DX || send == GroupDimensions->SpinBox_DX) {
377     myRadius1 = newValue;
378     myOkRadius1 = true;
379   }
380   else if(send == GroupPoints->SpinBox_DY || send == GroupDimensions->SpinBox_DY) {
381     myRadius2 = newValue;
382     myOkRadius2 = true;
383   }
384   
385   if(myOkPoint1 && myOkDir && myOkRadius1 && myOkRadius2)
386     MakeTorusSimulationAndDisplay();
387   return;
388 }
389
390
391 //=================================================================================
392 // function : MakeTorusSimulationAndDisplay()
393 // purpose  :
394 //=================================================================================
395 void PrimitiveGUI_TorusDlg::MakeTorusSimulationAndDisplay() 
396 {
397   myGeomBase->EraseSimulationShape();
398   mySimulationTopoDs.Nullify();
399
400   try {
401     gp_Ax2 anAxis(myPoint1, myDir);
402     mySimulationTopoDs = BRepPrimAPI_MakeTorus(anAxis, myRadius1, myRadius2).Shape();
403     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
404   }
405   catch(Standard_Failure) {
406     MESSAGE("Exception catched in MakeTorusSimulationAndDisplay");
407   }
408   return;
409 }