Salome HOME
Fix for PAL10496.
[modules/geom.git] / src / RepairGUI / RepairGUI_FreeBoundDlg.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_FreeBoundDlg.cxx
25 //  Author : Sergey LITONIN
26 //  Module : GEOM
27 //  $Header$
28
29 #include "RepairGUI_FreeBoundDlg.h"
30 #include "GEOMBase.h"
31 #include "GeometryGUI.h"
32 #include "GEOM_Displayer.h"
33 #include "GEOMImpl_Types.hxx"
34
35 #include "SalomeApp_Application.h"
36 #include "LightApp_Application.h"
37 #include "LightApp_SelectionMgr.h"
38 #include "SUIT_MessageBox.h"
39 #include "SUIT_Session.h"
40
41 #include <TColStd_MapOfInteger.hxx>
42
43 #include <qlineedit.h>
44 #include <qlabel.h>
45 #include <qlayout.h>
46 #include <qgroupbox.h>
47 #include <qpushbutton.h>
48
49 #define SPACING 5
50 #define MARGIN 10
51
52 /*!
53   Class       : RepairGUI_FreeBoundDlg
54   Description : Dialog for displaying free boundaries of selected face, shell or solid
55 */
56
57 //=================================================================================
58 // function : RepairGUI_FreeBoundDlg
59 // purpose  : Constructor
60 //=================================================================================
61 RepairGUI_FreeBoundDlg::RepairGUI_FreeBoundDlg(GeometryGUI* theGUI, QWidget* theParent, const char*)
62 : QDialog( theParent, "RepairGUI_FreeBoundDlg", false,
63            WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose ),
64   GEOMBase_Helper( dynamic_cast<SUIT_Desktop*>( theParent ) ),
65   myGeomGUI( theGUI )
66 {
67   setCaption( tr( "CAPTION" ) );
68
69   QPixmap iconSelect( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM",tr( "ICON_SELECT" ) ) );
70   
71   QGroupBox* aMainGrp = new QGroupBox( 1, Qt::Horizontal, tr( "FREE_BOUND" ), this );
72   
73   QGroupBox* aSelGrp = new QGroupBox( 1, Qt::Vertical, aMainGrp );
74   aSelGrp->setInsideMargin( 0 );
75   aSelGrp->setFrameStyle( QFrame::NoFrame );
76   new QLabel( tr( "GEOM_OBJECT" ), aSelGrp );
77   ( new QPushButton( aSelGrp ) )->setPixmap( iconSelect );
78   myEdit = new QLineEdit( aSelGrp );
79   myEdit->setReadOnly( true );
80   myEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
81
82   myClosedLbl = new QLabel( tr( "NUMBER_CLOSED" ), aMainGrp );
83   myOpenLbl = new QLabel( tr( "NUMBER_OPEN" ), aMainGrp );
84
85   QFrame* aFrame = new QFrame( this );
86   aFrame->setFrameStyle( QFrame::Box | QFrame::Sunken );
87   QPushButton* aCloseBtn = new QPushButton( tr( "GEOM_BUT_CLOSE" ), aFrame );
88   QPushButton* aHelpBtn = new QPushButton( tr( "GEOM_BUT_HELP" ), aFrame );
89   QHBoxLayout* aBtnLay = new QHBoxLayout( aFrame, MARGIN, SPACING );
90   aBtnLay->addWidget( aCloseBtn );
91   aBtnLay->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
92   aBtnLay->addWidget( aHelpBtn );
93
94   QVBoxLayout* aLay = new QVBoxLayout( this );
95   aLay->setSpacing( SPACING );
96   aLay->setMargin( MARGIN );
97   aLay->addWidget( aMainGrp );
98   aLay->addWidget( aFrame );
99   
100   myHelpFileName = "check_free_boundaries.htm";
101
102   connect( aCloseBtn, SIGNAL( clicked() ), SLOT( onClose() ) );
103   connect( aHelpBtn, SIGNAL( clicked() ), SLOT( onHelp() ) );
104
105   Init();
106 }
107
108 //=================================================================================
109 // function : ~RepairGUI_FreeBoundDlg
110 // purpose  : Destructor
111 //=================================================================================
112 RepairGUI_FreeBoundDlg::~RepairGUI_FreeBoundDlg()
113 {
114 }
115
116 //=================================================================================
117 // function : onClose
118 // purpose  : SLOT. Called when "close" button pressed. Close dialog
119 //=================================================================================
120 void RepairGUI_FreeBoundDlg::onClose()
121 {
122   globalSelection();
123   disconnect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0 );
124   myGeomGUI->SetActiveDialogBox( 0 );
125   reject();
126   erasePreview();
127 }
128
129 //=================================================================================
130 // function : onHelp()
131 // purpose  :
132 //=================================================================================
133 void RepairGUI_FreeBoundDlg::onHelp()
134 {
135   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
136   if (app)
137     app->onHelpContextModule(myGeomGUI ? app->moduleName(myGeomGUI->moduleName()) : QString(""), myHelpFileName);
138   else {
139     SUIT_MessageBox::warn1
140       (0, tr("WRN_WARNING"), tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
141        arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
142        tr("BUT_OK"));
143   }
144 }
145
146 //=================================================================================
147 // function : onDeactivate
148 // purpose  : Deactivate this dialog
149 //=================================================================================
150 void RepairGUI_FreeBoundDlg::onDeactivate()
151 {
152   setEnabled(false);
153   globalSelection();
154   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
155   myGeomGUI->SetActiveDialogBox( 0 );
156 }
157
158 //=================================================================================
159 // function : onActivate
160 // purpose  : Activate this dialog
161 //=================================================================================
162 void RepairGUI_FreeBoundDlg::onActivate()
163 {
164   myGeomGUI->EmitSignalDeactivateDialog();
165   setEnabled( true );
166   myGeomGUI->SetActiveDialogBox( this );
167   connect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
168            SIGNAL( currentSelectionChanged() ), SLOT( onSelectionDone() ) );
169   activateSelection();
170   onSelectionDone();
171 }
172
173 //=================================================================================
174 // function : onSelectionDone
175 // purpose  : SLOT. Called when selection changed.
176 //=================================================================================
177 void RepairGUI_FreeBoundDlg::onSelectionDone()
178 {
179   if( IObjectCount() != 1 )
180     return;
181
182   Standard_Boolean isOk = Standard_False;
183   GEOM::GEOM_Object_var anObj =
184     GEOMBase::ConvertIOinGEOMObject( firstIObject(), isOk );
185
186   if ( !isOk || anObj->_is_nil() || !GEOMBase::IsShape( anObj ) )
187     return;
188   else
189   {
190     myObj = anObj;
191     displayPreview( false, true, true, 3 );
192   }
193 }
194
195 //=================================================================================
196 // function : Init
197 // purpose  : Initialize dialog fields
198 //=================================================================================
199 void RepairGUI_FreeBoundDlg::Init()
200 {
201   myNbClosed = myNbOpen = 0;
202   myObj = GEOM::GEOM_Object::_nil();
203
204   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), SLOT  ( onDeactivate() ) );
205   connect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
206            SIGNAL( currentSelectionChanged() ), SLOT( onSelectionDone() ) );
207
208   activateSelection();
209   onSelectionDone();
210 }
211
212 //=================================================================================
213 // function : enterEvent
214 // purpose  : Activate dialog
215 //=================================================================================
216 void RepairGUI_FreeBoundDlg::enterEvent( QEvent* e )
217 {
218   onActivate();
219 }
220
221 //=================================================================================
222 // function : closeEvent
223 // purpose  : Close dialog
224 //=================================================================================
225 void RepairGUI_FreeBoundDlg::closeEvent( QCloseEvent* e )
226 {
227   onClose();
228 }
229
230 //=================================================================================
231 // function : activateSelection
232 // purpose  : activate selection of faces, shells, and solids
233 //=================================================================================
234 void RepairGUI_FreeBoundDlg::activateSelection()
235 {
236   TColStd_MapOfInteger aMap;
237   aMap.Add( GEOM_FACE );
238   aMap.Add( GEOM_SHELL );
239   aMap.Add( GEOM_SOLID );
240   aMap.Add( GEOM_COMPOUND );
241   globalSelection( aMap );
242 }
243
244 //=================================================================================
245 // function : createOperation
246 // purpose  : Create operation
247 //=================================================================================
248 GEOM::GEOM_IOperations_ptr RepairGUI_FreeBoundDlg::createOperation()
249 {
250   return getGeomEngine()->GetIHealingOperations( getStudyId() );
251 }
252
253 //=================================================================================
254 // function : isValid
255 // purpose  :
256 //=================================================================================
257 bool RepairGUI_FreeBoundDlg::isValid( QString& msg )
258 {
259   return !myObj->_is_nil();
260 }
261
262 //=================================================================================
263 // function : execute
264 // purpose  : Get free boundaries
265 //=================================================================================
266 bool RepairGUI_FreeBoundDlg::execute( ObjectList& objects )
267 {
268   if ( !IsPreview() || myObj->_is_nil() )
269     return false;
270
271   GEOM::ListOfGO_var aClosed, anOpen;
272
273   bool result = GEOM::GEOM_IHealingOperations::_narrow(
274     getOperation() )->GetFreeBoundary( myObj, aClosed, anOpen );
275
276   if ( result )
277   {
278     myNbClosed = aClosed->length();
279     myNbOpen = anOpen->length();
280     int i;
281     for ( i = 0; i < myNbClosed; i++ )
282       objects.push_back( aClosed[i]._retn() );
283     for ( i = 0; i < myNbOpen; i++ )
284       objects.push_back( anOpen[i]._retn() );
285
286     myEdit->setText( GEOMBase::GetName( myObj ) );
287     QString aLabelText = tr( "NUMBER_CLOSED" ) + QString( "%1" ).arg( myNbClosed );
288     myClosedLbl->setText( aLabelText );
289     aLabelText = tr( "NUMBER_OPEN" ) + QString( "%1" ).arg( myNbOpen );
290     myOpenLbl->setText( aLabelText );
291   }
292   else
293   {
294     myEdit->setText( GEOMBase::GetName( myObj ) );
295     myClosedLbl->setText( tr( "NUMBER_CLOSED" ) );
296     myOpenLbl->setText( tr( "NUMBER_OPEN" ) );
297   }
298
299   return result;
300 }
301
302 //=================================================================================
303 // function : keyPressEvent()
304 // purpose  :
305 //=================================================================================
306 void RepairGUI_FreeBoundDlg::keyPressEvent( QKeyEvent* e )
307 {
308   QDialog::keyPressEvent( e );
309   if ( e->isAccepted() )
310     return;
311
312   if ( e->key() == Key_F1 )
313     {
314       e->accept();
315       onHelp();
316     }
317 }