Salome HOME
DCQ : New Architecture
[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   QAD_Application::getDesktop()->putInfo(tr(""));
225   if (mySimulationTopoDs.IsNull())
226     return;
227   myGeomBase->EraseSimulationShape();
228   mySimulationTopoDs.Nullify();
229
230   switch(myConstructorId)
231     {
232     case 0 :
233       {
234         if(myOkPoint1 && myOkPoint2)
235           myPrimitiveGUI->MakeBoxAndDisplay(myPoint1, myPoint2);
236         break;
237       }
238     case 1 :
239       {
240         /* Recup args and call method */
241         double vx = GroupDimensions->SpinBox_DX->GetValue();
242         double vy = GroupDimensions->SpinBox_DY->GetValue();
243         double vz = GroupDimensions->SpinBox_DZ->GetValue();
244         myPoint1.SetCoord(0.0, 0.0, 0.0);
245         myPoint2.SetCoord(vx, vy, vz);
246         myPrimitiveGUI->MakeBoxAndDisplay(myPoint1, myPoint2);
247         break;
248       }
249     }
250   return;
251 }
252
253
254 //=================================================================================
255 // function : SelectionIntoArgument()
256 // purpose  : Called when selection as changed
257 //=================================================================================
258 void PrimitiveGUI_BoxDlg::SelectionIntoArgument()
259 {
260   myGeomBase->EraseSimulationShape();
261   myEditCurrentArgument->setText("");
262   QString aString = ""; /* name of selection */
263
264   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
265   if (nbSel != 1) {
266     if (myEditCurrentArgument == GroupPoints->LineEdit1)
267       myOkPoint1 = false;
268     else if ( myEditCurrentArgument == GroupPoints->LineEdit2)
269       myOkPoint2 = false;
270     return;
271   }
272
273   // nbSel == 1
274   TopoDS_Shape S; 
275   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
276     return;
277
278   if(myEditCurrentArgument == GroupPoints->LineEdit1 && myGeomBase->VertexToPoint(S, myPoint1)) {
279     myEditCurrentArgument->setText(aString);
280     myOkPoint1 = true;
281   }
282   else if(myEditCurrentArgument == GroupPoints->LineEdit2 && myGeomBase->VertexToPoint(S, myPoint2)) {
283     myEditCurrentArgument->setText(aString);
284     myOkPoint2 = true;
285   }
286
287   if(myOkPoint1 && myOkPoint2 && TestBoxDimensions(myPoint1, myPoint2)) {
288     mySimulationTopoDs = BRepPrimAPI_MakeBox(myPoint1, myPoint2).Shape();
289     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
290   }
291   return;
292 }
293
294
295 //=================================================================================
296 // function : SetEditCurrentArgument()
297 // purpose  :
298 //=================================================================================
299 void PrimitiveGUI_BoxDlg::SetEditCurrentArgument()
300 {
301   if(myConstructorId != 0)
302     return;
303
304   QPushButton* send = (QPushButton*)sender();
305
306   if(send == GroupPoints->PushButton1) {
307     GroupPoints->LineEdit1->setFocus();
308     myEditCurrentArgument = GroupPoints->LineEdit1;
309   }
310   else if(send == GroupPoints->PushButton2) {
311     GroupPoints->LineEdit2->setFocus();
312     myEditCurrentArgument = GroupPoints->LineEdit2;
313   }
314   mySelection->AddFilter(myVertexFilter);
315   this->SelectionIntoArgument();
316
317   return;
318 }
319
320
321 //=================================================================================
322 // function : LineEditReturnPressed()
323 // purpose  :
324 //=================================================================================
325 void PrimitiveGUI_BoxDlg::LineEditReturnPressed()
326 {
327   QLineEdit* send = (QLineEdit*)sender();
328   if(send == GroupPoints->LineEdit1)
329     myEditCurrentArgument = GroupPoints->LineEdit1;
330   else if (send == GroupPoints->LineEdit2)
331     myEditCurrentArgument = GroupPoints->LineEdit2;
332   else
333     return;
334
335   GEOMBase_Skeleton::LineEditReturnPressed();
336   return;
337 }
338
339
340 //=================================================================================
341 // function : ActivateThisDialog()
342 // purpose  :
343 //=================================================================================
344 void PrimitiveGUI_BoxDlg::ActivateThisDialog()
345 {
346   GEOMBase_Skeleton::ActivateThisDialog();
347   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
348   if(myConstructorId == 0)
349     mySelection->AddFilter(myVertexFilter);
350   if(!mySimulationTopoDs.IsNull())
351     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
352   return;
353 }
354
355
356 //=================================================================================
357 // function : enterEvent [REDEFINED]
358 // purpose  :
359 //=================================================================================
360 void PrimitiveGUI_BoxDlg::enterEvent(QEvent* e)
361 {
362   if(GroupConstructors->isEnabled())
363     return;
364   this->ActivateThisDialog();
365   return;
366 }
367
368
369 //=================================================================================
370 // function : ValueChangedInSpinBox()
371 // purpose  :
372 //=================================================================================
373 void PrimitiveGUI_BoxDlg::ValueChangedInSpinBox(double newValue)
374 {
375   myGeomBase->EraseSimulationShape();
376   mySimulationTopoDs.Nullify();
377   QObject* send = (QObject*)sender();
378   double vx, vy, vz;
379
380   if(send == GroupDimensions->SpinBox_DX) {
381     vx = newValue;
382     vy = GroupDimensions->SpinBox_DY->GetValue();
383     vz = GroupDimensions->SpinBox_DZ->GetValue();
384   }
385   else if(send == GroupDimensions->SpinBox_DY) {
386     vx = GroupDimensions->SpinBox_DX->GetValue();
387     vy = newValue;
388     vz = GroupDimensions->SpinBox_DZ->GetValue();
389   }
390   else if(send == GroupDimensions->SpinBox_DZ) {
391     vx = GroupDimensions->SpinBox_DX->GetValue();
392     vy = GroupDimensions->SpinBox_DY->GetValue();
393     vz = newValue;
394   }
395
396   myPoint1.SetCoord(0.0, 0.0, 0.0);
397   myPoint2.SetCoord(vx, vy, vz);
398
399   if(TestBoxDimensions(myPoint1, myPoint2)) {
400     mySimulationTopoDs = BRepPrimAPI_MakeBox(myPoint1, myPoint2).Shape();
401     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
402   }
403   return;
404 }
405
406
407 //=================================================================================
408 // function : TestBoxDimensions()
409 // purpose  :
410 //=================================================================================
411 bool PrimitiveGUI_BoxDlg::TestBoxDimensions(gp_Pnt P1, gp_Pnt P2)
412 {
413   if ((fabs(P1.X() - P2.X()) > Precision::Confusion()) &&
414       (fabs(P1.Y() - P2.Y()) > Precision::Confusion()) &&
415       (fabs(P1.Z() - P2.Z()) > Precision::Confusion()))
416     return true;
417   return false;
418 }