Salome HOME
d81792e8fc7f106b3e529c38e64abad9d9ddf0d4
[modules/geom.git] / TransformationGUI_OffsetDlg.cxx
1 //  Copyright (C) 2007-2010  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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : TransformationGUI_OffsetDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "TransformationGUI_OffsetDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_Session.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 //=================================================================================
39 // class    : TransformationGUI_OffsetDlg()
40 // purpose  : Constructs a TransformationGUI_OffsetDlg which is a child of 'parent', with the 
41 //            name 'name' and widget flags set to 'f'.
42 //            The dialog will by default be modeless, unless you set 'modal' to
43 //            TRUE to construct a modal dialog.
44 //=================================================================================
45 TransformationGUI_OffsetDlg::TransformationGUI_OffsetDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
46                                                           bool modal, Qt::WindowFlags fl )
47   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
48 {
49   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_OFFSET" ) ) );
50   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
51
52   setWindowTitle( tr( "GEOM_OFFSET_TITLE" ) );
53
54   /***************************************************************/
55   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_OFFSET" ) );
56   mainFrame()->RadioButton1->setIcon( image0 );
57   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
58   mainFrame()->RadioButton2->close();
59   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
60   mainFrame()->RadioButton3->close();
61  
62   GroupPoints = new DlgRef_1Sel1Spin1Check( centralWidget() );
63   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
64   GroupPoints->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
65   GroupPoints->TextLabel2->setText( tr( "GEOM_OFFSET" ) );
66   GroupPoints->CheckButton1->setText( tr( "GEOM_CREATE_COPY" ) );
67
68   // san -- modification of an exisitng object by offset is not allowed
69   GroupPoints->CheckButton1->hide();
70
71   GroupPoints->PushButton1->setIcon( image1 );
72   
73   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
74   layout->setMargin( 0 ); layout->setSpacing( 6 );
75   layout->addWidget( GroupPoints );
76   
77   /***************************************************************/
78
79   setHelpFileName( "offset_operation_page.html" );
80   
81   Init();
82 }
83
84
85 //=================================================================================
86 // function : ~TransformationGUI_OffsetDlg()
87 // purpose  : Destroys the object and frees any allocated resources
88 //=================================================================================
89 TransformationGUI_OffsetDlg::~TransformationGUI_OffsetDlg()
90 {  
91   /* no need to delete child widgets, Qt does it all for us */
92 }
93
94
95 //=================================================================================
96 // function : Init()
97 // purpose  :
98 //=================================================================================
99 void TransformationGUI_OffsetDlg::Init()
100 {  
101   /* init variables */
102   myEditCurrentArgument = GroupPoints->LineEdit1;
103   GroupPoints->LineEdit1->setReadOnly( true );
104   
105   /* Get setting of step value from file configuration */
106   double step = 1;
107    
108   /* min, max, step and decimals for spin boxes & initial values */
109   initSpinBox( GroupPoints->SpinBox_DX, COORD_MIN, COORD_MAX, step, "length_precision" );
110   GroupPoints->SpinBox_DX->setValue( 1e-05 );
111   
112   // Activate Create a Copy mode
113   GroupPoints->CheckButton1->setChecked( true );
114   CreateCopyModeChanged( true );
115
116   mainFrame()->GroupBoxPublish->show();
117
118   /* signals and slots connections */
119   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
120   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
121   
122   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
123   connect( myGeomGUI->getApp()->selectionMgr(), 
124            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
125
126   connect( GroupPoints->SpinBox_DX,   SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
127   connect( GroupPoints->CheckButton1, SIGNAL( toggled( bool ) ),        this, SLOT( CreateCopyModeChanged( bool ) ) );
128   
129   initName( tr( "GEOM_OFFSET" ) );
130
131   globalSelection( GEOM_ALLSHAPES );
132   resize(100,100);
133   SelectionIntoArgument();
134 }
135
136
137 //=================================================================================
138 // function : ClickOnOk()
139 // purpose  :
140 //=================================================================================
141 void TransformationGUI_OffsetDlg::ClickOnOk()
142 {
143   if ( ClickOnApply() )
144     ClickOnCancel();
145 }
146
147
148 //=================================================================================
149 // function : ClickOnApply()
150 // purpose  :
151 //=================================================================================
152 bool TransformationGUI_OffsetDlg::ClickOnApply()
153 {
154   if ( !onAccept( GroupPoints->CheckButton1->isChecked() ) )
155     return false;
156   
157   initName();
158   return true;
159 }
160
161
162 //=================================================================================
163 // function : SelectionIntoArgument()
164 // purpose  : Called when selection has changed
165 //=================================================================================
166 void TransformationGUI_OffsetDlg::SelectionIntoArgument()
167 {
168   myEditCurrentArgument->setText( "" );
169   QString aName;
170
171   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
172   SALOME_ListIO aSelList;
173   aSelMgr->selectedObjects(aSelList);
174
175   int aNbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aName);
176   if ( aNbSel < 1 ) {
177     myObjects.length( 0 );
178     return;
179   }
180
181   // nbSel > 0
182   GEOMBase::ConvertListOfIOInListOfGO (aSelList, myObjects);
183   if (!myObjects.length())
184     return;
185
186   myEditCurrentArgument->setText(aName);
187
188   displayPreview();
189 }
190
191
192 //=================================================================================
193 // function : LineEditReturnPressed()
194 // purpose  :
195 //=================================================================================
196 void TransformationGUI_OffsetDlg::LineEditReturnPressed()
197 {
198   QLineEdit* send = (QLineEdit*)sender();
199   if ( send == GroupPoints->LineEdit1 ) {
200     myEditCurrentArgument = GroupPoints->LineEdit1;
201     GEOMBase_Skeleton::LineEditReturnPressed();
202   }
203 }
204
205
206 //=================================================================================
207 // function : SetEditCurrentArgument()
208 // purpose  :
209 //=================================================================================
210 void TransformationGUI_OffsetDlg::SetEditCurrentArgument()
211 {
212   QPushButton* send = (QPushButton*)sender();
213   
214   if ( send == GroupPoints->PushButton1 ) {
215     GroupPoints->PushButton1->setDown(true);
216     myEditCurrentArgument = GroupPoints->LineEdit1;
217     myEditCurrentArgument->setFocus();
218     SelectionIntoArgument();
219   }
220 }
221
222
223 //=================================================================================
224 // function : enterEvent()
225 // purpose  : when mouse enter onto the QWidget
226 //=================================================================================
227 void TransformationGUI_OffsetDlg::enterEvent( QEvent* )
228 {
229   if ( !mainFrame()->GroupConstructors->isEnabled() )
230     ActivateThisDialog();
231 }
232
233
234 //=================================================================================
235 // function : ActivateThisDialog()
236 // purpose  :
237 //=================================================================================
238 void TransformationGUI_OffsetDlg::ActivateThisDialog()
239 {
240   GEOMBase_Skeleton::ActivateThisDialog();
241   connect( myGeomGUI->getApp()->selectionMgr(), 
242            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
243   globalSelection( GEOM_ALLSHAPES );
244   myEditCurrentArgument = GroupPoints->LineEdit1;
245   myEditCurrentArgument->setFocus();
246 }
247
248
249 //=================================================================================
250 // function : ValueChangedInSpinBox()
251 // purpose  :
252 //=================================================================================
253 void TransformationGUI_OffsetDlg::ValueChangedInSpinBox()
254 {
255   displayPreview();
256 }
257
258
259 //=================================================================================
260 // function : createOperation
261 // purpose  :
262 //=================================================================================
263 GEOM::GEOM_IOperations_ptr TransformationGUI_OffsetDlg::createOperation()
264 {
265   return getGeomEngine()->GetITransformOperations( getStudyId() );
266 }
267
268 //=================================================================================
269 // function : isValid
270 // purpose  :
271 //=================================================================================
272 bool TransformationGUI_OffsetDlg::isValid( QString& msg )
273 {
274   //return !(myObjects.length() == 0);
275   if ( myObjects.length() == 0 ) return false;
276
277   for ( int i = 0; i < myObjects.length(); i++ ) {
278     GEOM::shape_type aType = myObjects[i]->GetShapeType();
279     if ( aType != GEOM::FACE && aType != GEOM::SHELL && aType != GEOM::SOLID ) {
280       msg = tr( "ERROR_SHAPE_TYPE" );
281       return false;
282     }
283   }
284   return GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() );
285 }
286
287 //=================================================================================
288 // function : execute
289 // purpose  :
290 //=================================================================================
291 bool TransformationGUI_OffsetDlg::execute( ObjectList& objects )
292 {
293   bool res = false;
294   
295   GEOM::GEOM_Object_var anObj;
296   
297   GEOM::GEOM_ITransformOperations_var anOper = GEOM::GEOM_ITransformOperations::_narrow(getOperation());
298
299   if ( GroupPoints->CheckButton1->isChecked() || IsPreview() ) {
300     for ( int i = 0; i < myObjects.length(); i++ ) {
301       
302       anObj = anOper->OffsetShapeCopy( myObjects[i], GetOffset() );
303       if ( !anObj->_is_nil() ) {
304         if(!IsPreview()) {
305           anObj->SetParameters(GroupPoints->SpinBox_DX->text().toLatin1().constData());
306         }
307         objects.push_back( anObj._retn() );
308       }
309     }
310   }
311   else {
312     for ( int i = 0; i < myObjects.length(); i++ ) {
313       anObj = anOper->OffsetShape( myObjects[i], GetOffset() );
314       if ( !anObj->_is_nil() )
315         objects.push_back( anObj._retn() );
316     }
317   }
318   res = true;
319     
320   return res;
321 }
322
323 //=================================================================================
324 // function : restoreSubShapes
325 // purpose  :
326 //=================================================================================
327 void TransformationGUI_OffsetDlg::restoreSubShapes( SALOMEDS::Study_ptr   theStudy,
328                                                     SALOMEDS::SObject_ptr theSObject )
329 {
330   if ( mainFrame()->CheckBoxRestoreSS->isChecked() ) {
331     // empty list of arguments means that all arguments should be restored
332     getGeomEngine()->RestoreSubShapesSO( theStudy, theSObject, GEOM::ListOfGO(),
333                                          /*theFindMethod=*/GEOM::FSM_Transformed,
334                                          /*theInheritFirstArg=*/true,
335                                          mainFrame()->CheckBoxAddPrefix->isChecked() );
336   }
337 }
338
339 //=================================================================================
340 // function : GetOffset()
341 // purpose  :
342 //=================================================================================
343 double TransformationGUI_OffsetDlg::GetOffset() const
344 {
345   return GroupPoints->SpinBox_DX->value();
346 }
347
348 //=================================================================================
349 // function :  CreateCopyModeChanged()
350 // purpose  :
351 //=================================================================================
352 void TransformationGUI_OffsetDlg::CreateCopyModeChanged( bool isCreateCopy )
353 {
354   mainFrame()->GroupBoxName->setEnabled( isCreateCopy );
355 }