Salome HOME
DCQ : New Architecture
[modules/geom.git] / src / RepairGUI / RepairGUI_SuppressFacesDlg.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   : RepairGUI_SuppressFacesDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "RepairGUI_SuppressFacesDlg.h"
31
32 #include "DisplayGUI.h"
33 #include "QAD_RightFrame.h"
34 #include "OCCViewer_Viewer3d.h"
35
36 //=================================================================================
37 // class    : RepairGUI_SuppressFacesDlg()
38 // purpose  : Constructs a RepairGUI_SuppressFacesDlg  which is a child of 'parent', with the
39 //            name 'name' and widget flags set to 'f'.
40 //            The dialog will by default be modeless, unless you set 'modal' to
41 //            TRUE to construct a modal dialog.
42 //=================================================================================
43 RepairGUI_SuppressFacesDlg::RepairGUI_SuppressFacesDlg(QWidget* parent, const char* name, RepairGUI* theRepairGUI, SALOME_Selection* Sel, Handle(AIS_InteractiveContext) ic, bool modal, WFlags fl)
44   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
45 {
46   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_SUPRESS_FACE")));
47   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
48     
49   setCaption(tr("GEOM_SUPRESSFACE_TITLE"));
50
51   /***************************************************************/
52   GroupConstructors->setTitle(tr("GEOM_SUPRESSFACE"));
53   RadioButton1->setPixmap(image0);
54   RadioButton2->close(TRUE);
55   RadioButton3->close(TRUE);
56
57   GroupPoints = new DlgRef_1Sel1Check_QTD(this, "GroupPoints");
58   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
59   GroupPoints->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
60   GroupPoints->CheckButton1->setText(tr("GEOM_SUPRESSFACE_SELECT"));
61   GroupPoints->PushButton1->setPixmap(image1);
62
63   Layout1->addWidget(GroupPoints, 1, 0);
64   /***************************************************************/
65
66   /* Initialisations */
67   myRepairGUI = theRepairGUI;
68   Init(ic);
69 }
70
71
72 //=================================================================================
73 // function : ~RepairGUI_SuppressFacesDlg()
74 // purpose  : Destroys the object and frees any allocated resources
75 //=================================================================================
76 RepairGUI_SuppressFacesDlg::~RepairGUI_SuppressFacesDlg()
77 {
78   // no need to delete child widgets, Qt does it all for us
79 }
80
81
82 //=================================================================================
83 // function : Init()
84 // purpose  :
85 //=================================================================================
86 void RepairGUI_SuppressFacesDlg::Init(Handle(AIS_InteractiveContext) ic)
87 {
88   /* init variables */
89   myEditCurrentArgument = GroupPoints->LineEdit1;
90
91   myOkSelectSubMode = false;
92   myOkShape = myUseLocalContext = false;
93   myIC = ic;
94   myLocalContextId = -1;
95
96   /* signals and slots connections */
97   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
98   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
99   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
100
101   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
102   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
103
104   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
105   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
106
107   connect(GroupPoints->CheckButton1, SIGNAL(stateChanged(int)), this, SLOT(ActivateUserSelection()));
108   
109   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
110
111   /* displays Dialog */
112   GroupPoints->show();
113   this->show();
114
115   return;
116 }
117
118
119 //=================================================================================
120 // function : ClickOnOk()
121 // purpose  : Same than click on apply but close this dialog.
122 //=================================================================================
123 void RepairGUI_SuppressFacesDlg::ClickOnOk()
124 {
125   this->ClickOnApply();
126   this->ClickOnCancel();
127   return;
128 }
129
130
131
132 //=================================================================================
133 // function : ClickOnApply()
134 // purpose  :
135 //=================================================================================
136 void RepairGUI_SuppressFacesDlg::ClickOnApply()
137 {
138   QAD_Application::getDesktop()->putInfo(tr(""));
139   bool testResult = false;
140
141   if(myOkShape && myOkSelectSubMode)
142     testResult = myRepairGUI->OnSuppressFaces(myShape, myShapeIOR, myLocalContextId, myUseLocalContext); 
143
144   if(!testResult)
145     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
146   else
147     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
148
149   /* Reset arguments to allow a new selection */
150   this->ResetStateOfDialog();
151   return ;
152 }
153
154
155 //=================================================================================
156 // function : ClickOnCancel()
157 // purpose  :
158 //=================================================================================
159 void RepairGUI_SuppressFacesDlg::ClickOnCancel()
160 {
161   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
162     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
163     myIC = v3d->getAISContext();
164     if(myUseLocalContext) {
165       myIC->CloseLocalContext(myLocalContextId);
166       myUseLocalContext = false;
167     }
168     DisplayGUI* myDisplayGUI = new DisplayGUI();
169     myDisplayGUI->OnDisplayAll(true);
170   }
171   GEOMBase_Skeleton::ClickOnCancel();
172   return;
173 }
174
175
176 //=================================================================================
177 // function : SelectionIntoArgument()
178 // purpose  : Called when selection as changed or other case
179 //          : used only by SelectButtonC1A1 (LineEditC1A1)
180 //=================================================================================
181 void RepairGUI_SuppressFacesDlg::SelectionIntoArgument()
182 {
183   myEditCurrentArgument->setText("");
184   this->ResetStateOfDialog();
185   QString aString = ""; /* name of selection */
186
187   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
188   if(nbSel != 1) {
189     myOkShape = false;
190     return;
191   }
192   
193   /* nbSel == 1 */
194   TopoDS_Shape S;
195   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject();
196   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
197     return;
198   
199   if(!IO->hasEntry()) {
200     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_SHAPE_IN_STUDY"));
201     return;
202   }
203
204   /* Test the exact type of topology to suppress faces into */
205   if(!S.IsNull() && (S.ShapeType() == TopAbs_SOLID || S.ShapeType() == TopAbs_SHELL || S.ShapeType() == TopAbs_COMPOUND)) {
206     if(IO->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject))) {
207       Handle(GEOM_InteractiveObject) GIObject = Handle(GEOM_InteractiveObject)::DownCast(IO);
208       myShapeIOR = GIObject->getIOR(); /* the Geom IOR string of selection */
209       myEditCurrentArgument->setText(aString);
210       myShape = S;
211       myOkShape = true;
212       return;
213     } 
214     
215     if(IO->hasEntry()) {
216       SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
217       SALOMEDS::SObject_var obj = aStudy->FindObjectID(IO->getEntry());
218       SALOMEDS::GenericAttribute_var anAttr;
219       SALOMEDS::AttributeIOR_var anIOR;
220       if(!obj->_is_nil()) {
221         if(obj->FindAttribute(anAttr, "AttributeIOR")) {
222           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
223           myShapeIOR = anIOR->Value();
224           myOkShape = true;
225           myShape = S;
226           myEditCurrentArgument->setText(aString);
227           return;
228         }
229       }
230     }
231
232   }
233   return;
234 }
235
236
237 //=================================================================================
238 // function : SetEditCurrentArgument()
239 // purpose  :
240 //=================================================================================
241 void RepairGUI_SuppressFacesDlg::SetEditCurrentArgument()
242 {
243   QPushButton* send = (QPushButton*)sender();
244
245   if(send == GroupPoints->PushButton1) {
246     GroupPoints->LineEdit1->setFocus();
247     myEditCurrentArgument = GroupPoints->LineEdit1;
248   }
249   this->SelectionIntoArgument();
250
251   return;
252 }
253
254
255 //=================================================================================
256 // function : LineEditReturnPressed()
257 // purpose  :
258 //=================================================================================
259 void RepairGUI_SuppressFacesDlg::LineEditReturnPressed()
260 {
261   QLineEdit* send = (QLineEdit*)sender();
262   if(send == GroupPoints->LineEdit1)
263     myEditCurrentArgument = GroupPoints->LineEdit1;
264   else
265     return;
266
267   GEOMBase_Skeleton::LineEditReturnPressed();
268   return;
269 }
270
271
272 //=================================================================================
273 // function : DeactivateActiveDialog()
274 // purpose  :
275 //=================================================================================
276 void RepairGUI_SuppressFacesDlg::DeactivateActiveDialog()
277 {
278   if(GroupConstructors->isEnabled()) {
279     this->ResetStateOfDialog();
280     GEOMBase_Skeleton::DeactivateActiveDialog();
281   }
282   return;
283 }
284
285
286 //=================================================================================
287 // function : ActivateThisDialog()
288 // purpose  :
289 //=================================================================================
290 void RepairGUI_SuppressFacesDlg::ActivateThisDialog()
291 {
292   GEOMBase_Skeleton::ActivateThisDialog();
293   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
294   return;
295 }
296
297
298 //=================================================================================
299 // function : enterEvent()
300 // purpose  : Mouse enter onto the dialog to activate it
301 //=================================================================================
302 void RepairGUI_SuppressFacesDlg::enterEvent(QEvent* e)
303 {
304   if(GroupConstructors->isEnabled())
305     return;
306   this->ActivateThisDialog();
307   return;
308 }
309
310
311 //=================================================================================
312 // function : closeEvent()
313 // purpose  :
314 //=================================================================================
315 void RepairGUI_SuppressFacesDlg::closeEvent(QCloseEvent* e)
316 {
317   /* same than click on cancel button */
318   this->ClickOnCancel();
319   return;
320 }
321
322
323 //=================================================================================
324 // function : ActivateUserSelection()
325 // purpose  : Activate selection of faces when CheckBox1->isChecked()...
326 //=================================================================================
327 void RepairGUI_SuppressFacesDlg::ActivateUserSelection()
328 {
329   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
330     if(!myOkShape) {
331       this->ResetStateOfDialog();
332       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_SELECT_FIRST"));
333       return;
334     }
335   
336     myOkSelectSubMode = GroupPoints->CheckButton1->isChecked();
337     
338     if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
339       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
340       myIC = v3d->getAISContext();
341       if(myUseLocalContext) {
342         myIC->CloseLocalContext(myLocalContextId);
343         myUseLocalContext = false;
344       }
345       DisplayGUI* myDisplayGUI = new DisplayGUI();
346       myDisplayGUI->OnDisplayAll(true);
347     }
348     
349     if(myOkShape && myOkSelectSubMode) {
350       /* local context is defined into the method : 4 = FACES sub selection */
351       DisplayGUI* myDisplayGUI = new DisplayGUI();
352       myDisplayGUI->PrepareSubShapeSelection(int(TopAbs_FACE), myLocalContextId);    
353       myUseLocalContext = true;
354       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_SELECT_FACE"));
355     }
356   } 
357   else
358     GroupPoints->CheckButton1->setChecked(false);
359   return;
360 }
361
362
363
364 //=================================================================================
365 // function : ResetStateOfDialog()
366 // purpose  : Completely reset the state of method including local context
367 //=================================================================================
368 void RepairGUI_SuppressFacesDlg::ResetStateOfDialog()
369 {
370   myOkShape = false ;
371   myEditCurrentArgument->setText("");
372
373   /* Select sub shapes mode not checked */
374   myOkSelectSubMode = false;    
375   GroupPoints->CheckButton1->setChecked(FALSE);
376
377   /* Close its local contact if opened */
378   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
379     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
380     myIC = v3d->getAISContext();
381     if(myUseLocalContext) {
382       myIC->CloseLocalContext(myLocalContextId);
383       myUseLocalContext = false;
384     }
385     DisplayGUI* myDisplayGUI = new DisplayGUI();
386     myDisplayGUI->OnDisplayAll(true);
387   }
388   return;
389 }