Salome HOME
f811dba46c6c3c083e63dda1158995ef8c599e79
[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   globalSelection( GEOM_ALLSHAPES );
133 }
134
135
136 //=================================================================================
137 // function : ClickOnOk()
138 // purpose  :
139 //=================================================================================
140 void GenerationGUI_RevolDlg::ClickOnOk()
141 {
142   if ( ClickOnApply() )
143     ClickOnCancel();
144 }
145
146
147 //=================================================================================
148 // function : ClickOnApply()
149 // purpose  :
150 //=================================================================================
151 bool GenerationGUI_RevolDlg::ClickOnApply()
152 {
153   if ( !onAccept() )
154     return false;
155
156   initName();
157   return true;
158 }
159
160 //=======================================================================
161 //function : isAcceptableBase
162 //purpose  : return true if theBase can be used as algo argument
163 //=======================================================================
164
165 static bool isAcceptableBase(const TopoDS_Shape& theBase)
166 {
167   switch ( theBase.ShapeType() ) {
168   case TopAbs_VERTEX:
169   case TopAbs_EDGE:
170   case TopAbs_WIRE:
171   case TopAbs_FACE:
172   case TopAbs_SHELL:
173     return true;
174   case TopAbs_SOLID:
175   case TopAbs_COMPSOLID:
176     return false;
177   case TopAbs_COMPOUND: {
178     TopExp_Explorer exp( theBase, TopAbs_SOLID);
179     return !exp.More();
180   }
181   default:
182     return false;
183   }
184   return false;
185 }
186
187 //=================================================================================
188 // function : SelectionIntoArgument()
189 // purpose  : Called when selection as changed or other case
190 //=================================================================================
191 void GenerationGUI_RevolDlg::SelectionIntoArgument()
192 {
193   erasePreview();
194   myEditCurrentArgument->setText("");
195   
196   if(mySelection->IObjectCount() != 1) {
197     if(myEditCurrentArgument == GroupPoints->LineEdit1)
198       myOkBase = false;        
199     else if(myEditCurrentArgument == GroupPoints->LineEdit2)
200       myOkAxis = false;
201     return;
202   }
203   
204   // nbSel == 1
205   Standard_Boolean testResult = Standard_False;
206   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject( mySelection->firstIObject(), testResult );
207   
208   if (!testResult)
209     return;
210
211   if(myEditCurrentArgument == GroupPoints->LineEdit1) {
212     TopoDS_Shape S;
213     myOkBase = false;
214     
215     if ( !GEOMBase::GetShape(aSelectedObject, S) || !isAcceptableBase( S ) )
216       return;
217     
218     myBase = aSelectedObject;
219     myOkBase = true;
220   }
221   else if(myEditCurrentArgument == GroupPoints->LineEdit2) {
222     myAxis = aSelectedObject;
223     myOkAxis = true;
224   }
225   myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
226
227   displayPreview();
228 }
229
230
231 //=================================================================================
232 // function : SetEditCurrentArgument()
233 // purpose  :
234 //=================================================================================
235 void GenerationGUI_RevolDlg::SetEditCurrentArgument()
236 {
237   QPushButton* send = (QPushButton*)sender();
238   globalSelection( GEOM_ALLSHAPES );
239
240   if(send == GroupPoints->PushButton1) {
241     GroupPoints->LineEdit1->setFocus();
242     myEditCurrentArgument = GroupPoints->LineEdit1;
243   }
244   else if(send == GroupPoints->PushButton2) {
245     GroupPoints->LineEdit2->setFocus();
246     myEditCurrentArgument = GroupPoints->LineEdit2;
247     globalSelection( GEOM_LINE );
248   }
249   SelectionIntoArgument();
250 }
251
252
253 //=================================================================================
254 // function : LineEditReturnPressed()
255 // purpose  :
256 //=================================================================================
257 void GenerationGUI_RevolDlg::LineEditReturnPressed()
258 {  
259   QLineEdit* send = (QLineEdit*)sender();
260   if(send == GroupPoints->LineEdit1 ||
261      send == GroupPoints->LineEdit2)
262     {
263       myEditCurrentArgument = send;
264       GEOMBase_Skeleton::LineEditReturnPressed();
265     }
266 }
267
268
269 //=================================================================================
270 // function : ActivateThisDialog()
271 // purpose  :
272 //=================================================================================
273 void GenerationGUI_RevolDlg::ActivateThisDialog()
274 {
275   GEOMBase_Skeleton::ActivateThisDialog();
276   globalSelection( GEOM_ALLSHAPES );
277   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
278   GroupPoints->LineEdit1->setFocus();
279   myEditCurrentArgument = GroupPoints->LineEdit1;
280   displayPreview();
281 }
282
283
284 //=================================================================================
285 // function : enterEvent()
286 // purpose  :
287 //=================================================================================
288 void GenerationGUI_RevolDlg::enterEvent(QEvent* e)
289 {
290   if ( !GroupConstructors->isEnabled() )
291     ActivateThisDialog();
292 }
293
294
295 //=================================================================================
296 // function : ValueChangedInSpinBox()
297 // purpose  :
298 //=================================================================================
299 void GenerationGUI_RevolDlg::ValueChangedInSpinBox()
300 {
301   displayPreview();
302 }
303
304
305 //=================================================================================
306 // function : getAngle()
307 // purpose  :
308 //=================================================================================
309 double GenerationGUI_RevolDlg::getAngle() const
310 {
311   return GroupPoints->SpinBox_DX->GetValue();
312 }
313
314 //=================================================================================
315 // function : createOperation
316 // purpose  :
317 //=================================================================================
318 GEOM::GEOM_IOperations_ptr GenerationGUI_RevolDlg::createOperation()
319 {
320   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
321 }
322
323 //=================================================================================
324 // function : isValid
325 // purpose  :
326 //=================================================================================
327 bool GenerationGUI_RevolDlg::isValid( QString& )
328 {
329   return myOkBase && myOkAxis;
330 }
331
332 //=================================================================================
333 // function : execute
334 // purpose  :
335 //=================================================================================
336 bool GenerationGUI_RevolDlg::execute( ObjectList& objects )
337 {
338   GEOM::GEOM_Object_var anObj;
339
340   anObj = GEOM::GEOM_I3DPrimOperations::_narrow(
341     getOperation() )->MakeRevolutionAxisAngle( myBase, myAxis, getAngle() * PI180 );
342
343   if ( !anObj->_is_nil() )
344     objects.push_back( anObj._retn() );
345
346   return true;
347 }
348
349
350 //=================================================================================
351 // function :  onReverse()
352 // purpose  :
353 //=================================================================================
354 void GenerationGUI_RevolDlg::onReverse()
355 {
356   double anOldValue = GroupPoints->SpinBox_DX->GetValue();
357   GroupPoints->SpinBox_DX->SetValue( -anOldValue );
358 }