Salome HOME
Fix bug 13058: 'Remove webs' functionality works incorrectly.
[modules/geom.git] / src / BasicGUI / BasicGUI_VectorDlg.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   : BasicGUI_VectorDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "BasicGUI_VectorDlg.h"
30
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "LightApp_SelectionMgr.h"
35
36 #include <qlabel.h>
37
38 #include "GEOMImpl_Types.hxx"
39
40 #include "utilities.h"
41
42 using namespace std;
43
44 //=================================================================================
45 // class    : BasicGUI_VectorDlg()
46 // purpose  : Constructs a BasicGUI_VectorDlg 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 BasicGUI_VectorDlg::BasicGUI_VectorDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
52                                        const char* name, bool modal, WFlags fl)
53   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, fl)
54 {
55   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_VECTOR_2P")));
56   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_VECTOR_DXYZ")));
57   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
58
59   setCaption(tr("GEOM_VECTOR_TITLE"));
60
61   /***************************************************************/
62   GroupConstructors->setTitle(tr("GEOM_VECTOR"));
63   RadioButton1->setPixmap(image0);
64   RadioButton2->setPixmap(image1);
65   RadioButton3->close(TRUE);
66
67   GroupPoints = new DlgRef_2Sel_QTD(this, "GroupPoints");
68   GroupPoints->GroupBox1->setTitle(tr("GEOM_POINTS"));
69   GroupPoints->TextLabel1->setText(tr("GEOM_POINT_I").arg("1"));
70   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_I").arg("2"));
71   GroupPoints->PushButton1->setPixmap(image2);
72   GroupPoints->PushButton2->setPixmap(image2);
73
74   GroupPoints->LineEdit1->setReadOnly( true );
75   GroupPoints->LineEdit2->setReadOnly( true );
76
77   GroupDimensions = new DlgRef_3Spin1Check(this, "GroupDimensions");
78   GroupDimensions->GroupBox1->setTitle(tr("GEOM_COORDINATES"));
79   GroupDimensions->TextLabel1->setText(tr("GEOM_DX"));
80   GroupDimensions->TextLabel2->setText(tr("GEOM_DY"));
81   GroupDimensions->TextLabel3->setText(tr("GEOM_DZ"));
82   GroupDimensions->CheckBox1->setText(tr("GEOM_REVERSE_VECTOR"));
83
84   Layout1->addWidget(GroupPoints, 2, 0);
85   Layout1->addWidget(GroupDimensions, 2, 0);
86   /***************************************************************/
87
88   setHelpFileName("vector.htm");
89   
90   /* Initialisations */
91   Init();
92 }
93
94
95 //=================================================================================
96 // function : ~BasicGUI_VectorDlg()
97 // purpose  : Destroys the object and frees any allocated resources
98 //=================================================================================
99 BasicGUI_VectorDlg::~BasicGUI_VectorDlg()
100 {  
101 }
102
103
104 //=================================================================================
105 // function : Init()
106 // purpose  :
107 //=================================================================================
108 void BasicGUI_VectorDlg::Init()
109 {
110     /* init variables */
111   myEditCurrentArgument = GroupPoints->LineEdit1;
112
113   myPoint1 = GEOM::GEOM_Object::_nil();
114   myPoint2 = GEOM::GEOM_Object::_nil();
115
116   /* Get setting of step value from file configuration */
117   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
118   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
119  
120   /* min, max, step and decimals for spin boxes */
121   GroupDimensions->SpinBox_DX->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, 3);
122   GroupDimensions->SpinBox_DY->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, 3);
123   GroupDimensions->SpinBox_DZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, 3);
124
125   double dx( 0. ), dy( 0. ), dz( 200. );
126   GroupDimensions->SpinBox_DX->SetValue( dx );
127   GroupDimensions->SpinBox_DY->SetValue( dy );
128   GroupDimensions->SpinBox_DZ->SetValue( dz );
129
130   GroupDimensions->CheckBox1->setChecked(FALSE);
131
132   /* signals and slots connections */
133   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
134   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
135   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
136   
137   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
138   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
139   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
140
141   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
142   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
143
144   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
145   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
146
147   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
148   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
149   connect(GroupDimensions->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
150
151   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
152   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DY, SLOT(SetStep(double)));
153   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DZ, SLOT(SetStep(double)));
154
155   connect(GroupDimensions->CheckBox1, SIGNAL(stateChanged(int)), this, SLOT(ReverseVector(int)));
156
157   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
158           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
159
160   initName( tr("GEOM_VECTOR") );
161
162   GroupDimensions->hide();
163   ConstructorsClicked( 0 );
164 }
165
166
167 //=================================================================================
168 // function : ConstructorsClicked()
169 // purpose  : Radio button management
170 //=================================================================================
171 void BasicGUI_VectorDlg::ConstructorsClicked( int constructorId )
172 {
173   disconnect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0);
174   myPoint1 = GEOM::GEOM_Object::_nil();
175   myPoint2 = GEOM::GEOM_Object::_nil();
176
177   switch (constructorId)
178     {
179     case 0:
180       {
181         GroupDimensions->hide();
182         resize(0, 0);
183         GroupPoints->show();
184
185         myEditCurrentArgument = GroupPoints->LineEdit1;
186         GroupPoints->LineEdit1->setText("");
187         GroupPoints->LineEdit2->setText("");
188
189         globalSelection( GEOM_POINT );
190         connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
191                 SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
192         break;
193       }
194     case 1:
195       {
196         GroupPoints->hide();
197         resize( 0, 0 );
198         GroupDimensions->show();
199         
200         double dx( 0. ), dy( 0. ), dz( 0. ); 
201         GroupDimensions->SpinBox_DX->SetValue( dx );
202         GroupDimensions->SpinBox_DY->SetValue( dy );
203         GroupDimensions->SpinBox_DZ->SetValue( dz );
204
205         GroupDimensions->CheckBox1->setChecked( FALSE );
206         break;
207       }
208     }
209
210   displayPreview();
211 }
212
213
214 //=================================================================================
215 // function : ClickOnOk()
216 // purpose  :
217 //=================================================================================
218 void BasicGUI_VectorDlg::ClickOnOk()
219 {
220   if ( ClickOnApply() )
221     ClickOnCancel();
222 }
223
224 //=======================================================================
225 // function : ClickOnCancel()
226 // purpose  :
227 //=======================================================================
228 void BasicGUI_VectorDlg::ClickOnCancel()
229 {
230   GEOMBase_Skeleton::ClickOnCancel();
231 }
232
233 //=================================================================================
234 // function : ClickOnApply()
235 // purpose  :
236 //=================================================================================
237 bool BasicGUI_VectorDlg::ClickOnApply()
238 {
239   buttonApply->setFocus();
240
241   if ( !onAccept() )
242     return false;
243
244   initName();
245   if ( getConstructorId() != 1 )
246         ConstructorsClicked( getConstructorId() );
247   return true;
248 }
249
250
251 //=================================================================================
252 // function : SelectionIntoArgument()
253 // purpose  : Called when selection as changed or other case
254 //=================================================================================
255 void BasicGUI_VectorDlg::SelectionIntoArgument()
256 {
257   myEditCurrentArgument->setText("");
258
259   if ( IObjectCount() != 1 ) 
260   {
261     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
262       myPoint1 = GEOM::GEOM_Object::_nil();
263     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
264       myPoint2 = GEOM::GEOM_Object::_nil();
265     return;
266   }
267
268   // nbSel == 1 
269   Standard_Boolean aRes = Standard_False;
270   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
271   if ( !CORBA::is_nil( aSelectedObject ) && aRes )
272   {
273     myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
274     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint1 = aSelectedObject;
275     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myPoint2 = aSelectedObject;
276   }
277
278   displayPreview();
279 }
280
281
282 //=================================================================================
283 // function : SetEditCurrentArgument()
284 // purpose  :
285 //=================================================================================
286 void BasicGUI_VectorDlg::SetEditCurrentArgument()
287 {
288   QPushButton* send = (QPushButton*)sender();
289   if      ( send == GroupPoints->PushButton1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
290   else if ( send == GroupPoints->PushButton2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
291   myEditCurrentArgument->setFocus();
292   SelectionIntoArgument();
293 }
294
295
296 //=================================================================================
297 // function : LineEditReturnPressed()
298 // purpose  :
299 //=================================================================================
300 void BasicGUI_VectorDlg::LineEditReturnPressed()
301
302   QLineEdit* send = (QLineEdit*)sender();
303   if      ( send == GroupPoints->LineEdit1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
304   else if ( send == GroupPoints->LineEdit2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
305   else return;
306   GEOMBase_Skeleton::LineEditReturnPressed();
307 }
308
309
310 //=================================================================================
311 // function : ActivateThisDialog()
312 // purpose  :
313 //=================================================================================
314 void BasicGUI_VectorDlg::ActivateThisDialog()
315 {
316   GEOMBase_Skeleton::ActivateThisDialog();
317   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
318           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
319         
320   ConstructorsClicked( getConstructorId() );
321 }
322
323 //=================================================================================
324 // function : DeactivateActiveDialog()
325 // purpose  : public slot to deactivate if active
326 //=================================================================================
327 void BasicGUI_VectorDlg::DeactivateActiveDialog()
328 {
329   // myGeomGUI->SetState( -1 );
330   GEOMBase_Skeleton::DeactivateActiveDialog();
331 }
332
333 //=================================================================================
334 // function : enterEvent()
335 // purpose  :
336 //=================================================================================
337 void BasicGUI_VectorDlg::enterEvent(QEvent* e)
338 {
339   if ( !GroupConstructors->isEnabled() )
340     ActivateThisDialog();
341 }
342
343 //=================================================================================
344 // function : ValueChangedInSpinBox()
345 // purpose  :
346 //=================================================================================
347 void BasicGUI_VectorDlg::ValueChangedInSpinBox( double newValue )
348 {
349   displayPreview();
350 }
351
352 //=================================================================================
353 // function : ReverseVector()
354 // purpose  : 'state' not used here
355 //=================================================================================
356 void BasicGUI_VectorDlg::ReverseVector(int state)
357 {
358   double dx = -GroupDimensions->SpinBox_DX->GetValue();
359   double dy = -GroupDimensions->SpinBox_DY->GetValue();
360   double dz = -GroupDimensions->SpinBox_DZ->GetValue();
361
362   GroupDimensions->SpinBox_DX->SetValue( dx );
363   GroupDimensions->SpinBox_DY->SetValue( dy );
364   GroupDimensions->SpinBox_DZ->SetValue( dz );
365   
366   displayPreview();
367 }
368
369 //=================================================================================
370 // function : createOperation
371 // purpose  :
372 //=================================================================================
373 GEOM::GEOM_IOperations_ptr BasicGUI_VectorDlg::createOperation()
374 {
375   return myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
376 }
377
378 //=================================================================================
379 // function : isValid
380 // purpose  :
381 //=================================================================================
382 bool BasicGUI_VectorDlg::isValid( QString& msg )
383 {
384   return getConstructorId() == 0 ? !myPoint1->_is_nil() && !myPoint2->_is_nil(): true;
385 }
386
387 //=================================================================================
388 // function : execute
389 // purpose  :
390 //=================================================================================
391 bool BasicGUI_VectorDlg::execute( ObjectList& objects )
392 {
393   bool res = false;
394   
395   GEOM::GEOM_Object_var anObj;
396
397   switch ( getConstructorId() ) {
398   case 0 :
399     anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeVectorTwoPnt( myPoint1, myPoint2 );
400     res = true;
401     break;
402
403   case 1 :
404     {
405       double dx = GroupDimensions->SpinBox_DX->GetValue();
406       double dy = GroupDimensions->SpinBox_DY->GetValue();
407       double dz = GroupDimensions->SpinBox_DZ->GetValue();
408       anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeVectorDXDYDZ( dx,dy,dz );
409       res = true;
410       break;
411     }
412   }
413
414   if ( !anObj->_is_nil() )
415     objects.push_back( anObj._retn() );
416
417   return res;
418 }
419