Salome HOME
Preparation of intermediate revision
[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, Open CASCADE S.A.S.
25 //
26 #include "RepairGUI_SuppressFacesDlg.h"
27
28 #include <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_operation_page.html" );
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   resize(100,100);
122 }
123
124
125 //=================================================================================
126 // function : ClickOnOk()
127 // purpose  : Same than click on apply but close this dialog.
128 //=================================================================================
129 void RepairGUI_SuppressFacesDlg::ClickOnOk()
130 {
131   if ( ClickOnApply() )
132     ClickOnCancel();
133 }
134
135 //=================================================================================
136 // function : ClickOnApply()
137 // purpose  :
138 //=================================================================================
139 bool RepairGUI_SuppressFacesDlg::ClickOnApply()
140 {
141   if ( !onAccept() )
142     return false;
143
144   initName();
145
146   myEditCurrentArgument->setText( "" );
147   myObjects->length( 0 );
148   myFaces.clear();
149
150   initSelection();
151   
152   return true;
153 }
154
155
156 //=================================================================================
157 // function : SelectionIntoArgument()
158 // purpose  : Called when selection as changed or other case
159 //          : used only by SelectButtonC1A1 (LineEditC1A1)
160 //=================================================================================
161 void RepairGUI_SuppressFacesDlg::SelectionIntoArgument()
162 {
163   myEditCurrentArgument->setText( "" );
164
165   Standard_Boolean aRes = Standard_False;
166   int i = 0;
167   int numFaces = 0;
168
169   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
170   SALOME_ListIO aSelList;
171   aSelMgr->selectedObjects(aSelList);
172
173   myObjects->length( aSelList.Extent() );
174   myFaces.clear();
175
176   for (SALOME_ListIteratorOfListIO anIt (aSelList); anIt.More(); anIt.Next()) {
177     Handle(SALOME_InteractiveObject) anIO = anIt.Value();
178     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIO, aRes );
179     if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
180       TopoDS_Shape aShape;
181       if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) ) {
182         if ( aShape.ShapeType() <= TopAbs_FACE ) { // FACE, SHELL, SOLID, COMPOUND
183           GEOM::short_array anIndexes;
184
185           TColStd_IndexedMapOfInteger aMap;
186           aSelMgr->GetIndexes( anIO, aMap );
187           
188           if ( !aMap.IsEmpty() ) {
189             Convert( aMap, anIndexes );
190             myObjects[i++] = aSelectedObject; // append the object
191             myFaces.append( anIndexes );   // append faces' indexes
192             numFaces += anIndexes.length();// just for text field output
193           }
194         }
195       }
196     }
197   }
198   myObjects->length( i ); // this is the right length, smaller of equal to the previously set
199   if ( numFaces )
200     myEditCurrentArgument->setText( QString::number( numFaces ) + "_" + tr( "GEOM_FACE" ) + tr( "_S_" ) );
201 }
202
203 //=================================================================================
204 // function : Convert()
205 // purpose  :
206 //=================================================================================
207 void RepairGUI_SuppressFacesDlg::Convert( const TColStd_IndexedMapOfInteger& theMap, GEOM::short_array& theOutSeq )
208 {
209   const int n = theMap.Extent();
210   theOutSeq.length( n );
211   for ( int i = 0; i < n; i++ )
212     theOutSeq[i] = theMap( i+1 );
213 }
214
215 //=================================================================================
216 // function : SetEditCurrentArgument()
217 // purpose  :
218 //=================================================================================
219 void RepairGUI_SuppressFacesDlg::SetEditCurrentArgument()
220 {
221   if ( sender() == GroupPoints->PushButton1 ) {
222     GroupPoints->LineEdit1->setFocus();
223     myEditCurrentArgument = GroupPoints->LineEdit1;
224   }
225   SelectionIntoArgument();
226 }
227
228
229 //=================================================================================
230 // function : LineEditReturnPressed()
231 // purpose  :
232 //=================================================================================
233 void RepairGUI_SuppressFacesDlg::LineEditReturnPressed()
234 {
235   if ( sender() == GroupPoints->LineEdit1 ) {
236     myEditCurrentArgument = GroupPoints->LineEdit1;
237     GEOMBase_Skeleton::LineEditReturnPressed();
238   }
239 }
240
241
242 //=================================================================================
243 // function : ActivateThisDialog()
244 // purpose  :
245 //=================================================================================
246 void RepairGUI_SuppressFacesDlg::ActivateThisDialog()
247 {
248   GEOMBase_Skeleton::ActivateThisDialog();
249   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
250            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
251
252   //myGeomGUI->SetState( 0 );
253   initSelection();
254 }
255
256
257 //=================================================================================
258 // function : enterEvent()
259 // purpose  : Mouse enter onto the dialog to activate it
260 //=================================================================================
261 void RepairGUI_SuppressFacesDlg::enterEvent( QEvent* )
262 {
263   if ( !mainFrame()->GroupConstructors->isEnabled() )
264     ActivateThisDialog();
265 }
266
267
268 //=================================================================================
269 // function : createOperation
270 // purpose  :
271 //=================================================================================
272 GEOM::GEOM_IOperations_ptr RepairGUI_SuppressFacesDlg::createOperation()
273 {
274   return getGeomEngine()->GetIHealingOperations( getStudyId() );
275 }
276
277 //=================================================================================
278 // function : isValid
279 // purpose  :
280 //=================================================================================
281 bool RepairGUI_SuppressFacesDlg::isValid( QString& )
282 {
283   const int objL = myObjects->length(), facesL = myFaces.size();
284   return ( objL && objL == facesL );
285 }
286
287 //=================================================================================
288 // function : execute
289 // purpose  :
290 //=================================================================================
291 bool RepairGUI_SuppressFacesDlg::execute( ObjectList& objects )
292 {
293   QStringList anErrorObjNames;
294   for ( int i = 0; i < myObjects->length(); i++ ) {
295     GEOM::GEOM_Object_var obj = myObjects[i];
296     GEOM::short_array faces = myFaces[i];
297     //MESSAGE(">>>> Dlg, passing faces.. len = " << faces.length());
298     GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow( getOperation() );
299     GEOM::GEOM_Object_var anObj = anOper->SuppressFaces( obj, faces );
300     if ( anObj->_is_nil() )
301       anErrorObjNames << GEOMBase::GetName( obj );
302     else
303       objects.push_back( anObj._retn() );
304   }
305
306   if ( !anErrorObjNames.empty() )
307     MESSAGE( "ERRORS occured while processing the following objects: " << anErrorObjNames.join( " " ).toLatin1().data() );
308
309   return anErrorObjNames.size() < myObjects->length(); // true if at least one object was OK, false if ALL objects were nil after Healing.
310 }
311
312 //=================================================================================
313 // function : initSelection
314 // purpose  :
315 //=================================================================================
316 void RepairGUI_SuppressFacesDlg::initSelection()
317 {
318   GEOM::GEOM_Object_var aNullGeomObject;
319   localSelection( aNullGeomObject, TopAbs_FACE ); // load local selection on ALL objects
320 }