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