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