Salome HOME
0020082: EDF 869 GEOM : Edges Orientation indicator/reverse
[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 {
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   myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
97
98   edgesLayout->addWidget(myListWidget,   0, 0, 3, 3);
99   edgesLayout->addWidget(myAddButton,    0, 4);
100   edgesLayout->addWidget(myRemoveButton, 1, 4);
101
102   edgesLayout->setRowStretch(2, 5);
103   edgesLayout->setColumnStretch(2, 5);
104
105   setLayout( edgesLayout );
106   setMinimumWidth( 300 );
107
108   myMaxSize = 1000;
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(); 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
285   myListWidget->blockSignals( false );
286
287   if( myListOfIDs.size() >= myMaxSize )
288     myAddButton->setEnabled( false );
289 }
290          
291 //=================================================================================
292 // function : onRemove()
293 // purpose  : Called when Remove Button Clicked
294 //=================================================================================
295 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
296 {
297   if ( myListWidget->count() < 1 )
298     return;
299
300   myListWidget->blockSignals( true );
301   QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
302   QListWidgetItem* item;
303   foreach(item, selItems) {
304     QString idStr = item->text();
305     int id = idStr.toInt();
306
307     int index = myListOfIDs.indexOf( id );
308     myListOfIDs.removeAt( index );
309     delete item;
310   }
311
312   onListSelectionChanged();
313   myListWidget->blockSignals( false );
314   
315   myAddButton->setEnabled( true );
316 }
317
318 //=================================================================================
319 // function : onListSelectionChanged()
320 // purpose  : Called when selection in element list is changed
321 //=================================================================================
322 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
323 {
324   if ( !myPreviewActor )
325     return;
326
327   mySelectionMgr->clearSelected();
328   TColStd_MapOfInteger aIndexes;
329   QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
330   QListWidgetItem* anItem;
331   foreach(anItem, selItems)
332     myPreviewActor->HighlightID( anItem->text().toInt() );
333 }
334
335 //=================================================================================
336 // function : setGeomShape
337 // purpose  : Called to set geometry
338 //================================================================================
339 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
340 {
341   if ( theEntry != "") {
342     myParamValue = theEntry;
343     myEntry = theEntry;
344     myGeomShape = GetTopoDSByEntry( theEntry );
345     updateState();
346     myIsNotCorrected = true;
347   }
348 }
349
350 //=================================================================================
351 // function : updateState
352 // purpose  : update Widget state
353 //=================================================================================
354 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
355 {
356   bool state = false;
357   if ( !myGeomShape.IsNull() )
358     state = true;
359   
360   myListWidget->setEnabled( state );
361   myAddButton->setEnabled( state );
362   myRemoveButton->setEnabled( state );
363   
364   if (state) {
365     myPreviewActor = new SMESH_PreviewActorsCollection();
366     myPreviewActor->SetSelector( mySelector );
367     myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
368     myPreviewActor->SetShown( false );
369     myIsShown = false;
370     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
371       myRenderer = aViewWindow->getRenderer();
372       myPreviewActor->AddToRender( myRenderer );
373       aViewWindow->Repaint();
374     }
375   }
376 }
377
378 //=================================================================================
379 // function : GetGeomObjectByEntry
380 // purpose  : Called to get GeomObject
381 //=================================================================================
382 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
383 {
384   GEOM::GEOM_Object_var aGeomObj;
385   SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
386   if (aStudy != 0) {
387     SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
388     SALOMEDS::GenericAttribute_var anAttr;
389
390     if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
391       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
392       CORBA::String_var aVal = anIOR->Value();
393       CORBA::Object_var obj = aStudy->ConvertIORToObject(aVal);
394       aGeomObj = GEOM::GEOM_Object::_narrow(obj);
395     }
396   }
397   return aGeomObj;
398 }
399
400 //=================================================================================
401 // function : setObjectByEntry
402 // purpose  : Called to get GeomObject
403 //=================================================================================
404 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
405 {
406   TopoDS_Shape shape;
407   GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
408   GEOMBase::GetShape(aGeomObj, shape);
409   return shape;
410 }
411
412 //=================================================================================
413 // function : GetListOfIds
414 // purpose  : Called to get the list of SubShapes IDs
415 //=================================================================================
416 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
417 {
418   SMESH::long_array_var anArray = new SMESH::long_array;
419
420   if ( myMainEntry != "" && myIsNotCorrected )
421     myListOfIDs = GetCorrectedListOfIDs( true );
422
423   int size = myListOfIDs.size();
424   anArray->length( size );
425   if ( size ) {
426     for (int i = 0; i < size; i++) {
427         anArray[i] = myListOfIDs.at(i);
428     }
429   }
430   return anArray;
431 }
432
433 //=================================================================================
434 // function : SetListOfIds
435 // purpose  : Called to set the list of SubShapes IDs
436 //=================================================================================
437 void StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
438 {
439   mySelectedIDs.clear();
440   myListOfIDs.clear();
441   int size = theIds->length();
442   for ( int i = 0; i < size; i++ )
443     mySelectedIDs.append( theIds[ i ] );
444
445   mySelectedIDs = GetCorrectedListOfIDs( false );
446   onAdd();
447 }
448
449 //=================================================================================
450 // function : SetMainShapeEntry
451 // purpose  : Called to set the Main Object Entry
452 //=================================================================================
453 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
454 {
455   myMainEntry = theEntry;
456   myMainShape = GetTopoDSByEntry( theEntry );
457   myIsNotCorrected = true;
458 }
459
460 //=================================================================================
461 // function : GetMainShapeEntry
462 // purpose  : Called to get the Main Object Entry
463 //=================================================================================
464 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
465 {
466   if ( myMainEntry == "")
467     return myEntry.toLatin1().data();
468
469   return myMainEntry.toLatin1().data();
470 }
471
472 //=================================================================================
473 // function : GetCorrectedListOfIds
474 // purpose  : Called to convert the list of IDs from subshape IDs to main shape IDs
475 //=================================================================================
476 QList<int> StdMeshersGUI_SubShapeSelectorWdg::GetCorrectedListOfIDs( bool fromSubshapeToMainshape )
477 {
478   if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) &&  fromSubshapeToMainshape )
479     return myListOfIDs;
480   else   if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) &&  !fromSubshapeToMainshape )
481     return mySelectedIDs;
482
483   QList<int> aList;
484   TopTools_IndexedMapOfShape   aGeomMap;
485   TopTools_IndexedMapOfShape   aMainMap;
486   TopExp::MapShapes(myGeomShape, aGeomMap);
487   TopExp::MapShapes(myMainShape, aMainMap);
488
489   if ( fromSubshapeToMainshape ) { // convert indexes from subshape to mainshape
490     int size = myListOfIDs.size();
491     for (int i = 0; i < size; i++) {
492       TopoDS_Shape aSubShape = aGeomMap.FindKey( myListOfIDs.at(i) );
493       int index = aMainMap.FindIndex( aSubShape );
494       aList.append( index );
495     }
496     myIsNotCorrected = false;
497   } else { // convert indexes from main shape to subshape
498     int size = mySelectedIDs.size();
499     for (int i = 0; i < size; i++) {
500       TopoDS_Shape aSubShape = aMainMap.FindKey( mySelectedIDs.at(i) );
501       int index = aGeomMap.FindIndex( aSubShape );
502       aList.append( index );
503     }
504   }
505
506   return aList;
507 }