Salome HOME
Merge from V5_1_main 14/05/2010
[modules/geom.git] / src / RepairGUI / RepairGUI_SuppressFacesDlg.cxx
1 //  Copyright (C) 2007-2010  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   : RepairGUI_SuppressFacesDlg.cxx
25 //  Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "RepairGUI_SuppressFacesDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Application.h>
36 #include <SalomeApp_Study.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <SALOME_ListIteratorOfListIO.hxx>
39
40 // OCCT Includes
41 #include <TopAbs.hxx>
42 #include <TopExp.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <TColStd_MapOfInteger.hxx>
46 #include <TColStd_IndexedMapOfInteger.hxx>
47
48 #include <GEOMImpl_Types.hxx>
49
50 //=================================================================================
51 // class    : RepairGUI_SuppressFacesDlg()
52 // purpose  : Constructs a RepairGUI_SuppressFacesDlg  which is a child of 'parent', with the
53 //            name 'name' and widget flags set to 'f'.
54 //            The dialog will by default be modeless, unless you set 'modal' to
55 //            TRUE to construct a modal dialog.
56 //=================================================================================
57 RepairGUI_SuppressFacesDlg::RepairGUI_SuppressFacesDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
58                                                         bool modal)
59   : GEOMBase_Skeleton(theGeometryGUI, parent, modal)
60 {
61   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_SUPRESS_FACE")));
62   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
63
64   setWindowTitle(tr("GEOM_SUPRESSFACE_TITLE"));
65
66   /***************************************************************/
67   mainFrame()->GroupConstructors->setTitle(tr("GEOM_SUPRESSFACE"));
68   mainFrame()->RadioButton1->setIcon(image0);
69   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
70   mainFrame()->RadioButton2->close();
71   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
72   mainFrame()->RadioButton3->close();
73
74   GroupArgs = new DlgRef_2Sel(centralWidget());
75   GroupArgs->GroupBox1->setTitle(tr("Faces to remove"));
76   GroupArgs->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
77   GroupArgs->PushButton1->setIcon(image1);
78   GroupArgs->LineEdit1->setReadOnly(true);
79
80   GroupArgs->TextLabel2->setText(tr("Faces to remove"));
81   GroupArgs->PushButton2->setIcon(image1);
82   GroupArgs->LineEdit2->setReadOnly(true);
83
84   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
85   layout->setMargin(0); layout->setSpacing(6);
86   layout->addWidget(GroupArgs);
87   /***************************************************************/
88
89   setHelpFileName("suppress_faces_operation_page.html");
90
91   Init();
92 }
93
94 //=================================================================================
95 // function : ~RepairGUI_SuppressFacesDlg()
96 // purpose  : Destroys the object and frees any allocated resources
97 //=================================================================================
98 RepairGUI_SuppressFacesDlg::~RepairGUI_SuppressFacesDlg()
99 {
100 }
101
102 //=================================================================================
103 // function : Init()
104 // purpose  :
105 //=================================================================================
106 void RepairGUI_SuppressFacesDlg::Init()
107 {
108   // init variables
109   GroupArgs->LineEdit1->clear();
110   GroupArgs->LineEdit2->clear();
111   myObject = GEOM::GEOM_Object::_nil();
112   myFacesInd = new GEOM::short_array();
113   myFacesInd->length(0);
114   
115   mainFrame()->GroupBoxPublish->show();
116
117   // signals and slots connections
118   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
119   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
120
121   connect(GroupArgs->PushButton1, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
122   connect(GroupArgs->PushButton2, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
123
124   connect(GroupArgs->LineEdit1,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
125   connect(GroupArgs->LineEdit2,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
126
127   initName(tr("SUPRESS_FACE_NEW_OBJ_NAME"));
128
129   GroupArgs->PushButton1->click();
130   SelectionIntoArgument();
131   resize(100,100);
132 }
133
134 //=================================================================================
135 // function : ClickOnOk()
136 // purpose  : Same than click on apply but close this dialog.
137 //=================================================================================
138 void RepairGUI_SuppressFacesDlg::ClickOnOk()
139 {
140   if (ClickOnApply())
141     ClickOnCancel();
142 }
143
144 //=================================================================================
145 // function : ClickOnApply()
146 // purpose  :
147 //=================================================================================
148 bool RepairGUI_SuppressFacesDlg::ClickOnApply()
149 {
150   if (!onAccept())
151     return false;
152
153   initName();
154   // activate first line edit
155   GroupArgs->PushButton1->click();
156   return true;
157 }
158
159 //=================================================================================
160 // function : SelectionIntoArgument()
161 // purpose  : Called when selection is changed or on dialog initialization or activation
162 //=================================================================================
163 void RepairGUI_SuppressFacesDlg::SelectionIntoArgument()
164 {
165   myEditCurrentArgument->setText("");
166   // the second argument depends on the first one
167   GroupArgs->LineEdit2->setText("");
168   myFacesInd->length(0);
169
170   if (myEditCurrentArgument == GroupArgs->LineEdit1)
171     myObject = GEOM::GEOM_Object::_nil();
172
173   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
174   SALOME_ListIO aSelList;
175   aSelMgr->selectedObjects(aSelList);
176
177   if (myEditCurrentArgument == GroupArgs->LineEdit1) {
178     if (aSelList.Extent() == 1) {
179       Handle(SALOME_InteractiveObject) anIO = aSelList.First();
180
181       Standard_Boolean aRes;
182       myObject = GEOMBase::ConvertIOinGEOMObject(anIO, aRes);
183       if (aRes && GEOMBase::IsShape(myObject)) {
184         myEditCurrentArgument->setText(GEOMBase::GetName(myObject));
185         TopoDS_Shape aShape;
186         if (GEOMBase::GetShape(myObject, aShape, TopAbs_FACE))
187           GroupArgs->LineEdit2->setText(myEditCurrentArgument->text());
188
189         // clear selection
190         disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
191         myGeomGUI->getApp()->selectionMgr()->clearSelected();
192         connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
193                 this, SLOT(SelectionIntoArgument()));
194
195         GroupArgs->PushButton2->click();
196       }
197       else
198         myObject = GEOM::GEOM_Object::_nil();
199     }
200   }
201   else if (myEditCurrentArgument == GroupArgs->LineEdit2) {
202     TColStd_IndexedMapOfInteger aMap;
203
204     if (aSelList.Extent() == 1) {
205       Handle(SALOME_InteractiveObject) anIO = aSelList.First();
206       aSelMgr->GetIndexes(anIO, aMap);
207     }
208
209     if (aMap.IsEmpty() && aSelList.Extent() > 0) { // try to detect selected published sub-shape
210       SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>(myGeomGUI->getApp()->activeStudy());
211       if (!appStudy) return;
212       _PTR(Study) aStudy = appStudy->studyDS();
213
214       TopTools_IndexedMapOfShape aMainMap;
215       TopoDS_Shape aMainShape = GEOM_Client().GetShape(GeometryGUI::GetGeomGen(), myObject);
216       TopExp::MapShapes(aMainShape, aMainMap);
217
218       SALOME_ListIteratorOfListIO anIter (aSelList);
219       for (int i = 0; anIter.More(); anIter.Next(), i++) {
220         Handle(SALOME_InteractiveObject) anIO = anIter.Value();
221         QString anEntry = anIO->getEntry();
222
223         _PTR(SObject) aSObj (aStudy->FindObjectID(anEntry.toLatin1().constData()));
224         GEOM::GEOM_Object_var aGeomObj =
225           GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
226         TopoDS_Shape aShape;
227         if (GEOMBase::GetShape(aGeomObj, aShape)) {
228           if (aGeomObj->GetType() == GEOM_GROUP || aShape.ShapeType() == TopAbs_FACE) {
229             TopExp_Explorer anExp (aShape, TopAbs_FACE);
230             for (; anExp.More(); anExp.Next()) {
231               TopoDS_Shape aSubShape = anExp.Current();
232               int anIndex = aMainMap.FindIndex(aSubShape);
233               if (anIndex >= 0) {
234                 aMap.Add(anIndex);
235               }
236             }
237           }
238         }
239       }
240     }
241
242     const int n = aMap.Extent();
243     myFacesInd->length(n);
244     for (int i = 1; i <= n; i++)
245       myFacesInd[i-1] = aMap(i);
246     if (n)
247       myEditCurrentArgument->setText(QString::number(n) + "_" + tr("GEOM_FACE") + tr("_S_"));
248   }
249 }
250
251 //=================================================================================
252 // function : SetEditCurrentArgument()
253 // purpose  :
254 //=================================================================================
255 void RepairGUI_SuppressFacesDlg::SetEditCurrentArgument()
256 {
257   QPushButton* send = (QPushButton*)sender();
258
259   bool isEffective = false;
260
261   if (send == GroupArgs->PushButton1) {
262     isEffective = true;
263     myEditCurrentArgument = GroupArgs->LineEdit1;
264
265     GroupArgs->PushButton2->setDown(false);
266     GroupArgs->LineEdit2->setEnabled(false);
267   }
268   else if (send == GroupArgs->PushButton2 && !myObject->_is_nil()) {
269     isEffective = true;
270     myEditCurrentArgument = GroupArgs->LineEdit2;
271
272     GroupArgs->PushButton1->setDown(false);
273     GroupArgs->LineEdit1->setEnabled(false);
274   }
275
276   if (isEffective) {
277     initSelection();
278
279     // enable line edit
280     myEditCurrentArgument->setEnabled(true);
281     myEditCurrentArgument->setFocus();
282     // after setFocus(), because it will be setDown(false) when loses focus
283     send->setDown(true);
284   }
285 }
286
287 //=================================================================================
288 // function : LineEditReturnPressed()
289 // purpose  :
290 //=================================================================================
291 void RepairGUI_SuppressFacesDlg::LineEditReturnPressed()
292 {
293   const QObject* send = sender();
294   if (send == GroupArgs->LineEdit1 || send == GroupArgs->LineEdit2) {
295     myEditCurrentArgument = (QLineEdit*)send;
296     GEOMBase_Skeleton::LineEditReturnPressed();
297   }
298 }
299
300 //=================================================================================
301 // function : ActivateThisDialog()
302 // purpose  :
303 //=================================================================================
304 void RepairGUI_SuppressFacesDlg::ActivateThisDialog()
305 {
306   GEOMBase_Skeleton::ActivateThisDialog();
307   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
308            this, SLOT( SelectionIntoArgument() ) );
309
310   myEditCurrentArgument = GroupArgs->LineEdit1;
311   myEditCurrentArgument->setText( "" );
312   GroupArgs->LineEdit2->setText( "" );
313   myObject = GEOM::GEOM_Object::_nil();
314   myFacesInd->length( 0 );
315
316   initSelection();
317 }
318
319 //=================================================================================
320 // function : enterEvent()
321 // purpose  : Mouse enter onto the dialog to activate it
322 //=================================================================================
323 void RepairGUI_SuppressFacesDlg::enterEvent (QEvent*)
324 {
325   if (!mainFrame()->GroupConstructors->isEnabled())
326     ActivateThisDialog();
327 }
328
329 //=================================================================================
330 // function : createOperation
331 // purpose  :
332 //=================================================================================
333 GEOM::GEOM_IOperations_ptr RepairGUI_SuppressFacesDlg::createOperation()
334 {
335   return getGeomEngine()->GetIHealingOperations(getStudyId());
336 }
337
338 //=================================================================================
339 // function : isValid
340 // purpose  :
341 //=================================================================================
342 bool RepairGUI_SuppressFacesDlg::isValid (QString&)
343 {
344   TopoDS_Shape aTmpShape;
345   return !myObject->_is_nil() && (myFacesInd->length() ||
346                                   GEOMBase::GetShape(myObject, aTmpShape, TopAbs_WIRE));
347 }
348
349 //=================================================================================
350 // function : execute
351 // purpose  :
352 //=================================================================================
353 bool RepairGUI_SuppressFacesDlg::execute (ObjectList& objects)
354 {
355   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow(getOperation());
356   GEOM::GEOM_Object_var anObj = anOper->SuppressFaces(myObject, myFacesInd);
357
358   bool aResult = !anObj->_is_nil();
359   if (aResult)
360     objects.push_back(anObj._retn());
361
362   return aResult;
363 }
364
365 //=================================================================================
366 // function : initSelection
367 // purpose  :
368 //=================================================================================
369 void RepairGUI_SuppressFacesDlg::initSelection()
370 {
371   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
372
373   if (myEditCurrentArgument == GroupArgs->LineEdit1) {
374     TColStd_MapOfInteger aTypes;
375     aTypes.Add(GEOM_COMPOUND);
376     aTypes.Add(GEOM_SOLID);
377     aTypes.Add(GEOM_SHELL);
378     aTypes.Add(GEOM_FACE);
379
380     globalSelection(aTypes);
381   }
382   else if (myEditCurrentArgument == GroupArgs->LineEdit2) {
383     localSelection(myObject, TopAbs_FACE);
384   }
385
386   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
387           this, SLOT(SelectionIntoArgument()));
388 }
389
390 //=================================================================================
391 // function : restoreSubShapes
392 // purpose  :
393 //=================================================================================
394 void RepairGUI_SuppressFacesDlg::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
395                                                    SALOMEDS::SObject_ptr theSObject)
396 {
397   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
398     // empty list of arguments means that all arguments should be restored
399     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
400                                         /*theFindMethod=*/GEOM::FSM_GetInPlace,
401                                         /*theInheritFirstArg=*/true,
402                                         mainFrame()->CheckBoxAddPrefix->isChecked());
403   }
404 }