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