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