]> SALOME platform Git repositories - modules/geom.git/blob - src/GenerationGUI/GenerationGUI_RevolDlg.cxx
Salome HOME
Issue 0020580: improved validation in integer and double spin boxes, possibility...
[modules/geom.git] / src / GenerationGUI / GenerationGUI_RevolDlg.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   : GenerationGUI_RevolDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25 //
26 #include "GenerationGUI_RevolDlg.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 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS.hxx>
40 #include <TopExp.hxx>
41 #include <TColStd_IndexedMapOfInteger.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
43 #include <TopExp_Explorer.hxx>
44
45 #include <GEOMImpl_Types.hxx>
46
47 //=================================================================================
48 // class    : GenerationGUI_RevolDlg()
49 // purpose  : Constructs a GenerationGUI_RevolDlg 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 GenerationGUI_RevolDlg::GenerationGUI_RevolDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
55                                                 bool modal, Qt::WindowFlags fl)
56   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl)
57 {
58   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_REVOL")));
59   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
60
61   setWindowTitle(tr("GEOM_REVOLUTION_TITLE"));
62
63   /***************************************************************/
64   mainFrame()->GroupConstructors->setTitle(tr("GEOM_REVOLUTION"));
65   mainFrame()->RadioButton1->setIcon(image0);
66   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
67   mainFrame()->RadioButton2->close();
68   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
69   mainFrame()->RadioButton3->close();
70   myBothway = false;
71
72   GroupPoints = new DlgRef_2Sel1Spin2Check(centralWidget());
73   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
74   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
75   GroupPoints->TextLabel2->setText(tr("GEOM_AXIS"));
76   GroupPoints->TextLabel3->setText(tr("GEOM_ANGLE"));
77   GroupPoints->PushButton1->setIcon(image1);
78   GroupPoints->PushButton2->setIcon(image1);
79   GroupPoints->LineEdit1->setReadOnly(true);
80   GroupPoints->LineEdit2->setReadOnly(true);
81   GroupPoints->CheckButton1->setText(tr("GEOM_BOTHWAY"));
82   GroupPoints->CheckButton2->setText(tr("GEOM_REVERSE"));
83
84   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
85   layout->setMargin(0); layout->setSpacing(6);
86   layout->addWidget(GroupPoints);
87   /***************************************************************/
88
89   setHelpFileName("create_revolution_page.html");
90
91   // Initialisation
92   Init();
93 }
94
95 //=================================================================================
96 // function : ~GenerationGUI_RevolDlg()
97 // purpose  : Destroys the object and frees any allocated resources
98 //=================================================================================
99 GenerationGUI_RevolDlg::~GenerationGUI_RevolDlg()
100 {
101   // no need to delete child widgets, Qt does it all for us
102 }
103
104 //=================================================================================
105 // function : Init()
106 // purpose  :
107 //=================================================================================
108 void GenerationGUI_RevolDlg::Init()
109 {
110   // min, max, step and decimals for spin boxes & initial values
111   double SpecificStep = 5;
112   initSpinBox(GroupPoints->SpinBox_DX, -360.0, 360.0, SpecificStep, "angle_precision" );
113   GroupPoints->SpinBox_DX->setValue(45.0);
114
115   // init variables
116   GroupPoints->LineEdit1->setReadOnly(true);
117   GroupPoints->LineEdit2->setReadOnly(true);
118
119   GroupPoints->LineEdit1->setText("");
120   GroupPoints->LineEdit2->setText("");
121   myAxis = GEOM::GEOM_Object::_nil();
122   myOkBase = myOkAxis = false;
123
124   // signals and slots connections
125   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
126   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
127
128   connect(GroupPoints->PushButton1,  SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
129   connect(GroupPoints->PushButton2,  SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
130
131   connect(GroupPoints->LineEdit1,    SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
132   connect(GroupPoints->LineEdit2,    SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
133
134   connect(GroupPoints->SpinBox_DX,   SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
135   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)),        this, SLOT(onBothway()));
136   connect(GroupPoints->CheckButton2, SIGNAL(toggled(bool)),        this, SLOT(onReverse()));
137
138   // san: We don't need this, as the default step value is not used in this dialog box
139   //connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
140
141   initName(tr("GEOM_REVOLUTION"));
142   resize(100,100);
143
144   GroupPoints->PushButton1->click();
145   SelectionIntoArgument();
146 }
147
148 //=================================================================================
149 // function : SetDoubleSpinBoxStep()
150 // purpose  : Double spin box management
151 //=================================================================================
152 void GenerationGUI_RevolDlg::SetDoubleSpinBoxStep (double step)
153 {
154   GroupPoints->SpinBox_DX->setSingleStep(step);
155 }
156
157 //=================================================================================
158 // function : ClickOnOk()
159 // purpose  :
160 //=================================================================================
161 void GenerationGUI_RevolDlg::ClickOnOk()
162 {
163   if (ClickOnApply())
164     ClickOnCancel();
165 }
166
167 //=================================================================================
168 // function : ClickOnApply()
169 // purpose  :
170 //=================================================================================
171 bool GenerationGUI_RevolDlg::ClickOnApply()
172 {
173   if (!onAccept())
174     return false;
175
176   initName();
177   // activate selection and connect selection manager
178   GroupPoints->PushButton1->click();
179   return true;
180 }
181
182 //=======================================================================
183 //function : isAcceptableBase
184 //purpose  : return true if theBase can be used as algo argument
185 //=======================================================================
186 static bool isAcceptableBase (const TopoDS_Shape& theBase)
187 {
188   switch (theBase.ShapeType()) {
189   case TopAbs_VERTEX:
190   case TopAbs_EDGE:
191   case TopAbs_WIRE:
192   case TopAbs_FACE:
193   case TopAbs_SHELL:
194     return true;
195   case TopAbs_SOLID:
196   case TopAbs_COMPSOLID:
197     return false;
198   case TopAbs_COMPOUND: {
199     TopExp_Explorer exp(theBase, TopAbs_SOLID);
200     return !exp.More();
201   }
202   default:
203     return false;
204   }
205   return false;
206 }
207
208 //=================================================================================
209 // function : SelectionIntoArgument()
210 // purpose  : Called when selection is changed or on dialog initialization or activation
211 //=================================================================================
212 void GenerationGUI_RevolDlg::SelectionIntoArgument()
213 {
214   erasePreview();
215   myEditCurrentArgument->setText("");
216   if      (myEditCurrentArgument == GroupPoints->LineEdit1) myOkBase = false;
217   else if (myEditCurrentArgument == GroupPoints->LineEdit2) myOkAxis = false;
218
219   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
220   SALOME_ListIO aSelList;
221   aSelMgr->selectedObjects(aSelList);
222
223   if (aSelList.Extent() < 1)
224     return;
225
226   Standard_Boolean testResult = Standard_False;
227   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
228   QString aName = GEOMBase::GetName(aSelectedObject);
229
230   if (!testResult || aSelectedObject->_is_nil())
231     return;
232
233   TopoDS_Shape S;
234   if (!GEOMBase::GetShape(aSelectedObject, S) || S.IsNull())
235     return;
236
237   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
238     myOkBase = false;
239     if (aSelList.Extent() > 1)
240       aName = QString( "%1_objects").arg( aSelList.Extent() );
241
242     if ( aSelList.Extent() > 0 ) {
243       GEOMBase::ConvertListOfIOInListOfGO(aSelList, myBaseObjects, true);
244       // check base shapes
245       for (int i=0; i < myBaseObjects.length(); i++) {
246         GEOMBase::GetShape(myBaseObjects[i], S);
247         if (!isAcceptableBase(S))
248           return;
249       }
250       myEditCurrentArgument->setText( aName );
251       myOkBase = true;
252     }
253   }
254   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
255     TColStd_IndexedMapOfInteger aMap;
256     aSelMgr->GetIndexes(aSelList.First(), aMap);
257     if (aMap.Extent() == 1) {
258       int anIndex = aMap(1);
259       aName.append(":edge_" + QString::number(anIndex));
260
261       //Find SubShape Object in Father
262       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
263
264       if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
265         GEOM::GEOM_IShapesOperations_var aShapesOp =
266           getGeomEngine()->GetIShapesOperations(getStudyId());
267         myAxis = aShapesOp->GetSubShape(aSelectedObject, anIndex);
268         myOkAxis = true;
269       } else {
270         myAxis = aFindedObject;
271         myOkAxis = true;
272       }
273     } else {
274       myOkAxis = true;
275       if (S.ShapeType() != TopAbs_EDGE) {
276         aSelectedObject = GEOM::GEOM_Object::_nil();
277         aName = "";
278         myOkAxis = false;
279       }
280       myAxis = aSelectedObject;
281     }
282     myEditCurrentArgument->setText(aName);
283     if (myOkAxis && !myOkBase)
284       GroupPoints->PushButton1->click();
285   }
286
287   displayPreview();
288 }
289
290 //=================================================================================
291 // function : SetEditCurrentArgument()
292 // purpose  :
293 //=================================================================================
294 void GenerationGUI_RevolDlg::SetEditCurrentArgument()
295 {
296   QPushButton* send = (QPushButton*)sender();
297
298   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
299   globalSelection(GEOM_ALLSHAPES);
300   if (send == GroupPoints->PushButton1) {
301     myEditCurrentArgument = GroupPoints->LineEdit1;
302     GroupPoints->PushButton2->setDown(false);
303     GroupPoints->LineEdit2->setEnabled(false);
304   }
305   else if (send == GroupPoints->PushButton2) {
306     myEditCurrentArgument = GroupPoints->LineEdit2;
307     GroupPoints->PushButton1->setDown(false);
308     GroupPoints->LineEdit1->setEnabled(false);
309     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
310   }
311   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
312           this, SLOT(SelectionIntoArgument()));
313
314   // enable line edit
315   myEditCurrentArgument->setEnabled(true);
316   myEditCurrentArgument->setFocus();
317   // after setFocus(), because it will be setDown(false) when loses focus
318   send->setDown(true);
319
320   // seems we need it only to avoid preview disappearing, caused by selection mode change
321   displayPreview();
322 }
323
324 //=================================================================================
325 // function : LineEditReturnPressed()
326 // purpose  :
327 //=================================================================================
328 void GenerationGUI_RevolDlg::LineEditReturnPressed()
329 {
330   QLineEdit* send = (QLineEdit*)sender();
331   if (send == GroupPoints->LineEdit1 ||
332       send == GroupPoints->LineEdit2) {
333     myEditCurrentArgument = send;
334     GEOMBase_Skeleton::LineEditReturnPressed();
335   }
336 }
337
338 //=================================================================================
339 // function : ActivateThisDialog()
340 // purpose  :
341 //=================================================================================
342 void GenerationGUI_RevolDlg::ActivateThisDialog()
343 {
344   GEOMBase_Skeleton::ActivateThisDialog();
345   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
346            this, SLOT( SelectionIntoArgument() ) );
347
348   displayPreview();
349 }
350
351 //=================================================================================
352 // function : enterEvent()
353 // purpose  :
354 //=================================================================================
355 void GenerationGUI_RevolDlg::enterEvent (QEvent*)
356 {
357   if (!mainFrame()->GroupConstructors->isEnabled())
358     ActivateThisDialog();
359 }
360
361 //=================================================================================
362 // function : ValueChangedInSpinBox()
363 // purpose  :
364 //=================================================================================
365 void GenerationGUI_RevolDlg::ValueChangedInSpinBox()
366 {
367   displayPreview();
368 }
369
370 //=================================================================================
371 // function : getAngle()
372 // purpose  :
373 //=================================================================================
374 double GenerationGUI_RevolDlg::getAngle() const
375 {
376   return GroupPoints->SpinBox_DX->value();
377 }
378
379 //=================================================================================
380 // function : createOperation
381 // purpose  :
382 //=================================================================================
383 GEOM::GEOM_IOperations_ptr GenerationGUI_RevolDlg::createOperation()
384 {
385   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
386 }
387
388 //=================================================================================
389 // function : isValid
390 // purpose  :
391 //=================================================================================
392 bool GenerationGUI_RevolDlg::isValid (QString& msg)
393 {
394   bool ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() );
395   return myOkBase && myOkAxis && ok;
396 }
397
398 //=================================================================================
399 // function : execute
400 // purpose  :
401 //=================================================================================
402 bool GenerationGUI_RevolDlg::execute (ObjectList& objects)
403 {
404   GEOM::GEOM_Object_var anObj, aBase;
405   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
406
407   for (int i=0; i < myBaseObjects.length(); i++) {
408     aBase = myBaseObjects[i];
409
410     if (!myBothway)
411       anObj = anOper->MakeRevolutionAxisAngle(aBase, myAxis, getAngle() * PI180);
412     else
413       anObj = anOper->MakeRevolutionAxisAngle2Ways(aBase, myAxis, getAngle() * PI180);
414     
415     if (!anObj->_is_nil()) {
416       if (!IsPreview()) {
417         QStringList aParameters;
418         aParameters << GroupPoints->SpinBox_DX->text();
419         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
420       }
421       objects.push_back(anObj._retn());
422     }
423   }
424
425   return true;
426 }
427
428 //=================================================================================
429 // function :  onReverse()
430 // purpose  :
431 //=================================================================================
432 void GenerationGUI_RevolDlg::onReverse()
433 {
434   double anOldValue = GroupPoints->SpinBox_DX->value();
435   GroupPoints->SpinBox_DX->setValue(-anOldValue);
436 }
437
438 //=================================================================================
439 // function :  onBothway()
440 // purpose  :
441 //=================================================================================
442 void GenerationGUI_RevolDlg::onBothway()
443 {
444   bool anOldValue = myBothway;
445   myBothway = !anOldValue;
446   GroupPoints->CheckButton2->setEnabled(!myBothway);
447   displayPreview();
448 }
449
450 //=================================================================================
451 // function : addSubshapeToStudy
452 // purpose  : virtual method to add new SubObjects if local selection
453 //=================================================================================
454 void GenerationGUI_RevolDlg::addSubshapesToStudy()
455 {
456   QMap<QString, GEOM::GEOM_Object_var> objMap;
457
458   objMap[GroupPoints->LineEdit2->text()] = myAxis;
459
460   addSubshapesToFather(objMap);
461 }
462
463 //=================================================================================
464 // function : extractPrefix
465 // purpose  : redefined from GEOMBase_Helper class
466 //=================================================================================
467 bool GenerationGUI_RevolDlg::extractPrefix() const
468 {
469   return myBaseObjects.length() > 1;
470 }