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