Salome HOME
PAL8395: Improve RemoveExtraEdges
[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 #include "TransformationGUI_ScaleDlg.h"
30
31 #include "QAD_Desktop.h"
32
33 #include "GEOMImpl_Types.hxx"
34
35 #include <qcheckbox.h>
36
37 #include "utilities.h"
38
39 using namespace std;
40
41 //=================================================================================
42 // class    : TransformationGUI_ScaleDlg()
43 // purpose  : Constructs a TransformationGUI_ScaleDlg which is a child of 'parent', with the 
44 //            name 'name' and widget flags set to 'f'.
45 //            The dialog will by default be modeless, unless you set 'modal' to
46 //            TRUE to construct a modal dialog.
47 //=================================================================================
48 TransformationGUI_ScaleDlg::TransformationGUI_ScaleDlg(QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
49   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
50 {
51   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_SCALE")));
52   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
53
54   setCaption(tr("GEOM_SCALE_TITLE"));
55
56   /***************************************************************/
57   GroupConstructors->setTitle(tr("GEOM_SCALE"));
58   RadioButton1->setPixmap(image0);
59   RadioButton2->close(TRUE);
60   RadioButton3->close(TRUE);
61
62   GroupPoints = new DlgRef_2Sel1Spin2Check(this, "GroupPoints");
63   GroupPoints->CheckButton2->hide();
64   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
65   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
66   GroupPoints->TextLabel2->setText(tr("GEOM_CENTRAL_POINT"));
67   GroupPoints->TextLabel3->setText(tr("GEOM_SCALE_FACTOR"));
68   GroupPoints->LineEdit1->setReadOnly( true );
69   GroupPoints->LineEdit2->setReadOnly( true );
70   GroupPoints->PushButton1->setPixmap(image1);
71   GroupPoints->PushButton2->setPixmap(image1);
72   GroupPoints->CheckButton1->setText(tr("GEOM_CREATE_COPY"));
73
74   // san -- modification of an exisitng object by offset is not allowed
75   GroupPoints->CheckButton1->hide();
76
77   Layout1->addWidget(GroupPoints, 2, 0);
78   /***************************************************************/
79   double aFactor = 2.0;
80   double SpecificStep = 0.5;
81   /* min, max, step and decimals for spin boxes & initial values */
82   GroupPoints->SpinBox_DX->RangeStepAndValidator(-999.999, 999.999, SpecificStep, 3);
83   GroupPoints->SpinBox_DX->SetValue(aFactor);
84   
85   // Activate Create a Copy mode
86   GroupPoints->CheckButton1->setChecked(true);
87   CreateCopyModeChanged(true);
88
89   /* signals and slots connections */
90   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
91   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
92   
93   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
94   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
95   
96   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
97   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
98
99   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
100   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
101   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
102    
103   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
104
105   Init();
106 }
107
108
109 //=================================================================================
110 // function : ~TransformationGUI_ScaleDlg()
111 // purpose  : Destroys the object and frees any allocated resources
112 //=================================================================================
113 TransformationGUI_ScaleDlg::~TransformationGUI_ScaleDlg()
114 {  
115   /* no need to delete child widgets, Qt does it all for us */
116 }
117
118
119 //=================================================================================
120 // function : Init()
121 // purpose  :
122 //=================================================================================
123 void TransformationGUI_ScaleDlg::Init()
124 {
125   myEditCurrentArgument = GroupPoints->LineEdit1;
126   GroupPoints->LineEdit2->clear();
127   
128   myPoint = GEOM::GEOM_Object::_nil();
129   
130   initName( tr( "GEOM_SCALE" ) );
131 }
132
133
134 //=================================================================================
135 // function : ClickOnOk()
136 // purpose  :
137 //=================================================================================
138 void TransformationGUI_ScaleDlg::ClickOnOk()
139 {
140   if ( ClickOnApply() )
141     ClickOnCancel();
142 }
143
144
145 //=================================================================================
146 // function : ClickOnApply()
147 // purpose  :
148 //=================================================================================
149 bool TransformationGUI_ScaleDlg::ClickOnApply()
150 {
151   if ( !onAccept(GroupPoints->CheckButton1->isChecked()) )
152     return false;
153
154   Init();
155   return true;
156 }
157
158
159 //=======================================================================
160 // function : ClickOnCancel()
161 // purpose  :
162 //=======================================================================
163 void TransformationGUI_ScaleDlg::ClickOnCancel()
164 {
165   GEOMBase_Skeleton::ClickOnCancel();
166 }
167
168
169 //=================================================================================
170 // function : SelectionIntoArgument()
171 // purpose  : Called when selection as changed or other case
172 //=================================================================================
173 void TransformationGUI_ScaleDlg::SelectionIntoArgument()
174 {
175   myEditCurrentArgument->setText("");
176   QString aName;
177
178   if(myEditCurrentArgument == GroupPoints->LineEdit1)
179     {
180       int aNbSel = GEOMBase::GetNameOfSelectedIObjects(mySelection, aName);
181       if(aNbSel < 1)
182         {
183           myObjects.length(0);
184           return;
185         }
186       GEOMBase::ConvertListOfIOInListOfGO(mySelection->StoredIObjects(), myObjects);
187       if (!myObjects.length())
188         return;
189       if(aNbSel != 1)
190         aName = tr("%1_objects").arg(aNbSel);
191     }
192   else if(myEditCurrentArgument == GroupPoints->LineEdit2)
193     {
194       if(mySelection->IObjectCount() != 1)
195         {
196           myPoint = GEOM::GEOM_Object::_nil();
197           return;
198         }
199       Standard_Boolean testResult = Standard_False;
200       myPoint = GEOMBase::ConvertIOinGEOMObject(mySelection->firstIObject(), testResult );
201       if(!testResult || CORBA::is_nil( myPoint ))
202         return;
203       aName = GEOMBase::GetName( myPoint );
204     }
205   myEditCurrentArgument->setText( aName );
206   
207   displayPreview();
208 }
209
210
211 //=================================================================================
212 // function : LineEditReturnPressed()
213 // purpose  :
214 //=================================================================================
215 void TransformationGUI_ScaleDlg::LineEditReturnPressed()
216 {
217   QLineEdit* send = (QLineEdit*)sender();
218   if(send == GroupPoints->LineEdit1 ||
219      send == GroupPoints->LineEdit2)
220     {
221       myEditCurrentArgument = send;
222       GEOMBase_Skeleton::LineEditReturnPressed();
223     }
224 }
225
226
227 //=================================================================================
228 // function : SetEditCurrentArgument()
229 // purpose  :
230 //=================================================================================
231 void TransformationGUI_ScaleDlg::SetEditCurrentArgument()
232 {
233   QPushButton* send = (QPushButton*)sender();
234   
235   if(send == GroupPoints->PushButton1) {
236     myEditCurrentArgument = GroupPoints->LineEdit1;
237     globalSelection();
238   }
239   else if(send == GroupPoints->PushButton2) {
240     myEditCurrentArgument = GroupPoints->LineEdit2;
241     globalSelection( GEOM_POINT );
242   }
243   
244   myEditCurrentArgument->setFocus();
245   SelectionIntoArgument();
246 }
247
248
249 //=================================================================================
250 // function : ActivateThisDialog()
251 // purpose  :
252 //=================================================================================
253 void TransformationGUI_ScaleDlg::ActivateThisDialog()
254 {
255   GEOMBase_Skeleton::ActivateThisDialog();
256   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
257   globalSelection();
258   GroupPoints->LineEdit1->setFocus();
259   myEditCurrentArgument = GroupPoints->LineEdit1;
260 }
261
262
263 //=================================================================================
264 // function : DeactivateActiveDialog()
265 // purpose  : public slot to deactivate if active
266 //=================================================================================
267 void TransformationGUI_ScaleDlg::DeactivateActiveDialog()
268 {
269   GEOMBase_Skeleton::DeactivateActiveDialog();
270 }
271
272
273 //=================================================================================
274 // function : enterEvent()
275 // purpose  :
276 //=================================================================================
277 void TransformationGUI_ScaleDlg::enterEvent(QEvent* e)
278 {
279   if( !GroupConstructors->isEnabled() )
280     ActivateThisDialog();
281 }
282
283
284 //=================================================================================
285 // function : ValueChangedInSpinBox()
286 // purpose  :
287 //=================================================================================
288 void TransformationGUI_ScaleDlg::ValueChangedInSpinBox()
289 {
290   displayPreview();
291 }
292
293
294 //=================================================================================
295 // function : createOperation
296 // purpose  :
297 //=================================================================================
298 GEOM::GEOM_IOperations_ptr  TransformationGUI_ScaleDlg::createOperation()
299 {
300   return getGeomEngine()->GetITransformOperations( getStudyId() );
301 }
302
303 //=================================================================================
304 // function : isValid
305 // purpose  :
306 //=================================================================================
307 bool TransformationGUI_ScaleDlg::isValid( QString& msg )
308 {
309   return !(myObjects.length() == 0 || myPoint->_is_nil() || fabs(GetFactor()) <= 0.00001);
310 }
311
312
313 //=================================================================================
314 // function : execute
315 // purpose  :
316 //=================================================================================
317 bool TransformationGUI_ScaleDlg::execute( ObjectList& objects )
318 {
319   bool res = false;
320   
321   GEOM::GEOM_Object_var anObj;
322
323   if (GroupPoints->CheckButton1->isChecked() || IsPreview())
324     for (int i = 0; i < myObjects.length(); i++)
325       {
326         anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->ScaleShapeCopy( myObjects[i], myPoint, GetFactor() );
327         if ( !anObj->_is_nil() )
328           objects.push_back( anObj._retn() );
329       }
330   else
331     for (int i = 0; i < myObjects.length(); i++)
332       {
333         anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->ScaleShape( myObjects[i], myPoint, GetFactor() );
334         if ( !anObj->_is_nil() )
335           objects.push_back( anObj._retn() );
336       }
337   res = true;
338   
339   return res;
340 }
341
342
343 //=================================================================================
344 // function : closeEvent
345 // purpose  :
346 //=================================================================================
347 void  TransformationGUI_ScaleDlg::closeEvent( QCloseEvent* e )
348 {
349   GEOMBase_Skeleton::closeEvent( e );
350 }
351
352
353 //=================================================================================
354 // function : GetFactor()
355 // purpose  :
356 //=================================================================================
357 double TransformationGUI_ScaleDlg::GetFactor() const
358 {
359   return GroupPoints->SpinBox_DX->GetValue();
360 }
361
362
363 //=================================================================================
364 // function :  CreateCopyModeChanged()
365 // purpose  :
366 //=================================================================================
367 void TransformationGUI_ScaleDlg::CreateCopyModeChanged(bool isCreateCopy)
368 {
369   this->GroupBoxName->setEnabled(isCreateCopy);
370 }