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