Salome HOME
updated copyright message
[modules/geom.git] / src / BasicGUI / BasicGUI_VectorDlg.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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.nullify();
123   myPoint2.nullify();
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( GroupDimensions->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
154   connect( GroupDimensions->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
155   connect( GroupDimensions->SpinBox_DZ, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
156
157   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
158
159   connect( GroupDimensions->CheckButton1, SIGNAL( stateChanged( int ) ), this, SLOT( ReverseVector( int ) ) );
160
161   connect( myGeomGUI->getApp()->selectionMgr(),
162            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
163
164   initName( tr("GEOM_VECTOR") );
165
166   setConstructorId( 1 ); // simplest constructor
167   ConstructorsClicked( 1 );
168 }
169
170 //=================================================================================
171 // function : SetDoubleSpinBoxStep()
172 // purpose  : Double spin box management
173 //=================================================================================
174 void BasicGUI_VectorDlg::SetDoubleSpinBoxStep( double step )
175 {
176   GroupDimensions->SpinBox_DX->setSingleStep(step);
177   GroupDimensions->SpinBox_DY->setSingleStep(step);
178   GroupDimensions->SpinBox_DZ->setSingleStep(step);
179 }
180
181
182 //=================================================================================
183 // function : ConstructorsClicked()
184 // purpose  : Radio button management
185 //=================================================================================
186 void BasicGUI_VectorDlg::ConstructorsClicked( int constructorId )
187 {
188   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
189   myPoint1.nullify();
190   myPoint2.nullify();
191
192   switch ( constructorId ) {
193   case 0:
194     {
195       GroupDimensions->hide();
196       GroupPoints->show();
197
198       myEditCurrentArgument = GroupPoints->LineEdit1;
199       GroupPoints->LineEdit1->setText( "" );
200       GroupPoints->LineEdit2->setText( "" );
201       GroupPoints->PushButton1->setDown( true );
202       GroupPoints->PushButton2->setDown( false );
203       GroupPoints->LineEdit1->setEnabled( true );
204       GroupPoints->LineEdit2->setEnabled( false );
205
206       globalSelection(); // close local contexts, if any
207       localSelection( TopAbs_VERTEX );
208       connect( myGeomGUI->getApp()->selectionMgr(),
209                SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
210       break;
211     }
212   case 1:
213     {
214       GroupPoints->hide();
215       GroupDimensions->show();
216       globalSelection(); // close local contexts, if any
217
218       double dx( 0. ), dy( 0. ), dz( 0. );
219       GroupDimensions->SpinBox_DX->setValue( dx );
220       GroupDimensions->SpinBox_DY->setValue( dy );
221       GroupDimensions->SpinBox_DZ->setValue( dz );
222
223       GroupDimensions->CheckButton1->setChecked( false );
224       break;
225     }
226   }
227
228   qApp->processEvents();
229   updateGeometry();
230   resize( minimumSizeHint() );
231   SelectionIntoArgument();
232
233   displayPreview(true);
234 }
235
236
237 //=================================================================================
238 // function : ClickOnOk()
239 // purpose  :
240 //=================================================================================
241 void BasicGUI_VectorDlg::ClickOnOk()
242 {
243   setIsApplyAndClose( true );
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.nullify();
281     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
282       myPoint2.nullify();
283     return;
284   }
285
286   GEOM::GeomObjPtr aSelectedObject = getSelected( TopAbs_VERTEX );
287   TopoDS_Shape aShape;
288   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
289     QString aName = GEOMBase::GetName( aSelectedObject.get() );
290     myEditCurrentArgument->setText(aName);
291     if (myEditCurrentArgument == GroupPoints->LineEdit1) {
292       myPoint1 = aSelectedObject;
293       if (myPoint1 && !myPoint2)
294         GroupPoints->PushButton2->click();
295     }
296     else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
297       myPoint2 = aSelectedObject;
298       if (myPoint2 && !myPoint1)
299         GroupPoints->PushButton1->click();
300     }
301   }
302
303   displayPreview(true);
304 }
305
306 //=================================================================================
307 // function : SetEditCurrentArgument()
308 // purpose  :
309 //=================================================================================
310 void BasicGUI_VectorDlg::SetEditCurrentArgument()
311 {
312   QPushButton* send = (QPushButton*)sender();
313   if      ( send == GroupPoints->PushButton1 ) {
314     myEditCurrentArgument = GroupPoints->LineEdit1;
315     GroupPoints->PushButton2->setDown(false);
316     GroupPoints->LineEdit1->setEnabled(true);
317     GroupPoints->LineEdit2->setEnabled(false);
318   }
319   else if ( send == GroupPoints->PushButton2 ) {
320     myEditCurrentArgument = GroupPoints->LineEdit2;
321     GroupPoints->PushButton1->setDown(false);
322     GroupPoints->LineEdit1->setEnabled(false);
323     GroupPoints->LineEdit2->setEnabled(true);
324   }
325   myEditCurrentArgument->setFocus();
326   //  SelectionIntoArgument();
327   globalSelection(); // close local selection to clear it
328   localSelection( TopAbs_VERTEX );
329   send->setDown(true);
330   displayPreview(true);
331 }
332
333
334 //=================================================================================
335 // function : ActivateThisDialog()
336 // purpose  :
337 //=================================================================================
338 void BasicGUI_VectorDlg::ActivateThisDialog()
339 {
340   GEOMBase_Skeleton::ActivateThisDialog();
341   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
342            this, SLOT( SelectionIntoArgument() ) );
343         
344   ConstructorsClicked( getConstructorId() );
345 }
346
347 //=================================================================================
348 // function : DeactivateActiveDialog()
349 // purpose  : public slot to deactivate if active
350 //=================================================================================
351 void BasicGUI_VectorDlg::DeactivateActiveDialog()
352 {
353   GEOMBase_Skeleton::DeactivateActiveDialog();
354 }
355
356 //=================================================================================
357 // function : enterEvent()
358 // purpose  :
359 //=================================================================================
360 void BasicGUI_VectorDlg::enterEvent( QEvent* )
361 {
362   if ( !mainFrame()->GroupConstructors->isEnabled() )
363     ActivateThisDialog();
364 }
365
366 //=================================================================================
367 // function : ValueChangedInSpinBox()
368 // purpose  :
369 //=================================================================================
370 void BasicGUI_VectorDlg::ValueChangedInSpinBox( double /*newValue*/ )
371 {
372   displayPreview(true);
373 }
374
375 //=================================================================================
376 // function : ReverseVector()
377 // purpose  : 'state' not used here
378 //=================================================================================
379 void BasicGUI_VectorDlg::ReverseVector( int /*state*/ )
380 {
381   double dx = -GroupDimensions->SpinBox_DX->value();
382   double dy = -GroupDimensions->SpinBox_DY->value();
383   double dz = -GroupDimensions->SpinBox_DZ->value();
384
385   GroupDimensions->SpinBox_DX->setValue( dx );
386   GroupDimensions->SpinBox_DY->setValue( dy );
387   GroupDimensions->SpinBox_DZ->setValue( dz );
388
389   displayPreview(true);
390 }
391
392 //=================================================================================
393 // function : createOperation
394 // purpose  :
395 //=================================================================================
396 GEOM::GEOM_IOperations_ptr BasicGUI_VectorDlg::createOperation()
397 {
398   return myGeomGUI->GetGeomGen()->GetIBasicOperations();
399 }
400
401 //=================================================================================
402 // function : isValid
403 // purpose  :
404 //=================================================================================
405 bool BasicGUI_VectorDlg::isValid( QString& msg )
406 {
407   bool ok = false;
408   if(getConstructorId() == 0) 
409     ok = myPoint1 && myPoint2;
410   else if(getConstructorId() == 1)
411   {
412     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) &&
413          GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() ) &&
414          GroupDimensions->SpinBox_DZ->isValid( msg, !IsPreview() );
415   }
416   return ok;
417 }
418
419 //=================================================================================
420 // function : execute
421 // purpose  :
422 //=================================================================================
423 bool BasicGUI_VectorDlg::execute( ObjectList& objects )
424 {
425   bool res = false;
426
427   GEOM::GEOM_Object_var anObj;
428
429   GEOM::GEOM_IBasicOperations_var anOper = GEOM::GEOM_IBasicOperations::_narrow( getOperation() );
430
431   switch ( getConstructorId() ) {
432   case 0 :
433     {
434       anObj = anOper->MakeVectorTwoPnt( myPoint1.get(), myPoint2.get() );
435       res = true;
436       break;
437     }
438   case 1 :
439     {
440       double dx = GroupDimensions->SpinBox_DX->value();
441       double dy = GroupDimensions->SpinBox_DY->value();
442       double dz = GroupDimensions->SpinBox_DZ->value();
443       
444       QStringList aParameters;
445       aParameters << GroupDimensions->SpinBox_DX->text();
446       aParameters << GroupDimensions->SpinBox_DY->text();
447       aParameters << GroupDimensions->SpinBox_DZ->text();
448       anObj = anOper->MakeVectorDXDYDZ( dx, dy, dz );
449
450       if ( !anObj->_is_nil() && !IsPreview() )
451         anObj->SetParameters(aParameters.join(":").toUtf8().constData());
452       
453       res = true;
454       break;
455     }
456   }
457
458   if ( !anObj->_is_nil() )
459     objects.push_back( anObj._retn() );
460
461   return res;
462 }
463
464 //=================================================================================
465 // function : addSubshapeToStudy
466 // purpose  : virtual method to add new SubObjects if local selection
467 //=================================================================================
468 void BasicGUI_VectorDlg::addSubshapesToStudy()
469 {
470   if ( getConstructorId() == 0 ) {
471     GEOMBase::PublishSubObject( myPoint1.get() );
472     GEOMBase::PublishSubObject( myPoint2.get() );
473   }
474 }
475
476 //=================================================================================
477 // function : getSourceObjects
478 // purpose  : virtual method to get source objects
479 //=================================================================================
480 QList<GEOM::GeomObjPtr> BasicGUI_VectorDlg::getSourceObjects()
481 {
482   QList<GEOM::GeomObjPtr> res;
483   res << myPoint1 << myPoint2;
484   return res;
485 }