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