Salome HOME
GEOM: It should be forbidden creation of primitives with zero dimensions.
[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 // File   : PrimitiveGUI_TorusDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "PrimitiveGUI_TorusDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 // OCCT Includes
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS.hxx>
41 #include <TopExp.hxx>
42 #include <TColStd_IndexedMapOfInteger.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
44
45 #include <GEOMImpl_Types.hxx>
46
47 //=================================================================================
48 // class    : PrimitiveGUI_TorusDlg()
49 // purpose  : Constructs a PrimitiveGUI_TorusDlg which is a child of 'parent', with the
50 //            name 'name' and widget flags set to 'f'.
51 //            The dialog will by default be modeless, unless you set 'modal' to
52 //            TRUE to construct a modal dialog.
53 //=================================================================================
54 PrimitiveGUI_TorusDlg::PrimitiveGUI_TorusDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
55                                               bool modal, Qt::WindowFlags fl)
56   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
57     myInitial(true)
58 {
59   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
60   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TORUS_PV")));
61   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TORUS_DXYZ")));
62   QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
63
64   setWindowTitle(tr("GEOM_TORUS_TITLE"));
65
66   /***************************************************************/
67   mainFrame()->GroupConstructors->setTitle(tr("GEOM_TORUS"));
68   mainFrame()->RadioButton1->setIcon(image0);
69   mainFrame()->RadioButton2->setIcon(image1);
70   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
71   mainFrame()->RadioButton3->close();
72
73   GroupPoints = new DlgRef_2Sel2Spin(centralWidget());
74   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
75   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_POINT"));
76   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
77   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS_I").arg(1));
78   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_I").arg(2));
79   GroupPoints->PushButton1->setIcon(image2);
80   GroupPoints->PushButton2->setIcon(image2);
81
82   GroupDimensions = new DlgRef_2Spin(centralWidget());
83   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
84   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS_I").arg(1));
85   GroupDimensions->TextLabel2->setText(tr("GEOM_RADIUS_I").arg(2));
86
87   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
88   layout->setMargin(0); layout->setSpacing(6);
89   layout->addWidget(GroupPoints);
90   layout->addWidget(GroupDimensions);
91   /***************************************************************/
92
93   setHelpFileName("create_torus_page.html");
94
95   Init();
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 // function : Init()
109 // purpose  :
110 //=================================================================================
111 void PrimitiveGUI_TorusDlg::Init()
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 & initial values
118   initSpinBox(GroupPoints->SpinBox_DX, 0.000001, COORD_MAX, step, 6); // VSR: TODO: DBL_DIGITS_DISPLAY
119   initSpinBox(GroupPoints->SpinBox_DY, 0.000001, COORD_MAX, step, 6); // VSR: TODO: DBL_DIGITS_DISPLAY
120   initSpinBox(GroupDimensions->SpinBox_DX, 0.000001, COORD_MAX, step, 6); // VSR: TODO: DBL_DIGITS_DISPLAY
121   initSpinBox(GroupDimensions->SpinBox_DY, 0.000001, COORD_MAX, step, 6); // VSR: TODO: DBL_DIGITS_DISPLAY
122
123   // init variables
124   GroupPoints->LineEdit1->setReadOnly(true);
125   GroupPoints->LineEdit2->setReadOnly(true);
126
127   GroupPoints->LineEdit1->setText("");
128   GroupPoints->LineEdit2->setText("");
129   myPoint = myDir = GEOM::GEOM_Object::_nil();
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(this, SIGNAL(constructorsClicked(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)), this, SLOT(SetDoubleSpinBoxStep(double)));
154
155   initName(tr("GEOM_TORUS"));
156
157   setConstructorId(1); // simplest constructor
158   ConstructorsClicked(1);
159 }
160
161 //=================================================================================
162 // function : SetDoubleSpinBoxStep()
163 // purpose  : Double spin box management
164 //=================================================================================
165 void PrimitiveGUI_TorusDlg::SetDoubleSpinBoxStep (double step)
166 {
167   GroupPoints->SpinBox_DX->setSingleStep(step);
168   GroupPoints->SpinBox_DY->setSingleStep(step);
169   GroupDimensions->SpinBox_DX->setSingleStep(step);
170   GroupDimensions->SpinBox_DY->setSingleStep(step);
171 }
172
173 //=================================================================================
174 // function : ConstructorsClicked()
175 // purpose  : Radio button management
176 //=================================================================================
177 void PrimitiveGUI_TorusDlg::ConstructorsClicked (int constructorId)
178 {
179   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
180
181   switch (constructorId) {
182   case 0:
183     {
184       GroupDimensions->hide();
185       GroupPoints->show();
186
187       GroupPoints->PushButton1->click();
188       break;
189     }
190   case 1:
191     {
192       GroupPoints->hide();
193       GroupDimensions->show();
194       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
195       globalSelection(); // close local contexts, if any
196       break;
197     }
198   }
199
200   qApp->processEvents();
201   updateGeometry();
202   resize(minimumSize());
203
204   if (myInitial) {
205     myInitial = false;
206     if (constructorId == 0) {
207       // on dialog initialization we init the first field with a selected object (if any)
208       SelectionIntoArgument();
209     }
210     else {
211       displayPreview();
212     }
213   }
214   else {
215     displayPreview();
216   }
217 }
218
219 //=================================================================================
220 // function : ClickOnOk()
221 // purpose  :
222 //=================================================================================
223 void PrimitiveGUI_TorusDlg::ClickOnOk()
224 {
225   if (ClickOnApply())
226     ClickOnCancel();
227 }
228
229 //=================================================================================
230 // function : ClickOnApply()
231 // purpose  :
232 //=================================================================================
233 bool PrimitiveGUI_TorusDlg::ClickOnApply()
234 {
235   if (!onAccept())
236     return false;
237
238   initName();
239   // activate selection and connect selection manager
240   ConstructorsClicked(getConstructorId());
241   return true;
242 }
243
244 //=================================================================================
245 // function : SelectionIntoArgument()
246 // purpose  : Called when selection is changed or on dialog initialization or activation
247 //=================================================================================
248 void PrimitiveGUI_TorusDlg::SelectionIntoArgument()
249 {
250   if (getConstructorId() != 0)
251     return;
252
253   erasePreview();
254   myEditCurrentArgument->setText("");
255
256   if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint = GEOM::GEOM_Object::_nil();
257   else if (myEditCurrentArgument == GroupPoints->LineEdit2) myDir   = GEOM::GEOM_Object::_nil();
258
259   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
260   SALOME_ListIO aSelList;
261   aSelMgr->selectedObjects(aSelList);
262
263   if (aSelList.Extent() != 1)
264     return;
265
266   // nbSel == 1
267   Standard_Boolean testResult = Standard_False;
268   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
269
270   if (!testResult || CORBA::is_nil(aSelectedObject))
271     return;
272
273   QString aName = GEOMBase::GetName(aSelectedObject);
274
275   // Get Selected object if selected subshape
276   TopoDS_Shape aShape;
277   if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
278   {
279     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
280     if (myEditCurrentArgument == GroupPoints->LineEdit2)
281       aNeedType = TopAbs_EDGE;
282
283     TColStd_IndexedMapOfInteger aMap;
284     aSelMgr->GetIndexes(aSelList.First(), aMap);
285     if (aMap.Extent() == 1) { // Local Selection
286       int anIndex = aMap(1);
287       if (aNeedType == TopAbs_EDGE)
288         aName.append(":edge_" + QString::number(anIndex));
289       else
290         aName.append(":vertex_" + QString::number(anIndex));
291
292       //Find SubShape Object in Father
293       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
294
295       if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
296         GEOM::GEOM_IShapesOperations_var aShapesOp =
297           getGeomEngine()->GetIShapesOperations(getStudyId());
298         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
299       }
300       else {
301         aSelectedObject = aFindedObject; // get Object from study
302       }
303     }
304     else { // Global Selection
305       if (aShape.ShapeType() != aNeedType) {
306         aSelectedObject = GEOM::GEOM_Object::_nil();
307         aName = "";
308       }
309     }
310   }
311
312   myEditCurrentArgument->setText(aName);
313
314   // clear selection
315   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
316   myGeomGUI->getApp()->selectionMgr()->clearSelected();
317   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
318           this, SLOT(SelectionIntoArgument()));
319
320   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
321     myPoint = aSelectedObject;
322     if (!myPoint->_is_nil() && myDir->_is_nil())
323       GroupPoints->PushButton2->click();
324   }
325   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
326     myDir = aSelectedObject;
327     if (!myDir->_is_nil() && myPoint->_is_nil())
328       GroupPoints->PushButton1->click();
329   }
330
331   displayPreview();
332 }
333
334 //=================================================================================
335 // function : SetEditCurrentArgument()
336 // purpose  :
337 //=================================================================================
338 void PrimitiveGUI_TorusDlg::SetEditCurrentArgument()
339 {
340   QPushButton* send = (QPushButton*)sender();
341
342   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
343   if (send == GroupPoints->PushButton1) {
344     myEditCurrentArgument = GroupPoints->LineEdit1;
345
346     GroupPoints->PushButton2->setDown(false);
347     GroupPoints->LineEdit2->setEnabled(false);
348
349     globalSelection(GEOM_POINT); // to break previous local selection
350     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
351   }
352   else if (send == GroupPoints->PushButton2) {
353     myEditCurrentArgument = GroupPoints->LineEdit2;
354
355     GroupPoints->PushButton1->setDown(false);
356     GroupPoints->LineEdit1->setEnabled(false);
357
358     globalSelection(GEOM_LINE);  // to break previous local selection
359     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
360   }
361   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
362           this, SLOT(SelectionIntoArgument()));
363
364   // enable line edit
365   myEditCurrentArgument->setEnabled(true);
366   myEditCurrentArgument->setFocus();
367   // after setFocus(), because it will be setDown(false) when loses focus
368   send->setDown(true);
369
370   // seems we need it only to avoid preview disappearing, caused by selection mode change
371   displayPreview();
372 }
373
374 //=================================================================================
375 // function : LineEditReturnPressed()
376 // purpose  :
377 //=================================================================================
378 void PrimitiveGUI_TorusDlg::LineEditReturnPressed()
379 {
380   QLineEdit* send = (QLineEdit*)sender();
381   if (send == GroupPoints->LineEdit1 ||
382       send == GroupPoints->LineEdit2) {
383     myEditCurrentArgument = send;
384     GEOMBase_Skeleton::LineEditReturnPressed();
385   }
386 }
387
388 //=================================================================================
389 // function : ActivateThisDialog()
390 // purpose  :
391 //=================================================================================
392 void PrimitiveGUI_TorusDlg::ActivateThisDialog()
393 {
394   GEOMBase_Skeleton::ActivateThisDialog();
395
396   // reinit, because some selected objects could be removed
397   Init();
398 }
399
400 //=================================================================================
401 // function : enterEvent()
402 // purpose  :
403 //=================================================================================
404 void PrimitiveGUI_TorusDlg::enterEvent (QEvent*)
405 {
406   if (!mainFrame()->GroupConstructors->isEnabled())
407     ActivateThisDialog();
408 }
409
410 //=================================================================================
411 // function : ValueChangedInSpinBox()
412 // purpose  :
413 //=================================================================================
414 void PrimitiveGUI_TorusDlg::ValueChangedInSpinBox()
415 {
416   displayPreview();
417 }
418
419 //=================================================================================
420 // function : createOperation
421 // purpose  :
422 //=================================================================================
423 GEOM::GEOM_IOperations_ptr PrimitiveGUI_TorusDlg::createOperation()
424 {
425   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
426 }
427
428 //=================================================================================
429 // function : isValid
430 // purpose  :
431 //=================================================================================
432 bool PrimitiveGUI_TorusDlg::isValid (QString&)
433 {
434   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil()) : true;
435 }
436
437 //=================================================================================
438 // function : execute
439 // purpose  :
440 //=================================================================================
441 bool PrimitiveGUI_TorusDlg::execute (ObjectList& objects)
442 {
443   bool res = false;
444
445   GEOM::GEOM_Object_var anObj;
446
447   switch (getConstructorId()) {
448   case 0:
449     if (!CORBA::is_nil(myPoint) && !CORBA::is_nil(myDir)) {
450       anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->
451         MakeTorusPntVecRR(myPoint, myDir, getRadius1(), getRadius2());
452       res = true;
453     }
454     break;
455   case 1:
456     anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->
457       MakeTorusRR(getRadius1(), getRadius2());
458     res = true;
459     break;
460   }
461
462   if (!anObj->_is_nil())
463     objects.push_back(anObj._retn());
464
465   return res;
466 }
467
468 //=================================================================================
469 // function : getRadius1()
470 // purpose  :
471 //=================================================================================
472 double PrimitiveGUI_TorusDlg::getRadius1() const
473 {
474   int aConstructorId = getConstructorId();
475   if (aConstructorId == 0)
476     return GroupPoints->SpinBox_DX->value();
477   else if (aConstructorId == 1)
478     return GroupDimensions->SpinBox_DX->value();
479   return 0;
480 }
481
482 //=================================================================================
483 // function : getRadius2()
484 // purpose  :
485 //=================================================================================
486 double PrimitiveGUI_TorusDlg::getRadius2() const
487 {
488   int aConstructorId = getConstructorId();
489   if (aConstructorId == 0)
490     return GroupPoints->SpinBox_DY->value();
491   else if (aConstructorId == 1)
492     return GroupDimensions->SpinBox_DY->value();
493   return 0;
494 }
495
496 //=================================================================================
497 // function : addSubshapeToStudy
498 // purpose  : virtual method to add new SubObjects if local selection
499 //=================================================================================
500 void PrimitiveGUI_TorusDlg::addSubshapesToStudy()
501 {
502   QMap<QString, GEOM::GEOM_Object_var> objMap;
503
504   switch (getConstructorId()) {
505   case 0:
506     objMap[GroupPoints->LineEdit1->text()] = myPoint;
507     objMap[GroupPoints->LineEdit2->text()] = myDir;
508     break;
509   case 1:
510     return;
511   }
512   addSubshapesToFather(objMap);
513 }