Salome HOME
*** empty log message ***
[modules/geom.git] / src / TransformationGUI / TransformationGUI_TranslationDlg.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_TranslationDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "TransformationGUI_TranslationDlg.h"
27
28 #include <GEOM_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_TranslationDlg()
41 // purpose  : Constructs a TransformationGUI_TranslationDlg 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_TranslationDlg::TransformationGUI_TranslationDlg
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_TRANSLATION_DXYZ" ) ) );
52   QPixmap image1( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_TRANSLATION_POINTS" ) ) );
53   QPixmap image2( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_TRANSLATION_VECTOR" ) ) );
54   QPixmap image3( aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
55
56   setWindowTitle( tr( "GEOM_TRANSLATION_TITLE" ) );
57
58   /***************************************************************/
59   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_TRANSLATION" ) );
60   mainFrame()->RadioButton1->setIcon( image0 );
61   mainFrame()->RadioButton2->setIcon( image1 );
62   mainFrame()->RadioButton3->setIcon( image2 );
63   mainFrame()->RadioButton1->setChecked( true );
64
65   GroupPoints = new DlgRef_3Sel3Spin1Check( centralWidget() );
66   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
67   GroupPoints->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
68   GroupPoints->TextLabel3->setText( tr( "GEOM_POINT_I" ).arg( 2 ) );
69   GroupPoints->TextLabel4->setText( tr( "GEOM_DX" ) );
70   GroupPoints->TextLabel5->setText( tr( "GEOM_DY" ) );
71   GroupPoints->TextLabel6->setText( tr( "GEOM_DZ" ) );
72   GroupPoints->PushButton1->setIcon( image3 );
73   GroupPoints->PushButton2->setIcon( image3 );
74   GroupPoints->PushButton3->setIcon( image3 );
75   GroupPoints->CheckBox1->setText( tr( "GEOM_CREATE_COPY" ) );
76
77   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
78   layout->setMargin( 0 ); layout->setSpacing( 6 );
79   layout->addWidget( GroupPoints );
80   /***************************************************************/
81   
82   setHelpFileName( "translation.htm" );
83   
84   Init();
85 }
86
87
88 //=================================================================================
89 // function : ~TransformationGUI_TranslationDlg()
90 // purpose  : Destroys the object and frees any allocated resources
91 //=================================================================================
92 TransformationGUI_TranslationDlg::~TransformationGUI_TranslationDlg()
93 {
94   // no need to delete child widgets, Qt does it all for us
95 }
96
97
98 //=================================================================================
99 // function : Init()
100 // purpose  :
101 //=================================================================================
102 void TransformationGUI_TranslationDlg::Init()
103 {
104   /* init variables */
105   myEditCurrentArgument = GroupPoints->LineEdit1;
106   GroupPoints->LineEdit1->setReadOnly( true );
107   GroupPoints->LineEdit2->setReadOnly( true );
108   GroupPoints->LineEdit3->setReadOnly( true );
109   
110   myVector = myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
111   
112   // Activate Create a Copy mode
113   GroupPoints->CheckBox1->setChecked( true );
114   CreateCopyModeChanged( true );
115   
116   /* Get setting of step value from file configuration */
117   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
118   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
119   
120   /* min, max, step and decimals for spin boxes & initial values */
121   initSpinBox( GroupPoints->SpinBox1, COORD_MIN, COORD_MAX, step, 3 );
122   initSpinBox( GroupPoints->SpinBox2, COORD_MIN, COORD_MAX, step, 3 );
123   initSpinBox( GroupPoints->SpinBox3, COORD_MIN, COORD_MAX, step, 3 );
124   
125   GroupPoints->SpinBox1->setValue( 0.0 );
126   GroupPoints->SpinBox2->setValue( 0.0 );
127   GroupPoints->SpinBox3->setValue( 0.0 );
128   
129   /* signals and slots connections */
130   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
131   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
132
133   connect( this, SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
134
135   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
136   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
137   connect( GroupPoints->PushButton3, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
138
139   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ); //@ Delete ?
140
141   connect( GroupPoints->SpinBox1, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
142   connect( GroupPoints->SpinBox2, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
143   connect( GroupPoints->SpinBox3, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
144   
145   // VSR: TODO ->>
146   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupPoints->SpinBox1, SLOT( SetStep( double ) ) );
147   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupPoints->SpinBox2, SLOT( SetStep( double ) ) );
148   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), GroupPoints->SpinBox3, SLOT( SetStep( double ) ) );
149   // <<-
150   
151   connect( GroupPoints->CheckBox1, SIGNAL( toggled( bool ) ), this, SLOT( CreateCopyModeChanged( bool ) ) );
152   
153   connect( myGeomGUI->getApp()->selectionMgr(), 
154            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
155
156   initName( tr( "GEOM_TRANSLATION" ) );
157
158   ConstructorsClicked( 0 );
159 }
160
161
162 //=================================================================================
163 // function : ConstructorsClicked()
164 // purpose  : Radio button management
165 //=================================================================================
166 void TransformationGUI_TranslationDlg::ConstructorsClicked( int constructorId )
167 {
168   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
169   
170   myEditCurrentArgument = GroupPoints->LineEdit1;
171   globalSelection();
172
173   switch ( constructorId ) {
174   case 0: /* translation an object by dx, dy, dz */
175     {    
176       GroupPoints->ShowRows( 1, 2, false );
177       GroupPoints->ShowRows( 3, 5, true );
178       break;
179     }
180   case 1: /* translation an object by 2 points */
181     {
182       GroupPoints->ShowRows( 3, 5, false );
183       GroupPoints->ShowRows( 0, 2, true );
184       GroupPoints->TextLabel2->setText( tr( "GEOM_POINT_I" ).arg( 1 ) );
185       GroupPoints->LineEdit2->clear();
186       GroupPoints->LineEdit3->clear();
187       myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
188       break;
189     } 
190   case 2: /* translation an object by vector */
191     {
192       GroupPoints->ShowRows( 2, 5, false );
193       GroupPoints->ShowRows( 0, 1, true );
194       GroupPoints->TextLabel2->setText( tr( "GEOM_VECTOR" ) );
195       GroupPoints->LineEdit2->clear();
196       myVector = GEOM::GEOM_Object::_nil();
197       break;
198     }
199   }
200   
201   qApp->processEvents();
202   updateGeometry();
203   resize( minimumSize() );
204
205   myEditCurrentArgument->setFocus();
206   connect( myGeomGUI->getApp()->selectionMgr(), 
207            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
208 }
209
210
211 //=================================================================================
212 // function : ClickOnOk()
213 // purpose  :
214 //=================================================================================
215 void TransformationGUI_TranslationDlg::ClickOnOk()
216 {
217   if ( ClickOnApply() )
218     ClickOnCancel();
219 }
220
221
222 //=================================================================================
223 // function : ClickOnApply()
224 // purpose  :
225 //=================================================================================
226 bool TransformationGUI_TranslationDlg::ClickOnApply()
227 {
228   if ( !onAccept(GroupPoints->CheckBox1->isChecked()) )
229     return false;
230   
231   initName();
232   ConstructorsClicked( getConstructorId() );
233   return true;
234 }
235
236
237 //=================================================================================
238 // function : SelectionIntoArgument()
239 // purpose  : Called when selection as changed or other case
240 //=================================================================================
241 void TransformationGUI_TranslationDlg::SelectionIntoArgument()
242 {
243   myEditCurrentArgument->setText( "" );
244   QString aName;
245   
246   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
247     int aNbSel = GEOMBase::GetNameOfSelectedIObjects( selectedIO(), aName );
248     
249     if ( aNbSel < 1 ) {
250       myObjects.length( 0 );
251           return;
252     }
253     GEOMBase::ConvertListOfIOInListOfGO( selectedIO(), myObjects );
254     if ( !myObjects.length() )
255       return;
256   }
257   else {
258     if ( IObjectCount() != 1 ) {
259       if ( myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 1 )
260         myPoint1 = GEOM::GEOM_Object::_nil();
261       else if ( myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 2 )
262         myVector = GEOM::GEOM_Object::_nil();
263       else if ( myEditCurrentArgument == GroupPoints->LineEdit3 )
264         myPoint2 = GEOM::GEOM_Object::_nil();
265       return;
266     }
267     
268     Standard_Boolean testResult = Standard_False;;
269     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
270     
271     if ( !testResult || CORBA::is_nil( aSelectedObject ) )
272       return;
273     
274     if ( myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 1 )
275       myPoint1 = aSelectedObject;
276     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 2 )
277       myVector = aSelectedObject;
278     else if ( myEditCurrentArgument == GroupPoints->LineEdit3 )
279       myPoint2 = aSelectedObject; 
280     
281     aName = GEOMBase::GetName( aSelectedObject );
282   }
283   
284   myEditCurrentArgument->setText( aName );
285   
286   displayPreview();
287 }
288
289
290 //=================================================================================
291 // function : LineEditReturnPressed()
292 // purpose  :
293 //=================================================================================
294 void TransformationGUI_TranslationDlg::LineEditReturnPressed()
295 {
296   QLineEdit* send = (QLineEdit*)sender();
297   if ( send == GroupPoints->LineEdit1 ) {
298     myEditCurrentArgument = send;
299     GEOMBase_Skeleton::LineEditReturnPressed();
300   }
301 }
302
303
304 //=================================================================================
305 // function : SetEditCurrentArgument()
306 // purpose  :
307 //=================================================================================
308 void TransformationGUI_TranslationDlg::SetEditCurrentArgument()
309 {    
310   QPushButton* send = (QPushButton*)sender();
311   
312   if ( send == GroupPoints->PushButton1 ) {
313     myEditCurrentArgument = GroupPoints->LineEdit1;
314     globalSelection();
315   }
316   else if ( send == GroupPoints->PushButton2 ) {
317     myEditCurrentArgument = GroupPoints->LineEdit2;
318     getConstructorId() == 1 ? globalSelection( GEOM_POINT ) :
319       globalSelection( GEOM_LINE  );
320   }
321   else if ( send == GroupPoints->PushButton3 ) {
322     myEditCurrentArgument = GroupPoints->LineEdit3;
323     globalSelection( GEOM_POINT );
324   }
325   
326   myEditCurrentArgument->setFocus();
327   SelectionIntoArgument();
328 }
329
330
331 //=================================================================================
332 // function : ActivateThisDialog()
333 // purpose  :
334 //=================================================================================
335 void TransformationGUI_TranslationDlg::ActivateThisDialog()
336 {
337   GEOMBase_Skeleton::ActivateThisDialog();
338   connect( myGeomGUI->getApp()->selectionMgr(), 
339            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
340
341   ConstructorsClicked( getConstructorId() );
342 }
343
344
345 //=================================================================================
346 // function : enterEvent()
347 // purpose  :
348 //=================================================================================
349 void TransformationGUI_TranslationDlg::enterEvent( QEvent* )
350 {
351   if( !mainFrame()->GroupConstructors->isEnabled() )
352     ActivateThisDialog();
353 }
354
355
356 //=================================================================================
357 // function : ValueChangedInSpinBox()
358 // purpose  :
359 //=================================================================================
360 void TransformationGUI_TranslationDlg::ValueChangedInSpinBox()
361 {
362   displayPreview();
363 }
364
365
366 //=================================================================================
367 // function : createOperation
368 // purpose  :
369 //=================================================================================
370 GEOM::GEOM_IOperations_ptr TransformationGUI_TranslationDlg::createOperation()
371 {
372   return myGeomGUI->GetGeomGen()->GetITransformOperations( getStudyId() );
373 }
374
375
376 //=================================================================================
377 // function : isValid
378 // purpose  :
379 //=================================================================================
380 bool TransformationGUI_TranslationDlg::isValid( QString& /*msg*/ )
381 {
382   int aConstructorId = getConstructorId();
383   
384   switch ( aConstructorId ) {
385   case 0: 
386     return !( myObjects.length() == 0 );
387     break;
388   case 1: 
389     return !( myObjects.length() == 0 || myPoint1->_is_nil() || myPoint2->_is_nil() );
390     break;
391   case 2: 
392     return !( myObjects.length() == 0 || myVector->_is_nil() );
393     break;
394   default: 
395     return false;
396   }
397 }
398
399 //=================================================================================
400 // function : execute
401 // purpose  :
402 //=================================================================================
403 bool TransformationGUI_TranslationDlg::execute( ObjectList& objects )
404 {
405   bool res = false;
406   bool toCreateCopy = IsPreview() || GroupPoints->CheckBox1->isChecked();
407   
408   GEOM::GEOM_Object_var anObj;
409
410   switch ( getConstructorId() ) {
411   case 0:
412     {
413       double dx = GroupPoints->SpinBox1->value();
414       double dy = GroupPoints->SpinBox2->value();
415       double dz = GroupPoints->SpinBox3->value();
416       
417       if ( toCreateCopy ) {
418         for ( int i = 0; i < myObjects.length(); i++ ) {
419           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->TranslateDXDYDZCopy( myObjects[i], dx, dy, dz );
420           if ( !anObj->_is_nil() )
421             objects.push_back( anObj._retn() );
422         }
423       }
424       else {
425         for ( int i = 0; i < myObjects.length(); i++ ) {
426           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->TranslateDXDYDZ( myObjects[i], dx, dy, dz );
427           if ( !anObj->_is_nil() )
428             objects.push_back( anObj._retn() );
429         }
430       }
431       res = true;
432       break;
433     }
434   case 1:
435     {
436       if ( toCreateCopy ) {
437         for ( int i = 0; i < myObjects.length(); i++ ) {
438           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->TranslateTwoPointsCopy( myObjects[i], myPoint1, myPoint2 );
439           if ( !anObj->_is_nil() )
440             objects.push_back( anObj._retn() );
441         }
442       }
443       else {
444         for ( int i = 0; i < myObjects.length(); i++ ) {
445           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->TranslateTwoPoints( myObjects[i], myPoint1, myPoint2 );   
446           if ( !anObj->_is_nil() )
447             objects.push_back( anObj._retn() );
448         }
449       }
450       res = true;
451       break;
452     }
453   case 2:
454     {
455       if ( toCreateCopy ) {
456         for ( int i = 0; i < myObjects.length(); i++ ) {
457           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->TranslateVectorCopy( myObjects[i], myVector );
458           if ( !anObj->_is_nil() )
459             objects.push_back( anObj._retn() );
460         }
461       }
462       else {
463         for ( int i = 0; i < myObjects.length(); i++ ) {
464           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->TranslateVector( myObjects[i], myVector );
465           if ( !anObj->_is_nil() )
466             objects.push_back( anObj._retn() );
467         }
468       }
469       res = true;
470       break;
471     }
472   }
473   
474   return res;
475 }
476
477
478 //=================================================================================
479 // function : closeEvent
480 // purpose  :
481 //=================================================================================
482 void  TransformationGUI_TranslationDlg::closeEvent( QCloseEvent* e )
483 {
484   // myGeomGUI->SetState( -1 );
485   GEOMBase_Skeleton::closeEvent( e );
486 }
487
488
489 //=================================================================================
490 // function :  CreateCopyModeChanged()
491 // purpose  :
492 //=================================================================================
493 void TransformationGUI_TranslationDlg::CreateCopyModeChanged( bool isCreateCopy )
494 {
495   mainFrame()->GroupBoxName->setEnabled( isCreateCopy );
496 }