]> SALOME platform Git repositories - modules/geom.git/blob - src/TransformationGUI/TransformationGUI_ScaleDlg.cxx
Salome HOME
Preparation of intermediate revision
[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 <SalomeApp_Notebook.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(true);
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, DBL_DIGITS_DISPLAY);
160   initSpinBox(SpinBox_FY, COORD_MIN, COORD_MAX, SpecificStep, DBL_DIGITS_DISPLAY);
161   initSpinBox(SpinBox_FZ, COORD_MIN, COORD_MAX, SpecificStep, DBL_DIGITS_DISPLAY);
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   myPoint = GEOM::GEOM_Object::_nil();
171
172   // Signals and slots connections
173   connect(buttonOk(),        SIGNAL(clicked()), this, SLOT(ClickOnOk()));
174   connect(buttonApply(),     SIGNAL(clicked()), this, SLOT(ClickOnApply()));
175   connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
176
177   connect(PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
178   connect(PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
179
180   connect(LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
181   connect(LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
182
183   connect(SpinBox_FX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
184   connect(SpinBox_FY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
185   connect(SpinBox_FZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
186
187   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
188
189   connect(CheckBoxCopy, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
190
191   initName(tr("GEOM_SCALE"));
192
193   ConstructorsClicked(0);
194 }
195
196 //=================================================================================
197 // function : ConstructorsClicked()
198 // purpose  : Radio button management
199 //=================================================================================
200 void TransformationGUI_ScaleDlg::ConstructorsClicked (int constructorId)
201 {
202   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
203
204   switch (constructorId) {
205   case 0: // translation an object by dx, dy, dz
206     TextLabel3->setText(tr("GEOM_SCALE_FACTOR"));
207     TextLabel4->hide();
208     TextLabel5->hide();
209     SpinBox_FY->hide();
210     SpinBox_FZ->hide();
211
212     PushButton1->click();
213     break;
214   case 1: // translation an object by 2 points
215     TextLabel3->setText(tr("GEOM_SCALE_FACTOR_X"));
216     TextLabel4->show();
217     TextLabel5->show();
218     SpinBox_FY->show();
219     SpinBox_FZ->show();
220
221     PushButton1->click();
222     break;
223   default:
224     break;
225   }
226
227   qApp->processEvents();
228   updateGeometry();
229   resize(minimumSizeHint());
230
231   if (myInitial) {
232     myInitial = false;
233     SelectionIntoArgument();
234   }
235   else {
236     displayPreview();
237   }
238 }
239
240 //=================================================================================
241 // function : ClickOnOk()
242 // purpose  :
243 //=================================================================================
244 void TransformationGUI_ScaleDlg::ClickOnOk()
245 {
246   if (ClickOnApply())
247     ClickOnCancel();
248 }
249
250 //=================================================================================
251 // function : ClickOnApply()
252 // purpose  :
253 //=================================================================================
254 bool TransformationGUI_ScaleDlg::ClickOnApply()
255 {
256   if (!onAccept(CheckBoxCopy->isChecked()))
257     return false;
258
259   initName(tr("GEOM_SCALE"));
260   // activate selection and connect selection manager
261   ConstructorsClicked(getConstructorId());
262   return true;
263 }
264
265 //=================================================================================
266 // function : SelectionIntoArgument()
267 // purpose  : Called when selection is changed or on dialog initialization or activation
268 //=================================================================================
269 void TransformationGUI_ScaleDlg::SelectionIntoArgument()
270 {
271   erasePreview();
272   myEditCurrentArgument->setText("");
273
274   if (myEditCurrentArgument == LineEdit1)
275     myObjects.length(0);
276   else if (myEditCurrentArgument == LineEdit2)
277     myPoint = GEOM::GEOM_Object::_nil();
278
279   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
280   SALOME_ListIO aSelList;
281   aSelMgr->selectedObjects(aSelList);
282
283   QString aName;
284
285   if (myEditCurrentArgument == LineEdit1)
286   {
287     int aNbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aName);
288     if (aNbSel < 1)
289       return;
290
291     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myObjects);
292     if (!myObjects.length())
293       return;
294     else
295       myEditCurrentArgument->setText(aName);
296   }
297   else if (myEditCurrentArgument == LineEdit2)
298   {
299     GEOM::GEOM_Object_var aSelectedObject = GEOM::GEOM_Object::_nil();
300     if (aSelList.Extent() == 1)
301     {
302       Standard_Boolean testResult = Standard_False;
303       aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
304       if (testResult)
305       {
306         aName = GEOMBase::GetName(aSelectedObject);
307
308         TopoDS_Shape aShape;
309         if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
310         {
311           TColStd_IndexedMapOfInteger aMap;
312           aSelMgr->GetIndexes(aSelList.First(), aMap);
313           if (aMap.Extent() == 1)
314           {
315             int anIndex = aMap(1);
316             aName += QString(":vertex_%1").arg(anIndex);
317
318             //Find SubShape Object in Father
319             GEOM::GEOM_Object_var aFindedObject = findObjectInFather(aSelectedObject, aName);
320
321             if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
322               GEOM::GEOM_IShapesOperations_var aShapesOp =
323                 getGeomEngine()->GetIShapesOperations(getStudyId());
324               aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
325             }
326             else
327               aSelectedObject = aFindedObject; // get Object from study
328           }
329           else // Global Selection
330           {
331             if (aShape.ShapeType() != TopAbs_VERTEX) {
332               aSelectedObject = GEOM::GEOM_Object::_nil();
333               aName = "";
334             }
335           }
336         }
337       }
338     }
339
340     myPoint = aSelectedObject;
341     myEditCurrentArgument->setText(aName);
342
343     if (!myPoint->_is_nil() && !myObjects.length())
344       PushButton1->click();
345   }
346
347   // clear selection
348   /*  disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
349   myGeomGUI->getApp()->selectionMgr()->clearSelected();
350   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
351   this, SLOT(SelectionIntoArgument()));*/
352   // here commented, because multiple objects can be selected IPAL 21437
353
354   displayPreview();
355 }
356
357 //=================================================================================
358 // function : SetEditCurrentArgument()
359 // purpose  :
360 //=================================================================================
361 void TransformationGUI_ScaleDlg::SetEditCurrentArgument()
362 {
363   QPushButton* send = (QPushButton*)sender();
364
365   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
366   globalSelection();
367
368   if (send == PushButton1) {
369     myEditCurrentArgument = LineEdit1;
370
371     PushButton2->setDown(false);
372     LineEdit2->setEnabled(false);
373   }
374   else if (send == PushButton2) {
375     myEditCurrentArgument = LineEdit2;
376
377     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
378
379     PushButton1->setDown(false);
380     LineEdit1->setEnabled(false);
381   }
382   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
383           this, SLOT(SelectionIntoArgument()));
384
385   // enable line edit
386   myEditCurrentArgument->setEnabled(true);
387   myEditCurrentArgument->setFocus();
388   // after setFocus(), because it will be setDown(false) when loses focus
389   send->setDown(true);
390
391   // seems we need it only to avoid preview disappearing, caused by selection mode change
392   displayPreview();
393 }
394
395 //=================================================================================
396 // function : LineEditReturnPressed()
397 // purpose  :
398 //=================================================================================
399 void TransformationGUI_ScaleDlg::LineEditReturnPressed()
400 {
401   QLineEdit* send = (QLineEdit*)sender();
402   if (send == LineEdit1 || send == LineEdit2)
403   {
404     myEditCurrentArgument = send;
405     GEOMBase_Skeleton::LineEditReturnPressed();
406   }
407 }
408
409 //=================================================================================
410 // function : ActivateThisDialog()
411 // purpose  :
412 //=================================================================================
413 void TransformationGUI_ScaleDlg::ActivateThisDialog()
414 {
415   GEOMBase_Skeleton::ActivateThisDialog();
416   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
417            this, SLOT( SelectionIntoArgument() ) );
418
419   ConstructorsClicked( getConstructorId() );
420 }
421
422 //=================================================================================
423 // function : enterEvent()
424 // purpose  :
425 //=================================================================================
426 void TransformationGUI_ScaleDlg::enterEvent (QEvent*)
427 {
428   if (!mainFrame()->GroupConstructors->isEnabled())
429     ActivateThisDialog();
430 }
431
432 //=================================================================================
433 // function : ValueChangedInSpinBox()
434 // purpose  :
435 //=================================================================================
436 void TransformationGUI_ScaleDlg::ValueChangedInSpinBox()
437 {
438   displayPreview();
439 }
440
441 //=================================================================================
442 // function : SetDoubleSpinBoxStep()
443 // purpose  : Double spin box management
444 //=================================================================================
445 void TransformationGUI_ScaleDlg::SetDoubleSpinBoxStep(double step)
446 {
447   SpinBox_FX->setSingleStep(step);
448   SpinBox_FY->setSingleStep(step);
449   SpinBox_FZ->setSingleStep(step);
450 }
451
452 //=================================================================================
453 // function : createOperation
454 // purpose  :
455 //=================================================================================
456 GEOM::GEOM_IOperations_ptr TransformationGUI_ScaleDlg::createOperation()
457 {
458   return getGeomEngine()->GetITransformOperations(getStudyId());
459 }
460
461 //=================================================================================
462 // function : isValid
463 // purpose  :
464 //=================================================================================
465 bool TransformationGUI_ScaleDlg::isValid (QString& msg)
466 {
467   // && !myPoint->_is_nil()
468   if (getConstructorId() == 0) {
469     bool ok = SpinBox_FX->isValid( msg, !IsPreview() );
470     return myObjects.length() > 0 && fabs(SpinBox_FX->value()) > 0.00001 && ok;
471   }
472
473   bool ok = true;
474   ok = SpinBox_FX->isValid( msg, !IsPreview() ) && ok;
475   ok = SpinBox_FY->isValid( msg, !IsPreview() ) && ok;
476   ok = SpinBox_FZ->isValid( msg, !IsPreview() ) && ok;
477   return myObjects.length() > 0 &&
478     fabs(SpinBox_FX->value()) > 0.00001 &&
479     fabs(SpinBox_FY->value()) > 0.00001 &&
480     fabs(SpinBox_FZ->value()) > 0.00001 && ok;
481 }
482
483 //=================================================================================
484 // function : execute
485 // purpose  :
486 //=================================================================================
487 bool TransformationGUI_ScaleDlg::execute (ObjectList& objects)
488 {
489   bool toCreateCopy = IsPreview() || CheckBoxCopy->isChecked();
490
491   GEOM::GEOM_Object_var anObj;
492
493   GEOM::GEOM_ITransformOperations_var anOper = GEOM::GEOM_ITransformOperations::_narrow(getOperation());
494
495   switch (getConstructorId())
496   {
497   case 0:
498     {
499       if (toCreateCopy)
500       {
501         for (int i = 0; i < myObjects.length(); i++)
502         {
503           anObj = anOper->ScaleShapeCopy(myObjects[i], myPoint, SpinBox_FX->value());
504           if (!anObj->_is_nil()) {
505             if(!IsPreview()) 
506               myNoteBook->setParameters(anObj, 1, SpinBox_FX);
507             objects.push_back(anObj._retn());
508           }
509         }
510       }
511       else
512       {
513         for (int i = 0; i < myObjects.length(); i++)
514         {
515           anObj = anOper->ScaleShape(myObjects[i], myPoint, SpinBox_FX->value());
516           if (!anObj->_is_nil())
517             objects.push_back(anObj._retn());
518         }
519       }
520     }
521     break;
522   case 1:
523     {
524       if (toCreateCopy)
525       {
526         for (int i = 0; i < myObjects.length(); i++)
527         {
528           anObj = anOper->ScaleShapeAlongAxesCopy(myObjects[i], myPoint, SpinBox_FX->value(),
529                                                   SpinBox_FY->value(), SpinBox_FZ->value());
530           if (!anObj->_is_nil())
531             if(!IsPreview()) {
532               myNoteBook->setParameters(anObj, 3, SpinBox_FX, SpinBox_FY, SpinBox_FZ);
533             }
534             objects.push_back(anObj._retn());
535         }
536       }
537       else
538       {
539         for (int i = 0; i < myObjects.length(); i++)
540         {
541           anObj = anOper->ScaleShapeAlongAxes(myObjects[i], myPoint, SpinBox_FX->value(),
542                                               SpinBox_FY->value(), SpinBox_FZ->value());
543           if (!anObj->_is_nil())
544             objects.push_back(anObj._retn());
545         }
546       }
547     }
548     break;
549   default:
550     break;
551   }
552
553   return true;
554 }
555
556 //=================================================================================
557 // function : restoreSubShapes
558 // purpose  :
559 //=================================================================================
560 void TransformationGUI_ScaleDlg::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
561                                                    SALOMEDS::SObject_ptr theSObject)
562 {
563   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
564     // empty list of arguments means that all arguments should be restored
565     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
566                                         /*theFindMethod=*/GEOM::FSM_Transformed,
567                                         /*theInheritFirstArg=*/true);
568   }
569 }
570
571 //=================================================================================
572 // function :  CreateCopyModeChanged()
573 // purpose  :
574 //=================================================================================
575 void TransformationGUI_ScaleDlg::CreateCopyModeChanged (bool isCreateCopy)
576 {
577   mainFrame()->GroupBoxName->setEnabled(isCreateCopy);
578 }
579
580 //=================================================================================
581 // function : addSubshapesToStudy
582 // purpose  : virtual method to add new SubObjects if local selection
583 //=================================================================================
584 void TransformationGUI_ScaleDlg::addSubshapesToStudy()
585 {
586   bool toCreateCopy = IsPreview() || CheckBoxCopy->isChecked();
587   if (toCreateCopy)
588   {
589     if (!myPoint->_is_nil())
590     {
591       QMap<QString, GEOM::GEOM_Object_var> objMap;
592       objMap[LineEdit2->text()] = myPoint;
593       addSubshapesToFather(objMap);
594     }
595   }
596 }