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