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