Salome HOME
Porting to Qt4.
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 "DlgRef_4Sel1Spin2Check.h"
31 #include "DlgRef_SpinBox.h"
32
33 #include "GeometryGUI.h"
34 #include "GEOMBase.h"
35
36 #include "SUIT_ResourceMgr.h"
37 #include "SUIT_Session.h"
38 #include "SalomeApp_Application.h"
39 #include "LightApp_SelectionMgr.h"
40
41 #include "GEOMImpl_Types.hxx"
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
53   (GeometryGUI* theGeometryGUI, QWidget* parent,  const char* name, bool modal, Qt::WindowFlags fl)
54   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
55 {
56   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
57   QPixmap image0 (aResMgr->loadPixmap("GEOM",tr("ICON_DLG_ROTATION")));
58   QPixmap image1 (aResMgr->loadPixmap("GEOM",tr("ICON_SELECT")));
59   QPixmap image2 (aResMgr->loadPixmap("GEOM",tr("ICON_DLG_ROTATION_THREE_POINTS")));
60
61   setWindowTitle(tr("GEOM_ROTATION_TITLE"));
62
63   /***************************************************************/
64   GroupConstructors->setTitle(tr("GEOM_ROTATION"));
65   RadioButton1->setIcon(image0);
66   RadioButton2->setIcon(image2);
67   RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
68   RadioButton3->close();
69
70   GroupPoints = new DlgRef_4Sel1Spin2Check(this, "GroupPoints");
71   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
72   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
73   GroupPoints->TextLabel2->setText(tr("GEOM_AXIS"));
74   GroupPoints->TextLabel3->setText(tr("GEOM_ANGLE"));
75   GroupPoints->TextLabel4->setText(tr("GEOM_POINT_I").arg("1"));
76   GroupPoints->TextLabel5->setText(tr("GEOM_POINT_I").arg("2"));
77
78   GroupPoints->LineEdit1->setReadOnly(true);
79   GroupPoints->LineEdit2->setReadOnly(true);
80   GroupPoints->LineEdit4->setReadOnly(true);
81   GroupPoints->LineEdit5->setReadOnly(true);
82   GroupPoints->PushButton1->setIcon(image1);
83   GroupPoints->PushButton2->setIcon(image1);
84   GroupPoints->PushButton4->setIcon(image1);
85   GroupPoints->PushButton5->setIcon(image1);
86   GroupPoints->CheckButton1->setText(tr("GEOM_CREATE_COPY"));
87   GroupPoints->CheckButton2->setText(tr("GEOM_REVERSE"));
88
89   gridLayout1->addWidget(GroupPoints, 2, 0);
90   /***************************************************************/
91   double anAngle = 0;
92   double SpecificStep = 5;
93   /* min, max, step and decimals for spin boxes & initial values */
94   GroupPoints->SpinBox_DX->RangeStepAndValidator(COORD_MIN, COORD_MAX, SpecificStep, 3);
95   GroupPoints->SpinBox_DX->SetValue(anAngle);
96
97   // Activate Create a Copy mode
98   GroupPoints->CheckButton1->setChecked(true);
99   CreateCopyModeChanged(true);
100   
101   /* signals and slots connections */
102   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
103   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
104   connect(GroupConstructors, SIGNAL(clicked(int)), SLOT(ConstructorsClicked(int)));
105
106   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
107   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
108   connect(GroupPoints->PushButton4, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
109   connect(GroupPoints->PushButton5, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
110  
111   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
112   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
113
114   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
115   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
116   connect(GroupPoints->CheckButton2, SIGNAL(toggled(bool)), this, SLOT(onReverse()));
117   
118   connect(myGeomGUI->getApp()->selectionMgr(),
119           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
120
121   setHelpFileName("rotation.htm");
122
123   Init();
124 }
125
126
127 //=================================================================================
128 // function : ~TransformationGUI_RotationDlg()
129 // purpose  : Destroys the object and frees any allocated resources
130 //=================================================================================
131 TransformationGUI_RotationDlg::~TransformationGUI_RotationDlg()
132 {
133   // no need to delete child widgets, Qt does it all for us
134 }
135
136
137 //=================================================================================
138 // function : Init()
139 // purpose  :
140 //=================================================================================
141 void TransformationGUI_RotationDlg::Init()
142 {
143   /* init variables */
144   myEditCurrentArgument = GroupPoints->LineEdit1;
145   GroupPoints->LineEdit2->clear();
146   
147   myAxis = myCentPoint = myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
148   
149   initName( tr( "GEOM_ROTATION" ).toLatin1().constData() );
150   ConstructorsClicked( 0 );
151 }
152
153 //=================================================================================
154 // function : ConstructorsClicked()
155 // purpose  : Radio button management
156 //=================================================================================
157 void TransformationGUI_RotationDlg::ConstructorsClicked(int constructorId)
158 {
159   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
160   
161   myEditCurrentArgument = GroupPoints->LineEdit1;
162   globalSelection();
163
164   switch (constructorId)
165     {
166     case 0: /* rotation an object angle and axis */
167       {  
168         GroupPoints->ShowRows(2,3,false);
169         resize(0,0);
170         GroupPoints->TextLabel2->setText(tr("GEOM_AXIS"));
171         GroupPoints->LineEdit2->clear();
172         GroupPoints->ShowRows(4,4,true);
173         myAxis = GEOM::GEOM_Object::_nil();
174         break;
175       }
176     case 1: /* rotation an object by 3 points */
177       {
178         GroupPoints->ShowRows(4,4,false);
179         resize(0,0);
180         GroupPoints->ShowRows(2,3,true);
181         GroupPoints->TextLabel2->setText(tr("GEOM_CENTRAL_POINT"));
182         GroupPoints->TextLabel4->setText(tr("GEOM_POINT_I").arg("1"));
183         GroupPoints->TextLabel5->setText(tr("GEOM_POINT_I").arg("2"));
184         GroupPoints->LineEdit2->clear();
185         GroupPoints->LineEdit4->clear();
186         GroupPoints->LineEdit5->clear();
187         myCentPoint = myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
188         break;
189       } 
190     }
191   
192   myEditCurrentArgument->setFocus();
193   connect(myGeomGUI->getApp()->selectionMgr(), 
194           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
195 }
196
197 //=================================================================================
198 // function : ClickOnOk()
199 // purpose  :
200 //=================================================================================
201 void TransformationGUI_RotationDlg::ClickOnOk()
202 {
203   if ( ClickOnApply() )
204     ClickOnCancel();
205 }
206
207
208 //=================================================================================
209 // function : ClickOnApply()
210 // purpose  :
211 //=================================================================================
212 bool TransformationGUI_RotationDlg::ClickOnApply()
213 {
214   if ( !onAccept( GroupPoints->CheckButton1->isChecked()) )
215     return false;
216   
217   initName();
218   ConstructorsClicked( getConstructorId() );
219   return true;
220 }
221
222
223 //=================================================================================
224 // function : SelectionIntoArgument()
225 // purpose  : Called when selection as changed or other case
226 //=================================================================================
227 void TransformationGUI_RotationDlg::SelectionIntoArgument()
228 {
229   myEditCurrentArgument->setText("");
230   QString aName;
231   
232   if(myEditCurrentArgument == GroupPoints->LineEdit1)
233     {
234       int aNbSel = GEOMBase::GetNameOfSelectedIObjects(selectedIO(), aName);
235       if(aNbSel < 1)
236         {
237           myObjects.length(0);
238           return;
239         }
240       GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), myObjects);
241       if (!myObjects.length())
242         return;
243     }
244   else 
245     {
246       if(IObjectCount() != 1)
247         {
248           if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 0)
249             myAxis = GEOM::GEOM_Object::_nil();
250           else if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 1)
251             myCentPoint  = GEOM::GEOM_Object::_nil();
252           else if(myEditCurrentArgument == GroupPoints->LineEdit4)
253             myPoint1 = GEOM::GEOM_Object::_nil();
254           else if(myEditCurrentArgument == GroupPoints->LineEdit5)
255             myPoint2 = GEOM::GEOM_Object::_nil();
256           return;
257         }
258
259       Standard_Boolean testResult = Standard_False;
260       GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
261       if(!testResult || CORBA::is_nil( aSelectedObject ))
262         return;
263
264       if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 0)
265         myAxis = aSelectedObject;
266       else if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 1)
267         myCentPoint = aSelectedObject;
268       else if(myEditCurrentArgument == GroupPoints->LineEdit4)
269         myPoint1 = aSelectedObject;
270       else if(myEditCurrentArgument == GroupPoints->LineEdit5)
271         myPoint2 = aSelectedObject;
272
273       aName = GEOMBase::GetName( aSelectedObject );
274     }
275   myEditCurrentArgument->setText( aName );
276   
277   displayPreview();
278 }
279
280
281 //=================================================================================
282 // function : SetEditCurrentArgument()
283 // purpose  :
284 //=================================================================================
285 void TransformationGUI_RotationDlg::SetEditCurrentArgument()
286 {
287   QPushButton* send = (QPushButton*)sender();
288   
289   if(send == GroupPoints->PushButton1) {
290     myEditCurrentArgument = GroupPoints->LineEdit1;
291     globalSelection();
292   }
293   else if(send == GroupPoints->PushButton2) {
294     myEditCurrentArgument = GroupPoints->LineEdit2;
295     getConstructorId() == 0 ? globalSelection( GEOM_LINE ) :
296                               globalSelection( GEOM_POINT  );
297   }
298   else if (send == GroupPoints->PushButton4)
299     {
300       myEditCurrentArgument = GroupPoints->LineEdit4;
301       globalSelection( GEOM_POINT );
302     }
303   else if (send == GroupPoints->PushButton5)
304     {
305       myEditCurrentArgument = GroupPoints->LineEdit5;
306       globalSelection( GEOM_POINT );
307     }
308
309   myEditCurrentArgument->setFocus();
310   SelectionIntoArgument();
311 }
312
313
314 //=================================================================================
315 // function : LineEditReturnPressed()
316 // purpose  :
317 //=================================================================================
318 void TransformationGUI_RotationDlg::LineEditReturnPressed()
319 {  
320   QLineEdit* send = (QLineEdit*)sender();
321   if(send == GroupPoints->LineEdit1 ||
322      send == GroupPoints->LineEdit2)
323     {
324       myEditCurrentArgument = send;
325       GEOMBase_Skeleton::LineEditReturnPressed();
326     }
327 }
328
329
330 //=================================================================================
331 // function : ActivateThisDialog()
332 // purpose  :
333 //=================================================================================
334 void TransformationGUI_RotationDlg::ActivateThisDialog()
335 {
336   GEOMBase_Skeleton::ActivateThisDialog();
337   connect(myGeomGUI->getApp()->selectionMgr(), 
338           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
339
340   ConstructorsClicked( getConstructorId() );
341 }
342
343
344 //=================================================================================
345 // function : enterEvent()
346 // purpose  :
347 //=================================================================================
348 void TransformationGUI_RotationDlg::enterEvent(QEvent* e)
349 {
350   if (!GroupConstructors->isEnabled())
351     ActivateThisDialog();
352 }
353
354
355 //=================================================================================
356 // function : ValueChangedInSpinBox()
357 // purpose  :
358 //=================================================================================
359 void TransformationGUI_RotationDlg::ValueChangedInSpinBox()
360 {
361   displayPreview();
362 }
363
364
365 //=================================================================================
366 // function : createOperation
367 // purpose  :
368 //=================================================================================
369 GEOM::GEOM_IOperations_ptr TransformationGUI_RotationDlg::createOperation()
370 {
371   return getGeomEngine()->GetITransformOperations( getStudyId() );
372 }
373
374
375 //=================================================================================
376 // function : isValid
377 // purpose  :
378 //=================================================================================
379 bool TransformationGUI_RotationDlg::isValid( QString& msg )
380 {
381   switch (getConstructorId())
382     {
383     case 0: 
384       {
385         return !(myObjects.length() == 0 || myAxis->_is_nil());
386         break;
387       }
388     case 1: 
389       {
390         return !(myObjects.length() == 0 || myCentPoint->_is_nil() || myPoint1->_is_nil() || myPoint2->_is_nil() );
391         break;
392       }
393     default: return false;
394     }
395 }
396
397
398 //=================================================================================
399 // function : execute
400 // purpose  :
401 //=================================================================================
402 bool TransformationGUI_RotationDlg::execute( ObjectList& objects )
403 {
404   bool res = false;
405   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
406
407   GEOM::GEOM_Object_var anObj;
408   
409   switch ( getConstructorId() ) 
410     {
411     case 0 :
412       {
413         if (toCreateCopy)
414           for (int i = 0; i < myObjects.length(); i++)
415             {
416               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->RotateCopy( myObjects[i], myAxis, GetAngle() * PI180 );
417               if ( !anObj->_is_nil() )
418                 objects.push_back( anObj._retn() );
419             }
420         else
421           for (int i = 0; i < myObjects.length(); i++)
422             {
423               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->Rotate( myObjects[i], myAxis, GetAngle() * PI180 );
424               if ( !anObj->_is_nil() )
425                 objects.push_back( anObj._retn() );
426             }
427         res = true;
428         break;
429       }
430     case 1 :
431       {
432         if (toCreateCopy)
433           for (int i = 0; i < myObjects.length(); i++)
434             {
435               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->RotateThreePointsCopy( myObjects[i], myCentPoint, myPoint1, myPoint2 );
436               if ( !anObj->_is_nil() )
437                 objects.push_back( anObj._retn() );
438             }
439         else
440           for (int i = 0; i < myObjects.length(); i++)
441             {
442               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->RotateThreePoints( myObjects[i], myCentPoint, myPoint1, myPoint2 );   
443               if ( !anObj->_is_nil() )
444                 objects.push_back( anObj._retn() );
445             }
446         res = true;
447         break;
448       }
449     }
450   
451   return res;
452 }
453
454
455 //=================================================================================
456 // function : closeEvent
457 // purpose  :
458 //=================================================================================
459 void TransformationGUI_RotationDlg::closeEvent( QCloseEvent* e )
460 {
461   GEOMBase_Skeleton::closeEvent( e );
462 }
463
464
465 //=================================================================================
466 // function : GetAngle()
467 // purpose  :
468 //=================================================================================
469 double TransformationGUI_RotationDlg::GetAngle() const
470 {
471   return GroupPoints->SpinBox_DX->GetValue();
472 }
473
474
475 //=================================================================================
476 // function :  CreateCopyModeChanged()
477 // purpose  :
478 //=================================================================================
479 void TransformationGUI_RotationDlg::CreateCopyModeChanged(bool isCreateCopy)
480 {
481   this->GroupBoxName->setEnabled(isCreateCopy);
482 }
483
484
485 //=================================================================================
486 // function :  onReverse()
487 // purpose  :
488 //=================================================================================
489 void TransformationGUI_RotationDlg::onReverse()
490 {
491   double anOldValue = GroupPoints->SpinBox_DX->GetValue();
492   GroupPoints->SpinBox_DX->SetValue( -anOldValue );
493 }