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