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