Salome HOME
DCQ : New Architecture
[modules/geom.git] / src / TransformationGUI / TransformationGUI_RotationDlg.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   : TransformationGUI_RotationDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "TransformationGUI_RotationDlg.h"
31
32 #include <gp_Lin.hxx>
33 #include <BRepBuilderAPI_Transform.hxx>
34 #include <BRepAdaptor_Curve.hxx>
35
36 //=================================================================================
37 // class    : TransformationGUI_RotationDlg()
38 // purpose  : Constructs a TransformationGUI_RotationDlg which is a child of 'parent', with the 
39 //            name 'name' and widget flags set to 'f'.
40 //            The dialog will by default be modeless, unless you set 'modal' to
41 //            TRUE to construct a modal dialog.
42 //=================================================================================
43 TransformationGUI_RotationDlg::TransformationGUI_RotationDlg(QWidget* parent,  const char* name, TransformationGUI* theTransformationGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
44   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
45 {
46   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_ROTATION")));
47   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
48
49   setCaption(tr("GEOM_ROTATION_TITLE"));
50
51   /***************************************************************/
52   GroupConstructors->setTitle(tr("GEOM_ROTATION"));
53   RadioButton1->setPixmap(image0);
54   RadioButton2->close(TRUE);
55   RadioButton3->close(TRUE);
56
57   GroupPoints = new DlgRef_2Sel1Spin1Check(this, "GroupPoints");
58   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
59   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECT"));
60   GroupPoints->TextLabel2->setText(tr("GEOM_AXIS"));
61   GroupPoints->TextLabel3->setText(tr("GEOM_ANGLE"));
62   GroupPoints->CheckButton1->setText(tr("GEOM_REVERSE"));
63   GroupPoints->PushButton1->setPixmap(image1);
64   GroupPoints->PushButton2->setPixmap(image1);
65
66   Layout1->addWidget(GroupPoints, 1, 0);
67   /***************************************************************/
68
69   /* Initialisations */
70   myTransformationGUI = theTransformationGUI;
71   Init();
72 }
73
74
75 //=================================================================================
76 // function : ~TransformationGUI_RotationDlg()
77 // purpose  : Destroys the object and frees any allocated resources
78 //=================================================================================
79 TransformationGUI_RotationDlg::~TransformationGUI_RotationDlg()
80 {
81   // no need to delete child widgets, Qt does it all for us
82 }
83
84
85 //=================================================================================
86 // function : Init()
87 // purpose  :
88 //=================================================================================
89 void TransformationGUI_RotationDlg::Init()
90 {
91   /* init variables */
92   myEditCurrentArgument = GroupPoints->LineEdit1;
93
94   myAngle = 0.0;
95   myOkBase = myOkAxis = false;
96
97   myEdgeFilter = new GEOM_ShapeTypeFilter(TopAbs_EDGE, myGeom);
98
99   double SpecificStep = 5;
100   /* min, max, step and decimals for spin boxes & initial values */
101   GroupPoints->SpinBox_DX->RangeStepAndValidator(-999.999, 999.999, SpecificStep, 3);
102   GroupPoints->SpinBox_DX->SetValue(myAngle);
103
104   /* signals and slots connections */
105   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
106   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
107
108   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
109   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
110
111   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
112   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
113
114   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
115   connect(GroupPoints->CheckButton1, SIGNAL(stateChanged(int)), this, SLOT(ReverseAngle(int)));
116   
117   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
118
119   /* displays Dialog */
120   GroupPoints->show();
121   this->show();
122
123   return;
124 }
125
126
127 //=================================================================================
128 // function : ClickOnOk()
129 // purpose  :
130 //=================================================================================
131 void TransformationGUI_RotationDlg::ClickOnOk()
132 {
133   this->ClickOnApply();
134   ClickOnCancel();
135   return;
136 }
137
138
139 //=================================================================================
140 // function : ClickOnApply()
141 // purpose  :
142 //=================================================================================
143 void TransformationGUI_RotationDlg::ClickOnApply()
144 {
145   QAD_Application::getDesktop()->putInfo(tr(""));
146   if (mySimulationTopoDs.IsNull())
147     return;
148   myGeomBase->EraseSimulationShape();
149   mySimulationTopoDs.Nullify();
150
151   if(myOkBase && myOkAxis)  
152     myTransformationGUI->MakeRotationAndDisplay(myGeomShape, myLoc, myDir, myAngle*PI180); 
153   return;
154 }
155
156
157 //=================================================================================
158 // function : SelectionIntoArgument()
159 // purpose  : Called when selection as changed or other case
160 //=================================================================================
161 void TransformationGUI_RotationDlg::SelectionIntoArgument()
162 {
163   myGeomBase->EraseSimulationShape();
164   mySimulationTopoDs.Nullify();
165   myEditCurrentArgument->setText("");
166   QString aString = ""; /* name of selection */
167   
168   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
169   if(nbSel != 1) {
170     if(myEditCurrentArgument == GroupPoints->LineEdit1)
171       myOkBase = false;
172     else if(myEditCurrentArgument == GroupPoints->LineEdit2)
173       myOkAxis = false;
174     return;
175   }
176   
177   // nbSel == 1
178   TopoDS_Shape S; 
179   Standard_Boolean testResult;
180   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject();
181   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
182     return;
183     
184   if(myEditCurrentArgument == GroupPoints->LineEdit1) {
185     myGeomShape = myGeomBase->ConvertIOinGEOMShape(IO, testResult);
186     if(!testResult)
187       return;
188     myEditCurrentArgument->setText(aString);
189     myOkBase = true;
190     myBase = S;
191   }
192   else if(myEditCurrentArgument == GroupPoints->LineEdit2) {
193     BRepAdaptor_Curve curv(TopoDS::Edge(S));
194     myDir = curv.Line().Direction();
195     myLoc = curv.Line().Location();
196     myEditCurrentArgument->setText(aString);
197     myOkAxis = true;
198   }
199
200   if(myOkBase && myOkAxis)
201     this->MakeRotationSimulationAndDisplay();
202   return; 
203 }
204
205
206 //=================================================================================
207 // function : SetEditCurrentArgument()
208 // purpose  :
209 //=================================================================================
210 void TransformationGUI_RotationDlg::SetEditCurrentArgument()
211 {
212   QPushButton* send = (QPushButton*)sender();
213   mySelection->ClearFilters();
214
215   if(send == GroupPoints->PushButton1) {
216     GroupPoints->LineEdit1->setFocus();
217     myEditCurrentArgument = GroupPoints->LineEdit1;
218   }
219   else if(send == GroupPoints->PushButton2) {
220     GroupPoints->LineEdit2->setFocus();
221     myEditCurrentArgument = GroupPoints->LineEdit2;
222     mySelection->AddFilter(myEdgeFilter);
223   }
224   this->SelectionIntoArgument();
225
226   return;
227 }
228
229
230 //=================================================================================
231 // function : LineEditReturnPressed()
232 // purpose  :
233 //=================================================================================
234 void TransformationGUI_RotationDlg::LineEditReturnPressed()
235 {  
236   QLineEdit* send = (QLineEdit*)sender();
237   if(send == GroupPoints->LineEdit1)
238     myEditCurrentArgument = GroupPoints->LineEdit1;
239   else if (send == GroupPoints->LineEdit2)
240     myEditCurrentArgument = GroupPoints->LineEdit2;
241   else
242     return;
243
244   GEOMBase_Skeleton::LineEditReturnPressed();
245   return;
246 }
247
248
249 //=================================================================================
250 // function : ActivateThisDialog()
251 // purpose  :
252 //=================================================================================
253 void TransformationGUI_RotationDlg::ActivateThisDialog()
254 {
255   GEOMBase_Skeleton::ActivateThisDialog();
256   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
257   GroupPoints->LineEdit1->setFocus();
258   myEditCurrentArgument = GroupPoints->LineEdit1;
259   if(!mySimulationTopoDs.IsNull())
260     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
261   return;
262 }
263
264
265 //=================================================================================
266 // function : enterEvent()
267 // purpose  :
268 //=================================================================================
269 void TransformationGUI_RotationDlg::enterEvent(QEvent* e)
270 {
271   if (GroupConstructors->isEnabled())
272     return;
273   this->ActivateThisDialog();
274   return;
275 }
276
277
278 //=================================================================================
279 // function : ValueChangedInSpinBox()
280 // purpose  :
281 //=================================================================================
282 void TransformationGUI_RotationDlg::ValueChangedInSpinBox(double newValue)
283 {
284   myAngle = newValue;
285   if(myOkBase && myOkAxis)
286     MakeRotationSimulationAndDisplay();
287   return;
288 }
289
290
291 //=================================================================================
292 // function : MakeRotationSimulationAndDisplay()
293 // purpose  :
294 //=================================================================================
295 void TransformationGUI_RotationDlg::MakeRotationSimulationAndDisplay() 
296 {
297   myGeomBase->EraseSimulationShape();
298   mySimulationTopoDs.Nullify();
299
300   try {
301     gp_Ax1 AX(myLoc, myDir);
302     gp_Trsf theTransformation;
303     theTransformation.SetRotation(AX, myAngle*PI180);
304     BRepBuilderAPI_Transform myBRepTransformation(myBase, theTransformation, Standard_False);
305     this->mySimulationTopoDs = myBRepTransformation.Shape();
306     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
307   }
308   catch(Standard_Failure) {
309     MESSAGE("Exception catched in MakeRotationSimulationAndDisplay");
310     return;
311   }
312   return;
313 }
314
315
316 //=================================================================================
317 // function : ReverseAngle()
318 // purpose  : 'state' not used here
319 //=================================================================================
320 void TransformationGUI_RotationDlg::ReverseAngle(int state)
321 {
322   myAngle = -myAngle;
323   GroupPoints->SpinBox_DX->SetValue(myAngle);
324   if(myOkBase && myOkAxis)
325     MakeRotationSimulationAndDisplay();
326   return;
327 }