Salome HOME
Porting to Qt4.
[modules/geom.git] / src / TransformationGUI / TransformationGUI_OffsetDlg.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_OffsetDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "TransformationGUI_OffsetDlg.h"
30 #include "DlgRef_1Sel1Spin1Check.h"
31 #include "DlgRef_SpinBox.h"
32
33 #include "GeometryGUI.h"
34 #include "GEOMBase.h"
35
36 #include "SUIT_ResourceMgr.h"
37 #include "SUIT_Session.h"
38 #include "SalomeApp_Application.h"
39 #include "LightApp_SelectionMgr.h"
40
41 using namespace std;
42
43 //=================================================================================
44 // class    : TransformationGUI_OffsetDlg()
45 // purpose  : Constructs a TransformationGUI_OffsetDlg which is a child of 'parent', with the 
46 //            name 'name' and widget flags set to 'f'.
47 //            The dialog will by default be modeless, unless you set 'modal' to
48 //            TRUE to construct a modal dialog.
49 //=================================================================================
50 TransformationGUI_OffsetDlg::TransformationGUI_OffsetDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
51                                                          const char* name, bool modal, Qt::WindowFlags fl)
52     :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
53 {
54   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_OFFSET")));
55   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
56
57   setWindowTitle(tr("GEOM_OFFSET_TITLE"));
58
59   /***************************************************************/
60   GroupConstructors->setTitle(tr("GEOM_OFFSET"));
61   RadioButton1->setIcon(image0);
62   RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
63   RadioButton2->close();
64   RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
65   RadioButton3->close();
66  
67   GroupPoints = new DlgRef_1Sel1Spin1Check(this, "GroupPoints");
68   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
69   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
70   GroupPoints->TextLabel2->setText(tr("GEOM_OFFSET"));
71   GroupPoints->CheckButton1->setText(tr("GEOM_CREATE_COPY"));
72
73   // san -- modification of an exisitng object by offset is not allowed
74   GroupPoints->CheckButton1->hide();
75
76   GroupPoints->PushButton1->setIcon(image1);
77   
78   gridLayout1->addWidget(GroupPoints, 2, 0);
79   
80   /***************************************************************/
81
82   setHelpFileName("offset_surface.htm");  
83   
84   Init();
85 }
86
87
88 //=================================================================================
89 // function : ~TransformationGUI_OffsetDlg()
90 // purpose  : Destroys the object and frees any allocated resources
91 //=================================================================================
92 TransformationGUI_OffsetDlg::~TransformationGUI_OffsetDlg()
93 {  
94   /* no need to delete child widgets, Qt does it all for us */
95 }
96
97
98 //=================================================================================
99 // function : Init()
100 // purpose  :
101 //=================================================================================
102 void TransformationGUI_OffsetDlg::Init()
103 {  
104   /* init variables */
105   myEditCurrentArgument = GroupPoints->LineEdit1;
106   GroupPoints->LineEdit1->setReadOnly( true );
107   
108   /* Get setting of step value from file configuration */
109   double step = 1;
110    
111   /* min, max, step and decimals for spin boxes & initial values */
112   GroupPoints->SpinBox_DX->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, 3);
113   GroupPoints->SpinBox_DX->setDecimals(5);
114   //@ GroupPoints->SpinBox_DX->setDblPrecision(1e-05);    
115   GroupPoints->SpinBox_DX->SetValue(1e-05);
116   
117   // Activate Create a Copy mode
118   GroupPoints->CheckButton1->setChecked(true);
119   CreateCopyModeChanged(true);
120
121   /* signals and slots connections */
122   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
123   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
124   
125   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
126   connect(myGeomGUI->getApp()->selectionMgr(), 
127           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
128
129   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
130   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
131   
132   initName( tr( "GEOM_OFFSET" ).toLatin1().constData() );
133
134   globalSelection( GEOM_ALLSHAPES );
135   
136 }
137
138
139 //=================================================================================
140 // function : ClickOnOk()
141 // purpose  :
142 //=================================================================================
143 void TransformationGUI_OffsetDlg::ClickOnOk()
144 {
145   if ( ClickOnApply() )
146     ClickOnCancel();
147 }
148
149
150 //=================================================================================
151 // function : ClickOnApply()
152 // purpose  :
153 //=================================================================================
154 bool TransformationGUI_OffsetDlg::ClickOnApply()
155 {
156   if ( !onAccept( GroupPoints->CheckButton1->isChecked() ) )
157     return false;
158   
159   initName();
160   return true;
161 }
162
163
164 //=================================================================================
165 // function : SelectionIntoArgument()
166 // purpose  : Called when selection has changed
167 //=================================================================================
168 void TransformationGUI_OffsetDlg::SelectionIntoArgument()
169 {
170   myEditCurrentArgument->setText("");
171   QString aName;
172
173   int aNbSel = GEOMBase::GetNameOfSelectedIObjects(selectedIO(), aName);
174   if(aNbSel < 1)
175     {
176       myObjects.length(0);
177       return;
178     }
179
180   // nbSel > 0
181   GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), myObjects);
182   if (!myObjects.length())
183     return;
184   
185   myEditCurrentArgument->setText( aName );
186   
187   displayPreview();
188 }
189
190
191 //=================================================================================
192 // function : LineEditReturnPressed()
193 // purpose  :
194 //=================================================================================
195 void TransformationGUI_OffsetDlg::LineEditReturnPressed()
196 {
197   QLineEdit* send = (QLineEdit*)sender();
198   if ( send == GroupPoints->LineEdit1 )
199   {
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     {
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 * e)
228 {
229   if ( !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   {
279     GEOM::shape_type aType = myObjects[i]->GetShapeType();
280     if( aType != GEOM::FACE && aType != GEOM::SHELL && aType != GEOM::SOLID ){
281        msg = tr("ERROR_SHAPE_TYPE");
282        return false;
283     }
284   }
285   return true;
286 }
287
288
289 //=================================================================================
290 // function : execute
291 // purpose  :
292 //=================================================================================
293 bool TransformationGUI_OffsetDlg::execute( ObjectList& objects )
294 {
295   bool res = false;
296   
297   GEOM::GEOM_Object_var anObj;
298   
299   
300   if (GroupPoints->CheckButton1->isChecked() || IsPreview())
301     for (int i = 0; i < myObjects.length(); i++)
302       {
303
304         anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->OffsetShapeCopy( myObjects[i], GetOffset() );
305         if ( !anObj->_is_nil() )
306           objects.push_back( anObj._retn() );
307       }
308   else
309     for (int i = 0; i < myObjects.length(); i++)
310       {
311         anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->OffsetShape( myObjects[i], GetOffset() );
312         if ( !anObj->_is_nil() )
313           objects.push_back( anObj._retn() );
314       }
315      
316   res = true;
317     
318   return res;
319 }
320
321 //=================================================================================
322 // function : closeEvent
323 // purpose  :
324 //=================================================================================
325 void TransformationGUI_OffsetDlg::closeEvent( QCloseEvent* e )
326 {
327   // myGeomGUI->SetState( -1 );
328   GEOMBase_Skeleton::closeEvent( e );
329 }
330
331
332 //=================================================================================
333 // function : GetOffset()
334 // purpose  :
335 //=================================================================================
336 double TransformationGUI_OffsetDlg::GetOffset() const
337 {
338   return GroupPoints->SpinBox_DX->GetValue();
339 }
340
341
342 //=================================================================================
343 // function :  CreateCopyModeChanged()
344 // purpose  :
345 //=================================================================================
346 void TransformationGUI_OffsetDlg::CreateCopyModeChanged(bool isCreateCopy)
347 {
348   this->GroupBoxName->setEnabled(isCreateCopy);
349 }