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