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