Salome HOME
0758dd4cfe3fb211a8f70edb9333785b1c7d0557
[modules/geom.git] / src / RepairGUI / RepairGUI_FreeFacesDlg.cxx
1 // Copyright (C) 2007-2023  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 <GEOMBase.h>
30 #include <GeometryGUI.h>
31 #include <GEOMImpl_Types.hxx>
32 #include <GEOM_Constants.h>
33
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <SalomeApp_Application.h>
37 #include <SalomeApp_Study.h>
38 #include <SalomeApp_Tools.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_OverrideCursor.h>
41 #include <SUIT_ResourceMgr.h>
42 #include <SUIT_Session.h>
43 #include <SUIT_ViewManager.h>
44 #include <SUIT_ViewWindow.h>
45
46 #include <TColStd_MapOfInteger.hxx>
47 #include <TopExp.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49
50 #include <QGroupBox>
51 #include <QHBoxLayout>
52 #include <QLabel>
53 #include <QLineEdit>
54 #include <QPushButton>
55 #include <QRadioButton>
56 #include <QVBoxLayout>
57
58
59 #define SPACING 6
60 #define MARGIN  9
61 #define MIN_WIDTH 200
62
63 //=================================================================================
64 // class    : RepairGUI_FreeFacesDlg()
65 // purpose  : Constructs a RepairGUI_FreeFacesDlg  which is a child of 'parent', with the
66 //            name 'name' and widget flags set to 'f'.
67 //            The dialog will by default be modeless, unless you set 'modal' to
68 //            TRUE to construct a modal dialog.
69 //=================================================================================
70 RepairGUI_FreeFacesDlg::RepairGUI_FreeFacesDlg( GeometryGUI* GUI, QWidget* parent,
71                                                 bool modal)
72   : GEOMBase_Skeleton(GUI, parent, modal),
73     myDisplayer      (0),
74     myEdit           (0)
75 {
76   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_FREE_FACES")));
77   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
78
79   setWindowTitle( tr( "GEOM_FREE_FACES_TITLE" ) );
80
81   /***************************************************************/
82
83   mainFrame()->GroupConstructors->setTitle(tr("GEOM_FREE_FACES"));
84   mainFrame()->RadioButton1->setIcon(image0);
85   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
86   mainFrame()->RadioButton2->close();
87   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
88   mainFrame()->RadioButton3->close();
89
90   mainFrame()->GroupBoxName->hide();
91
92   QGroupBox* aMainGrp = new QGroupBox( tr( "GEOM_SELECTED_SHAPE" ), this );
93   
94   QLabel* lab = new QLabel( tr( "GEOM_OBJECT" ), aMainGrp );
95   QPushButton *aSelBtn = new QPushButton( aMainGrp );
96   aSelBtn->setIcon( image1 );
97   myEdit = new QLineEdit( aMainGrp );
98   myEdit->setReadOnly( true );
99   myEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
100   myEdit->setMinimumWidth( MIN_WIDTH );
101
102   QHBoxLayout* aMainLay = new QHBoxLayout( aMainGrp );
103   aMainLay->setSpacing( SPACING );
104   aMainLay->setMargin( MARGIN );
105   aMainLay->addWidget( lab );
106   aMainLay->addWidget( aSelBtn );
107   aMainLay->addWidget( myEdit );
108
109   QVBoxLayout* aLay = new QVBoxLayout (centralWidget());
110   aLay->setSpacing(SPACING);
111   aLay->setMargin(MARGIN);
112   aLay->addWidget(aMainGrp);
113
114   resize(minimumSizeHint());
115   /***************************************************************/
116
117   myHelpFileName = "free_faces_page.html";
118
119   Init();
120 }
121
122
123 //=================================================================================
124 // function : ~RepairGUI_FreeFacesDlg()
125 // purpose  : Destroys the object and frees any allocated resources
126 //=================================================================================
127 RepairGUI_FreeFacesDlg::~RepairGUI_FreeFacesDlg()
128 {
129 }
130
131
132 //=================================================================================
133 // function : Init()
134 // purpose  :
135 //=================================================================================
136 void RepairGUI_FreeFacesDlg::Init()
137 {
138   myObj = GEOM::GEOM_Object::_nil();
139   myEditCurrentArgument = myEdit;
140
141   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
142   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
143   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
144           this, SLOT(SelectionIntoArgument()));
145
146   initName(tr("GEOM_FREE_FACES_NAME"));
147   buttonOk()->setEnabled(false);
148   buttonApply()->setEnabled(false);
149   activateSelection();
150   SelectionIntoArgument();
151 }
152
153 //=================================================================================
154 // function : ClickOnOk()
155 // purpose  :
156 //=================================================================================
157 void RepairGUI_FreeFacesDlg::ClickOnOk()
158 {
159   if (ClickOnApply())
160     ClickOnCancel();
161 }
162
163 //=================================================================================
164 // function : ClickOnApply()
165 // purpose  :
166 //=================================================================================
167 bool RepairGUI_FreeFacesDlg::ClickOnApply()
168 {
169   if (!onAccept())
170     return false;
171
172   initName();
173   return true;
174 }
175
176 //=================================================================================
177 // function : ActivateThisDialog()
178 // purpose  :
179 //=================================================================================
180 void RepairGUI_FreeFacesDlg::ActivateThisDialog()
181 {
182   GEOMBase_Skeleton::ActivateThisDialog();
183
184   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
185           this, SLOT(SelectionIntoArgument()));
186
187   activateSelection();
188   displayPreview(true);
189 }
190
191 //=================================================================================
192 // function : enterEvent()
193 // purpose  :
194 //=================================================================================
195 void RepairGUI_FreeFacesDlg::enterEvent(QEvent*)
196 {
197   if (!mainFrame()->GroupConstructors->isEnabled())
198     ActivateThisDialog();
199 }
200
201 //=================================================================================
202 // function : SelectionIntoArgument
203 // purpose  : SLOT. Called when selection changed.
204 //=================================================================================
205 void RepairGUI_FreeFacesDlg::SelectionIntoArgument()
206 {
207   myEditCurrentArgument->setText("");
208   myObj = GEOM::GEOM_Object::_nil();
209   erasePreview();
210
211   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
212   SALOME_ListIO aSelList;
213   aSelMgr->selectedObjects(aSelList);
214
215   if ( aSelList.Extent() != 1 ) {
216     buttonOk()->setEnabled(false);
217     buttonApply()->setEnabled(false);
218     return;
219   }
220
221   GEOM::GEOM_Object_var anObj =
222     GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
223
224   if ( !GEOMBase::IsShape( anObj ) ) {
225     buttonOk()->setEnabled(false);
226     buttonApply()->setEnabled(false);
227     return;
228   } else {
229     myObj = anObj;
230     myEditCurrentArgument->setText(GEOMBase::GetName(myObj));
231     displayPreview( true, false, true, true, 3 );
232   }
233 }
234
235 //=================================================================================
236 // function : activateSelection
237 // purpose  : activate selection of solids
238 //=================================================================================
239 void RepairGUI_FreeFacesDlg::activateSelection()
240 {
241   TColStd_MapOfInteger aMap;
242   aMap.Add( GEOM_SOLID );
243   aMap.Add( GEOM_COMPOUND );
244   globalSelection( aMap );
245 }
246
247 //=================================================================================
248 // function : createOperation
249 // purpose  :
250 //=================================================================================
251 GEOM::GEOM_IOperations_ptr RepairGUI_FreeFacesDlg::createOperation()
252 {
253   return getGeomEngine()->GetIShapesOperations();
254 }
255
256 //=================================================================================
257 // function : isValid
258 // purpose  :
259 //=================================================================================
260 bool RepairGUI_FreeFacesDlg::isValid( QString& )
261 {
262   return !myObj->_is_nil() ;
263 }
264
265 //=================================================================================
266 // function : execute
267 // purpose  :
268 //=================================================================================
269 bool RepairGUI_FreeFacesDlg::execute( ObjectList& objects )
270 {
271   bool aResult = false;
272   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
273   GEOM::ListOfLong_var aFaceLst = anOper->GetFreeFacesIDs( myObj );
274   TopoDS_Shape aSelShape;
275   TopoDS_Shape aFace; 
276   TopTools_IndexedMapOfShape anIndices;
277   int aNbObj = 0;
278
279   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) ) {
280     myEdit->setText( GEOMBase::GetName( myObj ) );
281     QString aMess;
282     if ( !isValid( aMess ) ) {
283       erasePreview( true );
284       buttonOk()->setEnabled(false);
285       buttonApply()->setEnabled(false);
286       return false;
287     }
288     
289     SUIT_OverrideCursor wc;
290
291     TopExp::MapShapes( aSelShape, anIndices);
292     SALOME_Prs* aPrs = 0;
293     
294     //Get object trancparency and set it to preview
295     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
296     double transparency = 1.0;
297     if( app ) {
298       SUIT_ViewWindow* window = app->desktop( )->activeWindow( );
299       if( window && window->getViewManager() ) {
300         if ( app ) {
301           SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
302           if( aStudy ) {
303             int aMgrId = window->getViewManager()->getGlobalId();
304             CORBA::String_var aMainEntry = myObj->GetStudyEntry();
305             QString anEntry = aMainEntry.in();
306             QVariant v = aStudy->getObjectProperty( aMgrId , anEntry , GEOM::propertyName( GEOM::Transparency ) , transparency );
307             if( v.canConvert( QVariant::Double ) ) {
308               transparency = v.toDouble();
309             }
310           }
311         }
312       }
313     }
314
315     int i;
316     int n = aFaceLst->length();
317
318     for (i = 0; i < n; i++ ) {
319       aFace = anIndices.FindKey( aFaceLst[i] );
320       try {
321         getDisplayer()->SetColor( Quantity_NOC_RED );
322         getDisplayer()->SetTransparency( transparency );
323         getDisplayer()->SetWidth( 2 );
324         getDisplayer()->SetNbIsos( 1 );
325         getDisplayer()->SetIsosWidth( 1 );
326         getDisplayer()->SetIsosColor( Quantity_NOC_RED );
327         getDisplayer()->SetDisplayMode( 0 );
328         getDisplayer()->SetToActivate( false );
329         aPrs = !aFace.IsNull() ? getDisplayer()->BuildPrs( aFace ) : 0;
330         if ( aPrs )
331           displayPreview( aPrs, true );
332       }
333       catch( const SALOME::SALOME_Exception& e )
334       {
335         SalomeApp_Tools::QtCatchCorbaException( e );
336         buttonOk()->setEnabled(false);
337         buttonApply()->setEnabled(false);
338         return false;
339       }
340     }
341
342     // Create sub-objects
343     GEOM::ListOfGO_var aList = anOper->MakeSubShapes(myObj, aFaceLst);
344
345     aNbObj = aList->length();
346
347     for (i = 0; i < aNbObj; i++) {
348       objects.push_back(GEOM::GEOM_Object::_duplicate(aList[i]));
349     }
350
351     aResult = true;
352   }
353
354   buttonOk()->setEnabled(aNbObj > 0);
355   buttonApply()->setEnabled(aNbObj > 0);
356
357   return aResult;
358 }
359
360 //================================================================
361 // Function : getDisplayer
362 // Purpose  :
363 //================================================================
364 GEOM_Displayer* RepairGUI_FreeFacesDlg::getDisplayer()
365 {
366   if ( !myDisplayer )
367     myDisplayer = new GEOM_Displayer();
368   return myDisplayer;
369 }
370
371 //================================================================
372 // Function : getFather
373 // Purpose  : Get father object for object to be added in study
374 //            (called with addInStudy method)
375 //================================================================
376 GEOM::GEOM_Object_ptr RepairGUI_FreeFacesDlg::getFather (GEOM::GEOM_Object_ptr)
377 {
378   return myObj;
379 }
380
381 //=================================================================================
382 // function : getSourceObjects
383 // purpose  : virtual method to get source objects
384 //=================================================================================
385 QList<GEOM::GeomObjPtr> RepairGUI_FreeFacesDlg::getSourceObjects()
386 {
387   QList<GEOM::GeomObjPtr> res;
388   GEOM::GeomObjPtr aGeomObjPtr(myObj);
389   res << aGeomObjPtr;
390   return res;
391 }