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