Salome HOME
To avoid memory leaks
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_SubShapeSelectorWdg.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 // File   : StdMeshersGUI_SubShapeSelectorWdg.cxx
23 // Author : Open CASCADE S.A.S. (dmv)
24 // SMESH includes
25 //
26 #include "StdMeshersGUI_SubShapeSelectorWdg.h"
27
28 // SMESH Includes
29 #include <SMESH_Type.h>
30 #include "SMESHGUI_MeshUtils.h"
31 #include <SMESH_Actor.h>
32 #include <SMESH_PreviewActorsCollection.h>
33 #include <SMESH_ActorUtils.h>
34 #include "SMESHGUI_GroupUtils.h"
35 #include "SMESH_Gen_i.hxx"
36 #include "SMESHGUI_GEOMGenUtils.h"
37
38 // SVTK Includes
39 #include <SVTK_ViewWindow.h>
40 #include <SVTK_ViewModel.h>
41 #include <SVTK_ViewWindow.h>
42 #include <SVTK_Selector.h>
43
44 // SALOME GUI includes
45 #include <SALOME_ListIO.hxx>
46 #include <LightApp_SelectionMgr.h>
47 #include <SALOME_ListIteratorOfListIO.hxx>
48
49 // SUIT Includes
50 #include <SUIT_ResourceMgr.h>
51
52 // GEOM Includes
53 #include <GEOMBase.h>
54
55 // Qt includes
56 #include <QPushButton>
57 #include <QGridLayout>
58 #include <QListWidget>
59 #include <QCheckBox>
60 #include <QLineEdit>
61
62 // OCCT includes
63 #include <TColStd_MapOfInteger.hxx>
64 #include <TColStd_IndexedMapOfInteger.hxx>
65 #include <TopoDS_Shape.hxx>
66 #include <TopExp.hxx>
67 #include <TopExp_Explorer.hxx>
68
69 // SALOME KERNEL includes
70 #include <SALOMEDS_SObject.hxx>
71
72 #define SPACING 6
73 #define MARGIN 0
74
75 //================================================================================
76 /*!
77  *  Constructor
78  */
79 //================================================================================
80
81 StdMeshersGUI_SubShapeSelectorWdg
82 ::StdMeshersGUI_SubShapeSelectorWdg( QWidget * parent ): 
83   QWidget( parent ),
84   myPreviewActor( 0 )
85 {
86   QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
87
88   QGridLayout* edgesLayout = new QGridLayout( this );
89   edgesLayout->setMargin( MARGIN );
90   edgesLayout->setSpacing( SPACING );
91   
92   myListWidget    = new QListWidget( this );
93   myAddButton    = new QPushButton( tr( "SMESH_BUT_ADD" ),    this );
94   myRemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), this );      
95   myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
96
97   edgesLayout->addWidget(myListWidget,   0, 0, 3, 3);
98   edgesLayout->addWidget(myAddButton,    0, 4);
99   edgesLayout->addWidget(myRemoveButton, 1, 4);
100
101   edgesLayout->setRowStretch(2, 5);
102   edgesLayout->setColumnStretch(2, 5);
103
104   setLayout( edgesLayout );
105   setMinimumWidth( 300 );
106
107   myMaxSize = 1000;
108   mySubShType = TopAbs_EDGE;
109
110   init();
111 }
112
113 //================================================================================
114 /*!
115  *  Destructor
116  */
117 //================================================================================
118
119 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
120 {
121   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
122     if ( myPreviewActor ) {
123       myPreviewActor->RemoveFromRender( myRenderer );
124       aViewWindow->Repaint();
125
126       delete myPreviewActor;
127       myPreviewActor = 0;
128     }
129   }
130   myEntry = "";
131   myParamValue = "";
132   myMainShape.Nullify();
133 }
134
135 //================================================================================
136 /*!
137  *  Create a layout, initialize fields
138  */
139 //================================================================================
140
141 void StdMeshersGUI_SubShapeSelectorWdg::init()
142 {
143   myParamValue = "";
144   myListOfIDs.clear();
145   mySelectedIDs.clear();
146
147   mySMESHGUI     = SMESHGUI::GetSMESHGUI();
148   mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
149   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
150
151   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
152     aViewWindow->SetSelectionMode( ActorSelection );
153
154   connect( myAddButton,    SIGNAL(clicked()), SLOT(onAdd()));
155   connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
156   
157   connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
158   connect( myListWidget,   SIGNAL(itemSelectionChanged()),    this, SLOT(onListSelectionChanged()));
159
160   updateState();
161 }
162
163 //================================================================================
164 /*!
165  *  Create a layout, initialize fields
166  */
167 //================================================================================
168
169 void StdMeshersGUI_SubShapeSelectorWdg::showPreview( bool visible)
170 {
171   if ( !myPreviewActor )
172     return;
173
174   if ( myIsShown != visible ) {
175     myPreviewActor->SetShown( visible );
176     
177     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
178       aViewWindow->Repaint();
179
180     myIsShown = visible;
181   }
182 }
183
184 //=================================================================================
185 // function : SelectionIntoArgument()
186 // purpose  : Called when selection as changed or other case
187 //=================================================================================
188 void StdMeshersGUI_SubShapeSelectorWdg::SelectionIntoArgument()
189 {
190   if ( !myPreviewActor )
191     return;
192
193   mySelectedIDs.clear();
194
195   // get selected mesh
196   SALOME_ListIO aList;
197   mySelectionMgr->selectedObjects( aList );
198   int nbSel = aList.Extent();
199
200   if (nbSel < 1)
201     return;
202
203   SALOME_ListIteratorOfListIO anIt (aList);
204     
205   for ( ; anIt.More(); anIt.Next()) { // Loop on selected objects
206     Handle(SALOME_InteractiveObject) IO = anIt.Value();
207
208     GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( IO->getEntry() );  
209     if ( !CORBA::is_nil( aGeomObj ) ) { // Selected Object From Study
210       GEOM::GEOM_Object_ptr aGeomFatherObj = aGeomObj->GetMainShape();
211       QString aFatherEntry = "";
212       QString aMainFatherEntry = "";
213       TopoDS_Shape shape;
214       if ( !CORBA::is_nil( aGeomFatherObj ) ) {
215         // Get Main Shape
216         GEOM::GEOM_Object_var aGeomMain = GetGeomObjectByEntry( myEntry );
217         if ( !CORBA::is_nil( aGeomMain ) && aGeomMain->GetType() == 37 ) {  // Main Shape is a Group
218           GEOM::GEOM_Object_ptr aMainFatherObj = aGeomMain->GetMainShape();
219           if ( !CORBA::is_nil( aMainFatherObj ) )
220             aMainFatherEntry = aMainFatherObj->GetStudyEntry();
221         }
222         aFatherEntry = aGeomFatherObj->GetStudyEntry();
223       }
224       
225       if ( aFatherEntry != "" && ( aFatherEntry == myEntry || aFatherEntry == aMainFatherEntry ) ) {
226         if ( aGeomObj->GetType() == 37 /*GEOM_GROUP*/ ) { // Selected Group that belongs the main object
227           GEOMBase::GetShape(aGeomObj, shape); 
228           if ( !shape.IsNull() ) {
229             TopExp_Explorer exp( shape, mySubShType );
230             for ( ; exp.More(); exp.Next() ) {
231               int index = myPreviewActor->GetIndexByShape( exp.Current() );
232               if ( index ) {
233                 mySelectedIDs.append( index );
234                 myPreviewActor->HighlightID( index );
235               }
236             }
237           }
238         } else if ( aGeomObj->GetType() == 28 /*GEOM_SUBSHAPE*/  ) {
239           GEOMBase::GetShape(aGeomObj, shape); 
240           if ( !shape.IsNull() && shape.ShapeType() == mySubShType ) {
241             int index = myPreviewActor->GetIndexByShape( shape );
242             if ( index ) {
243               mySelectedIDs.append( index );
244               myPreviewActor->HighlightID( index );
245             }
246           }
247         }
248       }
249     } else { // Selected Actor from Actor Collection
250       QString anEntry = IO->getEntry();
251       QString str = "_";
252       int index = anEntry.lastIndexOf( str );
253       anEntry.remove(0, index+1);
254       int ind = anEntry.toInt();
255       if ( ind )
256         mySelectedIDs.append( ind );
257     }
258   }
259 }
260
261 //=================================================================================
262 // function : onAdd()
263 // purpose  : Called when Add Button Clicked
264 //=================================================================================
265 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
266 {
267   if ( mySelectedIDs.size() < 1 )
268     return;
269
270   myListWidget->blockSignals( true );
271   for (int i = 0; i < mySelectedIDs.size(); i++) {
272     if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
273       QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
274
275       QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
276       anItem->setSelected(true);
277       
278       myListOfIDs.append( mySelectedIDs.at(i) );
279     }
280   }
281   onListSelectionChanged();
282
283   myListWidget->blockSignals( false );
284
285   if( myListOfIDs.size() >= myMaxSize )
286     myAddButton->setEnabled( false );
287 }
288          
289 //=================================================================================
290 // function : onRemove()
291 // purpose  : Called when Remove Button Clicked
292 //=================================================================================
293 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
294 {
295   if ( myListWidget->count() < 1 )
296     return;
297
298   myListWidget->blockSignals( true );
299   QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
300   QListWidgetItem* item;
301   foreach(item, selItems) {
302     QString idStr = item->text();
303     int id = idStr.toInt();
304
305     int index = myListOfIDs.indexOf( id );
306     myListOfIDs.removeAt( index );
307     delete item;
308   }
309
310   onListSelectionChanged();
311   myListWidget->blockSignals( false );
312   
313   myAddButton->setEnabled( true );
314 }
315
316 //=================================================================================
317 // function : onListSelectionChanged()
318 // purpose  : Called when selection in element list is changed
319 //=================================================================================
320 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
321 {
322   if ( !myPreviewActor )
323     return;
324
325   mySelectionMgr->clearSelected();
326   TColStd_MapOfInteger aIndexes;
327   QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
328   QListWidgetItem* anItem;
329   foreach(anItem, selItems)
330     myPreviewActor->HighlightID( anItem->text().toInt() );
331 }
332
333 //=================================================================================
334 // function : setGeomShape
335 // purpose  : Called to set geometry
336 //================================================================================
337 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
338 {
339   if ( theEntry != "") {
340     myParamValue = theEntry;
341     myEntry = theEntry;
342     myMainShape = GetTopoDSByEntry( theEntry );
343     updateState();
344   }
345 }
346
347 //=================================================================================
348 // function : updateState
349 // purpose  : update Widget state
350 //=================================================================================
351 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
352 {
353   bool state = false;
354   if ( !myMainShape.IsNull() )
355     state = true;
356   
357   myListWidget->setEnabled( state );
358   myAddButton->setEnabled( state );
359   myRemoveButton->setEnabled( state );
360   
361   if (state) {
362     myPreviewActor = new SMESH_PreviewActorsCollection();
363     myPreviewActor->SetSelector( mySelector );
364     //myPreviewActor->Init( myMainShape, TopAbs_EDGE, myEntry );
365     myPreviewActor->Init( myMainShape, mySubShType, myEntry );
366     myPreviewActor->SetShown( false );
367     myIsShown = false;
368     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
369       myRenderer = aViewWindow->getRenderer();
370       myPreviewActor->AddToRender( myRenderer );
371       aViewWindow->Repaint();
372     }
373   }
374 }
375
376 //=================================================================================
377 // function : GetGeomObjectByEntry
378 // purpose  : Called to get GeomObject
379 //=================================================================================
380 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
381 {
382   GEOM::GEOM_Object_var aGeomObj;
383   SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
384   if (aStudy != 0) {
385     SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
386     SALOMEDS::GenericAttribute_var anAttr;
387
388     if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
389       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
390       CORBA::String_var aVal = anIOR->Value();
391       CORBA::Object_var obj = aStudy->ConvertIORToObject(aVal);
392       aGeomObj = GEOM::GEOM_Object::_narrow(obj);
393     }
394   }
395   return aGeomObj;
396 }
397
398 //=================================================================================
399 // function : setObjectByEntry
400 // purpose  : Called to get GeomObject
401 //=================================================================================
402 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
403 {
404   TopoDS_Shape shape;
405   GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
406   GEOMBase::GetShape(aGeomObj, shape);
407   return shape;
408 }
409
410 //=================================================================================
411 // function : GetListOfIds
412 // purpose  : Called to get the list of SubShapes IDs
413 //=================================================================================
414 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
415 {
416   SMESH::long_array_var anArray = new SMESH::long_array;
417   int size = myListOfIDs.size();
418   anArray->length( size );
419   if ( size ) {
420     for (int i = 0; i < size; i++) {
421         anArray[i] = myListOfIDs.at(i);
422     }
423   }
424   return anArray;
425 }
426
427 //=================================================================================
428 // function : SetListOfIds
429 // purpose  : Called to set the list of SubShapes IDs
430 //=================================================================================
431 void StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
432 {
433   mySelectedIDs.clear();
434   myListOfIDs.clear();
435   int size = theIds->length();
436   for ( int i = 0; i < size; i++ )
437     mySelectedIDs.append( theIds[ i ] );
438   onAdd();
439 }
440