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