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