Salome HOME
Update copyright information
[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 <TopoDS_Shape.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS.hxx>
39 #include <TopExp.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42
43 #include <qcheckbox.h>
44 #include <qlabel.h>
45
46 #include "GEOMImpl_Types.hxx"
47
48 #include "utilities.h"
49
50 using namespace std;
51
52 //=================================================================================
53 // class    : TransformationGUI_RotationDlg()
54 // purpose  : Constructs a TransformationGUI_RotationDlg which is a child of 'parent', with the
55 //            name 'name' and widget flags set to 'f'.
56 //            The dialog will by default be modeless, unless you set 'modal' to
57 //            TRUE to construct a modal dialog.
58 //=================================================================================
59 TransformationGUI_RotationDlg::TransformationGUI_RotationDlg
60   (GeometryGUI* theGeometryGUI, QWidget* parent,  const char* name, bool modal, WFlags fl)
61   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
62                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
63 {
64   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
65   QPixmap image0 (aResMgr->loadPixmap("GEOM",tr("ICON_DLG_ROTATION")));
66   QPixmap image1 (aResMgr->loadPixmap("GEOM",tr("ICON_SELECT")));
67   QPixmap image2 (aResMgr->loadPixmap("GEOM",tr("ICON_DLG_ROTATION_THREE_POINTS")));
68
69   setCaption(tr("GEOM_ROTATION_TITLE"));
70
71   /***************************************************************/
72   GroupConstructors->setTitle(tr("GEOM_ROTATION"));
73   RadioButton1->setPixmap(image0);
74   RadioButton2->setPixmap(image2);
75   RadioButton3->close(TRUE);
76
77   GroupPoints = new DlgRef_4Sel1Spin2Check(this, "GroupPoints");
78   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
79   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
80   GroupPoints->TextLabel2->setText(tr("GEOM_AXIS"));
81   GroupPoints->TextLabel3->setText(tr("GEOM_ANGLE"));
82   GroupPoints->TextLabel4->setText(tr("GEOM_POINT_I").arg("1"));
83   GroupPoints->TextLabel5->setText(tr("GEOM_POINT_I").arg("2"));
84
85   GroupPoints->LineEdit1->setReadOnly(true);
86   GroupPoints->LineEdit2->setReadOnly(true);
87   GroupPoints->LineEdit4->setReadOnly(true);
88   GroupPoints->LineEdit5->setReadOnly(true);
89   GroupPoints->PushButton1->setPixmap(image1);
90   GroupPoints->PushButton2->setPixmap(image1);
91   GroupPoints->PushButton4->setPixmap(image1);
92   GroupPoints->PushButton5->setPixmap(image1);
93   GroupPoints->CheckButton1->setText(tr("GEOM_CREATE_COPY"));
94   GroupPoints->CheckButton2->setText(tr("GEOM_REVERSE"));
95
96   Layout1->addWidget(GroupPoints, 2, 0);
97   /***************************************************************/
98
99   double anAngle = 0;
100   double SpecificStep = 5;
101   /* min, max, step and decimals for spin boxes & initial values */
102   GroupPoints->SpinBox_DX->RangeStepAndValidator(COORD_MIN, COORD_MAX, SpecificStep, DBL_DIGITS_DISPLAY);
103   GroupPoints->SpinBox_DX->SetValue(anAngle);
104
105   // Activate Create a Copy mode
106   GroupPoints->CheckButton1->setChecked(true);
107   CreateCopyModeChanged(true);
108
109   GroupBoxPublish->show();
110
111   /* signals and slots connections */
112   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
113   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
114   connect(GroupConstructors, SIGNAL(clicked(int)), SLOT(ConstructorsClicked(int)));
115
116   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
117   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
118   connect(GroupPoints->PushButton4, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
119   connect(GroupPoints->PushButton5, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
120
121   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
122   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
123
124   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
125   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
126   connect(GroupPoints->CheckButton2, SIGNAL(toggled(bool)), this, SLOT(onReverse()));
127
128   connect(myGeomGUI->getApp()->selectionMgr(),
129           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
130
131   setHelpFileName("rotation_operation_page.html");
132
133   Init();
134 }
135
136
137 //=================================================================================
138 // function : ~TransformationGUI_RotationDlg()
139 // purpose  : Destroys the object and frees any allocated resources
140 //=================================================================================
141 TransformationGUI_RotationDlg::~TransformationGUI_RotationDlg()
142 {
143   // no need to delete child widgets, Qt does it all for us
144 }
145
146
147 //=================================================================================
148 // function : Init()
149 // purpose  :
150 //=================================================================================
151 void TransformationGUI_RotationDlg::Init()
152 {
153   /* init variables */
154   myEditCurrentArgument = GroupPoints->LineEdit1;
155   GroupPoints->LineEdit2->clear();
156
157   myAxis = myCentPoint = myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
158
159   initName( tr( "GEOM_ROTATION" ) );
160   ConstructorsClicked( 0 );
161 }
162
163 //=================================================================================
164 // function : ConstructorsClicked()
165 // purpose  : Radio button management
166 //=================================================================================
167 void TransformationGUI_RotationDlg::ConstructorsClicked(int constructorId)
168 {
169   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
170
171   myEditCurrentArgument = GroupPoints->LineEdit1;
172   globalSelection();
173
174   switch (constructorId)
175     {
176     case 0: /* rotation an object angle and axis */
177       {
178         GroupPoints->ShowRows(2,3,false);
179         resize(0,0);
180         GroupPoints->TextLabel2->setText(tr("GEOM_AXIS"));
181         GroupPoints->LineEdit2->clear();
182         GroupPoints->ShowRows(4,4,true);
183         myAxis = GEOM::GEOM_Object::_nil();
184         break;
185       }
186     case 1: /* rotation an object by 3 points */
187       {
188         GroupPoints->ShowRows(4,4,false);
189         resize(0,0);
190         GroupPoints->ShowRows(2,3,true);
191         GroupPoints->TextLabel2->setText(tr("GEOM_CENTRAL_POINT"));
192         GroupPoints->TextLabel4->setText(tr("GEOM_POINT_I").arg("1"));
193         GroupPoints->TextLabel5->setText(tr("GEOM_POINT_I").arg("2"));
194         GroupPoints->LineEdit2->clear();
195         GroupPoints->LineEdit4->clear();
196         GroupPoints->LineEdit5->clear();
197         myCentPoint = myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
198         break;
199       }
200     }
201
202   myEditCurrentArgument->setFocus();
203   connect(myGeomGUI->getApp()->selectionMgr(),
204           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
205 }
206
207 //=================================================================================
208 // function : ClickOnOk()
209 // purpose  :
210 //=================================================================================
211 void TransformationGUI_RotationDlg::ClickOnOk()
212 {
213   if ( ClickOnApply() )
214     ClickOnCancel();
215 }
216
217
218 //=================================================================================
219 // function : ClickOnApply()
220 // purpose  :
221 //=================================================================================
222 bool TransformationGUI_RotationDlg::ClickOnApply()
223 {
224   if ( !onAccept( GroupPoints->CheckButton1->isChecked()) )
225     return false;
226
227   initName();
228   ConstructorsClicked( getConstructorId() );
229   return true;
230 }
231
232
233 //=================================================================================
234 // function : SelectionIntoArgument()
235 // purpose  : Called when selection as changed or other case
236 //=================================================================================
237 void TransformationGUI_RotationDlg::SelectionIntoArgument()
238 {
239   myEditCurrentArgument->setText("");
240   QString aName;
241
242   if(myEditCurrentArgument == GroupPoints->LineEdit1)
243     {
244       int aNbSel = GEOMBase::GetNameOfSelectedIObjects(selectedIO(), aName);
245       if(aNbSel < 1)
246         {
247           myObjects.length(0);
248           return;
249         }
250       GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), myObjects);
251       if (!myObjects.length())
252         return;
253     }
254   else
255     {
256       if(IObjectCount() != 1)
257         {
258           if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 0)
259             myAxis = GEOM::GEOM_Object::_nil();
260           else if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 1)
261             myCentPoint  = GEOM::GEOM_Object::_nil();
262           else if(myEditCurrentArgument == GroupPoints->LineEdit4)
263             myPoint1 = GEOM::GEOM_Object::_nil();
264           else if(myEditCurrentArgument == GroupPoints->LineEdit5)
265             myPoint2 = GEOM::GEOM_Object::_nil();
266           return;
267         }
268
269       Standard_Boolean testResult = Standard_False;
270       GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
271       if(!testResult || CORBA::is_nil( aSelectedObject ))
272         return;
273
274       aName = GEOMBase::GetName( aSelectedObject );
275       TopoDS_Shape aShape;
276       if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
277         {
278           TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
279           if (myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 0)
280             aNeedType = TopAbs_EDGE;
281
282           LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
283           TColStd_IndexedMapOfInteger aMap;
284           aSelMgr->GetIndexes( firstIObject(), aMap );
285           if ( aMap.Extent() == 1 )
286             {
287               int anIndex = aMap( 1 );
288               if (aNeedType == TopAbs_EDGE)
289                 aName += QString(":edge_%1").arg(anIndex);
290               else
291                 aName += QString(":vertex_%1").arg(anIndex);
292
293               //Find SubShape Object in Father
294               GEOM::GEOM_Object_var aFindedObject = findObjectInFather(aSelectedObject, aName);
295
296               if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
297                 GEOM::GEOM_IShapesOperations_var aShapesOp =
298                   getGeomEngine()->GetIShapesOperations( getStudyId() );
299                 aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
300               }
301               else
302                 aSelectedObject = aFindedObject; // get Object from study
303             }
304           else {
305             if (aShape.ShapeType() != aNeedType) {
306               aSelectedObject = GEOM::GEOM_Object::_nil();
307               aName = "";
308             }
309           }
310         }
311
312       if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 0)
313         myAxis = aSelectedObject;
314       else if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 1)
315         myCentPoint = aSelectedObject;
316       else if(myEditCurrentArgument == GroupPoints->LineEdit4)
317         myPoint1 = aSelectedObject;
318       else if(myEditCurrentArgument == GroupPoints->LineEdit5)
319         myPoint2 = aSelectedObject;
320
321     }
322   myEditCurrentArgument->setText( aName );
323
324   displayPreview();
325 }
326
327
328 //=================================================================================
329 // function : SetEditCurrentArgument()
330 // purpose  :
331 //=================================================================================
332 void TransformationGUI_RotationDlg::SetEditCurrentArgument()
333 {
334   QPushButton* send = (QPushButton*)sender();
335   globalSelection();
336
337   if(send == GroupPoints->PushButton1) {
338     myEditCurrentArgument = GroupPoints->LineEdit1;
339   }
340   else if(send == GroupPoints->PushButton2) {
341     myEditCurrentArgument = GroupPoints->LineEdit2;
342     if (getConstructorId() == 0)
343       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
344     else
345       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
346   }
347   else if (send == GroupPoints->PushButton4)
348     {
349       myEditCurrentArgument = GroupPoints->LineEdit4;
350       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
351     }
352   else if (send == GroupPoints->PushButton5)
353     {
354       myEditCurrentArgument = GroupPoints->LineEdit5;
355       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
356     }
357
358   myEditCurrentArgument->setFocus();
359   SelectionIntoArgument();
360 }
361
362
363 //=================================================================================
364 // function : LineEditReturnPressed()
365 // purpose  :
366 //=================================================================================
367 void TransformationGUI_RotationDlg::LineEditReturnPressed()
368 {
369   QLineEdit* send = (QLineEdit*)sender();
370   if(send == GroupPoints->LineEdit1 ||
371      send == GroupPoints->LineEdit2)
372     {
373       myEditCurrentArgument = send;
374       GEOMBase_Skeleton::LineEditReturnPressed();
375     }
376 }
377
378
379 //=================================================================================
380 // function : ActivateThisDialog()
381 // purpose  :
382 //=================================================================================
383 void TransformationGUI_RotationDlg::ActivateThisDialog()
384 {
385   GEOMBase_Skeleton::ActivateThisDialog();
386   connect(myGeomGUI->getApp()->selectionMgr(),
387           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
388
389   ConstructorsClicked( getConstructorId() );
390 }
391
392
393 //=================================================================================
394 // function : enterEvent()
395 // purpose  :
396 //=================================================================================
397 void TransformationGUI_RotationDlg::enterEvent(QEvent* e)
398 {
399   if (!GroupConstructors->isEnabled())
400     ActivateThisDialog();
401 }
402
403
404 //=================================================================================
405 // function : ValueChangedInSpinBox()
406 // purpose  :
407 //=================================================================================
408 void TransformationGUI_RotationDlg::ValueChangedInSpinBox()
409 {
410   displayPreview();
411 }
412
413
414 //=================================================================================
415 // function : createOperation
416 // purpose  :
417 //=================================================================================
418 GEOM::GEOM_IOperations_ptr TransformationGUI_RotationDlg::createOperation()
419 {
420   return getGeomEngine()->GetITransformOperations( getStudyId() );
421 }
422
423
424 //=================================================================================
425 // function : isValid
426 // purpose  :
427 //=================================================================================
428 bool TransformationGUI_RotationDlg::isValid( QString& msg )
429 {
430   switch (getConstructorId())
431     {
432     case 0:
433       {
434         return !(myObjects.length() == 0 || myAxis->_is_nil());
435         break;
436       }
437     case 1:
438       {
439         return !(myObjects.length() == 0 || myCentPoint->_is_nil() || myPoint1->_is_nil() || myPoint2->_is_nil() );
440         break;
441       }
442     default: return false;
443     }
444 }
445
446
447 //=================================================================================
448 // function : execute
449 // purpose  :
450 //=================================================================================
451 bool TransformationGUI_RotationDlg::execute( ObjectList& objects )
452 {
453   bool res = false;
454   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
455
456   GEOM::GEOM_Object_var anObj;
457
458   switch ( getConstructorId() )
459     {
460     case 0 :
461       {
462         if (toCreateCopy)
463           for (int i = 0; i < myObjects.length(); i++)
464             {
465               myCurrObject = myObjects[i];
466               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
467                 RotateCopy( myObjects[i], myAxis, GetAngle() * PI180 );
468               if ( !anObj->_is_nil() )
469                 objects.push_back( anObj._retn() );
470             }
471         else
472           for (int i = 0; i < myObjects.length(); i++)
473             {
474               myCurrObject = myObjects[i];
475               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
476                 Rotate( myObjects[i], myAxis, GetAngle() * PI180 );
477               if ( !anObj->_is_nil() )
478                 objects.push_back( anObj._retn() );
479             }
480         res = true;
481         break;
482       }
483     case 1 :
484       {
485         if (toCreateCopy)
486           for (int i = 0; i < myObjects.length(); i++)
487             {
488               myCurrObject = myObjects[i];
489               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
490                 RotateThreePointsCopy( myObjects[i], myCentPoint, myPoint1, myPoint2 );
491               if ( !anObj->_is_nil() )
492                 objects.push_back( anObj._retn() );
493             }
494         else
495           for (int i = 0; i < myObjects.length(); i++)
496             {
497               myCurrObject = myObjects[i];
498               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
499                 RotateThreePoints( myObjects[i], myCentPoint, myPoint1, myPoint2 );     
500               if ( !anObj->_is_nil() )
501                 objects.push_back( anObj._retn() );
502             }
503         res = true;
504         break;
505       }
506     }
507
508   return res;
509 }
510
511 //=================================================================================
512 // function : restoreSubShapes
513 // purpose  :
514 //=================================================================================
515 void TransformationGUI_RotationDlg::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
516                                                       SALOMEDS::SObject_ptr theSObject)
517 {
518   if (CheckBoxRestoreSS->isChecked()) {
519     // we pass here the first operation argument (object) through the list of arguments
520     // because the rotation operation place its arguments in the data structure in another order,
521     // and we need to point the first argument directly
522     GEOM::ListOfGO_var anArgs = new GEOM::ListOfGO;
523     anArgs->length(1);
524     anArgs[0] = myCurrObject;
525     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, anArgs,
526                                         /*theFindMethod=*/GEOM::FSM_Transformed,
527                                         /*theInheritFirstArg=*/true);
528   }
529 }
530
531 //=================================================================================
532 // function : GetAngle()
533 // purpose  :
534 //=================================================================================
535 double TransformationGUI_RotationDlg::GetAngle() const
536 {
537   return GroupPoints->SpinBox_DX->GetValue();
538 }
539
540
541 //=================================================================================
542 // function :  CreateCopyModeChanged()
543 // purpose  :
544 //=================================================================================
545 void TransformationGUI_RotationDlg::CreateCopyModeChanged(bool isCreateCopy)
546 {
547   this->GroupBoxName->setEnabled(isCreateCopy);
548 }
549
550
551 //=================================================================================
552 // function :  onReverse()
553 // purpose  :
554 //=================================================================================
555 void TransformationGUI_RotationDlg::onReverse()
556 {
557   double anOldValue = GroupPoints->SpinBox_DX->GetValue();
558   GroupPoints->SpinBox_DX->SetValue( -anOldValue );
559 }
560
561 //=================================================================================
562 // function : addSubshapeToStudy
563 // purpose  : virtual method to add new SubObjects if local selection
564 //=================================================================================
565 void TransformationGUI_RotationDlg::addSubshapesToStudy()
566 {
567   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
568   if (toCreateCopy) {
569   QMap<QString, GEOM::GEOM_Object_var> objMap;
570   switch (getConstructorId())
571     {
572     case 0:
573       objMap[GroupPoints->LineEdit2->text()] = myAxis;
574       break;
575     case 1:
576       objMap[GroupPoints->LineEdit2->text()] = myCentPoint;
577       objMap[GroupPoints->LineEdit4->text()] = myPoint1;
578       objMap[GroupPoints->LineEdit5->text()] = myPoint2;
579       break;
580     }
581   addSubshapesToFather( objMap );
582   }
583 }