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