Salome HOME
Improve actions management
[modules/geom.git] / src / BasicGUI / BasicGUI_VectorDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : BasicGUI_VectorDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
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   GroupPoints->PushButton1->setDown( true );
77
78   GroupPoints->LineEdit1->setReadOnly( true );
79   GroupPoints->LineEdit2->setReadOnly( true );
80   GroupPoints->LineEdit1->setEnabled( true );
81   GroupPoints->LineEdit2->setEnabled( false );
82
83   GroupDimensions = new DlgRef_3Spin1Check( centralWidget() );
84   GroupDimensions->GroupBox1->setTitle( tr( "GEOM_COORDINATES" ) );
85   GroupDimensions->TextLabel1->setText( tr( "GEOM_DX" ) );
86   GroupDimensions->TextLabel2->setText( tr( "GEOM_DY" ) );
87   GroupDimensions->TextLabel3->setText( tr( "GEOM_DZ" ) );
88   GroupDimensions->CheckButton1->setText( tr( "GEOM_REVERSE_VECTOR" ) );
89
90   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
91   layout->setMargin( 0 ); layout->setSpacing( 6 );
92   layout->addWidget( GroupPoints );
93   layout->addWidget( GroupDimensions );
94   /***************************************************************/
95
96   setHelpFileName( "create_vector_page.html" );
97
98   /* Initialisations */
99   Init();
100 }
101
102
103 //=================================================================================
104 // function : ~BasicGUI_VectorDlg()
105 // purpose  : Destroys the object and frees any allocated resources
106 //=================================================================================
107 BasicGUI_VectorDlg::~BasicGUI_VectorDlg()
108 {
109 }
110
111
112 //=================================================================================
113 // function : Init()
114 // purpose  :
115 //=================================================================================
116 void BasicGUI_VectorDlg::Init()
117 {
118     /* init variables */
119   myEditCurrentArgument = GroupPoints->LineEdit1;
120
121   myPoint1 = GEOM::GEOM_Object::_nil();
122   myPoint2 = GEOM::GEOM_Object::_nil();
123
124   /* Get setting of step value from file configuration */
125   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
126   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
127
128   /* min, max, step and decimals for spin boxes */
129   initSpinBox( GroupDimensions->SpinBox_DX, COORD_MIN, COORD_MAX, step, 6 ); // VSR:TODO : DBL_DIGITS_DISPLAY
130   initSpinBox( GroupDimensions->SpinBox_DY, COORD_MIN, COORD_MAX, step, 6 ); // VSR:TODO : DBL_DIGITS_DISPLAY
131   initSpinBox( GroupDimensions->SpinBox_DZ, COORD_MIN, COORD_MAX, step, 6 ); // VSR:TODO : DBL_DIGITS_DISPLAY
132
133   double dx( 0. ), dy( 0. ), dz( 200. );
134   GroupDimensions->SpinBox_DX->setValue( dx );
135   GroupDimensions->SpinBox_DY->setValue( dy );
136   GroupDimensions->SpinBox_DZ->setValue( dz );
137
138   GroupDimensions->CheckButton1->setChecked( false );
139
140   /* signals and slots connections */
141   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
142   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
143
144   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
145   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
146
147   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
148
149   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
150   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
151
152   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
153   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
154
155   connect( GroupDimensions->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
156   connect( GroupDimensions->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
157   connect( GroupDimensions->SpinBox_DZ, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
158
159   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
160
161   connect( GroupDimensions->CheckButton1, SIGNAL( stateChanged( int ) ), this, SLOT( ReverseVector( int ) ) );
162
163   connect( myGeomGUI->getApp()->selectionMgr(),
164            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
165
166   initName( tr("GEOM_VECTOR") );
167
168   setConstructorId( 1 ); // simplest constructor
169   ConstructorsClicked( 1 );
170 }
171
172 //=================================================================================
173 // function : SetDoubleSpinBoxStep()
174 // purpose  : Double spin box management
175 //=================================================================================
176 void BasicGUI_VectorDlg::SetDoubleSpinBoxStep( double step )
177 {
178   GroupDimensions->SpinBox_DX->setSingleStep(step);
179   GroupDimensions->SpinBox_DY->setSingleStep(step);
180   GroupDimensions->SpinBox_DZ->setSingleStep(step);
181 }
182
183
184 //=================================================================================
185 // function : ConstructorsClicked()
186 // purpose  : Radio button management
187 //=================================================================================
188 void BasicGUI_VectorDlg::ConstructorsClicked( int constructorId )
189 {
190   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
191   myPoint1 = GEOM::GEOM_Object::_nil();
192   myPoint2 = GEOM::GEOM_Object::_nil();
193
194   switch ( constructorId ) {
195   case 0:
196     {
197       GroupDimensions->hide();
198       GroupPoints->show();
199
200       myEditCurrentArgument = GroupPoints->LineEdit1;
201       GroupPoints->LineEdit1->setText( "" );
202       GroupPoints->LineEdit2->setText( "" );
203       GroupPoints->PushButton1->setDown( true );
204       GroupPoints->PushButton2->setDown( false );
205       GroupPoints->LineEdit1->setEnabled( true );
206       GroupPoints->LineEdit2->setEnabled( false );
207
208       globalSelection(); // close local contexts, if any
209       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
210       connect( myGeomGUI->getApp()->selectionMgr(),
211                SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
212       break;
213     }
214   case 1:
215     {
216       GroupPoints->hide();
217       GroupDimensions->show();
218       globalSelection(); // close local contexts, if any
219
220       double dx( 0. ), dy( 0. ), dz( 0. );
221       GroupDimensions->SpinBox_DX->setValue( dx );
222       GroupDimensions->SpinBox_DY->setValue( dy );
223       GroupDimensions->SpinBox_DZ->setValue( dz );
224
225       GroupDimensions->CheckButton1->setChecked( false );
226       break;
227     }
228   }
229
230   qApp->processEvents();
231   updateGeometry();
232   resize( minimumSizeHint() );
233
234   displayPreview();
235 }
236
237
238 //=================================================================================
239 // function : ClickOnOk()
240 // purpose  :
241 //=================================================================================
242 void BasicGUI_VectorDlg::ClickOnOk()
243 {
244   if ( ClickOnApply() )
245     ClickOnCancel();
246 }
247
248 //=================================================================================
249 // function : ClickOnApply()
250 // purpose  :
251 //=================================================================================
252 bool BasicGUI_VectorDlg::ClickOnApply()
253 {
254   buttonApply()->setFocus();
255
256   if ( !onAccept() )
257     return false;
258
259   initName();
260   if ( getConstructorId() != 1 )
261     ConstructorsClicked( getConstructorId() );
262   return true;
263 }
264
265
266 //=================================================================================
267 // function : SelectionIntoArgument()
268 // purpose  : Called when selection as changed or other case
269 //=================================================================================
270 void BasicGUI_VectorDlg::SelectionIntoArgument()
271 {
272   myEditCurrentArgument->setText( "" );
273
274   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
275   SALOME_ListIO aSelList;
276   aSelMgr->selectedObjects(aSelList);
277
278   if (aSelList.Extent() != 1) {
279     if (myEditCurrentArgument == GroupPoints->LineEdit1)
280       myPoint1 = GEOM::GEOM_Object::_nil();
281     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
282       myPoint2 = GEOM::GEOM_Object::_nil();
283     return;
284   }
285
286   // nbSel == 1
287   Standard_Boolean aRes = Standard_False;
288   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aRes);
289   if (!CORBA::is_nil(aSelectedObject) && aRes) {
290     QString aName = GEOMBase::GetName(aSelectedObject);
291
292     TopoDS_Shape aShape;
293     if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull()) {
294       TColStd_IndexedMapOfInteger aMap;
295       aSelMgr->GetIndexes(aSelList.First(), aMap);
296       if (aMap.Extent() == 1) { // Local Selection
297         int anIndex = aMap(1);
298         aName += QString(":vertex_%1").arg(anIndex);
299
300         //Find SubShape Object in Father
301         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
302         
303         if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
304           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
305           aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
306         }
307         else {
308           aSelectedObject = aFindedObject; // get Object from study
309         }
310       }
311       else { // Global Selection
312         if (aShape.ShapeType() != TopAbs_VERTEX) {
313           aSelectedObject = GEOM::GEOM_Object::_nil();
314           aName = "";
315         }
316       }
317     }
318
319     myEditCurrentArgument->setText(aName);
320
321     if (!aSelectedObject->_is_nil()) { // clear selection if something selected
322       globalSelection();
323       localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
324     }
325
326     if      (myEditCurrentArgument == GroupPoints->LineEdit1) {
327       myPoint1 = aSelectedObject;
328       if (!myPoint1->_is_nil() && myPoint2->_is_nil())
329         GroupPoints->PushButton2->click();
330     }
331     else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
332       myPoint2 = aSelectedObject;
333       if (!myPoint2->_is_nil() && myPoint1->_is_nil())
334         GroupPoints->PushButton1->click();
335     }
336   }
337
338   displayPreview();
339 }
340
341 //=================================================================================
342 // function : SetEditCurrentArgument()
343 // purpose  :
344 //=================================================================================
345 void BasicGUI_VectorDlg::SetEditCurrentArgument()
346 {
347   QPushButton* send = (QPushButton*)sender();
348   if      ( send == GroupPoints->PushButton1 ) {
349     myEditCurrentArgument = GroupPoints->LineEdit1;
350     GroupPoints->PushButton2->setDown(false);
351     GroupPoints->LineEdit1->setEnabled(true);
352     GroupPoints->LineEdit2->setEnabled(false);
353   }
354   else if ( send == GroupPoints->PushButton2 ) {
355     myEditCurrentArgument = GroupPoints->LineEdit2;
356     GroupPoints->PushButton1->setDown(false);
357     GroupPoints->LineEdit1->setEnabled(false);
358     GroupPoints->LineEdit2->setEnabled(true);
359   }
360   myEditCurrentArgument->setFocus();
361   //  SelectionIntoArgument();
362   globalSelection(); // close local selection to clear it
363   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
364   send->setDown(true);
365   displayPreview();
366 }
367
368
369 //=================================================================================
370 // function : LineEditReturnPressed()
371 // purpose  :
372 //=================================================================================
373 void BasicGUI_VectorDlg::LineEditReturnPressed()
374 {
375   QLineEdit* send = (QLineEdit*)sender();
376   if      ( send == GroupPoints->LineEdit1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
377   else if ( send == GroupPoints->LineEdit2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
378   else return;
379   GEOMBase_Skeleton::LineEditReturnPressed();
380 }
381
382
383 //=================================================================================
384 // function : ActivateThisDialog()
385 // purpose  :
386 //=================================================================================
387 void BasicGUI_VectorDlg::ActivateThisDialog()
388 {
389   GEOMBase_Skeleton::ActivateThisDialog();
390   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
391            this, SLOT( SelectionIntoArgument() ) );
392         
393   ConstructorsClicked( getConstructorId() );
394 }
395
396 //=================================================================================
397 // function : DeactivateActiveDialog()
398 // purpose  : public slot to deactivate if active
399 //=================================================================================
400 void BasicGUI_VectorDlg::DeactivateActiveDialog()
401 {
402   GEOMBase_Skeleton::DeactivateActiveDialog();
403 }
404
405 //=================================================================================
406 // function : enterEvent()
407 // purpose  :
408 //=================================================================================
409 void BasicGUI_VectorDlg::enterEvent( QEvent* )
410 {
411   if ( !mainFrame()->GroupConstructors->isEnabled() )
412     ActivateThisDialog();
413 }
414
415 //=================================================================================
416 // function : ValueChangedInSpinBox()
417 // purpose  :
418 //=================================================================================
419 void BasicGUI_VectorDlg::ValueChangedInSpinBox( double newValue )
420 {
421   displayPreview();
422 }
423
424 //=================================================================================
425 // function : ReverseVector()
426 // purpose  : 'state' not used here
427 //=================================================================================
428 void BasicGUI_VectorDlg::ReverseVector( int state )
429 {
430   double dx = -GroupDimensions->SpinBox_DX->value();
431   double dy = -GroupDimensions->SpinBox_DY->value();
432   double dz = -GroupDimensions->SpinBox_DZ->value();
433
434   GroupDimensions->SpinBox_DX->setValue( dx );
435   GroupDimensions->SpinBox_DY->setValue( dy );
436   GroupDimensions->SpinBox_DZ->setValue( dz );
437
438   displayPreview();
439 }
440
441 //=================================================================================
442 // function : createOperation
443 // purpose  :
444 //=================================================================================
445 GEOM::GEOM_IOperations_ptr BasicGUI_VectorDlg::createOperation()
446 {
447   return myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
448 }
449
450 //=================================================================================
451 // function : isValid
452 // purpose  :
453 //=================================================================================
454 bool BasicGUI_VectorDlg::isValid( QString& msg )
455 {
456   if(getConstructorId() == 0) 
457     return !myPoint1->_is_nil() && !myPoint2->_is_nil();
458   else if(getConstructorId() == 1)
459   {
460     bool ok = true;
461     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
462     ok = GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
463     ok = GroupDimensions->SpinBox_DZ->isValid( msg, !IsPreview() ) && ok;
464     return ok;
465   }
466   return false;
467 }
468
469 //=================================================================================
470 // function : execute
471 // purpose  :
472 //=================================================================================
473 bool BasicGUI_VectorDlg::execute( ObjectList& objects )
474 {
475   bool res = false;
476
477   GEOM::GEOM_Object_var anObj;
478
479   GEOM::GEOM_IBasicOperations_var anOper = GEOM::GEOM_IBasicOperations::_narrow( getOperation() );
480
481   switch ( getConstructorId() ) {
482   case 0 :
483     {
484       anObj = anOper->MakeVectorTwoPnt( myPoint1, myPoint2 );
485       res = true;
486       break;
487     }
488   case 1 :
489     {
490       double dx = GroupDimensions->SpinBox_DX->value();
491       double dy = GroupDimensions->SpinBox_DY->value();
492       double dz = GroupDimensions->SpinBox_DZ->value();
493       
494       QStringList aParameters;
495       aParameters << GroupDimensions->SpinBox_DX->text();
496       aParameters << GroupDimensions->SpinBox_DY->text();
497       aParameters << GroupDimensions->SpinBox_DZ->text();
498       anObj = anOper->MakeVectorDXDYDZ( dx, dy, dz );
499
500       if ( !anObj->_is_nil() && !IsPreview() )
501         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
502       
503       res = true;
504       break;
505     }
506   }
507
508   if ( !anObj->_is_nil() )
509     objects.push_back( anObj._retn() );
510
511   return res;
512 }
513
514 //=================================================================================
515 // function : addSubshapeToStudy
516 // purpose  : virtual method to add new SubObjects if local selection
517 //=================================================================================
518 void BasicGUI_VectorDlg::addSubshapesToStudy()
519 {
520   QMap<QString, GEOM::GEOM_Object_var> objMap;
521
522   switch ( getConstructorId() ) {
523   case 0:
524     objMap[GroupPoints->LineEdit1->text()] = myPoint1;
525     objMap[GroupPoints->LineEdit2->text()] = myPoint2;
526     break;
527   case 1:
528     return;
529   }
530   addSubshapesToFather( objMap );
531 }