Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_17Jan08)
[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 <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 <TopoDS_Shape.hxx>
38 #include <TopoDS.hxx>
39 #include <TopExp.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42
43 #include <GEOMImpl_Types.hxx>
44
45 //=================================================================================
46 // class    : BasicGUI_VectorDlg()
47 // purpose  : Constructs a BasicGUI_VectorDlg 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 BasicGUI_VectorDlg::BasicGUI_VectorDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
53                                         bool modal, Qt::WindowFlags fl )
54   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
55 {
56   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_VECTOR_2P" ) ) );
57   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_VECTOR_DXYZ" ) ) );
58   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
59
60   setWindowTitle( tr( "GEOM_VECTOR_TITLE" ) );
61
62   /***************************************************************/
63   mainFrame()->GroupConstructors->setTitle(tr("GEOM_VECTOR"));
64   mainFrame()->RadioButton1->setIcon(image0);
65   mainFrame()->RadioButton2->setIcon(image1);
66   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
67   mainFrame()->RadioButton3->close();
68
69   GroupPoints = new DlgRef_2Sel( centralWidget() );
70
71   GroupPoints->GroupBox1->setTitle( tr( "GEOM_POINTS" ) );
72   GroupPoints->TextLabel1->setText( tr( "GEOM_POINT_I" ).arg( 1 ) );
73   GroupPoints->TextLabel2->setText( tr( "GEOM_POINT_I" ).arg( 2 ) );
74   GroupPoints->PushButton1->setIcon( image2 );
75   GroupPoints->PushButton2->setIcon( image2 );
76
77   GroupPoints->LineEdit1->setReadOnly( true );
78   GroupPoints->LineEdit2->setReadOnly( true );
79
80   GroupDimensions = new DlgRef_3Spin1Check( centralWidget() );
81   GroupDimensions->GroupBox1->setTitle( tr( "GEOM_COORDINATES" ) );
82   GroupDimensions->TextLabel1->setText( tr( "GEOM_DX" ) );
83   GroupDimensions->TextLabel2->setText( tr( "GEOM_DY" ) );
84   GroupDimensions->TextLabel3->setText( tr( "GEOM_DZ" ) );
85   GroupDimensions->CheckButton1->setText( tr( "GEOM_REVERSE_VECTOR" ) );
86
87   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
88   layout->setMargin( 0 ); layout->setSpacing( 6 );
89   layout->addWidget( GroupPoints );
90   layout->addWidget( GroupDimensions );
91   /***************************************************************/
92
93   setHelpFileName( "create_vector_page.html" );
94   
95   /* Initialisations */
96   Init();
97 }
98
99
100 //=================================================================================
101 // function : ~BasicGUI_VectorDlg()
102 // purpose  : Destroys the object and frees any allocated resources
103 //=================================================================================
104 BasicGUI_VectorDlg::~BasicGUI_VectorDlg()
105 {  
106 }
107
108
109 //=================================================================================
110 // function : Init()
111 // purpose  :
112 //=================================================================================
113 void BasicGUI_VectorDlg::Init()
114 {
115     /* init variables */
116   myEditCurrentArgument = GroupPoints->LineEdit1;
117
118   myPoint1 = GEOM::GEOM_Object::_nil();
119   myPoint2 = GEOM::GEOM_Object::_nil();
120
121   /* Get setting of step value from file configuration */
122   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
123   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
124  
125   /* min, max, step and decimals for spin boxes */
126   initSpinBox( GroupDimensions->SpinBox_DX, COORD_MIN, COORD_MAX, step, 3 ); // VSR:TODO : DBL_DIGITS_DISPLAY
127   initSpinBox( GroupDimensions->SpinBox_DY, COORD_MIN, COORD_MAX, step, 3 ); // VSR:TODO : DBL_DIGITS_DISPLAY
128   initSpinBox( GroupDimensions->SpinBox_DZ, COORD_MIN, COORD_MAX, step, 3 ); // VSR:TODO : DBL_DIGITS_DISPLAY
129
130   double dx( 0. ), dy( 0. ), dz( 200. );
131   GroupDimensions->SpinBox_DX->setValue( dx );
132   GroupDimensions->SpinBox_DY->setValue( dy );
133   GroupDimensions->SpinBox_DZ->setValue( dz );
134
135   GroupDimensions->CheckButton1->setChecked( false );
136
137   /* signals and slots connections */
138   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
139   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
140   
141   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
142   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
143
144   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
145
146   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
147   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
148
149   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
150   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
151
152   connect( GroupDimensions->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
153   connect( GroupDimensions->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
154   connect( GroupDimensions->SpinBox_DZ, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
155
156   // VSR: TODO ->>
157   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupDimensions->SpinBox_DX, SLOT( SetStep( double ) ) );
158   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupDimensions->SpinBox_DY, SLOT( SetStep( double ) ) );
159   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupDimensions->SpinBox_DZ, SLOT( SetStep( double ) ) );
160   // <<-
161
162   connect( GroupDimensions->CheckButton1, SIGNAL( stateChanged( int ) ), this, SLOT( ReverseVector( int ) ) );
163
164   connect( myGeomGUI->getApp()->selectionMgr(), 
165            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
166
167   initName( tr("GEOM_VECTOR") );
168
169   ConstructorsClicked( 0 );
170 }
171
172
173 //=================================================================================
174 // function : ConstructorsClicked()
175 // purpose  : Radio button management
176 //=================================================================================
177 void BasicGUI_VectorDlg::ConstructorsClicked( int constructorId )
178 {
179   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
180   myPoint1 = GEOM::GEOM_Object::_nil();
181   myPoint2 = GEOM::GEOM_Object::_nil();
182
183   switch ( constructorId ) {
184   case 0:
185     {
186       GroupDimensions->hide();
187       GroupPoints->show();
188       
189       myEditCurrentArgument = GroupPoints->LineEdit1;
190       GroupPoints->LineEdit1->setText( "" );
191       GroupPoints->LineEdit2->setText( "" );
192       
193       globalSelection(); // close local contexts, if any
194       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
195       connect( myGeomGUI->getApp()->selectionMgr(), 
196                SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
197       break;
198     }
199   case 1:
200     {
201       GroupPoints->hide();
202       GroupDimensions->show();
203       globalSelection(); // close local contexts, if any
204       
205       double dx( 0. ), dy( 0. ), dz( 0. ); 
206       GroupDimensions->SpinBox_DX->setValue( dx );
207       GroupDimensions->SpinBox_DY->setValue( dy );
208       GroupDimensions->SpinBox_DZ->setValue( dz );
209       
210       GroupDimensions->CheckButton1->setChecked( false );
211       break;
212     }
213   }
214
215   qApp->processEvents();
216   updateGeometry();
217   resize( minimumSize() );
218
219   displayPreview();
220 }
221
222
223 //=================================================================================
224 // function : ClickOnOk()
225 // purpose  :
226 //=================================================================================
227 void BasicGUI_VectorDlg::ClickOnOk()
228 {
229   if ( ClickOnApply() )
230     ClickOnCancel();
231 }
232
233 //=================================================================================
234 // function : ClickOnApply()
235 // purpose  :
236 //=================================================================================
237 bool BasicGUI_VectorDlg::ClickOnApply()
238 {
239   buttonApply()->setFocus();
240
241   if ( !onAccept() )
242     return false;
243
244   initName();
245   if ( getConstructorId() != 1 )
246         ConstructorsClicked( getConstructorId() );
247   return true;
248 }
249
250
251 //=================================================================================
252 // function : SelectionIntoArgument()
253 // purpose  : Called when selection as changed or other case
254 //=================================================================================
255 void BasicGUI_VectorDlg::SelectionIntoArgument()
256 {
257   myEditCurrentArgument->setText( "" );
258
259   if ( IObjectCount() != 1 ) {
260     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
261       myPoint1 = GEOM::GEOM_Object::_nil();
262     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
263       myPoint2 = GEOM::GEOM_Object::_nil();
264     return;
265   }
266
267   // nbSel == 1 
268   Standard_Boolean aRes = Standard_False;
269   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
270   if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
271     QString aName = GEOMBase::GetName(aSelectedObject);
272
273     TopoDS_Shape aShape;
274     if ( GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
275       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
276       TColStd_IndexedMapOfInteger aMap;
277       aSelMgr->GetIndexes( firstIObject(), aMap );
278       if ( aMap.Extent() == 1 ) { // Local Selection
279         int anIndex = aMap( 1 );
280         aName += QString( ":vertex_%1" ).arg( anIndex );
281
282         //Find SubShape Object in Father
283         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
284         
285         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
286           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
287           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
288         }
289         else {
290           aSelectedObject = aFindedObject; // get Object from study
291         }
292       }
293       else { // Global Selection
294         if ( aShape.ShapeType() != TopAbs_VERTEX ) {
295           aSelectedObject = GEOM::GEOM_Object::_nil();
296           aName = "";
297         }
298       }
299     }
300
301     myEditCurrentArgument->setText(aName);
302
303     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint1 = aSelectedObject;
304     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myPoint2 = aSelectedObject;
305   }
306
307   displayPreview();
308 }
309
310
311 //=================================================================================
312 // function : SetEditCurrentArgument()
313 // purpose  :
314 //=================================================================================
315 void BasicGUI_VectorDlg::SetEditCurrentArgument()
316 {
317   QPushButton* send = (QPushButton*)sender();
318   if      ( send == GroupPoints->PushButton1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
319   else if ( send == GroupPoints->PushButton2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
320   myEditCurrentArgument->setFocus();
321   SelectionIntoArgument();
322 }
323
324
325 //=================================================================================
326 // function : LineEditReturnPressed()
327 // purpose  :
328 //=================================================================================
329 void BasicGUI_VectorDlg::LineEditReturnPressed()
330
331   QLineEdit* send = (QLineEdit*)sender();
332   if      ( send == GroupPoints->LineEdit1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
333   else if ( send == GroupPoints->LineEdit2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
334   else return;
335   GEOMBase_Skeleton::LineEditReturnPressed();
336 }
337
338
339 //=================================================================================
340 // function : ActivateThisDialog()
341 // purpose  :
342 //=================================================================================
343 void BasicGUI_VectorDlg::ActivateThisDialog()
344 {
345   GEOMBase_Skeleton::ActivateThisDialog();
346   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
347            this, SLOT( SelectionIntoArgument() ) );
348         
349   ConstructorsClicked( getConstructorId() );
350 }
351
352 //=================================================================================
353 // function : DeactivateActiveDialog()
354 // purpose  : public slot to deactivate if active
355 //=================================================================================
356 void BasicGUI_VectorDlg::DeactivateActiveDialog()
357 {
358   GEOMBase_Skeleton::DeactivateActiveDialog();
359 }
360
361 //=================================================================================
362 // function : enterEvent()
363 // purpose  :
364 //=================================================================================
365 void BasicGUI_VectorDlg::enterEvent( QEvent* )
366 {
367   if ( !mainFrame()->GroupConstructors->isEnabled() )
368     ActivateThisDialog();
369 }
370
371 //=================================================================================
372 // function : ValueChangedInSpinBox()
373 // purpose  :
374 //=================================================================================
375 void BasicGUI_VectorDlg::ValueChangedInSpinBox( double newValue )
376 {
377   displayPreview();
378 }
379
380 //=================================================================================
381 // function : ReverseVector()
382 // purpose  : 'state' not used here
383 //=================================================================================
384 void BasicGUI_VectorDlg::ReverseVector( int state )
385 {
386   double dx = -GroupDimensions->SpinBox_DX->value();
387   double dy = -GroupDimensions->SpinBox_DY->value();
388   double dz = -GroupDimensions->SpinBox_DZ->value();
389
390   GroupDimensions->SpinBox_DX->setValue( dx );
391   GroupDimensions->SpinBox_DY->setValue( dy );
392   GroupDimensions->SpinBox_DZ->setValue( dz );
393   
394   displayPreview();
395 }
396
397 //=================================================================================
398 // function : createOperation
399 // purpose  :
400 //=================================================================================
401 GEOM::GEOM_IOperations_ptr BasicGUI_VectorDlg::createOperation()
402 {
403   return myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
404 }
405
406 //=================================================================================
407 // function : isValid
408 // purpose  :
409 //=================================================================================
410 bool BasicGUI_VectorDlg::isValid( QString& msg )
411 {
412   return getConstructorId() == 0 ? !myPoint1->_is_nil() && !myPoint2->_is_nil() : true;
413 }
414
415 //=================================================================================
416 // function : execute
417 // purpose  :
418 //=================================================================================
419 bool BasicGUI_VectorDlg::execute( ObjectList& objects )
420 {
421   bool res = false;
422   
423   GEOM::GEOM_Object_var anObj;
424
425   switch ( getConstructorId() ) {
426   case 0 :
427     {
428       anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeVectorTwoPnt( myPoint1, myPoint2 );
429       res = true;
430       break;
431     }
432   case 1 :
433     {
434       double dx = GroupDimensions->SpinBox_DX->value();
435       double dy = GroupDimensions->SpinBox_DY->value();
436       double dz = GroupDimensions->SpinBox_DZ->value();
437       anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeVectorDXDYDZ( dx, dy, dz );
438       res = true;
439       break;
440     }
441   }
442
443   if ( !anObj->_is_nil() )
444     objects.push_back( anObj._retn() );
445
446   return res;
447 }
448
449 //=================================================================================
450 // function : addSubshapeToStudy
451 // purpose  : virtual method to add new SubObjects if local selection
452 //=================================================================================
453 void BasicGUI_VectorDlg::addSubshapesToStudy()
454 {
455   QMap<QString, GEOM::GEOM_Object_var> objMap;
456
457   switch ( getConstructorId() ) {
458   case 0:
459     objMap[GroupPoints->LineEdit1->text()] = myPoint1;
460     objMap[GroupPoints->LineEdit2->text()] = myPoint2;
461     break;
462   case 1:
463     return;
464   }
465   addSubshapesToFather( objMap );
466 }