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