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 myInfoLabel = new QLabel( this );
100 myPrevButton = new QPushButton( "<<", this );
101 myNextButton = new QPushButton( ">>", this );
102 myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
104 edgesLayout->addWidget(myListWidget, 0, 0, 3, 3);
105 edgesLayout->addWidget(myAddButton, 0, 3);
106 edgesLayout->addWidget(myRemoveButton, 1, 3);
107 edgesLayout->addWidget(myInfoLabel, 3, 0, 1, 3);
108 edgesLayout->addWidget(myPrevButton, 4, 0);
109 edgesLayout->addWidget(myNextButton, 4, 2);
111 edgesLayout->setRowStretch(2, 5);
112 edgesLayout->setColumnStretch(1, 5);
114 myListWidget->setMinimumWidth(300);
115 myInfoLabel->setMinimumWidth(300);
116 myInfoLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
117 myInfoLabel->setAlignment(Qt::AlignCenter);
119 mySubShType = aSubShType;
124 //================================================================================
128 //================================================================================
130 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
132 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
133 if ( myPreviewActor ) {
134 myPreviewActor->RemoveFromRender( myRenderer );
135 aViewWindow->Repaint();
137 delete myPreviewActor;
143 myMainShape.Nullify();
145 if ( mySelectionMgr && myFilter )
146 mySelectionMgr->removeFilter( myFilter );
147 delete myFilter; myFilter=0;
149 SUIT_SelectionFilter* filter;
150 foreach( filter, myGeomFilters )
154 //================================================================================
156 * Create a layout, initialize fields
158 //================================================================================
160 void StdMeshersGUI_SubShapeSelectorWdg::init()
163 myIsNotCorrected = true; // to dont call the GetCorrectedValue method twice
165 mySelectedIDs.clear();
167 myAddButton->setEnabled( false );
168 myRemoveButton->setEnabled( false );
170 mySMESHGUI = SMESHGUI::GetSMESHGUI();
171 mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
172 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
174 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
175 aViewWindow->SetSelectionMode( ActorSelection );
180 connect( myAddButton, SIGNAL(clicked()), SLOT(onAdd()));
181 connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
182 connect( myPrevButton, SIGNAL(clicked()), SLOT(onPrevious()));
183 connect( myNextButton, SIGNAL(clicked()), SLOT(onNext()));
185 connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
186 connect( myListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
191 //================================================================================
193 * \brief Install filters to select sub-shapes of mySubShType or their groups
195 //================================================================================
197 void StdMeshersGUI_SubShapeSelectorWdg::setFilter()
199 SalomeApp_Study* study = mySMESHGUI->activeStudy();
200 GEOM_TypeFilter* typeFilter = new GEOM_TypeFilter(study, mySubShType, /*isShapeType=*/true );
201 GEOM_CompoundFilter* gpoupFilter = new GEOM_CompoundFilter(study);
202 gpoupFilter->addSubType( mySubShType );
203 myGeomFilters.append( typeFilter );
204 myGeomFilters.append( gpoupFilter );
205 myFilter = new SMESH_LogicalFilter( myGeomFilters, SMESH_LogicalFilter::LO_OR );
206 mySelectionMgr->installFilter( myFilter );
209 //================================================================================
211 * Create a layout, initialize fields
213 //================================================================================
215 void StdMeshersGUI_SubShapeSelectorWdg::showPreview( bool visible)
217 if ( !myPreviewActor )
220 if ( myIsShown != visible ) {
221 myPreviewActor->SetShown( visible );
223 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
224 aViewWindow->Repaint();
230 //=================================================================================
231 // function : SelectionIntoArgument()
232 // purpose : Called when selection as changed or other case
233 //=================================================================================
234 void StdMeshersGUI_SubShapeSelectorWdg::SelectionIntoArgument()
236 if ( !myPreviewActor )
239 mySelectedIDs.clear();
243 mySelectionMgr->selectedObjects( aList );
244 int nbSel = aList.Extent();
247 SALOME_ListIteratorOfListIO anIt (aList);
249 for ( ; anIt.More(); anIt.Next()) { // Loop on selected objects
250 Handle(SALOME_InteractiveObject) IO = anIt.Value();
252 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( IO->getEntry() );
253 if ( !CORBA::is_nil( aGeomObj ) ) { // Selected Object From Study
254 GEOM::GEOM_Object_ptr aGeomFatherObj = aGeomObj->GetMainShape();
255 QString aFatherEntry = "";
256 QString aMainFatherEntry = "";
258 if ( !CORBA::is_nil( aGeomFatherObj ) ) {
260 GEOM::GEOM_Object_var aGeomMain = GetGeomObjectByEntry( myEntry );
261 if ( !CORBA::is_nil( aGeomMain ) && aGeomMain->GetType() == 37 ) { // Main Shape is a Group
262 GEOM::GEOM_Object_ptr aMainFatherObj = aGeomMain->GetMainShape();
263 if ( !CORBA::is_nil( aMainFatherObj ) )
264 aMainFatherEntry = aMainFatherObj->GetStudyEntry();
266 aFatherEntry = aGeomFatherObj->GetStudyEntry();
269 if ( aFatherEntry != "" && ( aFatherEntry == myEntry || aFatherEntry == aMainFatherEntry ) )
271 if ( aGeomObj->GetType() == 37 /*GEOM_GROUP*/ ) { // Selected Group that belongs the main object
272 GEOMBase::GetShape(aGeomObj, shape);
273 if ( !shape.IsNull() ) {
274 TopExp_Explorer exp( shape, mySubShType );
275 for ( ; exp.More(); exp.Next() ) {
276 int index = myPreviewActor->GetIndexByShape( exp.Current() );
278 mySelectedIDs.append( index );
279 myPreviewActor->HighlightID( index );
283 } else if ( aGeomObj->GetType() == 28 /*GEOM_SUBSHAPE*/ ) {
284 GEOMBase::GetShape(aGeomObj, shape);
285 if ( !shape.IsNull() && shape.ShapeType() == mySubShType ) {
286 int index = myPreviewActor->GetIndexByShape( shape );
288 mySelectedIDs.append( index );
289 myPreviewActor->HighlightID( index );
294 } else { // Selected Actor from Actor Collection
295 QString anEntry = IO->getEntry();
297 int index = anEntry.lastIndexOf( str );
298 anEntry.remove(0, index+1);
299 int ind = anEntry.toInt();
301 mySelectedIDs.append( ind );
306 myAddButton->setEnabled( ( myListWidget->count() < myMaxSize || myMaxSize == -1 ) && mySelectedIDs.size() > 0 && ( mySelectedIDs.size() <= myMaxSize || myMaxSize == -1 ) );
308 //Connect Selected Ids in viewer and dialog's Ids list
309 bool signalsBlocked = myListWidget->blockSignals( true );
310 myListWidget->clearSelection();
311 if ( mySelectedIDs.size() > 0 ) {
312 for (int i = 0; i < mySelectedIDs.size(); i++) {
313 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
314 QList<QListWidgetItem*> anItems = myListWidget->findItems ( anID, Qt::MatchExactly );
315 QListWidgetItem* item;
316 foreach(item, anItems)
317 item->setSelected(true);
320 myListWidget->blockSignals( signalsBlocked );
323 //=================================================================================
324 // function : onAdd()
325 // purpose : Called when Add Button Clicked
326 //=================================================================================
327 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
329 if ( mySelectedIDs.size() < 1 )
332 myListWidget->blockSignals( true );
333 for (int i = 0; i < mySelectedIDs.size() && (myMaxSize == -1 || myListOfIDs.size() < myMaxSize); i++) {
334 if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
335 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
337 QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
338 anItem->setSelected(true);
340 myListOfIDs.append( mySelectedIDs.at(i) );
343 onListSelectionChanged();
344 myListWidget->blockSignals( false );
345 myAddButton->setEnabled( myMaxSize == -1 || myListOfIDs.size() < myMaxSize );
348 //=================================================================================
349 // function : onRemove()
350 // purpose : Called when Remove Button Clicked
351 //=================================================================================
352 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
354 if ( myListWidget->count() < 1 )
357 myListWidget->blockSignals( true );
358 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
359 QListWidgetItem* item;
360 foreach(item, selItems) {
361 QString idStr = item->text();
362 int id = idStr.toInt();
364 int index = myListOfIDs.indexOf( id );
365 myListOfIDs.removeAt( index );
369 onListSelectionChanged();
370 myListWidget->blockSignals( false );
372 myAddButton->setEnabled( true );
375 void StdMeshersGUI_SubShapeSelectorWdg::onPrevious()
377 if ( myPreviewActor ) {
378 myPreviewActor->previous();
379 myListWidget->clearSelection();
381 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
382 aViewWindow->Repaint();
386 void StdMeshersGUI_SubShapeSelectorWdg::onNext()
388 if ( myPreviewActor ) {
389 myPreviewActor->next();
390 myListWidget->clearSelection();
392 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
393 aViewWindow->Repaint();
397 //=================================================================================
398 // function : onListSelectionChanged()
399 // purpose : Called when selection in element list is changed
400 //=================================================================================
401 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
403 if ( !myPreviewActor )
406 mySelectionMgr->clearSelected();
407 TColStd_MapOfInteger aIndexes;
408 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
409 QListWidgetItem* anItem;
410 foreach(anItem, selItems)
411 myPreviewActor->HighlightID( anItem->text().toInt() );
413 // update remove button
414 myRemoveButton->setEnabled( selItems.size() > 0 );
417 //=================================================================================
418 // function : setGeomShape
419 // purpose : Called to set geometry whose sub-shapes are selected
420 //================================================================================
421 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
423 if ( theEntry != "") {
424 myParamValue = theEntry;
426 myGeomShape = GetTopoDSByEntry( theEntry );
428 myIsNotCorrected = true;
432 //=================================================================================
433 // function : updateState
434 // purpose : update Widget state
435 //=================================================================================
436 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
439 if ( !myGeomShape.IsNull() )
441 myInfoLabel->setVisible( false );
442 myPrevButton->setVisible( false );
443 myNextButton->setVisible( false );
445 myListWidget->setEnabled( state );
446 myAddButton->setEnabled( mySelectedIDs.size() > 0 );
449 myPreviewActor = new SMESH_PreviewActorsCollection();
450 myPreviewActor->SetSelector( mySelector );
451 myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
452 myPreviewActor->SetShown( false );
454 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
455 myRenderer = aViewWindow->getRenderer();
456 myPreviewActor->AddToRender( myRenderer );
457 aViewWindow->Repaint();
463 //=================================================================================
464 // function : GetGeomObjectByEntry
465 // purpose : Called to get GeomObject
466 //=================================================================================
467 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
469 GEOM::GEOM_Object_var aGeomObj;
470 SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
472 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
473 SALOMEDS::GenericAttribute_var anAttr;
475 if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
476 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
477 CORBA::String_var aVal = anIOR->Value();
478 CORBA::Object_var obj = aStudy->ConvertIORToObject(aVal);
479 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
485 //=================================================================================
486 // function : setObjectByEntry
487 // purpose : Called to get GeomObject
488 //=================================================================================
489 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
492 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
493 GEOMBase::GetShape(aGeomObj, shape);
497 //=================================================================================
498 // function : GetListOfIds
499 // purpose : Called to get the list of SubShapes IDs
500 //=================================================================================
501 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
503 SMESH::long_array_var anArray = new SMESH::long_array;
505 if ( myMainEntry != "" && myIsNotCorrected )
506 myListOfIDs = GetCorrectedListOfIDs( true );
508 int size = myListOfIDs.size();
509 anArray->length( size );
511 for (int i = 0; i < size; i++) {
512 anArray[i] = myListOfIDs.at(i);
518 //=================================================================================
519 // function : SetListOfIds
520 // purpose : Called to set the list of SubShapes IDs
521 //=================================================================================
522 void StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
524 mySelectedIDs.clear();
526 int size = theIds->length();
527 for ( int i = 0; i < size; i++ )
528 mySelectedIDs.append( theIds[ i ] );
530 mySelectedIDs = GetCorrectedListOfIDs( false );
534 //=================================================================================
535 // function : SetMainShapeEntry
536 // purpose : Called to set the Entry of main shape of the mesh
537 //=================================================================================
538 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
540 myMainEntry = theEntry;
541 myMainShape = GetTopoDSByEntry( theEntry );
542 myIsNotCorrected = true;
545 //=================================================================================
546 // function : GetMainShapeEntry
547 // purpose : Called to get the Main Object Entry
548 //=================================================================================
549 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
551 if ( myMainEntry == "")
552 return myEntry.toLatin1().data();
554 return myMainEntry.toLatin1().data();
557 //=================================================================================
558 // function : GetCorrectedListOfIds
559 // purpose : Called to convert the list of IDs from subshape IDs to main shape IDs
560 //=================================================================================
561 QList<int> StdMeshersGUI_SubShapeSelectorWdg::GetCorrectedListOfIDs( bool fromSubshapeToMainshape )
563 if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && fromSubshapeToMainshape )
565 else if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && !fromSubshapeToMainshape )
566 return mySelectedIDs;
569 TopTools_IndexedMapOfShape aGeomMap;
570 TopTools_IndexedMapOfShape aMainMap;
571 TopExp::MapShapes(myGeomShape, aGeomMap);
572 TopExp::MapShapes(myMainShape, aMainMap);
574 if ( fromSubshapeToMainshape ) { // convert indexes from subshape to mainshape
575 int size = myListOfIDs.size();
576 for (int i = 0; i < size; i++) {
577 TopoDS_Shape aSubShape = aGeomMap.FindKey( myListOfIDs.at(i) );
578 int index = aMainMap.FindIndex( aSubShape );
579 aList.append( index );
581 myIsNotCorrected = false;
582 } else { // convert indexes from main shape to subshape
583 int size = mySelectedIDs.size();
584 for (int i = 0; i < size; i++) {
585 TopoDS_Shape aSubShape = aMainMap.FindKey( mySelectedIDs.at(i) );
586 int index = aGeomMap.FindIndex( aSubShape );
587 aList.append( index );
594 void StdMeshersGUI_SubShapeSelectorWdg::updateButtons()
596 if ( myPreviewActor ) {
597 int total = myPreviewActor->count();
598 int chunk = myPreviewActor->currentChunk();
599 int chunkSize = myPreviewActor->chunkSize();
600 int imin = chunk*chunkSize+1;
601 int imax = std::min((chunk+1)*chunkSize, total);
602 bool vis = imax > 0 && total > chunkSize;
603 myInfoLabel->setVisible( vis );
604 myPrevButton->setVisible( vis );
605 myNextButton->setVisible( vis );
606 myInfoLabel->setText( tr( "X_FROM_Y_ITEMS_SHOWN" ).arg(imin).arg(imax).arg(total) );
607 myPrevButton->setEnabled( myPreviewActor->hasPrevious() );
608 myNextButton->setEnabled( myPreviewActor->hasNext() );