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