Salome HOME
3e10b401da516c2b8a6027ba938590a78e5b994c
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_TorusDlg.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_TorusDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "PrimitiveGUI_TorusDlg.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_TorusDlg()
50 // purpose  : Constructs a PrimitiveGUI_TorusDlg 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_TorusDlg::PrimitiveGUI_TorusDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
56                                               bool modal, Qt::WindowFlags fl)
57   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl)
58 {
59   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
60   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TORUS_PV")));
61   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TORUS_DXYZ")));
62   QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
63
64   setWindowTitle(tr("GEOM_TORUS_TITLE"));
65
66   /***************************************************************/
67   mainFrame()->GroupConstructors->setTitle(tr("GEOM_TORUS"));
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_I").arg(1));
78   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_I").arg(2));
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_I").arg(1));
85   GroupDimensions->TextLabel2->setText(tr("GEOM_RADIUS_I").arg(2));
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_torus_page.html");
94
95   Init();
96 }
97
98 //=================================================================================
99 // function : ~PrimitiveGUI_TorusDlg()
100 // purpose  : Destroys the object and frees any allocated resources
101 //=================================================================================
102 PrimitiveGUI_TorusDlg::~PrimitiveGUI_TorusDlg()
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_TorusDlg::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.000001, COORD_MAX, step, "length_precision" );
119   initSpinBox(GroupPoints->SpinBox_DY, 0.000001, COORD_MAX, step, "length_precision" );
120   initSpinBox(GroupDimensions->SpinBox_DX, 0.000001, COORD_MAX, step, "length_precision" );
121   initSpinBox(GroupDimensions->SpinBox_DY, 0.000001, COORD_MAX, step, "length_precision" );
122
123   // init variables
124   GroupPoints->LineEdit1->setReadOnly(true);
125   GroupPoints->LineEdit2->setReadOnly(true);
126
127   GroupPoints->LineEdit1->setText("");
128   GroupPoints->LineEdit2->setText("");
129   myPoint.nullify();
130   myDir.nullify();
131
132   GroupPoints->SpinBox_DX->setValue(300.0);
133   GroupPoints->SpinBox_DY->setValue(100.0);
134   GroupDimensions->SpinBox_DX->setValue(300.0);
135   GroupDimensions->SpinBox_DY->setValue(100.0);
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->SpinBox_DX,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
147   connect(GroupPoints->SpinBox_DY,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
148   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
149   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
150
151   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
152
153   initName(tr("GEOM_TORUS"));
154
155   setConstructorId(1); // simplest constructor
156   ConstructorsClicked(1);
157 }
158
159 //=================================================================================
160 // function : SetDoubleSpinBoxStep()
161 // purpose  : Double spin box management
162 //=================================================================================
163 void PrimitiveGUI_TorusDlg::SetDoubleSpinBoxStep (double step)
164 {
165   GroupPoints->SpinBox_DX->setSingleStep(step);
166   GroupPoints->SpinBox_DY->setSingleStep(step);
167   GroupDimensions->SpinBox_DX->setSingleStep(step);
168   GroupDimensions->SpinBox_DY->setSingleStep(step);
169 }
170
171 //=================================================================================
172 // function : ConstructorsClicked()
173 // purpose  : Radio button management
174 //=================================================================================
175 void PrimitiveGUI_TorusDlg::ConstructorsClicked (int constructorId)
176 {
177   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
178
179   switch (constructorId) {
180   case 0:
181     {
182       GroupDimensions->hide();
183       GroupPoints->show();
184
185       GroupPoints->PushButton1->click();
186       break;
187     }
188   case 1:
189     {
190       GroupPoints->hide();
191       GroupDimensions->show();
192       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
193       globalSelection(); // close local contexts, if any
194       break;
195     }
196   }
197
198   qApp->processEvents();
199   updateGeometry();
200   resize(minimumSizeHint());
201   SelectionIntoArgument();
202
203   displayPreview(true);
204 }
205
206 //=================================================================================
207 // function : ClickOnOk()
208 // purpose  :
209 //=================================================================================
210 void PrimitiveGUI_TorusDlg::ClickOnOk()
211 {
212   setIsApplyAndClose( true );
213   if (ClickOnApply())
214     ClickOnCancel();
215 }
216
217 //=================================================================================
218 // function : ClickOnApply()
219 // purpose  :
220 //=================================================================================
221 bool PrimitiveGUI_TorusDlg::ClickOnApply()
222 {
223   if (!onAccept())
224     return false;
225
226   initName();
227   // activate selection and connect selection manager
228   ConstructorsClicked(getConstructorId());
229   return true;
230 }
231
232 //=================================================================================
233 // function : SelectionIntoArgument()
234 // purpose  : Called when selection is changed or on dialog initialization or activation
235 //=================================================================================
236 void PrimitiveGUI_TorusDlg::SelectionIntoArgument()
237 {
238   if (getConstructorId() != 0)
239     return;
240
241   erasePreview();
242   myEditCurrentArgument->setText("");
243
244   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
245   SALOME_ListIO aSelList;
246   aSelMgr->selectedObjects(aSelList);
247
248   if (aSelList.Extent() != 1) {
249     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint.nullify();
250     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myDir.nullify();
251     return;
252   }
253
254   TopAbs_ShapeEnum aNeedType = myEditCurrentArgument == GroupPoints->LineEdit2 ? TopAbs_EDGE : TopAbs_VERTEX;
255   GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
256   TopoDS_Shape aShape;
257   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
258     QString aName = GEOMBase::GetName( aSelectedObject.get() );
259
260     myEditCurrentArgument->setText(aName);
261     
262     // clear selection
263     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
264     myGeomGUI->getApp()->selectionMgr()->clearSelected();
265     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
266             this, SLOT(SelectionIntoArgument()));
267     
268     if (myEditCurrentArgument == GroupPoints->LineEdit1) {
269       myPoint = aSelectedObject;
270       if (myPoint && !myDir)
271         GroupPoints->PushButton2->click();
272     }
273     else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
274       myDir = aSelectedObject;
275       if (myDir && !myPoint)
276         GroupPoints->PushButton1->click();
277     }
278   }
279
280   displayPreview(true);
281 }
282
283 //=================================================================================
284 // function : SetEditCurrentArgument()
285 // purpose  :
286 //=================================================================================
287 void PrimitiveGUI_TorusDlg::SetEditCurrentArgument()
288 {
289   QPushButton* send = (QPushButton*)sender();
290
291   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
292   if (send == GroupPoints->PushButton1) {
293     myEditCurrentArgument = GroupPoints->LineEdit1;
294
295     GroupPoints->PushButton2->setDown(false);
296     GroupPoints->LineEdit2->setEnabled(false);
297
298     globalSelection(GEOM_POINT); // to break previous local selection
299     localSelection(TopAbs_VERTEX);
300   }
301   else if (send == GroupPoints->PushButton2) {
302     myEditCurrentArgument = GroupPoints->LineEdit2;
303
304     GroupPoints->PushButton1->setDown(false);
305     GroupPoints->LineEdit1->setEnabled(false);
306
307     globalSelection(GEOM_LINE);  // to break previous local selection
308     localSelection(TopAbs_EDGE);
309   }
310   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
311           this, SLOT(SelectionIntoArgument()));
312
313   // enable line edit
314   myEditCurrentArgument->setEnabled(true);
315   myEditCurrentArgument->setFocus();
316   // after setFocus(), because it will be setDown(false) when loses focus
317   send->setDown(true);
318
319   // seems we need it only to avoid preview disappearing, caused by selection mode change
320   displayPreview(true);
321 }
322
323 //=================================================================================
324 // function : ActivateThisDialog()
325 // purpose  :
326 //=================================================================================
327 void PrimitiveGUI_TorusDlg::ActivateThisDialog()
328 {
329   GEOMBase_Skeleton::ActivateThisDialog();
330
331   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
332            this, SLOT( SelectionIntoArgument() ) );
333   
334   ConstructorsClicked( getConstructorId() );
335 }
336
337 //=================================================================================
338 // function : enterEvent()
339 // purpose  :
340 //=================================================================================
341 void PrimitiveGUI_TorusDlg::enterEvent (QEvent*)
342 {
343   if (!mainFrame()->GroupConstructors->isEnabled())
344     ActivateThisDialog();
345 }
346
347 //=================================================================================
348 // function : ValueChangedInSpinBox()
349 // purpose  :
350 //=================================================================================
351 void PrimitiveGUI_TorusDlg::ValueChangedInSpinBox()
352 {
353   displayPreview(true);
354 }
355
356 //=================================================================================
357 // function : createOperation
358 // purpose  :
359 //=================================================================================
360 GEOM::GEOM_IOperations_ptr PrimitiveGUI_TorusDlg::createOperation()
361 {
362   return getGeomEngine()->GetI3DPrimOperations();
363 }
364
365 //=================================================================================
366 // function : isValid
367 // purpose  :
368 //=================================================================================
369 bool PrimitiveGUI_TorusDlg::isValid (QString& msg)
370 {
371   bool ok = false;
372   if( getConstructorId() == 0 )
373   {
374     ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) &&
375          GroupPoints->SpinBox_DY->isValid( msg, !IsPreview() ) &&
376          myPoint && myDir;
377   }
378   else if( getConstructorId() == 1 )
379   {
380     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) &&
381          GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() );
382   }
383   return ok;
384 }
385
386 //=================================================================================
387 // function : execute
388 // purpose  :
389 //=================================================================================
390 bool PrimitiveGUI_TorusDlg::execute (ObjectList& objects)
391 {
392   bool res = false;
393
394   GEOM::GEOM_Object_var anObj;
395
396   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
397
398   switch (getConstructorId()) {
399   case 0:
400     if ( myPoint && myDir ) {
401       anObj = anOper->MakeTorusPntVecRR(myPoint.get(), myDir.get(), getRadius1(), getRadius2());
402       if (!anObj->_is_nil() && !IsPreview())
403       {
404         QStringList aParameters;
405         aParameters << GroupPoints->SpinBox_DX->text();
406         aParameters << GroupPoints->SpinBox_DY->text();
407         anObj->SetParameters(aParameters.join(":").toUtf8().constData());
408       }
409       res = true;
410     }
411     break;
412   case 1:
413     anObj = anOper->MakeTorusRR(getRadius1(), getRadius2());
414     if (!anObj->_is_nil() && !IsPreview())
415     {
416       QStringList aParameters;
417       aParameters << GroupDimensions->SpinBox_DX->text();
418       aParameters << GroupDimensions->SpinBox_DY->text();
419       anObj->SetParameters(aParameters.join(":").toUtf8().constData());
420     }
421     res = true;
422     break;
423   }
424
425   if (!anObj->_is_nil())
426     objects.push_back(anObj._retn());
427
428   return res;
429 }
430
431 //=================================================================================
432 // function : getRadius1()
433 // purpose  :
434 //=================================================================================
435 double PrimitiveGUI_TorusDlg::getRadius1() const
436 {
437   int aConstructorId = getConstructorId();
438   if (aConstructorId == 0)
439     return GroupPoints->SpinBox_DX->value();
440   else if (aConstructorId == 1)
441     return GroupDimensions->SpinBox_DX->value();
442   return 0;
443 }
444
445 //=================================================================================
446 // function : getRadius2()
447 // purpose  :
448 //=================================================================================
449 double PrimitiveGUI_TorusDlg::getRadius2() const
450 {
451   int aConstructorId = getConstructorId();
452   if (aConstructorId == 0)
453     return GroupPoints->SpinBox_DY->value();
454   else if (aConstructorId == 1)
455     return GroupDimensions->SpinBox_DY->value();
456   return 0;
457 }
458
459 //=================================================================================
460 // function : addSubshapeToStudy
461 // purpose  : virtual method to add new SubObjects if local selection
462 //=================================================================================
463 void PrimitiveGUI_TorusDlg::addSubshapesToStudy()
464 {
465   if ( getConstructorId() == 0 ) {
466     GEOMBase::PublishSubObject( myPoint.get() );
467     GEOMBase::PublishSubObject( myDir.get() );
468   }
469 }
470
471 //=================================================================================
472 // function : getSourceObjects
473 // purpose  : virtual method to get source objects
474 //=================================================================================
475 QList<GEOM::GeomObjPtr> PrimitiveGUI_TorusDlg::getSourceObjects()
476 {
477   QList<GEOM::GeomObjPtr> res;
478   res << myPoint << myDir;
479   return res;
480 }