Salome HOME
IMP 0020670: Be able to keep the same names for the 'Set ... subshapes arguments...
[modules/geom.git] / src / TransformationGUI / TransformationGUI_ScaleDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : TransformationGUI_ScaleDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25 //
26 #include "TransformationGUI_ScaleDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SalomeApp_DoubleSpinBox.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 // OCCT Includes
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS.hxx>
41 #include <TopExp.hxx>
42 #include <TColStd_IndexedMapOfInteger.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
44
45 #include <GEOMImpl_Types.hxx>
46
47 //=================================================================================
48 // class    : TransformationGUI_ScaleDlg()
49 // purpose  : Constructs a TransformationGUI_ScaleDlg which is a child of 'parent', with the
50 //            name 'name' and widget flags set to 'f'.
51 //            The dialog will by default be modeless, unless you set 'modal' to
52 //            TRUE to construct a modal dialog.
53 //=================================================================================
54 TransformationGUI_ScaleDlg::TransformationGUI_ScaleDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
55                                                         bool modal, Qt::WindowFlags fl)
56   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
57     myInitial(true)
58 {
59   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
60   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_SCALE")));
61   QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_SCALE_ALONG_AXES")));
62   QPixmap image3 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
63
64   setWindowTitle(tr("GEOM_SCALE_TITLE"));
65
66   // Constructors
67   mainFrame()->GroupConstructors->setTitle(tr("GEOM_SCALE"));
68   mainFrame()->RadioButton1->setIcon(image1);
69   mainFrame()->RadioButton2->setIcon(image2);
70   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
71   mainFrame()->RadioButton3->close();
72
73   // Own widgets
74   GroupBox1 = new QGroupBox(tr("GEOM_ARGUMENTS"), this);
75   QGridLayout* OwnLayout = new QGridLayout(GroupBox1);
76   OwnLayout->setSpacing(6);
77   OwnLayout->setMargin(11);
78
79   TextLabel1 = new QLabel(tr("GEOM_OBJECTS"), GroupBox1);
80   PushButton1 = new QPushButton(GroupBox1);
81   PushButton1->setIcon(image3);
82   PushButton1->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
83   LineEdit1 = new QLineEdit(GroupBox1);
84   LineEdit1->setReadOnly(true);
85   LineEdit1->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
86
87   TextLabel2 = new QLabel(tr("GEOM_CENTRAL_POINT"), GroupBox1);
88   PushButton2 = new QPushButton(GroupBox1);
89   PushButton2->setIcon(image3);
90   PushButton2->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
91   LineEdit2 = new QLineEdit(GroupBox1);
92   LineEdit2->setReadOnly(true);
93   LineEdit2->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
94
95   TextLabel3 = new QLabel(tr("GEOM_SCALE_FACTOR"), GroupBox1);
96   SpinBox_FX = new SalomeApp_DoubleSpinBox(GroupBox1);
97
98   TextLabel4 = new QLabel(tr("GEOM_SCALE_FACTOR_Y"), GroupBox1);
99   SpinBox_FY = new SalomeApp_DoubleSpinBox(GroupBox1);
100
101   TextLabel5 = new QLabel(tr("GEOM_SCALE_FACTOR_Z"), GroupBox1);
102   SpinBox_FZ = new SalomeApp_DoubleSpinBox(GroupBox1);
103
104   CheckBoxCopy = new QCheckBox(tr("GEOM_CREATE_COPY"), GroupBox1);
105   CheckBoxCopy->setChecked(true);
106
107   // Layouting
108   OwnLayout->addWidget(TextLabel1,   0, 0);
109   OwnLayout->addWidget(PushButton1,  0, 1);
110   OwnLayout->addWidget(LineEdit1,    0, 2, 1, 2);
111   OwnLayout->addWidget(TextLabel2,   1, 0);
112   OwnLayout->addWidget(PushButton2,  1, 1);
113   OwnLayout->addWidget(LineEdit2,    1, 2, 1, 2);
114   OwnLayout->addWidget(TextLabel3,   2, 0);
115   OwnLayout->addWidget(SpinBox_FX,   2, 2);
116   OwnLayout->addWidget(TextLabel4,   3, 0);
117   OwnLayout->addWidget(SpinBox_FY,   3, 2);
118   OwnLayout->addWidget(TextLabel5,   4, 0);
119   OwnLayout->addWidget(SpinBox_FZ,   4, 2);
120   OwnLayout->addWidget(CheckBoxCopy, 5, 0, 1, 4);
121
122   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
123   layout->setMargin(0); layout->setSpacing(6);
124   layout->addWidget(GroupBox1);
125
126   setHelpFileName("scale_operation_page.html");
127
128   // Modification of an existing object by offset is not allowed
129   CheckBoxCopy->hide();
130
131   // Activate Create a Copy mode
132   CreateCopyModeChanged(true);
133
134   // Allowed inheritance of children and visual properties by the scaling result
135   mainFrame()->GroupBoxPublish->show();
136
137   Init();
138 }
139
140 //=================================================================================
141 // function : ~TransformationGUI_ScaleDlg()
142 // purpose  : Destroys the object and frees any allocated resources
143 //=================================================================================
144 TransformationGUI_ScaleDlg::~TransformationGUI_ScaleDlg()
145 {
146 }
147
148 //=================================================================================
149 // function : Init()
150 // purpose  :
151 //=================================================================================
152 void TransformationGUI_ScaleDlg::Init()
153 {
154   // Min, max, step and decimals for spin boxes & initial values
155   double aFactor = 2.0;
156   double SpecificStep = 0.5;
157
158   initSpinBox(SpinBox_FX, COORD_MIN, COORD_MAX, SpecificStep, DBL_DIGITS_DISPLAY);
159   initSpinBox(SpinBox_FY, COORD_MIN, COORD_MAX, SpecificStep, DBL_DIGITS_DISPLAY);
160   initSpinBox(SpinBox_FZ, COORD_MIN, COORD_MAX, SpecificStep, DBL_DIGITS_DISPLAY);
161   SpinBox_FX->setValue(aFactor);
162   SpinBox_FY->setValue(aFactor);
163   SpinBox_FZ->setValue(aFactor);
164
165   // init variables
166   LineEdit1->setText("");
167   LineEdit2->setText("");
168
169   myPoint = GEOM::GEOM_Object::_nil();
170
171   // Signals and slots connections
172   connect(buttonOk(),        SIGNAL(clicked()), this, SLOT(ClickOnOk()));
173   connect(buttonApply(),     SIGNAL(clicked()), this, SLOT(ClickOnApply()));
174   connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
175
176   connect(PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
177   connect(PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
178
179   connect(LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
180   connect(LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
181
182   connect(SpinBox_FX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
183   connect(SpinBox_FY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
184   connect(SpinBox_FZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
185
186   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
187
188   connect(CheckBoxCopy, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
189
190   initName(tr("GEOM_SCALE"));
191
192   ConstructorsClicked(0);
193 }
194
195 //=================================================================================
196 // function : ConstructorsClicked()
197 // purpose  : Radio button management
198 //=================================================================================
199 void TransformationGUI_ScaleDlg::ConstructorsClicked (int constructorId)
200 {
201   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
202
203   switch (constructorId) {
204   case 0: // translation an object by dx, dy, dz
205     TextLabel3->setText(tr("GEOM_SCALE_FACTOR"));
206     TextLabel4->hide();
207     TextLabel5->hide();
208     SpinBox_FY->hide();
209     SpinBox_FZ->hide();
210
211     PushButton1->click();
212     break;
213   case 1: // translation an object by 2 points
214     TextLabel3->setText(tr("GEOM_SCALE_FACTOR_X"));
215     TextLabel4->show();
216     TextLabel5->show();
217     SpinBox_FY->show();
218     SpinBox_FZ->show();
219
220     PushButton1->click();
221     break;
222   default:
223     break;
224   }
225
226   qApp->processEvents();
227   updateGeometry();
228   resize(minimumSizeHint());
229
230   if (myInitial) {
231     myInitial = false;
232     SelectionIntoArgument();
233   }
234   else {
235     displayPreview();
236   }
237 }
238
239 //=================================================================================
240 // function : ClickOnOk()
241 // purpose  :
242 //=================================================================================
243 void TransformationGUI_ScaleDlg::ClickOnOk()
244 {
245   if (ClickOnApply())
246     ClickOnCancel();
247 }
248
249 //=================================================================================
250 // function : ClickOnApply()
251 // purpose  :
252 //=================================================================================
253 bool TransformationGUI_ScaleDlg::ClickOnApply()
254 {
255   if (!onAccept(CheckBoxCopy->isChecked()))
256     return false;
257
258   initName(tr("GEOM_SCALE"));
259   // activate selection and connect selection manager
260   ConstructorsClicked(getConstructorId());
261   return true;
262 }
263
264 //=================================================================================
265 // function : SelectionIntoArgument()
266 // purpose  : Called when selection is changed or on dialog initialization or activation
267 //=================================================================================
268 void TransformationGUI_ScaleDlg::SelectionIntoArgument()
269 {
270   erasePreview();
271   myEditCurrentArgument->setText("");
272
273   if (myEditCurrentArgument == LineEdit1)
274     myObjects.length(0);
275   else if (myEditCurrentArgument == LineEdit2)
276     myPoint = GEOM::GEOM_Object::_nil();
277
278   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
279   SALOME_ListIO aSelList;
280   aSelMgr->selectedObjects(aSelList);
281
282   QString aName;
283
284   if (myEditCurrentArgument == LineEdit1)
285   {
286     int aNbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aName);
287     if (aNbSel < 1)
288       return;
289
290     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myObjects);
291     if (!myObjects.length())
292       return;
293     else
294       myEditCurrentArgument->setText(aName);
295   }
296   else if (myEditCurrentArgument == LineEdit2)
297   {
298     GEOM::GEOM_Object_var aSelectedObject = GEOM::GEOM_Object::_nil();
299     if (aSelList.Extent() == 1)
300     {
301       Standard_Boolean testResult = Standard_False;
302       aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
303       if (testResult)
304       {
305         aName = GEOMBase::GetName(aSelectedObject);
306
307         TopoDS_Shape aShape;
308         if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
309         {
310           TColStd_IndexedMapOfInteger aMap;
311           aSelMgr->GetIndexes(aSelList.First(), aMap);
312           if (aMap.Extent() == 1)
313           {
314             int anIndex = aMap(1);
315             aName += QString(":vertex_%1").arg(anIndex);
316
317             //Find SubShape Object in Father
318             GEOM::GEOM_Object_var aFindedObject = findObjectInFather(aSelectedObject, aName);
319
320             if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
321               GEOM::GEOM_IShapesOperations_var aShapesOp =
322                 getGeomEngine()->GetIShapesOperations(getStudyId());
323               aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
324             }
325             else
326               aSelectedObject = aFindedObject; // get Object from study
327           }
328           else // Global Selection
329           {
330             if (aShape.ShapeType() != TopAbs_VERTEX) {
331               aSelectedObject = GEOM::GEOM_Object::_nil();
332               aName = "";
333             }
334           }
335         }
336       }
337     }
338
339     myPoint = aSelectedObject;
340     myEditCurrentArgument->setText(aName);
341
342     if (!myPoint->_is_nil() && !myObjects.length())
343       PushButton1->click();
344   }
345
346   // clear selection
347   /*  disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
348   myGeomGUI->getApp()->selectionMgr()->clearSelected();
349   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
350   this, SLOT(SelectionIntoArgument()));*/
351   // here commented, because multiple objects can be selected IPAL 21437
352
353   displayPreview();
354 }
355
356 //=================================================================================
357 // function : SetEditCurrentArgument()
358 // purpose  :
359 //=================================================================================
360 void TransformationGUI_ScaleDlg::SetEditCurrentArgument()
361 {
362   QPushButton* send = (QPushButton*)sender();
363
364   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
365   globalSelection();
366
367   if (send == PushButton1) {
368     myEditCurrentArgument = LineEdit1;
369
370     PushButton2->setDown(false);
371     LineEdit2->setEnabled(false);
372   }
373   else if (send == PushButton2) {
374     myEditCurrentArgument = LineEdit2;
375
376     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
377
378     PushButton1->setDown(false);
379     LineEdit1->setEnabled(false);
380   }
381   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
382           this, SLOT(SelectionIntoArgument()));
383
384   // enable line edit
385   myEditCurrentArgument->setEnabled(true);
386   myEditCurrentArgument->setFocus();
387   // after setFocus(), because it will be setDown(false) when loses focus
388   send->setDown(true);
389
390   // seems we need it only to avoid preview disappearing, caused by selection mode change
391   displayPreview();
392 }
393
394 //=================================================================================
395 // function : LineEditReturnPressed()
396 // purpose  :
397 //=================================================================================
398 void TransformationGUI_ScaleDlg::LineEditReturnPressed()
399 {
400   QLineEdit* send = (QLineEdit*)sender();
401   if (send == LineEdit1 || send == LineEdit2)
402   {
403     myEditCurrentArgument = send;
404     GEOMBase_Skeleton::LineEditReturnPressed();
405   }
406 }
407
408 //=================================================================================
409 // function : ActivateThisDialog()
410 // purpose  :
411 //=================================================================================
412 void TransformationGUI_ScaleDlg::ActivateThisDialog()
413 {
414   GEOMBase_Skeleton::ActivateThisDialog();
415   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
416            this, SLOT( SelectionIntoArgument() ) );
417
418   ConstructorsClicked( getConstructorId() );
419 }
420
421 //=================================================================================
422 // function : enterEvent()
423 // purpose  :
424 //=================================================================================
425 void TransformationGUI_ScaleDlg::enterEvent (QEvent*)
426 {
427   if (!mainFrame()->GroupConstructors->isEnabled())
428     ActivateThisDialog();
429 }
430
431 //=================================================================================
432 // function : ValueChangedInSpinBox()
433 // purpose  :
434 //=================================================================================
435 void TransformationGUI_ScaleDlg::ValueChangedInSpinBox()
436 {
437   displayPreview();
438 }
439
440 //=================================================================================
441 // function : SetDoubleSpinBoxStep()
442 // purpose  : Double spin box management
443 //=================================================================================
444 void TransformationGUI_ScaleDlg::SetDoubleSpinBoxStep(double step)
445 {
446   SpinBox_FX->setSingleStep(step);
447   SpinBox_FY->setSingleStep(step);
448   SpinBox_FZ->setSingleStep(step);
449 }
450
451 //=================================================================================
452 // function : createOperation
453 // purpose  :
454 //=================================================================================
455 GEOM::GEOM_IOperations_ptr TransformationGUI_ScaleDlg::createOperation()
456 {
457   return getGeomEngine()->GetITransformOperations(getStudyId());
458 }
459
460 //=================================================================================
461 // function : isValid
462 // purpose  :
463 //=================================================================================
464 bool TransformationGUI_ScaleDlg::isValid (QString& msg)
465 {
466   // && !myPoint->_is_nil()
467   if (getConstructorId() == 0) {
468     bool ok = SpinBox_FX->isValid( msg, !IsPreview() );
469     return myObjects.length() > 0 && fabs(SpinBox_FX->value()) > 0.00001 && ok;
470   }
471
472   bool ok = true;
473   ok = SpinBox_FX->isValid( msg, !IsPreview() ) && ok;
474   ok = SpinBox_FY->isValid( msg, !IsPreview() ) && ok;
475   ok = SpinBox_FZ->isValid( msg, !IsPreview() ) && ok;
476   return myObjects.length() > 0 &&
477     fabs(SpinBox_FX->value()) > 0.00001 &&
478     fabs(SpinBox_FY->value()) > 0.00001 &&
479     fabs(SpinBox_FZ->value()) > 0.00001 && ok;
480 }
481
482 //=================================================================================
483 // function : execute
484 // purpose  :
485 //=================================================================================
486 bool TransformationGUI_ScaleDlg::execute (ObjectList& objects)
487 {
488   bool toCreateCopy = IsPreview() || CheckBoxCopy->isChecked();
489
490   GEOM::GEOM_Object_var anObj;
491
492   GEOM::GEOM_ITransformOperations_var anOper = GEOM::GEOM_ITransformOperations::_narrow(getOperation());
493
494   switch (getConstructorId())
495   {
496   case 0:
497     {
498       if (toCreateCopy)
499       {
500         for (int i = 0; i < myObjects.length(); i++)
501         {
502           anObj = anOper->ScaleShapeCopy(myObjects[i], myPoint, SpinBox_FX->value());
503           if (!anObj->_is_nil()) {
504             if(!IsPreview()) 
505               anObj->SetParameters(SpinBox_FX->text().toLatin1().constData());
506             objects.push_back(anObj._retn());
507           }
508         }
509       }
510       else
511       {
512         for (int i = 0; i < myObjects.length(); i++)
513         {
514           anObj = anOper->ScaleShape(myObjects[i], myPoint, SpinBox_FX->value());
515           if (!anObj->_is_nil())
516             objects.push_back(anObj._retn());
517         }
518       }
519     }
520     break;
521   case 1:
522     {
523       if (toCreateCopy)
524       {
525         for (int i = 0; i < myObjects.length(); i++)
526         {
527           anObj = anOper->ScaleShapeAlongAxesCopy(myObjects[i], myPoint, SpinBox_FX->value(),
528                                                   SpinBox_FY->value(), SpinBox_FZ->value());
529           if (!anObj->_is_nil())
530             if(!IsPreview()) {
531               QStringList aParameters;
532               aParameters<<SpinBox_FX->text();
533               aParameters<<SpinBox_FY->text();
534               aParameters<<SpinBox_FZ->text();
535               anObj->SetParameters(aParameters.join(":").toLatin1().constData());
536             }
537             objects.push_back(anObj._retn());
538         }
539       }
540       else
541       {
542         for (int i = 0; i < myObjects.length(); i++)
543         {
544           anObj = anOper->ScaleShapeAlongAxes(myObjects[i], myPoint, SpinBox_FX->value(),
545                                               SpinBox_FY->value(), SpinBox_FZ->value());
546           if (!anObj->_is_nil())
547             objects.push_back(anObj._retn());
548         }
549       }
550     }
551     break;
552   default:
553     break;
554   }
555
556   return true;
557 }
558
559 //=================================================================================
560 // function : restoreSubShapes
561 // purpose  :
562 //=================================================================================
563 void TransformationGUI_ScaleDlg::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
564                                                    SALOMEDS::SObject_ptr theSObject)
565 {
566   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
567     // empty list of arguments means that all arguments should be restored
568     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
569                                         /*theFindMethod=*/GEOM::FSM_Transformed,
570                                         /*theInheritFirstArg=*/true,
571                                         mainFrame()->CheckBoxAddPrefix->isChecked());
572   }
573 }
574
575 //=================================================================================
576 // function :  CreateCopyModeChanged()
577 // purpose  :
578 //=================================================================================
579 void TransformationGUI_ScaleDlg::CreateCopyModeChanged (bool isCreateCopy)
580 {
581   mainFrame()->GroupBoxName->setEnabled(isCreateCopy);
582 }
583
584 //=================================================================================
585 // function : addSubshapesToStudy
586 // purpose  : virtual method to add new SubObjects if local selection
587 //=================================================================================
588 void TransformationGUI_ScaleDlg::addSubshapesToStudy()
589 {
590   bool toCreateCopy = IsPreview() || CheckBoxCopy->isChecked();
591   if (toCreateCopy)
592   {
593     if (!myPoint->_is_nil())
594     {
595       QMap<QString, GEOM::GEOM_Object_var> objMap;
596       objMap[LineEdit2->text()] = myPoint;
597       addSubshapesToFather(objMap);
598     }
599   }
600 }