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