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