1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : StdMeshersGUI_SubShapeSelectorWdg.cxx
21 // Author : Open CASCADE S.A.S. (dmv)
24 #include "StdMeshersGUI_SubShapeSelectorWdg.h"
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"
38 #include <SVTK_ViewWindow.h>
39 #include <SVTK_ViewModel.h>
40 #include <SVTK_ViewWindow.h>
41 #include <SVTK_Selector.h>
43 // SALOME GUI includes
44 #include <SALOME_ListIO.hxx>
45 #include <LightApp_SelectionMgr.h>
46 #include <SALOME_ListIteratorOfListIO.hxx>
49 #include <SUIT_ResourceMgr.h>
53 #include <GEOM_TypeFilter.h>
54 #include <GEOM_CompoundFilter.h>
57 #include <QPushButton>
58 #include <QGridLayout>
59 #include <QListWidget>
64 #include <TColStd_MapOfInteger.hxx>
65 #include <TColStd_IndexedMapOfInteger.hxx>
66 #include <TopoDS_Shape.hxx>
68 #include <TopExp_Explorer.hxx>
69 #include <StdSelect_TypeOfEdge.hxx>
71 // SALOME KERNEL includes
72 #include <SALOMEDS_SObject.hxx>
78 //================================================================================
82 //================================================================================
84 StdMeshersGUI_SubShapeSelectorWdg
85 ::StdMeshersGUI_SubShapeSelectorWdg( QWidget * parent, TopAbs_ShapeEnum aSubShType ):
90 QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
92 QGridLayout* edgesLayout = new QGridLayout( this );
93 edgesLayout->setMargin( MARGIN );
94 edgesLayout->setSpacing( SPACING );
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 );
101 edgesLayout->addWidget(myListWidget, 0, 0, 3, 3);
102 edgesLayout->addWidget(myAddButton, 0, 4);
103 edgesLayout->addWidget(myRemoveButton, 1, 4);
105 //edgesLayout->setRowStretch(2, 5);
106 edgesLayout->setColumnStretch(2, 5);
108 setLayout( edgesLayout );
109 setMinimumWidth( 300 );
111 mySubShType = aSubShType;
116 //================================================================================
120 //================================================================================
122 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
124 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
125 if ( myPreviewActor ) {
126 myPreviewActor->RemoveFromRender( myRenderer );
127 aViewWindow->Repaint();
129 delete myPreviewActor;
135 myMainShape.Nullify();
137 if ( mySelectionMgr && myFilter )
138 mySelectionMgr->removeFilter( myFilter );
139 delete myFilter; myFilter=0;
141 SUIT_SelectionFilter* filter;
142 foreach( filter, myGeomFilters )
146 //================================================================================
148 * Create a layout, initialize fields
150 //================================================================================
152 void StdMeshersGUI_SubShapeSelectorWdg::init()
155 myIsNotCorrected = true; // to dont call the GetCorrectedValue method twice
157 mySelectedIDs.clear();
159 myAddButton->setEnabled( false );
160 myRemoveButton->setEnabled( false );
162 mySMESHGUI = SMESHGUI::GetSMESHGUI();
163 mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
164 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
166 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
167 aViewWindow->SetSelectionMode( ActorSelection );
172 connect( myAddButton, SIGNAL(clicked()), SLOT(onAdd()));
173 connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
175 connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
176 connect( myListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
181 //================================================================================
183 * \brief Install filters to select sub-shapes of mySubShType or their groups
185 //================================================================================
187 void StdMeshersGUI_SubShapeSelectorWdg::setFilter()
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 );
199 //================================================================================
201 * Create a layout, initialize fields
203 //================================================================================
205 void StdMeshersGUI_SubShapeSelectorWdg::showPreview( bool visible)
207 if ( !myPreviewActor )
210 if ( myIsShown != visible ) {
211 myPreviewActor->SetShown( visible );
213 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
214 aViewWindow->Repaint();
220 //=================================================================================
221 // function : SelectionIntoArgument()
222 // purpose : Called when selection as changed or other case
223 //=================================================================================
224 void StdMeshersGUI_SubShapeSelectorWdg::SelectionIntoArgument()
226 if ( !myPreviewActor )
229 mySelectedIDs.clear();
233 mySelectionMgr->selectedObjects( aList );
234 int nbSel = aList.Extent();
237 SALOME_ListIteratorOfListIO anIt (aList);
239 for ( ; anIt.More(); anIt.Next()) { // Loop on selected objects
240 Handle(SALOME_InteractiveObject) IO = anIt.Value();
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 = "";
248 if ( !CORBA::is_nil( aGeomFatherObj ) ) {
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();
256 aFatherEntry = aGeomFatherObj->GetStudyEntry();
259 if ( aFatherEntry != "" && ( aFatherEntry == myEntry || aFatherEntry == aMainFatherEntry ) )
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() );
268 mySelectedIDs.append( index );
269 myPreviewActor->HighlightID( index );
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 );
278 mySelectedIDs.append( index );
279 myPreviewActor->HighlightID( index );
284 } else { // Selected Actor from Actor Collection
285 QString anEntry = IO->getEntry();
287 int index = anEntry.lastIndexOf( str );
288 anEntry.remove(0, index+1);
289 int ind = anEntry.toInt();
291 mySelectedIDs.append( ind );
296 myAddButton->setEnabled( ( myListWidget->count() < myMaxSize || myMaxSize == -1 ) && mySelectedIDs.size() > 0 && ( mySelectedIDs.size() <= myMaxSize || myMaxSize == -1 ) );
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);
311 //=================================================================================
312 // function : onAdd()
313 // purpose : Called when Add Button Clicked
314 //=================================================================================
315 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
317 if ( mySelectedIDs.size() < 1 )
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) );
325 QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
326 anItem->setSelected(true);
328 myListOfIDs.append( mySelectedIDs.at(i) );
331 onListSelectionChanged();
332 myListWidget->blockSignals( false );
333 myAddButton->setEnabled( myMaxSize == -1 || myListOfIDs.size() < myMaxSize );
336 //=================================================================================
337 // function : onRemove()
338 // purpose : Called when Remove Button Clicked
339 //=================================================================================
340 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
342 if ( myListWidget->count() < 1 )
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();
352 int index = myListOfIDs.indexOf( id );
353 myListOfIDs.removeAt( index );
357 onListSelectionChanged();
358 myListWidget->blockSignals( false );
360 myAddButton->setEnabled( true );
363 //=================================================================================
364 // function : onListSelectionChanged()
365 // purpose : Called when selection in element list is changed
366 //=================================================================================
367 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
369 if ( !myPreviewActor )
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() );
379 // update remove button
380 myRemoveButton->setEnabled( selItems.size() > 0 );
383 //=================================================================================
384 // function : setGeomShape
385 // purpose : Called to set geometry whose sub-shapes are selected
386 //================================================================================
387 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
389 if ( theEntry != "") {
390 myParamValue = theEntry;
392 myGeomShape = GetTopoDSByEntry( theEntry );
394 myIsNotCorrected = true;
398 //=================================================================================
399 // function : updateState
400 // purpose : update Widget state
401 //=================================================================================
402 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
405 if ( !myGeomShape.IsNull() )
408 myListWidget->setEnabled( state );
409 myAddButton->setEnabled( mySelectedIDs.size() > 0 );
412 myPreviewActor = new SMESH_PreviewActorsCollection();
413 myPreviewActor->SetSelector( mySelector );
414 myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
415 myPreviewActor->SetShown( false );
417 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
418 myRenderer = aViewWindow->getRenderer();
419 myPreviewActor->AddToRender( myRenderer );
420 aViewWindow->Repaint();
425 //=================================================================================
426 // function : GetGeomObjectByEntry
427 // purpose : Called to get GeomObject
428 //=================================================================================
429 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
431 GEOM::GEOM_Object_var aGeomObj;
432 SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
434 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
435 SALOMEDS::GenericAttribute_var anAttr;
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);
447 //=================================================================================
448 // function : setObjectByEntry
449 // purpose : Called to get GeomObject
450 //=================================================================================
451 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
454 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
455 GEOMBase::GetShape(aGeomObj, shape);
459 //=================================================================================
460 // function : GetListOfIds
461 // purpose : Called to get the list of SubShapes IDs
462 //=================================================================================
463 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
465 SMESH::long_array_var anArray = new SMESH::long_array;
467 if ( myMainEntry != "" && myIsNotCorrected )
468 myListOfIDs = GetCorrectedListOfIDs( true );
470 int size = myListOfIDs.size();
471 anArray->length( size );
473 for (int i = 0; i < size; i++) {
474 anArray[i] = myListOfIDs.at(i);
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)
486 mySelectedIDs.clear();
488 int size = theIds->length();
489 for ( int i = 0; i < size; i++ )
490 mySelectedIDs.append( theIds[ i ] );
492 mySelectedIDs = GetCorrectedListOfIDs( false );
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 )
502 myMainEntry = theEntry;
503 myMainShape = GetTopoDSByEntry( theEntry );
504 myIsNotCorrected = true;
507 //=================================================================================
508 // function : GetMainShapeEntry
509 // purpose : Called to get the Main Object Entry
510 //=================================================================================
511 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
513 if ( myMainEntry == "")
514 return myEntry.toLatin1().data();
516 return myMainEntry.toLatin1().data();
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 )
525 if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && fromSubshapeToMainshape )
527 else if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && !fromSubshapeToMainshape )
528 return mySelectedIDs;
531 TopTools_IndexedMapOfShape aGeomMap;
532 TopTools_IndexedMapOfShape aMainMap;
533 TopExp::MapShapes(myGeomShape, aGeomMap);
534 TopExp::MapShapes(myMainShape, aMainMap);
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 );
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 );