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