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