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