Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/geom.git] / src / OperationGUI / OperationGUI_FilletDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : OperationGUI_FilletDlg.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "OperationGUI_FilletDlg.h"
31
32 #include "DisplayGUI.h"
33 #include "QAD_MessageBox.h"
34
35 #include <TopoDS_Edge.hxx>
36 #include <AIS_InteractiveContext.hxx>
37 #include <TopExp_Explorer.hxx>
38 #include <BRepFilletAPI_MakeFillet.hxx>
39 #include <Precision.hxx>
40
41 #include <Standard_ErrorHandler.hxx> 
42 #include <Standard_Failure.hxx>
43
44 //=================================================================================
45 // class    : OperationGUI_FilletDlg()
46 // purpose  : Constructs a OperationGUI_FilletDlg which is a child of 'parent', with the 
47 //            name 'name' and widget flags set to 'f'.
48 //            The dialog will by default be modeless, unless you set 'modal' to
49 //            TRUE to construct a modal dialog.
50 //=================================================================================
51 OperationGUI_FilletDlg::OperationGUI_FilletDlg(QWidget* parent, const char* name, OperationGUI* theOperationGUI, SALOME_Selection* Sel, Handle(AIS_InteractiveContext) ic, bool modal, WFlags fl)
52   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
53 {
54   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_FILLET_ALL")));
55   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_FILLET_EDGE")));
56   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_FILLET_FACE")));
57   QPixmap image3(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
58
59   setCaption(tr("GEOM_FILLET_TITLE"));
60
61   /***************************************************************/
62   GroupConstructors->setTitle(tr("GEOM_FILLET"));
63   RadioButton1->setPixmap(image0);
64   RadioButton2->setPixmap(image1);
65   RadioButton3->setPixmap(image2);
66
67   Group1 = new DlgRef_1Sel1Spin(this, "Group1");
68   Group1->GroupBox1->setTitle(tr("GEOM_FILLET_ALL"));
69   Group1->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
70   Group1->TextLabel2->setText(tr("GEOM_RADIUS"));
71   Group1->PushButton1->setPixmap(image3);
72
73   Layout1->addWidget(Group1, 1, 0);
74   /***************************************************************/
75
76   /* Initialisations */
77   myOperationGUI = theOperationGUI;
78   Init(ic);
79 }
80
81
82 //=================================================================================
83 // function : ~OperationGUI_FilletDlg()
84 // purpose  : Destroys the object and frees any allocated resources
85 //=================================================================================
86 OperationGUI_FilletDlg::~OperationGUI_FilletDlg()
87 {  
88   /* no need to delete child widgets, Qt does it all for us */
89 }
90
91
92 //=================================================================================
93 // function : Init()
94 // purpose  :
95 //=================================================================================
96 void OperationGUI_FilletDlg::Init(Handle(AIS_InteractiveContext) ic)
97 {
98   /* init variables */
99   myConstructorId = 0;
100   myEditCurrentArgument = Group1->LineEdit1;
101
102   myRadius = 5.0;
103   myOkShape = false;
104   myIC = ic;
105   myLocalContextId = -1;
106   myUseLocalContext = false;
107
108   double SpecificStep = 10.0;
109   /* min, max, step and decimals for spin boxes */
110   Group1->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, SpecificStep, 3);
111   Group1->SpinBox_DX->SetValue(myRadius);
112
113   /* signals and slots connections */
114   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
115   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
116   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
117
118   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
119   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
120   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
121
122   connect(Group1->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
123   connect(Group1->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
124
125   connect(Group1->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
126   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), Group1->SpinBox_DX, SLOT(SetStep(double)));
127
128   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
129
130   /* displays Dialog */
131   Group1->show();
132   this->show();
133
134   return;
135 }
136
137
138 //=================================================================================
139 // function : ConstructorsClicked()
140 // purpose  : Radio button management
141 //=================================================================================
142 void OperationGUI_FilletDlg::ConstructorsClicked(int constructorId)
143 {
144   myConstructorId = constructorId;
145   myGeomBase->EraseSimulationShape();
146   mySimulationTopoDs.Nullify();
147   disconnect(mySelection, 0, this, 0);
148   myOkShape = false;
149   myRadius = 5.0;
150
151   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC && myUseLocalContext) {
152     myIC->CloseLocalContext(myLocalContextId);
153     DisplayGUI* myDisplayGUI = new DisplayGUI();
154     myDisplayGUI->OnDisplayAll(true);
155     myUseLocalContext = false;
156   }
157
158   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() != VIEW_OCC) {
159     myConstructorId = constructorId = 0; //No subshape selection if viewer is not OCC
160     RadioButton1->setChecked(TRUE);
161   }
162   
163   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
164   Group1->LineEdit1->setText("");
165   Group1->SpinBox_DX->SetValue(myRadius);
166   switch (constructorId)
167     {
168     case 0: /* Fillet All */
169       {
170         Group1->GroupBox1->setTitle(tr("GEOM_FILLET_ALL"));
171         myShapeType = -1;
172         break;
173       }
174     case 1: /* Fillet edges */
175       {
176         Group1->GroupBox1->setTitle(tr("GEOM_FILLET_EDGES"));
177         myShapeType = 6;
178         break;
179       }
180     case 2: /* Fillet Faces */
181       {
182         Group1->GroupBox1->setTitle(tr("GEOM_FILLET_FACES"));
183         myShapeType = 4;
184         break;
185       }
186     }
187   return;
188 }
189
190
191 //=================================================================================
192 // function : ClickOnOk()
193 // purpose  :
194 //=================================================================================
195 void OperationGUI_FilletDlg::ClickOnOk()
196 {
197   this->ClickOnApply();
198   this->ClickOnCancel();
199   return;
200 }
201
202
203 //=================================================================================
204 // function : ClickOnApply()
205 // purpose  :
206 //=================================================================================
207 void OperationGUI_FilletDlg::ClickOnApply()
208 {
209   buttonApply->setFocus();
210   QApplication::setOverrideCursor(Qt::waitCursor);
211   QAD_Application::getDesktop()->putInfo(tr(""));
212   myGeomBase->EraseSimulationShape();
213   mySimulationTopoDs.Nullify();
214
215   bool testResult = false;
216   switch(myConstructorId)
217     { 
218     case 0 : /* Fillet All */
219       { 
220         if(myOkShape)
221           testResult = myOperationGUI->OnFilletGetAll(myShape, myRadius, myShapeType, myShapeIOR);
222         break;
223       }
224     case 1 : /* Fillet Edge */
225       { 
226         if(myOkShape)
227           testResult = myOperationGUI->OnFilletGetSelected(myShape, myShapeIOR, myRadius, myShapeType, myLocalContextId, myUseLocalContext);
228         break;
229       }
230     case 2 : /* Fillet Face */
231       {
232         if(myOkShape)
233           testResult = myOperationGUI->OnFilletGetSelected(myShape, myShapeIOR, myRadius, myShapeType, myLocalContextId, myUseLocalContext);       
234         break;
235       }
236     }
237
238   if(!testResult) 
239     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
240   else
241     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
242   /* Reset all arguments and local context to allow user a new selection ...*/
243   this->ResetStateOfDialog();
244   QApplication::restoreOverrideCursor();
245   return;
246 }
247
248
249 //=================================================================================
250 // function : ClickOnCancel()
251 // purpose  :
252 //=================================================================================
253 void OperationGUI_FilletDlg::ClickOnCancel()
254 {
255   this->ResetStateOfDialog();
256   GEOMBase_Skeleton::ClickOnCancel();
257   return;
258 }
259
260
261 //=================================================================================
262 // function : SelectionIntoArgument()
263 // purpose  : Called when selection has changed
264 //=================================================================================
265 void OperationGUI_FilletDlg::SelectionIntoArgument()
266 {
267   myGeomBase->EraseSimulationShape();
268   mySimulationTopoDs.Nullify();
269   myEditCurrentArgument->setText("");
270   this->ResetStateOfDialog();
271   QString aString = ""; /* name of selection */
272
273   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
274   if (nbSel == 1) {
275     TopoDS_Shape S;
276     Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject();
277     if(!myGeomBase->GetTopoFromSelection(mySelection, S))
278       return;
279     if(!IO->hasEntry()) {
280       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_SHAPE_IN_STUDY"));
281       return;
282     }
283         
284     if(!S.IsNull() &&  S.ShapeType() <= 2) {
285       if(IO->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject))) {
286         Handle(GEOM_InteractiveObject) GIObject = Handle(GEOM_InteractiveObject)::DownCast(IO);
287         myShapeIOR = GIObject->getIOR(); /* the Geom IOR string of selection */
288         myEditCurrentArgument->setText(aString);
289         myShape = S;
290         myOkShape = true;
291       }
292       
293       if(IO->hasEntry()) {
294         SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
295         SALOMEDS::SObject_var obj = aStudy->FindObjectID(IO->getEntry());
296         SALOMEDS::GenericAttribute_var anAttr;
297         SALOMEDS::AttributeIOR_var anIOR;
298         if(!obj->_is_nil()) {
299           if(obj->FindAttribute(anAttr, "AttributeIOR")) {
300             anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
301             myShapeIOR = anIOR->Value();
302             myOkShape = true;
303             myShape = S;
304             myEditCurrentArgument->setText(aString);
305           }
306         }
307       }
308     }
309     if(myOkShape && QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC && myConstructorId == 0)
310       this->MakePreview();
311   }
312   else 
313     return;
314   
315   if(myOkShape && myShapeType!=-1 && myConstructorId != 0) {
316     /* local context is defined into the method */
317     DisplayGUI* myDisplayGUI = new DisplayGUI();
318     myDisplayGUI->PrepareSubShapeSelection(myShapeType, myLocalContextId);  
319     myUseLocalContext = true;
320   }
321 }
322
323
324 //=================================================================================
325 // function : SetEditCurrentArgument()
326 // purpose  :
327 //=================================================================================
328 void OperationGUI_FilletDlg::SetEditCurrentArgument()
329 {
330   QPushButton* send = (QPushButton*)sender();  
331
332   if(send == Group1->PushButton1)
333     Group1->LineEdit1->setFocus();
334
335   this->SelectionIntoArgument();
336   return;
337 }
338
339
340 //=================================================================================
341 // function : DeactivateActiveDialog()
342 // purpose  :
343 //=================================================================================
344 void OperationGUI_FilletDlg::DeactivateActiveDialog()
345 {
346   this->ResetStateOfDialog();
347   GEOMBase_Skeleton::DeactivateActiveDialog();
348   return;
349 }
350
351
352 //=================================================================================
353 // function : ActivateThisDialog()
354 // purpose  :
355 //=================================================================================
356 void OperationGUI_FilletDlg::ActivateThisDialog()
357 {
358   GEOMBase_Skeleton::ActivateThisDialog();
359   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
360   if(!mySimulationTopoDs.IsNull())
361     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
362   return;
363 }
364
365
366 //=================================================================================
367 // function : enterEvent()
368 // purpose  :
369 //=================================================================================
370 void OperationGUI_FilletDlg::enterEvent(QEvent* e)
371 {
372   if(GroupConstructors->isEnabled())
373     return;
374   this->ActivateThisDialog();
375   return;
376 }
377
378
379 //=================================================================================
380 // function : closeEvent()
381 // purpose  :
382 //=================================================================================
383 void OperationGUI_FilletDlg::closeEvent(QCloseEvent* e)
384
385   /* same than click on cancel button */
386   this->ClickOnCancel();
387   return;
388 }
389
390
391 //=================================================================================
392 // function : ResetStateOfDialog()
393 // purpose  :
394 //=================================================================================
395 void OperationGUI_FilletDlg::ResetStateOfDialog()
396 {
397   myOkShape = false;
398   myEditCurrentArgument->setText("");
399   QApplication::restoreOverrideCursor();
400
401   /* Close its local contact if opened */
402   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC && myUseLocalContext) {
403     myIC->CloseLocalContext(myLocalContextId);
404     myUseLocalContext = false;
405     DisplayGUI* myDisplayGUI = new DisplayGUI();
406     myDisplayGUI->OnDisplayAll(true);
407   }
408   return;
409 }
410
411
412 //=================================================================================
413 // function : ValueChangedInSpinBox()
414 // purpose  :
415 //=================================================================================
416 void OperationGUI_FilletDlg::ValueChangedInSpinBox(double newValue)
417 {
418   myGeomBase->EraseSimulationShape(); 
419   mySimulationTopoDs.Nullify();
420
421   myRadius = newValue;
422
423   if(myOkShape && QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC && myConstructorId == 0)
424     this->MakePreview();
425   return;
426 }
427
428
429 //=================================================================================
430 // function : MakePreview()
431 // purpose  :
432 //=================================================================================
433 void OperationGUI_FilletDlg::MakePreview()
434 {
435   QApplication::setOverrideCursor(Qt::waitCursor);
436   TopoDS_Shape tds;
437
438   try {
439     BRepFilletAPI_MakeFillet fill(myShape);
440     switch(myConstructorId) 
441       {
442       case 0: /* Fillet All */
443         {
444           TopExp_Explorer Exp(myShape, TopAbs_EDGE);
445           for(Exp; Exp.More(); Exp.Next()) {
446             TopoDS_Edge E = TopoDS::Edge(Exp.Current());
447             fill.Add(E);
448           }
449           
450           for(int i = 1; i <= fill.NbContours(); i++) {
451             try {
452               fill.SetRadius(myRadius,i,i);
453             }  
454             catch(Standard_Failure) {
455               QApplication::restoreOverrideCursor();
456               QAD_MessageBox::warn1 (QAD_Application::getDesktop(), tr("GEOM_WRN_WARNING"), tr("GEOM_FILLET_ABORT").arg(myRadius), tr("GEOM_BUT_OK"));
457               QApplication::setOverrideCursor(Qt::waitCursor);
458               myGeomBase->EraseSimulationShape(); 
459               mySimulationTopoDs.Nullify();
460               Group1->SpinBox_DX->SetValue(5.0);
461               QApplication::restoreOverrideCursor();
462               return;
463             }
464           }
465           tds = fill.Shape();
466           break;
467         }
468       //    case 1: /* Fillet edges */
469       //    case 2: /* Fillet Faces */
470       }
471
472     if(!tds.IsNull()) {
473       mySimulationTopoDs = tds;
474       myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
475     }
476   }  
477   catch(Standard_Failure) {
478     QApplication::restoreOverrideCursor();
479     QAD_MessageBox::warn1 (QAD_Application::getDesktop(), tr("GEOM_WRN_WARNING"), tr("GEOM_FILLET_ABORT").arg(myRadius), tr("GEOM_BUT_OK"));
480     QApplication::setOverrideCursor(Qt::waitCursor);
481     myGeomBase->EraseSimulationShape(); 
482     mySimulationTopoDs.Nullify();
483     Group1->SpinBox_DX->SetValue(5.0);
484   }
485
486   QApplication::restoreOverrideCursor();
487   return;
488 }