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