Salome HOME
3e6f7fa8e94e8ac55fd12c1b25051799eab47c82
[modules/geom.git] / src / GenerationGUI / GenerationGUI_RevolDlg.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   : GenerationGUI_RevolDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "GenerationGUI_RevolDlg.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_Edge.hxx>
39 #include <TopoDS.hxx>
40 #include <TopExp.hxx>
41 #include <TColStd_IndexedMapOfInteger.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
43 #include <TopExp_Explorer.hxx>
44
45 #include <GEOMImpl_Types.hxx>
46
47 //=================================================================================
48 // class    : GenerationGUI_RevolDlg()
49 // purpose  : Constructs a GenerationGUI_RevolDlg which is a child of 'parent', with the 
50 //            name 'name' and widget flags set to 'f'.
51 //            The dialog will by default be modeless, unless you set 'modal' to
52 //            TRUE to construct a modal dialog.
53 //=================================================================================
54 GenerationGUI_RevolDlg::GenerationGUI_RevolDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
55                                                 bool modal, Qt::WindowFlags fl )
56   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
57 {
58   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_REVOL" ) ) );
59   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
60
61   setWindowTitle( tr( "GEOM_REVOLUTION_TITLE" ) );
62
63   /***************************************************************/
64   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_REVOLUTION" ) );
65   mainFrame()->RadioButton1->setIcon( image0 );
66   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
67   mainFrame()->RadioButton2->close();
68   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
69   mainFrame()->RadioButton3->close();
70   myBothway = false;
71
72   GroupPoints = new DlgRef_2Sel1Spin2Check( centralWidget() );
73   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
74   GroupPoints->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
75   GroupPoints->TextLabel2->setText( tr( "GEOM_AXIS" ) );
76   GroupPoints->TextLabel3->setText( tr( "GEOM_ANGLE" ) );
77   GroupPoints->PushButton1->setIcon( image1 );
78   GroupPoints->PushButton2->setIcon( image1 );
79   GroupPoints->LineEdit1->setReadOnly( true );
80   GroupPoints->LineEdit2->setReadOnly( true );
81   GroupPoints->CheckButton1->setText( tr( "GEOM_BOTHWAY" ) );
82   GroupPoints->CheckButton2->setText( tr( "GEOM_REVERSE" ) );
83
84   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
85   layout->setMargin( 0 ); layout->setSpacing( 6 );
86   layout->addWidget( GroupPoints );
87   /***************************************************************/
88
89   setHelpFileName( "create_revolution_page.html" );
90
91   /* Initialisations */
92   Init();
93 }
94
95
96 //=================================================================================
97 // function : ~GenerationGUI_RevolDlg()
98 // purpose  : Destroys the object and frees any allocated resources
99 //=================================================================================
100 GenerationGUI_RevolDlg::~GenerationGUI_RevolDlg()
101 {
102   // no need to delete child widgets, Qt does it all for us
103 }
104
105
106 //=================================================================================
107 // function : Init()
108 // purpose  :
109 //=================================================================================
110 void GenerationGUI_RevolDlg::Init()
111 {
112   /* init variables */
113   myEditCurrentArgument = GroupPoints->LineEdit1;
114   GroupPoints->LineEdit1->setReadOnly( true );
115   GroupPoints->LineEdit2->setReadOnly( true );
116
117   myOkBase = myOkAxis = false;
118
119   double SpecificStep = 5;
120   /* min, max, step and decimals for spin boxes & initial values */
121   //initSpinBox( GroupPoints->SpinBox_DX, COORD_MIN, COORD_MAX, SpecificStep, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
122   // 05.06.2008 skl for IPAL12958
123   initSpinBox( GroupPoints->SpinBox_DX, 0.0, 360.0, SpecificStep, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
124   GroupPoints->SpinBox_DX->setValue( 45.0 );
125
126   /* signals and slots connections */
127   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
128   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
129
130   connect( GroupPoints->PushButton1,  SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
131   connect( GroupPoints->PushButton2,  SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
132
133   connect( GroupPoints->LineEdit1,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
134   connect( GroupPoints->LineEdit2,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
135
136   connect( GroupPoints->SpinBox_DX,   SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
137   connect( GroupPoints->CheckButton1, SIGNAL( toggled( bool ) ),        this, SLOT( onBothway() ) );
138   connect( GroupPoints->CheckButton2, SIGNAL( toggled( bool ) ),        this, SLOT( onReverse() ) );
139
140   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
141
142   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
143            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
144
145   initName( tr( "GEOM_REVOLUTION" ) );
146
147   globalSelection( GEOM_ALLSHAPES );
148 }
149
150 //=================================================================================
151 // function : SetDoubleSpinBoxStep()
152 // purpose  : Double spin box management
153 //=================================================================================
154 void GenerationGUI_RevolDlg::SetDoubleSpinBoxStep( double step )
155 {
156   GroupPoints->SpinBox_DX->setSingleStep(step);
157 }
158
159
160
161 //=================================================================================
162 // function : ClickOnOk()
163 // purpose  :
164 //=================================================================================
165 void GenerationGUI_RevolDlg::ClickOnOk()
166 {
167   if ( ClickOnApply() )
168     ClickOnCancel();
169 }
170
171
172 //=================================================================================
173 // function : ClickOnApply()
174 // purpose  :
175 //=================================================================================
176 bool GenerationGUI_RevolDlg::ClickOnApply()
177 {
178   if ( !onAccept() )
179     return false;
180
181   initName();
182   return true;
183 }
184
185 //=======================================================================
186 //function : isAcceptableBase
187 //purpose  : return true if theBase can be used as algo argument
188 //=======================================================================
189
190 static bool isAcceptableBase( const TopoDS_Shape& theBase )
191 {
192   switch ( theBase.ShapeType() ) {
193   case TopAbs_VERTEX:
194   case TopAbs_EDGE:
195   case TopAbs_WIRE:
196   case TopAbs_FACE:
197   case TopAbs_SHELL:
198     return true;
199   case TopAbs_SOLID:
200   case TopAbs_COMPSOLID:
201     return false;
202   case TopAbs_COMPOUND: {
203     TopExp_Explorer exp( theBase, TopAbs_SOLID );
204     return !exp.More();
205   }
206   default:
207     return false;
208   }
209   return false;
210 }
211
212 //=================================================================================
213 // function : SelectionIntoArgument()
214 // purpose  : Called when selection as changed or other case
215 //=================================================================================
216 void GenerationGUI_RevolDlg::SelectionIntoArgument()
217 {
218   erasePreview();
219   myEditCurrentArgument->setText( "" );
220   
221   if ( IObjectCount() != 1 ) {
222     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
223       myOkBase = false;        
224     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
225       myOkAxis = false;
226     return;
227   }
228   
229   // nbSel == 1
230   Standard_Boolean testResult = Standard_False;
231   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
232   QString aName = GEOMBase::GetName( aSelectedObject );
233   
234   if ( !testResult )
235     return;
236
237   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
238     TopoDS_Shape S;
239     myOkBase = false;
240     
241     if ( !GEOMBase::GetShape(aSelectedObject, S) || !isAcceptableBase( S ) )
242       return;
243     
244     myBase = aSelectedObject;
245     myOkBase = true;
246   }
247   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
248     if ( testResult && !aSelectedObject->_is_nil() ) {
249       TopoDS_Shape aShape;
250       
251       if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
252         LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
253         TColStd_IndexedMapOfInteger aMap;
254         aSelMgr->GetIndexes( firstIObject(), aMap );
255         if ( aMap.Extent() == 1 ) {
256           
257           int anIndex = aMap( 1 );
258           aName.append( ":edge_" + QString::number( anIndex ) );
259           
260           //Find SubShape Object in Father
261           GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
262           
263           if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
264             GEOM::GEOM_IShapesOperations_var aShapesOp =
265               getGeomEngine()->GetIShapesOperations( getStudyId() );
266             myAxis = aShapesOp->GetSubShape( aSelectedObject, anIndex );
267             myOkAxis = true;
268           }
269           else {
270             myAxis = aFindedObject;
271             myOkAxis = true;
272           }
273         }
274         else {
275           myOkAxis = true;
276           if ( aShape.ShapeType() != TopAbs_EDGE ) {
277             aSelectedObject = GEOM::GEOM_Object::_nil();
278             aName = "";
279             myOkAxis = false;
280           }
281           myAxis = aSelectedObject;
282         }
283       }
284     }
285   }
286
287   myEditCurrentArgument->setText( aName );
288
289   displayPreview();
290 }
291
292
293 //=================================================================================
294 // function : SetEditCurrentArgument()
295 // purpose  :
296 //=================================================================================
297 void GenerationGUI_RevolDlg::SetEditCurrentArgument()
298 {
299   QPushButton* send = (QPushButton*)sender();
300   globalSelection( GEOM_ALLSHAPES );
301
302   if ( send == GroupPoints->PushButton1 ) {
303     GroupPoints->LineEdit1->setFocus();
304     myEditCurrentArgument = GroupPoints->LineEdit1;
305   }
306   else if ( send == GroupPoints->PushButton2 ) {
307     GroupPoints->LineEdit2->setFocus();
308     myEditCurrentArgument = GroupPoints->LineEdit2;
309     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
310   }
311   SelectionIntoArgument();
312 }
313
314
315 //=================================================================================
316 // function : LineEditReturnPressed()
317 // purpose  :
318 //=================================================================================
319 void GenerationGUI_RevolDlg::LineEditReturnPressed()
320 {  
321   QLineEdit* send = (QLineEdit*)sender();
322   if ( send == GroupPoints->LineEdit1 ||
323        send == GroupPoints->LineEdit2 ) {
324     myEditCurrentArgument = send;
325     GEOMBase_Skeleton::LineEditReturnPressed();
326   }
327 }
328
329
330 //=================================================================================
331 // function : ActivateThisDialog()
332 // purpose  :
333 //=================================================================================
334 void GenerationGUI_RevolDlg::ActivateThisDialog()
335 {
336   GEOMBase_Skeleton::ActivateThisDialog();
337   globalSelection( GEOM_ALLSHAPES );
338   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), SIGNAL(currentSelectionChanged()), this, SLOT( SelectionIntoArgument() ) );
339   GroupPoints->LineEdit1->setFocus();
340   myEditCurrentArgument = GroupPoints->LineEdit1;
341   displayPreview();
342 }
343
344
345 //=================================================================================
346 // function : enterEvent()
347 // purpose  :
348 //=================================================================================
349 void GenerationGUI_RevolDlg::enterEvent( QEvent* )
350 {
351   if ( !mainFrame()->GroupConstructors->isEnabled() )
352     ActivateThisDialog();
353 }
354
355
356 //=================================================================================
357 // function : ValueChangedInSpinBox()
358 // purpose  :
359 //=================================================================================
360 void GenerationGUI_RevolDlg::ValueChangedInSpinBox()
361 {
362   displayPreview();
363 }
364
365
366 //=================================================================================
367 // function : getAngle()
368 // purpose  :
369 //=================================================================================
370 double GenerationGUI_RevolDlg::getAngle() const
371 {
372   return GroupPoints->SpinBox_DX->value();
373 }
374
375 //=================================================================================
376 // function : createOperation
377 // purpose  :
378 //=================================================================================
379 GEOM::GEOM_IOperations_ptr GenerationGUI_RevolDlg::createOperation()
380 {
381   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
382 }
383
384 //=================================================================================
385 // function : isValid
386 // purpose  :
387 //=================================================================================
388 bool GenerationGUI_RevolDlg::isValid( QString& )
389 {
390   return myOkBase && myOkAxis;
391 }
392
393 //=================================================================================
394 // function : execute
395 // purpose  :
396 //=================================================================================
397 bool GenerationGUI_RevolDlg::execute( ObjectList& objects )
398 {
399   GEOM::GEOM_Object_var anObj;
400
401   if ( !myBothway ) {
402     anObj = GEOM::GEOM_I3DPrimOperations::_narrow(
403       getOperation() )->MakeRevolutionAxisAngle( myBase, myAxis, getAngle() * PI180 );
404   }
405   else {
406     anObj = GEOM::GEOM_I3DPrimOperations::_narrow(
407       getOperation() )->MakeRevolutionAxisAngle2Ways( myBase, myAxis, getAngle() * PI180 );
408   }
409
410   if ( !anObj->_is_nil() )
411     objects.push_back( anObj._retn() );
412
413   return true;
414 }
415
416
417 //=================================================================================
418 // function :  onReverse()
419 // purpose  :
420 //=================================================================================
421 void GenerationGUI_RevolDlg::onReverse()
422 {
423   double anOldValue = GroupPoints->SpinBox_DX->value();
424   GroupPoints->SpinBox_DX->setValue( -anOldValue );
425 }
426
427
428 //=================================================================================
429 // function :  onBothway()
430 // purpose  :
431 //=================================================================================
432 void GenerationGUI_RevolDlg::onBothway()
433 {
434   bool anOldValue = myBothway;
435   myBothway = !anOldValue;
436   GroupPoints->CheckButton2->setEnabled( !myBothway );  
437   displayPreview();
438 }
439
440 //=================================================================================
441 // function : addSubshapeToStudy
442 // purpose  : virtual method to add new SubObjects if local selection
443 //=================================================================================
444 void GenerationGUI_RevolDlg::addSubshapesToStudy()
445 {
446   QMap<QString, GEOM::GEOM_Object_var> objMap;
447
448   objMap[GroupPoints->LineEdit2->text()] = myAxis;
449
450   addSubshapesToFather( objMap );
451 }