Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / RepairGUI / RepairGUI_RemoveHolesDlg.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_RemoveHolesDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25 //
26 #include "RepairGUI_RemoveHolesDlg.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 <SUIT_MessageBox.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 // OCCT Includes
39 #include <TopAbs.hxx>
40 #include <TColStd_MapOfInteger.hxx>
41 #include <TColStd_IndexedMapOfInteger.hxx>
42
43 #include <GEOMImpl_Types.hxx>
44
45 //=================================================================================
46 // class    : RepairGUI_RemoveHolesDlg()
47 // purpose  : Constructs a RepairGUI_RemoveHolesDlg  which is a child of 'parent', with the
48 //            name 'name' and widget flags set to 'f'.
49 //            The dialog will by default be modeless, unless you set 'modal' to
50 //            TRUE to construct a modal dialog.
51 //=================================================================================
52 RepairGUI_RemoveHolesDlg::RepairGUI_RemoveHolesDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
53                                                     bool modal)
54   : GEOMBase_Skeleton(theGeometryGUI, parent, modal)
55 {
56   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_SUPPRESS_HOLES")));
57   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
58
59   setWindowTitle(tr("GEOM_REMOVE_HOLES_TITLE"));
60
61   /***************************************************************/
62   mainFrame()->GroupConstructors->setTitle(tr("GEOM_REMOVE_HOLES_TITLE"));
63   mainFrame()->RadioButton1->setIcon(image0);
64   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
65   mainFrame()->RadioButton2->close();
66   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
67   mainFrame()->RadioButton3->close();
68
69   GroupPoints = new DlgRef_1Sel1Check1Sel(centralWidget());
70   GroupPoints->GroupBox1->setTitle(tr("GEOM_HOLES"));
71   GroupPoints->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
72   GroupPoints->PushButton1->setIcon(image1);
73   GroupPoints->LineEdit1->setReadOnly(true);
74   GroupPoints->CheckButton1->setText(tr("GEOM_REMOVE_ALL_HOLES"));
75   GroupPoints->TextLabel2->setText(tr("GEOM_WIRES_TO_REMOVE"));
76   GroupPoints->PushButton2->setIcon(image1);
77   GroupPoints->LineEdit2->setReadOnly(true);
78
79   myFreeBoundBtn = new QPushButton(tr("GEOM_DETECT") + QString(" [%1]").arg(tr("GEOM_FREE_BOUNDARIES")),
80                                     GroupPoints->Box);
81   QVBoxLayout* l = new QVBoxLayout(GroupPoints->Box);
82   l->setMargin(0); l->setSpacing(0);
83   l->addWidget(myFreeBoundBtn);
84
85   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
86   layout->setMargin(0); layout->setSpacing(6);
87   layout->addWidget(GroupPoints);
88   /***************************************************************/
89
90   setHelpFileName("suppress_holes_operation_page.html");
91
92   Init();
93 }
94
95 //=================================================================================
96 // function : ~RepairGUI_RemoveHolesDlg()
97 // purpose  : Destroys the object and frees any allocated resources
98 //=================================================================================
99 RepairGUI_RemoveHolesDlg::~RepairGUI_RemoveHolesDlg()
100 {
101 }
102
103 //=================================================================================
104 // function : Init()
105 // purpose  :
106 //=================================================================================
107 void RepairGUI_RemoveHolesDlg::Init()
108 {
109   // init variables
110   GroupPoints->LineEdit1->clear();
111   GroupPoints->LineEdit2->clear();
112   myObject = GEOM::GEOM_Object::_nil();
113   myWiresInd = new GEOM::short_array();
114   myWiresInd->length(0);
115
116   myClosed = -1;
117   myOpen = -1;
118
119   // signals and slots connections
120   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
121   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
122
123   connect(GroupPoints->PushButton1, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
124   connect(GroupPoints->PushButton2, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
125
126   connect(GroupPoints->LineEdit1,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
127   connect(GroupPoints->LineEdit2,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
128
129   connect(GroupPoints->CheckButton1, SIGNAL(clicked()), this, SLOT(onRemoveAllClicked()));
130
131   connect(myFreeBoundBtn, SIGNAL(clicked()), this, SLOT(onDetect()));
132
133   initName(tr("REMOVE_HOLES_NEW_OBJ_NAME"));
134
135   GroupPoints->PushButton1->click();
136   SelectionIntoArgument();
137 }
138
139 //=================================================================================
140 // function : ClickOnOk()
141 // purpose  : Same than click on apply but close this dialog.
142 //=================================================================================
143 void RepairGUI_RemoveHolesDlg::ClickOnOk()
144 {
145   if (ClickOnApply())
146     ClickOnCancel();
147 }
148
149 //=================================================================================
150 // function : ClickOnApply()
151 // purpose  :
152 //=================================================================================
153 bool RepairGUI_RemoveHolesDlg::ClickOnApply()
154 {
155   if (!onAccept())
156     return false;
157
158   initName();
159   // activate first line edit
160   GroupPoints->PushButton1->click();
161   return true;
162 }
163
164 //=================================================================================
165 // function : SelectionIntoArgument()
166 // purpose  : Called when selection is changed or on dialog initialization or activation
167 //=================================================================================
168 void RepairGUI_RemoveHolesDlg::SelectionIntoArgument()
169 {
170   erasePreview();
171   myEditCurrentArgument->setText("");
172   // the second argument depends on the first one
173   GroupPoints->LineEdit2->setText("");
174   myWiresInd->length(0);
175
176   if (myEditCurrentArgument == GroupPoints->LineEdit1)
177     myObject = GEOM::GEOM_Object::_nil();
178
179   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
180   SALOME_ListIO aSelList;
181   aSelMgr->selectedObjects(aSelList);
182
183   if (aSelList.Extent() == 1) {
184     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
185
186     if (myEditCurrentArgument == GroupPoints->LineEdit1) { // face selection
187       Standard_Boolean aRes;
188       myObject = GEOMBase::ConvertIOinGEOMObject(anIO, aRes);
189       if (aRes && GEOMBase::IsShape(myObject)) {
190         myEditCurrentArgument->setText(GEOMBase::GetName(myObject));
191
192         // clear selection
193         disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
194         myGeomGUI->getApp()->selectionMgr()->clearSelected();
195         connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
196                 this, SLOT(SelectionIntoArgument()));
197
198         if (!GroupPoints->CheckButton1->isChecked())
199           GroupPoints->PushButton2->click();
200       }
201       else
202         myObject = GEOM::GEOM_Object::_nil();
203     }
204     else if (myEditCurrentArgument == GroupPoints->LineEdit2 &&
205              !GroupPoints->CheckButton1->isChecked()) {
206       TColStd_IndexedMapOfInteger aMap;
207       aSelMgr->GetIndexes(anIO, aMap);
208       const int n = aMap.Extent();
209       myWiresInd->length(n);
210       for (int i = 1; i <= n; i++)
211         myWiresInd[i-1] = aMap(i);
212       if (n)
213         myEditCurrentArgument->setText(QString::number(n) + "_" + tr("GEOM_WIRE") + tr("_S_"));
214     }
215   }
216 }
217
218 //=================================================================================
219 // function : SetEditCurrentArgument()
220 // purpose  :
221 //=================================================================================
222 void RepairGUI_RemoveHolesDlg::SetEditCurrentArgument()
223 {
224   QPushButton* send = (QPushButton*)sender();
225
226   bool isEffective = false;
227
228   if (send == GroupPoints->PushButton1) {
229     isEffective = true;
230     myEditCurrentArgument = GroupPoints->LineEdit1;
231
232     GroupPoints->PushButton2->setDown(false);
233     GroupPoints->LineEdit2->setEnabled(false);
234   }
235   else if (send == GroupPoints->PushButton2 && !myObject->_is_nil()) {
236     isEffective = true;
237     myEditCurrentArgument = GroupPoints->LineEdit2;
238
239     GroupPoints->PushButton1->setDown(false);
240     GroupPoints->LineEdit1->setEnabled(false);
241   }
242
243   if (isEffective) {
244     initSelection();
245
246     // enable line edit
247     myEditCurrentArgument->setEnabled(true);
248     myEditCurrentArgument->setFocus();
249     // after setFocus(), because it will be setDown(false) when loses focus
250     send->setDown(true);
251   }
252 }
253
254 //=================================================================================
255 // function : LineEditReturnPressed()
256 // purpose  :
257 //=================================================================================
258 void RepairGUI_RemoveHolesDlg::LineEditReturnPressed()
259 {
260   const QObject* send = sender();
261   if (send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2) {
262     myEditCurrentArgument = (QLineEdit*)send;
263     GEOMBase_Skeleton::LineEditReturnPressed();
264   }
265 }
266
267 //=================================================================================
268 // function : ActivateThisDialog()
269 // purpose  :
270 //=================================================================================
271 void RepairGUI_RemoveHolesDlg::ActivateThisDialog()
272 {
273   GEOMBase_Skeleton::ActivateThisDialog();
274   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
275            this, SLOT( SelectionIntoArgument() ) );
276
277   myEditCurrentArgument = GroupPoints->LineEdit1;
278   myEditCurrentArgument->setText( "" );
279   GroupPoints->LineEdit2->setText( "" );
280   myObject = GEOM::GEOM_Object::_nil();
281   myWiresInd->length( 0 );
282
283   myClosed = -1;
284   myOpen = -1;
285
286   initSelection();
287 }
288
289 //=================================================================================
290 // function : enterEvent()
291 // purpose  : Mouse enter onto the dialog to activate it
292 //=================================================================================
293 void RepairGUI_RemoveHolesDlg::enterEvent (QEvent*)
294 {
295   if (!mainFrame()->GroupConstructors->isEnabled())
296     ActivateThisDialog();
297 }
298
299 //=================================================================================
300 // function : createOperation
301 // purpose  :
302 //=================================================================================
303 GEOM::GEOM_IOperations_ptr RepairGUI_RemoveHolesDlg::createOperation()
304 {
305   return getGeomEngine()->GetIHealingOperations(getStudyId());
306 }
307
308 //=================================================================================
309 // function : isValid
310 // purpose  :
311 //=================================================================================
312 bool RepairGUI_RemoveHolesDlg::isValid (QString&)
313 {
314   myClosed = -1;
315   return !myObject->_is_nil() && (IsPreview() || GroupPoints->CheckButton1->isChecked() || myWiresInd->length());
316 }
317
318 //=================================================================================
319 // function : execute
320 // purpose  :
321 //=================================================================================
322 bool RepairGUI_RemoveHolesDlg::execute (ObjectList& objects)
323 {
324   bool aResult = false;
325
326   if (IsPreview()) {
327     // called from onDetect(): detect free boundary edges,
328     // highlight them (add to objects), display message dialog
329     GEOM::ListOfGO_var aClosed, anOpen;
330
331     aResult = GEOM::GEOM_IHealingOperations::_narrow(getOperation())->
332       GetFreeBoundary(myObject, aClosed, anOpen);
333
334     if (aResult) {
335       myClosed = aClosed->length();
336       myOpen = anOpen->length();
337       int i;
338       for (i = 0; i < myClosed; i++)
339         objects.push_back(aClosed[i]._retn());
340       for (i = 0; i < myOpen; i++)
341         objects.push_back(anOpen[i]._retn());
342     }
343     else
344       myClosed = -1;
345   }
346   else {
347     GEOM::GEOM_Object_var anObj = GEOM::GEOM_IHealingOperations::_narrow(getOperation())->
348       FillHoles(myObject, myWiresInd);
349     aResult = !anObj->_is_nil();
350     if (aResult)
351       objects.push_back(anObj._retn());
352   }
353
354   return aResult;
355 }
356
357 //=================================================================================
358 // function : onRemoveAllClicked
359 // purpose  :
360 //=================================================================================
361 void RepairGUI_RemoveHolesDlg::onRemoveAllClicked()
362 {
363   bool b = GroupPoints->CheckButton1->isChecked();
364   GroupPoints->TextLabel2->setEnabled(!b);
365   GroupPoints->PushButton2->setEnabled(!b);
366   GroupPoints->LineEdit2->setEnabled(!b);
367   if (b) {
368     GroupPoints->LineEdit2->setText("");
369     myWiresInd->length(0);
370   }
371 }
372
373 //=================================================================================
374 // function : initSelection
375 // purpose  :
376 //=================================================================================
377 void RepairGUI_RemoveHolesDlg::initSelection()
378 {
379   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
380
381   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
382     TColStd_MapOfInteger aTypes;
383     aTypes.Add(GEOM_COMPOUND);
384     aTypes.Add(GEOM_SOLID);
385     aTypes.Add(GEOM_SHELL);
386     aTypes.Add(GEOM_FACE);
387
388     globalSelection(aTypes);
389   }
390   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
391     localSelection(myObject, TopAbs_EDGE);
392     localSelection(myObject, TopAbs_WIRE);
393   }
394
395   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
396           this, SLOT(SelectionIntoArgument()));
397 }
398
399 //=================================================================================
400 // function : onDetect
401 // purpose  :
402 //=================================================================================
403 void RepairGUI_RemoveHolesDlg::onDetect()
404 {
405   displayPreview(false, true, true, 3);
406
407   // field myClosed,myOpen is initialized in execute() method, called by displayPreview().
408   QString msg;
409   if (myClosed != -1)
410     msg = tr("GEOM_FREE_BOUNDS_MSG").arg(myClosed + myOpen).arg(myClosed).arg(myOpen);
411   else
412     msg = tr("GEOM_FREE_BOUNDS_ERROR");
413   SUIT_MessageBox::information(this, tr("GEOM_FREE_BOUNDS_TLT"), msg);
414 }