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