Salome HOME
be4d176012e1cfa97fcdffbd4d9cdd679786ca4a
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_ConeDlg.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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 {
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, "length_precision" );
120   initSpinBox(GroupPoints->SpinBox_DY, 0.0, COORD_MAX, step, "length_precision" );
121   initSpinBox(GroupPoints->SpinBox_DZ, 0.00001, COORD_MAX, step, "length_precision" );
122   initSpinBox(GroupDimensions->SpinBox_DX, 0.0, COORD_MAX, step, "length_precision" );
123   initSpinBox(GroupDimensions->SpinBox_DY, 0.0, COORD_MAX, step, "length_precision" );
124   initSpinBox(GroupDimensions->SpinBox_DZ, 0.00001, COORD_MAX, step, "length_precision" );
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.nullify();
133   myDir.nullify();
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->SpinBox_DX,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
153   connect(GroupPoints->SpinBox_DY,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
154   connect(GroupPoints->SpinBox_DZ,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
155   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
156   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
157   connect(GroupDimensions->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
158
159   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
160
161   initName(tr("GEOM_CONE"));
162
163   setConstructorId(1); // simplest constructor
164   ConstructorsClicked(1);
165 }
166
167 //=================================================================================
168 // function : SetDoubleSpinBoxStep()
169 // purpose  : Double spin box management
170 //=================================================================================
171 void PrimitiveGUI_ConeDlg::SetDoubleSpinBoxStep (double step)
172 {
173   GroupPoints->SpinBox_DX->setSingleStep(step);
174   GroupPoints->SpinBox_DY->setSingleStep(step);
175   GroupPoints->SpinBox_DZ->setSingleStep(step);
176   GroupDimensions->SpinBox_DX->setSingleStep(step);
177   GroupDimensions->SpinBox_DY->setSingleStep(step);
178   GroupDimensions->SpinBox_DZ->setSingleStep(step);
179 }
180
181 //=================================================================================
182 // function : ConstructorsClicked()
183 // purpose  : Radio button management
184 //=================================================================================
185 void PrimitiveGUI_ConeDlg::ConstructorsClicked (int constructorId)
186 {
187   switch (constructorId) {
188   case 0:
189     {
190       GroupDimensions->hide();
191       GroupPoints->show();
192
193       GroupPoints->PushButton1->click();
194       break;
195     }
196   case 1:
197     {
198       GroupPoints->hide();
199       GroupDimensions->show();
200       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
201       globalSelection(); // close local contexts, if any
202
203       displayPreview(true);
204       break;
205     }
206   }
207
208   qApp->processEvents();
209   updateGeometry();
210   resize(minimumSizeHint());
211   SelectionIntoArgument();
212
213   displayPreview(true);
214 }
215
216 //=================================================================================
217 // function : ClickOnOk()
218 // purpose  :
219 //=================================================================================
220 void PrimitiveGUI_ConeDlg::ClickOnOk()
221 {
222   setIsApplyAndClose( true );
223   if (ClickOnApply())
224     ClickOnCancel();
225 }
226
227 //=================================================================================
228 // function : ClickOnApply()
229 // purpose  :
230 //=================================================================================
231 bool PrimitiveGUI_ConeDlg::ClickOnApply()
232 {
233   if (!onAccept())
234     return false;
235
236   initName();
237   // activate selection and connect selection manager
238   ConstructorsClicked(getConstructorId());
239   return true;
240 }
241
242 //=================================================================================
243 // function : SelectionIntoArgument()
244 // purpose  : Called when selection is changed or on dialog initialization or activation
245 //=================================================================================
246 void PrimitiveGUI_ConeDlg::SelectionIntoArgument()
247 {
248   if (getConstructorId() != 0)
249     return;
250
251   erasePreview();
252   myEditCurrentArgument->setText("");
253
254   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
255   SALOME_ListIO aSelList;
256   aSelMgr->selectedObjects(aSelList);
257
258   if (aSelList.Extent() != 1) {
259     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint.nullify();
260     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myDir.nullify();
261     return;
262   }
263
264   TopAbs_ShapeEnum aNeedType = myEditCurrentArgument == GroupPoints->LineEdit2 ? TopAbs_EDGE : TopAbs_VERTEX;
265   GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
266   TopoDS_Shape aShape;
267   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
268     QString aName = GEOMBase::GetName( aSelectedObject.get() );
269
270     myEditCurrentArgument->setText(aName);
271     
272     if (myEditCurrentArgument == GroupPoints->LineEdit1) {
273       myPoint = aSelectedObject;
274       if (myPoint && !myDir)
275         GroupPoints->PushButton2->click();
276     }
277     else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
278       myDir = aSelectedObject;
279       if (myDir && !myPoint)
280         GroupPoints->PushButton1->click();
281     }
282
283     if ((myPoint && !myDir) || (myDir && !myPoint)) {
284       // clear selection
285       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
286       myGeomGUI->getApp()->selectionMgr()->clearSelected();
287       connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
288               this, SLOT(SelectionIntoArgument()));
289     }
290   }
291
292   displayPreview(true);
293 }
294
295 //=================================================================================
296 // function : SetEditCurrentArgument()
297 // purpose  :
298 //=================================================================================
299 void PrimitiveGUI_ConeDlg::SetEditCurrentArgument()
300 {
301   QPushButton* send = (QPushButton*)sender();
302
303   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
304   if (send == GroupPoints->PushButton1) {
305     myEditCurrentArgument = GroupPoints->LineEdit1;
306
307     GroupPoints->PushButton2->setDown(false);
308     GroupPoints->LineEdit2->setEnabled(false);
309
310     globalSelection(GEOM_POINT); // to break previous local selection
311     localSelection(TopAbs_VERTEX);
312   }
313   else if (send == GroupPoints->PushButton2) {
314     myEditCurrentArgument = GroupPoints->LineEdit2;
315
316     GroupPoints->PushButton1->setDown(false);
317     GroupPoints->LineEdit1->setEnabled(false);
318
319     globalSelection(GEOM_LINE);// to break previous local selection
320     localSelection(TopAbs_EDGE);
321   }
322   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
323           this, SLOT(SelectionIntoArgument()));
324
325   // enable line edit
326   myEditCurrentArgument->setEnabled(true);
327   myEditCurrentArgument->setFocus();
328   // after setFocus(), because it will be setDown(false) when loses focus
329   send->setDown(true);
330
331   // seems we need it only to avoid preview disappearing, caused by selection mode change
332   displayPreview(true);
333 }
334
335 //=================================================================================
336 // function : ActivateThisDialog()
337 // purpose  :
338 //=================================================================================
339 void PrimitiveGUI_ConeDlg::ActivateThisDialog()
340 {
341   GEOMBase_Skeleton::ActivateThisDialog();
342
343   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
344            this, SLOT( SelectionIntoArgument() ) );
345
346   ConstructorsClicked( getConstructorId() );
347 }
348
349 //=================================================================================
350 // function : enterEvent()
351 // purpose  :
352 //=================================================================================
353 void PrimitiveGUI_ConeDlg::enterEvent (QEvent*)
354 {
355   if (!mainFrame()->GroupConstructors->isEnabled())
356     ActivateThisDialog();
357 }
358
359 //=================================================================================
360 // function : ValueChangedInSpinBox()
361 // purpose  :
362 //=================================================================================
363 void PrimitiveGUI_ConeDlg::ValueChangedInSpinBox()
364 {
365   displayPreview(true);
366 }
367
368 //=================================================================================
369 // function : createOperation
370 // purpose  :
371 //=================================================================================
372 GEOM::GEOM_IOperations_ptr  PrimitiveGUI_ConeDlg::createOperation()
373 {
374   return getGeomEngine()->GetI3DPrimOperations();
375 }
376
377 //=================================================================================
378 // function : isValid
379 // purpose  :
380 //=================================================================================
381 bool  PrimitiveGUI_ConeDlg::isValid (QString& msg)
382 {
383   bool ok = false;
384   if( getConstructorId() == 0 )
385   {
386     ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) &&
387          GroupPoints->SpinBox_DY->isValid( msg, !IsPreview() ) &&
388          GroupPoints->SpinBox_DZ->isValid( msg, !IsPreview() ) &&
389          myPoint && myDir;
390   }
391   else if( getConstructorId() == 1 )
392   {
393     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) &&
394          GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() ) &&
395          GroupDimensions->SpinBox_DZ->isValid( msg, !IsPreview() );
396   }
397   ok = ( getRadius1() > 0 || getRadius2() > 0 ) && qAbs( getHeight() ) > Precision::Confusion() && ok;
398   return ok;
399 }
400
401 //=================================================================================
402 // function : execute
403 // purpose  :
404 //=================================================================================
405 bool PrimitiveGUI_ConeDlg::execute (ObjectList& objects)
406 {
407   bool res = false;
408
409   GEOM::GEOM_Object_var anObj;
410
411   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
412
413   switch (getConstructorId()) {
414   case 0:
415     if ( myPoint && myDir ) {
416       anObj = anOper->MakeConePntVecR1R2H(myPoint.get(), myDir.get(), getRadius1(), getRadius2(), getHeight());
417       if (!anObj->_is_nil() && !IsPreview())
418       {
419         QStringList aParameters;
420         aParameters << GroupPoints->SpinBox_DX->text();
421         aParameters << GroupPoints->SpinBox_DY->text();
422         aParameters << GroupPoints->SpinBox_DZ->text();
423         anObj->SetParameters(aParameters.join(":").toUtf8().constData());
424       }
425       res = true;
426     }
427     break;
428   case 1:
429     anObj = anOper->MakeConeR1R2H(getRadius1(), getRadius2(), getHeight());
430     if (!anObj->_is_nil() && !IsPreview())
431     {
432       QStringList aParameters;
433       aParameters << GroupDimensions->SpinBox_DX->text();
434       aParameters << GroupDimensions->SpinBox_DY->text();
435       aParameters << GroupDimensions->SpinBox_DZ->text();
436       anObj->SetParameters(aParameters.join(":").toUtf8().constData());
437     }
438     res = true;
439     break;
440   }
441
442   if (!anObj->_is_nil())
443     objects.push_back(anObj._retn());
444
445   return res;
446 }
447
448 //=================================================================================
449 // function : getRadius1()
450 // purpose  :
451 //=================================================================================
452 double PrimitiveGUI_ConeDlg::getRadius1() const
453 {
454   int aConstructorId = getConstructorId();
455   if (aConstructorId == 0)
456     return GroupPoints->SpinBox_DX->value();
457   else if (aConstructorId == 1)
458     return GroupDimensions->SpinBox_DX->value();
459   return 0;
460 }
461
462 //=================================================================================
463 // function : getRadius2()
464 // purpose  :
465 //=================================================================================
466 double PrimitiveGUI_ConeDlg::getRadius2() const
467 {
468   int aConstructorId = getConstructorId();
469   if (aConstructorId == 0)
470     return GroupPoints->SpinBox_DY->value();
471   else if (aConstructorId == 1)
472     return GroupDimensions->SpinBox_DY->value();
473   return 0;
474 }
475
476 //=================================================================================
477 // function : getHeight()
478 // purpose  :
479 //=================================================================================
480 double PrimitiveGUI_ConeDlg::getHeight() const
481 {
482   int aConstructorId = getConstructorId();
483   if (aConstructorId == 0)
484     return GroupPoints->SpinBox_DZ->value();
485   else if (aConstructorId == 1)
486     return GroupDimensions->SpinBox_DZ->value();
487   return 0;
488 }
489
490 //=================================================================================
491 // function : addSubshapeToStudy
492 // purpose  : virtual method to add new SubObjects if local selection
493 //=================================================================================
494 void PrimitiveGUI_ConeDlg::addSubshapesToStudy()
495 {
496   if ( getConstructorId() == 0 ) {
497     GEOMBase::PublishSubObject( myPoint.get() );
498     GEOMBase::PublishSubObject( myDir.get() );
499   }
500 }
501
502 //=================================================================================
503 // function : getSourceObjects
504 // purpose  : virtual method to get source objects
505 //=================================================================================
506 QList<GEOM::GeomObjPtr> PrimitiveGUI_ConeDlg::getSourceObjects()
507 {
508   QList<GEOM::GeomObjPtr> res;
509   res << myPoint << myDir;
510   return res;
511 }