Salome HOME
f0cca0130cedf71ef000d6efda4f318b7a295576
[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   /* signals and slots connections */
110   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
111   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
112   connect(GroupConstructors, SIGNAL(clicked(int)), SLOT(ConstructorsClicked(int)));
113
114   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
115   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
116   connect(GroupPoints->PushButton4, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
117   connect(GroupPoints->PushButton5, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
118
119   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
120   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
121
122   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
123   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
124   connect(GroupPoints->CheckButton2, SIGNAL(toggled(bool)), this, SLOT(onReverse()));
125
126   connect(myGeomGUI->getApp()->selectionMgr(),
127           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
128
129   setHelpFileName("rotation_operation_page.html");
130
131   Init();
132 }
133
134
135 //=================================================================================
136 // function : ~TransformationGUI_RotationDlg()
137 // purpose  : Destroys the object and frees any allocated resources
138 //=================================================================================
139 TransformationGUI_RotationDlg::~TransformationGUI_RotationDlg()
140 {
141   // no need to delete child widgets, Qt does it all for us
142 }
143
144
145 //=================================================================================
146 // function : Init()
147 // purpose  :
148 //=================================================================================
149 void TransformationGUI_RotationDlg::Init()
150 {
151   /* init variables */
152   myEditCurrentArgument = GroupPoints->LineEdit1;
153   GroupPoints->LineEdit2->clear();
154
155   myAxis = myCentPoint = myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
156
157   initName( tr( "GEOM_ROTATION" ) );
158   ConstructorsClicked( 0 );
159 }
160
161 //=================================================================================
162 // function : ConstructorsClicked()
163 // purpose  : Radio button management
164 //=================================================================================
165 void TransformationGUI_RotationDlg::ConstructorsClicked(int constructorId)
166 {
167   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
168
169   myEditCurrentArgument = GroupPoints->LineEdit1;
170   globalSelection();
171
172   switch (constructorId)
173     {
174     case 0: /* rotation an object angle and axis */
175       {
176         GroupPoints->ShowRows(2,3,false);
177         resize(0,0);
178         GroupPoints->TextLabel2->setText(tr("GEOM_AXIS"));
179         GroupPoints->LineEdit2->clear();
180         GroupPoints->ShowRows(4,4,true);
181         myAxis = GEOM::GEOM_Object::_nil();
182         break;
183       }
184     case 1: /* rotation an object by 3 points */
185       {
186         GroupPoints->ShowRows(4,4,false);
187         resize(0,0);
188         GroupPoints->ShowRows(2,3,true);
189         GroupPoints->TextLabel2->setText(tr("GEOM_CENTRAL_POINT"));
190         GroupPoints->TextLabel4->setText(tr("GEOM_POINT_I").arg("1"));
191         GroupPoints->TextLabel5->setText(tr("GEOM_POINT_I").arg("2"));
192         GroupPoints->LineEdit2->clear();
193         GroupPoints->LineEdit4->clear();
194         GroupPoints->LineEdit5->clear();
195         myCentPoint = myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
196         break;
197       }
198     }
199
200   myEditCurrentArgument->setFocus();
201   connect(myGeomGUI->getApp()->selectionMgr(),
202           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
203 }
204
205 //=================================================================================
206 // function : ClickOnOk()
207 // purpose  :
208 //=================================================================================
209 void TransformationGUI_RotationDlg::ClickOnOk()
210 {
211   if ( ClickOnApply() )
212     ClickOnCancel();
213 }
214
215
216 //=================================================================================
217 // function : ClickOnApply()
218 // purpose  :
219 //=================================================================================
220 bool TransformationGUI_RotationDlg::ClickOnApply()
221 {
222   if ( !onAccept( GroupPoints->CheckButton1->isChecked()) )
223     return false;
224
225   initName();
226   ConstructorsClicked( getConstructorId() );
227   return true;
228 }
229
230
231 //=================================================================================
232 // function : SelectionIntoArgument()
233 // purpose  : Called when selection as changed or other case
234 //=================================================================================
235 void TransformationGUI_RotationDlg::SelectionIntoArgument()
236 {
237   myEditCurrentArgument->setText("");
238   QString aName;
239
240   if(myEditCurrentArgument == GroupPoints->LineEdit1)
241     {
242       int aNbSel = GEOMBase::GetNameOfSelectedIObjects(selectedIO(), aName);
243       if(aNbSel < 1)
244         {
245           myObjects.length(0);
246           return;
247         }
248       GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), myObjects);
249       if (!myObjects.length())
250         return;
251     }
252   else
253     {
254       if(IObjectCount() != 1)
255         {
256           if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 0)
257             myAxis = GEOM::GEOM_Object::_nil();
258           else if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 1)
259             myCentPoint  = GEOM::GEOM_Object::_nil();
260           else if(myEditCurrentArgument == GroupPoints->LineEdit4)
261             myPoint1 = GEOM::GEOM_Object::_nil();
262           else if(myEditCurrentArgument == GroupPoints->LineEdit5)
263             myPoint2 = GEOM::GEOM_Object::_nil();
264           return;
265         }
266
267       Standard_Boolean testResult = Standard_False;
268       GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
269       if(!testResult || CORBA::is_nil( aSelectedObject ))
270         return;
271
272       aName = GEOMBase::GetName( aSelectedObject );
273       TopoDS_Shape aShape;
274       if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
275         {
276           TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
277           if (myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 0)
278             aNeedType = TopAbs_EDGE;
279
280           LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
281           TColStd_IndexedMapOfInteger aMap;
282           aSelMgr->GetIndexes( firstIObject(), aMap );
283           if ( aMap.Extent() == 1 )
284             {
285               int anIndex = aMap( 1 );
286               if (aNeedType == TopAbs_EDGE)
287                 aName += QString(":edge_%1").arg(anIndex);
288               else
289                 aName += QString(":vertex_%1").arg(anIndex);
290
291               //Find SubShape Object in Father
292               GEOM::GEOM_Object_var aFindedObject = findObjectInFather(aSelectedObject, aName);
293
294               if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
295                 GEOM::GEOM_IShapesOperations_var aShapesOp =
296                   getGeomEngine()->GetIShapesOperations( getStudyId() );
297                 aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
298               }
299               else
300                 aSelectedObject = aFindedObject; // get Object from study
301             }
302           else {
303             if (aShape.ShapeType() != aNeedType) {
304               aSelectedObject = GEOM::GEOM_Object::_nil();
305               aName = "";
306             }
307           }
308         }
309      
310       if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 0)
311         myAxis = aSelectedObject;
312       else if(myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 1)
313         myCentPoint = aSelectedObject;
314       else if(myEditCurrentArgument == GroupPoints->LineEdit4)
315         myPoint1 = aSelectedObject;
316       else if(myEditCurrentArgument == GroupPoints->LineEdit5)
317         myPoint2 = aSelectedObject;
318
319     }
320   myEditCurrentArgument->setText( aName );
321
322   displayPreview();
323 }
324
325
326 //=================================================================================
327 // function : SetEditCurrentArgument()
328 // purpose  :
329 //=================================================================================
330 void TransformationGUI_RotationDlg::SetEditCurrentArgument()
331 {
332   QPushButton* send = (QPushButton*)sender();
333   globalSelection();
334
335   if(send == GroupPoints->PushButton1) {
336     myEditCurrentArgument = GroupPoints->LineEdit1;
337   }
338   else if(send == GroupPoints->PushButton2) {
339     myEditCurrentArgument = GroupPoints->LineEdit2;
340     if (getConstructorId() == 0)
341       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
342     else
343       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
344   }
345   else if (send == GroupPoints->PushButton4)
346     {
347       myEditCurrentArgument = GroupPoints->LineEdit4;
348       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
349     }
350   else if (send == GroupPoints->PushButton5)
351     {
352       myEditCurrentArgument = GroupPoints->LineEdit5;
353       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
354     }
355
356   myEditCurrentArgument->setFocus();
357   SelectionIntoArgument();
358 }
359
360
361 //=================================================================================
362 // function : LineEditReturnPressed()
363 // purpose  :
364 //=================================================================================
365 void TransformationGUI_RotationDlg::LineEditReturnPressed()
366 {
367   QLineEdit* send = (QLineEdit*)sender();
368   if(send == GroupPoints->LineEdit1 ||
369      send == GroupPoints->LineEdit2)
370     {
371       myEditCurrentArgument = send;
372       GEOMBase_Skeleton::LineEditReturnPressed();
373     }
374 }
375
376
377 //=================================================================================
378 // function : ActivateThisDialog()
379 // purpose  :
380 //=================================================================================
381 void TransformationGUI_RotationDlg::ActivateThisDialog()
382 {
383   GEOMBase_Skeleton::ActivateThisDialog();
384   connect(myGeomGUI->getApp()->selectionMgr(),
385           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
386
387   ConstructorsClicked( getConstructorId() );
388 }
389
390
391 //=================================================================================
392 // function : enterEvent()
393 // purpose  :
394 //=================================================================================
395 void TransformationGUI_RotationDlg::enterEvent(QEvent* e)
396 {
397   if (!GroupConstructors->isEnabled())
398     ActivateThisDialog();
399 }
400
401
402 //=================================================================================
403 // function : ValueChangedInSpinBox()
404 // purpose  :
405 //=================================================================================
406 void TransformationGUI_RotationDlg::ValueChangedInSpinBox()
407 {
408   displayPreview();
409 }
410
411
412 //=================================================================================
413 // function : createOperation
414 // purpose  :
415 //=================================================================================
416 GEOM::GEOM_IOperations_ptr TransformationGUI_RotationDlg::createOperation()
417 {
418   return getGeomEngine()->GetITransformOperations( getStudyId() );
419 }
420
421
422 //=================================================================================
423 // function : isValid
424 // purpose  :
425 //=================================================================================
426 bool TransformationGUI_RotationDlg::isValid( QString& msg )
427 {
428   switch (getConstructorId())
429     {
430     case 0:
431       {
432         return !(myObjects.length() == 0 || myAxis->_is_nil());
433         break;
434       }
435     case 1:
436       {
437         return !(myObjects.length() == 0 || myCentPoint->_is_nil() || myPoint1->_is_nil() || myPoint2->_is_nil() );
438         break;
439       }
440     default: return false;
441     }
442 }
443
444
445 //=================================================================================
446 // function : execute
447 // purpose  :
448 //=================================================================================
449 bool TransformationGUI_RotationDlg::execute( ObjectList& objects )
450 {
451   bool res = false;
452   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
453
454   GEOM::GEOM_Object_var anObj;
455
456   switch ( getConstructorId() )
457     {
458     case 0 :
459       {
460         if (toCreateCopy)
461           for (int i = 0; i < myObjects.length(); i++)
462             {
463               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
464                 RotateCopy( myObjects[i], myAxis, GetAngle() * PI180 );
465               if ( !anObj->_is_nil() )
466                 objects.push_back( anObj._retn() );
467             }
468         else
469           for (int i = 0; i < myObjects.length(); i++)
470             {
471               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
472                 Rotate( myObjects[i], myAxis, GetAngle() * PI180 );
473               if ( !anObj->_is_nil() )
474                 objects.push_back( anObj._retn() );
475             }
476         res = true;
477         break;
478       }
479     case 1 :
480       {
481         if (toCreateCopy)
482           for (int i = 0; i < myObjects.length(); i++)
483             {
484               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
485                 RotateThreePointsCopy( myObjects[i], myCentPoint, myPoint1, myPoint2 );
486               if ( !anObj->_is_nil() )
487                 objects.push_back( anObj._retn() );
488             }
489         else
490           for (int i = 0; i < myObjects.length(); i++)
491             {
492               anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
493                 RotateThreePoints( myObjects[i], myCentPoint, myPoint1, myPoint2 );     
494               if ( !anObj->_is_nil() )
495                 objects.push_back( anObj._retn() );
496             }
497         res = true;
498         break;
499       }
500     }
501
502   return res;
503 }
504
505
506 //=================================================================================
507 // function : closeEvent
508 // purpose  :
509 //=================================================================================
510 void TransformationGUI_RotationDlg::closeEvent( QCloseEvent* e )
511 {
512   GEOMBase_Skeleton::closeEvent( e );
513 }
514
515
516 //=================================================================================
517 // function : GetAngle()
518 // purpose  :
519 //=================================================================================
520 double TransformationGUI_RotationDlg::GetAngle() const
521 {
522   return GroupPoints->SpinBox_DX->GetValue();
523 }
524
525
526 //=================================================================================
527 // function :  CreateCopyModeChanged()
528 // purpose  :
529 //=================================================================================
530 void TransformationGUI_RotationDlg::CreateCopyModeChanged(bool isCreateCopy)
531 {
532   this->GroupBoxName->setEnabled(isCreateCopy);
533 }
534
535
536 //=================================================================================
537 // function :  onReverse()
538 // purpose  :
539 //=================================================================================
540 void TransformationGUI_RotationDlg::onReverse()
541 {
542   double anOldValue = GroupPoints->SpinBox_DX->GetValue();
543   GroupPoints->SpinBox_DX->SetValue( -anOldValue );
544 }
545
546 //=================================================================================
547 // function : addSubshapeToStudy
548 // purpose  : virtual method to add new SubObjects if local selection
549 //=================================================================================
550 void TransformationGUI_RotationDlg::addSubshapesToStudy()
551 {
552   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
553   if (toCreateCopy) {
554   QMap<QString, GEOM::GEOM_Object_var> objMap;
555   switch (getConstructorId())
556     {
557     case 0:
558       objMap[GroupPoints->LineEdit2->text()] = myAxis;
559       break;
560     case 1:
561       objMap[GroupPoints->LineEdit2->text()] = myCentPoint;
562       objMap[GroupPoints->LineEdit4->text()] = myPoint1;
563       objMap[GroupPoints->LineEdit5->text()] = myPoint2;
564       break;
565     }
566   addSubshapesToFather( objMap );
567   }
568 }