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