Salome HOME
Fix for bug 0019870(Import/Export should show current directory).
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_TorusDlg.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   : PrimitiveGUI_TorusDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "PrimitiveGUI_TorusDlg.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_Edge.hxx>
38 #include <TopoDS.hxx>
39 #include <TopExp.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42
43 #include <qlabel.h>
44
45 #include "GEOMImpl_Types.hxx"
46
47 #include "utilities.h"
48
49 //=================================================================================
50 // class    : PrimitiveGUI_TorusDlg()
51 // purpose  : Constructs a PrimitiveGUI_TorusDlg which is a child of 'parent', with the
52 //            name 'name' and widget flags set to 'f'.
53 //            The dialog will by default be modeless, unless you set 'modal' to
54 //            TRUE to construct a modal dialog.
55 //=================================================================================
56 PrimitiveGUI_TorusDlg::PrimitiveGUI_TorusDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
57                                              const char* name, bool modal, WFlags fl)
58   : GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
59                       WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
60 {
61   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
62   QPixmap image0(aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TORUS_PV")));
63   QPixmap image1(aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TORUS_DXYZ")));
64   QPixmap image2(aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
65
66   setCaption(tr("GEOM_TORUS_TITLE"));
67
68   /***************************************************************/
69   GroupConstructors->setTitle(tr("GEOM_TORUS"));
70   RadioButton1->setPixmap(image0);
71   RadioButton2->setPixmap(image1);
72   RadioButton3->close(TRUE);
73
74   GroupPoints = new DlgRef_2Sel2Spin(this, "GroupPoints");
75   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
76   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_POINT"));
77   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
78   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS_I").arg("1"));
79   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_I").arg("2"));
80   GroupPoints->PushButton1->setPixmap(image2);
81   GroupPoints->PushButton2->setPixmap(image2);
82
83   GroupDimensions = new DlgRef_2Spin(this, "GroupDimensions");
84   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
85   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS_I").arg("1"));
86   GroupDimensions->TextLabel2->setText(tr("GEOM_RADIUS_I").arg("2"));
87
88   Layout1->addWidget(GroupPoints, 2, 0);
89   Layout1->addWidget(GroupDimensions, 2, 0);
90   /***************************************************************/
91
92   setHelpFileName("create_torus_page.html");
93
94   Init();
95 }
96
97
98 //=================================================================================
99 // function : ~PrimitiveGUI_TorusDlg()
100 // purpose  : Destroys the object and frees any allocated resources
101 //=================================================================================
102 PrimitiveGUI_TorusDlg::~PrimitiveGUI_TorusDlg()
103 {
104     // no need to delete child widgets, Qt does it all for us
105 }
106
107
108 //=================================================================================
109 // function : Init()
110 // purpose  :
111 //=================================================================================
112 void PrimitiveGUI_TorusDlg::Init()
113 {
114   /* init variables */
115   myEditCurrentArgument = GroupPoints->LineEdit1;
116   GroupPoints->LineEdit1->setReadOnly( true );
117   GroupPoints->LineEdit2->setReadOnly( true );
118
119   myPoint = myDir = GEOM::GEOM_Object::_nil();
120
121   /* Get setting of step value from file configuration */
122   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
123   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
124
125   /* min, max, step and decimals for spin boxes & initial values */
126   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
127   GroupPoints->SpinBox_DY->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
128   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
129   GroupDimensions->SpinBox_DY->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
130
131   GroupPoints->SpinBox_DX->SetValue(300.0);
132   GroupPoints->SpinBox_DY->SetValue(100.0);
133   GroupDimensions->SpinBox_DX->SetValue(300.0);
134   GroupDimensions->SpinBox_DY->SetValue(100.0);
135
136   /* signals and slots connections */
137   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
138   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
139
140   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
141
142   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
143   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
144
145   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
146   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
147
148   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
149   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
150   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
151   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
152
153   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
154           GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
155   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
156           GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
157   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
158           GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
159   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
160           GroupDimensions->SpinBox_DY, SLOT(SetStep(double)));
161
162   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
163           this, SLOT(SelectionIntoArgument()));
164
165   initName( tr( "GEOM_TORUS" ) );
166   ConstructorsClicked(0);
167 }
168
169 //=================================================================================
170 // function : ConstructorsClicked()
171 // purpose  : Radio button management
172 //=================================================================================
173 void PrimitiveGUI_TorusDlg::ConstructorsClicked (int constructorId)
174 {
175   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
176
177   switch (constructorId)
178   {
179   case 0:
180     {
181       globalSelection(); // close local contexts, if any
182       localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
183
184       GroupDimensions->hide();
185       resize(0, 0);
186       GroupPoints->show();
187
188       myEditCurrentArgument = GroupPoints->LineEdit1;
189       GroupPoints->LineEdit1->setText(tr(""));
190       GroupPoints->LineEdit2->setText(tr(""));
191       myPoint = myDir = GEOM::GEOM_Object::_nil();
192
193       connect(myGeomGUI->getApp()->selectionMgr(),
194               SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
195       SelectionIntoArgument();
196
197       break;
198     }
199   case 1:
200     {
201       GroupPoints->hide();
202       globalSelection(); // close local contexts, if any
203       resize(0, 0);
204       GroupDimensions->show();
205
206       break;
207     }
208   }
209
210   displayPreview();
211 }
212
213
214 //=================================================================================
215 // function : ClickOnOk()
216 // purpose  :
217 //=================================================================================
218 void PrimitiveGUI_TorusDlg::ClickOnOk()
219 {
220   if (ClickOnApply())
221     ClickOnCancel();
222 }
223
224
225 //=================================================================================
226 // function : ClickOnApply()
227 // purpose  :
228 //=================================================================================
229 bool PrimitiveGUI_TorusDlg::ClickOnApply()
230 {
231   if (!onAccept())
232     return false;
233
234   initName();
235   return true;
236 }
237
238
239 //=================================================================================
240 // function : SelectionIntoArgument()
241 // purpose  : Called when selection as changed or other case
242 //=================================================================================
243 void PrimitiveGUI_TorusDlg::SelectionIntoArgument()
244 {
245   if (getConstructorId() != 0)
246     return;
247
248   myEditCurrentArgument->setText("");
249
250   if (IObjectCount() != 1)
251   {
252     if (myEditCurrentArgument == GroupPoints->LineEdit1)
253       myPoint = GEOM::GEOM_Object::_nil();
254     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
255       myDir = GEOM::GEOM_Object::_nil();
256     return;
257   }
258
259   /* nbSel == 1 */
260   Standard_Boolean testResult = Standard_False;
261   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult);
262
263   if (!testResult || CORBA::is_nil(aSelectedObject))
264     return;
265
266   QString aName = GEOMBase::GetName(aSelectedObject);
267   TopoDS_Shape aShape;
268   if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
269   {
270     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
271     if (myEditCurrentArgument == GroupPoints->LineEdit2)
272       aNeedType = TopAbs_EDGE;
273
274     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
275     TColStd_IndexedMapOfInteger aMap;
276     aSelMgr->GetIndexes(firstIObject(), aMap);
277     if (aMap.Extent() == 1) // Local Selection
278     {
279       int anIndex = aMap(1);
280       if (aNeedType == TopAbs_EDGE)
281         aName.append(":edge_" + QString::number(anIndex));
282       else
283         aName.append(":vertex_" + QString::number(anIndex));
284
285       //Find SubShape Object in Father
286       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
287
288       if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
289         GEOM::GEOM_IShapesOperations_var aShapesOp =
290           getGeomEngine()->GetIShapesOperations( getStudyId() );
291         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
292       }
293       else
294         aSelectedObject = aFindedObject; // get Object from study
295     }
296     else // Global Selection
297     {
298       if (aShape.ShapeType() != aNeedType) {
299         aSelectedObject = GEOM::GEOM_Object::_nil();
300         aName = "";
301       }
302     }
303   }
304
305   myEditCurrentArgument->setText(aName);
306
307   if (myEditCurrentArgument == GroupPoints->LineEdit1)
308     myPoint = aSelectedObject;
309   else if (myEditCurrentArgument == GroupPoints->LineEdit2)
310     myDir = aSelectedObject;
311
312   displayPreview();
313 }
314
315 //=================================================================================
316 // function : LineEditReturnPressed()
317 // purpose  :
318 //=================================================================================
319 void PrimitiveGUI_TorusDlg::LineEditReturnPressed()
320 {
321   QLineEdit* send = (QLineEdit*)sender();
322   if (send == GroupPoints->LineEdit1 ||
323       send == GroupPoints->LineEdit2)
324   {
325     myEditCurrentArgument = send;
326     GEOMBase_Skeleton::LineEditReturnPressed();
327   }
328 }
329
330
331 //=================================================================================
332 // function : SetEditCurrentArgument()
333 // purpose  :
334 //=================================================================================
335 void PrimitiveGUI_TorusDlg::SetEditCurrentArgument()
336 {
337   QPushButton* send = (QPushButton*)sender();
338   globalSelection( GEOM_POINT ); // to break previous local selection
339
340   if (send == GroupPoints->PushButton1) {
341     myEditCurrentArgument = GroupPoints->LineEdit1;
342     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
343   }
344   else if (send == GroupPoints->PushButton2) {
345     myEditCurrentArgument = GroupPoints->LineEdit2;
346     GEOM::GEOM_Object_var anObj;
347     localSelection( anObj, TopAbs_EDGE );
348   }
349
350   myEditCurrentArgument->setFocus();
351   SelectionIntoArgument();
352 }
353
354
355 //=================================================================================
356 // function : ActivateThisDialog()
357 // purpose  :
358 //=================================================================================
359 void PrimitiveGUI_TorusDlg::ActivateThisDialog()
360 {
361   GEOMBase_Skeleton::ActivateThisDialog();
362
363   connect(myGeomGUI->getApp()->selectionMgr(),
364           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
365
366   ConstructorsClicked( getConstructorId() );
367 }
368
369
370 //=================================================================================
371 // function : enterEvent()
372 // purpose  :
373 //=================================================================================
374 void PrimitiveGUI_TorusDlg::enterEvent(QEvent* e)
375 {
376   if ( !GroupConstructors->isEnabled() )
377     ActivateThisDialog();
378 }
379
380
381 //=================================================================================
382 // function : ValueChangedInSpinBox
383 // purpose  :
384 //=================================================================================
385 void PrimitiveGUI_TorusDlg::ValueChangedInSpinBox()
386 {
387   displayPreview();
388 }
389
390
391 //=================================================================================
392 // function : createOperation
393 // purpose  :
394 //=================================================================================
395 GEOM::GEOM_IOperations_ptr PrimitiveGUI_TorusDlg::createOperation()
396 {
397   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
398 }
399
400 //=================================================================================
401 // function : isValid
402 // purpose  :
403 //=================================================================================
404 bool PrimitiveGUI_TorusDlg::isValid( QString& msg )
405 {
406   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil()) : true;
407 }
408
409 //=================================================================================
410 // function : execute
411 // purpose  :
412 //=================================================================================
413 bool PrimitiveGUI_TorusDlg::execute( ObjectList& objects )
414 {
415   bool res = false;
416
417   GEOM::GEOM_Object_var anObj;
418
419   switch ( getConstructorId() )
420   {
421   case 0:
422     {
423       if (!CORBA::is_nil( myPoint ) && !CORBA::is_nil( myDir ))
424       {
425         anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->
426           MakeTorusPntVecRR(myPoint, myDir, getRadius1(), getRadius2());
427         res = true;
428       }
429       break;
430     }
431   case 1:
432     {
433       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->
434         MakeTorusRR(getRadius1(), getRadius2());
435       res = true;
436       break;
437     }
438   }
439
440   if ( !anObj->_is_nil() )
441     objects.push_back( anObj._retn() );
442
443   return res;
444 }
445
446 //=================================================================================
447 // function : getRadius1()
448 // purpose  :
449 //=================================================================================
450 double PrimitiveGUI_TorusDlg::getRadius1() const
451 {
452   int aConstructorId = getConstructorId();
453   if (aConstructorId == 0)
454     return GroupPoints->SpinBox_DX->GetValue();
455   else if (aConstructorId == 1)
456     return GroupDimensions->SpinBox_DX->GetValue();
457   return 0;
458 }
459
460 //=================================================================================
461 // function : getRadius2()
462 // purpose  :
463 //=================================================================================
464 double PrimitiveGUI_TorusDlg::getRadius2() const
465 {
466   int aConstructorId = getConstructorId();
467   if (aConstructorId == 0)
468     return GroupPoints->SpinBox_DY->GetValue();
469   else if (aConstructorId == 1)
470     return GroupDimensions->SpinBox_DY->GetValue();
471   return 0;
472 }
473
474 //=================================================================================
475 // function : addSubshapeToStudy
476 // purpose  : virtual method to add new SubObjects if local selection
477 //=================================================================================
478 void PrimitiveGUI_TorusDlg::addSubshapesToStudy()
479 {
480   QMap<QString, GEOM::GEOM_Object_var> objMap;
481
482 switch (getConstructorId())
483   {
484   case 0:
485     objMap[GroupPoints->LineEdit1->text()] = myPoint;
486     objMap[GroupPoints->LineEdit2->text()] = myDir;
487     break;
488   case 1:
489     return;
490   }
491  addSubshapesToFather( objMap );
492 }