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