Salome HOME
*** empty log message ***
[modules/geom.git] / src / RepairGUI / RepairGUI_SuppressFacesDlg.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 // File   : RepairGUI_SuppressFacesDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "RepairGUI_SuppressFacesDlg.h"
27
28 #include <GEOM_DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SalomeApp_Application.h>
33 #include <LightApp_SelectionMgr.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_Session.h>
36 #include <SALOME_ListIteratorOfListIO.hxx>
37
38 #include <GEOMImpl_Types.hxx>
39
40 #include <TopAbs.hxx>
41 #include <TColStd_IndexedMapOfInteger.hxx>
42
43 #include <utilities.h>
44
45 //=================================================================================
46 // class    : RepairGUI_SuppressFacesDlg()
47 // purpose  : Constructs a RepairGUI_SuppressFacesDlg  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_SuppressFacesDlg::RepairGUI_SuppressFacesDlg( 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_SUPRESS_FACE" ) ) );
57   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
58     
59   setWindowTitle( tr("GEOM_SUPRESSFACE_TITLE" ) );
60
61   /***************************************************************/
62   mainFrame()->GroupConstructors->setTitle(tr("GEOM_SUPRESSFACE"));
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_1Sel( centralWidget() );
70   GroupPoints->GroupBox1->setTitle( tr( "Faces to remove" ) );
71   GroupPoints->TextLabel1->setText( tr( "Faces" ) );
72   GroupPoints->PushButton1->setIcon( image1 );
73   GroupPoints->LineEdit1->setReadOnly( true );
74
75   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
76   layout->setMargin( 0 ); layout->setSpacing( 6 );
77   layout->addWidget( GroupPoints );
78   /***************************************************************/
79   
80   setHelpFileName( "suppress_faces.htm" );
81
82   Init();
83 }
84
85
86 //=================================================================================
87 // function : ~RepairGUI_SuppressFacesDlg()
88 // purpose  : Destroys the object and frees any allocated resources
89 //=================================================================================
90 RepairGUI_SuppressFacesDlg::~RepairGUI_SuppressFacesDlg()
91 {
92 }
93
94
95 //=================================================================================
96 // function : Init()
97 // purpose  :
98 //=================================================================================
99 void RepairGUI_SuppressFacesDlg::Init()
100 {
101   /* init variables */
102   myEditCurrentArgument = GroupPoints->LineEdit1;
103
104   myObjects = new GEOM::ListOfGO();
105   myObjects->length( 0 );
106
107   //myGeomGUI->SetState( 0 );
108   initSelection();
109           
110   /* signals and slots connections */
111   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
112   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
113
114   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
115   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
116
117   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
118            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
119
120   initName( tr( "SUPRESS_FACE_NEW_OBJ_NAME" ) );
121 }
122
123
124 //=================================================================================
125 // function : ClickOnOk()
126 // purpose  : Same than click on apply but close this dialog.
127 //=================================================================================
128 void RepairGUI_SuppressFacesDlg::ClickOnOk()
129 {
130   if ( ClickOnApply() )
131     ClickOnCancel();
132 }
133
134 //=================================================================================
135 // function : ClickOnApply()
136 // purpose  :
137 //=================================================================================
138 bool RepairGUI_SuppressFacesDlg::ClickOnApply()
139 {
140   if ( !onAccept() )
141     return false;
142
143   initName();
144
145   myEditCurrentArgument->setText( "" );
146   myObjects->length( 0 );
147   myFaces.clear();
148
149   initSelection();
150   
151   return true;
152 }
153
154
155 //=================================================================================
156 // function : SelectionIntoArgument()
157 // purpose  : Called when selection as changed or other case
158 //          : used only by SelectButtonC1A1 (LineEditC1A1)
159 //=================================================================================
160 void RepairGUI_SuppressFacesDlg::SelectionIntoArgument()
161 {
162   myEditCurrentArgument->setText( "" );
163
164   Standard_Boolean aRes = Standard_False;
165   int i = 0;
166   int numFaces = 0;
167   myObjects->length( IObjectCount() );
168   myFaces.clear();
169   for ( SALOME_ListIteratorOfListIO anIt( selectedIO() ); anIt.More(); anIt.Next() ) {
170     Handle(SALOME_InteractiveObject) anIO = anIt.Value();
171     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIO, aRes );
172     if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
173       TopoDS_Shape aShape;
174       if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) ) {
175         if ( aShape.ShapeType() <= TopAbs_FACE ) { // FACE, SHELL, SOLID, COMPOUND
176           GEOM::short_array anIndexes;
177
178           TColStd_IndexedMapOfInteger aMap;
179           myGeomGUI->getApp()->selectionMgr()->GetIndexes( anIO, aMap );
180           
181           if ( !aMap.IsEmpty() ) {
182             Convert( aMap, anIndexes );
183             myObjects[i++] = aSelectedObject; // append the object
184             myFaces.append( anIndexes );   // append faces' indexes
185             numFaces += anIndexes.length();// just for text field output
186           }
187         }
188       }
189     }
190   }
191   myObjects->length( i ); // this is the right length, smaller of equal to the previously set
192   if ( numFaces )
193     myEditCurrentArgument->setText( QString::number( numFaces ) + "_" + tr( "GEOM_FACE" ) + tr( "_S_" ) );
194 }
195
196 //=================================================================================
197 // function : Convert()
198 // purpose  :
199 //=================================================================================
200 void RepairGUI_SuppressFacesDlg::Convert( const TColStd_IndexedMapOfInteger& theMap, GEOM::short_array& theOutSeq )
201 {
202   const int n = theMap.Extent();
203   theOutSeq.length( n );
204   for ( int i = 0; i < n; i++ )
205     theOutSeq[i] = theMap( i+1 );
206 }
207
208 //=================================================================================
209 // function : SetEditCurrentArgument()
210 // purpose  :
211 //=================================================================================
212 void RepairGUI_SuppressFacesDlg::SetEditCurrentArgument()
213 {
214   if ( sender() == GroupPoints->PushButton1 ) {
215     GroupPoints->LineEdit1->setFocus();
216     myEditCurrentArgument = GroupPoints->LineEdit1;
217   }
218   SelectionIntoArgument();
219 }
220
221
222 //=================================================================================
223 // function : LineEditReturnPressed()
224 // purpose  :
225 //=================================================================================
226 void RepairGUI_SuppressFacesDlg::LineEditReturnPressed()
227 {
228   if ( sender() == GroupPoints->LineEdit1 ) {
229     myEditCurrentArgument = GroupPoints->LineEdit1;
230     GEOMBase_Skeleton::LineEditReturnPressed();
231   }
232 }
233
234
235 //=================================================================================
236 // function : ActivateThisDialog()
237 // purpose  :
238 //=================================================================================
239 void RepairGUI_SuppressFacesDlg::ActivateThisDialog()
240 {
241   GEOMBase_Skeleton::ActivateThisDialog();
242   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
243            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
244
245   //myGeomGUI->SetState( 0 );
246   initSelection();
247 }
248
249
250 //=================================================================================
251 // function : enterEvent()
252 // purpose  : Mouse enter onto the dialog to activate it
253 //=================================================================================
254 void RepairGUI_SuppressFacesDlg::enterEvent( QEvent* )
255 {
256   if ( !mainFrame()->GroupConstructors->isEnabled() )
257     ActivateThisDialog();
258 }
259
260
261 //=================================================================================
262 // function : closeEvent()
263 // purpose  :
264 //=================================================================================
265 void RepairGUI_SuppressFacesDlg::closeEvent( QCloseEvent* e )
266 {
267   //myGeomGUI->SetState( -1 );
268   GEOMBase_Skeleton::closeEvent( e );
269 }
270
271 //=================================================================================
272 // function : createOperation
273 // purpose  :
274 //=================================================================================
275 GEOM::GEOM_IOperations_ptr RepairGUI_SuppressFacesDlg::createOperation()
276 {
277   return getGeomEngine()->GetIHealingOperations( getStudyId() );
278 }
279
280 //=================================================================================
281 // function : isValid
282 // purpose  :
283 //=================================================================================
284 bool RepairGUI_SuppressFacesDlg::isValid( QString& )
285 {
286   const int objL = myObjects->length(), facesL = myFaces.size();
287   return ( objL && objL == facesL );
288 }
289
290 //=================================================================================
291 // function : execute
292 // purpose  :
293 //=================================================================================
294 bool RepairGUI_SuppressFacesDlg::execute( ObjectList& objects )
295 {
296   QStringList anErrorObjNames;
297   for ( int i = 0; i < myObjects->length(); i++ ) {
298     GEOM::GEOM_Object_var obj = myObjects[i];
299     GEOM::short_array faces = myFaces[i];
300     //MESSAGE(">>>> Dlg, passing faces.. len = " << faces.length());
301     GEOM::GEOM_Object_var anObj = GEOM::GEOM_IHealingOperations::_narrow( getOperation() )->SuppressFaces( obj, faces );
302     if ( anObj->_is_nil() )
303       anErrorObjNames << GEOMBase::GetName( obj );
304     else
305       objects.push_back( anObj._retn() );
306   }
307
308   if ( !anErrorObjNames.empty() )
309     MESSAGE( "ERRORS occured while processing the following objects: " << anErrorObjNames.join( " " ).toLatin1().data() );
310
311   return anErrorObjNames.size() < myObjects->length(); // true if at least one object was OK, false if ALL objects were nil after Healing.
312 }
313
314 //=================================================================================
315 // function : initSelection
316 // purpose  :
317 //=================================================================================
318 void RepairGUI_SuppressFacesDlg::initSelection()
319 {
320   GEOM::GEOM_Object_var aNullGeomObject;
321   localSelection( aNullGeomObject, TopAbs_FACE ); // load local selection on ALL objects
322 }