Salome HOME
Mantis issues 0021432 and 0021404: MakeFace[Wires] should generate exception on attem...
[modules/geom.git] / src / GenerationGUI / GenerationGUI_RevolDlg.cxx
1 // Copyright (C) 2007-2011  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
71   GroupPoints = new DlgRef_2Sel1Spin2Check(centralWidget());
72   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
73   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
74   GroupPoints->TextLabel2->setText(tr("GEOM_AXIS"));
75   GroupPoints->TextLabel3->setText(tr("GEOM_ANGLE"));
76   GroupPoints->PushButton1->setIcon(image1);
77   GroupPoints->PushButton2->setIcon(image1);
78   GroupPoints->LineEdit1->setReadOnly(true);
79   GroupPoints->LineEdit2->setReadOnly(true);
80   GroupPoints->CheckButton1->setText(tr("GEOM_BOTHWAY"));
81   GroupPoints->CheckButton2->setText(tr("GEOM_REVERSE"));
82
83   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
84   layout->setMargin(0); layout->setSpacing(6);
85   layout->addWidget(GroupPoints);
86   /***************************************************************/
87
88   setHelpFileName("create_revolution_page.html");
89
90   // Initialisation
91   Init();
92 }
93
94 //=================================================================================
95 // function : ~GenerationGUI_RevolDlg()
96 // purpose  : Destroys the object and frees any allocated resources
97 //=================================================================================
98 GenerationGUI_RevolDlg::~GenerationGUI_RevolDlg()
99 {
100   // no need to delete child widgets, Qt does it all for us
101 }
102
103 //=================================================================================
104 // function : Init()
105 // purpose  :
106 //=================================================================================
107 void GenerationGUI_RevolDlg::Init()
108 {
109   // min, max, step and decimals for spin boxes & initial values
110   double SpecificStep = 5;
111   initSpinBox(GroupPoints->SpinBox_DX, -360.0, 360.0, SpecificStep, "angle_precision" );
112   GroupPoints->SpinBox_DX->setValue(45.0);
113
114   // init variables
115   GroupPoints->LineEdit1->setReadOnly(true);
116   GroupPoints->LineEdit2->setReadOnly(true);
117
118   GroupPoints->LineEdit1->setText("");
119   GroupPoints->LineEdit2->setText("");
120
121   myBaseObjects.clear();
122   myAxis.nullify();
123
124   showOnlyPreviewControl();
125
126   // signals and slots connections
127   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
128   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
129
130   connect(GroupPoints->PushButton1,  SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
131   connect(GroupPoints->PushButton2,  SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
132
133   connect(GroupPoints->SpinBox_DX,   SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
134   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)),        this, SLOT(onBothway()));
135   connect(GroupPoints->CheckButton2, SIGNAL(toggled(bool)),        this, SLOT(onReverse()));
136
137   // san: We don't need this, as the default step value is not used in this dialog box
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   setIsApplyAndClose( true );
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 : SelectionIntoArgument()
184 // purpose  : Called when selection is changed or on dialog initialization or activation
185 //=================================================================================
186 void GenerationGUI_RevolDlg::SelectionIntoArgument()
187 {
188   erasePreview();
189   myEditCurrentArgument->setText("");
190
191   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
192     myBaseObjects.clear();
193     QList<GEOM::GeomObjPtr> objects = getSelected( TopAbs_SHAPE, -1 );
194     for ( int i = 0; i < objects.count(); i++ ) {
195       GEOM::shape_type stype = objects[i]->GetMaxShapeType();
196       if ( stype < GEOM::SHELL || stype > GEOM::VERTEX )
197         continue;
198       myBaseObjects << objects[i];
199     }
200     if ( !myBaseObjects.isEmpty() ) {
201       QString aName = myBaseObjects.count() > 1 ? QString( "%1_objects").arg( myBaseObjects.count() ) : GEOMBase::GetName( myBaseObjects[0].get() );
202       myEditCurrentArgument->setText( aName );
203     }
204   }
205   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
206     myAxis = getSelected( TopAbs_EDGE );
207     if ( myAxis ) {
208       QString aName = GEOMBase::GetName( myAxis.get() );
209       myEditCurrentArgument->setText( aName );
210       if ( myBaseObjects.isEmpty() )
211         GroupPoints->PushButton1->click();
212     }
213   }
214   processPreview();
215 }
216
217 //=================================================================================
218 // function : SetEditCurrentArgument()
219 // purpose  :
220 //=================================================================================
221 void GenerationGUI_RevolDlg::SetEditCurrentArgument()
222 {
223   QPushButton* send = (QPushButton*)sender();
224
225   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
226   globalSelection(GEOM_ALLSHAPES);
227   if (send == GroupPoints->PushButton1) {
228     myEditCurrentArgument = GroupPoints->LineEdit1;
229     GroupPoints->PushButton2->setDown(false);
230     GroupPoints->LineEdit2->setEnabled(false);
231   }
232   else if (send == GroupPoints->PushButton2) {
233     myEditCurrentArgument = GroupPoints->LineEdit2;
234     GroupPoints->PushButton1->setDown(false);
235     GroupPoints->LineEdit1->setEnabled(false);
236     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
237   }
238   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
239           this, SLOT(SelectionIntoArgument()));
240
241   // enable line edit
242   myEditCurrentArgument->setEnabled(true);
243   myEditCurrentArgument->setFocus();
244   // after setFocus(), because it will be setDown(false) when loses focus
245   send->setDown(true);
246
247   // seems we need it only to avoid preview disappearing, caused by selection mode change
248   processPreview();
249 }
250
251 //=================================================================================
252 // function : ActivateThisDialog()
253 // purpose  :
254 //=================================================================================
255 void GenerationGUI_RevolDlg::ActivateThisDialog()
256 {
257   GEOMBase_Skeleton::ActivateThisDialog();
258   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
259            this, SLOT( SelectionIntoArgument() ) );
260
261   processPreview();
262 }
263
264 //=================================================================================
265 // function : enterEvent()
266 // purpose  :
267 //=================================================================================
268 void GenerationGUI_RevolDlg::enterEvent (QEvent*)
269 {
270   if (!mainFrame()->GroupConstructors->isEnabled())
271     ActivateThisDialog();
272 }
273
274 //=================================================================================
275 // function : ValueChangedInSpinBox()
276 // purpose  :
277 //=================================================================================
278 void GenerationGUI_RevolDlg::ValueChangedInSpinBox()
279 {
280   processPreview();
281 }
282
283 //=================================================================================
284 // function : getAngle()
285 // purpose  :
286 //=================================================================================
287 double GenerationGUI_RevolDlg::getAngle() const
288 {
289   return GroupPoints->SpinBox_DX->value();
290 }
291
292 //=================================================================================
293 // function : createOperation
294 // purpose  :
295 //=================================================================================
296 GEOM::GEOM_IOperations_ptr GenerationGUI_RevolDlg::createOperation()
297 {
298   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
299 }
300
301 //=================================================================================
302 // function : isValid
303 // purpose  :
304 //=================================================================================
305 bool GenerationGUI_RevolDlg::isValid (QString& msg)
306 {
307   return GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) && !myBaseObjects.isEmpty() && myAxis;
308 }
309
310 //=================================================================================
311 // function : execute
312 // purpose  :
313 //=================================================================================
314 bool GenerationGUI_RevolDlg::execute (ObjectList& objects)
315 {
316   GEOM::GEOM_Object_var anObj;
317   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
318
319   for (int i = 0; i < myBaseObjects.count(); i++) {
320     if (!GroupPoints->CheckButton1->isChecked())
321       anObj = anOper->MakeRevolutionAxisAngle(myBaseObjects[i].get(), myAxis.get(),
322                                               getAngle() * M_PI / 180.);
323     else
324       anObj = anOper->MakeRevolutionAxisAngle2Ways(myBaseObjects[i].get(), myAxis.get(),
325                                                    getAngle() * M_PI / 180.);
326     
327     if (!anObj->_is_nil()) {
328       if (!IsPreview()) {
329         QStringList aParameters;
330         aParameters << GroupPoints->SpinBox_DX->text();
331         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
332       }
333       objects.push_back(anObj._retn());
334     }
335   }
336
337   return true;
338 }
339
340 //=================================================================================
341 // function :  onReverse()
342 // purpose  :
343 //=================================================================================
344 void GenerationGUI_RevolDlg::onReverse()
345 {
346   double anOldValue = GroupPoints->SpinBox_DX->value();
347   GroupPoints->SpinBox_DX->setValue(-anOldValue);
348 }
349
350 //=================================================================================
351 // function :  onBothway()
352 // purpose  :
353 //=================================================================================
354 void GenerationGUI_RevolDlg::onBothway()
355 {
356   GroupPoints->CheckButton2->setEnabled(!GroupPoints->CheckButton1->isChecked());
357   processPreview();
358 }
359
360 //=================================================================================
361 // function : addSubshapeToStudy
362 // purpose  : virtual method to add new SubObjects if local selection
363 //=================================================================================
364 void GenerationGUI_RevolDlg::addSubshapesToStudy()
365 {
366   GEOMBase::PublishSubObject( myAxis.get() );
367 }
368
369 //=================================================================================
370 // function : extractPrefix
371 // purpose  : redefined from GEOMBase_Helper class
372 //=================================================================================
373 bool GenerationGUI_RevolDlg::extractPrefix() const
374 {
375   return myBaseObjects.count() > 1;
376 }