]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_VectorDlg.cxx
Salome HOME
DCQ : New Archi
[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   QAD_Application::getDesktop()->putInfo(tr(""));
241   if (mySimulationTopoDs.IsNull())
242     return;
243   myGeomBase->EraseSimulationShape();
244   mySimulationTopoDs.Nullify();
245
246   switch(myConstructorId)
247     { 
248     case 0 :
249       { 
250         if(myOkPoint1 && myOkPoint2) 
251           myBasicGUI->MakeVectorAndDisplay(myPoint1, myPoint2);
252         break ;
253       }
254     case 1 :
255       {
256         /* Recup args and call method */
257         myDx = GroupDimensions->SpinBox_DX->GetValue();
258         myDy = GroupDimensions->SpinBox_DY->GetValue();
259         myDz = GroupDimensions->SpinBox_DZ->GetValue();
260         myPoint1.SetCoord(0.0, 0.0, 0.0);
261         myPoint2.SetCoord(myDx, myDy, myDz);    
262         myBasicGUI->MakeVectorAndDisplay(myPoint1, myPoint2);
263         break;
264       }
265     }
266   return;
267 }
268
269
270 //=================================================================================
271 // function : SelectionIntoArgument()
272 // purpose  : Called when selection as changed or other case
273 //=================================================================================
274 void BasicGUI_VectorDlg::SelectionIntoArgument()
275 {
276   myGeomBase->EraseSimulationShape();
277   myEditCurrentArgument->setText("");
278   QString aString = ""; /* name of selection */
279
280   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
281   if (nbSel != 1) {
282     if (myEditCurrentArgument == GroupPoints->LineEdit1)
283       myOkPoint1 = false;
284     else if ( myEditCurrentArgument == GroupPoints->LineEdit2)
285       myOkPoint2 = false;
286     return;
287   }
288
289   // nbSel == 1
290   TopoDS_Shape S; 
291   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
292     return;
293
294   if(myEditCurrentArgument == GroupPoints->LineEdit1 && myGeomBase->VertexToPoint(S, myPoint1)) {
295     myEditCurrentArgument->setText(aString);
296     myOkPoint1 = true;
297   }
298   else if(myEditCurrentArgument == GroupPoints->LineEdit2 && myGeomBase->VertexToPoint(S, myPoint2)) {
299     myEditCurrentArgument->setText(aString);
300     myOkPoint2 = true;
301   }
302
303   if(myOkPoint1 && myOkPoint2 && myPoint1.Distance(myPoint2) > Precision::Confusion()) {
304     mySimulationTopoDs = BRepBuilderAPI_MakeEdge(myPoint1, myPoint2).Shape();
305     /* Add arrow in simulation */
306     bool noNeedToTest = this->AddArrowToSimulation(mySimulationTopoDs);
307     myGeomBase->DisplaySimulationShape(mySimulationTopoDs); 
308   }
309   return;
310 }
311
312
313 //=================================================================================
314 // function : SetEditCurrentArgument()
315 // purpose  :
316 //=================================================================================
317 void BasicGUI_VectorDlg::SetEditCurrentArgument()
318 {
319   if(myConstructorId != 0)
320     return;
321
322   QPushButton* send = (QPushButton*)sender();
323
324   if(send == GroupPoints->PushButton1) {
325     GroupPoints->LineEdit1->setFocus();
326     myEditCurrentArgument = GroupPoints->LineEdit1;
327   }
328   else if(send == GroupPoints->PushButton2) {
329     GroupPoints->LineEdit2->setFocus();
330     myEditCurrentArgument = GroupPoints->LineEdit2;
331   }
332   mySelection->AddFilter(myVertexFilter);
333   this->SelectionIntoArgument();
334
335   return;
336 }
337
338
339 //=================================================================================
340 // function : LineEditReturnPressed()
341 // purpose  :
342 //=================================================================================
343 void BasicGUI_VectorDlg::LineEditReturnPressed()
344
345   QLineEdit* send = (QLineEdit*)sender();
346   if(send == GroupPoints->LineEdit1)
347     myEditCurrentArgument = GroupPoints->LineEdit1;
348   else if (send == GroupPoints->LineEdit2)
349     myEditCurrentArgument = GroupPoints->LineEdit2;
350   else
351     return;
352
353   GEOMBase_Skeleton::LineEditReturnPressed();
354   return;
355 }
356
357
358 //=================================================================================
359 // function : ActivateThisDialog()
360 // purpose  :
361 //=================================================================================
362 void BasicGUI_VectorDlg::ActivateThisDialog()
363 {
364   GEOMBase_Skeleton::ActivateThisDialog();
365   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
366   if(myConstructorId == 0)
367     mySelection->AddFilter(myVertexFilter);
368   if(!mySimulationTopoDs.IsNull())
369     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
370   return;
371 }
372
373
374 //=================================================================================
375 // function : enterEvent()
376 // purpose  :
377 //=================================================================================
378 void BasicGUI_VectorDlg::enterEvent(QEvent* e)
379 {
380   if(GroupConstructors->isEnabled())
381     return;
382   this->ActivateThisDialog();
383   return;
384 }
385
386
387 //=================================================================================
388 // function : ValueChangedInSpinBox()
389 // purpose  :
390 //=================================================================================
391 void BasicGUI_VectorDlg::ValueChangedInSpinBox(double newValue)
392 {
393   myGeomBase->EraseSimulationShape();
394   mySimulationTopoDs.Nullify();
395   QObject* send = (QObject*)sender();
396
397   if(send == GroupDimensions->SpinBox_DX)
398     myDx = newValue;
399   else if (send == GroupDimensions->SpinBox_DY)
400     myDy = newValue;
401   else if (send == GroupDimensions->SpinBox_DZ)
402     myDz = newValue;
403
404   myPoint1.SetCoord(0.0, 0.0, 0.0);
405   myPoint2.SetCoord(myDx, myDy, myDz);
406   
407   if(myPoint1.Distance(myPoint2) > Precision::Confusion()) { // mySimulationTopoDs
408     mySimulationTopoDs = BRepBuilderAPI_MakeEdge(myPoint1, myPoint2).Shape();
409     /* Create simulation vector with an arrow */
410     this->AddArrowToSimulation(mySimulationTopoDs);
411     myGeomBase->DisplaySimulationShape(mySimulationTopoDs); 
412   }
413   return;
414 }
415
416
417 //=================================================================================
418 // function : AddArrowToSimulation()
419 // purpose  : An arrow (cone topology) is added to 'modifiedShape'
420 //          : to simulate a vector or an 'oriented line' display. The result is in 'modifiedShape'.
421 //          : If an arrow can't be added returns false and 'modifiedShape' isn't modified !
422 //=================================================================================
423 bool BasicGUI_VectorDlg::AddArrowToSimulation(TopoDS_Shape& modifiedShape)
424 {
425   TopoDS_Shape arrow;
426   /* Try to add a cone simulation shape to show direction of a linear edge */
427   if(myGeomBase->CreateArrowForLinearEdge(modifiedShape, arrow)) {
428     TopoDS_Compound Comp;
429     BRep_Builder B;
430     B.MakeCompound (Comp);
431     B.Add(Comp, modifiedShape);
432     B.Add(Comp, arrow);
433     modifiedShape = Comp;
434     return true;
435   }
436   return false;
437 }
438
439
440 //=================================================================================
441 // function : ReverseVector()
442 // purpose  : 'state' not used here
443 //=================================================================================
444 void BasicGUI_VectorDlg::ReverseVector(int state)
445 {
446   myGeomBase->EraseSimulationShape(); 
447   mySimulationTopoDs.Nullify();
448
449   myDx = -myDx;
450   GroupDimensions->SpinBox_DX->SetValue(myDx);
451   myDy = -myDy;
452   GroupDimensions->SpinBox_DY->SetValue(myDy);
453   myDz = -myDz;
454   GroupDimensions->SpinBox_DZ->SetValue(myDz);
455   
456   myPoint1.SetCoord(0.0, 0.0, 0.0);
457   myPoint2.SetCoord(myDx, myDy, myDz);
458   
459   /* In the appropriate constructor */
460   if(myPoint1.Distance(myPoint2) > Precision::Confusion()) {
461     mySimulationTopoDs = BRepBuilderAPI_MakeEdge(myPoint1, myPoint2).Shape();
462     /* Add arrow in simulation */
463     bool noNeedToTest = this->AddArrowToSimulation(mySimulationTopoDs);
464     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
465   }
466   return;
467 }