Salome HOME
25872683d28172aa800bf6662eb4d35cd965c90d
[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   GroupPoints->SpinBox_DX->setValue( 45.0 );
123
124   /* signals and slots connections */
125   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
126   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
127
128   connect( GroupPoints->PushButton1,  SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
129   connect( GroupPoints->PushButton2,  SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
130
131   connect( GroupPoints->LineEdit1,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
132   connect( GroupPoints->LineEdit2,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
133
134   connect( GroupPoints->SpinBox_DX,   SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
135   connect( GroupPoints->CheckButton1, SIGNAL( toggled( bool ) ),        this, SLOT( onBothway() ) );
136   connect( GroupPoints->CheckButton2, SIGNAL( toggled( bool ) ),        this, SLOT( onReverse() ) );
137
138   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
139
140   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
141            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
142
143   initName( tr( "GEOM_REVOLUTION" ) );
144
145   globalSelection( GEOM_ALLSHAPES );
146 }
147
148 //=================================================================================
149 // function : SetDoubleSpinBoxStep()
150 // purpose  : Double spin box management
151 //=================================================================================
152 void GenerationGUI_RevolDlg::SetDoubleSpinBoxStep( double step )
153 {
154   GroupPoints->SpinBox_DX->setSingleStep(step);
155 }
156
157
158
159 //=================================================================================
160 // function : ClickOnOk()
161 // purpose  :
162 //=================================================================================
163 void GenerationGUI_RevolDlg::ClickOnOk()
164 {
165   if ( ClickOnApply() )
166     ClickOnCancel();
167 }
168
169
170 //=================================================================================
171 // function : ClickOnApply()
172 // purpose  :
173 //=================================================================================
174 bool GenerationGUI_RevolDlg::ClickOnApply()
175 {
176   if ( !onAccept() )
177     return false;
178
179   initName();
180   return true;
181 }
182
183 //=======================================================================
184 //function : isAcceptableBase
185 //purpose  : return true if theBase can be used as algo argument
186 //=======================================================================
187
188 static bool isAcceptableBase( const TopoDS_Shape& theBase )
189 {
190   switch ( theBase.ShapeType() ) {
191   case TopAbs_VERTEX:
192   case TopAbs_EDGE:
193   case TopAbs_WIRE:
194   case TopAbs_FACE:
195   case TopAbs_SHELL:
196     return true;
197   case TopAbs_SOLID:
198   case TopAbs_COMPSOLID:
199     return false;
200   case TopAbs_COMPOUND: {
201     TopExp_Explorer exp( theBase, TopAbs_SOLID );
202     return !exp.More();
203   }
204   default:
205     return false;
206   }
207   return false;
208 }
209
210 //=================================================================================
211 // function : SelectionIntoArgument()
212 // purpose  : Called when selection as changed or other case
213 //=================================================================================
214 void GenerationGUI_RevolDlg::SelectionIntoArgument()
215 {
216   erasePreview();
217   myEditCurrentArgument->setText( "" );
218   
219   if ( IObjectCount() != 1 ) {
220     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
221       myOkBase = false;        
222     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
223       myOkAxis = false;
224     return;
225   }
226   
227   // nbSel == 1
228   Standard_Boolean testResult = Standard_False;
229   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
230   QString aName = GEOMBase::GetName( aSelectedObject );
231   
232   if ( !testResult )
233     return;
234
235   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
236     TopoDS_Shape S;
237     myOkBase = false;
238     
239     if ( !GEOMBase::GetShape(aSelectedObject, S) || !isAcceptableBase( S ) )
240       return;
241     
242     myBase = aSelectedObject;
243     myOkBase = true;
244   }
245   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
246     if ( testResult && !aSelectedObject->_is_nil() ) {
247       TopoDS_Shape aShape;
248       
249       if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
250         LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
251         TColStd_IndexedMapOfInteger aMap;
252         aSelMgr->GetIndexes( firstIObject(), aMap );
253         if ( aMap.Extent() == 1 ) {
254           
255           int anIndex = aMap( 1 );
256           aName.append( ":edge_" + QString::number( anIndex ) );
257           
258           //Find SubShape Object in Father
259           GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
260           
261           if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
262             GEOM::GEOM_IShapesOperations_var aShapesOp =
263               getGeomEngine()->GetIShapesOperations( getStudyId() );
264             myAxis = aShapesOp->GetSubShape( aSelectedObject, anIndex );
265             myOkAxis = true;
266           }
267           else {
268             myAxis = aFindedObject;
269             myOkAxis = true;
270           }
271         }
272         else {
273           myOkAxis = true;
274           if ( aShape.ShapeType() != TopAbs_EDGE ) {
275             aSelectedObject = GEOM::GEOM_Object::_nil();
276             aName = "";
277             myOkAxis = false;
278           }
279           myAxis = aSelectedObject;
280         }
281       }
282     }
283   }
284
285   myEditCurrentArgument->setText( aName );
286
287   displayPreview();
288 }
289
290
291 //=================================================================================
292 // function : SetEditCurrentArgument()
293 // purpose  :
294 //=================================================================================
295 void GenerationGUI_RevolDlg::SetEditCurrentArgument()
296 {
297   QPushButton* send = (QPushButton*)sender();
298   globalSelection( GEOM_ALLSHAPES );
299
300   if ( send == GroupPoints->PushButton1 ) {
301     GroupPoints->LineEdit1->setFocus();
302     myEditCurrentArgument = GroupPoints->LineEdit1;
303   }
304   else if ( send == GroupPoints->PushButton2 ) {
305     GroupPoints->LineEdit2->setFocus();
306     myEditCurrentArgument = GroupPoints->LineEdit2;
307     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
308   }
309   SelectionIntoArgument();
310 }
311
312
313 //=================================================================================
314 // function : LineEditReturnPressed()
315 // purpose  :
316 //=================================================================================
317 void GenerationGUI_RevolDlg::LineEditReturnPressed()
318 {  
319   QLineEdit* send = (QLineEdit*)sender();
320   if ( send == GroupPoints->LineEdit1 ||
321        send == GroupPoints->LineEdit2 ) {
322     myEditCurrentArgument = send;
323     GEOMBase_Skeleton::LineEditReturnPressed();
324   }
325 }
326
327
328 //=================================================================================
329 // function : ActivateThisDialog()
330 // purpose  :
331 //=================================================================================
332 void GenerationGUI_RevolDlg::ActivateThisDialog()
333 {
334   GEOMBase_Skeleton::ActivateThisDialog();
335   globalSelection( GEOM_ALLSHAPES );
336   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), SIGNAL(currentSelectionChanged()), this, SLOT( SelectionIntoArgument() ) );
337   GroupPoints->LineEdit1->setFocus();
338   myEditCurrentArgument = GroupPoints->LineEdit1;
339   displayPreview();
340 }
341
342
343 //=================================================================================
344 // function : enterEvent()
345 // purpose  :
346 //=================================================================================
347 void GenerationGUI_RevolDlg::enterEvent( QEvent* )
348 {
349   if ( !mainFrame()->GroupConstructors->isEnabled() )
350     ActivateThisDialog();
351 }
352
353
354 //=================================================================================
355 // function : ValueChangedInSpinBox()
356 // purpose  :
357 //=================================================================================
358 void GenerationGUI_RevolDlg::ValueChangedInSpinBox()
359 {
360   displayPreview();
361 }
362
363
364 //=================================================================================
365 // function : getAngle()
366 // purpose  :
367 //=================================================================================
368 double GenerationGUI_RevolDlg::getAngle() const
369 {
370   return GroupPoints->SpinBox_DX->value();
371 }
372
373 //=================================================================================
374 // function : createOperation
375 // purpose  :
376 //=================================================================================
377 GEOM::GEOM_IOperations_ptr GenerationGUI_RevolDlg::createOperation()
378 {
379   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
380 }
381
382 //=================================================================================
383 // function : isValid
384 // purpose  :
385 //=================================================================================
386 bool GenerationGUI_RevolDlg::isValid( QString& )
387 {
388   return myOkBase && myOkAxis;
389 }
390
391 //=================================================================================
392 // function : execute
393 // purpose  :
394 //=================================================================================
395 bool GenerationGUI_RevolDlg::execute( ObjectList& objects )
396 {
397   GEOM::GEOM_Object_var anObj;
398
399   if ( !myBothway ) {
400     anObj = GEOM::GEOM_I3DPrimOperations::_narrow(
401       getOperation() )->MakeRevolutionAxisAngle( myBase, myAxis, getAngle() * PI180 );
402   }
403   else {
404     anObj = GEOM::GEOM_I3DPrimOperations::_narrow(
405       getOperation() )->MakeRevolutionAxisAngle2Ways( myBase, myAxis, getAngle() * PI180 );
406   }
407
408   if ( !anObj->_is_nil() )
409     objects.push_back( anObj._retn() );
410
411   return true;
412 }
413
414
415 //=================================================================================
416 // function :  onReverse()
417 // purpose  :
418 //=================================================================================
419 void GenerationGUI_RevolDlg::onReverse()
420 {
421   double anOldValue = GroupPoints->SpinBox_DX->value();
422   GroupPoints->SpinBox_DX->setValue( -anOldValue );
423 }
424
425
426 //=================================================================================
427 // function :  onBothway()
428 // purpose  :
429 //=================================================================================
430 void GenerationGUI_RevolDlg::onBothway()
431 {
432   bool anOldValue = myBothway;
433   myBothway = !anOldValue;
434   GroupPoints->CheckButton2->setEnabled( !myBothway );  
435   displayPreview();
436 }
437
438 //=================================================================================
439 // function : addSubshapeToStudy
440 // purpose  : virtual method to add new SubObjects if local selection
441 //=================================================================================
442 void GenerationGUI_RevolDlg::addSubshapesToStudy()
443 {
444   QMap<QString, GEOM::GEOM_Object_var> objMap;
445
446   objMap[GroupPoints->LineEdit2->text()] = myAxis;
447
448   addSubshapesToFather( objMap );
449 }