Salome HOME
PR: synchro V6_main tag mergeto_V7_main_11Feb13
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_SubShapeSelectorWdg.cxx
1 // Copyright (C) 2007-2012  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
72 #define SPACING 6
73 #define MARGIN 0
74
75 //================================================================================
76 /*!
77  *  Constructor
78  */
79 //================================================================================
80
81 StdMeshersGUI_SubShapeSelectorWdg
82 ::StdMeshersGUI_SubShapeSelectorWdg( QWidget * parent, TopAbs_ShapeEnum aSubShType ): 
83   QWidget( parent ),
84   myPreviewActor( 0 ),
85   myMaxSize( -1 )
86 {
87   QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
88
89   QGridLayout* edgesLayout = new QGridLayout( this );
90   edgesLayout->setMargin( MARGIN );
91   edgesLayout->setSpacing( SPACING );
92   
93   myListWidget   = new QListWidget( this );
94   myAddButton    = new QPushButton( tr( "SMESH_BUT_ADD" ),    this );
95   myRemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), this );      
96   myInfoLabel    = new QLabel( this );
97   myPrevButton   = new QPushButton( "<<", this );
98   myNextButton   = new QPushButton( ">>", this );
99   myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
100
101   edgesLayout->addWidget(myListWidget,   0, 0, 3, 3);
102   edgesLayout->addWidget(myAddButton,    0, 3);
103   edgesLayout->addWidget(myRemoveButton, 1, 3);
104   edgesLayout->addWidget(myInfoLabel,    3, 0, 1, 3);
105   edgesLayout->addWidget(myPrevButton,   4, 0);
106   edgesLayout->addWidget(myNextButton,   4, 2);
107
108   edgesLayout->setRowStretch(2, 5);
109   edgesLayout->setColumnStretch(1, 5);
110
111   myListWidget->setMinimumWidth(300);
112   myInfoLabel->setMinimumWidth(300);
113   myInfoLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
114   myInfoLabel->setAlignment(Qt::AlignCenter);
115
116   mySubShType = aSubShType;
117
118   init();
119 }
120
121 //================================================================================
122 /*!
123  *  Destructor
124  */
125 //================================================================================
126
127 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
128 {
129   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
130     if ( myPreviewActor ) {
131       myPreviewActor->RemoveFromRender( myRenderer );
132       aViewWindow->Repaint();
133
134       delete myPreviewActor;
135       myPreviewActor = 0;
136     }
137   }
138   myEntry = "";
139   myParamValue = "";
140   myMainShape.Nullify();
141
142   if ( mySelectionMgr && myFilter )
143     mySelectionMgr->removeFilter( myFilter );
144   delete myFilter; myFilter=0;
145
146   SUIT_SelectionFilter* filter;
147   foreach( filter, myGeomFilters )
148     delete filter;
149 }
150
151 //================================================================================
152 /*!
153  *  Create a layout, initialize fields
154  */
155 //================================================================================
156
157 void StdMeshersGUI_SubShapeSelectorWdg::init()
158 {
159   myParamValue = "";
160   myIsNotCorrected = true; // to dont call the GetCorrectedValue method twice
161   myListOfIDs.clear();
162   mySelectedIDs.clear();
163
164   myAddButton->setEnabled( false );
165   myRemoveButton->setEnabled( false );
166
167   mySMESHGUI     = SMESHGUI::GetSMESHGUI();
168   mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
169   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
170
171   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
172     aViewWindow->SetSelectionMode( ActorSelection );
173
174   myFilter=0;
175   //setFilter();
176
177   connect( myAddButton,    SIGNAL(clicked()), SLOT(onAdd()));
178   connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
179   connect( myPrevButton,   SIGNAL(clicked()), SLOT(onPrevious()));
180   connect( myNextButton,   SIGNAL(clicked()), SLOT(onNext()));
181   
182   connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
183   connect( myListWidget,   SIGNAL(itemSelectionChanged()),    this, SLOT(onListSelectionChanged()));
184
185   updateState();
186 }
187
188 //================================================================================
189 /*!
190  * \brief Install filters to select sub-shapes of mySubShType or their groups
191  */
192 //================================================================================
193
194 void StdMeshersGUI_SubShapeSelectorWdg::setFilter()
195 {
196   SalomeApp_Study* study = mySMESHGUI->activeStudy();
197   GEOM_TypeFilter* typeFilter = new GEOM_TypeFilter(study, mySubShType, /*isShapeType=*/true );
198   GEOM_CompoundFilter* gpoupFilter = new GEOM_CompoundFilter(study);
199   gpoupFilter->addSubType( mySubShType );
200   myGeomFilters.append( typeFilter );
201   myGeomFilters.append( gpoupFilter );
202   myFilter = new SMESH_LogicalFilter( myGeomFilters, SMESH_LogicalFilter::LO_OR );
203   mySelectionMgr->installFilter( myFilter );
204 }
205
206 //================================================================================
207 /*!
208  *  Create a layout, initialize fields
209  */
210 //================================================================================
211
212 void StdMeshersGUI_SubShapeSelectorWdg::showPreview( bool visible)
213 {
214   if ( !myPreviewActor )
215     return;
216
217   if ( myIsShown != visible ) {
218     myPreviewActor->SetShown( visible );
219     
220     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
221       aViewWindow->Repaint();
222
223     myIsShown = visible;
224   }
225 }
226
227 //=================================================================================
228 // function : SelectionIntoArgument()
229 // purpose  : Called when selection as changed or other case
230 //=================================================================================
231 void StdMeshersGUI_SubShapeSelectorWdg::SelectionIntoArgument()
232 {
233   if ( !myPreviewActor )
234     return;
235
236   mySelectedIDs.clear();
237
238   // get selected mesh
239   SALOME_ListIO aList;
240   mySelectionMgr->selectedObjects( aList );
241   int nbSel = aList.Extent();
242
243   if (nbSel > 0) {
244     SALOME_ListIteratorOfListIO anIt (aList);
245     
246     for ( ; anIt.More(); anIt.Next()) { // Loop on selected objects
247       Handle(SALOME_InteractiveObject) IO = anIt.Value();
248       
249       GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( IO->getEntry() );
250       if ( !CORBA::is_nil( aGeomObj ) ) { // Selected Object From Study
251         GEOM::GEOM_Object_var aGeomFatherObj = aGeomObj->GetMainShape();
252         QString aFatherEntry = "";
253         QString aMainFatherEntry = "";
254         TopoDS_Shape shape;
255         if ( !CORBA::is_nil( aGeomFatherObj ) ) {
256           // Get Main Shape
257           GEOM::GEOM_Object_var aGeomMain = GetGeomObjectByEntry( myEntry );
258           if ( !CORBA::is_nil( aGeomMain ) && aGeomMain->GetType() == 37 ) {  // Main Shape is a Group
259             GEOM::GEOM_Object_var aMainFatherObj = aGeomMain->GetMainShape();
260             if ( !CORBA::is_nil( aMainFatherObj ) )
261               aMainFatherEntry = aMainFatherObj->GetStudyEntry();
262           }
263           aFatherEntry = aGeomFatherObj->GetStudyEntry();
264         }
265
266         if ( aFatherEntry != "" && ( aFatherEntry == myEntry || aFatherEntry == aMainFatherEntry ) )
267         {
268           if ( aGeomObj->GetType() == 37 /*GEOM_GROUP*/ ) { // Selected Group that belongs the main object
269             GEOMBase::GetShape(aGeomObj, shape); 
270             if ( !shape.IsNull() ) {
271               TopExp_Explorer exp( shape, mySubShType );
272               for ( ; exp.More(); exp.Next() ) {
273                 int index = myPreviewActor->GetIndexByShape( exp.Current() );
274                 if ( index ) {
275                   mySelectedIDs.append( index );
276                   myPreviewActor->HighlightID( index );
277                 }
278               }
279             }
280           } else if ( aGeomObj->GetType() == 28 /*GEOM_SUBSHAPE*/  ) {
281             GEOMBase::GetShape(aGeomObj, shape); 
282             if ( !shape.IsNull() && shape.ShapeType() == mySubShType ) {
283               int index = myPreviewActor->GetIndexByShape( shape );
284               if ( index ) {
285                 mySelectedIDs.append( index );
286                 myPreviewActor->HighlightID( index );
287               }
288             }
289           }
290         }
291       } else { // Selected Actor from Actor Collection
292         QString anEntry = IO->getEntry();
293         QString str = "_";
294         int index = anEntry.lastIndexOf( str );
295         anEntry.remove(0, index+1);
296         int ind = anEntry.toInt();
297         if ( ind )
298           mySelectedIDs.append( ind );
299       }
300     }
301   }
302   // update add button
303   myAddButton->setEnabled( ( myListWidget->count() < myMaxSize || myMaxSize == -1 ) &&
304                            mySelectedIDs.size() > 0 &&
305                            ( mySelectedIDs.size() <= myMaxSize || myMaxSize == -1 ) );
306
307   //Connect Selected Ids in viewer and dialog's Ids list
308   bool signalsBlocked = myListWidget->blockSignals( true );
309   myListWidget->clearSelection();
310   if ( mySelectedIDs.size() > 0 ) {
311     for (int i = 0; i < mySelectedIDs.size(); i++) {
312       QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
313       QList<QListWidgetItem*> anItems = myListWidget->findItems ( anID, Qt::MatchExactly );
314       QListWidgetItem* item;
315       foreach(item, anItems)
316         item->setSelected(true);
317     }
318   }
319   myListWidget->blockSignals( signalsBlocked );
320 }
321
322 //=================================================================================
323 // function : onAdd()
324 // purpose  : Called when Add Button Clicked
325 //=================================================================================
326 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
327 {
328   if ( mySelectedIDs.size() < 1 )
329     return;
330
331   myListWidget->blockSignals( true );
332   for (int i = 0; i < mySelectedIDs.size() && (myMaxSize == -1 || myListOfIDs.size() < myMaxSize); i++) {
333     if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
334       QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
335
336       QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
337       anItem->setSelected(true);
338       
339       myListOfIDs.append( mySelectedIDs.at(i) );
340     }
341   }
342   onListSelectionChanged();
343   myListWidget->blockSignals( false );
344   myAddButton->setEnabled( myMaxSize == -1 || myListOfIDs.size() < myMaxSize );
345 }
346          
347 //=================================================================================
348 // function : onRemove()
349 // purpose  : Called when Remove Button Clicked
350 //=================================================================================
351 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
352 {
353   if ( myListWidget->count() < 1 )
354     return;
355
356   myListWidget->blockSignals( true );
357   QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
358   QListWidgetItem* item;
359   foreach(item, selItems) {
360     QString idStr = item->text();
361     int id = idStr.toInt();
362
363     int index = myListOfIDs.indexOf( id );
364     myListOfIDs.removeAt( index );
365     delete item;
366   }
367
368   onListSelectionChanged();
369   myListWidget->blockSignals( false );
370   
371   myAddButton->setEnabled( true );
372 }
373
374 void StdMeshersGUI_SubShapeSelectorWdg::onPrevious()
375 {
376   if ( myPreviewActor ) {
377     myPreviewActor->previous();
378     myListWidget->clearSelection();
379     updateButtons();
380     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
381       aViewWindow->Repaint();
382   }
383 }
384
385 void StdMeshersGUI_SubShapeSelectorWdg::onNext()
386 {
387   if ( myPreviewActor ) {
388     myPreviewActor->next();
389     myListWidget->clearSelection();
390     updateButtons();
391     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
392       aViewWindow->Repaint();
393   }
394 }
395
396 //=================================================================================
397 // function : onListSelectionChanged()
398 // purpose  : Called when selection in element list is changed
399 //=================================================================================
400 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
401 {
402   if ( !myPreviewActor )
403     return;
404
405   mySelectionMgr->clearSelected();
406   TColStd_MapOfInteger aIndexes;
407   QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
408   QListWidgetItem* anItem;
409   foreach(anItem, selItems)
410     myPreviewActor->HighlightID( anItem->text().toInt() );
411
412   // update remove button
413   myRemoveButton->setEnabled( selItems.size() > 0 );
414 }
415
416 //=================================================================================
417 // function : setGeomShape
418 // purpose  : Called to set geometry whose sub-shapes are selected
419 //================================================================================
420 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
421 {
422   if ( theEntry != "") {
423     myParamValue = theEntry;
424     myEntry = theEntry;
425     myGeomShape = GetTopoDSByEntry( theEntry );
426     updateState();
427     myIsNotCorrected = true;
428   }
429 }
430
431 //=================================================================================
432 // function : updateState
433 // purpose  : update Widget state
434 //=================================================================================
435 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
436 {
437   bool state = false;
438   if ( !myGeomShape.IsNull() )
439     state = true;
440   myInfoLabel->setVisible( false );
441   myPrevButton->setVisible( false );
442   myNextButton->setVisible( false );
443   
444   myListWidget->setEnabled( state );
445   myAddButton->setEnabled( mySelectedIDs.size() > 0 );
446   
447   if (state) {
448     myPreviewActor = new SMESH_PreviewActorsCollection();
449     myPreviewActor->SetSelector( mySelector );
450     myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
451     myPreviewActor->SetShown( false );
452     myIsShown = false;
453     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
454       myRenderer = aViewWindow->getRenderer();
455       myPreviewActor->AddToRender( myRenderer );
456       aViewWindow->Repaint();
457     }
458     updateButtons();
459   }
460 }
461
462 //=================================================================================
463 // function : GetGeomObjectByEntry
464 // purpose  : Called to get GeomObject
465 //=================================================================================
466 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
467 {
468   GEOM::GEOM_Object_var aGeomObj;
469   SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
470   if ( !aStudy->_is_nil() )
471   {
472     SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
473     if (!aSObj->_is_nil() )
474     {
475       CORBA::Object_var obj = aSObj->GetObject();
476       aGeomObj = GEOM::GEOM_Object::_narrow(obj);
477       aSObj->UnRegister();
478     }
479   }
480   return aGeomObj._retn();
481 }
482
483 //=================================================================================
484 // function : setObjectByEntry
485 // purpose  : Called to get GeomObject
486 //=================================================================================
487 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
488 {
489   TopoDS_Shape shape;
490   GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
491   GEOMBase::GetShape(aGeomObj, shape);
492   return shape;
493 }
494
495 //=================================================================================
496 // function : GetListOfIds
497 // purpose  : Called to get the list of SubShapes IDs
498 //=================================================================================
499 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
500 {
501   SMESH::long_array_var anArray = new SMESH::long_array;
502
503   if ( myMainEntry != "" && myIsNotCorrected )
504     myListOfIDs = GetCorrectedListOfIDs( true );
505
506   int size = myListOfIDs.size();
507   anArray->length( size );
508   if ( size ) {
509     for (int i = 0; i < size; i++) {
510         anArray[i] = myListOfIDs.at(i);
511     }
512   }
513   return anArray;
514 }
515
516 //=================================================================================
517 // function : SetListOfIds
518 // purpose  : Called to set the list of SubShapes IDs
519 //=================================================================================
520 void StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
521 {
522   mySelectedIDs.clear();
523   myListOfIDs.clear();
524   int size = theIds->length();
525   for ( int i = 0; i < size; i++ )
526     mySelectedIDs.append( theIds[ i ] );
527
528   mySelectedIDs = GetCorrectedListOfIDs( false );
529   onAdd();
530 }
531
532 //=================================================================================
533 // function : SetMainShapeEntry
534 // purpose  : Called to set the Entry of main shape of the mesh
535 //=================================================================================
536 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
537 {
538   myMainEntry = theEntry;
539   myMainShape = GetTopoDSByEntry( theEntry );
540   myIsNotCorrected = true;
541 }
542
543 //=================================================================================
544 // function : GetMainShapeEntry
545 // purpose  : Called to get the Main Object Entry
546 //=================================================================================
547 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
548 {
549   if ( myMainEntry == "")
550     return myEntry.toLatin1().data();
551
552   return myMainEntry.toLatin1().data();
553 }
554
555 //=================================================================================
556 // function : GetCorrectedListOfIds
557 // purpose  : Called to convert the list of IDs from sub-shape IDs to main shape IDs
558 //=================================================================================
559 QList<int> StdMeshersGUI_SubShapeSelectorWdg::GetCorrectedListOfIDs( bool fromSubshapeToMainshape )
560 {
561   if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) &&  fromSubshapeToMainshape )
562     return myListOfIDs;
563   else   if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) &&  !fromSubshapeToMainshape )
564     return mySelectedIDs;
565
566   QList<int> aList;
567   TopTools_IndexedMapOfShape   aGeomMap;
568   TopTools_IndexedMapOfShape   aMainMap;
569   TopExp::MapShapes(myGeomShape, aGeomMap);
570   TopExp::MapShapes(myMainShape, aMainMap);
571
572   if ( fromSubshapeToMainshape ) { // convert indexes from sub-shape to mainshape
573     int size = myListOfIDs.size();
574     for (int i = 0; i < size; i++) {
575       TopoDS_Shape aSubShape = aGeomMap.FindKey( myListOfIDs.at(i) );
576       int index = aMainMap.FindIndex( aSubShape );
577       aList.append( index );
578     }
579     myIsNotCorrected = false;
580   } else { // convert indexes from main shape to sub-shape
581     int size = mySelectedIDs.size();
582     for (int i = 0; i < size; i++) {
583       TopoDS_Shape aSubShape = aMainMap.FindKey( mySelectedIDs.at(i) );
584       int index = aGeomMap.FindIndex( aSubShape );
585       aList.append( index );
586     }
587   }
588
589   return aList;
590 }
591
592 void StdMeshersGUI_SubShapeSelectorWdg::updateButtons()
593 {
594   if ( myPreviewActor ) {
595     int total = myPreviewActor->count();
596     int chunk = myPreviewActor->currentChunk();
597     int chunkSize = myPreviewActor->chunkSize();
598     int imin = chunk*chunkSize+1;
599     int imax = std::min((chunk+1)*chunkSize, total);
600     bool vis = imax > 0 && total > chunkSize;
601     myInfoLabel->setVisible( vis );
602     myPrevButton->setVisible( vis );
603     myNextButton->setVisible( vis );
604     myInfoLabel->setText( tr( "X_FROM_Y_ITEMS_SHOWN" ).arg(imin).arg(imax).arg(total) );
605     myPrevButton->setEnabled( myPreviewActor->hasPrevious() );
606     myNextButton->setEnabled( myPreviewActor->hasNext() );
607   }
608 }