Salome HOME
0016757: EDF 546 GEOM : Add Face and Disc in geom primitives
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_FaceDlg.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2003  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 // File   : PrimitiveGUI_FaceDlg.cxx
23 // Author : Dmitry Matveitchev, OCN.
24 //
25
26 #include "PrimitiveGUI_FaceDlg.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    : PrimitiveGUI_FaceDlg()
47 // purpose  : Constructs a PrimitiveGUI_FaceDlg 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 PrimitiveGUI_FaceDlg::PrimitiveGUI_FaceDlg( 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_FACE_PLANE_HW" ) ) );
57   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
58   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_FACE_HW" ) ) );
59
60   setWindowTitle( tr( "GEOM_FACE_TITLE" ) );
61  
62   /***************************************************************/
63   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_FACE" ) );
64   mainFrame()->RadioButton1->setIcon( image0 );
65   mainFrame()->RadioButton2->setIcon( image2 );
66   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
67   mainFrame()->RadioButton3->close();
68
69   GroupPlane = new DlgRef_1Sel2Spin( centralWidget() );
70   GroupPlane->GroupBox1->setTitle( tr( "GEOM_FACES" ) );
71   GroupPlane->TextLabel1->setText( tr( "GEOM_FACE" ) );
72   GroupPlane->TextLabel2->setText( tr( "GEOM_HEIGHT" ) );
73   GroupPlane->TextLabel3->setText( tr( "GEOM_WIDTH" ) );
74   GroupPlane->PushButton1->setIcon( image1 );
75   GroupPlane->LineEdit1->setReadOnly( true );
76
77   GroupDimensions = new DlgRef_2Spin( centralWidget() );
78   GroupDimensions->GroupBox1->setTitle( tr( "GEOM_BOX_OBJ" ) );
79   GroupDimensions->TextLabel1->setText( tr( "GEOM_HEIGHT" ) );
80   GroupDimensions->TextLabel2->setText( tr( "GEOM_WIDTH" ) );
81
82   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
83   layout->setMargin( 0 ); layout->setSpacing( 6 );
84   layout->addWidget( GroupPlane );
85   layout->addWidget( GroupDimensions );
86
87   /***************************************************************/
88
89   setHelpFileName( "create_face_page.html" );
90
91   Init();
92 }
93
94
95 //=================================================================================
96 // function : ~PrimitiveGUI_FaceDlg()
97 // purpose  : Destroys the object and frees any allocated resources
98 //=================================================================================
99 PrimitiveGUI_FaceDlg::~PrimitiveGUI_FaceDlg()
100 {  
101 }
102
103
104 //=================================================================================
105 // function : Init()
106 // purpose  :
107 //=================================================================================
108 void PrimitiveGUI_FaceDlg::Init()
109 {
110   /* init variables */
111   myEditCurrentArgument = GroupPlane->LineEdit1;
112   myFace = GEOM::GEOM_Object::_nil();
113   globalSelection(); // close local contexts, if any
114   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
115
116   /* Get setting of step value from file configuration */
117   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
118   double aStep = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
119
120   double aDefaultSize = 100.0;
121   /* min, max, step and decimals for spin boxes */
122   initSpinBox( GroupPlane->SpinBox_DX, 0.001, COORD_MAX, aStep, 3 ); // VSR:TODO : DBL_DIGITS_DISPLAY
123   GroupPlane->SpinBox_DX->setValue( aDefaultSize );
124   initSpinBox( GroupPlane->SpinBox_DY, 0.001, COORD_MAX, aStep, 3 ); // VSR:TODO : DBL_DIGITS_DISPLAY
125   GroupPlane->SpinBox_DY->setValue( aDefaultSize );
126
127   initSpinBox( GroupDimensions->SpinBox_DX, 0.001, COORD_MAX, aStep, 3 ); // VSR:TODO : DBL_DIGITS_DISPLAY
128   GroupDimensions->SpinBox_DX->setValue( aDefaultSize );
129   initSpinBox( GroupDimensions->SpinBox_DY, 0.001, COORD_MAX, aStep, 3 ); // VSR:TODO : DBL_DIGITS_DISPLAY
130   GroupDimensions->SpinBox_DY->setValue( aDefaultSize );
131
132         
133   /* signals and slots connections */
134   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
135   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
136   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
137
138   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
139
140   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
141   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
142
143   connect( GroupPlane->PushButton1,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
144   connect( GroupPlane->LineEdit1,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
145   connect( GroupPlane->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
146   connect( GroupPlane->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
147
148   connect( GroupDimensions->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
149   connect( GroupDimensions->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
150
151   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
152            this, SLOT( SelectionIntoArgument() ) );
153   
154   initName( tr( "GEOM_FACE" ) );
155
156   ConstructorsClicked( 0 );
157 }
158
159 //=================================================================================
160 // function : SetDoubleSpinBoxStep()
161 // purpose  : Double spin box management
162 //=================================================================================
163 void PrimitiveGUI_FaceDlg::SetDoubleSpinBoxStep( double step )
164 {
165   GroupPlane->SpinBox_DX->setSingleStep(step);
166   GroupPlane->SpinBox_DY->setSingleStep(step);
167 }
168
169 //=================================================================================
170 // function : ValueChangedInSpinBox()
171 // purpose  :
172 //=================================================================================
173 void PrimitiveGUI_FaceDlg::ValueChangedInSpinBox( double newValue )
174 {
175   displayPreview();
176 }
177
178 //=================================================================================
179 // function : ClickOnOk()
180 // purpose  :
181 //=================================================================================
182 void PrimitiveGUI_FaceDlg::ClickOnOk()
183 {
184   if ( ClickOnApply() )
185     ClickOnCancel();
186 }
187
188 //=================================================================================
189 // function : ClickOnApply()
190 // purpose  :
191 //=================================================================================
192 bool PrimitiveGUI_FaceDlg::ClickOnApply()
193 {
194   if ( !onAccept() )
195     return false;
196   
197   initName();
198
199   myEditCurrentArgument->setText( "" );
200   ConstructorsClicked( getConstructorId() );
201
202   return true;
203 }
204
205 //=================================================================================
206 // function : ConstructorsClicked()
207 // purpose  : Radio button management
208 //=================================================================================
209 void PrimitiveGUI_FaceDlg::ConstructorsClicked( int constructorId )
210 {  
211   switch ( constructorId ) {
212   case 0:
213     {
214       globalSelection(); // close local contexts, if any
215       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
216
217       myEditCurrentArgument = GroupPlane->LineEdit1;
218       myEditCurrentArgument->setText("");
219       myFace = GEOM::GEOM_Object::_nil();
220       GroupDimensions->hide();
221       GroupPlane->show();
222       break;
223     }
224   case 1:
225     {
226       globalSelection(); // close local contexts, if any
227       GroupPlane->hide();
228       GroupDimensions->show();
229       break;
230     }
231   }
232
233   qApp->processEvents();
234   updateGeometry();
235   resize( minimumSize() );
236   SelectionIntoArgument();
237 }
238
239 //=================================================================================
240 // function : SelectionIntoArgument()
241 // purpose  : Called when selection as changed or other case
242 //=================================================================================
243 void PrimitiveGUI_FaceDlg::SelectionIntoArgument()
244 {
245   if (getConstructorId() != 0) {
246     displayPreview();
247     return;
248   }
249
250   myEditCurrentArgument->setText( "" );
251
252   if ( IObjectCount() != 1 ) {
253     if ( myEditCurrentArgument == GroupPlane->LineEdit1 )  myFace = GEOM::GEOM_Object::_nil();
254     return;
255   }
256
257   // nbSel == 1 
258   Standard_Boolean aRes = Standard_False;
259   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
260   if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
261     QString aName = GEOMBase::GetName( aSelectedObject );
262     
263     TopoDS_Shape aShape;
264     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
265       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
266       TColStd_IndexedMapOfInteger aMap;
267       aSelMgr->GetIndexes( firstIObject(), aMap );
268       if ( aMap.Extent() == 1 ) { // Local Selection
269         int anIndex = aMap( 1 );
270         aName += QString( ":face_%1" ).arg( anIndex );
271
272         //Find SubShape Object in Father
273         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
274
275         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
276           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
277           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
278         }
279         else
280           aSelectedObject = aFindedObject; // get Object from study
281       }
282       else { // Global Selection
283         if ( aShape.ShapeType() != TopAbs_FACE ) {
284           aSelectedObject = GEOM::GEOM_Object::_nil();
285           aName = "";
286         }
287       }
288     }
289
290     myEditCurrentArgument->setText( aName );
291     if ( myEditCurrentArgument == GroupPlane->LineEdit1 )  myFace = aSelectedObject;
292   }
293   displayPreview();
294 }
295
296
297 //=================================================================================
298 // function : SetEditCurrentArgument()
299 // purpose  :
300 //=================================================================================
301 void PrimitiveGUI_FaceDlg::SetEditCurrentArgument()
302 {
303   QPushButton* send = (QPushButton*)sender();
304
305   globalSelection(); // close previous local contexts
306   if ( send == GroupPlane->PushButton1 ) {
307     myEditCurrentArgument = GroupPlane->LineEdit1;
308     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
309   }
310
311   myEditCurrentArgument->setFocus();
312   SelectionIntoArgument();
313 }
314
315
316 //=================================================================================
317 // function : LineEditReturnPressed()
318 // purpose  :
319 //=================================================================================
320 void PrimitiveGUI_FaceDlg::LineEditReturnPressed()
321 {
322   QLineEdit* send = (QLineEdit*)sender();
323   if ( send == GroupPlane->LineEdit1 )  myEditCurrentArgument = GroupPlane->LineEdit1;
324   else return;
325   GEOMBase_Skeleton::LineEditReturnPressed();
326 }
327
328
329 //=================================================================================
330 // function : ActivateThisDialog()
331 // purpose  :
332 //=================================================================================
333 void PrimitiveGUI_FaceDlg::ActivateThisDialog()
334 {
335   GEOMBase_Skeleton::ActivateThisDialog();
336   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
337            this, SLOT( SelectionIntoArgument() ) );
338
339   ConstructorsClicked( getConstructorId() );
340 }
341
342 //=================================================================================
343 // function : DeactivateActiveDialog()
344 // purpose  : public slot to deactivate if active
345 //=================================================================================
346 void PrimitiveGUI_FaceDlg::DeactivateActiveDialog()
347 {
348   GEOMBase_Skeleton::DeactivateActiveDialog();
349 }
350
351 //=================================================================================
352 // function : enterEvent()
353 // purpose  :
354 //=================================================================================
355 void PrimitiveGUI_FaceDlg::enterEvent( QEvent* )
356 {
357   if ( !mainFrame()->GroupConstructors->isEnabled() )
358     ActivateThisDialog();
359 }
360
361 //=================================================================================
362 // function : createOperation
363 // purpose  :
364 //=================================================================================
365 GEOM::GEOM_IOperations_ptr PrimitiveGUI_FaceDlg::createOperation()
366 {
367   return myGeomGUI->GetGeomGen()->GetI3DPrimOperations( getStudyId() );
368 }
369
370 //=================================================================================
371 // function : isValid
372 // purpose  :
373 //=================================================================================
374 bool PrimitiveGUI_FaceDlg::isValid( QString& msg )
375 {
376   const int id = getConstructorId();
377   if ( id == 0 )
378     return !myFace->_is_nil();
379   else if ( id == 1 )
380     return true;
381   return false;
382 }
383
384 //=================================================================================
385 // function : execute
386 // purpose  :
387 //=================================================================================
388 bool PrimitiveGUI_FaceDlg::execute( ObjectList& objects )
389 {
390   bool res = false;
391   GEOM::GEOM_Object_var anObj;
392   switch ( getConstructorId() ) {
393   case 0 :
394     anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeFacePlaneHW( myFace, GroupPlane->SpinBox_DX->value(), GroupPlane->SpinBox_DY->value() );
395     res = true;
396     break;
397   case 1 :
398     anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeFaceHW( GroupDimensions->SpinBox_DX->value(), GroupDimensions->SpinBox_DY->value() );
399     res = true;
400     break;
401   }
402   
403   if ( !anObj->_is_nil() )
404     objects.push_back( anObj._retn() );
405
406   return res;
407 }
408
409 //=================================================================================
410 // function : addSubshapeToStudy
411 // purpose  : virtual method to add new SubObjects if local selection
412 //=================================================================================
413 void PrimitiveGUI_FaceDlg::addSubshapesToStudy()
414 {
415   QMap<QString, GEOM::GEOM_Object_var> objMap;
416   switch ( getConstructorId() ) {
417   case 0 :
418     objMap[GroupPlane->LineEdit1->text()] = myFace;
419     break;
420   }
421   addSubshapesToFather( objMap );
422 }