Salome HOME
6daee534596477c0be959e22fc46f836e07ba081
[modules/geom.git] / src / BasicGUI / BasicGUI_VectorDlg.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   : BasicGUI_VectorDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "BasicGUI_VectorDlg.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    : BasicGUI_VectorDlg()
41 // purpose  : Constructs a BasicGUI_VectorDlg 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 BasicGUI_VectorDlg::BasicGUI_VectorDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
47                                         bool modal, Qt::WindowFlags fl )
48   : GEOMBase_Skeleton( theGeometryGUI, parent, "BasicGUI_VectorDlg", modal, fl )
49 {
50   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_VECTOR_2P" ) ) );
51   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_VECTOR_DXYZ" ) ) );
52   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
53
54   setWindowTitle( tr( "GEOM_VECTOR_TITLE" ) );
55
56   /***************************************************************/
57   mainFrame()->GroupConstructors->setTitle(tr("GEOM_VECTOR"));
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
65   GroupPoints->GroupBox1->setTitle( tr( "GEOM_POINTS" ) );
66   GroupPoints->TextLabel1->setText( tr( "GEOM_POINT_I" ).arg( 1 ) );
67   GroupPoints->TextLabel2->setText( tr( "GEOM_POINT_I" ).arg( 2 ) );
68   GroupPoints->PushButton1->setIcon( image2 );
69   GroupPoints->PushButton2->setIcon( image2 );
70
71   GroupPoints->LineEdit1->setReadOnly( true );
72   GroupPoints->LineEdit2->setReadOnly( true );
73
74   GroupDimensions = new DlgRef_3Spin1Check( centralWidget() );
75   GroupDimensions->GroupBox1->setTitle( tr( "GEOM_COORDINATES" ) );
76   GroupDimensions->TextLabel1->setText( tr( "GEOM_DX" ) );
77   GroupDimensions->TextLabel2->setText( tr( "GEOM_DY" ) );
78   GroupDimensions->TextLabel3->setText( tr( "GEOM_DZ" ) );
79   GroupDimensions->CheckButton1->setText( tr( "GEOM_REVERSE_VECTOR" ) );
80
81   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
82   layout->setMargin( 0 ); layout->setSpacing( 6 );
83   layout->addWidget( GroupPoints );
84   layout->addWidget( GroupDimensions );
85   /***************************************************************/
86
87   setHelpFileName( "vector.htm" );
88   
89   /* Initialisations */
90   Init();
91 }
92
93
94 //=================================================================================
95 // function : ~BasicGUI_VectorDlg()
96 // purpose  : Destroys the object and frees any allocated resources
97 //=================================================================================
98 BasicGUI_VectorDlg::~BasicGUI_VectorDlg()
99 {  
100 }
101
102
103 //=================================================================================
104 // function : Init()
105 // purpose  :
106 //=================================================================================
107 void BasicGUI_VectorDlg::Init()
108 {
109     /* init variables */
110   myEditCurrentArgument = GroupPoints->LineEdit1;
111
112   myPoint1 = GEOM::GEOM_Object::_nil();
113   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   initSpinBox( GroupDimensions->SpinBox_DX, COORD_MIN, COORD_MAX, step, 3 );
121   initSpinBox( GroupDimensions->SpinBox_DY, COORD_MIN, COORD_MAX, step, 3 );
122   initSpinBox( GroupDimensions->SpinBox_DZ, COORD_MIN, COORD_MAX, step, 3 );
123
124   double dx( 0. ), dy( 0. ), dz( 200. );
125   GroupDimensions->SpinBox_DX->setValue( dx );
126   GroupDimensions->SpinBox_DY->setValue( dy );
127   GroupDimensions->SpinBox_DZ->setValue( dz );
128
129   GroupDimensions->CheckButton1->setChecked( false );
130
131   /* signals and slots connections */
132   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
133   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
134   
135   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
136   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
137
138   connect( this,           SIGNAL( constructorsClicked( 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( double ) ) );
147   connect( GroupDimensions->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
148   connect( GroupDimensions->SpinBox_DZ, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
149
150   // VSR: TODO ->>
151   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupDimensions->SpinBox_DX, SLOT( SetStep( double ) ) );
152   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupDimensions->SpinBox_DY, SLOT( SetStep( double ) ) );
153   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupDimensions->SpinBox_DZ, SLOT( SetStep( double ) ) );
154   // <<-
155
156   connect( GroupDimensions->CheckButton1, SIGNAL( stateChanged( int ) ), this, SLOT( ReverseVector( int ) ) );
157
158   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
159            SIGNAL( currentSelectionChanged() ), this, SLOT(SelectionIntoArgument() ) );
160
161   initName( tr("GEOM_VECTOR") );
162
163   ConstructorsClicked( 0 );
164 }
165
166
167 //=================================================================================
168 // function : ConstructorsClicked()
169 // purpose  : Radio button management
170 //=================================================================================
171 void BasicGUI_VectorDlg::ConstructorsClicked( int constructorId )
172 {
173   disconnect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 0, this, 0 );
174   myPoint1 = GEOM::GEOM_Object::_nil();
175   myPoint2 = GEOM::GEOM_Object::_nil();
176
177   switch ( constructorId ) {
178   case 0:
179     {
180       GroupDimensions->hide();
181       GroupPoints->show();
182       
183       myEditCurrentArgument = GroupPoints->LineEdit1;
184       GroupPoints->LineEdit1->setText( "" );
185       GroupPoints->LineEdit2->setText( "" );
186       
187       globalSelection( GEOM_POINT );
188       connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
189                SIGNAL( currentSelectionChanged() ), this, SLOT(SelectionIntoArgument() ) );
190       break;
191     }
192   case 1:
193     {
194       GroupPoints->hide();
195       GroupDimensions->show();
196       
197       double dx( 0. ), dy( 0. ), dz( 0. ); 
198       GroupDimensions->SpinBox_DX->setValue( dx );
199       GroupDimensions->SpinBox_DY->setValue( dy );
200       GroupDimensions->SpinBox_DZ->setValue( dz );
201       
202       GroupDimensions->CheckButton1->setChecked( false );
203       break;
204     }
205   }
206
207   qApp->processEvents();
208   updateGeometry();
209   resize( minimumSize() );
210
211   displayPreview();
212 }
213
214
215 //=================================================================================
216 // function : ClickOnOk()
217 // purpose  :
218 //=================================================================================
219 void BasicGUI_VectorDlg::ClickOnOk()
220 {
221   if ( ClickOnApply() )
222     ClickOnCancel();
223 }
224
225 //=================================================================================
226 // function : ClickOnApply()
227 // purpose  :
228 //=================================================================================
229 bool BasicGUI_VectorDlg::ClickOnApply()
230 {
231   buttonApply()->setFocus();
232
233   if ( !onAccept() )
234     return false;
235
236   initName();
237   if ( getConstructorId() != 1 )
238         ConstructorsClicked( getConstructorId() );
239   return true;
240 }
241
242
243 //=================================================================================
244 // function : SelectionIntoArgument()
245 // purpose  : Called when selection as changed or other case
246 //=================================================================================
247 void BasicGUI_VectorDlg::SelectionIntoArgument()
248 {
249   myEditCurrentArgument->setText( "" );
250
251   if ( IObjectCount() != 1 ) {
252     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
253       myPoint1 = GEOM::GEOM_Object::_nil();
254     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
255       myPoint2 = GEOM::GEOM_Object::_nil();
256     return;
257   }
258
259   // nbSel == 1 
260   Standard_Boolean aRes = Standard_False;
261   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
262   if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
263     myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
264     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint1 = aSelectedObject;
265     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myPoint2 = aSelectedObject;
266   }
267
268   displayPreview();
269 }
270
271
272 //=================================================================================
273 // function : SetEditCurrentArgument()
274 // purpose  :
275 //=================================================================================
276 void BasicGUI_VectorDlg::SetEditCurrentArgument()
277 {
278   QPushButton* send = (QPushButton*)sender();
279   if      ( send == GroupPoints->PushButton1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
280   else if ( send == GroupPoints->PushButton2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
281   myEditCurrentArgument->setFocus();
282   SelectionIntoArgument();
283 }
284
285
286 //=================================================================================
287 // function : LineEditReturnPressed()
288 // purpose  :
289 //=================================================================================
290 void BasicGUI_VectorDlg::LineEditReturnPressed()
291
292   QLineEdit* send = (QLineEdit*)sender();
293   if      ( send == GroupPoints->LineEdit1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
294   else if ( send == GroupPoints->LineEdit2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
295   else return;
296   GEOMBase_Skeleton::LineEditReturnPressed();
297 }
298
299
300 //=================================================================================
301 // function : ActivateThisDialog()
302 // purpose  :
303 //=================================================================================
304 void BasicGUI_VectorDlg::ActivateThisDialog()
305 {
306   GEOMBase_Skeleton::ActivateThisDialog();
307   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
308            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
309         
310   ConstructorsClicked( getConstructorId() );
311 }
312
313 //=================================================================================
314 // function : DeactivateActiveDialog()
315 // purpose  : public slot to deactivate if active
316 //=================================================================================
317 void BasicGUI_VectorDlg::DeactivateActiveDialog()
318 {
319   // myGeomGUI->SetState( -1 );
320   GEOMBase_Skeleton::DeactivateActiveDialog();
321 }
322
323 //=================================================================================
324 // function : enterEvent()
325 // purpose  :
326 //=================================================================================
327 void BasicGUI_VectorDlg::enterEvent( QEvent* )
328 {
329   if ( !mainFrame()->GroupConstructors->isEnabled() )
330     ActivateThisDialog();
331 }
332
333 //=================================================================================
334 // function : ValueChangedInSpinBox()
335 // purpose  :
336 //=================================================================================
337 void BasicGUI_VectorDlg::ValueChangedInSpinBox( double newValue )
338 {
339   displayPreview();
340 }
341
342 //=================================================================================
343 // function : ReverseVector()
344 // purpose  : 'state' not used here
345 //=================================================================================
346 void BasicGUI_VectorDlg::ReverseVector( int state )
347 {
348   double dx = -GroupDimensions->SpinBox_DX->value();
349   double dy = -GroupDimensions->SpinBox_DY->value();
350   double dz = -GroupDimensions->SpinBox_DZ->value();
351
352   GroupDimensions->SpinBox_DX->setValue( dx );
353   GroupDimensions->SpinBox_DY->setValue( dy );
354   GroupDimensions->SpinBox_DZ->setValue( dz );
355   
356   displayPreview();
357 }
358
359 //=================================================================================
360 // function : createOperation
361 // purpose  :
362 //=================================================================================
363 GEOM::GEOM_IOperations_ptr BasicGUI_VectorDlg::createOperation()
364 {
365   return myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
366 }
367
368 //=================================================================================
369 // function : isValid
370 // purpose  :
371 //=================================================================================
372 bool BasicGUI_VectorDlg::isValid( QString& msg )
373 {
374   return getConstructorId() == 0 ? !myPoint1->_is_nil() && !myPoint2->_is_nil() : true;
375 }
376
377 //=================================================================================
378 // function : execute
379 // purpose  :
380 //=================================================================================
381 bool BasicGUI_VectorDlg::execute( ObjectList& objects )
382 {
383   bool res = false;
384   
385   GEOM::GEOM_Object_var anObj;
386
387   switch ( getConstructorId() ) {
388   case 0 :
389     {
390       anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeVectorTwoPnt( myPoint1, myPoint2 );
391       res = true;
392       break;
393     }
394   case 1 :
395     {
396       double dx = GroupDimensions->SpinBox_DX->value();
397       double dy = GroupDimensions->SpinBox_DY->value();
398       double dz = GroupDimensions->SpinBox_DZ->value();
399       anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeVectorDXDYDZ( dx, dy, dz );
400       res = true;
401       break;
402     }
403   }
404
405   if ( !anObj->_is_nil() )
406     objects.push_back( anObj._retn() );
407
408   return res;
409 }
410