Salome HOME
Merge from V5_1_main 14/05/2010
[modules/geom.git] / src / RepairGUI / RepairGUI_FreeFacesDlg.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_FreeFacesDlg.cxx
25 // Author : Vladimir KLYACHIN, Open CASCADE S.A.S. (vladimir.klyachin@opencascade.com)
26 //
27 #include "RepairGUI_FreeFacesDlg.h"
28
29 #include <LightApp_Application.h>
30 #include <LightApp_SelectionMgr.h>
31 #include <SalomeApp_Application.h>
32 #include <SalomeApp_Tools.h>
33
34 #include <SUIT_MessageBox.h>
35 #include <SUIT_Session.h>
36 #include <SUIT_OverrideCursor.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_ResourceMgr.h>
39
40 #include <GEOMImpl_Types.hxx>
41
42 #include <TopTools_IndexedMapOfShape.hxx>
43 #include <TColStd_MapOfInteger.hxx>
44 #include <TopExp.hxx>
45
46 #include <GEOMBase.h>
47 #include <GeometryGUI.h>
48 #include <GEOM_Displayer.h>
49
50 #include <QLineEdit>
51 #include <QLabel>
52 #include <QGroupBox>
53 #include <QPushButton>
54 #include <QHBoxLayout>
55 #include <QVBoxLayout>
56 #include <QKeyEvent>
57
58 #define SPACING 6
59 #define MARGIN  9
60 #define MIN_WIDTH 200
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                                                 bool modal )
71   : QDialog( parent, false ),
72     GEOMBase_Helper( dynamic_cast<SUIT_Desktop*>( parent ) ),
73     myGeomGUI( GUI ), 
74     myDisplayer( 0 )
75 {
76   setAttribute( Qt::WA_DeleteOnClose );
77
78   setSizeGripEnabled( true );
79
80   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
81
82   setWindowTitle( tr( "GEOM_FREE_FACES_TITLE" ) );
83
84   /***************************************************************/
85
86   QGroupBox* aMainGrp = new QGroupBox( tr( "GEOM_SELECTED_SHAPE" ), this );
87   
88   QLabel* lab = new QLabel( tr( "GEOM_OBJECT" ), aMainGrp );
89   mySelBtn = new QPushButton( aMainGrp );
90   mySelBtn->setIcon( image1 );
91   myEdit = new QLineEdit( aMainGrp );
92   myEdit->setReadOnly( true );
93   myEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
94   myEdit->setMinimumWidth( MIN_WIDTH );
95
96   QHBoxLayout* aMainLay = new QHBoxLayout( aMainGrp );
97   aMainLay->setSpacing( SPACING );
98   aMainLay->setMargin( MARGIN );
99   aMainLay->addWidget( lab );
100   aMainLay->addWidget( mySelBtn );
101   aMainLay->addWidget( myEdit );
102
103   QFrame* aFrame = new QFrame( this );
104   aFrame->setFrameStyle( QFrame::Box | QFrame::Sunken );
105   QPushButton* aCloseBtn = new QPushButton( tr( "GEOM_BUT_CLOSE" ), aFrame );
106   QPushButton* aHelpBtn = new QPushButton( tr( "GEOM_BUT_HELP" ), aFrame );
107
108   QHBoxLayout* aBtnLay = new QHBoxLayout( aFrame );
109   aBtnLay->setSpacing( SPACING );
110   aBtnLay->setMargin( MARGIN );
111   aBtnLay->addWidget( aCloseBtn );
112   aBtnLay->addSpacing( SPACING );
113   aBtnLay->addStretch();
114   aBtnLay->addWidget( aHelpBtn );
115
116   QVBoxLayout* aLay = new QVBoxLayout( this );
117   aLay->setSpacing( SPACING );
118   aLay->setMargin( MARGIN );
119   aLay->addWidget( aMainGrp );
120   aLay->addStretch();
121   aLay->addWidget( aFrame );
122
123   myHelpFileName = "using_measurement_tools_page.html#faces_anchor";
124
125   connect( aCloseBtn, SIGNAL( clicked() ), SLOT( onClose() ) );
126   connect( aHelpBtn,  SIGNAL( clicked() ), SLOT( onHelp() ) );
127   connect( mySelBtn,  SIGNAL( clicked() ),
128            this,      SLOT  ( onSetEditCurrentArgument() ) );
129   /***************************************************************/
130
131   Init();
132 }
133
134
135 //=================================================================================
136 // function : ~RepairGUI_FreeFacesDlg()
137 // purpose  : Destroys the object and frees any allocated resources
138 //=================================================================================
139 RepairGUI_FreeFacesDlg::~RepairGUI_FreeFacesDlg()
140 {
141 }
142
143
144 //=================================================================================
145 // function : onClose
146 // purpose  : SLOT. Called when "close" button pressed. Close dialog
147 //=================================================================================
148 void RepairGUI_FreeFacesDlg::onClose()
149 {
150   globalSelection();
151   disconnect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 0, this, 0 );
152   myGeomGUI->SetActiveDialogBox( 0 );
153   reject();
154   erasePreview();
155 }
156
157 //=================================================================================
158 // function : onHelp()
159 // purpose  :
160 //=================================================================================
161 void RepairGUI_FreeFacesDlg::onHelp()
162 {
163   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
164   if ( app )
165     app->onHelpContextModule( myGeomGUI ? app->moduleName( myGeomGUI->moduleName() ) : QString(""), myHelpFileName );
166   else {
167     QString platform;
168 #ifdef WIN32
169     platform = "winapplication";
170 #else
171     platform = "application";
172 #endif
173     SUIT_MessageBox::warning( this, 
174                               tr( "WRN_WARNING" ),
175                               tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
176                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", 
177                                                                     platform ) ).
178                               arg( myHelpFileName ) );
179   }
180 }
181
182 //=================================================================================
183 // function : onDeactivate
184 // purpose  : Deactivate this dialog
185 //=================================================================================
186 void RepairGUI_FreeFacesDlg::onDeactivate()
187 {
188   setEnabled( false );
189   globalSelection();
190   disconnect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 0, this, 0 );
191   myGeomGUI->SetActiveDialogBox( 0 );
192 }
193
194 //=================================================================================
195 // function : onActivate
196 // purpose  : Activate this dialog
197 //=================================================================================
198 void RepairGUI_FreeFacesDlg::onActivate()
199 {
200   myGeomGUI->EmitSignalDeactivateDialog();
201   setEnabled( true );
202   myGeomGUI->SetActiveDialogBox( this );
203   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
204            SIGNAL( currentSelectionChanged() ), SLOT( onSelectionDone() ) );
205   activateSelection();
206 }
207
208 //=================================================================================
209 // function : Init()
210 // purpose  :
211 //=================================================================================
212 void RepairGUI_FreeFacesDlg::Init()
213 {
214   myObj = GEOM::GEOM_Object::_nil();
215
216   /* signals and slots connections */
217   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), SLOT  ( onDeactivate() ) );
218   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
219            SIGNAL( currentSelectionChanged() ), SLOT  ( onSelectionDone() ) );
220
221   activateSelection();
222   onSelectionDone();
223 }
224
225 //=================================================================================
226 // function : onSelectionDone
227 // purpose  : SLOT. Called when selection changed.
228 //=================================================================================
229 void RepairGUI_FreeFacesDlg::onSelectionDone()
230 {
231   erasePreview();
232
233   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
234   SALOME_ListIO aSelList;
235   aSelMgr->selectedObjects(aSelList);
236
237   if ( aSelList.Extent() != 1 ) {
238     myEdit->setText( "" );
239     return;
240   }
241
242   Standard_Boolean isOk = Standard_False;
243   GEOM::GEOM_Object_var anObj =
244     GEOMBase::ConvertIOinGEOMObject( aSelList.First(), isOk );
245
246   if ( !isOk || anObj->_is_nil() || !GEOMBase::IsShape( anObj ) ) {
247     myEdit->setText( "" );
248     return;
249   }
250   else {
251     myObj = anObj;
252     displayPreview( false, true, true, 3 );
253   }
254 }
255
256 //=================================================================================
257 // function : enterEvent()
258 // purpose  : Mouse enter onto the dialog to activate it
259 //=================================================================================
260 void RepairGUI_FreeFacesDlg::enterEvent( QEvent* )
261 {
262   onActivate();
263 }
264
265 //=================================================================================
266 // function : activateSelection
267 // purpose  : activate selection of faces, shells, and solids
268 //=================================================================================
269 void RepairGUI_FreeFacesDlg::activateSelection()
270 {
271   TColStd_MapOfInteger aMap;
272   aMap.Add( GEOM_SOLID );
273   aMap.Add( GEOM_COMPOUND );
274   globalSelection( aMap );
275 }
276
277 //=================================================================================
278 // function : closeEvent()
279 // purpose  :
280 //=================================================================================
281 void RepairGUI_FreeFacesDlg::closeEvent( QCloseEvent* )
282 {
283   onClose();
284 }
285
286 //=================================================================================
287 // function : createOperation
288 // purpose  :
289 //=================================================================================
290 GEOM::GEOM_IOperations_ptr RepairGUI_FreeFacesDlg::createOperation()
291 {
292   return getGeomEngine()->GetIShapesOperations( getStudyId() );
293 }
294
295 //=================================================================================
296 // function : isValid
297 // purpose  :
298 //=================================================================================
299 bool RepairGUI_FreeFacesDlg::isValid( QString& )
300 {
301   return !myObj->_is_nil() ;
302 }
303
304 //=================================================================================
305 // function : execute
306 // purpose  :
307 //=================================================================================
308 bool RepairGUI_FreeFacesDlg::execute( ObjectList& objects )
309 {
310   bool aResult = false;
311   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
312   GEOM::ListOfLong_var aFaceLst = anOper->GetFreeFacesIDs( myObj );
313   TopoDS_Shape aSelShape;
314   TopoDS_Shape aFace; 
315   TopTools_IndexedMapOfShape anIndices;
316   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) ) {
317     myEdit->setText( GEOMBase::GetName( myObj ) );
318     QString aMess;
319     if ( !isValid( aMess ) ) {
320       erasePreview( true );
321       return false;
322     }
323     
324     SUIT_OverrideCursor();
325
326     TopExp::MapShapes( aSelShape, anIndices);
327     SALOME_Prs* aPrs = 0;
328     
329     for ( int i = 0, n = aFaceLst->length(); i < n; i++ ) {
330       aFace = anIndices.FindKey( aFaceLst[i] );
331       try {
332         getDisplayer()->SetColor( Quantity_NOC_RED );
333         getDisplayer()->SetToActivate( false );
334         aPrs = !aFace.IsNull() ? getDisplayer()->BuildPrs( aFace ) : 0;
335         if ( aPrs )
336           displayPreview( aPrs, true );
337       }
338       catch( const SALOME::SALOME_Exception& e )
339       {
340         SalomeApp_Tools::QtCatchCorbaException( e );
341       }
342     }
343   }
344   return aResult;
345 }
346
347 //================================================================
348 // Function : getDisplayer
349 // Purpose  :
350 //================================================================
351 GEOM_Displayer* RepairGUI_FreeFacesDlg::getDisplayer()
352 {
353   if ( !myDisplayer )
354     myDisplayer = new GEOM_Displayer( getStudy() );
355   return myDisplayer;
356 }
357
358 //=================================================================================
359 // function : SetEditCurrentArgument
360 // purpose  :
361 //=================================================================================
362 void RepairGUI_FreeFacesDlg::onSetEditCurrentArgument()
363 {
364   myEdit->setFocus();
365   onSelectionDone();
366 }
367
368 //=================================================================================
369 // function : keyPressEvent()
370 // purpose  :
371 //=================================================================================
372 void RepairGUI_FreeFacesDlg::keyPressEvent( QKeyEvent* e )
373 {
374   QDialog::keyPressEvent( e );
375   if ( e->isAccepted() )
376     return;
377
378   if ( e->key() == Qt::Key_F1 ) {
379     e->accept();
380     onHelp();
381   }
382 }