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