Salome HOME
SMH: Merged GEOM (NEWGUI, HEAD, POLYWORK)
[modules/geom.git] / src / RepairGUI / RepairGUI_FreeFacesDlg.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_FreeFacesDlg.cxx
25 //  Author : VKN
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "RepairGUI_FreeFacesDlg.h"
31
32 #include "SalomeApp_Application.h"
33 #include "SalomeApp_SelectionMgr.h"
34 #include "SalomeApp_Tools.h"
35 #include "SUIT_Session.h"
36
37 #include "SALOME_ListIteratorOfListIO.hxx"
38
39 #include "GEOMImpl_Types.hxx"
40 #include <TopTools_IndexedMapOfShape.hxx>
41 #include <TColStd_MapOfInteger.hxx>
42 #include <TopExp.hxx>
43 #include "GEOMBase.h"
44 #include "GeometryGUI.h"
45 #include "GEOM_Displayer.h"
46
47 #include <qapplication.h>
48 #include <qlineedit.h>
49 #include <qlabel.h>
50 #include <qlayout.h>
51 #include <qgroupbox.h>
52 #include <qpushbutton.h>
53 #define SPACING 5
54 #define MARGIN 10
55 #define MIN_WIDTH 200
56
57
58 //=================================================================================
59 // class    : RepairGUI_FreeFacesDlg()
60 // purpose  : Constructs a RepairGUI_FreeFacesDlg  which is a child of 'parent', with the
61 //            name 'name' and widget flags set to 'f'.
62 //            The dialog will by default be modeless, unless you set 'modal' to
63 //            TRUE to construct a modal dialog.
64 //=================================================================================
65 RepairGUI_FreeFacesDlg::RepairGUI_FreeFacesDlg(GeometryGUI* GUI, QWidget* parent, const char* name, bool modal, WFlags fl)
66 :QDialog( parent, "RepairGUI_FreeBoundDlg", false,
67           WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose ),
68  myGeomGUI( GUI )
69 {
70   myDisplayer = 0;
71
72   setSizeGripEnabled( TRUE );
73   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
74
75   setCaption(tr("GEOM_FREE_FACES_TITLE"));
76
77   /***************************************************************/
78
79   QGroupBox* aMainGrp = new QGroupBox( 1, Qt::Horizontal, tr( "GEOM_SELECTED_SHAPE" ), this );
80   
81
82   QGroupBox* aSelGrp = new QGroupBox( 1, Qt::Vertical, aMainGrp );
83
84   aSelGrp->setInsideMargin( 0 );
85   aSelGrp->setFrameStyle( QFrame::NoFrame );
86   new QLabel( tr( "GEOM_OBJECT" ), aSelGrp );
87   mySelBtn = new QPushButton( aSelGrp );
88   mySelBtn->setPixmap( image1 );
89   myEdit = new QLineEdit( aSelGrp );
90   myEdit->setReadOnly( true );
91   myEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
92   myEdit->setMinimumWidth( MIN_WIDTH );
93
94   QFrame* aFrame = new QFrame( this );
95   aFrame->setFrameStyle( QFrame::Box | QFrame::Sunken );
96   QPushButton* aCloseBtn = new QPushButton( tr( "GEOM_BUT_CLOSE" ), aFrame );
97   QHBoxLayout* aBtnLay = new QHBoxLayout( aFrame, MARGIN, SPACING );
98   aBtnLay->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
99   aBtnLay->addWidget( aCloseBtn );
100   aBtnLay->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
101
102   QVBoxLayout* aLay = new QVBoxLayout( this );
103   aLay->setSpacing( SPACING );
104   aLay->setMargin( MARGIN );
105   aLay->addWidget( aMainGrp );
106   aLay->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
107   aLay->addWidget( aFrame );
108   
109   connect( aCloseBtn, SIGNAL( clicked() ), SLOT( onClose() ) );
110   connect( mySelBtn,    SIGNAL( clicked() ),
111            this,        SLOT  ( onSetEditCurrentArgument() ) );
112   /***************************************************************/
113
114   Init();
115 }
116
117
118 //=================================================================================
119 // function : ~RepairGUI_FreeFacesDlg()
120 // purpose  : Destroys the object and frees any allocated resources
121 //=================================================================================
122 RepairGUI_FreeFacesDlg::~RepairGUI_FreeFacesDlg()
123 {
124 }
125
126
127 //=================================================================================
128 // function : onClose
129 // purpose  : SLOT. Called when "close" button pressed. Close dialog
130 //=================================================================================
131 void RepairGUI_FreeFacesDlg::onClose()
132 {
133   globalSelection();
134   disconnect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0 );
135   myGeomGUI->SetActiveDialogBox( 0 );
136   reject();
137   erasePreview();
138 }
139
140 //=================================================================================
141 // function : onDeactivate
142 // purpose  : Deactivate this dialog
143 //=================================================================================
144 void RepairGUI_FreeFacesDlg::onDeactivate()
145 {
146   setEnabled(false);
147   globalSelection();
148   disconnect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0 );
149   myGeomGUI->SetActiveDialogBox( 0 );
150 }
151
152 //=================================================================================
153 // function : onActivate
154 // purpose  : Activate this dialog
155 //=================================================================================
156 void RepairGUI_FreeFacesDlg::onActivate()
157 {
158   myGeomGUI->EmitSignalDeactivateDialog();
159   setEnabled( true );
160   myGeomGUI->SetActiveDialogBox( this );
161   connect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
162            SIGNAL( currentSelectionChanged() ), SLOT  ( onSelectionDone() ) );
163   activateSelection();
164 }
165
166 //=================================================================================
167 // function : Init()
168 // purpose  :
169 //=================================================================================
170 void RepairGUI_FreeFacesDlg::Init()
171 {
172   myObj = GEOM::GEOM_Object::_nil();
173
174   /* signals and slots connections */
175   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), SLOT  ( onDeactivate() ) );
176   connect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
177            SIGNAL( currentSelectionChanged() ), SLOT  ( onSelectionDone() ) );
178
179   activateSelection();
180   onSelectionDone();
181 }
182
183 //=================================================================================
184 // function : onSelectionDone
185 // purpose  : SLOT. Called when selection changed.
186 //=================================================================================
187 void RepairGUI_FreeFacesDlg::onSelectionDone()
188 {
189   erasePreview();
190   if( IObjectCount() != 1 )
191   {
192     myEdit->setText( "" );
193     return;
194   }
195
196   Standard_Boolean isOk = Standard_False;
197   GEOM::GEOM_Object_var anObj =
198     GEOMBase::ConvertIOinGEOMObject( firstIObject(), isOk );
199
200   if ( !isOk || anObj->_is_nil() || !GEOMBase::IsShape( anObj ) )
201   {
202     myEdit->setText( "" );
203     return;
204   }
205   else
206   {
207     myObj = anObj;
208     displayPreview( false, true, true, 3 );
209   }
210 }
211
212 //=================================================================================
213 // function : enterEvent()
214 // purpose  : Mouse enter onto the dialog to activate it
215 //=================================================================================
216 void RepairGUI_FreeFacesDlg::enterEvent(QEvent* e)
217 {
218   onActivate();
219 }
220
221 //=================================================================================
222 // function : activateSelection
223 // purpose  : activate selection of faces, shells, and solids
224 //=================================================================================
225 void RepairGUI_FreeFacesDlg::activateSelection()
226 {
227   TColStd_MapOfInteger aMap;
228   aMap.Add( GEOM_SOLID );
229   aMap.Add( GEOM_COMPOUND );
230   globalSelection( aMap );
231 }
232
233 //=================================================================================
234 // function : closeEvent()
235 // purpose  :
236 //=================================================================================
237 void RepairGUI_FreeFacesDlg::closeEvent(QCloseEvent* e)
238 {
239   onClose();
240 }
241
242 //=================================================================================
243 // function : createOperation
244 // purpose  :
245 //=================================================================================
246 GEOM::GEOM_IOperations_ptr RepairGUI_FreeFacesDlg::createOperation()
247 {
248   return getGeomEngine()->GetIShapesOperations( getStudyId() );
249 }
250
251 //=================================================================================
252 // function : isValid
253 // purpose  :
254 //=================================================================================
255 bool RepairGUI_FreeFacesDlg::isValid( QString& msg )
256 {
257   return !myObj->_is_nil() ;
258 }
259
260 //=================================================================================
261 // function : execute
262 // purpose  :
263 //=================================================================================
264 bool RepairGUI_FreeFacesDlg::execute( ObjectList& objects )
265 {
266   bool aResult = false;
267   GEOM::ListOfLong_var aFaceLst = 
268     GEOM::GEOM_IShapesOperations::_narrow( getOperation() )->GetFreeFacesIDs( myObj );
269   TopoDS_Shape aSelShape;
270   TopoDS_Shape aFace; 
271   TopTools_IndexedMapOfShape anIndices;
272   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) )
273   {
274     myEdit->setText( GEOMBase::GetName( myObj ) );
275     QString aMess;
276     if ( !isValid( aMess ) )
277     {
278       erasePreview( true );
279       return false;
280     }
281
282     TopExp::MapShapes( aSelShape, anIndices);
283     SALOME_Prs* aPrs = 0;
284     QApplication::setOverrideCursor( Qt::waitCursor );
285
286     for ( int i = 0, n = aFaceLst->length(); i < n; i++ )
287     {
288       aFace = anIndices.FindKey( aFaceLst[i] );
289       try
290       {
291         getDisplayer()->SetColor( Quantity_NOC_RED );
292         getDisplayer()->SetToActivate( false );
293         aPrs = !aFace.IsNull() ? getDisplayer()->BuildPrs( aFace ) : 0;
294         if ( aPrs )
295           displayPreview( aPrs, true );
296       }
297       catch( const SALOME::SALOME_Exception& e )
298       {
299         SalomeApp_Tools::QtCatchCorbaException( e );
300       }
301     }
302   }
303   return aResult;
304 }
305
306 //================================================================
307 // Function : getDisplayer
308 // Purpose  :
309 //================================================================
310 GEOM_Displayer* RepairGUI_FreeFacesDlg::getDisplayer()
311 {
312   if ( !myDisplayer )
313     myDisplayer = new GEOM_Displayer( getStudy() );
314   return myDisplayer;
315 }
316
317 //=================================================================================
318 // function : SetEditCurrentArgument
319 // purpose  :
320 //=================================================================================
321 void RepairGUI_FreeFacesDlg::onSetEditCurrentArgument()
322 {
323   myEdit->setFocus();
324   onSelectionDone();
325 }
326
327 //=================================================================================
328 // function : getDesktop()
329 // purpose  :
330 //=================================================================================
331 SUIT_Desktop* RepairGUI_FreeFacesDlg::getDesktop() const
332 {
333   return dynamic_cast<SUIT_Desktop*>( parentWidget() );
334 }