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