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