Salome HOME
5adb56a628a713128b4881edfb8d517621efd8ec
[modules/geom.git] / src / OperationGUI / OperationGUI_ExtrudedFeatureDlg.cxx
1 // Copyright (C) 2007-2022  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   : OperationGUI_ExtrudedFeatureDlg.cxx
25 //  Author : Renaud NEDELEC, Open CASCADE S.A.S.
26
27 #include "OperationGUI.h"
28 #include "OperationGUI_ExtrudedFeatureDlg.h"
29
30 #include <SUIT_Session.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <SalomeApp_Application.h>
33 #include <LightApp_SelectionMgr.h>
34
35 #include <GeometryGUI.h>
36 #include <GEOMBase.h>
37
38 #include <DlgRef.h>
39
40 // OCCT Includes
41 #include <TColStd_HSequenceOfTransient.hxx>
42
43 #include <GEOMImpl_Types.hxx>
44
45 //=================================================================================
46 // class    : OperationGUI_ExtrudedFeatureDlg()
47 // purpose  : Constructs a OperationGUI_ExtrudedCutDlg which is a child of 'parent',
48 //            with the name 'name' and widget flags set to 'f'.
49 //            The dialog will by default be modeless, unless you set 'modal' to
50 //            TRUE to construct a modal dialog.
51 //=================================================================================
52
53 OperationGUI_ExtrudedFeatureDlg::OperationGUI_ExtrudedFeatureDlg(const int theOperation,
54                                                                  GeometryGUI* theGeometryGUI,
55                                                                  QWidget* parent,bool modal, 
56                                                                  Qt::WindowFlags fl)
57   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
58     myOperation(theOperation)
59 {
60   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
61   QPixmap image0;
62   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
63   QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICO_DRAFT")));
64   QPixmap image3 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_CHANGE_DIRECTION")));
65   QString aTitle;
66   switch (myOperation) 
67   {
68     case OperationGUI::BOSS:
69       image0 = QPixmap(aResMgr->loadPixmap("GEOM", tr("ICO_EXTRUDED_BOSS")));
70       aTitle = tr("GEOM_EXTRUDED_BOSS_TITLE");
71       setHelpFileName("extruded_boss_operation_page.html");
72       break;
73     case OperationGUI::CUT:
74       image0 = QPixmap( aResMgr->loadPixmap("GEOM", tr("ICO_EXTRUDED_CUT")));
75       aTitle = tr("GEOM_EXTRUDED_CUT_TITLE");
76       setHelpFileName("extruded_cut_operation_page.html");
77       break;
78   }
79     
80   setWindowTitle(aTitle);
81   mainFrame()->GroupConstructors->setTitle(aTitle);
82   mainFrame()->RadioButton1->setIcon(image0);
83   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
84   mainFrame()->RadioButton2->close();
85   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
86   mainFrame()->RadioButton3->close();
87   
88   mainFrame()->RadioButton1->setChecked(true);
89   
90   myGroup = new DlgRef_2Sel2Spin2Push(centralWidget());
91   
92   myGroup->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
93   
94   myGroup->PushButton1->setIcon(image1);
95   myGroup->PushButton2->setIcon(image1);
96   myGroup->PushButton3->setIcon(image2);
97   myGroup->PushButton4->setIcon(image3);
98   myGroup->LineEdit1->setReadOnly(true);
99   myGroup->LineEdit2->setReadOnly(true);
100   myGroup->TextLabel1->setText(tr("GEOM_INIT_SHAPE"));
101   myGroup->TextLabel2->setText(tr("GEOM_PROFILE"));
102   myGroup->TextLabel3->setText(tr("GEOM_HEIGHT"));
103   myGroup->TextLabel4->setText(tr("GEOM_DRAFT_ANGLE"));
104   myGroup->TextLabel5->setText(tr("GEOM_CHANGE_DIRECTION"));
105  
106   
107   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
108   layout->setMargin(0); 
109   layout->setSpacing(6);
110   layout->addWidget(myGroup);
111
112   Init();
113    
114 }
115
116 //=================================================================================
117 // function : ~OperationGUI_ExtrudedFeatureDlg()
118 // purpose  : Destroys the object and frees any allocated resources
119 //=================================================================================
120 OperationGUI_ExtrudedFeatureDlg::~OperationGUI_ExtrudedFeatureDlg()
121 {
122 }
123
124 //=================================================================================
125 // function : Init()
126 // purpose  :
127 //=================================================================================
128 void OperationGUI_ExtrudedFeatureDlg::Init()
129 {
130   // init variables
131   myEditCurrentArgument = myGroup->LineEdit1;
132   
133   // Get setting of step value from file configuration
134   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
135   double step = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
136   
137   initSpinBox(myGroup->SpinBox_DX, COORD_MIN, COORD_MAX, step, "length_precision" );
138   initSpinBox(myGroup->SpinBox_DY, COORD_MIN, COORD_MAX, step, "length_precision" );
139
140   myGroup->LineEdit1->setText("");
141   myGroup->LineEdit2->setText("");
142   myGroup->SpinBox_DX->setValue(10.0);
143   myGroup->SpinBox_DY->setValue(10.0);
144   myGroup->SpinBox_DY->setEnabled(false);
145   myObject1.nullify();
146   myObject2.nullify();
147   
148   // signals and slots connections
149   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
150   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
151
152   connect(myGroup->PushButton1, SIGNAL(clicked()),     this, SLOT(SetEditCurrentArgument()));
153   connect(myGroup->PushButton2, SIGNAL(clicked()),     this, SLOT(SetEditCurrentArgument()));
154   connect(myGroup->PushButton3, SIGNAL(clicked(bool)), this, SLOT(ButtonClicked(bool)));
155   connect(myGroup->PushButton4, SIGNAL(clicked(bool)), this, SLOT(ButtonClicked(bool)));
156   connect(myGroup->SpinBox_DX,  SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
157   connect(myGroup->SpinBox_DY,  SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
158   
159
160   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
161            SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
162   
163   switch (myOperation) 
164   {
165     case OperationGUI::BOSS:
166       initName(tr("GEOM_EXTRUDED_BOSS"));     
167       break;
168     case OperationGUI::CUT:
169       initName(tr("GEOM_EXTRUDED_CUT"));  
170       break;
171   }
172   
173   mainFrame()->RadioButton1->setFocus();
174   
175   globalSelection(GEOM_ALLSHAPES);
176   
177   myGroup->PushButton1->click();
178   SelectionIntoArgument();
179   
180   resize(minimumSizeHint());
181 }
182
183 //=================================================================================
184 // function : ClickOnOk()
185 // purpose  :
186 //=================================================================================
187 void OperationGUI_ExtrudedFeatureDlg::ClickOnOk()
188 {
189   setIsApplyAndClose( true );
190   if (ClickOnApply())
191     ClickOnCancel();
192 }
193
194 //=================================================================================
195 // function : ClickOnApply()
196 // purpose  :
197 //=================================================================================
198 bool OperationGUI_ExtrudedFeatureDlg::ClickOnApply()
199 {
200   if (!onAccept())
201     return false;
202
203   initName();
204   // activate selection and connect selection manager
205   myGroup->PushButton1->click();
206   return true;
207 }
208
209 //=================================================================================
210 // function : SetEditCurrentArgument()
211 // purpose  :
212 //=================================================================================
213 void OperationGUI_ExtrudedFeatureDlg::SetEditCurrentArgument()
214 {
215   QPushButton* send = (QPushButton*)sender();
216   
217   if (send == myGroup->PushButton1) {
218     myEditCurrentArgument = myGroup->LineEdit1;
219
220     myGroup->PushButton2->setDown(false);
221     myGroup->LineEdit2->setEnabled(false);
222   }
223   else if (send == myGroup->PushButton2) {
224     myEditCurrentArgument = myGroup->LineEdit2;
225
226     myGroup->PushButton1->setDown(false);
227     myGroup->LineEdit1->setEnabled(false);
228   }
229   
230   // enable line edit
231   myEditCurrentArgument->setEnabled(true);
232   myEditCurrentArgument->setFocus();
233   // after setFocus(), because it will be setDown(false) when loses focus
234   send->setDown(true);
235 }
236
237
238 //=================================================================================
239 // function : SelectionIntoArgument()
240 // purpose  : Called when selection is changed or on dialog initialization or activation
241 //=================================================================================
242 void OperationGUI_ExtrudedFeatureDlg::SelectionIntoArgument()
243 {
244   myEditCurrentArgument->setText("");
245
246   QList<TopAbs_ShapeEnum> types;
247   types<<TopAbs_SHAPE;
248   
249   if (myEditCurrentArgument == myGroup->LineEdit1)
250   {
251     types.clear();
252     types<<TopAbs_COMPOUND<<TopAbs_SOLID;
253   }
254   if (myEditCurrentArgument == myGroup->LineEdit2)
255   {
256     types.clear();
257     types << TopAbs_EDGE << TopAbs_WIRE << TopAbs_FACE;
258   }
259
260   GEOM::GeomObjPtr aSelectedObject = getSelected( types );
261   TopoDS_Shape aShape;
262   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
263     QString aName = GEOMBase::GetName( aSelectedObject.get() );
264     myEditCurrentArgument->setText( aName );
265
266     // clear selection
267     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
268     myGeomGUI->getApp()->selectionMgr()->clearSelected();
269     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
270             this, SLOT(SelectionIntoArgument()));
271
272     if (myEditCurrentArgument == myGroup->LineEdit1) {
273       myObject1 = aSelectedObject;
274       if (!myObject2)
275         myGroup->PushButton2->click();
276     }
277     else if (myEditCurrentArgument == myGroup->LineEdit2) {
278       myObject2 = aSelectedObject;
279       if (!myObject1)
280         myGroup->PushButton1->click();
281     }
282     
283     displayPreview(true);
284   }
285   else {
286     if      (myEditCurrentArgument == myGroup->LineEdit1) myObject1.nullify();
287     else if (myEditCurrentArgument == myGroup->LineEdit2) myObject2.nullify();
288   }
289 }
290
291 //=================================================================================
292 // function : ActivateThisDialog()
293 // purpose  :
294 //=================================================================================
295 void OperationGUI_ExtrudedFeatureDlg::ActivateThisDialog()
296 {
297   GEOMBase_Skeleton::ActivateThisDialog();
298
299   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
300            this, SLOT( SelectionIntoArgument() ) );
301   processPreview();
302 }
303
304
305 //=================================================================================
306 // function : enterEvent()
307 // purpose  : when mouse enter onto the QWidget
308 //=================================================================================
309 void OperationGUI_ExtrudedFeatureDlg::enterEvent (QEvent*)
310 {
311   if (!mainFrame()->GroupConstructors->isEnabled())
312     ActivateThisDialog();
313 }
314
315 //=================================================================================
316 // function : ValueChangedInSpinBox()
317 // purpose  :
318 //=================================================================================
319 void OperationGUI_ExtrudedFeatureDlg::ValueChangedInSpinBox()
320 {
321   displayPreview(true);
322 }
323
324 //=================================================================================
325 // function : ButtonClicked(bool checked)
326 // purpose  :
327 //=================================================================================
328 void OperationGUI_ExtrudedFeatureDlg::ButtonClicked(bool checked)
329 {
330   QPushButton* send = (QPushButton*)sender();
331   if (send == myGroup->PushButton3)
332     myGroup->SpinBox_DY->setEnabled(checked);
333  
334   displayPreview(true);
335 }
336
337 //=================================================================================
338 // function : createOperation
339 // purpose  :
340 //=================================================================================
341 GEOM::GEOM_IOperations_ptr OperationGUI_ExtrudedFeatureDlg::createOperation()
342 {
343   return getGeomEngine()->GetI3DPrimOperations();
344 }
345
346 //=================================================================================
347 // function : isValid
348 // purpose  :
349 //=================================================================================
350 bool OperationGUI_ExtrudedFeatureDlg::isValid (QString&)
351 {
352   return myObject1 && myObject2; 
353 }
354
355 //=================================================================================
356 // function : execute
357 // purpose  :
358 //=================================================================================
359 bool OperationGUI_ExtrudedFeatureDlg::execute (ObjectList& objects)
360 {
361   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
362   
363   double angle=0.0;
364   double aHeight = myGroup->SpinBox_DX->value();
365   
366   if (myGroup->PushButton3->isChecked())
367     angle=myGroup->SpinBox_DY->value();
368     
369   bool isProtrusion = (myOperation == OperationGUI::BOSS);
370   bool isInvert = myGroup->PushButton4->isChecked();
371   
372   // Hide the initial shape in order to see the modifications on the preview
373   erase(myObject1.get(),false); 
374   
375   GEOM::GEOM_Object_var anObj = anOper->MakeDraftPrism(myObject1.get(), myObject2.get(), 
376                                                        aHeight,
377                                                        angle,
378                                                        isProtrusion,
379                                                        isInvert);
380   if (!anObj->_is_nil()) {
381     if (!IsPreview()) {
382       QStringList aParameters;
383       aParameters << myGroup->SpinBox_DX->text();
384       if (myGroup->PushButton3->isChecked())
385         aParameters << myGroup->SpinBox_DY->text();
386       anObj->SetParameters(aParameters.join(":").toUtf8().constData());
387     }
388     objects.push_back(anObj._retn());
389   }
390
391   return true;
392 }
393
394 // //=================================================================================
395 // // function : addSubshapeToStudy
396 // // purpose  : virtual method to add new SubObjects if local selection
397 // //=================================================================================
398 // void OperationGUI_ExtrudedFeatureDlg::addSubshapesToStudy()
399 // {
400 //   GEOMBase::PublishSubObject( myObject2.get() );
401 // }
402
403 //=================================================================================
404 // function : getSourceObjects
405 // purpose  : virtual method to get source objects
406 //=================================================================================
407 QList<GEOM::GeomObjPtr> OperationGUI_ExtrudedFeatureDlg::getSourceObjects()
408 {
409   QList<GEOM::GeomObjPtr> res;
410   res << myObject1 << myObject2;
411   return res;
412 }