Salome HOME
Changes for bug 0020673.
[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, 3); // VSR: TODO: DBL_DIGITS_DISPLAY
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   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
139
140   initName(tr("GEOM_REVOLUTION"));
141   resize(100,100);
142
143   GroupPoints->PushButton1->click();
144   SelectionIntoArgument();
145 }
146
147 //=================================================================================
148 // function : SetDoubleSpinBoxStep()
149 // purpose  : Double spin box management
150 //=================================================================================
151 void GenerationGUI_RevolDlg::SetDoubleSpinBoxStep (double step)
152 {
153   GroupPoints->SpinBox_DX->setSingleStep(step);
154 }
155
156 //=================================================================================
157 // function : ClickOnOk()
158 // purpose  :
159 //=================================================================================
160 void GenerationGUI_RevolDlg::ClickOnOk()
161 {
162   if (ClickOnApply())
163     ClickOnCancel();
164 }
165
166 //=================================================================================
167 // function : ClickOnApply()
168 // purpose  :
169 //=================================================================================
170 bool GenerationGUI_RevolDlg::ClickOnApply()
171 {
172   if (!onAccept())
173     return false;
174
175   initName();
176   // activate selection and connect selection manager
177   GroupPoints->PushButton1->click();
178   return true;
179 }
180
181 //=======================================================================
182 //function : isAcceptableBase
183 //purpose  : return true if theBase can be used as algo argument
184 //=======================================================================
185 static bool isAcceptableBase (const TopoDS_Shape& theBase)
186 {
187   switch (theBase.ShapeType()) {
188   case TopAbs_VERTEX:
189   case TopAbs_EDGE:
190   case TopAbs_WIRE:
191   case TopAbs_FACE:
192   case TopAbs_SHELL:
193     return true;
194   case TopAbs_SOLID:
195   case TopAbs_COMPSOLID:
196     return false;
197   case TopAbs_COMPOUND: {
198     TopExp_Explorer exp(theBase, TopAbs_SOLID);
199     return !exp.More();
200   }
201   default:
202     return false;
203   }
204   return false;
205 }
206
207 //=================================================================================
208 // function : SelectionIntoArgument()
209 // purpose  : Called when selection is changed or on dialog initialization or activation
210 //=================================================================================
211 void GenerationGUI_RevolDlg::SelectionIntoArgument()
212 {
213   erasePreview();
214   myEditCurrentArgument->setText("");
215   if      (myEditCurrentArgument == GroupPoints->LineEdit1) myOkBase = false;
216   else if (myEditCurrentArgument == GroupPoints->LineEdit2) myOkAxis = false;
217
218   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
219   SALOME_ListIO aSelList;
220   aSelMgr->selectedObjects(aSelList);
221
222   if (aSelList.Extent() < 1)
223     return;
224
225   Standard_Boolean testResult = Standard_False;
226   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
227   QString aName = GEOMBase::GetName(aSelectedObject);
228
229   if (!testResult || aSelectedObject->_is_nil())
230     return;
231
232   TopoDS_Shape S;
233   if (!GEOMBase::GetShape(aSelectedObject, S) || S.IsNull())
234     return;
235
236   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
237     myOkBase = false;
238     if (aSelList.Extent() > 1)
239       aName = QString( "%1_objects").arg( aSelList.Extent() );
240
241     if ( aSelList.Extent() > 0 ) {
242       GEOMBase::ConvertListOfIOInListOfGO(aSelList, myBaseObjects, true);
243       // check base shapes
244       for (int i=0; i < myBaseObjects.length(); i++) {
245         GEOMBase::GetShape(myBaseObjects[i], S);
246         if (!isAcceptableBase(S))
247           return;
248       }
249       myEditCurrentArgument->setText( aName );
250       myOkBase = true;
251     }
252   }
253   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
254     TColStd_IndexedMapOfInteger aMap;
255     aSelMgr->GetIndexes(aSelList.First(), aMap);
256     if (aMap.Extent() == 1) {
257       int anIndex = aMap(1);
258       aName.append(":edge_" + QString::number(anIndex));
259
260       //Find SubShape Object in Father
261       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
262
263       if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
264         GEOM::GEOM_IShapesOperations_var aShapesOp =
265           getGeomEngine()->GetIShapesOperations(getStudyId());
266         myAxis = aShapesOp->GetSubShape(aSelectedObject, anIndex);
267         myOkAxis = true;
268       } else {
269         myAxis = aFindedObject;
270         myOkAxis = true;
271       }
272     } else {
273       myOkAxis = true;
274       if (S.ShapeType() != TopAbs_EDGE) {
275         aSelectedObject = GEOM::GEOM_Object::_nil();
276         aName = "";
277         myOkAxis = false;
278       }
279       myAxis = aSelectedObject;
280     }
281     myEditCurrentArgument->setText(aName);
282     if (myOkAxis && !myOkBase)
283       GroupPoints->PushButton1->click();
284   }
285
286   displayPreview();
287 }
288
289 //=================================================================================
290 // function : SetEditCurrentArgument()
291 // purpose  :
292 //=================================================================================
293 void GenerationGUI_RevolDlg::SetEditCurrentArgument()
294 {
295   QPushButton* send = (QPushButton*)sender();
296
297   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
298   globalSelection(GEOM_ALLSHAPES);
299   if (send == GroupPoints->PushButton1) {
300     myEditCurrentArgument = GroupPoints->LineEdit1;
301     GroupPoints->PushButton2->setDown(false);
302     GroupPoints->LineEdit2->setEnabled(false);
303   }
304   else if (send == GroupPoints->PushButton2) {
305     myEditCurrentArgument = GroupPoints->LineEdit2;
306     GroupPoints->PushButton1->setDown(false);
307     GroupPoints->LineEdit1->setEnabled(false);
308     localSelection(GEOM::GEOM_Object::_nil(), 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();
321 }
322
323 //=================================================================================
324 // function : LineEditReturnPressed()
325 // purpose  :
326 //=================================================================================
327 void GenerationGUI_RevolDlg::LineEditReturnPressed()
328 {
329   QLineEdit* send = (QLineEdit*)sender();
330   if (send == GroupPoints->LineEdit1 ||
331       send == GroupPoints->LineEdit2) {
332     myEditCurrentArgument = send;
333     GEOMBase_Skeleton::LineEditReturnPressed();
334   }
335 }
336
337 //=================================================================================
338 // function : ActivateThisDialog()
339 // purpose  :
340 //=================================================================================
341 void GenerationGUI_RevolDlg::ActivateThisDialog()
342 {
343   GEOMBase_Skeleton::ActivateThisDialog();
344   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
345            this, SLOT( SelectionIntoArgument() ) );
346
347   displayPreview();
348 }
349
350 //=================================================================================
351 // function : enterEvent()
352 // purpose  :
353 //=================================================================================
354 void GenerationGUI_RevolDlg::enterEvent (QEvent*)
355 {
356   if (!mainFrame()->GroupConstructors->isEnabled())
357     ActivateThisDialog();
358 }
359
360 //=================================================================================
361 // function : ValueChangedInSpinBox()
362 // purpose  :
363 //=================================================================================
364 void GenerationGUI_RevolDlg::ValueChangedInSpinBox()
365 {
366   displayPreview();
367 }
368
369 //=================================================================================
370 // function : getAngle()
371 // purpose  :
372 //=================================================================================
373 double GenerationGUI_RevolDlg::getAngle() const
374 {
375   return GroupPoints->SpinBox_DX->value();
376 }
377
378 //=================================================================================
379 // function : createOperation
380 // purpose  :
381 //=================================================================================
382 GEOM::GEOM_IOperations_ptr GenerationGUI_RevolDlg::createOperation()
383 {
384   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
385 }
386
387 //=================================================================================
388 // function : isValid
389 // purpose  :
390 //=================================================================================
391 bool GenerationGUI_RevolDlg::isValid (QString& msg)
392 {
393   bool ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() );
394   return myOkBase && myOkAxis && ok;
395 }
396
397 //=================================================================================
398 // function : execute
399 // purpose  :
400 //=================================================================================
401 bool GenerationGUI_RevolDlg::execute (ObjectList& objects)
402 {
403   GEOM::GEOM_Object_var anObj, aBase;
404   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
405
406   for (int i=0; i < myBaseObjects.length(); i++) {
407     aBase = myBaseObjects[i];
408
409     if (!myBothway)
410       anObj = anOper->MakeRevolutionAxisAngle(aBase, myAxis, getAngle() * PI180);
411     else
412       anObj = anOper->MakeRevolutionAxisAngle2Ways(aBase, myAxis, getAngle() * PI180);
413     
414     if (!anObj->_is_nil()) {
415       if (!IsPreview()) {
416         QStringList aParameters;
417         aParameters << GroupPoints->SpinBox_DX->text();
418         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
419       }
420       objects.push_back(anObj._retn());
421     }
422   }
423
424   return true;
425 }
426
427 //=================================================================================
428 // function :  onReverse()
429 // purpose  :
430 //=================================================================================
431 void GenerationGUI_RevolDlg::onReverse()
432 {
433   double anOldValue = GroupPoints->SpinBox_DX->value();
434   GroupPoints->SpinBox_DX->setValue(-anOldValue);
435 }
436
437 //=================================================================================
438 // function :  onBothway()
439 // purpose  :
440 //=================================================================================
441 void GenerationGUI_RevolDlg::onBothway()
442 {
443   bool anOldValue = myBothway;
444   myBothway = !anOldValue;
445   GroupPoints->CheckButton2->setEnabled(!myBothway);
446   displayPreview();
447 }
448
449 //=================================================================================
450 // function : addSubshapeToStudy
451 // purpose  : virtual method to add new SubObjects if local selection
452 //=================================================================================
453 void GenerationGUI_RevolDlg::addSubshapesToStudy()
454 {
455   QMap<QString, GEOM::GEOM_Object_var> objMap;
456
457   objMap[GroupPoints->LineEdit2->text()] = myAxis;
458
459   addSubshapesToFather(objMap);
460 }
461
462 //=================================================================================
463 // function : extractPrefix
464 // purpose  : redefined from GEOMBase_Helper class
465 //=================================================================================
466 bool GenerationGUI_RevolDlg::extractPrefix() const
467 {
468   return myBaseObjects.length() > 1;
469 }