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