Salome HOME
Update copyright
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_SubShapeSelectorWdg.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File   : StdMeshersGUI_SubShapeSelectorWdg.cxx
21 // Author : Open CASCADE S.A.S. (dmv)
22 // SMESH includes
23 //
24 #include "StdMeshersGUI_SubShapeSelectorWdg.h"
25
26 // SMESH Includes
27 #include "SMESH_Type.h"
28 #include "SMESHGUI_MeshUtils.h"
29 #include "SMESH_Actor.h"
30 #include "SMESH_PreviewActorsCollection.h"
31 #include "SMESH_ActorUtils.h"
32 #include "SMESHGUI_GroupUtils.h"
33 #include "SMESH_Gen_i.hxx"
34 #include "SMESHGUI_GEOMGenUtils.h"
35 #include "SMESH_LogicalFilter.hxx"
36
37 // SVTK Includes
38 #include <SVTK_ViewWindow.h>
39 #include <SVTK_ViewModel.h>
40 #include <SVTK_ViewWindow.h>
41 #include <SVTK_Selector.h>
42
43 // SALOME GUI includes
44 #include <SALOME_ListIO.hxx>
45 #include <LightApp_SelectionMgr.h>
46 #include <SALOME_ListIteratorOfListIO.hxx>
47
48 // SUIT Includes
49 #include <SUIT_ResourceMgr.h>
50
51 // GEOM Includes
52 #include <GEOMBase.h>
53 #include <GEOM_TypeFilter.h>
54 #include <GEOM_CompoundFilter.h>
55
56 // Qt includes
57 #include <QPushButton>
58 #include <QGridLayout>
59 #include <QListWidget>
60 #include <QCheckBox>
61 #include <QLineEdit>
62
63 // OCCT includes
64 #include <TColStd_MapOfInteger.hxx>
65 #include <TColStd_IndexedMapOfInteger.hxx>
66 #include <TopoDS_Shape.hxx>
67 #include <TopExp.hxx>
68 #include <TopExp_Explorer.hxx>
69 #include <StdSelect_TypeOfEdge.hxx>
70
71 // SALOME KERNEL includes
72 #include <SALOMEDS_SObject.hxx>
73
74
75 #define SPACING 6
76 #define MARGIN 0
77
78 //================================================================================
79 /*!
80  *  Constructor
81  */
82 //================================================================================
83
84 StdMeshersGUI_SubShapeSelectorWdg
85 ::StdMeshersGUI_SubShapeSelectorWdg( QWidget * parent, TopAbs_ShapeEnum aSubShType ): 
86   QWidget( parent ),
87   myPreviewActor( 0 ),
88   myMaxSize( -1 )
89 {
90   QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
91
92   QGridLayout* edgesLayout = new QGridLayout( this );
93   edgesLayout->setMargin( MARGIN );
94   edgesLayout->setSpacing( SPACING );
95   
96   myListWidget    = new QListWidget( this );
97   myAddButton    = new QPushButton( tr( "SMESH_BUT_ADD" ),    this );
98   myRemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), this );      
99   myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
100
101   edgesLayout->addWidget(myListWidget,   0, 0, 3, 3);
102   edgesLayout->addWidget(myAddButton,    0, 4);
103   edgesLayout->addWidget(myRemoveButton, 1, 4);
104
105   //edgesLayout->setRowStretch(2, 5);
106   edgesLayout->setColumnStretch(2, 5);
107
108   setLayout( edgesLayout );
109   setMinimumWidth( 300 );
110
111   mySubShType = aSubShType;
112
113   init();
114 }
115
116 //================================================================================
117 /*!
118  *  Destructor
119  */
120 //================================================================================
121
122 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
123 {
124   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
125     if ( myPreviewActor ) {
126       myPreviewActor->RemoveFromRender( myRenderer );
127       aViewWindow->Repaint();
128
129       delete myPreviewActor;
130       myPreviewActor = 0;
131     }
132   }
133   myEntry = "";
134   myParamValue = "";
135   myMainShape.Nullify();
136
137   if ( mySelectionMgr && myFilter )
138     mySelectionMgr->removeFilter( myFilter );
139   delete myFilter; myFilter=0;
140
141   SUIT_SelectionFilter* filter;
142   foreach( filter, myGeomFilters )
143     delete filter;
144 }
145
146 //================================================================================
147 /*!
148  *  Create a layout, initialize fields
149  */
150 //================================================================================
151
152 void StdMeshersGUI_SubShapeSelectorWdg::init()
153 {
154   myParamValue = "";
155   myIsNotCorrected = true; // to dont call the GetCorrectedValue method twice
156   myListOfIDs.clear();
157   mySelectedIDs.clear();
158
159   myAddButton->setEnabled( false );
160   myRemoveButton->setEnabled( false );
161
162   mySMESHGUI     = SMESHGUI::GetSMESHGUI();
163   mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
164   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
165
166   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
167     aViewWindow->SetSelectionMode( ActorSelection );
168
169   myFilter=0;
170   //setFilter();
171
172   connect( myAddButton,    SIGNAL(clicked()), SLOT(onAdd()));
173   connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
174   
175   connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
176   connect( myListWidget,   SIGNAL(itemSelectionChanged()),    this, SLOT(onListSelectionChanged()));
177
178   updateState();
179 }
180
181 //================================================================================
182 /*!
183  * \brief Install filters to select sub-shapes of mySubShType or their groups
184  */
185 //================================================================================
186
187 void StdMeshersGUI_SubShapeSelectorWdg::setFilter()
188 {
189   SalomeApp_Study* study = mySMESHGUI->activeStudy();
190   GEOM_TypeFilter* typeFilter = new GEOM_TypeFilter(study, mySubShType, /*isShapeType=*/true );
191   GEOM_CompoundFilter* gpoupFilter = new GEOM_CompoundFilter(study);
192   gpoupFilter->addSubType( mySubShType );
193   myGeomFilters.append( typeFilter );
194   myGeomFilters.append( gpoupFilter );
195   myFilter = new SMESH_LogicalFilter( myGeomFilters, SMESH_LogicalFilter::LO_OR );
196   mySelectionMgr->installFilter( myFilter );
197 }
198
199 //================================================================================
200 /*!
201  *  Create a layout, initialize fields
202  */
203 //================================================================================
204
205 void StdMeshersGUI_SubShapeSelectorWdg::showPreview( bool visible)
206 {
207   if ( !myPreviewActor )
208     return;
209
210   if ( myIsShown != visible ) {
211     myPreviewActor->SetShown( visible );
212     
213     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
214       aViewWindow->Repaint();
215
216     myIsShown = visible;
217   }
218 }
219
220 //=================================================================================
221 // function : SelectionIntoArgument()
222 // purpose  : Called when selection as changed or other case
223 //=================================================================================
224 void StdMeshersGUI_SubShapeSelectorWdg::SelectionIntoArgument()
225 {
226   if ( !myPreviewActor )
227     return;
228
229   mySelectedIDs.clear();
230
231   // get selected mesh
232   SALOME_ListIO aList;
233   mySelectionMgr->selectedObjects( aList );
234   int nbSel = aList.Extent();
235
236   if (nbSel > 0) {
237     SALOME_ListIteratorOfListIO anIt (aList);
238     
239     for ( ; anIt.More(); anIt.Next()) { // Loop on selected objects
240       Handle(SALOME_InteractiveObject) IO = anIt.Value();
241       
242       GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( IO->getEntry() );  
243       if ( !CORBA::is_nil( aGeomObj ) ) { // Selected Object From Study
244         GEOM::GEOM_Object_ptr aGeomFatherObj = aGeomObj->GetMainShape();
245         QString aFatherEntry = "";
246         QString aMainFatherEntry = "";
247         TopoDS_Shape shape;
248         if ( !CORBA::is_nil( aGeomFatherObj ) ) {
249           // Get Main Shape
250           GEOM::GEOM_Object_var aGeomMain = GetGeomObjectByEntry( myEntry );
251           if ( !CORBA::is_nil( aGeomMain ) && aGeomMain->GetType() == 37 ) {  // Main Shape is a Group
252             GEOM::GEOM_Object_ptr aMainFatherObj = aGeomMain->GetMainShape();
253             if ( !CORBA::is_nil( aMainFatherObj ) )
254               aMainFatherEntry = aMainFatherObj->GetStudyEntry();
255           }
256           aFatherEntry = aGeomFatherObj->GetStudyEntry();
257         }
258         
259         if ( aFatherEntry != "" && ( aFatherEntry == myEntry || aFatherEntry == aMainFatherEntry ) )
260         {
261           if ( aGeomObj->GetType() == 37 /*GEOM_GROUP*/ ) { // Selected Group that belongs the main object
262             GEOMBase::GetShape(aGeomObj, shape); 
263             if ( !shape.IsNull() ) {
264               TopExp_Explorer exp( shape, mySubShType );
265               for ( ; exp.More(); exp.Next() ) {
266                 int index = myPreviewActor->GetIndexByShape( exp.Current() );
267                 if ( index ) {
268                   mySelectedIDs.append( index );
269                   myPreviewActor->HighlightID( index );
270                 }
271               }
272             }
273           } else if ( aGeomObj->GetType() == 28 /*GEOM_SUBSHAPE*/  ) {
274             GEOMBase::GetShape(aGeomObj, shape); 
275             if ( !shape.IsNull() && shape.ShapeType() == mySubShType ) {
276               int index = myPreviewActor->GetIndexByShape( shape );
277               if ( index ) {
278                 mySelectedIDs.append( index );
279                 myPreviewActor->HighlightID( index );
280               }
281             }
282           }
283         }
284       } else { // Selected Actor from Actor Collection
285         QString anEntry = IO->getEntry();
286         QString str = "_";
287         int index = anEntry.lastIndexOf( str );
288         anEntry.remove(0, index+1);
289         int ind = anEntry.toInt();
290         if ( ind )
291           mySelectedIDs.append( ind );
292       }
293     }
294   }
295   // update add button
296   myAddButton->setEnabled( ( myListWidget->count() < myMaxSize || myMaxSize == -1 ) && mySelectedIDs.size() > 0 && ( mySelectedIDs.size() <= myMaxSize || myMaxSize == -1 ) );
297
298   //Connect Selected Ids in viewer and dialog's Ids list
299   myListWidget->clearSelection();
300   if ( mySelectedIDs.size() > 0 ) {
301     for (int i = 0; i < mySelectedIDs.size(); i++) {
302       QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
303       QList<QListWidgetItem*> anItems = myListWidget->findItems ( anID, Qt::MatchExactly );
304       QListWidgetItem* item;
305       foreach(item, anItems)
306         item->setSelected(true);
307     }
308   }
309 }
310
311 //=================================================================================
312 // function : onAdd()
313 // purpose  : Called when Add Button Clicked
314 //=================================================================================
315 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
316 {
317   if ( mySelectedIDs.size() < 1 )
318     return;
319
320   myListWidget->blockSignals( true );
321   for (int i = 0; i < mySelectedIDs.size() && (myMaxSize == -1 || myListOfIDs.size() < myMaxSize); i++) {
322     if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
323       QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
324
325       QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
326       anItem->setSelected(true);
327       
328       myListOfIDs.append( mySelectedIDs.at(i) );
329     }
330   }
331   onListSelectionChanged();
332   myListWidget->blockSignals( false );
333   myAddButton->setEnabled( myMaxSize == -1 || myListOfIDs.size() < myMaxSize );
334 }
335          
336 //=================================================================================
337 // function : onRemove()
338 // purpose  : Called when Remove Button Clicked
339 //=================================================================================
340 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
341 {
342   if ( myListWidget->count() < 1 )
343     return;
344
345   myListWidget->blockSignals( true );
346   QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
347   QListWidgetItem* item;
348   foreach(item, selItems) {
349     QString idStr = item->text();
350     int id = idStr.toInt();
351
352     int index = myListOfIDs.indexOf( id );
353     myListOfIDs.removeAt( index );
354     delete item;
355   }
356
357   onListSelectionChanged();
358   myListWidget->blockSignals( false );
359   
360   myAddButton->setEnabled( true );
361 }
362
363 //=================================================================================
364 // function : onListSelectionChanged()
365 // purpose  : Called when selection in element list is changed
366 //=================================================================================
367 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
368 {
369   if ( !myPreviewActor )
370     return;
371
372   mySelectionMgr->clearSelected();
373   TColStd_MapOfInteger aIndexes;
374   QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
375   QListWidgetItem* anItem;
376   foreach(anItem, selItems)
377     myPreviewActor->HighlightID( anItem->text().toInt() );
378
379   // update remove button
380   myRemoveButton->setEnabled( selItems.size() > 0 );
381 }
382
383 //=================================================================================
384 // function : setGeomShape
385 // purpose  : Called to set geometry whose sub-shapes are selected
386 //================================================================================
387 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
388 {
389   if ( theEntry != "") {
390     myParamValue = theEntry;
391     myEntry = theEntry;
392     myGeomShape = GetTopoDSByEntry( theEntry );
393     updateState();
394     myIsNotCorrected = true;
395   }
396 }
397
398 //=================================================================================
399 // function : updateState
400 // purpose  : update Widget state
401 //=================================================================================
402 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
403 {
404   bool state = false;
405   if ( !myGeomShape.IsNull() )
406     state = true;
407   
408   myListWidget->setEnabled( state );
409   myAddButton->setEnabled( mySelectedIDs.size() > 0 );
410   
411   if (state) {
412     myPreviewActor = new SMESH_PreviewActorsCollection();
413     myPreviewActor->SetSelector( mySelector );
414     myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
415     myPreviewActor->SetShown( false );
416     myIsShown = false;
417     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
418       myRenderer = aViewWindow->getRenderer();
419       myPreviewActor->AddToRender( myRenderer );
420       aViewWindow->Repaint();
421     }
422   }
423 }
424
425 //=================================================================================
426 // function : GetGeomObjectByEntry
427 // purpose  : Called to get GeomObject
428 //=================================================================================
429 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
430 {
431   GEOM::GEOM_Object_var aGeomObj;
432   SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
433   if (aStudy != 0) {
434     SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
435     SALOMEDS::GenericAttribute_var anAttr;
436
437     if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
438       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
439       CORBA::String_var aVal = anIOR->Value();
440       CORBA::Object_var obj = aStudy->ConvertIORToObject(aVal);
441       aGeomObj = GEOM::GEOM_Object::_narrow(obj);
442     }
443   }
444   return aGeomObj;
445 }
446
447 //=================================================================================
448 // function : setObjectByEntry
449 // purpose  : Called to get GeomObject
450 //=================================================================================
451 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
452 {
453   TopoDS_Shape shape;
454   GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
455   GEOMBase::GetShape(aGeomObj, shape);
456   return shape;
457 }
458
459 //=================================================================================
460 // function : GetListOfIds
461 // purpose  : Called to get the list of SubShapes IDs
462 //=================================================================================
463 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
464 {
465   SMESH::long_array_var anArray = new SMESH::long_array;
466
467   if ( myMainEntry != "" && myIsNotCorrected )
468     myListOfIDs = GetCorrectedListOfIDs( true );
469
470   int size = myListOfIDs.size();
471   anArray->length( size );
472   if ( size ) {
473     for (int i = 0; i < size; i++) {
474         anArray[i] = myListOfIDs.at(i);
475     }
476   }
477   return anArray;
478 }
479
480 //=================================================================================
481 // function : SetListOfIds
482 // purpose  : Called to set the list of SubShapes IDs
483 //=================================================================================
484 void StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
485 {
486   mySelectedIDs.clear();
487   myListOfIDs.clear();
488   int size = theIds->length();
489   for ( int i = 0; i < size; i++ )
490     mySelectedIDs.append( theIds[ i ] );
491
492   mySelectedIDs = GetCorrectedListOfIDs( false );
493   onAdd();
494 }
495
496 //=================================================================================
497 // function : SetMainShapeEntry
498 // purpose  : Called to set the Entry of main shape of the mesh
499 //=================================================================================
500 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
501 {
502   myMainEntry = theEntry;
503   myMainShape = GetTopoDSByEntry( theEntry );
504   myIsNotCorrected = true;
505 }
506
507 //=================================================================================
508 // function : GetMainShapeEntry
509 // purpose  : Called to get the Main Object Entry
510 //=================================================================================
511 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
512 {
513   if ( myMainEntry == "")
514     return myEntry.toLatin1().data();
515
516   return myMainEntry.toLatin1().data();
517 }
518
519 //=================================================================================
520 // function : GetCorrectedListOfIds
521 // purpose  : Called to convert the list of IDs from subshape IDs to main shape IDs
522 //=================================================================================
523 QList<int> StdMeshersGUI_SubShapeSelectorWdg::GetCorrectedListOfIDs( bool fromSubshapeToMainshape )
524 {
525   if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) &&  fromSubshapeToMainshape )
526     return myListOfIDs;
527   else   if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) &&  !fromSubshapeToMainshape )
528     return mySelectedIDs;
529
530   QList<int> aList;
531   TopTools_IndexedMapOfShape   aGeomMap;
532   TopTools_IndexedMapOfShape   aMainMap;
533   TopExp::MapShapes(myGeomShape, aGeomMap);
534   TopExp::MapShapes(myMainShape, aMainMap);
535
536   if ( fromSubshapeToMainshape ) { // convert indexes from subshape to mainshape
537     int size = myListOfIDs.size();
538     for (int i = 0; i < size; i++) {
539       TopoDS_Shape aSubShape = aGeomMap.FindKey( myListOfIDs.at(i) );
540       int index = aMainMap.FindIndex( aSubShape );
541       aList.append( index );
542     }
543     myIsNotCorrected = false;
544   } else { // convert indexes from main shape to subshape
545     int size = mySelectedIDs.size();
546     for (int i = 0; i < size; i++) {
547       TopoDS_Shape aSubShape = aMainMap.FindKey( mySelectedIDs.at(i) );
548       int index = aGeomMap.FindIndex( aSubShape );
549       aList.append( index );
550     }
551   }
552
553   return aList;
554 }