Salome HOME
Implementation of the "21187: EDF 1137 GEOM: Performance issue when manipulating...
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_FaceDlg.cxx
1 //  Copyright (C) 2007-2010  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.
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( GEOM::GEOM_Object::_nil(), 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( GEOM::GEOM_Object::_nil(), 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( GEOM::GEOM_Object::_nil(), 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   if ( ClickOnApply() )
248     ClickOnCancel();
249 }
250
251 //=================================================================================
252 // function : ClickOnApply()
253 // purpose  :
254 //=================================================================================
255 bool PrimitiveGUI_FaceDlg::ClickOnApply()
256 {
257   if ( !onAccept() )
258     return false;
259   
260   initName();
261
262   myEditCurrentArgument->setText( "" );
263   ConstructorsClicked( getConstructorId() );
264
265   return true;
266 }
267
268 //=================================================================================
269 // function : ConstructorsClicked()
270 // purpose  : Radio button management
271 //=================================================================================
272 void PrimitiveGUI_FaceDlg::ConstructorsClicked( int constructorId )
273 {  
274   erasePreview();
275   switch ( constructorId ) {
276   case 0:
277     {
278       globalSelection(); // close local contexts, if any
279       GroupPlane->hide();
280       GroupDimensions->show();
281       GroupOrientation->show();
282       GroupType->hide();
283       GroupOrientation->RadioButton1->setChecked( true );
284       myOrientationType = 1;
285       break;
286     }
287   case 1:
288     {
289       globalSelection(); // close local contexts, if any
290       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
291       myEditCurrentArgument = GroupPlane->LineEdit1;
292       myEditCurrentArgument->setText("");
293       myEdge.nullify();
294       myFace.nullify();
295       GroupType->RadioButton1->setChecked( true );
296       TypeButtonClicked();
297       GroupDimensions->hide();
298       GroupOrientation->hide();
299       GroupType->show();
300       GroupPlane->show();
301       break;
302     }
303   }
304
305   qApp->processEvents();
306   updateGeometry();
307   resize( minimumSizeHint() );
308   SelectionIntoArgument();
309   //displayPreview(true);
310 }
311
312 //=================================================================================
313 // function : SelectionIntoArgument()
314 // purpose  : Called when selection as changed or other case
315 //=================================================================================
316 void PrimitiveGUI_FaceDlg::SelectionIntoArgument()
317 {
318   if (getConstructorId() == 0) {
319     displayPreview(true);
320     return;
321   }
322
323   myEditCurrentArgument->setText( "" );
324
325   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
326   SALOME_ListIO aSelList;
327   aSelMgr->selectedObjects(aSelList);
328
329   if ( aSelList.Extent() != 1 ) {
330     if ( myEditCurrentArgument == GroupPlane->LineEdit1 )  myEdge.nullify();
331     return;
332   }
333
334   TopAbs_ShapeEnum aNeedType = GroupType->RadioButton2->isChecked() ? TopAbs_FACE : TopAbs_EDGE;
335   GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
336   TopoDS_Shape aShape;
337   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
338     QString aName = GEOMBase::GetName( aSelectedObject.get() );
339
340     myEditCurrentArgument->setText( aName );
341     
342     if ( myEditCurrentArgument == GroupPlane->LineEdit1 && aNeedType == TopAbs_EDGE )
343       myEdge = aSelectedObject;
344     else if ( myEditCurrentArgument == GroupPlane->LineEdit1 && aNeedType == TopAbs_FACE )
345       myFace = aSelectedObject;
346   }
347
348   displayPreview(true);
349 }
350
351
352 //=================================================================================
353 // function : SetEditCurrentArgument()
354 // purpose  :
355 //=================================================================================
356 void PrimitiveGUI_FaceDlg::SetEditCurrentArgument()
357 {
358   QPushButton* send = (QPushButton*)sender();
359
360   globalSelection(); // close previous local contexts
361   if ( send == GroupPlane->PushButton1 ) {
362     myEditCurrentArgument = GroupPlane->LineEdit1;
363     if (GroupType->RadioButton1->isChecked())
364       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
365     else if (GroupType->RadioButton1->isChecked())
366       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
367   }
368
369   myEditCurrentArgument->setFocus();
370   SelectionIntoArgument();
371 }
372
373 //=================================================================================
374 // function : ActivateThisDialog()
375 // purpose  :
376 //=================================================================================
377 void PrimitiveGUI_FaceDlg::ActivateThisDialog()
378 {
379   GEOMBase_Skeleton::ActivateThisDialog();
380   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
381            this, SLOT( SelectionIntoArgument() ) );
382
383   ConstructorsClicked( getConstructorId() );
384 }
385
386 //=================================================================================
387 // function : DeactivateActiveDialog()
388 // purpose  : public slot to deactivate if active
389 //=================================================================================
390 void PrimitiveGUI_FaceDlg::DeactivateActiveDialog()
391 {
392   GEOMBase_Skeleton::DeactivateActiveDialog();
393 }
394
395 //=================================================================================
396 // function : enterEvent()
397 // purpose  :
398 //=================================================================================
399 void PrimitiveGUI_FaceDlg::enterEvent( QEvent* )
400 {
401   if ( !mainFrame()->GroupConstructors->isEnabled() )
402     ActivateThisDialog();
403 }
404
405 //=================================================================================
406 // function : createOperation
407 // purpose  :
408 //=================================================================================
409 GEOM::GEOM_IOperations_ptr PrimitiveGUI_FaceDlg::createOperation()
410 {
411   return myGeomGUI->GetGeomGen()->GetI3DPrimOperations( getStudyId() );
412 }
413
414 //=================================================================================
415 // function : isValid
416 // purpose  :
417 //=================================================================================
418 bool PrimitiveGUI_FaceDlg::isValid( QString& msg )
419 {
420   bool ok = false;
421   if( getConstructorId() == 0 )
422   {
423     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) &&
424          GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() );
425   }
426   else if( getConstructorId() == 1 )
427   {
428     ok = GroupPlane->SpinBox_DX->isValid( msg, !IsPreview() ) &&
429          GroupPlane->SpinBox_DY->isValid( msg, !IsPreview() ) &&
430          ( GroupType->RadioButton1->isChecked() ? myEdge : myFace );
431   }
432   return ok;
433 }
434
435 //=================================================================================
436 // function : execute
437 // purpose  :
438 //=================================================================================
439 bool PrimitiveGUI_FaceDlg::execute (ObjectList& objects)
440 {
441   bool res = false;
442   QStringList aParameters;
443   GEOM::GEOM_Object_var anObj;
444
445   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
446
447   switch (getConstructorId()) {
448   case 0:
449     anObj = anOper->MakeFaceHW(GroupDimensions->SpinBox_DX->value(),
450                                GroupDimensions->SpinBox_DY->value(), myOrientationType);
451     if (!anObj->_is_nil() && !IsPreview())
452     {
453       aParameters << GroupDimensions->SpinBox_DX->text();
454       aParameters << GroupDimensions->SpinBox_DY->text();
455       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
456     }
457     res = true;
458     break;
459   case 1:
460     anObj = GroupType->RadioButton1->isChecked() ? 
461       anOper->MakeFaceObjHW(myEdge.get(), GroupPlane->SpinBox_DX->value(), GroupPlane->SpinBox_DY->value()) : 
462       anOper->MakeFaceObjHW(myFace.get(), GroupPlane->SpinBox_DX->value(), GroupPlane->SpinBox_DY->value());
463     if (!anObj->_is_nil() && !IsPreview())
464     {
465       aParameters << GroupPlane->SpinBox_DX->text();
466       aParameters << GroupPlane->SpinBox_DY->text();
467       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
468     }
469     res = true;
470     break;
471   }
472
473   if (!anObj->_is_nil())
474     objects.push_back(anObj._retn());
475
476   return res;
477 }
478
479 //=================================================================================
480 // function : addSubshapeToStudy
481 // purpose  : virtual method to add new SubObjects if local selection
482 //=================================================================================
483 void PrimitiveGUI_FaceDlg::addSubshapesToStudy()
484 {
485   if ( getConstructorId() == 1 ) {
486     if ( GroupType->RadioButton1->isChecked() )
487      GEOMBase::PublishSubObject( myEdge.get() );
488     if ( GroupType->RadioButton2->isChecked() )
489      GEOMBase::PublishSubObject( myFace.get() );
490   }
491 }