Salome HOME
Fix memory leaks
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_CylinderDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : PrimitiveGUI_CylinderDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25 //
26 #include "PrimitiveGUI_CylinderDlg.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_CylinderDlg()
49 // purpose  : Constructs a PrimitiveGUI_CylinderDlg 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_CylinderDlg::PrimitiveGUI_CylinderDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
55                                                     bool modal, Qt::WindowFlags fl)
56   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
57     myInitial(true)
58 {
59   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_CYLINDER_PV")));
60   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_CYLINDER_DXYZ")));
61   QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
62
63   setWindowTitle(tr("GEOM_CYLINDER_TITLE"));
64
65   /***************************************************************/
66   mainFrame()->GroupConstructors->setTitle(tr("GEOM_CYLINDER"));
67   mainFrame()->RadioButton1->setIcon(image0);
68   mainFrame()->RadioButton2->setIcon(image1);
69   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
70   mainFrame()->RadioButton3->close();
71
72   GroupPoints = new DlgRef_2Sel2Spin(centralWidget());
73   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
74   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_POINT"));
75   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
76   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS"));
77   GroupPoints->TextLabel4->setText(tr("GEOM_HEIGHT"));
78   GroupPoints->PushButton1->setIcon(image2);
79   GroupPoints->PushButton2->setIcon(image2);
80
81   GroupDimensions = new DlgRef_2Spin(centralWidget());
82   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
83   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS"));
84   GroupDimensions->TextLabel2->setText(tr("GEOM_HEIGHT"));
85
86   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
87   layout->setMargin(0); layout->setSpacing(6);
88   layout->addWidget(GroupPoints);
89   layout->addWidget(GroupDimensions);
90   /***************************************************************/
91
92   setHelpFileName("create_cylinder_page.html");
93
94   Init();
95 }
96
97 //=================================================================================
98 // function : ~PrimitiveGUI_CylinderDlg()
99 // purpose  : Destroys the object and frees any allocated resources
100 //=================================================================================
101 PrimitiveGUI_CylinderDlg::~PrimitiveGUI_CylinderDlg()
102 {
103   // no need to delete child widgets, Qt does it all for us
104 }
105
106 //=================================================================================
107 // function : Init()
108 // purpose  :
109 //=================================================================================
110 void PrimitiveGUI_CylinderDlg::Init()
111 {
112   // Get setting of step value from file configuration
113   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
114   double step = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
115
116   // min, max, step and decimals for spin boxes & initial values
117   initSpinBox(GroupPoints->SpinBox_DX, 0.00001, COORD_MAX, step, 5); // VSR: TODO: DBL_DIGITS_DISPLAY
118   initSpinBox(GroupPoints->SpinBox_DY, 0.00001, COORD_MAX, step, 5); // VSR: TODO: DBL_DIGITS_DISPLAY
119   initSpinBox(GroupDimensions->SpinBox_DX, 0.00001, COORD_MAX, step, 5); // VSR: TODO: DBL_DIGITS_DISPLAY
120   initSpinBox(GroupDimensions->SpinBox_DY, 0.00001, COORD_MAX, step, 5); // VSR: TODO: DBL_DIGITS_DISPLAY
121
122   // init variables
123   myEditCurrentArgument = GroupPoints->LineEdit1;
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   double aRadius(100.0), aHeight(300.0);
132   GroupPoints->SpinBox_DX->setValue(aRadius);
133   GroupPoints->SpinBox_DY->setValue(aHeight);
134   GroupDimensions->SpinBox_DX->setValue(aRadius);
135   GroupDimensions->SpinBox_DY->setValue(aHeight);
136
137   // signals and slots connections
138   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
139   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
140
141   connect(this,          SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
142
143   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
144   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
145
146   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
147   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
148
149   connect(GroupPoints->SpinBox_DX,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
150   connect(GroupPoints->SpinBox_DY,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
151   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
152   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
153
154   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
155
156   initName(tr("GEOM_CYLINDER"));
157
158   setConstructorId(1); // simplest constructor
159   ConstructorsClicked(1);
160 }
161
162 //=================================================================================
163 // function : SetDoubleSpinBoxStep()
164 // purpose  : Double spin box management
165 //=================================================================================
166 void PrimitiveGUI_CylinderDlg::SetDoubleSpinBoxStep (double step)
167 {
168   GroupPoints->SpinBox_DX->setSingleStep(step);
169   GroupPoints->SpinBox_DY->setSingleStep(step);
170   GroupDimensions->SpinBox_DX->setSingleStep(step);
171   GroupDimensions->SpinBox_DY->setSingleStep(step);
172 }
173
174 //=================================================================================
175 // function : ConstructorsClicked()
176 // purpose  : Radio button management
177 //=================================================================================
178 void PrimitiveGUI_CylinderDlg::ConstructorsClicked (int constructorId)
179 {
180   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
181
182   switch (constructorId) {
183   case 0:
184     {
185       GroupDimensions->hide();
186       GroupPoints->show();
187
188       GroupPoints->PushButton1->click();
189       break;
190     }
191   case 1:
192     {
193       GroupPoints->hide();
194       GroupDimensions->show();
195       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
196       globalSelection(); // close local contexts, if any
197       break;
198     }
199   }
200
201   qApp->processEvents();
202   updateGeometry();
203   resize(minimumSizeHint());
204
205   if (myInitial) {
206     myInitial = false;
207     if (constructorId == 0) {
208       // on dialog initialization we init the first field with a selected object (if any)
209       SelectionIntoArgument();
210     }
211     else {
212       displayPreview();
213     }
214   }
215   else {
216     displayPreview();
217   }
218 }
219
220 //=================================================================================
221 // function : ClickOnOk()
222 // purpose  :
223 //=================================================================================
224 void PrimitiveGUI_CylinderDlg::ClickOnOk()
225 {
226   if (ClickOnApply())
227     ClickOnCancel();
228 }
229
230 //=================================================================================
231 // function : ClickOnApply()
232 // purpose  :
233 //=================================================================================
234 bool PrimitiveGUI_CylinderDlg::ClickOnApply()
235 {
236   if (!onAccept())
237     return false;
238
239   initName();
240   // activate selection and connect selection manager
241   ConstructorsClicked(getConstructorId());
242   return true;
243 }
244
245 //=================================================================================
246 // function : SelectionIntoArgument()
247 // purpose  : Called when selection is changed or on dialog initialization or activation
248 //=================================================================================
249 void PrimitiveGUI_CylinderDlg::SelectionIntoArgument()
250 {
251   if (getConstructorId() != 0)
252     return;
253
254   erasePreview();
255   myEditCurrentArgument->setText("");
256
257   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
258   SALOME_ListIO aSelList;
259   aSelMgr->selectedObjects(aSelList);
260
261   if (aSelList.Extent() != 1) {
262     if (myEditCurrentArgument == GroupPoints->LineEdit1)
263       myPoint = GEOM::GEOM_Object::_nil();
264     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
265       myDir = GEOM::GEOM_Object::_nil();
266     return;
267   }
268
269   // nbSel == 1
270   Standard_Boolean testResult = Standard_False;
271   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
272
273   if (!testResult || CORBA::is_nil(aSelectedObject))
274     return;
275
276   QString aName = GEOMBase::GetName(aSelectedObject);
277
278   // Get Selected object if selected subshape
279   TopoDS_Shape aShape;
280   if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
281   {
282     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
283     if (myEditCurrentArgument == GroupPoints->LineEdit2)
284       aNeedType = TopAbs_EDGE;
285
286     TColStd_IndexedMapOfInteger aMap;
287     aSelMgr->GetIndexes(aSelList.First(), aMap);
288     if (aMap.Extent() == 1) { // Local Selection
289       int anIndex = aMap(1);
290       if (aNeedType == TopAbs_EDGE)
291         aName.append(":edge_" + QString::number(anIndex));
292       else
293         aName.append(":vertex_" + QString::number(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 =
300           getGeomEngine()->GetIShapesOperations(getStudyId());
301         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
302       }
303       else {
304         aSelectedObject = aFindedObject; // get Object from study
305       }
306     }
307     else { // Global Selection
308       if (aShape.ShapeType() != aNeedType) {
309         aSelectedObject = GEOM::GEOM_Object::_nil();
310         aName = "";
311       }
312     }
313   }
314
315   myEditCurrentArgument->setText(aName);
316
317   // clear selection
318   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
319   myGeomGUI->getApp()->selectionMgr()->clearSelected();
320   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
321           this, SLOT(SelectionIntoArgument()));
322
323   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
324     myPoint = aSelectedObject;
325     if (!myPoint->_is_nil() && myDir->_is_nil())
326       GroupPoints->PushButton2->click();
327   }
328   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
329     myDir = aSelectedObject;
330     if (!myDir->_is_nil() && myPoint->_is_nil())
331       GroupPoints->PushButton1->click();
332   }
333
334   displayPreview();
335 }
336
337 //=================================================================================
338 // function : SetEditCurrentArgument()
339 // purpose  :
340 //=================================================================================
341 void PrimitiveGUI_CylinderDlg::SetEditCurrentArgument()
342 {
343   QPushButton* send = (QPushButton*)sender();
344
345   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
346   if (send == GroupPoints->PushButton1) {
347     myEditCurrentArgument = GroupPoints->LineEdit1;
348
349     GroupPoints->PushButton2->setDown(false);
350     GroupPoints->LineEdit2->setEnabled(false);
351
352     globalSelection(GEOM_POINT); // to break previous local selection
353     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
354   }
355   else if (send == GroupPoints->PushButton2) {
356     myEditCurrentArgument = GroupPoints->LineEdit2;
357
358     GroupPoints->PushButton1->setDown(false);
359     GroupPoints->LineEdit1->setEnabled(false);
360
361     globalSelection(GEOM_LINE);  // to break previous local selection
362     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
363   }
364   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
365           this, SLOT(SelectionIntoArgument()));
366
367   // enable line edit
368   myEditCurrentArgument->setEnabled(true);
369   myEditCurrentArgument->setFocus();
370   // after setFocus(), because it will be setDown(false) when loses focus
371   send->setDown(true);
372
373   // seems we need it only to avoid preview disappearing, caused by selection mode change
374   displayPreview();
375 }
376
377 //=================================================================================
378 // function : LineEditReturnPressed()
379 // purpose  :
380 //=================================================================================
381 void PrimitiveGUI_CylinderDlg::LineEditReturnPressed()
382 {
383   QLineEdit* send = (QLineEdit*)sender();
384   if (send == GroupPoints->LineEdit1 ||
385       send == GroupPoints->LineEdit2) {
386     myEditCurrentArgument = send;
387     GEOMBase_Skeleton::LineEditReturnPressed();
388   }
389 }
390
391 //=================================================================================
392 // function : ActivateThisDialog()
393 // purpose  :
394 //=================================================================================
395 void PrimitiveGUI_CylinderDlg::ActivateThisDialog()
396 {
397   GEOMBase_Skeleton::ActivateThisDialog();
398
399   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
400            this, SLOT( SelectionIntoArgument() ) );
401
402   ConstructorsClicked( getConstructorId() );
403 }
404
405 //=================================================================================
406 // function : enterEvent()
407 // purpose  :
408 //=================================================================================
409 void PrimitiveGUI_CylinderDlg::enterEvent (QEvent*)
410 {
411   if (!mainFrame()->GroupConstructors->isEnabled())
412     ActivateThisDialog();
413 }
414
415 //=================================================================================
416 // function : ValueChangedInSpinBox()
417 // purpose  :
418 //=================================================================================
419 void PrimitiveGUI_CylinderDlg::ValueChangedInSpinBox()
420 {
421   displayPreview();
422 }
423
424 //=================================================================================
425 // function : createOperation
426 // purpose  :
427 //=================================================================================
428 GEOM::GEOM_IOperations_ptr PrimitiveGUI_CylinderDlg::createOperation()
429 {
430   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
431 }
432
433 //=================================================================================
434 // function : isValid
435 // purpose  :
436 //=================================================================================
437 bool PrimitiveGUI_CylinderDlg::isValid (QString& msg)
438 {
439   bool ok = true;
440   if( getConstructorId() == 0 )
441   {
442     ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
443     ok = GroupPoints->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
444   }
445   else if( getConstructorId() == 1 )
446   {
447     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
448     ok = GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
449   }
450   ok = fabs( getHeight() ) > Precision::Confusion() && ok;
451   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil()) && ok : ok;
452 }
453
454 //=================================================================================
455 // function : execute
456 // purpose  :
457 //=================================================================================
458 bool PrimitiveGUI_CylinderDlg::execute (ObjectList& objects)
459 {
460   bool res = false;
461
462   GEOM::GEOM_Object_var anObj;
463
464   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
465
466   switch (getConstructorId()) {
467   case 0:
468     if (!CORBA::is_nil(myPoint) && !CORBA::is_nil(myDir)) {
469       anObj = anOper->MakeCylinderPntVecRH(myPoint, myDir, getRadius(), getHeight());
470       if (!anObj->_is_nil() && !IsPreview())
471       {
472         QStringList aParameters;
473         aParameters << GroupPoints->SpinBox_DX->text();
474         aParameters << GroupPoints->SpinBox_DY->text();
475         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
476       }
477       res = true;
478     }
479     break;
480   case 1:
481     anObj = anOper->MakeCylinderRH(getRadius(), getHeight());
482     if (!anObj->_is_nil() && !IsPreview())
483     {
484       QStringList aParameters;
485       aParameters << GroupDimensions->SpinBox_DX->text();
486       aParameters << GroupDimensions->SpinBox_DY->text();
487       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
488     }
489     res = true;
490     break;
491   }
492
493   if (!anObj->_is_nil())
494     objects.push_back(anObj._retn());
495
496   return res;
497 }
498
499 //=================================================================================
500 // function : getRadius()
501 // purpose  :
502 //=================================================================================
503 double PrimitiveGUI_CylinderDlg::getRadius() const
504 {
505   int aConstructorId = getConstructorId();
506   if (aConstructorId == 0)
507     return GroupPoints->SpinBox_DX->value();
508   else if (aConstructorId == 1)
509     return GroupDimensions->SpinBox_DX->value();
510   return 0;
511 }
512
513 //=================================================================================
514 // function : getHeight()
515 // purpose  :
516 //=================================================================================
517 double PrimitiveGUI_CylinderDlg::getHeight() const
518 {
519   int aConstructorId = getConstructorId();
520   if (aConstructorId == 0)
521     return GroupPoints->SpinBox_DY->value();
522   else if (aConstructorId == 1)
523     return GroupDimensions->SpinBox_DY->value();
524   return 0;
525 }
526
527 //=================================================================================
528 // function : addSubshapeToStudy
529 // purpose  : virtual method to add new SubObjects if local selection
530 //=================================================================================
531 void PrimitiveGUI_CylinderDlg::addSubshapesToStudy()
532 {
533   QMap<QString, GEOM::GEOM_Object_var> objMap;
534
535   switch (getConstructorId()) {
536   case 0:
537     objMap[GroupPoints->LineEdit1->text()] = myPoint;
538     objMap[GroupPoints->LineEdit2->text()] = myDir;
539     break;
540   case 1:
541     return;
542   }
543   addSubshapesToFather(objMap);
544 }