Salome HOME
e71497d848a639a5fd6484ff619c86e60f3380f7
[modules/geom.git] / src / TransformationGUI / TransformationGUI_MultiRotationDlg.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_MultiRotationDlg.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header$
28
29 #include "TransformationGUI_MultiRotationDlg.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 <qcheckbox.h>
44 #include <qlabel.h>
45
46 #include "GEOMImpl_Types.hxx"
47
48 #include "utilities.h"
49
50 using namespace std;
51
52 //=================================================================================
53 // class    : TransformationGUI_MultiRotationDlg()
54 // purpose  : Constructs a TransformationGUI_MultiRotationDlg 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_MultiRotationDlg::TransformationGUI_MultiRotationDlg
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_MULTIROTATION_SIMPLE")));
66   QPixmap image1(aResMgr->loadPixmap("GEOM",tr("ICON_DLG_MULTIROTATION_DOUBLE")));
67   QPixmap image2(aResMgr->loadPixmap("GEOM",tr("ICON_SELECT")));
68
69   setCaption(tr("GEOM_MULTIROTATION_TITLE"));
70
71   /***************************************************************/
72   GroupConstructors->setTitle(tr("GEOM_MULTIROTATION"));
73   RadioButton1->setPixmap(image0);
74   RadioButton2->setPixmap(image1);
75   RadioButton3->close(TRUE);
76
77   GroupPoints = new DlgRef_2Sel1Spin(this, "GroupPoints");
78   GroupPoints->GroupBox1->setTitle(tr("GEOM_MULTIROTATION_SIMPLE"));
79   GroupPoints->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
80   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
81   GroupPoints->TextLabel3->setText(tr("GEOM_NB_TIMES"));
82   GroupPoints->PushButton1->setPixmap(image2);
83   GroupPoints->PushButton2->setPixmap(image2);
84   GroupPoints->LineEdit1->setReadOnly(true);
85   GroupPoints->LineEdit2->setReadOnly(true);
86
87   GroupDimensions = new DlgRef_2Sel4Spin1Check(this, "GroupDimensions");
88   GroupDimensions->GroupBox1->setTitle(tr("GEOM_MULTIROTATION_DOUBLE"));
89   GroupDimensions->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
90   GroupDimensions->TextLabel2->setText(tr("GEOM_VECTOR"));
91   GroupDimensions->TextLabel3->setText(tr("GEOM_ANGLE"));
92   GroupDimensions->TextLabel4->setText(tr("GEOM_NB_TIMES"));
93   GroupDimensions->TextLabel5->setText(tr("GEOM_STEP"));
94   GroupDimensions->TextLabel6->setText(tr("GEOM_NB_TIMES"));
95   GroupDimensions->CheckButton1->setText(tr("GEOM_REVERSE"));
96   GroupDimensions->PushButton1->setPixmap(image2);
97   GroupDimensions->PushButton2->setPixmap(image2);
98   GroupDimensions->LineEdit1->setReadOnly(true);
99   GroupDimensions->LineEdit2->setReadOnly(true);
100
101   Layout1->addWidget(GroupPoints, 2, 0);
102   Layout1->addWidget(GroupDimensions, 2, 0);
103   /***************************************************************/
104
105   setHelpFileName("multi_rotation_operation_page.html");
106
107   Init();
108 }
109
110
111 //=================================================================================
112 // function : ~TransformationGUI_MultiRotationDlg()
113 // purpose  : Destroys the object and frees any allocated resources
114 //=================================================================================
115 TransformationGUI_MultiRotationDlg::~TransformationGUI_MultiRotationDlg()
116 {
117   // no need to delete child widgets, Qt does it all for us
118 }
119
120
121 //=================================================================================
122 // function : Init()
123 // purpose  :
124 //=================================================================================
125 void TransformationGUI_MultiRotationDlg::Init()
126 {
127   /* Get setting of step value from file configuration */
128   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
129   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
130
131   double SpecificStep1 = 5;
132   double SpecificStep2 = 1;
133   /* min, max, step and decimals for spin boxes & initial values */
134   GroupPoints->SpinBox_DX->RangeStepAndValidator(1.0, MAX_NUMBER, SpecificStep2, 10);
135   GroupPoints->SpinBox_DX->SetValue(myNbTimes1);
136
137   GroupDimensions->SpinBox_DX1->RangeStepAndValidator(COORD_MIN, COORD_MAX, SpecificStep1, DBL_DIGITS_DISPLAY);
138   GroupDimensions->SpinBox_DY1->RangeStepAndValidator(1.0, MAX_NUMBER, SpecificStep2, 10);
139   GroupDimensions->SpinBox_DX2->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY);
140   GroupDimensions->SpinBox_DY2->RangeStepAndValidator(1.0, MAX_NUMBER, SpecificStep2, 10);
141   GroupDimensions->SpinBox_DX1->SetValue(myAng);
142   GroupDimensions->SpinBox_DY1->SetValue(myNbTimes1);
143   GroupDimensions->SpinBox_DX2->SetValue(myStep);
144   GroupDimensions->SpinBox_DY2->SetValue(myNbTimes2);
145
146   /* signals and slots connections */
147   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
148   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
149   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
150
151   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
152   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
153   connect(GroupDimensions->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
154   connect(GroupDimensions->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
155
156   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
157   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
158   connect(GroupDimensions->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
159   connect(GroupDimensions->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
160
161   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
162   connect(GroupDimensions->SpinBox_DX1, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
163   connect(GroupDimensions->SpinBox_DY1, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
164   connect(GroupDimensions->SpinBox_DX2, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
165   connect(GroupDimensions->SpinBox_DY2, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
166
167   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
168   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX1, SLOT(SetStep(double)));
169   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DY1, SLOT(SetStep(double)));
170   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX2, SLOT(SetStep(double)));
171   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DY2, SLOT(SetStep(double)));
172
173   connect(GroupDimensions->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(ReverseAngle()));
174
175   connect(myGeomGUI->getApp()->selectionMgr(),
176           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
177
178   initName( tr( "GEOM_MULTIROTATION" ) );
179   ConstructorsClicked( 0 );
180 }
181
182
183 //=================================================================================
184 // function : ConstructorsClicked()
185 // purpose  : Radio button management
186 //=================================================================================
187 void TransformationGUI_MultiRotationDlg::ConstructorsClicked(int constructorId)
188 {
189   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
190
191   myAng = 45.0;
192   myStep = 50.0;
193   myNbTimes1 = myNbTimes2 = 2;
194
195   globalSelection( GEOM_ALLSHAPES );
196
197   switch (constructorId)
198     {
199     case 0: /* Rotate simple */
200       {
201         GroupDimensions->hide();
202         resize(0, 0);
203         GroupPoints->show();
204
205         myEditCurrentArgument = GroupPoints->LineEdit1;
206         GroupPoints->LineEdit1->setText("");
207         GroupPoints->LineEdit2->setText("");
208
209         GroupPoints->SpinBox_DX->SetValue(myNbTimes1);
210
211         break;
212       }
213     case 1: /* Rotate double */
214       {
215         GroupPoints->hide();
216         resize(0, 0);
217         GroupDimensions->show();
218
219         myEditCurrentArgument = GroupDimensions->LineEdit1;
220         GroupDimensions->LineEdit1->setText("");
221         GroupDimensions->LineEdit2->setText("");
222
223         GroupDimensions->SpinBox_DX1->SetValue(myAng);
224         GroupDimensions->SpinBox_DY1->SetValue(myNbTimes1);
225         GroupDimensions->SpinBox_DX2->SetValue(myStep);
226         GroupDimensions->SpinBox_DY2->SetValue(myNbTimes2);
227
228         break;
229       }
230     }
231
232   myEditCurrentArgument->setFocus();
233   myBase = myVector = GEOM::GEOM_Object::_nil();
234   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
235           this, SLOT(SelectionIntoArgument()));
236 }
237
238
239 //=================================================================================
240 // function : ClickOnOk()
241 // purpose  :
242 //=================================================================================
243 void TransformationGUI_MultiRotationDlg::ClickOnOk()
244 {
245   if ( ClickOnApply() )
246     ClickOnCancel();
247 }
248
249
250 //=================================================================================
251 // function : ClickOnApply()
252 // purpose  :
253 //=================================================================================
254 bool TransformationGUI_MultiRotationDlg::ClickOnApply()
255 {
256   if ( !onAccept() )
257     return false;
258
259   initName();
260   ConstructorsClicked( getConstructorId() );
261   return true;
262 }
263
264
265 //=================================================================================
266 // function : SelectionIntoArgument()
267 // purpose  : Called when selection as changed or other case
268 //=================================================================================
269 void TransformationGUI_MultiRotationDlg::SelectionIntoArgument()
270 {
271   myEditCurrentArgument->setText("");
272
273   if (IObjectCount() != 1) {
274     if (myEditCurrentArgument == GroupPoints->LineEdit1 ||
275         myEditCurrentArgument == GroupDimensions->LineEdit1)
276       myBase = GEOM::GEOM_Object::_nil();
277     else if (myEditCurrentArgument == GroupPoints->LineEdit2 ||
278              myEditCurrentArgument == GroupDimensions->LineEdit2)
279       myVector = GEOM::GEOM_Object::_nil();
280     return;
281   }
282
283   // nbSel == 1
284   Standard_Boolean testResult = Standard_False;;
285   GEOM::GEOM_Object_var aSelectedObject =
286     GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
287
288   if (!testResult || CORBA::is_nil(aSelectedObject) || !GEOMBase::IsShape(aSelectedObject))
289     return;
290
291   QString aName = GEOMBase::GetName( aSelectedObject );
292
293   if (myEditCurrentArgument == GroupPoints->LineEdit1 ||
294       myEditCurrentArgument == GroupDimensions->LineEdit1)
295     myBase = aSelectedObject;
296   else if (myEditCurrentArgument == GroupPoints->LineEdit2 ||
297            myEditCurrentArgument == GroupDimensions->LineEdit2) {
298     if ( testResult && !aSelectedObject->_is_nil() )
299         {
300           TopoDS_Shape aShape;
301           
302           
303           if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
304             {
305               LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
306               TColStd_IndexedMapOfInteger aMap;
307               aSelMgr->GetIndexes( firstIObject(), aMap );
308               if ( aMap.Extent() == 1 )
309                 {
310                   int anIndex = aMap( 1 );
311                   aName += QString(":edge_%1").arg(anIndex);
312                   
313                   //Find SubShape Object in Father
314                   GEOM::GEOM_Object_var aFindedObject = findObjectInFather(aSelectedObject, aName);
315                   
316                   if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
317                     GEOM::GEOM_IShapesOperations_var aShapesOp =
318                       getGeomEngine()->GetIShapesOperations( getStudyId() );
319                     myVector = aShapesOp->GetSubShape(aSelectedObject, anIndex);
320                   }
321                   else
322                     myVector = aFindedObject; // get existing object
323                 }
324               else {
325                 if (aShape.ShapeType() != TopAbs_EDGE) {
326                   aSelectedObject = GEOM::GEOM_Object::_nil();
327                   aName = "";
328                 }
329                 myVector = aSelectedObject;
330               }
331               
332             }
333         }
334   }
335   myEditCurrentArgument->setText( aName );
336
337   displayPreview();
338 }
339
340
341 //=================================================================================
342 // function : SetEditCurrentArgument()
343 // purpose  :
344 //=================================================================================
345 void TransformationGUI_MultiRotationDlg::SetEditCurrentArgument()
346 {
347   QPushButton* send = (QPushButton*)sender();
348   globalSelection( GEOM_ALLSHAPES );
349
350   if(send == GroupPoints->PushButton1) {
351     myEditCurrentArgument = GroupPoints->LineEdit1;
352   }
353   else if(send == GroupPoints->PushButton2) {
354     myEditCurrentArgument = GroupPoints->LineEdit2;
355     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
356   }
357   else if(send == GroupDimensions->PushButton1) {
358     myEditCurrentArgument = GroupDimensions->LineEdit1;
359   }
360   else if(send == GroupDimensions->PushButton2) {
361     myEditCurrentArgument = GroupDimensions->LineEdit2;
362     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
363   }
364
365   myEditCurrentArgument->setFocus();
366   SelectionIntoArgument();
367 }
368
369
370 //=================================================================================
371 // function : LineEditReturnPressed()
372 // purpose  :
373 //=================================================================================
374 void TransformationGUI_MultiRotationDlg::LineEditReturnPressed()
375 {
376   QLineEdit* send = (QLineEdit*)sender();
377   if(send == GroupPoints->LineEdit1 || send == GroupDimensions->LineEdit1 ||
378      send == GroupPoints->LineEdit2 || send == GroupDimensions->LineEdit2)
379     {
380       myEditCurrentArgument = send;
381       GEOMBase_Skeleton::LineEditReturnPressed();
382     }
383 }
384
385
386 //=================================================================================
387 // function : ActivateThisDialog()
388 // purpose  :
389 //=================================================================================
390 void TransformationGUI_MultiRotationDlg::ActivateThisDialog()
391 {
392   GEOMBase_Skeleton::ActivateThisDialog();
393   connect(myGeomGUI->getApp()->selectionMgr(),
394           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
395
396   ConstructorsClicked( getConstructorId() );
397 }
398
399
400 //=================================================================================
401 // function : enterEvent()
402 // purpose  :
403 //=================================================================================
404 void TransformationGUI_MultiRotationDlg::enterEvent(QEvent* e)
405 {
406   if(!GroupConstructors->isEnabled())
407     ActivateThisDialog();
408 }
409
410
411 //=================================================================================
412 // function : ValueChangedInSpinBox()
413 // purpose  :
414 //=================================================================================
415 void TransformationGUI_MultiRotationDlg::ValueChangedInSpinBox(double newValue)
416 {
417   QObject* send = (QObject*)sender();
418
419   if(send == GroupPoints->SpinBox_DX || send == GroupDimensions->SpinBox_DY1)
420     myNbTimes1 = (int)newValue;
421   else if(send == GroupDimensions->SpinBox_DX1)
422     myAng = newValue;
423   else if(send == GroupDimensions->SpinBox_DX2)
424     myStep = newValue;
425   else if(send == GroupDimensions->SpinBox_DY2)
426     myNbTimes2 = (int)newValue;
427
428   displayPreview();
429 }
430
431
432 //=================================================================================
433 // function : ReverseAngle()
434 // purpose  : 'state' not used here
435 //=================================================================================
436 void TransformationGUI_MultiRotationDlg::ReverseAngle()
437 {
438   myAng = -myAng;
439
440   int aConstructorId = getConstructorId();
441
442   if(aConstructorId == 0)
443     GroupPoints->SpinBox_DX->SetValue(myAng);
444   else if(aConstructorId == 1)
445     GroupDimensions->SpinBox_DX1->SetValue(myAng);
446
447   displayPreview();
448 }
449
450
451 //=================================================================================
452 // function : createOperation
453 // purpose  :
454 //=================================================================================
455 GEOM::GEOM_IOperations_ptr TransformationGUI_MultiRotationDlg::createOperation()
456 {
457   return myGeomGUI->GetGeomGen()->GetITransformOperations( getStudyId() );
458 }
459
460
461 //=================================================================================
462 // function : isValid
463 // purpose  :
464 //=================================================================================
465 bool TransformationGUI_MultiRotationDlg::isValid( QString& msg )
466 {
467   return !(myBase->_is_nil() || myVector->_is_nil());
468 }
469
470 //=================================================================================
471 // function : execute
472 // purpose  :
473 //=================================================================================
474 bool TransformationGUI_MultiRotationDlg::execute( ObjectList& objects )
475 {
476   bool res = false;
477
478   GEOM::GEOM_Object_var anObj;
479
480   switch ( getConstructorId() )
481     {
482     case 0 :
483       {
484         if ( !CORBA::is_nil( myBase ) && !CORBA::is_nil( myVector ) ) {
485           anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
486             MultiRotate1D( myBase, myVector, myNbTimes1 );
487           res = true;
488         }
489         break;
490       }
491     case 1 :
492       {
493         if ( !CORBA::is_nil( myBase ) && !CORBA::is_nil( myVector ) )
494           {
495             anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
496               MultiRotate2D( myBase, myVector, myAng, myNbTimes1, myStep, myNbTimes2 );
497             res = true;
498           }
499         break;
500       }
501     }
502
503   if ( !anObj->_is_nil() )
504     objects.push_back( anObj._retn() );
505
506   return res;
507 }
508
509
510 //=================================================================================
511 // function : closeEvent
512 // purpose  :
513 //=================================================================================
514 void  TransformationGUI_MultiRotationDlg::closeEvent( QCloseEvent* e )
515 {
516   // myGeomGUI->SetState( -1 );
517   GEOMBase_Skeleton::closeEvent( e );
518 }
519
520 //=================================================================================
521 // function : addSubshapeToStudy
522 // purpose  : virtual method to add new SubObjects if local selection
523 //=================================================================================
524 void TransformationGUI_MultiRotationDlg::addSubshapesToStudy()
525 {
526   QMap<QString, GEOM::GEOM_Object_var> objMap;
527   
528   switch (getConstructorId())
529     {
530     case 0:
531       objMap[GroupPoints->LineEdit2->text()] = myVector;
532       break;
533     case 1:
534       objMap[GroupDimensions->LineEdit2->text()] = myVector;
535       break;
536     }
537   addSubshapesToFather( objMap );
538 }