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