]> SALOME platform Git repositories - modules/geom.git/blob - src/TransformationGUI/TransformationGUI_ScaleDlg.cxx
Salome HOME
BUG 19185: Qt4 porting: set step value for double spin boxes
[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   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
110   connect( GroupPoints->CheckButton1, SIGNAL( toggled( bool ) ), this, SLOT( CreateCopyModeChanged( bool ) ) );
111    
112   connect( myGeomGUI->getApp()->selectionMgr(), 
113            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
114
115   setHelpFileName( "scale_operation_page.html" );
116
117   Init();
118 }
119
120
121 //=================================================================================
122 // function : ~TransformationGUI_ScaleDlg()
123 // purpose  : Destroys the object and frees any allocated resources
124 //=================================================================================
125 TransformationGUI_ScaleDlg::~TransformationGUI_ScaleDlg()
126 {  
127   /* no need to delete child widgets, Qt does it all for us */
128 }
129
130
131 //=================================================================================
132 // function : Init()
133 // purpose  :
134 //=================================================================================
135 void TransformationGUI_ScaleDlg::Init()
136 {
137   myEditCurrentArgument = GroupPoints->LineEdit1;
138   GroupPoints->LineEdit2->clear();
139   
140   myPoint = GEOM::GEOM_Object::_nil();
141   
142   initName( tr( "GEOM_SCALE" ) );
143 }
144
145
146 //=================================================================================
147 // function : ClickOnOk()
148 // purpose  :
149 //=================================================================================
150 void TransformationGUI_ScaleDlg::ClickOnOk()
151 {
152   if ( ClickOnApply() )
153     ClickOnCancel();
154 }
155
156
157 //=================================================================================
158 // function : ClickOnApply()
159 // purpose  :
160 //=================================================================================
161 bool TransformationGUI_ScaleDlg::ClickOnApply()
162 {
163   if ( !onAccept( GroupPoints->CheckButton1->isChecked() ) )
164     return false;
165
166   Init();
167   return true;
168 }
169
170
171 //=================================================================================
172 // function : SelectionIntoArgument()
173 // purpose  : Called when selection as changed or other case
174 //=================================================================================
175 void TransformationGUI_ScaleDlg::SelectionIntoArgument()
176 {
177   myEditCurrentArgument->setText( "" );
178   QString aName;
179
180   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
181     int aNbSel = GEOMBase::GetNameOfSelectedIObjects( selectedIO(), aName );
182     if ( aNbSel < 1 ) {
183       myObjects.length( 0 );
184       return;
185     }
186     GEOMBase::ConvertListOfIOInListOfGO( selectedIO(), myObjects );
187     if ( !myObjects.length() )
188       return;
189   }
190   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
191     if ( IObjectCount() != 1 ) {
192       myPoint = GEOM::GEOM_Object::_nil();
193       return;
194     }
195     Standard_Boolean testResult = Standard_False;
196     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
197     aName = GEOMBase::GetName( aSelectedObject );
198     
199     TopoDS_Shape aShape;
200     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
201       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
202       TColStd_IndexedMapOfInteger aMap;
203       aSelMgr->GetIndexes( firstIObject(), aMap );
204       if ( aMap.Extent() == 1 ) {
205         int anIndex = aMap( 1 );
206         aName += QString( ":vertex_%1" ).arg( anIndex );
207         
208         //Find SubShape Object in Father
209         GEOM::GEOM_Object_var aFindedObject = findObjectInFather( aSelectedObject, aName );
210         
211         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
212           GEOM::GEOM_IShapesOperations_var aShapesOp =
213             getGeomEngine()->GetIShapesOperations( getStudyId() );
214           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
215           aSelMgr->clearSelected();
216         }
217         else {
218           aSelectedObject = aFindedObject; // get Object from study
219         }
220       }
221       else {
222         if ( aShape.ShapeType() != TopAbs_VERTEX ) {
223           aSelectedObject = GEOM::GEOM_Object::_nil();
224           aName = "";
225         }
226       }
227     }
228     
229     myPoint = aSelectedObject;
230
231     if ( !testResult || CORBA::is_nil( myPoint ) )
232       return;
233   }
234   myEditCurrentArgument->setText( aName );
235   
236   displayPreview();
237 }
238
239
240 //=================================================================================
241 // function : LineEditReturnPressed()
242 // purpose  :
243 //=================================================================================
244 void TransformationGUI_ScaleDlg::LineEditReturnPressed()
245 {
246   QLineEdit* send = (QLineEdit*)sender();
247   if ( send == GroupPoints->LineEdit1 ||
248        send == GroupPoints->LineEdit2 ) {
249     myEditCurrentArgument = send;
250     GEOMBase_Skeleton::LineEditReturnPressed();
251   }
252 }
253
254
255 //=================================================================================
256 // function : SetEditCurrentArgument()
257 // purpose  :
258 //=================================================================================
259 void TransformationGUI_ScaleDlg::SetEditCurrentArgument()
260 {
261   QPushButton* send = (QPushButton*)sender();
262   globalSelection();
263   
264   if ( send == GroupPoints->PushButton1 ) {
265     myEditCurrentArgument = GroupPoints->LineEdit1;
266   }
267   else if ( send == GroupPoints->PushButton2 ) {
268     myEditCurrentArgument = GroupPoints->LineEdit2;
269     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
270   }
271   
272   myEditCurrentArgument->setFocus();
273   SelectionIntoArgument();
274 }
275
276
277 //=================================================================================
278 // function : ActivateThisDialog()
279 // purpose  :
280 //=================================================================================
281 void TransformationGUI_ScaleDlg::ActivateThisDialog()
282 {
283   GEOMBase_Skeleton::ActivateThisDialog();
284   connect( myGeomGUI->getApp()->selectionMgr(), 
285            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
286   globalSelection();
287   GroupPoints->LineEdit1->setFocus();
288   myEditCurrentArgument = GroupPoints->LineEdit1;
289 }
290
291
292 //=================================================================================
293 // function : enterEvent()
294 // purpose  :
295 //=================================================================================
296 void TransformationGUI_ScaleDlg::enterEvent( QEvent* )
297 {
298   if ( !mainFrame()->GroupConstructors->isEnabled() )
299     ActivateThisDialog();
300 }
301
302
303 //=================================================================================
304 // function : ValueChangedInSpinBox()
305 // purpose  :
306 //=================================================================================
307 void TransformationGUI_ScaleDlg::ValueChangedInSpinBox()
308 {
309   displayPreview();
310 }
311
312 //=================================================================================
313 // function : SetDoubleSpinBoxStep()
314 // purpose  : Double spin box management
315 //=================================================================================
316 void TransformationGUI_ScaleDlg::SetDoubleSpinBoxStep( double step )
317 {
318   GroupPoints->SpinBox_DX->setSingleStep(step);
319 }
320
321
322 //=================================================================================
323 // function : createOperation
324 // purpose  :
325 //=================================================================================
326 GEOM::GEOM_IOperations_ptr TransformationGUI_ScaleDlg::createOperation()
327 {
328   return myGeomGUI->GetGeomGen()->GetITransformOperations( getStudyId() );
329 }
330
331 //=================================================================================
332 // function : isValid
333 // purpose  :
334 //=================================================================================
335 bool TransformationGUI_ScaleDlg::isValid( QString& /*msg*/)
336 {
337   return !( myObjects.length() == 0 || myPoint->_is_nil() || fabs( GetFactor()) <= 0.00001 );
338 }
339
340
341 //=================================================================================
342 // function : execute
343 // purpose  :
344 //=================================================================================
345 bool TransformationGUI_ScaleDlg::execute( ObjectList& objects )
346 {
347   GEOM::GEOM_Object_var anObj;
348
349   if ( GroupPoints->CheckButton1->isChecked() || IsPreview() ) {
350     for ( int i = 0; i < myObjects.length(); i++ ) {
351       anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->ScaleShapeCopy( myObjects[i], myPoint, GetFactor() );
352       if ( !anObj->_is_nil() )
353         objects.push_back( anObj._retn() );
354     }
355   }
356   else {
357     for ( int i = 0; i < myObjects.length(); i++ ) {
358       anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->ScaleShape( myObjects[i], myPoint, GetFactor() );
359       if ( !anObj->_is_nil() )
360         objects.push_back( anObj._retn() );
361     }
362   }
363   return true;
364 }
365
366
367 //=================================================================================
368 // function : GetFactor()
369 // purpose  :
370 //=================================================================================
371 double TransformationGUI_ScaleDlg::GetFactor() const
372 {
373   return GroupPoints->SpinBox_DX->value();
374 }
375
376
377 //=================================================================================
378 // function :  CreateCopyModeChanged()
379 // purpose  :
380 //=================================================================================
381 void TransformationGUI_ScaleDlg::CreateCopyModeChanged( bool isCreateCopy )
382 {
383   mainFrame()->GroupBoxName->setEnabled(isCreateCopy);
384 }
385
386 //=================================================================================
387 // function : addSubshapeToStudy
388 // purpose  : virtual method to add new SubObjects if local selection
389 //=================================================================================
390 void TransformationGUI_ScaleDlg::addSubshapesToStudy()
391 {
392   bool toCreateCopy = IsPreview() || GroupPoints->CheckButton1->isChecked();
393   if ( toCreateCopy ) {
394     QMap<QString, GEOM::GEOM_Object_var> objMap;
395
396     objMap[GroupPoints->LineEdit2->text()] = myPoint;
397     
398     addSubshapesToFather( objMap );
399   }
400 }