Salome HOME
d5945e1bdf47f0d785c9c94da19c1d3b516a8bf8
[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 <TopExp_Explorer.hxx>
38 #include <GEOMImpl_Types.hxx>
39
40 //=================================================================================
41 // class    : GenerationGUI_RevolDlg()
42 // purpose  : Constructs a GenerationGUI_RevolDlg which is a child of 'parent', with the 
43 //            name 'name' and widget flags set to 'f'.
44 //            The dialog will by default be modeless, unless you set 'modal' to
45 //            TRUE to construct a modal dialog.
46 //=================================================================================
47 GenerationGUI_RevolDlg::GenerationGUI_RevolDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
48                                                 bool modal, Qt::WindowFlags fl )
49   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
50 {
51   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_REVOL" ) ) );
52   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
53
54   setWindowTitle( tr( "GEOM_REVOLUTION_TITLE" ) );
55
56   /***************************************************************/
57   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_REVOLUTION" ) );
58   mainFrame()->RadioButton1->setIcon( image0 );
59   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
60   mainFrame()->RadioButton2->close();
61   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
62   mainFrame()->RadioButton3->close();
63
64   GroupPoints = new DlgRef_2Sel1Spin2Check( centralWidget() );
65   GroupPoints->CheckButton1->hide();
66   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
67   GroupPoints->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
68   GroupPoints->TextLabel2->setText( tr( "GEOM_AXIS" ) );
69   GroupPoints->TextLabel3->setText( tr( "GEOM_ANGLE" ) );
70   GroupPoints->PushButton1->setIcon( image1 );
71   GroupPoints->PushButton2->setIcon( image1 );
72   GroupPoints->LineEdit1->setReadOnly( true );
73   GroupPoints->LineEdit2->setReadOnly( true );
74   GroupPoints->CheckButton2->setText( tr( "GEOM_REVERSE" ) );
75
76   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
77   layout->setMargin( 0 ); layout->setSpacing( 6 );
78   layout->addWidget( GroupPoints );
79   /***************************************************************/
80
81   setHelpFileName( "revolution.htm" );
82
83   /* Initialisations */
84   Init();
85 }
86
87
88 //=================================================================================
89 // function : ~GenerationGUI_RevolDlg()
90 // purpose  : Destroys the object and frees any allocated resources
91 //=================================================================================
92 GenerationGUI_RevolDlg::~GenerationGUI_RevolDlg()
93 {
94   // no need to delete child widgets, Qt does it all for us
95 }
96
97
98 //=================================================================================
99 // function : Init()
100 // purpose  :
101 //=================================================================================
102 void GenerationGUI_RevolDlg::Init()
103 {
104   /* init variables */
105   myEditCurrentArgument = GroupPoints->LineEdit1;
106   GroupPoints->LineEdit1->setReadOnly( true );
107   GroupPoints->LineEdit2->setReadOnly( true );
108
109   myOkBase = myOkAxis = false;
110
111   double SpecificStep = 5;
112   /* min, max, step and decimals for spin boxes & initial values */
113   initSpinBox( GroupPoints->SpinBox_DX, COORD_MIN, COORD_MAX, SpecificStep, 3 );
114   GroupPoints->SpinBox_DX->setValue( 45.0 );
115
116   /* signals and slots connections */
117   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
118   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
119
120   connect( GroupPoints->PushButton1,  SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
121   connect( GroupPoints->PushButton2,  SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
122
123   connect( GroupPoints->LineEdit1,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
124   connect( GroupPoints->LineEdit2,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
125
126   connect( GroupPoints->SpinBox_DX,   SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
127   connect( GroupPoints->CheckButton2, SIGNAL( toggled( bool ) ),        this, SLOT( onReverse() ) );
128
129   // VSR: TODO ->>
130   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupPoints->SpinBox_DX, SLOT( SetStep( double ) ) );
131   // <<-
132
133   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
134            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
135
136   initName( tr( "GEOM_REVOLUTION" ) );
137
138   globalSelection( GEOM_ALLSHAPES );
139 }
140
141
142 //=================================================================================
143 // function : ClickOnOk()
144 // purpose  :
145 //=================================================================================
146 void GenerationGUI_RevolDlg::ClickOnOk()
147 {
148   if ( ClickOnApply() )
149     ClickOnCancel();
150 }
151
152
153 //=================================================================================
154 // function : ClickOnApply()
155 // purpose  :
156 //=================================================================================
157 bool GenerationGUI_RevolDlg::ClickOnApply()
158 {
159   if ( !onAccept() )
160     return false;
161
162   initName();
163   return true;
164 }
165
166 //=======================================================================
167 //function : isAcceptableBase
168 //purpose  : return true if theBase can be used as algo argument
169 //=======================================================================
170
171 static bool isAcceptableBase( const TopoDS_Shape& theBase )
172 {
173   switch ( theBase.ShapeType() ) {
174   case TopAbs_VERTEX:
175   case TopAbs_EDGE:
176   case TopAbs_WIRE:
177   case TopAbs_FACE:
178   case TopAbs_SHELL:
179     return true;
180   case TopAbs_SOLID:
181   case TopAbs_COMPSOLID:
182     return false;
183   case TopAbs_COMPOUND: {
184     TopExp_Explorer exp( theBase, TopAbs_SOLID );
185     return !exp.More();
186   }
187   default:
188     return false;
189   }
190   return false;
191 }
192
193 //=================================================================================
194 // function : SelectionIntoArgument()
195 // purpose  : Called when selection as changed or other case
196 //=================================================================================
197 void GenerationGUI_RevolDlg::SelectionIntoArgument()
198 {
199   erasePreview();
200   myEditCurrentArgument->setText( "" );
201   
202   if ( IObjectCount() != 1 ) {
203     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
204       myOkBase = false;        
205     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
206       myOkAxis = false;
207     return;
208   }
209   
210   // nbSel == 1
211   Standard_Boolean testResult = Standard_False;
212   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
213   
214   if ( !testResult )
215     return;
216
217   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
218     TopoDS_Shape S;
219     myOkBase = false;
220     
221     if ( !GEOMBase::GetShape(aSelectedObject, S) || !isAcceptableBase( S ) )
222       return;
223     
224     myBase = aSelectedObject;
225     myOkBase = true;
226   }
227   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
228     myAxis = aSelectedObject;
229     myOkAxis = true;
230   }
231   myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
232
233   displayPreview();
234 }
235
236
237 //=================================================================================
238 // function : SetEditCurrentArgument()
239 // purpose  :
240 //=================================================================================
241 void GenerationGUI_RevolDlg::SetEditCurrentArgument()
242 {
243   QPushButton* send = (QPushButton*)sender();
244   globalSelection( GEOM_ALLSHAPES );
245
246   if ( send == GroupPoints->PushButton1 ) {
247     GroupPoints->LineEdit1->setFocus();
248     myEditCurrentArgument = GroupPoints->LineEdit1;
249   }
250   else if ( send == GroupPoints->PushButton2 ) {
251     GroupPoints->LineEdit2->setFocus();
252     myEditCurrentArgument = GroupPoints->LineEdit2;
253     globalSelection( GEOM_LINE );
254   }
255   SelectionIntoArgument();
256 }
257
258
259 //=================================================================================
260 // function : LineEditReturnPressed()
261 // purpose  :
262 //=================================================================================
263 void GenerationGUI_RevolDlg::LineEditReturnPressed()
264 {  
265   QLineEdit* send = (QLineEdit*)sender();
266   if ( send == GroupPoints->LineEdit1 ||
267        send == GroupPoints->LineEdit2 ) {
268     myEditCurrentArgument = send;
269     GEOMBase_Skeleton::LineEditReturnPressed();
270   }
271 }
272
273
274 //=================================================================================
275 // function : ActivateThisDialog()
276 // purpose  :
277 //=================================================================================
278 void GenerationGUI_RevolDlg::ActivateThisDialog()
279 {
280   GEOMBase_Skeleton::ActivateThisDialog();
281   globalSelection( GEOM_ALLSHAPES );
282   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), SIGNAL(currentSelectionChanged()), this, SLOT( SelectionIntoArgument() ) );
283   GroupPoints->LineEdit1->setFocus();
284   myEditCurrentArgument = GroupPoints->LineEdit1;
285   displayPreview();
286 }
287
288
289 //=================================================================================
290 // function : enterEvent()
291 // purpose  :
292 //=================================================================================
293 void GenerationGUI_RevolDlg::enterEvent( QEvent* )
294 {
295   if ( !mainFrame()->GroupConstructors->isEnabled() )
296     ActivateThisDialog();
297 }
298
299
300 //=================================================================================
301 // function : ValueChangedInSpinBox()
302 // purpose  :
303 //=================================================================================
304 void GenerationGUI_RevolDlg::ValueChangedInSpinBox()
305 {
306   displayPreview();
307 }
308
309
310 //=================================================================================
311 // function : getAngle()
312 // purpose  :
313 //=================================================================================
314 double GenerationGUI_RevolDlg::getAngle() const
315 {
316   return GroupPoints->SpinBox_DX->value();
317 }
318
319 //=================================================================================
320 // function : createOperation
321 // purpose  :
322 //=================================================================================
323 GEOM::GEOM_IOperations_ptr GenerationGUI_RevolDlg::createOperation()
324 {
325   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
326 }
327
328 //=================================================================================
329 // function : isValid
330 // purpose  :
331 //=================================================================================
332 bool GenerationGUI_RevolDlg::isValid( QString& )
333 {
334   return myOkBase && myOkAxis;
335 }
336
337 //=================================================================================
338 // function : execute
339 // purpose  :
340 //=================================================================================
341 bool GenerationGUI_RevolDlg::execute( ObjectList& objects )
342 {
343   GEOM::GEOM_Object_var anObj;
344
345   anObj = GEOM::GEOM_I3DPrimOperations::_narrow(
346     getOperation() )->MakeRevolutionAxisAngle( myBase, myAxis, getAngle() * PI180 );
347
348   if ( !anObj->_is_nil() )
349     objects.push_back( anObj._retn() );
350
351   return true;
352 }
353
354
355 //=================================================================================
356 // function :  onReverse()
357 // purpose  :
358 //=================================================================================
359 void GenerationGUI_RevolDlg::onReverse()
360 {
361   double anOldValue = GroupPoints->SpinBox_DX->value();
362   GroupPoints->SpinBox_DX->setValue( -anOldValue );
363 }