Salome HOME
SMH: Merged GEOM (NEWGUI, HEAD, POLYWORK)
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : TransformationGUI_OffsetDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "TransformationGUI_OffsetDlg.h"
30
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "SalomeApp_SelectionMgr.h"
35
36 #include <qlabel.h>
37 #include <qcheckbox.h>
38
39 #include "utilities.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(QWidget* parent,  const char* name, bool modal, WFlags fl)
51     :GEOMBase_Skeleton(parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
52 {
53   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_OFFSET")));
54   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
55
56   setCaption(tr("GEOM_OFFSET_TITLE"));
57
58   /***************************************************************/
59   GroupConstructors->setTitle(tr("GEOM_OFFSET"));
60   RadioButton1->setPixmap(image0);
61   RadioButton2->close(TRUE);
62   RadioButton3->close(TRUE);
63  
64   GroupPoints = new DlgRef_1Sel1Spin1Check(this, "GroupPoints");
65   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
66   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
67   GroupPoints->TextLabel2->setText(tr("GEOM_OFFSET"));
68   GroupPoints->CheckButton1->setText(tr("GEOM_CREATE_COPY"));
69
70   // san -- modification of an exisitng object by offset is not allowed
71   GroupPoints->CheckButton1->hide();
72
73   GroupPoints->PushButton1->setPixmap(image1);
74   
75   Layout1->addWidget(GroupPoints, 2, 0);
76   
77   /***************************************************************/
78   
79   Init();
80 }
81
82
83 //=================================================================================
84 // function : ~TransformationGUI_OffsetDlg()
85 // purpose  : Destroys the object and frees any allocated resources
86 //=================================================================================
87 TransformationGUI_OffsetDlg::~TransformationGUI_OffsetDlg()
88 {  
89   /* no need to delete child widgets, Qt does it all for us */
90 }
91
92
93 //=================================================================================
94 // function : Init()
95 // purpose  :
96 //=================================================================================
97 void TransformationGUI_OffsetDlg::Init()
98 {  
99   /* init variables */
100   myEditCurrentArgument = GroupPoints->LineEdit1;
101   GroupPoints->LineEdit1->setReadOnly( true );
102   
103   /* Get setting of step value from file configuration */
104   double step = 1;
105    
106   /* min, max, step and decimals for spin boxes & initial values */
107   GroupPoints->SpinBox_DX->RangeStepAndValidator(-999.999, 999.999, step, 3);
108   GroupPoints->SpinBox_DX->setPrecision(5);
109   //@ GroupPoints->SpinBox_DX->setDblPrecision(1e-05);    
110   GroupPoints->SpinBox_DX->SetValue(1e-05);
111   
112   // Activate Create a Copy mode
113   GroupPoints->CheckButton1->setChecked(true);
114   CreateCopyModeChanged(true);
115
116   /* signals and slots connections */
117   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
118   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
119   
120   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
121   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
122           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
123
124   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
125   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged(bool)));
126   
127   initName( tr( "GEOM_OFFSET" ) );
128
129   globalSelection( GEOM_ALLSHAPES );
130   
131 }
132
133
134 //=================================================================================
135 // function : ClickOnOk()
136 // purpose  :
137 //=================================================================================
138 void TransformationGUI_OffsetDlg::ClickOnOk()
139 {
140   if ( ClickOnApply() )
141     ClickOnCancel();
142 }
143
144
145 //=================================================================================
146 // function : ClickOnApply()
147 // purpose  :
148 //=================================================================================
149 bool TransformationGUI_OffsetDlg::ClickOnApply()
150 {
151   if ( !onAccept( GroupPoints->CheckButton1->isChecked() ) )
152     return false;
153   
154   initName();
155   return true;
156 }
157
158
159 //=======================================================================
160 // function : ClickOnCancel()
161 // purpose  :
162 //=======================================================================
163 void TransformationGUI_OffsetDlg::ClickOnCancel()
164 {
165   GEOMBase_Skeleton::ClickOnCancel();
166 }
167
168
169 //=================================================================================
170 // function : SelectionIntoArgument()
171 // purpose  : Called when selection has changed
172 //=================================================================================
173 void TransformationGUI_OffsetDlg::SelectionIntoArgument()
174 {
175   myEditCurrentArgument->setText("");
176   QString aName;
177
178   int aNbSel = GEOMBase::GetNameOfSelectedIObjects(selectedIO(), aName);
179   if(aNbSel < 1)
180     {
181       myObjects.length(0);
182       return;
183     }
184
185   // nbSel > 0
186   GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), myObjects);
187   if (!myObjects.length())
188     return;
189   
190   myEditCurrentArgument->setText( aName );
191   
192   displayPreview();
193 }
194
195
196 //=================================================================================
197 // function : LineEditReturnPressed()
198 // purpose  :
199 //=================================================================================
200 void TransformationGUI_OffsetDlg::LineEditReturnPressed()
201 {
202   QLineEdit* send = (QLineEdit*)sender();
203   if ( send == GroupPoints->LineEdit1 )
204   {
205     myEditCurrentArgument = GroupPoints->LineEdit1;
206     GEOMBase_Skeleton::LineEditReturnPressed();
207   }
208 }
209
210
211 //=================================================================================
212 // function : SetEditCurrentArgument()
213 // purpose  :
214 //=================================================================================
215 void TransformationGUI_OffsetDlg::SetEditCurrentArgument()
216 {
217   QPushButton* send = (QPushButton*)sender();
218   
219   if(send == GroupPoints->PushButton1)
220     {
221       myEditCurrentArgument = GroupPoints->LineEdit1;
222       myEditCurrentArgument->setFocus();
223       SelectionIntoArgument();
224     }
225 }
226
227
228 //=================================================================================
229 // function : enterEvent()
230 // purpose  : when mouse enter onto the QWidget
231 //=================================================================================
232 void TransformationGUI_OffsetDlg::enterEvent(QEvent * e)
233 {
234   if ( !GroupConstructors->isEnabled() )
235     ActivateThisDialog();
236 }
237
238
239 //=================================================================================
240 // function : ActivateThisDialog()
241 // purpose  :
242 //=================================================================================
243 void TransformationGUI_OffsetDlg::ActivateThisDialog()
244 {
245   GEOMBase_Skeleton::ActivateThisDialog();
246   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
247           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
248   globalSelection( GEOM_ALLSHAPES );
249   myEditCurrentArgument = GroupPoints->LineEdit1;
250   myEditCurrentArgument->setFocus();
251 }
252
253
254 //=================================================================================
255 // function : DeactivateActiveDialog()
256 // purpose  : public slot to deactivate if active
257 //=================================================================================
258 void TransformationGUI_OffsetDlg::DeactivateActiveDialog()
259 {
260  GEOMBase_Skeleton::DeactivateActiveDialog();
261 }
262
263
264 //=================================================================================
265 // function : ValueChangedInSpinBox()
266 // purpose  :
267 //=================================================================================
268 void TransformationGUI_OffsetDlg::ValueChangedInSpinBox()
269 {
270   displayPreview();
271 }
272
273
274 //=================================================================================
275 // function : createOperation
276 // purpose  :
277 //=================================================================================
278 GEOM::GEOM_IOperations_ptr TransformationGUI_OffsetDlg::createOperation()
279 {
280   return getGeomEngine()->GetITransformOperations( getStudyId() );
281 }
282
283 //=================================================================================
284 // function : isValid
285 // purpose  :
286 //=================================================================================
287 bool TransformationGUI_OffsetDlg::isValid( QString& msg )
288 {
289   return !(myObjects.length() == 0);
290 }
291
292
293 //=================================================================================
294 // function : execute
295 // purpose  :
296 //=================================================================================
297 bool TransformationGUI_OffsetDlg::execute( ObjectList& objects )
298 {
299   bool res = false;
300   
301   GEOM::GEOM_Object_var anObj;
302   
303   
304   if (GroupPoints->CheckButton1->isChecked() || IsPreview())
305     for (int i = 0; i < myObjects.length(); i++)
306       {
307         anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->OffsetShapeCopy( myObjects[i], GetOffset() );
308         if ( !anObj->_is_nil() )
309           objects.push_back( anObj._retn() );
310       }
311   else
312     for (int i = 0; i < myObjects.length(); i++)
313       {
314         anObj = GEOM::GEOM_ITransformOperations::_narrow( getOperation() )->OffsetShape( myObjects[i], GetOffset() );
315         if ( !anObj->_is_nil() )
316           objects.push_back( anObj._retn() );
317       }
318      
319   res = true;
320     
321   return res;
322 }
323
324 //=================================================================================
325 // function : closeEvent
326 // purpose  :
327 //=================================================================================
328 void TransformationGUI_OffsetDlg::closeEvent( QCloseEvent* e )
329 {
330   // myGeomGUI->SetState( -1 );
331   GEOMBase_Skeleton::closeEvent( e );
332 }
333
334
335 //=================================================================================
336 // function : GetOffset()
337 // purpose  :
338 //=================================================================================
339 double TransformationGUI_OffsetDlg::GetOffset() const
340 {
341   return GroupPoints->SpinBox_DX->GetValue();
342 }
343
344
345 //=================================================================================
346 // function :  CreateCopyModeChanged()
347 // purpose  :
348 //=================================================================================
349 void TransformationGUI_OffsetDlg::CreateCopyModeChanged(bool isCreateCopy)
350 {
351   this->GroupBoxName->setEnabled(isCreateCopy);
352 }