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