]> SALOME platform Git repositories - modules/geom.git/blob - src/TransformationGUI/TransformationGUI_ScaleDlg.cxx
Salome HOME
Fix for bug IPAL18899 : 4x REGRESS: Dump python on the study leads to fatal error...
[modules/geom.git] / src / TransformationGUI / TransformationGUI_ScaleDlg.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_ScaleDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "TransformationGUI_ScaleDlg.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.hxx>
38 #include <TopExp.hxx>
39 #include <TColStd_IndexedMapOfInteger.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
41
42 #include <qlabel.h>
43 #include <qcheckbox.h>
44
45 #include "GEOMImpl_Types.hxx"
46
47 #include "utilities.h"
48
49 using namespace std;
50
51 //=================================================================================
52 // class    : TransformationGUI_ScaleDlg()
53 // purpose  : Constructs a TransformationGUI_ScaleDlg which is a child of 'parent', with the
54 //            name 'name' and widget flags set to 'f'.
55 //            The dialog will by default be modeless, unless you set 'modal' to
56 //            TRUE to construct a modal dialog.
57 //=================================================================================
58 TransformationGUI_ScaleDlg::TransformationGUI_ScaleDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
59                                                        const char* name, bool modal, WFlags fl)
60   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
61                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
62 {
63   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_SCALE")));
64   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
65
66   setCaption(tr("GEOM_SCALE_TITLE"));
67
68   /***************************************************************/
69   GroupConstructors->setTitle(tr("GEOM_SCALE"));
70   RadioButton1->setPixmap(image0);
71   RadioButton2->close(TRUE);
72   RadioButton3->close(TRUE);
73
74   GroupPoints = new DlgRef_2Sel1Spin2Check(this, "GroupPoints");
75   GroupPoints->CheckButton2->hide();
76   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
77   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
78   GroupPoints->TextLabel2->setText(tr("GEOM_CENTRAL_POINT"));
79   GroupPoints->TextLabel3->setText(tr("GEOM_SCALE_FACTOR"));
80   GroupPoints->LineEdit1->setReadOnly( true );
81   GroupPoints->LineEdit2->setReadOnly( true );
82   GroupPoints->PushButton1->setPixmap(image1);
83   GroupPoints->PushButton2->setPixmap(image1);
84   GroupPoints->CheckButton1->setText(tr("GEOM_CREATE_COPY"));
85
86   // san -- modification of an exisitng object by offset is not allowed
87   GroupPoints->CheckButton1->hide();
88
89   Layout1->addWidget(GroupPoints, 2, 0);
90   /***************************************************************/
91   double aFactor = 2.0;
92   double SpecificStep = 0.5;
93   /* min, max, step and decimals for spin boxes & initial values */
94   GroupPoints->SpinBox_DX->RangeStepAndValidator(COORD_MIN, COORD_MAX, SpecificStep, DBL_DIGITS_DISPLAY);
95   GroupPoints->SpinBox_DX->SetValue(aFactor);
96
97   // Activate Create a Copy mode
98   GroupPoints->CheckButton1->setChecked(true);
99   CreateCopyModeChanged(true);
100
101   GroupBoxPublish->show();
102
103   /* signals and slots connections */
104   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
105   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
106
107   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
108   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
109
110   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
111   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
112
113   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
114   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
115   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
116
117   connect(myGeomGUI->getApp()->selectionMgr(),
118           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
119
120   setHelpFileName("scale_operation_page.html");
121
122   Init();
123 }
124
125
126 //=================================================================================
127 // function : ~TransformationGUI_ScaleDlg()
128 // purpose  : Destroys the object and frees any allocated resources
129 //=================================================================================
130 TransformationGUI_ScaleDlg::~TransformationGUI_ScaleDlg()
131 {
132   /* no need to delete child widgets, Qt does it all for us */
133 }
134
135
136 //=================================================================================
137 // function : Init()
138 // purpose  :
139 //=================================================================================
140 void TransformationGUI_ScaleDlg::Init()
141 {
142   myEditCurrentArgument = GroupPoints->LineEdit1;
143   GroupPoints->LineEdit2->clear();
144
145   myPoint = GEOM::GEOM_Object::_nil();
146
147   initName( tr( "GEOM_SCALE" ) );
148 }
149
150
151 //=================================================================================
152 // function : ClickOnOk()
153 // purpose  :
154 //=================================================================================
155 void TransformationGUI_ScaleDlg::ClickOnOk()
156 {
157   if ( ClickOnApply() )
158     ClickOnCancel();
159 }
160
161
162 //=================================================================================
163 // function : ClickOnApply()
164 // purpose  :
165 //=================================================================================
166 bool TransformationGUI_ScaleDlg::ClickOnApply()
167 {
168   if ( !onAccept(GroupPoints->CheckButton1->isChecked()) )
169     return false;
170
171   Init();
172   return true;
173 }
174
175
176 //=================================================================================
177 // function : SelectionIntoArgument()
178 // purpose  : Called when selection as changed or other case
179 //=================================================================================
180 void TransformationGUI_ScaleDlg::SelectionIntoArgument()
181 {
182   myEditCurrentArgument->setText("");
183   QString aName;
184
185   if(myEditCurrentArgument == GroupPoints->LineEdit1)
186     {
187       int aNbSel = GEOMBase::GetNameOfSelectedIObjects(selectedIO(), aName);
188       if(aNbSel < 1)
189         {
190           myObjects.length(0);
191           return;
192         }
193       GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), myObjects);
194       if (!myObjects.length())
195         return;
196     }
197   else if(myEditCurrentArgument == GroupPoints->LineEdit2)
198     {
199       if(IObjectCount() != 1)
200         {
201           myPoint = GEOM::GEOM_Object::_nil();
202           return;
203         }
204       Standard_Boolean testResult = Standard_False;
205       GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
206       aName = GEOMBase::GetName( aSelectedObject );
207
208       TopoDS_Shape aShape;
209       if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
210         {
211           LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
212           TColStd_IndexedMapOfInteger aMap;
213           aSelMgr->GetIndexes( firstIObject(), aMap );
214           if ( aMap.Extent() == 1 )
215             {
216               int anIndex = aMap( 1 );
217               aName += QString(":vertex_%1").arg(anIndex);
218
219               //Find SubShape Object in Father
220               GEOM::GEOM_Object_var aFindedObject = findObjectInFather(aSelectedObject, aName);
221
222               if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
223                 GEOM::GEOM_IShapesOperations_var aShapesOp =
224                   getGeomEngine()->GetIShapesOperations( getStudyId() );
225                 aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
226                 aSelMgr->clearSelected();
227               }
228               else
229                 aSelectedObject = aFindedObject; // get Object from study
230             }
231           else {
232             if (aShape.ShapeType() != TopAbs_VERTEX) {
233               aSelectedObject = GEOM::GEOM_Object::_nil();
234               aName = "";
235             }
236           }
237         }
238
239       myPoint = aSelectedObject;
240
241       if(!testResult || CORBA::is_nil( myPoint ))
242         return;
243
244     }
245   myEditCurrentArgument->setText( aName );
246
247   displayPreview();
248 }
249
250
251 //=================================================================================
252 // function : LineEditReturnPressed()
253 // purpose  :
254 //=================================================================================
255 void TransformationGUI_ScaleDlg::LineEditReturnPressed()
256 {
257   QLineEdit* send = (QLineEdit*)sender();
258   if(send == GroupPoints->LineEdit1 ||
259      send == GroupPoints->LineEdit2)
260     {
261       myEditCurrentArgument = send;
262       GEOMBase_Skeleton::LineEditReturnPressed();
263     }
264 }
265
266
267 //=================================================================================
268 // function : SetEditCurrentArgument()
269 // purpose  :
270 //=================================================================================
271 void TransformationGUI_ScaleDlg::SetEditCurrentArgument()
272 {
273   QPushButton* send = (QPushButton*)sender();
274   globalSelection();
275
276   if(send == GroupPoints->PushButton1) {
277     myEditCurrentArgument = GroupPoints->LineEdit1;
278   }
279   else if(send == GroupPoints->PushButton2) {
280     myEditCurrentArgument = GroupPoints->LineEdit2;
281     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
282   }
283
284   myEditCurrentArgument->setFocus();
285   SelectionIntoArgument();
286 }
287
288
289 //=================================================================================
290 // function : ActivateThisDialog()
291 // purpose  :
292 //=================================================================================
293 void TransformationGUI_ScaleDlg::ActivateThisDialog()
294 {
295   GEOMBase_Skeleton::ActivateThisDialog();
296   connect(myGeomGUI->getApp()->selectionMgr(),
297           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
298   globalSelection();
299   GroupPoints->LineEdit1->setFocus();
300   myEditCurrentArgument = GroupPoints->LineEdit1;
301 }
302
303
304 //=================================================================================
305 // function : enterEvent()
306 // purpose  :
307 //=================================================================================
308 void TransformationGUI_ScaleDlg::enterEvent(QEvent* e)
309 {
310   if( !GroupConstructors->isEnabled() )
311     ActivateThisDialog();
312 }
313
314
315 //=================================================================================
316 // function : ValueChangedInSpinBox()
317 // purpose  :
318 //=================================================================================
319 void TransformationGUI_ScaleDlg::ValueChangedInSpinBox()
320 {
321   displayPreview();
322 }
323
324
325 //=================================================================================
326 // function : createOperation
327 // purpose  :
328 //=================================================================================
329 GEOM::GEOM_IOperations_ptr  TransformationGUI_ScaleDlg::createOperation()
330 {
331   return myGeomGUI->GetGeomGen()->GetITransformOperations( getStudyId() );
332 }
333
334 //=================================================================================
335 // function : isValid
336 // purpose  :
337 //=================================================================================
338 bool TransformationGUI_ScaleDlg::isValid( QString& msg )
339 {
340   return !(myObjects.length() == 0 || myPoint->_is_nil() || fabs(GetFactor()) <= 0.00001);
341 }
342
343
344 //=================================================================================
345 // function : execute
346 // purpose  :
347 //=================================================================================
348 bool TransformationGUI_ScaleDlg::execute( ObjectList& objects )
349 {
350   bool res = false;
351
352   GEOM::GEOM_Object_var anObj;
353
354   if (GroupPoints->CheckButton1->isChecked() || IsPreview())
355     for (int i = 0; i < myObjects.length(); i++)
356       {
357         anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
358           ScaleShapeCopy( myObjects[i], myPoint, GetFactor() );
359         if ( !anObj->_is_nil() )
360           objects.push_back( anObj._retn() );
361       }
362   else
363     for (int i = 0; i < myObjects.length(); i++)
364       {
365         anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->
366           ScaleShape( myObjects[i], myPoint, GetFactor() );
367         if ( !anObj->_is_nil() )
368           objects.push_back( anObj._retn() );
369       }
370   res = true;
371
372   return res;
373 }
374
375 //=================================================================================
376 // function : restoreSubShapes
377 // purpose  :
378 //=================================================================================
379 void TransformationGUI_ScaleDlg::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
380                                                    SALOMEDS::SObject_ptr theSObject)
381 {
382   if (CheckBoxRestoreSS->isChecked()) {
383     // empty list of arguments means that all arguments should be restored
384     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
385                                         /*theFindMethod=*/GEOM::FSM_Transformed,
386                                         /*theInheritFirstArg=*/true);
387   }
388 }
389
390 //=================================================================================
391 // function : GetFactor()
392 // purpose  :
393 //=================================================================================
394 double TransformationGUI_ScaleDlg::GetFactor() const
395 {
396   return GroupPoints->SpinBox_DX->GetValue();
397 }
398
399
400 //=================================================================================
401 // function :  CreateCopyModeChanged()
402 // purpose  :
403 //=================================================================================
404 void TransformationGUI_ScaleDlg::CreateCopyModeChanged(bool isCreateCopy)
405 {
406   this->GroupBoxName->setEnabled(isCreateCopy);
407 }
408
409 //=================================================================================
410 // function : addSubshapeToStudy
411 // purpose  : virtual method to add new SubObjects if local selection
412 //=================================================================================
413 void TransformationGUI_ScaleDlg::addSubshapesToStudy()
414 {
415   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
416   if (toCreateCopy) {
417     QMap<QString, GEOM::GEOM_Object_var> objMap;
418
419     objMap[GroupPoints->LineEdit2->text()] = myPoint;
420
421     addSubshapesToFather( objMap );
422   }
423 }