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