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