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