Salome HOME
PAL 11107 - Make a rotation given an object and 3 points
[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
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "LightApp_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
53   (GeometryGUI* theGeometryGUI, QWidget* parent,  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   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
58   QPixmap image0 (aResMgr->loadPixmap("GEOM",tr("ICON_DLG_ROTATION")));
59   QPixmap image1 (aResMgr->loadPixmap("GEOM",tr("ICON_SELECT")));
60   QPixmap image2 (aResMgr->loadPixmap("GEOM",tr("ICON_DLG_ROTATION_THREE_POINTS")));
61
62   setCaption(tr("GEOM_ROTATION_TITLE"));
63
64   /***************************************************************/
65   GroupConstructors->setTitle(tr("GEOM_ROTATION"));
66   RadioButton1->setPixmap(image0);
67   RadioButton2->setPixmap(image2);
68   RadioButton3->close(TRUE);
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->setPixmap(image1);
83   GroupPoints->PushButton2->setPixmap(image1);
84   GroupPoints->PushButton4->setPixmap(image1);
85   GroupPoints->PushButton5->setPixmap(image1);
86   GroupPoints->CheckButton1->setText(tr("GEOM_CREATE_COPY"));
87   GroupPoints->CheckButton2->setText(tr("GEOM_REVERSE"));
88
89   Layout1->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" ) );
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         myCentPoint = myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
186         break;
187       } 
188     }
189   
190   myEditCurrentArgument->setFocus();
191   connect(myGeomGUI->getApp()->selectionMgr(), 
192           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
193 }
194
195 //=================================================================================
196 // function : ClickOnOk()
197 // purpose  :
198 //=================================================================================
199 void TransformationGUI_RotationDlg::ClickOnOk()
200 {
201   if ( ClickOnApply() )
202     ClickOnCancel();
203 }
204
205
206 //=================================================================================
207 // function : ClickOnApply()
208 // purpose  :
209 //=================================================================================
210 bool TransformationGUI_RotationDlg::ClickOnApply()
211 {
212   if ( !onAccept( GroupPoints->CheckButton1->isChecked()) )
213     return false;
214   
215   Init();
216   return true;
217 }
218
219
220 //=================================================================================
221 // function : SelectionIntoArgument()
222 // purpose  : Called when selection as changed or other case
223 //=================================================================================
224 void TransformationGUI_RotationDlg::SelectionIntoArgument()
225 {
226   myEditCurrentArgument->setText("");
227   QString aName;
228   
229   if(myEditCurrentArgument == GroupPoints->LineEdit1)
230     {
231       int aNbSel = GEOMBase::GetNameOfSelectedIObjects(selectedIO(), aName);
232       if(aNbSel < 1)
233         {
234           myObjects.length(0);
235           return;
236         }
237       GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), myObjects);
238       if (!myObjects.length())
239         return;
240     }
241   else 
242     {
243       if(IObjectCount() != 1)
244         {
245           if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 0)
246             myAxis = GEOM::GEOM_Object::_nil();
247           else if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 1)
248             myCentPoint  = GEOM::GEOM_Object::_nil();
249           else if(myEditCurrentArgument == GroupPoints->LineEdit4)
250             myPoint1 = GEOM::GEOM_Object::_nil();
251           else if(myEditCurrentArgument == GroupPoints->LineEdit5)
252             myPoint2 = GEOM::GEOM_Object::_nil();
253           return;
254         }
255
256       Standard_Boolean testResult = Standard_False;
257       GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
258       if(!testResult || CORBA::is_nil( aSelectedObject ))
259         return;
260
261       if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 0)
262         myAxis = aSelectedObject;
263       else if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 1)
264         myCentPoint = aSelectedObject;
265       else if(myEditCurrentArgument == GroupPoints->LineEdit4)
266         myPoint1 = aSelectedObject;
267       else if(myEditCurrentArgument == GroupPoints->LineEdit5)
268         myPoint2 = aSelectedObject;
269
270       aName = GEOMBase::GetName( aSelectedObject );
271     }
272   myEditCurrentArgument->setText( aName );
273   
274   displayPreview();
275 }
276
277
278 //=================================================================================
279 // function : SetEditCurrentArgument()
280 // purpose  :
281 //=================================================================================
282 void TransformationGUI_RotationDlg::SetEditCurrentArgument()
283 {
284   QPushButton* send = (QPushButton*)sender();
285   
286   if(send == GroupPoints->PushButton1) {
287     myEditCurrentArgument = GroupPoints->LineEdit1;
288     globalSelection();
289   }
290   else if(send == GroupPoints->PushButton2) {
291     myEditCurrentArgument = GroupPoints->LineEdit2;
292     getConstructorId() == 0 ? globalSelection( GEOM_LINE ) :
293                               globalSelection( GEOM_POINT  );
294   }
295   else if (send == GroupPoints->PushButton4)
296     {
297       myEditCurrentArgument = GroupPoints->LineEdit4;
298       globalSelection( GEOM_POINT );
299     }
300   else if (send == GroupPoints->PushButton5)
301     {
302       myEditCurrentArgument = GroupPoints->LineEdit5;
303       globalSelection( GEOM_POINT );
304     }
305
306   myEditCurrentArgument->setFocus();
307   SelectionIntoArgument();
308 }
309
310
311 //=================================================================================
312 // function : LineEditReturnPressed()
313 // purpose  :
314 //=================================================================================
315 void TransformationGUI_RotationDlg::LineEditReturnPressed()
316 {  
317   QLineEdit* send = (QLineEdit*)sender();
318   if(send == GroupPoints->LineEdit1 ||
319      send == GroupPoints->LineEdit2)
320     {
321       myEditCurrentArgument = send;
322       GEOMBase_Skeleton::LineEditReturnPressed();
323     }
324 }
325
326
327 //=================================================================================
328 // function : ActivateThisDialog()
329 // purpose  :
330 //=================================================================================
331 void TransformationGUI_RotationDlg::ActivateThisDialog()
332 {
333   GEOMBase_Skeleton::ActivateThisDialog();
334   connect(myGeomGUI->getApp()->selectionMgr(), 
335           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
336
337   ConstructorsClicked( getConstructorId() );
338 }
339
340
341 //=================================================================================
342 // function : enterEvent()
343 // purpose  :
344 //=================================================================================
345 void TransformationGUI_RotationDlg::enterEvent(QEvent* e)
346 {
347   if (!GroupConstructors->isEnabled())
348     ActivateThisDialog();
349 }
350
351
352 //=================================================================================
353 // function : ValueChangedInSpinBox()
354 // purpose  :
355 //=================================================================================
356 void TransformationGUI_RotationDlg::ValueChangedInSpinBox()
357 {
358   displayPreview();
359 }
360
361
362 //=================================================================================
363 // function : createOperation
364 // purpose  :
365 //=================================================================================
366 GEOM::GEOM_IOperations_ptr TransformationGUI_RotationDlg::createOperation()
367 {
368   return getGeomEngine()->GetITransformOperations( getStudyId() );
369 }
370
371
372 //=================================================================================
373 // function : isValid
374 // purpose  :
375 //=================================================================================
376 bool TransformationGUI_RotationDlg::isValid( QString& msg )
377 {
378   switch (getConstructorId())
379     {
380     case 0: 
381       {
382         return !(myObjects.length() == 0 || myAxis->_is_nil());
383         break;
384       }
385     case 1: 
386       {
387         return !(myObjects.length() == 0 || myCentPoint->_is_nil() || myPoint1->_is_nil() || myPoint2->_is_nil() );
388         break;
389       }
390     default: return false;
391     }
392 }
393
394
395 //=================================================================================
396 // function : execute
397 // purpose  :
398 //=================================================================================
399 bool TransformationGUI_RotationDlg::execute( ObjectList& objects )
400 {
401   bool res = false;
402   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
403
404   GEOM::GEOM_Object_var anObj;
405   
406   switch ( getConstructorId() ) 
407     {
408     case 0 :
409       {
410         if (toCreateCopy)
411           for (int i = 0; i < myObjects.length(); i++)
412             {
413               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->RotateCopy( myObjects[i], myAxis, GetAngle() * PI180 );
414               if ( !anObj->_is_nil() )
415                 objects.push_back( anObj._retn() );
416             }
417         else
418           for (int i = 0; i < myObjects.length(); i++)
419             {
420               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->Rotate( myObjects[i], myAxis, GetAngle() * PI180 );
421               if ( !anObj->_is_nil() )
422                 objects.push_back( anObj._retn() );
423             }
424         res = true;
425         break;
426       }
427     case 1 :
428       {
429         if (toCreateCopy)
430           for (int i = 0; i < myObjects.length(); i++)
431             {
432               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->RotateThreePointsCopy( myObjects[i], myCentPoint, myPoint1, myPoint2 );
433               if ( !anObj->_is_nil() )
434                 objects.push_back( anObj._retn() );
435             }
436         else
437           for (int i = 0; i < myObjects.length(); i++)
438             {
439               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->RotateThreePoints( myObjects[i], myCentPoint, myPoint1, myPoint2 );   
440               if ( !anObj->_is_nil() )
441                 objects.push_back( anObj._retn() );
442             }
443         res = true;
444         break;
445       }
446     }
447   
448   return res;
449 }
450
451
452 //=================================================================================
453 // function : closeEvent
454 // purpose  :
455 //=================================================================================
456 void TransformationGUI_RotationDlg::closeEvent( QCloseEvent* e )
457 {
458   GEOMBase_Skeleton::closeEvent( e );
459 }
460
461
462 //=================================================================================
463 // function : GetAngle()
464 // purpose  :
465 //=================================================================================
466 double TransformationGUI_RotationDlg::GetAngle() const
467 {
468   return GroupPoints->SpinBox_DX->GetValue();
469 }
470
471
472 //=================================================================================
473 // function :  CreateCopyModeChanged()
474 // purpose  :
475 //=================================================================================
476 void TransformationGUI_RotationDlg::CreateCopyModeChanged(bool isCreateCopy)
477 {
478   this->GroupBoxName->setEnabled(isCreateCopy);
479 }
480
481
482 //=================================================================================
483 // function :  onReverse()
484 // purpose  :
485 //=================================================================================
486 void TransformationGUI_RotationDlg::onReverse()
487 {
488   double anOldValue = GroupPoints->SpinBox_DX->GetValue();
489   GroupPoints->SpinBox_DX->SetValue( -anOldValue );
490 }