Salome HOME
Using files from package LightApp instead of SalomeApp
[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 #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(QWidget* parent, const char* name, bool modal, WFlags fl)
52   :GEOMBase_Skeleton(parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
53 {
54   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_SUPRESS_FACE")));
55   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
56     
57   setCaption(tr("GEOM_SUPRESSFACE_TITLE"));
58
59   /***************************************************************/
60   GroupConstructors->setTitle(tr("GEOM_SUPRESSFACE"));
61   RadioButton1->setPixmap(image0);
62   RadioButton2->close(TRUE);
63   RadioButton3->close(TRUE);
64
65   GroupPoints = new DlgRef_1Sel_QTD(this, "GroupPoints");
66   GroupPoints->GroupBox1->setTitle(tr("Faces to remove"));
67   GroupPoints->TextLabel1->setText(tr("Faces"));
68   GroupPoints->PushButton1->setPixmap(image1);
69   GroupPoints->LineEdit1->setReadOnly( true );
70
71   Layout1->addWidget(GroupPoints, 2, 0);
72   /***************************************************************/
73
74   Init();
75 }
76
77
78 //=================================================================================
79 // function : ~RepairGUI_SuppressFacesDlg()
80 // purpose  : Destroys the object and frees any allocated resources
81 //=================================================================================
82 RepairGUI_SuppressFacesDlg::~RepairGUI_SuppressFacesDlg()
83 {
84 }
85
86
87 //=================================================================================
88 // function : Init()
89 // purpose  :
90 //=================================================================================
91 void RepairGUI_SuppressFacesDlg::Init()
92 {
93   /* init variables */
94   myEditCurrentArgument = GroupPoints->LineEdit1;
95
96   myObjects = new GEOM::ListOfGO();
97   myObjects->length( 0 );
98
99   //myGeomGUI->SetState( 0 );
100   initSelection();
101           
102   /* signals and slots connections */
103   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
104   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
105   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
106
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
132 //=================================================================================
133 // function : ClickOnApply()
134 // purpose  :
135 //=================================================================================
136 bool RepairGUI_SuppressFacesDlg::ClickOnApply()
137 {
138   if ( !onAccept() )
139     return false;
140
141   initName();
142
143   myEditCurrentArgument->setText("");
144   myObjects->length( 0 );
145   myFaces.clear();
146
147   initSelection();
148   
149   return true;
150 }
151
152
153 //=================================================================================
154 // function : ClickOnCancel()
155 // purpose  :
156 //=================================================================================
157 void RepairGUI_SuppressFacesDlg::ClickOnCancel()
158 {
159   GEOMBase_Skeleton::ClickOnCancel();
160 }
161
162
163 //=================================================================================
164 // function : SelectionIntoArgument()
165 // purpose  : Called when selection as changed or other case
166 //          : used only by SelectButtonC1A1 (LineEditC1A1)
167 //=================================================================================
168 void RepairGUI_SuppressFacesDlg::SelectionIntoArgument()
169 {
170   myEditCurrentArgument->setText("");
171
172   Standard_Boolean aRes = Standard_False;
173   int i = 0;
174   int numFaces = 0;
175   myObjects->length( IObjectCount() );
176   myFaces.clear();
177   for ( SALOME_ListIteratorOfListIO anIt( selectedIO() ); anIt.More(); anIt.Next() )
178   {
179     Handle(SALOME_InteractiveObject) anIO = anIt.Value();
180     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIO, aRes );
181     if ( !CORBA::is_nil( aSelectedObject ) && aRes )
182     {
183       TopoDS_Shape aShape;
184       if ( myGeomBase->GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) )
185       {
186         if ( aShape.ShapeType() <= TopAbs_FACE ) // FACE, SHELL, SOLID, COMPOUND
187         {
188           GEOM::short_array anIndexes;
189
190           TColStd_IndexedMapOfInteger aMap;
191           ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr()->GetIndexes( anIO, aMap );
192           
193           if ( !aMap.IsEmpty() )
194           {
195             Convert( aMap, anIndexes );
196             myObjects[i++] = aSelectedObject; // append the object
197             myFaces.append( anIndexes );   // append faces' indexes
198             numFaces += anIndexes.length();// just for text field output
199           }
200         }
201       }
202     }
203   }
204   myObjects->length( i ); // this is the right length, smaller of equal to the previously set
205   if ( numFaces )
206     myEditCurrentArgument->setText( QString::number( numFaces ) + "_" + tr( "GEOM_FACE" ) + tr( "_S_" ) );
207 }
208
209 //=================================================================================
210 // function : Convert()
211 // purpose  :
212 //=================================================================================
213 void RepairGUI_SuppressFacesDlg::Convert( const TColStd_IndexedMapOfInteger& theMap, GEOM::short_array& theOutSeq )
214 {
215   const int n = theMap.Extent();
216   theOutSeq.length( n );
217   for ( int i = 0; i < n; i++ )
218     theOutSeq[i] = theMap( i+1 );
219 }
220
221 //=================================================================================
222 // function : SetEditCurrentArgument()
223 // purpose  :
224 //=================================================================================
225 void RepairGUI_SuppressFacesDlg::SetEditCurrentArgument()
226 {
227   if( sender() == GroupPoints->PushButton1 )
228   {
229     GroupPoints->LineEdit1->setFocus();
230     myEditCurrentArgument = GroupPoints->LineEdit1;
231   }
232   SelectionIntoArgument();
233 }
234
235
236 //=================================================================================
237 // function : LineEditReturnPressed()
238 // purpose  :
239 //=================================================================================
240 void RepairGUI_SuppressFacesDlg::LineEditReturnPressed()
241 {
242   if( sender() == GroupPoints->LineEdit1 )
243   {
244     myEditCurrentArgument = GroupPoints->LineEdit1;
245     GEOMBase_Skeleton::LineEditReturnPressed();
246   }
247 }
248
249
250 //=================================================================================
251 // function : DeactivateActiveDialog()
252 // purpose  :
253 //=================================================================================
254 void RepairGUI_SuppressFacesDlg::DeactivateActiveDialog()
255 {
256   //myGeomGUI->SetState( -1 );
257   GEOMBase_Skeleton::DeactivateActiveDialog();
258 }
259
260
261 //=================================================================================
262 // function : ActivateThisDialog()
263 // purpose  :
264 //=================================================================================
265 void RepairGUI_SuppressFacesDlg::ActivateThisDialog()
266 {
267   GEOMBase_Skeleton::ActivateThisDialog();
268   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
269           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
270
271   //myGeomGUI->SetState( 0 );
272   initSelection();
273 }
274
275
276 //=================================================================================
277 // function : enterEvent()
278 // purpose  : Mouse enter onto the dialog to activate it
279 //=================================================================================
280 void RepairGUI_SuppressFacesDlg::enterEvent(QEvent* e)
281 {
282   if ( !GroupConstructors->isEnabled() )
283     ActivateThisDialog();
284 }
285
286
287 //=================================================================================
288 // function : closeEvent()
289 // purpose  :
290 //=================================================================================
291 void RepairGUI_SuppressFacesDlg::closeEvent(QCloseEvent* e)
292 {
293   //myGeomGUI->SetState( -1 );
294   GEOMBase_Skeleton::closeEvent( e );
295 }
296
297 //=================================================================================
298 // function : createOperation
299 // purpose  :
300 //=================================================================================
301 GEOM::GEOM_IOperations_ptr RepairGUI_SuppressFacesDlg::createOperation()
302 {
303   return getGeomEngine()->GetIHealingOperations( getStudyId() );
304 }
305
306 //=================================================================================
307 // function : isValid
308 // purpose  :
309 //=================================================================================
310 bool RepairGUI_SuppressFacesDlg::isValid( QString& msg )
311 {
312   const int objL = myObjects->length(), facesL = myFaces.size();
313   return ( objL && objL == facesL );
314 }
315
316 //=================================================================================
317 // function : execute
318 // purpose  :
319 //=================================================================================
320 bool RepairGUI_SuppressFacesDlg::execute( ObjectList& objects )
321 {
322   QStringList anErrorObjNames;
323   for ( int i = 0; i < myObjects->length(); i++ )
324   {
325     GEOM::GEOM_Object_var obj = myObjects[i];
326     GEOM::short_array faces = myFaces[i];
327     //MESSAGE(">>>> Dlg, passing faces.. len = " << faces.length());
328     GEOM::GEOM_Object_var anObj = GEOM::GEOM_IHealingOperations::_narrow( getOperation() )->SuppressFaces( obj, faces );
329     if ( anObj->_is_nil() )
330       anErrorObjNames << GEOMBase::GetName( obj );
331     else
332       objects.push_back( anObj._retn() );
333   }
334
335   if ( !anErrorObjNames.empty() )
336     MESSAGE("ERRORS occured while processing the following objects: " << anErrorObjNames.join( " " ));
337
338   return anErrorObjNames.size() < myObjects->length(); // true if at least one object was OK, false if ALL objects were nil after Healing.
339 }
340
341 //=================================================================================
342 // function : initSelection
343 // purpose  :
344 //=================================================================================
345 void RepairGUI_SuppressFacesDlg::initSelection()
346 {
347   GEOM::GEOM_Object_var aNullGeomObject;
348   localSelection( aNullGeomObject, TopAbs_FACE ); // load local selection on ALL objects
349 }