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