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