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