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