Salome HOME
354a8056263d892a2f0315d999f8c0e00087b00e
[modules/geom.git] / src / GenerationGUI / GenerationGUI_RevolDlg.cxx
1 // Copyright (C) 2007-2023  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   : 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
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
122   myBaseObjects.clear();
123   myAxis.nullify();
124
125   showOnlyPreviewControl();
126
127   // signals and slots connections
128   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
129   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
130
131   connect(GroupPoints->PushButton1,  SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
132   connect(GroupPoints->PushButton2,  SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
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   setIsApplyAndClose( true );
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 : SelectionIntoArgument()
185 // purpose  : Called when selection is changed or on dialog initialization or activation
186 //=================================================================================
187 void GenerationGUI_RevolDlg::SelectionIntoArgument()
188 {
189   erasePreview();
190   myEditCurrentArgument->setText("");
191
192   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
193     myBaseObjects.clear();
194     QList<GEOM::GeomObjPtr> objects = getSelected( TopAbs_SHAPE, -1 );
195     for ( int i = 0; i < objects.count(); i++ ) {
196       GEOM::shape_type stype = objects[i]->GetMaxShapeType();
197       if ( stype < GEOM::SHELL || stype > GEOM::VERTEX )
198         continue;
199       myBaseObjects << objects[i];
200     }
201     if ( !myBaseObjects.isEmpty() ) {
202       QString aName = myBaseObjects.count() > 1 ? QString( "%1_objects").arg( myBaseObjects.count() ) : GEOMBase::GetName( myBaseObjects[0].get() );
203       myEditCurrentArgument->setText( aName );
204     }
205   }
206   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
207     myAxis = getSelected( TopAbs_EDGE );
208     if ( myAxis ) {
209       QString aName = GEOMBase::GetName( myAxis.get() );
210       myEditCurrentArgument->setText( aName );
211       if ( myBaseObjects.isEmpty() )
212         GroupPoints->PushButton1->click();
213     }
214   }
215   processPreview();
216 }
217
218 //=================================================================================
219 // function : SetEditCurrentArgument()
220 // purpose  :
221 //=================================================================================
222 void GenerationGUI_RevolDlg::SetEditCurrentArgument()
223 {
224   QPushButton* send = (QPushButton*)sender();
225
226   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
227   globalSelection(GEOM_ALLSHAPES);
228   if (send == GroupPoints->PushButton1) {
229     myEditCurrentArgument = GroupPoints->LineEdit1;
230     GroupPoints->PushButton2->setDown(false);
231     GroupPoints->LineEdit2->setEnabled(false);
232   }
233   else if (send == GroupPoints->PushButton2) {
234     myEditCurrentArgument = GroupPoints->LineEdit2;
235     GroupPoints->PushButton1->setDown(false);
236     GroupPoints->LineEdit1->setEnabled(false);
237     localSelection(TopAbs_EDGE);
238   }
239   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
240           this, SLOT(SelectionIntoArgument()));
241
242   // enable line edit
243   myEditCurrentArgument->setEnabled(true);
244   myEditCurrentArgument->setFocus();
245   // after setFocus(), because it will be setDown(false) when loses focus
246   send->setDown(true);
247
248   // seems we need it only to avoid preview disappearing, caused by selection mode change
249   processPreview();
250 }
251
252 //=================================================================================
253 // function : ActivateThisDialog()
254 // purpose  :
255 //=================================================================================
256 void GenerationGUI_RevolDlg::ActivateThisDialog()
257 {
258   GEOMBase_Skeleton::ActivateThisDialog();
259   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
260            this, SLOT( SelectionIntoArgument() ) );
261
262   processPreview();
263 }
264
265 //=================================================================================
266 // function : enterEvent()
267 // purpose  :
268 //=================================================================================
269 void GenerationGUI_RevolDlg::enterEvent (QEvent*)
270 {
271   if (!mainFrame()->GroupConstructors->isEnabled())
272     ActivateThisDialog();
273 }
274
275 //=================================================================================
276 // function : ValueChangedInSpinBox()
277 // purpose  :
278 //=================================================================================
279 void GenerationGUI_RevolDlg::ValueChangedInSpinBox()
280 {
281   processPreview();
282 }
283
284 //=================================================================================
285 // function : getAngle()
286 // purpose  :
287 //=================================================================================
288 double GenerationGUI_RevolDlg::getAngle() const
289 {
290   return GroupPoints->SpinBox_DX->value();
291 }
292
293 //=================================================================================
294 // function : createOperation
295 // purpose  :
296 //=================================================================================
297 GEOM::GEOM_IOperations_ptr GenerationGUI_RevolDlg::createOperation()
298 {
299   return getGeomEngine()->GetI3DPrimOperations();
300 }
301
302 //=================================================================================
303 // function : isValid
304 // purpose  :
305 //=================================================================================
306 bool GenerationGUI_RevolDlg::isValid (QString& msg)
307 {
308   return GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) && !myBaseObjects.isEmpty() && myAxis;
309 }
310
311 //=================================================================================
312 // function : execute
313 // purpose  :
314 //=================================================================================
315 bool GenerationGUI_RevolDlg::execute (ObjectList& objects)
316 {
317   GEOM::GEOM_Object_var anObj;
318   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
319
320   for (int i = 0; i < myBaseObjects.count(); i++) {
321     if (!GroupPoints->CheckButton1->isChecked())
322       anObj = anOper->MakeRevolutionAxisAngle(myBaseObjects[i].get(), myAxis.get(),
323                                               getAngle() * M_PI / 180.);
324     else
325       anObj = anOper->MakeRevolutionAxisAngle2Ways(myBaseObjects[i].get(), myAxis.get(),
326                                                    getAngle() * M_PI / 180.);
327     
328     if (!anObj->_is_nil()) {
329       if (!IsPreview()) {
330         QStringList aParameters;
331         aParameters << GroupPoints->SpinBox_DX->text();
332         anObj->SetParameters(aParameters.join(":").toUtf8().constData());
333       }
334       objects.push_back(anObj._retn());
335     }
336   }
337
338   return true;
339 }
340
341 //=================================================================================
342 // function :  onReverse()
343 // purpose  :
344 //=================================================================================
345 void GenerationGUI_RevolDlg::onReverse()
346 {
347   double anOldValue = GroupPoints->SpinBox_DX->value();
348   GroupPoints->SpinBox_DX->setValue(-anOldValue);
349 }
350
351 //=================================================================================
352 // function :  onBothway()
353 // purpose  :
354 //=================================================================================
355 void GenerationGUI_RevolDlg::onBothway()
356 {
357   GroupPoints->CheckButton2->setEnabled(!GroupPoints->CheckButton1->isChecked());
358   processPreview();
359 }
360
361 //=================================================================================
362 // function : addSubshapeToStudy
363 // purpose  : virtual method to add new SubObjects if local selection
364 //=================================================================================
365 void GenerationGUI_RevolDlg::addSubshapesToStudy()
366 {
367   GEOMBase::PublishSubObject( myAxis.get() );
368 }
369
370 //=================================================================================
371 // function : extractPrefix
372 // purpose  : redefined from GEOMBase_Helper class
373 //=================================================================================
374 bool GenerationGUI_RevolDlg::extractPrefix() const
375 {
376   return myBaseObjects.count() > 1;
377 }
378
379 //=================================================================================
380 // function : getSourceObjects
381 // purpose  : virtual method to get source objects
382 //=================================================================================
383 QList<GEOM::GeomObjPtr> GenerationGUI_RevolDlg::getSourceObjects()
384 {
385   QList<GEOM::GeomObjPtr> res(myBaseObjects);
386   res << myAxis;
387   return res;
388 }