Salome HOME
5bc166f772fdd94c6dbb5ab6ea4cf7c48feb9c3f
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_FaceDlg.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // GEOM GEOMGUI : GUI for Geometry component
21 // File   : PrimitiveGUI_FaceDlg.cxx
22 // Author : Dmitry Matveitchev, OCN.
23 //
24 #include "PrimitiveGUI_FaceDlg.h"
25
26 #include <DlgRef.h>
27 #include <GeometryGUI.h>
28 #include <GEOMBase.h>
29
30 #include <SUIT_ResourceMgr.h>
31 #include <SUIT_Session.h>
32 #include <SalomeApp_Application.h>
33 #include <LightApp_SelectionMgr.h>
34
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS.hxx>
37 #include <TopExp.hxx>
38 #include <TColStd_IndexedMapOfInteger.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40
41 #include <GEOMImpl_Types.hxx>
42
43 //=================================================================================
44 // class    : PrimitiveGUI_FaceDlg()
45 // purpose  : Constructs a PrimitiveGUI_FaceDlg which is a child of 'parent', with the 
46 //            name 'name' and widget flags set to 'f'.
47 //            The dialog will by default be modeless, unless you set 'modal' to
48 //            TRUE to construct a modal dialog.
49 //=================================================================================
50 PrimitiveGUI_FaceDlg::PrimitiveGUI_FaceDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
51                                     bool modal, Qt::WindowFlags fl )
52   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
53 {
54   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_FACE_OBJ_HW")));
55   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
56   QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_FACE_HW")));
57
58   setWindowTitle( tr( "GEOM_RECTANGLE_TITLE" ) );
59  
60   /***************************************************************/
61   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_RECTANGLE" ) );
62   mainFrame()->RadioButton1->setIcon( image2 );
63   mainFrame()->RadioButton2->setIcon( image0 );
64   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
65   mainFrame()->RadioButton3->close();
66
67   GroupPlane = new DlgRef_1Sel2Spin( centralWidget() );
68   GroupPlane->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
69   GroupPlane->TextLabel1->setText( tr( "GEOM_EDGE" ) );
70   GroupPlane->TextLabel2->setText( tr( "GEOM_HEIGHT" ) );
71   GroupPlane->TextLabel3->setText( tr( "GEOM_WIDTH" ) );
72   GroupPlane->PushButton1->setIcon( image1 );
73   GroupPlane->LineEdit1->setReadOnly( true );
74
75   GroupType = new DlgRef_3Radio( centralWidget() );
76   GroupType->GroupBox1->setTitle( tr( "GEOM_OBJECT_TYPE" ) );
77   GroupType->RadioButton1->setText( tr( "GEOM_EDGE" ) );
78   GroupType->RadioButton2->setText( tr( "GEOM_FACE" ) );
79   GroupType->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
80   GroupType->RadioButton3->close();
81
82   GroupDimensions = new DlgRef_2Spin( centralWidget() );
83   GroupDimensions->GroupBox1->setTitle( tr( "GEOM_BOX_OBJ" ) );
84   GroupDimensions->TextLabel1->setText( tr( "GEOM_HEIGHT" ) );
85   GroupDimensions->TextLabel2->setText( tr( "GEOM_WIDTH" ) );
86
87   GroupOrientation = new DlgRef_3Radio( centralWidget() );
88
89   GroupOrientation->GroupBox1->setTitle( tr( "GEOM_ORIENTATION" ) );
90   GroupOrientation->RadioButton1->setText( tr( "GEOM_WPLANE_OXY" ) );
91   GroupOrientation->RadioButton2->setText( tr( "GEOM_WPLANE_OYZ" ) );
92   GroupOrientation->RadioButton3->setText( tr( "GEOM_WPLANE_OZX" ) );
93
94   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
95   layout->setMargin( 0 ); layout->setSpacing( 6 );
96   layout->addWidget( GroupType );
97   layout->addWidget( GroupPlane );
98   layout->addWidget( GroupDimensions );
99   layout->addWidget( GroupOrientation );
100
101   /***************************************************************/
102
103   setHelpFileName( "create_squareface_page.html" );
104
105   Init();
106 }
107
108
109 //=================================================================================
110 // function : ~PrimitiveGUI_FaceDlg()
111 // purpose  : Destroys the object and frees any allocated resources
112 //=================================================================================
113 PrimitiveGUI_FaceDlg::~PrimitiveGUI_FaceDlg()
114 {  
115 }
116
117
118 //=================================================================================
119 // function : Init()
120 // purpose  :
121 //=================================================================================
122 void PrimitiveGUI_FaceDlg::Init()
123 {
124   /* init variables */
125   myEditCurrentArgument = GroupPlane->LineEdit1;
126   GroupType->RadioButton1->setChecked(true);
127   myEdge.nullify();
128   myFace.nullify();
129   globalSelection(); // close local contexts, if any
130   //  localSelection( TopAbs_EDGE );
131
132   myOrientationType = 1;
133
134   /* Get setting of step value from file configuration */
135   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
136   double aStep = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
137
138   double aDefaultSize = 100.0;
139   /* min, max, step and decimals for spin boxes */
140   initSpinBox( GroupPlane->SpinBox_DX, 0.00001, COORD_MAX, aStep, "length_precision" );
141   GroupPlane->SpinBox_DX->setValue( aDefaultSize );
142   initSpinBox( GroupPlane->SpinBox_DY, 0.00001, COORD_MAX, aStep, "length_precision" );
143   GroupPlane->SpinBox_DY->setValue( aDefaultSize );
144
145   initSpinBox( GroupDimensions->SpinBox_DX, 0.00001, COORD_MAX, aStep, "length_precision" );
146   GroupDimensions->SpinBox_DX->setValue( aDefaultSize );
147   initSpinBox( GroupDimensions->SpinBox_DY, 0.00001, COORD_MAX, aStep, "length_precision" );
148   GroupDimensions->SpinBox_DY->setValue( aDefaultSize );
149
150         
151   /* signals and slots connections */
152   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
153   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
154   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
155
156   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
157
158   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
159   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
160
161   connect( GroupPlane->PushButton1,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
162   connect( GroupPlane->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
163   connect( GroupPlane->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
164
165   connect( GroupDimensions->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
166   connect( GroupDimensions->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
167
168   connect( GroupOrientation->RadioButton1, SIGNAL( clicked() ), this, SLOT( RadioButtonClicked() ) );
169   connect( GroupOrientation->RadioButton2, SIGNAL( clicked() ), this, SLOT( RadioButtonClicked() ) );
170   connect( GroupOrientation->RadioButton3, SIGNAL( clicked() ), this, SLOT( RadioButtonClicked() ) );
171
172   connect( GroupType->RadioButton1, SIGNAL( clicked() ), this, SLOT( TypeButtonClicked() ) );
173   connect( GroupType->RadioButton2, SIGNAL( clicked() ), this, SLOT( TypeButtonClicked() ) );
174
175   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
176            this, SLOT( SelectionIntoArgument() ) );
177   
178   initName( tr( "GEOM_FACE" ) );
179
180   ConstructorsClicked( 0 );
181 }
182
183 //=================================================================================
184 // function : SetDoubleSpinBoxStep()
185 // purpose  : Double spin box management
186 //=================================================================================
187 void PrimitiveGUI_FaceDlg::SetDoubleSpinBoxStep( double step )
188 {
189   GroupPlane->SpinBox_DX->setSingleStep(step);
190   GroupPlane->SpinBox_DY->setSingleStep(step);
191   GroupDimensions->SpinBox_DX->setSingleStep(step);
192   GroupDimensions->SpinBox_DY->setSingleStep(step);
193 }
194
195 //=================================================================================
196 // function : ValueChangedInSpinBox()
197 // purpose  :
198 //=================================================================================
199 void PrimitiveGUI_FaceDlg::ValueChangedInSpinBox( double /*newValue*/ )
200 {
201   displayPreview(true);
202 }
203
204 //=================================================================================
205 // function : RadioBittonClicked()
206 // purpose  : Radio button management
207 //=================================================================================
208 void PrimitiveGUI_FaceDlg::RadioButtonClicked()
209 {
210   if ( GroupOrientation->RadioButton1->isChecked() )
211     myOrientationType = 1;
212   else if ( GroupOrientation->RadioButton2->isChecked() )
213     myOrientationType = 2;
214   else if ( GroupOrientation->RadioButton3->isChecked() )
215     myOrientationType = 3;
216   displayPreview(true);
217 }
218
219 //=================================================================================
220 // function : TypeBittonClicked()
221 // purpose  : Radio button management
222 //=================================================================================
223 void PrimitiveGUI_FaceDlg::TypeButtonClicked()
224 {
225   if ( GroupType->RadioButton1->isChecked() ) {
226     globalSelection(); // close local contexts, if any
227     localSelection( TopAbs_EDGE );
228     GroupPlane->TextLabel1->setText( tr( "GEOM_EDGE" ) );
229   }
230   else if ( GroupType->RadioButton2->isChecked() ) {
231     globalSelection(); // close local contexts, if any
232     localSelection( TopAbs_FACE );
233     GroupPlane->TextLabel1->setText( tr( "GEOM_FACE" ) );
234   }
235   myEditCurrentArgument = GroupPlane->LineEdit1;
236   myEditCurrentArgument->setText( "" );
237   myEdge.nullify();
238   myFace.nullify();
239 }
240
241 //=================================================================================
242 // function : ClickOnOk()
243 // purpose  :
244 //=================================================================================
245 void PrimitiveGUI_FaceDlg::ClickOnOk()
246 {
247   setIsApplyAndClose( true );
248   if ( ClickOnApply() )
249     ClickOnCancel();
250 }
251
252 //=================================================================================
253 // function : ClickOnApply()
254 // purpose  :
255 //=================================================================================
256 bool PrimitiveGUI_FaceDlg::ClickOnApply()
257 {
258   if ( !onAccept() )
259     return false;
260   
261   initName();
262
263   myEditCurrentArgument->setText( "" );
264   ConstructorsClicked( getConstructorId() );
265
266   return true;
267 }
268
269 //=================================================================================
270 // function : ConstructorsClicked()
271 // purpose  : Radio button management
272 //=================================================================================
273 void PrimitiveGUI_FaceDlg::ConstructorsClicked( int constructorId )
274 {  
275   erasePreview();
276   switch ( constructorId ) {
277   case 0:
278     {
279       globalSelection(); // close local contexts, if any
280       GroupPlane->hide();
281       GroupDimensions->show();
282       GroupOrientation->show();
283       GroupType->hide();
284       GroupOrientation->RadioButton1->setChecked( true );
285       myOrientationType = 1;
286       break;
287     }
288   case 1:
289     {
290       globalSelection(); // close local contexts, if any
291       localSelection( TopAbs_EDGE );
292       myEditCurrentArgument = GroupPlane->LineEdit1;
293       myEditCurrentArgument->setText("");
294       myEdge.nullify();
295       myFace.nullify();
296       GroupType->RadioButton1->setChecked( true );
297       TypeButtonClicked();
298       GroupDimensions->hide();
299       GroupOrientation->hide();
300       GroupType->show();
301       GroupPlane->show();
302       break;
303     }
304   }
305
306   qApp->processEvents();
307   updateGeometry();
308   resize( minimumSizeHint() );
309   SelectionIntoArgument();
310   //displayPreview(true);
311 }
312
313 //=================================================================================
314 // function : SelectionIntoArgument()
315 // purpose  : Called when selection as changed or other case
316 //=================================================================================
317 void PrimitiveGUI_FaceDlg::SelectionIntoArgument()
318 {
319   if (getConstructorId() == 0) {
320     displayPreview(true);
321     return;
322   }
323
324   myEditCurrentArgument->setText( "" );
325
326   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
327   SALOME_ListIO aSelList;
328   aSelMgr->selectedObjects(aSelList);
329
330   if ( aSelList.Extent() != 1 ) {
331     if ( myEditCurrentArgument == GroupPlane->LineEdit1 )  myEdge.nullify();
332     return;
333   }
334
335   TopAbs_ShapeEnum aNeedType = GroupType->RadioButton2->isChecked() ? TopAbs_FACE : TopAbs_EDGE;
336   GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
337   TopoDS_Shape aShape;
338   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
339     QString aName = GEOMBase::GetName( aSelectedObject.get() );
340
341     myEditCurrentArgument->setText( aName );
342     
343     if ( myEditCurrentArgument == GroupPlane->LineEdit1 && aNeedType == TopAbs_EDGE )
344       myEdge = aSelectedObject;
345     else if ( myEditCurrentArgument == GroupPlane->LineEdit1 && aNeedType == TopAbs_FACE )
346       myFace = aSelectedObject;
347   }
348
349   displayPreview(true);
350 }
351
352
353 //=================================================================================
354 // function : SetEditCurrentArgument()
355 // purpose  :
356 //=================================================================================
357 void PrimitiveGUI_FaceDlg::SetEditCurrentArgument()
358 {
359   QPushButton* send = (QPushButton*)sender();
360
361   globalSelection(); // close previous local contexts
362   if ( send == GroupPlane->PushButton1 ) {
363     myEditCurrentArgument = GroupPlane->LineEdit1;
364     if (GroupType->RadioButton1->isChecked())
365       localSelection( TopAbs_EDGE );
366     else if (GroupType->RadioButton1->isChecked())
367       localSelection( TopAbs_FACE );
368   }
369
370   myEditCurrentArgument->setFocus();
371   SelectionIntoArgument();
372 }
373
374 //=================================================================================
375 // function : ActivateThisDialog()
376 // purpose  :
377 //=================================================================================
378 void PrimitiveGUI_FaceDlg::ActivateThisDialog()
379 {
380   GEOMBase_Skeleton::ActivateThisDialog();
381   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
382            this, SLOT( SelectionIntoArgument() ) );
383
384   ConstructorsClicked( getConstructorId() );
385 }
386
387 //=================================================================================
388 // function : DeactivateActiveDialog()
389 // purpose  : public slot to deactivate if active
390 //=================================================================================
391 void PrimitiveGUI_FaceDlg::DeactivateActiveDialog()
392 {
393   GEOMBase_Skeleton::DeactivateActiveDialog();
394 }
395
396 //=================================================================================
397 // function : enterEvent()
398 // purpose  :
399 //=================================================================================
400 void PrimitiveGUI_FaceDlg::enterEvent( QEvent* )
401 {
402   if ( !mainFrame()->GroupConstructors->isEnabled() )
403     ActivateThisDialog();
404 }
405
406 //=================================================================================
407 // function : createOperation
408 // purpose  :
409 //=================================================================================
410 GEOM::GEOM_IOperations_ptr PrimitiveGUI_FaceDlg::createOperation()
411 {
412   return myGeomGUI->GetGeomGen()->GetI3DPrimOperations();
413 }
414
415 //=================================================================================
416 // function : isValid
417 // purpose  :
418 //=================================================================================
419 bool PrimitiveGUI_FaceDlg::isValid( QString& msg )
420 {
421   bool ok = false;
422   if( getConstructorId() == 0 )
423   {
424     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) &&
425          GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() );
426   }
427   else if( getConstructorId() == 1 )
428   {
429     ok = GroupPlane->SpinBox_DX->isValid( msg, !IsPreview() ) &&
430          GroupPlane->SpinBox_DY->isValid( msg, !IsPreview() ) &&
431          ( GroupType->RadioButton1->isChecked() ? myEdge : myFace );
432   }
433   return ok;
434 }
435
436 //=================================================================================
437 // function : execute
438 // purpose  :
439 //=================================================================================
440 bool PrimitiveGUI_FaceDlg::execute (ObjectList& objects)
441 {
442   bool res = false;
443   QStringList aParameters;
444   GEOM::GEOM_Object_var anObj;
445
446   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
447
448   switch (getConstructorId()) {
449   case 0:
450     anObj = anOper->MakeFaceHW(GroupDimensions->SpinBox_DX->value(),
451                                GroupDimensions->SpinBox_DY->value(), myOrientationType);
452     if (!anObj->_is_nil() && !IsPreview())
453     {
454       aParameters << GroupDimensions->SpinBox_DX->text();
455       aParameters << GroupDimensions->SpinBox_DY->text();
456       anObj->SetParameters(aParameters.join(":").toUtf8().constData());
457     }
458     res = true;
459     break;
460   case 1:
461     anObj = GroupType->RadioButton1->isChecked() ? 
462       anOper->MakeFaceObjHW(myEdge.get(), GroupPlane->SpinBox_DX->value(), GroupPlane->SpinBox_DY->value()) : 
463       anOper->MakeFaceObjHW(myFace.get(), GroupPlane->SpinBox_DX->value(), GroupPlane->SpinBox_DY->value());
464     if (!anObj->_is_nil() && !IsPreview())
465     {
466       aParameters << GroupPlane->SpinBox_DX->text();
467       aParameters << GroupPlane->SpinBox_DY->text();
468       anObj->SetParameters(aParameters.join(":").toUtf8().constData());
469     }
470     res = true;
471     break;
472   }
473
474   if (!anObj->_is_nil())
475     objects.push_back(anObj._retn());
476
477   return res;
478 }
479
480 //=================================================================================
481 // function : addSubshapeToStudy
482 // purpose  : virtual method to add new SubObjects if local selection
483 //=================================================================================
484 void PrimitiveGUI_FaceDlg::addSubshapesToStudy()
485 {
486   if ( getConstructorId() == 1 ) {
487     if ( GroupType->RadioButton1->isChecked() )
488      GEOMBase::PublishSubObject( myEdge.get() );
489     if ( GroupType->RadioButton2->isChecked() )
490      GEOMBase::PublishSubObject( myFace.get() );
491   }
492 }
493
494 //=================================================================================
495 // function : getSourceObjects
496 // purpose  : virtual method to get source objects
497 //=================================================================================
498 QList<GEOM::GeomObjPtr> PrimitiveGUI_FaceDlg::getSourceObjects()
499 {
500   QList<GEOM::GeomObjPtr> res;
501   res << myEdge << myFace;
502   return res;
503 }