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