]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_VectorDlg.cxx
Salome HOME
f5bb3bb449fd84ae5166212479b1423afbdb3489
[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 <TopoDS_Shape.hxx>
37 #include <TopoDS.hxx>
38 #include <TopExp.hxx>
39 #include <TColStd_IndexedMapOfInteger.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
41
42 #include <qlabel.h>
43
44 #include "GEOMImpl_Types.hxx"
45
46 #include "utilities.h"
47
48 using namespace std;
49
50 //=================================================================================
51 // class    : BasicGUI_VectorDlg()
52 // purpose  : Constructs a BasicGUI_VectorDlg which is a child of 'parent', with the 
53 //            name 'name' and widget flags set to 'f'.
54 //            The dialog will by default be modeless, unless you set 'modal' to
55 //            TRUE to construct a modal dialog.
56 //=================================================================================
57 BasicGUI_VectorDlg::BasicGUI_VectorDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
58                                        const char* name, bool modal, WFlags fl)
59   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, fl)
60 {
61   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_VECTOR_2P")));
62   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_VECTOR_DXYZ")));
63   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
64
65   setCaption(tr("GEOM_VECTOR_TITLE"));
66
67   /***************************************************************/
68   GroupConstructors->setTitle(tr("GEOM_VECTOR"));
69   RadioButton1->setPixmap(image0);
70   RadioButton2->setPixmap(image1);
71   RadioButton3->close(TRUE);
72
73   GroupPoints = new DlgRef_2Sel_QTD(this, "GroupPoints");
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->setPixmap(image2);
78   GroupPoints->PushButton2->setPixmap(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   Layout1->addWidget(GroupPoints, 2, 0);
91   Layout1->addWidget(GroupDimensions, 2, 0);
92   /***************************************************************/
93
94   setHelpFileName("create_vector_page.html");
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, DBL_DIGITS_DISPLAY);
128   GroupDimensions->SpinBox_DY->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY);
129   GroupDimensions->SpinBox_DZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY);
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(myGeomGUI->getApp()->selectionMgr(), 
164           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
165
166   initName( tr("GEOM_VECTOR") );
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(myGeomGUI->getApp()->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       GroupPoints->show();
190
191       myEditCurrentArgument = GroupPoints->LineEdit1;
192       GroupPoints->LineEdit1->setText("");
193       GroupPoints->LineEdit2->setText("");
194
195       globalSelection(); // close local contexts, if any
196       localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
197       connect(myGeomGUI->getApp()->selectionMgr(), 
198               SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
199       break;
200     }
201     case 1:
202     {
203       GroupPoints->hide();
204       resize( 0, 0 );
205       GroupDimensions->show();
206       globalSelection(); // close local contexts, if any
207
208       double dx( 0. ), dy( 0. ), dz( 0. ); 
209       GroupDimensions->SpinBox_DX->SetValue( dx );
210       GroupDimensions->SpinBox_DY->SetValue( dy );
211       GroupDimensions->SpinBox_DZ->SetValue( dz );
212
213       GroupDimensions->CheckBox1->setChecked( FALSE );
214       break;
215     }
216   }
217
218   displayPreview();
219 }
220
221
222 //=================================================================================
223 // function : ClickOnOk()
224 // purpose  :
225 //=================================================================================
226 void BasicGUI_VectorDlg::ClickOnOk()
227 {
228   if ( ClickOnApply() )
229     ClickOnCancel();
230 }
231
232 //=======================================================================
233 // function : ClickOnCancel()
234 // purpose  :
235 //=======================================================================
236 void BasicGUI_VectorDlg::ClickOnCancel()
237 {
238   GEOMBase_Skeleton::ClickOnCancel();
239 }
240
241 //=================================================================================
242 // function : ClickOnApply()
243 // purpose  :
244 //=================================================================================
245 bool BasicGUI_VectorDlg::ClickOnApply()
246 {
247   buttonApply->setFocus();
248
249   if ( !onAccept() )
250     return false;
251
252   initName();
253   if ( getConstructorId() != 1 )
254         ConstructorsClicked( getConstructorId() );
255   return true;
256 }
257
258
259 //=================================================================================
260 // function : SelectionIntoArgument()
261 // purpose  : Called when selection as changed or other case
262 //=================================================================================
263 void BasicGUI_VectorDlg::SelectionIntoArgument()
264 {
265   myEditCurrentArgument->setText("");
266
267   if (IObjectCount() != 1) 
268   {
269     if (myEditCurrentArgument == GroupPoints->LineEdit1)
270       myPoint1 = GEOM::GEOM_Object::_nil();
271     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
272       myPoint2 = GEOM::GEOM_Object::_nil();
273     return;
274   }
275
276   // nbSel == 1 
277   Standard_Boolean aRes = Standard_False;
278   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), aRes);
279   if (!CORBA::is_nil(aSelectedObject) && aRes)
280   {
281     QString aName = GEOMBase::GetName(aSelectedObject);
282
283     TopoDS_Shape aShape;
284     if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
285     {
286       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
287       TColStd_IndexedMapOfInteger aMap;
288       aSelMgr->GetIndexes(firstIObject(), aMap);
289       if (aMap.Extent() == 1) // Local Selection
290       {
291
292         int anIndex = aMap(1);
293         aName += QString(":vertex_%1").arg(anIndex);
294
295         //Find SubShape Object in Father
296         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
297         
298         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
299           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
300           aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
301         }
302         else
303           aSelectedObject = aFindedObject; // get Object from study
304       }
305       else // Global Selection
306       {
307         if (aShape.ShapeType() != TopAbs_VERTEX) {
308           aSelectedObject = GEOM::GEOM_Object::_nil();
309           aName = "";
310         }
311       }
312     }
313
314     myEditCurrentArgument->setText(aName);
315
316     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint1 = aSelectedObject;
317     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myPoint2 = aSelectedObject;
318   }
319
320   displayPreview();
321 }
322
323 //=================================================================================
324 // function : SetEditCurrentArgument()
325 // purpose  :
326 //=================================================================================
327 void BasicGUI_VectorDlg::SetEditCurrentArgument()
328 {
329   QPushButton* send = (QPushButton*)sender();
330   if      ( send == GroupPoints->PushButton1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
331   else if ( send == GroupPoints->PushButton2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
332   myEditCurrentArgument->setFocus();
333   SelectionIntoArgument();
334 }
335
336
337 //=================================================================================
338 // function : LineEditReturnPressed()
339 // purpose  :
340 //=================================================================================
341 void BasicGUI_VectorDlg::LineEditReturnPressed()
342
343   QLineEdit* send = (QLineEdit*)sender();
344   if      ( send == GroupPoints->LineEdit1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
345   else if ( send == GroupPoints->LineEdit2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
346   else return;
347   GEOMBase_Skeleton::LineEditReturnPressed();
348 }
349
350 //=================================================================================
351 // function : ActivateThisDialog()
352 // purpose  :
353 //=================================================================================
354 void BasicGUI_VectorDlg::ActivateThisDialog()
355 {
356   GEOMBase_Skeleton::ActivateThisDialog();
357   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
358           this, SLOT(SelectionIntoArgument()));
359         
360   ConstructorsClicked(getConstructorId());
361 }
362
363 //=================================================================================
364 // function : DeactivateActiveDialog()
365 // purpose  : public slot to deactivate if active
366 //=================================================================================
367 void BasicGUI_VectorDlg::DeactivateActiveDialog()
368 {
369   GEOMBase_Skeleton::DeactivateActiveDialog();
370 }
371
372 //=================================================================================
373 // function : enterEvent()
374 // purpose  :
375 //=================================================================================
376 void BasicGUI_VectorDlg::enterEvent(QEvent* e)
377 {
378   if ( !GroupConstructors->isEnabled() )
379     ActivateThisDialog();
380 }
381
382 //=================================================================================
383 // function : ValueChangedInSpinBox()
384 // purpose  :
385 //=================================================================================
386 void BasicGUI_VectorDlg::ValueChangedInSpinBox( double newValue )
387 {
388   displayPreview();
389 }
390
391 //=================================================================================
392 // function : ReverseVector()
393 // purpose  : 'state' not used here
394 //=================================================================================
395 void BasicGUI_VectorDlg::ReverseVector(int state)
396 {
397   double dx = -GroupDimensions->SpinBox_DX->GetValue();
398   double dy = -GroupDimensions->SpinBox_DY->GetValue();
399   double dz = -GroupDimensions->SpinBox_DZ->GetValue();
400
401   GroupDimensions->SpinBox_DX->SetValue( dx );
402   GroupDimensions->SpinBox_DY->SetValue( dy );
403   GroupDimensions->SpinBox_DZ->SetValue( dz );
404   
405   displayPreview();
406 }
407
408 //=================================================================================
409 // function : createOperation
410 // purpose  :
411 //=================================================================================
412 GEOM::GEOM_IOperations_ptr BasicGUI_VectorDlg::createOperation()
413 {
414   return myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
415 }
416
417 //=================================================================================
418 // function : isValid
419 // purpose  :
420 //=================================================================================
421 bool BasicGUI_VectorDlg::isValid( QString& msg )
422 {
423   return getConstructorId() == 0 ? !myPoint1->_is_nil() && !myPoint2->_is_nil(): true;
424 }
425
426 //=================================================================================
427 // function : execute
428 // purpose  :
429 //=================================================================================
430 bool BasicGUI_VectorDlg::execute( ObjectList& objects )
431 {
432   bool res = false;
433   
434   GEOM::GEOM_Object_var anObj;
435
436   switch ( getConstructorId() ) {
437   case 0 :
438     anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeVectorTwoPnt( myPoint1, myPoint2 );
439     res = true;
440     break;
441
442   case 1 :
443     {
444       double dx = GroupDimensions->SpinBox_DX->GetValue();
445       double dy = GroupDimensions->SpinBox_DY->GetValue();
446       double dz = GroupDimensions->SpinBox_DZ->GetValue();
447       anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeVectorDXDYDZ( dx,dy,dz );
448       res = true;
449       break;
450     }
451   }
452
453   if ( !anObj->_is_nil() )
454     objects.push_back( anObj._retn() );
455
456   return res;
457 }
458
459 //=================================================================================
460 // function : addSubshapeToStudy
461 // purpose  : virtual method to add new SubObjects if local selection
462 //=================================================================================
463 void BasicGUI_VectorDlg::addSubshapesToStudy()
464 {
465   QMap<QString, GEOM::GEOM_Object_var> objMap;
466
467 switch (getConstructorId())
468   {
469   case 0:
470      objMap[GroupPoints->LineEdit1->text()] = myPoint1;
471      objMap[GroupPoints->LineEdit2->text()] = myPoint2;
472     break;
473   case 1:
474     return;
475   }
476  addSubshapesToFather( objMap );
477 }