]> SALOME platform Git repositories - modules/geom.git/blob - src/TransformationGUI/TransformationGUI_TranslationDlg.cxx
Salome HOME
Avoid linking to the native omniORB
[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   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
153   
154   connect( GroupPoints->CheckBox1, SIGNAL( toggled( bool ) ), this, SLOT( CreateCopyModeChanged( bool ) ) );
155   
156   connect( myGeomGUI->getApp()->selectionMgr(), 
157            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
158
159   initName( tr( "GEOM_TRANSLATION" ) );
160
161   ConstructorsClicked( 0 );
162 }
163
164
165 //=================================================================================
166 // function : SetDoubleSpinBoxStep()
167 // purpose  : Double spin box management
168 //=================================================================================
169 void TransformationGUI_TranslationDlg::SetDoubleSpinBoxStep( double step )
170 {
171   GroupPoints->SpinBox1->setSingleStep(step);
172   GroupPoints->SpinBox2->setSingleStep(step);
173   GroupPoints->SpinBox3->setSingleStep(step);
174 }
175
176 //=================================================================================
177 // function : ConstructorsClicked()
178 // purpose  : Radio button management
179 //=================================================================================
180 void TransformationGUI_TranslationDlg::ConstructorsClicked( int constructorId )
181 {
182   erasePreview();
183   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
184   
185   myEditCurrentArgument = GroupPoints->LineEdit1;
186   globalSelection();
187
188   switch ( constructorId ) {
189   case 0: /* translation an object by dx, dy, dz */
190     {    
191       GroupPoints->ShowRows( 1, 2, false );
192       GroupPoints->ShowRows( 3, 5, true );
193       break;
194     }
195   case 1: /* translation an object by 2 points */
196     {
197       GroupPoints->ShowRows( 3, 5, false );
198       GroupPoints->ShowRows( 0, 2, true );
199       GroupPoints->TextLabel2->setText( tr( "GEOM_POINT_I" ).arg( 1 ) );
200       GroupPoints->LineEdit2->clear();
201       GroupPoints->LineEdit3->clear();
202       myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
203       break;
204     } 
205   case 2: /* translation an object by vector */
206     {
207       GroupPoints->ShowRows( 2, 5, false );
208       GroupPoints->ShowRows( 0, 1, true );
209       GroupPoints->TextLabel2->setText( tr( "GEOM_VECTOR" ) );
210       GroupPoints->LineEdit2->clear();
211       myVector = GEOM::GEOM_Object::_nil();
212       break;
213     }
214   }
215   
216   qApp->processEvents();
217   updateGeometry();
218   resize( minimumSize() );
219
220   myEditCurrentArgument->setFocus();
221   connect( myGeomGUI->getApp()->selectionMgr(), 
222            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
223 }
224
225
226 //=================================================================================
227 // function : ClickOnOk()
228 // purpose  :
229 //=================================================================================
230 void TransformationGUI_TranslationDlg::ClickOnOk()
231 {
232   if ( ClickOnApply() )
233     ClickOnCancel();
234 }
235
236
237 //=================================================================================
238 // function : ClickOnApply()
239 // purpose  :
240 //=================================================================================
241 bool TransformationGUI_TranslationDlg::ClickOnApply()
242 {
243   if ( !onAccept(GroupPoints->CheckBox1->isChecked()) )
244     return false;
245   
246   initName();
247
248   return true;
249 }
250
251
252 //=================================================================================
253 // function : SelectionIntoArgument()
254 // purpose  : Called when selection as changed or other case
255 //=================================================================================
256 void TransformationGUI_TranslationDlg::SelectionIntoArgument()
257 {
258   myEditCurrentArgument->setText( "" );
259   QString aName;
260   
261   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
262     int aNbSel = GEOMBase::GetNameOfSelectedIObjects( selectedIO(), aName );
263     
264     if ( aNbSel < 1 ) {
265       myObjects.length( 0 );
266           return;
267     }
268     GEOMBase::ConvertListOfIOInListOfGO( selectedIO(), myObjects );
269     if ( !myObjects.length() )
270       return;
271   }
272   else {
273     if ( IObjectCount() != 1 ) {
274       if ( myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 1 )
275         myPoint1 = GEOM::GEOM_Object::_nil();
276       else if ( myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 2 )
277         myVector = GEOM::GEOM_Object::_nil();
278       else if ( myEditCurrentArgument == GroupPoints->LineEdit3 )
279         myPoint2 = GEOM::GEOM_Object::_nil();
280       return;
281     }
282     
283     Standard_Boolean testResult = Standard_False;;
284     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
285     
286     if ( !testResult || CORBA::is_nil( aSelectedObject ) )
287       return;
288     
289     TopoDS_Shape aShape;
290     aName = GEOMBase::GetName( aSelectedObject );
291     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
292       TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
293       if ( myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 2 )
294         aNeedType = TopAbs_EDGE;
295       
296       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
297       TColStd_IndexedMapOfInteger aMap;
298       aSelMgr->GetIndexes( firstIObject(), aMap );
299       if ( aMap.Extent() == 1 ) {
300         int anIndex = aMap( 1 );
301         if ( aNeedType == TopAbs_EDGE )
302           aName += QString( ":edge_%1" ).arg( anIndex );
303         else
304           aName += QString( ":vertex_%1" ).arg( anIndex );
305         
306         //Find SubShape Object in Father
307         GEOM::GEOM_Object_var aFindedObject = findObjectInFather( aSelectedObject, aName );
308         
309         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
310           GEOM::GEOM_IShapesOperations_var aShapesOp =
311             getGeomEngine()->GetIShapesOperations( getStudyId() );
312           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
313         }
314         else {
315           aSelectedObject = aFindedObject;
316         }
317       } 
318       else { // Global Selection
319         if ( aShape.ShapeType() != aNeedType ) {
320           aSelectedObject = GEOM::GEOM_Object::_nil();
321           aName = "";
322         }
323       }
324     }
325     
326     if ( myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 1 )
327       myPoint1 = aSelectedObject;
328     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 2 )
329       myVector = aSelectedObject;
330     else if ( myEditCurrentArgument == GroupPoints->LineEdit3 )
331       myPoint2 = aSelectedObject;
332   }
333   
334   myEditCurrentArgument->setText( aName );
335   
336   displayPreview();
337 }
338
339
340 //=================================================================================
341 // function : LineEditReturnPressed()
342 // purpose  :
343 //=================================================================================
344 void TransformationGUI_TranslationDlg::LineEditReturnPressed()
345 {
346   QLineEdit* send = (QLineEdit*)sender();
347   if ( send == GroupPoints->LineEdit1 ) {
348     myEditCurrentArgument = send;
349     GEOMBase_Skeleton::LineEditReturnPressed();
350   }
351 }
352
353
354 //=================================================================================
355 // function : SetEditCurrentArgument()
356 // purpose  :
357 //=================================================================================
358 void TransformationGUI_TranslationDlg::SetEditCurrentArgument()
359 {    
360   QPushButton* send = (QPushButton*)sender();
361   globalSelection();
362   
363   if ( send == GroupPoints->PushButton1 ) {
364     myEditCurrentArgument = GroupPoints->LineEdit1;
365   }
366   else if ( send == GroupPoints->PushButton2 ) {
367     myEditCurrentArgument = GroupPoints->LineEdit2;
368     if ( getConstructorId() == 1 )
369       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
370     else 
371       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
372   }
373   else if ( send == GroupPoints->PushButton3 ) {
374     myEditCurrentArgument = GroupPoints->LineEdit3;
375     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
376   }
377   
378   myEditCurrentArgument->setFocus();
379   SelectionIntoArgument();
380 }
381
382
383 //=================================================================================
384 // function : ActivateThisDialog()
385 // purpose  :
386 //=================================================================================
387 void TransformationGUI_TranslationDlg::ActivateThisDialog()
388 {
389   GEOMBase_Skeleton::ActivateThisDialog();
390   
391   connect( myGeomGUI->getApp()->selectionMgr(), 
392            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
393
394   ConstructorsClicked( getConstructorId() );
395 }
396
397
398 //=================================================================================
399 // function : enterEvent()
400 // purpose  :
401 //=================================================================================
402 void TransformationGUI_TranslationDlg::enterEvent( QEvent* )
403 {
404   if ( !mainFrame()->GroupConstructors->isEnabled() )
405     ActivateThisDialog();
406 }
407
408
409 //=================================================================================
410 // function : ValueChangedInSpinBox()
411 // purpose  :
412 //=================================================================================
413 void TransformationGUI_TranslationDlg::ValueChangedInSpinBox()
414 {
415   displayPreview();
416 }
417
418
419 //=================================================================================
420 // function : createOperation
421 // purpose  :
422 //=================================================================================
423 GEOM::GEOM_IOperations_ptr TransformationGUI_TranslationDlg::createOperation()
424 {
425   return getGeomEngine()->GetITransformOperations( getStudyId() );
426 }
427
428
429 //=================================================================================
430 // function : isValid
431 // purpose  :
432 //=================================================================================
433 bool TransformationGUI_TranslationDlg::isValid( QString& /*msg*/ )
434 {
435   int aConstructorId = getConstructorId();
436   
437   switch ( aConstructorId ) {
438   case 0: 
439     return !( myObjects.length() == 0 );
440     break;
441   case 1: 
442     return !( myObjects.length() == 0 || myPoint1->_is_nil() || myPoint2->_is_nil() );
443     break;
444   case 2: 
445     return !( myObjects.length() == 0 || myVector->_is_nil() );
446     break;
447   default: 
448     return false;
449   }
450 }
451
452 //=================================================================================
453 // function : execute
454 // purpose  :
455 //=================================================================================
456 bool TransformationGUI_TranslationDlg::execute( ObjectList& objects )
457 {
458   bool res = false;
459   bool toCreateCopy = IsPreview() || GroupPoints->CheckBox1->isChecked();
460   
461   GEOM::GEOM_Object_var anObj;
462
463   switch ( getConstructorId() ) {
464   case 0:
465     {
466       double dx = GroupPoints->SpinBox1->value();
467       double dy = GroupPoints->SpinBox2->value();
468       double dz = GroupPoints->SpinBox3->value();
469       
470       if ( toCreateCopy ) {
471         for ( int i = 0; i < myObjects.length(); i++ ) {
472           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->TranslateDXDYDZCopy( myObjects[i], dx, dy, dz );
473           if ( !anObj->_is_nil() )
474             objects.push_back( anObj._retn() );
475         }
476       }
477       else {
478         for ( int i = 0; i < myObjects.length(); i++ ) {
479           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->TranslateDXDYDZ( myObjects[i], dx, dy, dz );
480           if ( !anObj->_is_nil() )
481             objects.push_back( anObj._retn() );
482         }
483       }
484       res = true;
485       break;
486     }
487   case 1:
488     {
489       if ( toCreateCopy ) {
490         for ( int i = 0; i < myObjects.length(); i++ ) {
491           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->TranslateTwoPointsCopy( myObjects[i], myPoint1, myPoint2 );
492           if ( !anObj->_is_nil() )
493             objects.push_back( anObj._retn() );
494         }
495       }
496       else {
497         for ( int i = 0; i < myObjects.length(); i++ ) {
498           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->TranslateTwoPoints( myObjects[i], myPoint1, myPoint2 );   
499           if ( !anObj->_is_nil() )
500             objects.push_back( anObj._retn() );
501         }
502       }
503       res = true;
504       break;
505     }
506   case 2:
507     {
508       if ( toCreateCopy ) {
509         for ( int i = 0; i < myObjects.length(); i++ ) {
510           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->TranslateVectorCopy( myObjects[i], myVector );
511           if ( !anObj->_is_nil() )
512             objects.push_back( anObj._retn() );
513         }
514       }
515       else {
516         for ( int i = 0; i < myObjects.length(); i++ ) {
517           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->TranslateVector( myObjects[i], myVector );
518           if ( !anObj->_is_nil() )
519             objects.push_back( anObj._retn() );
520         }
521       }
522       res = true;
523       break;
524     }
525   }
526   
527   return res;
528 }
529
530
531 //=================================================================================
532 // function :  CreateCopyModeChanged()
533 // purpose  :
534 //=================================================================================
535 void TransformationGUI_TranslationDlg::CreateCopyModeChanged( bool isCreateCopy )
536 {
537   mainFrame()->GroupBoxName->setEnabled( isCreateCopy );
538 }
539
540 //=================================================================================
541 // function : addSubshapeToStudy
542 // purpose  : virtual method to add new SubObjects if local selection
543 //=================================================================================
544 void TransformationGUI_TranslationDlg::addSubshapesToStudy()
545 {
546   bool toCreateCopy = IsPreview() || GroupPoints->CheckBox1->isChecked();
547   if ( toCreateCopy ) {
548     QMap<QString, GEOM::GEOM_Object_var> objMap;
549
550     switch ( getConstructorId() ) {
551     case 0:
552       return;
553     case 1:
554       objMap[GroupPoints->LineEdit2->text()] = myPoint1;
555       objMap[GroupPoints->LineEdit3->text()] = myPoint2;
556       break;
557     case 2:
558       objMap[GroupPoints->LineEdit2->text()] = myVector;
559       break;
560     }
561     addSubshapesToFather( objMap );
562   }
563 }