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