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