]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_VectorDlg.cxx
Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/geom.git] / src / BasicGUI / BasicGUI_VectorDlg.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_VectorDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "BasicGUI_VectorDlg.h"
31
32 #include "QAD_Config.h"
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Compound.hxx>
35 #include <BRepBuilderAPI_MakeEdge.hxx>
36 #include <BRep_Builder.hxx>
37 #include <Precision.hxx>
38
39 //=================================================================================
40 // class    : BasicGUI_VectorDlg()
41 // purpose  : Constructs a BasicGUI_VectorDlg which is a child of 'parent', with the 
42 //            name 'name' and widget flags set to 'f'.
43 //            The dialog will by default be modeless, unless you set 'modal' to
44 //            TRUE to construct a modal dialog.
45 //=================================================================================
46 BasicGUI_VectorDlg::BasicGUI_VectorDlg(QWidget* parent, const char* name, BasicGUI* theBasicGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
47   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
48 {
49   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_VECTOR_2P")));
50   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_VECTOR_DXYZ")));
51   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
52
53   setCaption(tr("GEOM_VECTOR_TITLE"));
54
55   /***************************************************************/
56   GroupConstructors->setTitle(tr("GEOM_VECTOR"));
57   RadioButton1->setPixmap(image0);
58   RadioButton2->setPixmap(image1);
59   RadioButton3->close(TRUE);
60
61   GroupPoints = new DlgRef_2Sel_QTD(this, "GroupPoints");
62   GroupPoints->GroupBox1->setTitle(tr("GEOM_POINTS"));
63   GroupPoints->TextLabel1->setText(tr("GEOM_POINT_I").arg("1"));
64   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_I").arg("2"));
65   GroupPoints->PushButton1->setPixmap(image2);
66   GroupPoints->PushButton2->setPixmap(image2);
67
68   GroupDimensions = new DlgRef_3Spin1Check(this, "GroupDimensions");
69   GroupDimensions->GroupBox1->setTitle(tr("GEOM_COORDINATES"));
70   GroupDimensions->TextLabel1->setText(tr("GEOM_DX"));
71   GroupDimensions->TextLabel2->setText(tr("GEOM_DY"));
72   GroupDimensions->TextLabel3->setText(tr("GEOM_DZ"));
73   GroupDimensions->CheckBox1->setText(tr("GEOM_REVERSE_VECTOR"));
74
75   Layout1->addWidget(GroupPoints, 1, 0);
76   Layout1->addWidget(GroupDimensions, 1, 0);
77   /***************************************************************/
78
79   /* Initialisations */
80   myBasicGUI = theBasicGUI;
81   Init();
82 }
83
84
85 //=================================================================================
86 // function : ~BasicGUI_VectorDlg()
87 // purpose  : Destroys the object and frees any allocated resources
88 //=================================================================================
89 BasicGUI_VectorDlg::~BasicGUI_VectorDlg()
90 {  
91   /* no need to delete child widgets, Qt does it all for us */
92 }
93
94
95 //=================================================================================
96 // function : Init()
97 // purpose  :
98 //=================================================================================
99 void BasicGUI_VectorDlg::Init()
100 {
101     /* init variables */
102   myConstructorId = 0;
103   myEditCurrentArgument = GroupPoints->LineEdit1;
104
105   myDx = 0.0;
106   myDy = 0.0;
107   myDz = 200.0;
108   myPoint1.SetCoord(0.0, 0.0, 0.0);
109   myPoint2.SetCoord(0.0, 0.0, 0.0);
110   myOkPoint1 = myOkPoint2 = false;
111
112   /*  Vertices Filter for all arguments */
113   myVertexFilter = new GEOM_ShapeTypeFilter(TopAbs_VERTEX, myGeom);
114   mySelection->AddFilter(myVertexFilter);
115
116   /* Get setting of step value from file configuration */
117   QString St = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
118   step = St.toDouble();
119
120   /* min, max, step and decimals for spin boxes */
121   GroupDimensions->SpinBox_DX->RangeStepAndValidator(-999.999, 999.999, step, 3);
122   GroupDimensions->SpinBox_DY->RangeStepAndValidator(-999.999, 999.999, step, 3);
123   GroupDimensions->SpinBox_DZ->RangeStepAndValidator(-999.999, 999.999, step, 3);
124
125   GroupDimensions->SpinBox_DX->SetValue(myDx);
126   GroupDimensions->SpinBox_DY->SetValue(myDy);
127   GroupDimensions->SpinBox_DZ->SetValue(myDz);
128
129   GroupDimensions->CheckBox1->setChecked(FALSE);
130
131   /* signals and slots connections */
132   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
133   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
134   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
135
136   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
137   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
138
139   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
140   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
141
142   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
143   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
144   connect(GroupDimensions->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
145
146   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
147   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DY, SLOT(SetStep(double)));
148   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DZ, SLOT(SetStep(double)));
149
150   connect(GroupDimensions->CheckBox1, SIGNAL(stateChanged(int)), this, SLOT(ReverseVector(int)));
151
152   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
153
154   /* displays Dialog */
155   GroupDimensions->hide();
156   GroupPoints->show();
157   this->show();
158
159   return;
160 }
161
162
163 //=================================================================================
164 // function : ConstructorsClicked()
165 // purpose  : Radio button management
166 //=================================================================================
167 void BasicGUI_VectorDlg::ConstructorsClicked(int constructorId)
168 {
169   myConstructorId = constructorId;
170   mySelection->ClearFilters();
171   myGeomBase->EraseSimulationShape();
172   disconnect(mySelection, 0, this, 0);
173   myOkPoint1 = myOkPoint2 = false;
174
175   switch (constructorId)
176     {
177     case 0:
178       {
179         GroupDimensions->hide();
180         resize(0, 0);
181         GroupPoints->show();
182
183         myEditCurrentArgument = GroupPoints->LineEdit1;
184         GroupPoints->LineEdit1->setText("");
185         GroupPoints->LineEdit2->setText("");
186
187         /* filter for next selection */
188         mySelection->AddFilter(myVertexFilter);
189         connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
190         break;
191       }
192     case 1:
193       {
194         GroupPoints->hide();
195         resize( 0, 0 );
196         GroupDimensions->show();
197
198         myDx = 0.0;
199         myDy = 0.0;
200         myDz = 200.0;
201
202         GroupDimensions->SpinBox_DX->SetValue(myDx);
203         GroupDimensions->SpinBox_DY->SetValue(myDy);
204         GroupDimensions->SpinBox_DZ->SetValue(myDz);
205
206         myPoint1.SetCoord(0.0, 0.0, 0.0) ;
207         myPoint2.SetCoord(myDx, myDy, myDz);
208
209         GroupDimensions->CheckBox1->setChecked(FALSE);
210
211         mySimulationTopoDs = BRepBuilderAPI_MakeEdge(myPoint1, myPoint2).Shape();
212         /* Add arrow in simulation */
213         bool noNeedToTest = AddArrowToSimulation(mySimulationTopoDs);
214         myGeomBase->DisplaySimulationShape(mySimulationTopoDs); 
215         break;
216       }
217     }
218  return;
219 }
220
221
222 //=================================================================================
223 // function : ClickOnOk()
224 // purpose  :
225 //=================================================================================
226 void BasicGUI_VectorDlg::ClickOnOk()
227 {
228   this->ClickOnApply();
229   ClickOnCancel();
230   return;
231 }
232
233
234 //=================================================================================
235 // function : ClickOnApply()
236 // purpose  :
237 //=================================================================================
238 void BasicGUI_VectorDlg::ClickOnApply()
239 {
240   buttonApply->setFocus();
241   QAD_Application::getDesktop()->putInfo(tr(""));
242   if (mySimulationTopoDs.IsNull())
243     return;
244   myGeomBase->EraseSimulationShape();
245   mySimulationTopoDs.Nullify();
246
247   switch(myConstructorId)
248     { 
249     case 0 :
250       { 
251         if(myOkPoint1 && myOkPoint2) 
252           myBasicGUI->MakeVectorAndDisplay(myPoint1, myPoint2);
253         break ;
254       }
255     case 1 :
256       {
257         /* Recup args and call method */
258         myDx = GroupDimensions->SpinBox_DX->GetValue();
259         myDy = GroupDimensions->SpinBox_DY->GetValue();
260         myDz = GroupDimensions->SpinBox_DZ->GetValue();
261         myPoint1.SetCoord(0.0, 0.0, 0.0);
262         myPoint2.SetCoord(myDx, myDy, myDz);    
263         myBasicGUI->MakeVectorAndDisplay(myPoint1, myPoint2);
264         break;
265       }
266     }
267   return;
268 }
269
270
271 //=================================================================================
272 // function : SelectionIntoArgument()
273 // purpose  : Called when selection as changed or other case
274 //=================================================================================
275 void BasicGUI_VectorDlg::SelectionIntoArgument()
276 {
277   myGeomBase->EraseSimulationShape();
278   myEditCurrentArgument->setText("");
279   QString aString = ""; /* name of selection */
280
281   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
282   if (nbSel != 1) {
283     if (myEditCurrentArgument == GroupPoints->LineEdit1)
284       myOkPoint1 = false;
285     else if ( myEditCurrentArgument == GroupPoints->LineEdit2)
286       myOkPoint2 = false;
287     return;
288   }
289
290   // nbSel == 1
291   TopoDS_Shape S; 
292   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
293     return;
294
295   if(myEditCurrentArgument == GroupPoints->LineEdit1 && myGeomBase->VertexToPoint(S, myPoint1)) {
296     myEditCurrentArgument->setText(aString);
297     myOkPoint1 = true;
298   }
299   else if(myEditCurrentArgument == GroupPoints->LineEdit2 && myGeomBase->VertexToPoint(S, myPoint2)) {
300     myEditCurrentArgument->setText(aString);
301     myOkPoint2 = true;
302   }
303
304   if(myOkPoint1 && myOkPoint2 && myPoint1.Distance(myPoint2) > Precision::Confusion()) {
305     mySimulationTopoDs = BRepBuilderAPI_MakeEdge(myPoint1, myPoint2).Shape();
306     /* Add arrow in simulation */
307     bool noNeedToTest = this->AddArrowToSimulation(mySimulationTopoDs);
308     myGeomBase->DisplaySimulationShape(mySimulationTopoDs); 
309   }
310   return;
311 }
312
313
314 //=================================================================================
315 // function : SetEditCurrentArgument()
316 // purpose  :
317 //=================================================================================
318 void BasicGUI_VectorDlg::SetEditCurrentArgument()
319 {
320   if(myConstructorId != 0)
321     return;
322
323   QPushButton* send = (QPushButton*)sender();
324
325   if(send == GroupPoints->PushButton1) {
326     GroupPoints->LineEdit1->setFocus();
327     myEditCurrentArgument = GroupPoints->LineEdit1;
328   }
329   else if(send == GroupPoints->PushButton2) {
330     GroupPoints->LineEdit2->setFocus();
331     myEditCurrentArgument = GroupPoints->LineEdit2;
332   }
333   mySelection->AddFilter(myVertexFilter);
334   this->SelectionIntoArgument();
335
336   return;
337 }
338
339
340 //=================================================================================
341 // function : LineEditReturnPressed()
342 // purpose  :
343 //=================================================================================
344 void BasicGUI_VectorDlg::LineEditReturnPressed()
345
346   QLineEdit* send = (QLineEdit*)sender();
347   if(send == GroupPoints->LineEdit1)
348     myEditCurrentArgument = GroupPoints->LineEdit1;
349   else if (send == GroupPoints->LineEdit2)
350     myEditCurrentArgument = GroupPoints->LineEdit2;
351   else
352     return;
353
354   GEOMBase_Skeleton::LineEditReturnPressed();
355   return;
356 }
357
358
359 //=================================================================================
360 // function : ActivateThisDialog()
361 // purpose  :
362 //=================================================================================
363 void BasicGUI_VectorDlg::ActivateThisDialog()
364 {
365   GEOMBase_Skeleton::ActivateThisDialog();
366   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
367   if(myConstructorId == 0)
368     mySelection->AddFilter(myVertexFilter);
369   if(!mySimulationTopoDs.IsNull())
370     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
371   return;
372 }
373
374
375 //=================================================================================
376 // function : enterEvent()
377 // purpose  :
378 //=================================================================================
379 void BasicGUI_VectorDlg::enterEvent(QEvent* e)
380 {
381   if(GroupConstructors->isEnabled())
382     return;
383   this->ActivateThisDialog();
384   return;
385 }
386
387
388 //=================================================================================
389 // function : ValueChangedInSpinBox()
390 // purpose  :
391 //=================================================================================
392 void BasicGUI_VectorDlg::ValueChangedInSpinBox(double newValue)
393 {
394   myGeomBase->EraseSimulationShape();
395   mySimulationTopoDs.Nullify();
396   QObject* send = (QObject*)sender();
397
398   if(send == GroupDimensions->SpinBox_DX)
399     myDx = newValue;
400   else if (send == GroupDimensions->SpinBox_DY)
401     myDy = newValue;
402   else if (send == GroupDimensions->SpinBox_DZ)
403     myDz = newValue;
404
405   myPoint1.SetCoord(0.0, 0.0, 0.0);
406   myPoint2.SetCoord(myDx, myDy, myDz);
407   
408   if(myPoint1.Distance(myPoint2) > Precision::Confusion()) { // mySimulationTopoDs
409     mySimulationTopoDs = BRepBuilderAPI_MakeEdge(myPoint1, myPoint2).Shape();
410     /* Create simulation vector with an arrow */
411     this->AddArrowToSimulation(mySimulationTopoDs);
412     myGeomBase->DisplaySimulationShape(mySimulationTopoDs); 
413   }
414   return;
415 }
416
417
418 //=================================================================================
419 // function : AddArrowToSimulation()
420 // purpose  : An arrow (cone topology) is added to 'modifiedShape'
421 //          : to simulate a vector or an 'oriented line' display. The result is in 'modifiedShape'.
422 //          : If an arrow can't be added returns false and 'modifiedShape' isn't modified !
423 //=================================================================================
424 bool BasicGUI_VectorDlg::AddArrowToSimulation(TopoDS_Shape& modifiedShape)
425 {
426   TopoDS_Shape arrow;
427   /* Try to add a cone simulation shape to show direction of a linear edge */
428   if(myGeomBase->CreateArrowForLinearEdge(modifiedShape, arrow)) {
429     TopoDS_Compound Comp;
430     BRep_Builder B;
431     B.MakeCompound (Comp);
432     B.Add(Comp, modifiedShape);
433     B.Add(Comp, arrow);
434     modifiedShape = Comp;
435     return true;
436   }
437   return false;
438 }
439
440
441 //=================================================================================
442 // function : ReverseVector()
443 // purpose  : 'state' not used here
444 //=================================================================================
445 void BasicGUI_VectorDlg::ReverseVector(int state)
446 {
447   myGeomBase->EraseSimulationShape(); 
448   mySimulationTopoDs.Nullify();
449
450   myDx = -myDx;
451   GroupDimensions->SpinBox_DX->SetValue(myDx);
452   myDy = -myDy;
453   GroupDimensions->SpinBox_DY->SetValue(myDy);
454   myDz = -myDz;
455   GroupDimensions->SpinBox_DZ->SetValue(myDz);
456   
457   myPoint1.SetCoord(0.0, 0.0, 0.0);
458   myPoint2.SetCoord(myDx, myDy, myDz);
459   
460   /* In the appropriate constructor */
461   if(myPoint1.Distance(myPoint2) > Precision::Confusion()) {
462     mySimulationTopoDs = BRepBuilderAPI_MakeEdge(myPoint1, myPoint2).Shape();
463     /* Add arrow in simulation */
464     bool noNeedToTest = this->AddArrowToSimulation(mySimulationTopoDs);
465     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
466   }
467   return;
468 }