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