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