Salome HOME
6ad4bbc87b68d5ed1fd5a8075c984904dc34466f
[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 "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "SalomeApp_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, const char* name, bool modal, WFlags fl)
53   :GEOMBase_Skeleton(parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu), myGeometryGUI(theGeometryGUI)
54 {
55   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BOX_2P")));
56   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BOX_DXYZ")));
57   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
58
59   setCaption(tr("GEOM_BOX_TITLE"));
60
61   /***************************************************************/
62   GroupConstructors->setTitle(tr("GEOM_BOX"));
63   RadioButton1->setPixmap(image0);
64   RadioButton2->setPixmap(image1);
65   RadioButton3->close(TRUE);
66
67   GroupPoints = new DlgRef_2Sel_QTD(this, "GroupPoints");
68   GroupPoints->GroupBox1->setTitle(tr("GEOM_DIAGONAL_POINTS"));
69   GroupPoints->TextLabel1->setText(tr("GEOM_POINT_I").arg("1"));
70   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_I").arg("2"));
71   GroupPoints->PushButton1->setPixmap(image2);
72   GroupPoints->PushButton2->setPixmap(image2);
73
74   GroupDimensions = new DlgRef_3Spin(this, "GroupDimensions");
75   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
76   GroupDimensions->TextLabel1->setText(tr("GEOM_DX"));
77   GroupDimensions->TextLabel2->setText(tr("GEOM_DY"));
78   GroupDimensions->TextLabel3->setText(tr("GEOM_DZ"));
79
80   Layout1->addWidget(GroupPoints, 2, 0);
81   Layout1->addWidget(GroupDimensions, 2, 0);
82   /***************************************************************/
83   
84   Init();
85 }
86
87
88 //=================================================================================
89 // function : ~DialogBox_Box()
90 // purpose  : Destroys the object and frees any allocated resources
91 //=================================================================================
92 PrimitiveGUI_BoxDlg::~PrimitiveGUI_BoxDlg()
93 {
94   // no need to delete child widgets, Qt does it all for us
95 }
96
97
98 //=================================================================================
99 // function : Init()
100 // purpose  :
101 //=================================================================================
102 void PrimitiveGUI_BoxDlg::Init()
103 {
104   /* init variables */
105   myEditCurrentArgument = GroupPoints->LineEdit1;
106   GroupPoints->LineEdit1->setReadOnly( true );
107   GroupPoints->LineEdit2->setReadOnly( true );
108   
109   myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
110     
111   /* Get setting of step value from file configuration */
112   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
113   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
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()));
137   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
138   connect(GroupDimensions->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
139   
140   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
141   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DY, SLOT(SetStep(double)));
142   connect(myGeometryGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DZ, SLOT(SetStep(double)));
143
144   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
145           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
146
147   initName( tr( "GEOM_BOX" ) );
148   ConstructorsClicked(0);
149 }
150
151
152 //=================================================================================
153 // function : ConstructorsClicked()
154 // purpose  : Radio button management
155 //=================================================================================
156 void PrimitiveGUI_BoxDlg::ConstructorsClicked(int constructorId)
157 {
158   disconnect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0);
159   
160   switch (constructorId)
161     {
162     case 0:
163       {
164         globalSelection( GEOM_POINT );
165
166         GroupDimensions->hide();
167         resize(0, 0);
168         GroupPoints->show();
169         
170         myEditCurrentArgument = GroupPoints->LineEdit1;
171         GroupPoints->LineEdit1->setText("");
172         GroupPoints->LineEdit2->setText("");
173         myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
174         
175         connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
176                 SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
177         break;
178       }
179     case 1:
180       {
181         GroupPoints->hide();
182         resize(0, 0);
183         GroupDimensions->show();
184         
185         break;
186       }
187     }
188   displayPreview();
189 }
190
191
192 //=================================================================================
193 // function : ClickOnOk()
194 // purpose  :
195 //=================================================================================
196 void PrimitiveGUI_BoxDlg::ClickOnOk()
197 {
198   if ( ClickOnApply() )
199     ClickOnCancel();
200 }
201
202
203 //=================================================================================
204 // function : ClickOnApply()
205 // purpose  :
206 //=================================================================================
207 bool PrimitiveGUI_BoxDlg::ClickOnApply()
208 {
209   if ( !onAccept() )
210     return false;
211   
212   initName();
213   ConstructorsClicked( getConstructorId() );
214   return true;
215 }
216
217
218 //=================================================================================
219 // function : ClickOnCancel()
220 // purpose  : 
221 //=================================================================================
222 void PrimitiveGUI_BoxDlg::ClickOnCancel()
223 {
224   GEOMBase_Skeleton::ClickOnCancel();
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 : DeactivateActiveDialog()
328 // purpose  : public slot to deactivate if active
329 //=================================================================================
330 void PrimitiveGUI_BoxDlg::DeactivateActiveDialog()
331 {
332   GEOMBase_Skeleton::DeactivateActiveDialog();
333 }
334
335
336 //=================================================================================
337 // function : ValueChangedInSpinBox()
338 // purpose  :
339 //=================================================================================
340 void PrimitiveGUI_BoxDlg::ValueChangedInSpinBox()
341 {
342   displayPreview();
343 }
344
345
346 //=================================================================================
347 // function : createOperation
348 // purpose  :
349 //=================================================================================
350 GEOM::GEOM_IOperations_ptr PrimitiveGUI_BoxDlg::createOperation()
351 {
352   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
353 }
354
355
356 //=================================================================================
357 // function : isValid
358 // purpose  :
359 //=================================================================================
360 bool PrimitiveGUI_BoxDlg::isValid( QString& msg )
361 {
362   return getConstructorId() == 0 ? !(myPoint1->_is_nil() || myPoint2->_is_nil()) : true;
363 }
364
365
366 //=================================================================================
367 // function : execute
368 // purpose  :
369 //=================================================================================
370 bool PrimitiveGUI_BoxDlg::execute( ObjectList& objects )
371 {
372   bool res = false;
373   
374   GEOM::GEOM_Object_var anObj;
375   
376   switch ( getConstructorId() ) 
377   {
378   case 0 :
379     { 
380       if ( !CORBA::is_nil( myPoint1 ) &&  !CORBA::is_nil( myPoint2 ) ) {
381         anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeBoxTwoPnt( myPoint1, myPoint2 );
382         res = true;
383       }
384       
385       break;
386     }
387   case 1 :
388     {
389       double x = GroupDimensions->SpinBox_DX->GetValue();
390       double y = GroupDimensions->SpinBox_DY->GetValue();
391       double z = GroupDimensions->SpinBox_DZ->GetValue();
392       
393       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeBoxDXDYDZ( x, y, z );
394       res = true;
395       break;
396     }
397   }
398   
399   if ( !anObj->_is_nil() )
400   objects.push_back( anObj._retn() );
401   
402   return res;
403 }
404
405 //=================================================================================
406 // function : closeEvent
407 // purpose  :
408 //=================================================================================
409 void  PrimitiveGUI_BoxDlg::closeEvent( QCloseEvent* e )
410 {
411   GEOMBase_Skeleton::closeEvent( e );
412 }