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