Salome HOME
Fix issue 0020479: EDF 1116 GEOM: Create a group, "Add" button is inactive but "Selec...
[modules/geom.git] / src / RepairGUI / RepairGUI_SuppressFacesDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  GEOM GEOMGUI : GUI for Geometry component
23 //  File   : RepairGUI_SuppressFacesDlg.cxx
24 //  Author : Lucien PIGNOLONI
25 //  Module : GEOM
26 //  $Header$
27 //
28 #include "RepairGUI_SuppressFacesDlg.h"
29
30 #include "SalomeApp_Application.h"
31 #include "LightApp_SelectionMgr.h"
32 #include "SUIT_Session.h"
33 #include "SALOME_ListIteratorOfListIO.hxx"
34
35 #include "GEOMImpl_Types.hxx"
36
37 #include <TopAbs.hxx>
38
39 #include <qlabel.h>
40
41 using namespace std;
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(GeometryGUI* theGeometryGUI, QWidget* parent,
51                                                        const char* name, bool modal, WFlags fl)
52   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
53                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
54 {
55   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_SUPRESS_FACE")));
56   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
57     
58   setCaption(tr("GEOM_SUPRESSFACE_TITLE"));
59
60   /***************************************************************/
61   GroupConstructors->setTitle(tr("GEOM_SUPRESSFACE"));
62   RadioButton1->setPixmap(image0);
63   RadioButton2->close(TRUE);
64   RadioButton3->close(TRUE);
65
66   GroupPoints = new DlgRef_1Sel_QTD(this, "GroupPoints");
67   GroupPoints->GroupBox1->setTitle(tr("Faces to remove"));
68   GroupPoints->TextLabel1->setText(tr("Faces"));
69   GroupPoints->PushButton1->setPixmap(image1);
70   GroupPoints->LineEdit1->setReadOnly( true );
71
72   Layout1->addWidget(GroupPoints, 2, 0);
73   /***************************************************************/
74   
75   setHelpFileName("suppress_faces_operation_page.html");
76
77   Init();
78 }
79
80
81 //=================================================================================
82 // function : ~RepairGUI_SuppressFacesDlg()
83 // purpose  : Destroys the object and frees any allocated resources
84 //=================================================================================
85 RepairGUI_SuppressFacesDlg::~RepairGUI_SuppressFacesDlg()
86 {
87 }
88
89
90 //=================================================================================
91 // function : Init()
92 // purpose  :
93 //=================================================================================
94 void RepairGUI_SuppressFacesDlg::Init()
95 {
96   /* init variables */
97   myEditCurrentArgument = GroupPoints->LineEdit1;
98
99   myObjects = new GEOM::ListOfGO();
100   myObjects->length( 0 );
101
102   //myGeomGUI->SetState( 0 );
103   initSelection();
104           
105   /* signals and slots connections */
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 // function : ClickOnApply()
131 // purpose  :
132 //=================================================================================
133 bool RepairGUI_SuppressFacesDlg::ClickOnApply()
134 {
135   if ( !onAccept() )
136     return false;
137
138   initName();
139
140   myEditCurrentArgument->setText("");
141   myObjects->length( 0 );
142   myFaces.clear();
143
144   initSelection();
145   
146   return true;
147 }
148
149
150 //=================================================================================
151 // function : SelectionIntoArgument()
152 // purpose  : Called when selection as changed or other case
153 //          : used only by SelectButtonC1A1 (LineEditC1A1)
154 //=================================================================================
155 void RepairGUI_SuppressFacesDlg::SelectionIntoArgument()
156 {
157   myEditCurrentArgument->setText("");
158
159   Standard_Boolean aRes = Standard_False;
160   int i = 0;
161   int numFaces = 0;
162   myObjects->length( IObjectCount() );
163   myFaces.clear();
164   for ( SALOME_ListIteratorOfListIO anIt( selectedIO() ); anIt.More(); anIt.Next() )
165   {
166     Handle(SALOME_InteractiveObject) anIO = anIt.Value();
167     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIO, aRes );
168     if ( !CORBA::is_nil( aSelectedObject ) && aRes )
169     {
170       TopoDS_Shape aShape;
171       if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) )
172       {
173         if ( aShape.ShapeType() <= TopAbs_FACE ) // FACE, SHELL, SOLID, COMPOUND
174         {
175           GEOM::short_array anIndexes;
176
177           TColStd_IndexedMapOfInteger aMap;
178           myGeomGUI->getApp()->selectionMgr()->GetIndexes( anIO, aMap );
179           
180           if ( !aMap.IsEmpty() )
181           {
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   {
216     GroupPoints->LineEdit1->setFocus();
217     myEditCurrentArgument = GroupPoints->LineEdit1;
218   }
219   SelectionIntoArgument();
220 }
221
222
223 //=================================================================================
224 // function : LineEditReturnPressed()
225 // purpose  :
226 //=================================================================================
227 void RepairGUI_SuppressFacesDlg::LineEditReturnPressed()
228 {
229   if( sender() == GroupPoints->LineEdit1 )
230   {
231     myEditCurrentArgument = GroupPoints->LineEdit1;
232     GEOMBase_Skeleton::LineEditReturnPressed();
233   }
234 }
235
236
237 //=================================================================================
238 // function : ActivateThisDialog()
239 // purpose  :
240 //=================================================================================
241 void RepairGUI_SuppressFacesDlg::ActivateThisDialog()
242 {
243   GEOMBase_Skeleton::ActivateThisDialog();
244   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
245           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
246
247   //myGeomGUI->SetState( 0 );
248   initSelection();
249 }
250
251
252 //=================================================================================
253 // function : enterEvent()
254 // purpose  : Mouse enter onto the dialog to activate it
255 //=================================================================================
256 void RepairGUI_SuppressFacesDlg::enterEvent(QEvent* e)
257 {
258   if ( !GroupConstructors->isEnabled() )
259     ActivateThisDialog();
260 }
261
262
263 //=================================================================================
264 // function : closeEvent()
265 // purpose  :
266 //=================================================================================
267 void RepairGUI_SuppressFacesDlg::closeEvent(QCloseEvent* e)
268 {
269   //myGeomGUI->SetState( -1 );
270   GEOMBase_Skeleton::closeEvent( e );
271 }
272
273 //=================================================================================
274 // function : createOperation
275 // purpose  :
276 //=================================================================================
277 GEOM::GEOM_IOperations_ptr RepairGUI_SuppressFacesDlg::createOperation()
278 {
279   return getGeomEngine()->GetIHealingOperations( getStudyId() );
280 }
281
282 //=================================================================================
283 // function : isValid
284 // purpose  :
285 //=================================================================================
286 bool RepairGUI_SuppressFacesDlg::isValid( QString& msg )
287 {
288   const int objL = myObjects->length(), facesL = myFaces.size();
289   return ( objL && objL == facesL );
290 }
291
292 //=================================================================================
293 // function : execute
294 // purpose  :
295 //=================================================================================
296 bool RepairGUI_SuppressFacesDlg::execute( ObjectList& objects )
297 {
298   QStringList anErrorObjNames;
299   for ( int i = 0; i < myObjects->length(); i++ )
300   {
301     GEOM::GEOM_Object_var obj = myObjects[i];
302     GEOM::short_array faces = myFaces[i];
303     //MESSAGE(">>>> Dlg, passing faces.. len = " << faces.length());
304     GEOM::GEOM_Object_var anObj = GEOM::GEOM_IHealingOperations::_narrow( getOperation() )->SuppressFaces( obj, faces );
305     if ( anObj->_is_nil() )
306       anErrorObjNames << GEOMBase::GetName( obj );
307     else
308       objects.push_back( anObj._retn() );
309   }
310
311   if ( !anErrorObjNames.empty() )
312     MESSAGE("ERRORS occured while processing the following objects: " << anErrorObjNames.join( " " ));
313
314   return anErrorObjNames.size() < myObjects->length(); // true if at least one object was OK, false if ALL objects were nil after Healing.
315 }
316
317 //=================================================================================
318 // function : initSelection
319 // purpose  :
320 //=================================================================================
321 void RepairGUI_SuppressFacesDlg::initSelection()
322 {
323   GEOM::GEOM_Object_var aNullGeomObject;
324   localSelection( aNullGeomObject, TopAbs_FACE ); // load local selection on ALL objects
325 }