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