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