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