Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/geom.git] / src / RepairGUI / RepairGUI_RemoveHolesDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : RepairGUI_RemoveHolesDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "RepairGUI_RemoveHolesDlg.h"
30
31 #include "SalomeApp_Application.h"
32 #include "LightApp_SelectionMgr.h"
33 #include "SUIT_MessageBox.h"
34 #include "SUIT_Session.h"
35
36 #include "SALOME_ListIteratorOfListIO.hxx"
37
38 #include "GEOMImpl_Types.hxx"
39
40 #include <TopAbs.hxx>
41 #include <TColStd_MapOfInteger.hxx>
42
43 using namespace std;
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                                                    const char* name, bool modal, WFlags fl)
54   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
55                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
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   setCaption(tr("GEOM_REMOVE_HOLES_TITLE"));
61
62   /***************************************************************/
63   GroupConstructors->setTitle(tr("GEOM_REMOVE_HOLES_TITLE"));
64   RadioButton1->setPixmap(image0);
65   RadioButton2->close(TRUE);
66   RadioButton3->close(TRUE);
67
68   GroupPoints = new DlgRef_1Sel_Ext(this, "GroupPoints");
69   GroupPoints->GroupBox1->setTitle(tr("GEOM_HOLES"));
70   GroupPoints->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
71   GroupPoints->PushButton1->setPixmap(image1);
72   GroupPoints->LineEdit1->setReadOnly( true );
73
74   Layout1->addWidget(GroupPoints, 2, 0);
75
76   myAllChk = new QCheckBox( tr( "GEOM_REMOVE_ALL_HOLES" ), GroupPoints->GroupBox1 );
77
78   QGridLayout* aSelectWiresLay = new QGridLayout( 0, 1, 1, 0, 6, "aSelectWiresLay");
79
80   mySelectWiresBtn = new QPushButton( GroupPoints->GroupBox1, "mySelectWiresBtn" );
81   mySelectWiresBtn->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, mySelectWiresBtn->sizePolicy().hasHeightForWidth() ) );
82   mySelectWiresBtn->setText( trUtf8( "" ) );
83   mySelectWiresBtn->setPixmap(image1);
84
85   mySelectWiresEdt = new QLineEdit( GroupPoints->GroupBox1, "mySelectWiresEdt" );
86   mySelectWiresEdt->setReadOnly( true );
87
88   mySelectWiresLbl = new QLabel( tr( "GEOM_WIRES_TO_REMOVE" ), GroupPoints->GroupBox1, "TextLabel1" );
89   mySelectWiresLbl->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, mySelectWiresLbl->sizePolicy().hasHeightForWidth() ) );
90
91   aSelectWiresLay->addWidget( mySelectWiresLbl, 0, 0 );
92         aSelectWiresLay->addWidget( mySelectWiresBtn, 0, 1 );
93   aSelectWiresLay->addWidget( mySelectWiresEdt, 0, 2 );
94   QSpacerItem* spacer = new QSpacerItem( 0, 16, QSizePolicy::Minimum, QSizePolicy::Expanding );
95   aSelectWiresLay->addItem( spacer, 1, 2 );
96
97   QGridLayout* aDetectLay = new QGridLayout( 0, 1, 1, 0, 6, "aDetectLay" );  
98         myFreeBoundBtn = new QPushButton( tr( "GEOM_DETECT" ), GroupPoints->GroupBox1 );
99   QLabel* aLbl = new QLabel( tr( "GEOM_FREE_BOUNDARIES" ), GroupPoints->GroupBox1 );
100   aDetectLay->addWidget( aLbl, 0, 0 );
101   aDetectLay->addWidget( myFreeBoundBtn, 0, 1 );
102   
103   GroupPoints->getGroupBoxLayout()->addMultiCellWidget( myAllChk, 1, 1, 0, 2 );
104   GroupPoints->getGroupBoxLayout()->addLayout( aSelectWiresLay, 2, 0 );
105   GroupPoints->getGroupBoxLayout()->addLayout( aDetectLay, 3, 0 );
106   /***************************************************************/
107
108   setHelpFileName("suppress_holes.htm");
109
110   Init();
111 }
112
113
114 //=================================================================================
115 // function : ~RepairGUI_RemoveHolesDlg()
116 // purpose  : Destroys the object and frees any allocated resources
117 //=================================================================================
118 RepairGUI_RemoveHolesDlg::~RepairGUI_RemoveHolesDlg()
119 {
120 }
121
122
123 //=================================================================================
124 // function : Init()
125 // purpose  :
126 //=================================================================================
127 void RepairGUI_RemoveHolesDlg::Init()
128 {
129   /* init variables */
130   myEditCurrentArgument = GroupPoints->LineEdit1;
131   
132   myObject = GEOM::GEOM_Object::_nil();
133   myWiresInd = new GEOM::short_array();
134   myWiresInd->length( 0 );
135
136   //myGeomGUI->SetState( 0 );
137   initSelection();
138
139   myClosed = -1;
140   myOpen = -1;
141   
142   /* signals and slots connections */
143   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
144   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
145
146   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
147   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
148   connect(mySelectWiresBtn, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
149   connect(mySelectWiresEdt, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
150
151   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
152           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
153
154   connect( myAllChk, SIGNAL( clicked() ), this, SLOT( onRemoveAllClicked() ) );
155   connect( myFreeBoundBtn, SIGNAL(clicked()), this, SLOT(onDetect()) );
156
157   initName( tr( "REMOVE_HOLES_NEW_OBJ_NAME" ) );
158 }
159
160
161 //=================================================================================
162 // function : ClickOnOk()
163 // purpose  : Same than click on apply but close this dialog.
164 //=================================================================================
165 void RepairGUI_RemoveHolesDlg::ClickOnOk()
166 {
167   if ( ClickOnApply() )
168     ClickOnCancel();
169 }
170
171 //=================================================================================
172 // function : ClickOnApply()
173 // purpose  :
174 //=================================================================================
175 bool RepairGUI_RemoveHolesDlg::ClickOnApply()
176 {
177   if ( !onAccept() )
178     return false;
179
180   initName();
181
182   myEditCurrentArgument = GroupPoints->LineEdit1;
183   myEditCurrentArgument->setText("");
184   mySelectWiresEdt->setText("");
185   myObject = GEOM::GEOM_Object::_nil();
186   myWiresInd->length( 0 );
187
188   initSelection();
189
190   return true;
191 }
192
193
194 //=================================================================================
195 // function : SelectionIntoArgument()
196 // purpose  : Called when selection
197 //=================================================================================
198 void RepairGUI_RemoveHolesDlg::SelectionIntoArgument()
199 {
200   erasePreview();
201   myEditCurrentArgument->setText("");
202   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myObject = GEOM::GEOM_Object::_nil();
203   else if ( myEditCurrentArgument == mySelectWiresEdt ) myWiresInd->length( 0 );
204
205   if ( IObjectCount() == 1 )
206   {
207     Handle(SALOME_InteractiveObject) anIO = firstIObject();
208
209     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )      // face selection
210     {
211       Standard_Boolean aRes;
212       myObject = GEOMBase::ConvertIOinGEOMObject( anIO, aRes );
213       if ( aRes && GEOMBase::IsShape( myObject ) )
214         myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
215       else
216         myObject = GEOM::GEOM_Object::_nil();
217     }
218     else if ( myEditCurrentArgument == mySelectWiresEdt && !myAllChk->isChecked() )
219     {
220       TColStd_IndexedMapOfInteger aMap;
221       ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr()->GetIndexes( anIO, aMap );
222       const int n = aMap.Extent();
223       myWiresInd->length( n );
224       for ( int i = 1; i <= n; i++ )
225         myWiresInd[i-1] = aMap( i );
226       if ( n )
227         myEditCurrentArgument->setText( QString::number( n ) + "_" + tr( "GEOM_WIRE" ) + tr( "_S_" ) );
228     }
229   }
230 }
231
232 //=================================================================================
233 // function : SetEditCurrentArgument()
234 // purpose  :
235 //=================================================================================
236 void RepairGUI_RemoveHolesDlg::SetEditCurrentArgument()
237 {
238   const QObject* send = sender();
239   if ( send == GroupPoints->PushButton1 )
240     myEditCurrentArgument = GroupPoints->LineEdit1; 
241   else if ( send == mySelectWiresBtn && !myObject->_is_nil() )
242     myEditCurrentArgument = mySelectWiresEdt;
243   if ( myEditCurrentArgument )
244   {
245     initSelection();
246     myEditCurrentArgument->setFocus();
247     SelectionIntoArgument();
248   }
249 }
250
251
252 //=================================================================================
253 // function : LineEditReturnPressed()
254 // purpose  :
255 //=================================================================================
256 void RepairGUI_RemoveHolesDlg::LineEditReturnPressed()
257 {
258   const QObject* send = sender();
259   if( send == GroupPoints->LineEdit1 || send == mySelectWiresEdt )
260   {
261     myEditCurrentArgument = (QLineEdit*)send;
262     GEOMBase_Skeleton::LineEditReturnPressed();
263   }
264 }
265
266
267 //=================================================================================
268 // function : ActivateThisDialog()
269 // purpose  :
270 //=================================================================================
271 void RepairGUI_RemoveHolesDlg::ActivateThisDialog()
272 {
273   GEOMBase_Skeleton::ActivateThisDialog();
274   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
275           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
276
277   myEditCurrentArgument = GroupPoints->LineEdit1;
278   myEditCurrentArgument->setText("");
279   mySelectWiresEdt->setText("");
280   myObject = GEOM::GEOM_Object::_nil();
281   myWiresInd->length( 0 );
282
283   myClosed = -1;
284   myOpen = -1;
285
286   //myGeomGUI->SetState( 0 );
287   initSelection();
288 }
289
290
291 //=================================================================================
292 // function : enterEvent()
293 // purpose  : Mouse enter onto the dialog to activate it
294 //=================================================================================
295 void RepairGUI_RemoveHolesDlg::enterEvent(QEvent* e)
296 {
297   if ( !GroupConstructors->isEnabled() )
298     ActivateThisDialog();
299 }
300
301
302 //=================================================================================
303 // function : closeEvent()
304 // purpose  :
305 //=================================================================================
306 void RepairGUI_RemoveHolesDlg::closeEvent(QCloseEvent* e)
307 {
308   //myGeomGUI->SetState( -1 );
309   GEOMBase_Skeleton::closeEvent( e );
310 }
311
312 //=================================================================================
313 // function : createOperation
314 // purpose  :
315 //=================================================================================
316 GEOM::GEOM_IOperations_ptr RepairGUI_RemoveHolesDlg::createOperation()
317 {
318   return getGeomEngine()->GetIHealingOperations( getStudyId() );
319 }
320
321 //=================================================================================
322 // function : isValid
323 // purpose  :
324 //=================================================================================
325 bool RepairGUI_RemoveHolesDlg::isValid( QString& msg )
326 {
327   myClosed = -1;
328   return !myObject->_is_nil() && ( IsPreview() || myAllChk->isChecked() || myWiresInd->length() );
329 }
330
331 //=================================================================================
332 // function : execute
333 // purpose  :
334 //=================================================================================
335 bool RepairGUI_RemoveHolesDlg::execute( ObjectList& objects )
336 {
337   bool aResult = false;
338   if ( IsPreview() ) // called from onDetect(): detect free boundary edges, highlight them (add to objects), display message dialog
339   {
340     GEOM::ListOfGO_var aClosed, anOpen;
341
342     aResult = GEOM::GEOM_IHealingOperations::_narrow( getOperation() )->GetFreeBoundary( myObject, aClosed, anOpen );
343
344     if ( aResult )
345     {
346       myClosed = aClosed->length();
347       myOpen = anOpen->length();
348       int i;
349       for ( i = 0; i < myClosed; i++ )
350         objects.push_back( aClosed[i]._retn() );
351       for ( i = 0; i < myOpen; i++ )
352         objects.push_back( anOpen[i]._retn() );
353     }
354     else
355       myClosed = -1;
356   }
357   else
358   {
359     GEOM::GEOM_Object_var anObj = GEOM::GEOM_IHealingOperations::_narrow( getOperation() )->FillHoles( myObject, myWiresInd );
360     aResult = !anObj->_is_nil();
361     if ( aResult )
362       objects.push_back( anObj._retn() );
363   }
364         
365   return aResult;
366 }
367
368 //=================================================================================
369 // function : onRemoveAllClicked
370 // purpose  :
371 //=================================================================================
372 void RepairGUI_RemoveHolesDlg::onRemoveAllClicked()
373 {
374   bool b = myAllChk->isOn();
375   mySelectWiresLbl->setEnabled( !b );
376   mySelectWiresBtn->setEnabled( !b );
377   mySelectWiresEdt->setEnabled( !b );
378   if ( b )
379   {
380     mySelectWiresEdt->setText( "" );
381     myWiresInd->length( 0 );
382   }
383 }
384
385 //=================================================================================
386 // function : initSelection
387 // purpose  :
388 //=================================================================================
389 void RepairGUI_RemoveHolesDlg::initSelection()
390 {
391   if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
392   {
393     TColStd_MapOfInteger aTypes;
394     aTypes.Add( GEOM_COMPOUND );
395     aTypes.Add( GEOM_SOLID );
396     aTypes.Add( GEOM_SHELL );
397     aTypes.Add( GEOM_FACE );
398     
399     globalSelection( aTypes );
400   }
401   else if ( myEditCurrentArgument == mySelectWiresEdt )
402   {
403     localSelection( myObject, TopAbs_EDGE );
404     localSelection( myObject, TopAbs_WIRE );
405   }
406 }
407
408
409 //=================================================================================
410 // function : onDetect
411 // purpose  :
412 //=================================================================================
413 void RepairGUI_RemoveHolesDlg::onDetect()
414 {
415   displayPreview( false, true, true, 3 );
416
417   // field myClosed,myOpen is initialized in execute() method, called by displayPreview().
418   QString msg;
419   if ( myClosed != -1 )
420     msg = tr( "GEOM_FREE_BOUNDS_MSG" ).arg( myClosed + myOpen ).arg( myClosed ).arg( myOpen );
421   else
422     msg = tr( "GEOM_FREE_BOUNDS_ERROR" );
423   SUIT_MessageBox::info1( this, tr( "GEOM_FREE_BOUNDS_TLT" ), msg, "Close" );
424 }
425