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