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