Salome HOME
33be3b87097afc192fe173487176422c58b3aa56
[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 // File   : PrimitiveGUI_BoxDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "PrimitiveGUI_BoxDlg.h"
27
28 #include <GEOM_DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_Session.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <GEOMImpl_Types.hxx>
38
39 //=================================================================================
40 // class    : PrimitiveGUI_BoxDlg()
41 // purpose  : Constructs a PrimitiveGUI_BoxDlg which is a child of 'parent', with the 
42 //            name 'name' and widget flags set to 'f'.
43 //            The dialog will by default be modeless, unless you set 'modal' to
44 //            TRUE to construct a modal dialog.
45 //=================================================================================
46 PrimitiveGUI_BoxDlg::PrimitiveGUI_BoxDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
47                                           bool modal, Qt::WindowFlags fl )
48   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
49 {
50   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BOX_2P" ) ) );
51   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BOX_DXYZ" ) ));
52   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
53
54   setWindowTitle( tr( "GEOM_BOX_TITLE" ) );
55
56   /***************************************************************/
57   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_BOX" ) );
58   mainFrame()->RadioButton1->setIcon( image0 );
59   mainFrame()->RadioButton2->setIcon( image1 );
60   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
61   mainFrame()->RadioButton3->close();
62
63   GroupPoints = new DlgRef_2Sel( centralWidget() );
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->setIcon( image2 );
68   GroupPoints->PushButton2->setIcon( image2 );
69
70   GroupDimensions = new DlgRef_3Spin( centralWidget() );
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   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
77   layout->setMargin( 0 ); layout->setSpacing( 6 );
78   layout->addWidget( GroupPoints );
79   layout->addWidget( GroupDimensions );
80   /***************************************************************/
81
82   setHelpFileName( "box.htm" );
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   initSpinBox( GroupDimensions->SpinBox_DX, COORD_MIN, COORD_MAX, step, 3 );
117   initSpinBox( GroupDimensions->SpinBox_DY, COORD_MIN, COORD_MAX, step, 3 );
118   initSpinBox( GroupDimensions->SpinBox_DZ, COORD_MIN, COORD_MAX, 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
129   connect( this,          SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
130
131   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
132   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
133
134   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
135   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
136
137   connect( GroupDimensions->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
138   connect( GroupDimensions->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
139   connect( GroupDimensions->SpinBox_DZ, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
140   
141   // VSR: TODO ->>
142   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupDimensions->SpinBox_DX, SLOT( SetStep( double ) ) );
143   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupDimensions->SpinBox_DY, SLOT( SetStep( double ) ) );
144   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupDimensions->SpinBox_DZ, SLOT( SetStep( double ) ) );
145   // <<-
146
147   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
148            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
149
150   initName( tr( "GEOM_BOX" ) );
151
152   ConstructorsClicked( 0 );
153 }
154
155
156 //=================================================================================
157 // function : ConstructorsClicked()
158 // purpose  : Radio button management
159 //=================================================================================
160 void PrimitiveGUI_BoxDlg::ConstructorsClicked( int constructorId )
161 {
162   disconnect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication( ) ) )->selectionMgr(), 0, this, 0 );
163   
164   switch (  constructorId ) {
165   case 0:
166     {
167       globalSelection( GEOM_POINT );
168       
169       GroupDimensions->hide();
170       GroupPoints->show();
171       
172       myEditCurrentArgument = GroupPoints->LineEdit1;
173       GroupPoints->LineEdit1->setText( "" );
174       GroupPoints->LineEdit2->setText( "" );
175       myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
176       
177       connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
178                SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
179       break;
180     }
181   case 1:
182     {
183       GroupPoints->hide();
184       GroupDimensions->show();
185       break;
186     }
187   }
188  
189   qApp->processEvents();
190   updateGeometry();
191   resize( minimumSize() );
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     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( 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     myEditCurrentArgument = send;
289     GEOMBase_Skeleton::LineEditReturnPressed();
290   }
291 }
292
293
294 //=================================================================================
295 // function : ActivateThisDialog()
296 // purpose  :
297 //=================================================================================
298 void PrimitiveGUI_BoxDlg::ActivateThisDialog()
299 {
300   GEOMBase_Skeleton::ActivateThisDialog();
301   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
302            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* )
313 {
314   if ( !mainFrame()->GroupConstructors->isEnabled() )
315     ActivateThisDialog();
316 }
317
318
319 //=================================================================================
320 // function : ValueChangedInSpinBox()
321 // purpose  :
322 //=================================================================================
323 void PrimitiveGUI_BoxDlg::ValueChangedInSpinBox()
324 {
325   displayPreview();
326 }
327
328
329 //=================================================================================
330 // function : createOperation
331 // purpose  :
332 //=================================================================================
333 GEOM::GEOM_IOperations_ptr PrimitiveGUI_BoxDlg::createOperation()
334 {
335   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
336 }
337
338
339 //=================================================================================
340 // function : isValid
341 // purpose  :
342 //=================================================================================
343 bool PrimitiveGUI_BoxDlg::isValid( QString& msg )
344 {
345   return getConstructorId() == 0 ? !( myPoint1->_is_nil() || myPoint2->_is_nil() ) : true;
346 }
347
348
349 //=================================================================================
350 // function : execute
351 // purpose  :
352 //=================================================================================
353 bool PrimitiveGUI_BoxDlg::execute( ObjectList& objects )
354 {
355   bool res = false;
356   
357   GEOM::GEOM_Object_var anObj;
358   
359   switch ( getConstructorId() ) {
360   case 0 :
361     { 
362       if ( !CORBA::is_nil( myPoint1 ) &&  !CORBA::is_nil( myPoint2 ) ) {
363         anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeBoxTwoPnt( myPoint1, myPoint2 );
364         res = true;
365       }
366       
367       break;
368     }
369   case 1 :
370     {
371       double x = GroupDimensions->SpinBox_DX->value();
372       double y = GroupDimensions->SpinBox_DY->value();
373       double z = GroupDimensions->SpinBox_DZ->value();
374       
375       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeBoxDXDYDZ( x, y, z );
376       res = true;
377       break;
378     }
379   }
380   
381   if ( !anObj->_is_nil() )
382   objects.push_back( anObj._retn() );
383   
384   return res;
385 }
386
387 //=================================================================================
388 // function : closeEvent
389 // purpose  :
390 //=================================================================================
391 void PrimitiveGUI_BoxDlg::closeEvent( QCloseEvent* e )
392 {
393   GEOMBase_Skeleton::closeEvent( e );
394 }