1 // Copyright (C) 2007-2020 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, or (at your option) any later version.
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 "SMESHGUI_Utils.h"
28 #include "SMESHGUI_VTKUtils.h"
29 #include "SMESH_Actor.h"
30 #include "SMESH_Gen_i.hxx"
31 #include "SMESH_LogicalFilter.hxx"
32 #include "SMESH_PreviewActorsCollection.h"
33 #include "SMESH_Type.h"
35 // SALOME GUI includes
36 #include <LightApp_SelectionMgr.h>
37 #include <SALOME_ListIO.hxx>
38 #include <SUIT_OverrideCursor.h>
39 #include <SUIT_ResourceMgr.h>
40 #include <SUIT_Session.h>
41 #include <SVTK_Selector.h>
42 #include <SVTK_ViewModel.h>
43 #include <SVTK_ViewWindow.h>
47 #include <GEOM_TypeFilter.h>
48 #include <GEOM_CompoundFilter.h>
51 #include <QPushButton>
52 #include <QGridLayout>
53 #include <QListWidget>
58 #include <TColStd_MapOfInteger.hxx>
59 #include <TopoDS_Shape.hxx>
61 #include <TopExp_Explorer.hxx>
67 //================================================================================
71 //================================================================================
73 StdMeshersGUI_SubShapeSelectorWdg
74 ::StdMeshersGUI_SubShapeSelectorWdg( QWidget * parent,
75 TopAbs_ShapeEnum subShType,
76 const bool toShowList,
77 const bool toShowActivateBtn,
78 const int minListWidth ):
83 QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
85 QGridLayout* layout = new QGridLayout( this );
86 layout->setMargin( MARGIN );
87 layout->setSpacing( SPACING );
91 QPixmap iconSelect (SUIT_Session::session()->resourceMgr()->loadPixmap("SMESH", tr("ICON_SELECT")));
92 myListWidget = new QListWidget( this );
93 myActivateButton = new QPushButton( iconSelect, "", 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 myListWidget->setMinimumWidth( minListWidth );
98 myListWidget->setWrapping( true );
99 myActivateButton->setCheckable( true );
104 myActivateButton = 0;
108 myInfoLabel = new QLabel( this );
109 myPrevButton = new QPushButton( "<<", this );
110 myNextButton = new QPushButton( ">>", this );
115 layout->addWidget(myListWidget, row, 0, 3+toShowActivateBtn, 3);
116 if ( toShowActivateBtn )
117 layout->addWidget( myActivateButton, row++, 3 );
119 myActivateButton->hide();
120 layout->addWidget(myAddButton, row, 3);
121 layout->addWidget(myRemoveButton, ++row, 3);
123 layout->addWidget(myInfoLabel, ++row, 0, 1, 3);
124 layout->addWidget(myPrevButton, ++row, 0);
125 layout->addWidget(myNextButton, row, 2);
127 layout->setRowStretch(row-2, 5);
128 layout->setColumnStretch(1, 5);
130 else // show only Prev and Next buttons
132 layout->addWidget(myInfoLabel, 0, 0, 1, 2);
133 layout->addWidget(myPrevButton, 1, 0);
134 layout->addWidget(myNextButton, 1, 1);
136 //myInfoLabel->setMinimumWidth(300);
137 //myInfoLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
138 myInfoLabel->setAlignment(Qt::AlignCenter);
140 mySubShType = subShType;
145 //================================================================================
149 //================================================================================
151 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
153 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
154 if ( myPreviewActor ) {
155 myPreviewActor->RemoveFromRender( myRenderer );
156 aViewWindow->Repaint();
158 delete myPreviewActor;
164 myMainShape.Nullify();
166 if ( mySelectionMgr && myFilter )
167 mySelectionMgr->removeFilter( myFilter );
168 delete myFilter; myFilter=0;
170 mySelectionMgr->clearSelected();
172 SUIT_SelectionFilter* filter;
173 foreach( filter, myGeomFilters )
177 //================================================================================
179 * Create a layout, initialize fields
181 //================================================================================
183 void StdMeshersGUI_SubShapeSelectorWdg::init()
187 mySelectedIDs.clear();
189 mySMESHGUI = SMESHGUI::GetSMESHGUI();
190 mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
191 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
193 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
194 aViewWindow->SetSelectionMode( ActorSelection );
200 myAddButton->setEnabled( false );
201 myRemoveButton->setEnabled( false );
203 connect( myListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
204 connect( myActivateButton, SIGNAL( toggled(bool) ), SLOT( ActivateSelection(bool)));
205 connect( myAddButton, SIGNAL(clicked()), SLOT(onAdd()));
206 connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
208 connect( myPrevButton, SIGNAL(clicked()), SLOT(onPrevious()));
209 connect( myNextButton, SIGNAL(clicked()), SLOT(onNext()));
211 connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(selectionIntoArgument()));
216 //================================================================================
218 * \brief Install filters to select sub-shapes of mySubShType or their groups
220 //================================================================================
222 void StdMeshersGUI_SubShapeSelectorWdg::setFilter()
226 // SalomeApp_Study* study = mySMESHGUI->activeStudy();
227 // GEOM_TypeFilter* typeFilter = new GEOM_TypeFilter(study, mySubShType, /*isShapeType=*/true );
228 // GEOM_CompoundFilter* gpoupFilter = new GEOM_CompoundFilter(study);
229 // gpoupFilter->addSubType( mySubShType );
230 // myGeomFilters.append( typeFilter );
231 // myGeomFilters.append( gpoupFilter );
232 // myFilter = new SMESH_LogicalFilter( myGeomFilters, SMESH_LogicalFilter::LO_OR );
234 // mySelectionMgr->installFilter( myFilter );
237 //================================================================================
239 * Create a layout, initialize fields
241 //================================================================================
243 void StdMeshersGUI_SubShapeSelectorWdg::ShowPreview( bool visible)
245 if ( !myPreviewActor )
248 if ( myIsShown != visible ) {
249 myPreviewActor->SetShown( visible );
251 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
252 aViewWindow->Repaint();
258 //================================================================================
260 * \brief Connect selection slots
261 * \param other - another StdMeshersGUI_ObjectReferenceParamWdg
263 //================================================================================
265 void StdMeshersGUI_SubShapeSelectorWdg::
266 AvoidSimultaneousSelection ( StdMeshersGUI_SubShapeSelectorWdg* other)
268 connect(other, SIGNAL(selectionActivated()), this, SLOT(deactivateSelection()));
269 connect(this, SIGNAL(selectionActivated()), other, SLOT(deactivateSelection()));
272 //================================================================================
274 * \brief Connect/disconnect to change of selection
276 //================================================================================
278 void StdMeshersGUI_SubShapeSelectorWdg::ActivateSelection( bool toActivate )
280 // adjust state of myActivateButton
281 if ( myActivateButton &&
282 myActivateButton != sender() &&
283 myActivateButton->isChecked() != toActivate )
285 myActivateButton->toggle();
289 if ( !mySelectionMgr ) return;
293 connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(selectionIntoArgument()));
297 disconnect(mySelectionMgr, 0, this, 0 );
300 if ( sender() == myActivateButton )
301 ShowPreview( toActivate );
304 emit selectionActivated();
307 //================================================================================
309 * \brief Clears selected IDs. This is a workaround of a bug that
310 * SUIT_SelectionMgr::clearSelected() does not emit currentSelectionChanged
312 //================================================================================
314 void StdMeshersGUI_SubShapeSelectorWdg::ClearSelected()
316 mySelectedIDs.clear();
317 selectionIntoArgument();
320 //=================================================================================
321 // function : selectionIntoArgument()
322 // purpose : Called when selection has changed or in other cases
323 //=================================================================================
324 void StdMeshersGUI_SubShapeSelectorWdg::selectionIntoArgument()
326 if ( !myPreviewActor )
329 mySelectedIDs.clear();
333 mySelectionMgr->selectedObjects( aList );
334 int nbSel = aList.Extent();
337 SALOME_ListIteratorOfListIO anIt (aList);
339 for ( ; anIt.More(); anIt.Next()) // Loop on selected objects
341 Handle(SALOME_InteractiveObject) IO = anIt.Value();
343 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( IO->getEntry() );
344 if ( !CORBA::is_nil( aGeomObj ) ) { // Selected Object From Study
345 // commented for IPAL52836
347 // GEOM::GEOM_Object_var aGeomFatherObj = aGeomObj->GetMainShape();
348 // QString aFatherEntry = "";
349 // QString aMainFatherEntry = "";
350 // TopoDS_Shape shape;
351 // if ( !CORBA::is_nil( aGeomFatherObj ) ) {
353 // GEOM::GEOM_Object_var aGeomMain = GetGeomObjectByEntry( myEntry.c_str() );
354 // if ( !CORBA::is_nil( aGeomMain ) && aGeomMain->GetType() == 37 ) { // Main Shape is a Group
355 // GEOM::GEOM_Object_var aMainFatherObj = aGeomMain->GetMainShape();
356 // if ( !CORBA::is_nil( aMainFatherObj ) )
357 // aMainFatherEntry = aMainFatherObj->GetStudyEntry();
359 // aFatherEntry = aGeomFatherObj->GetStudyEntry();
362 // if (( ! aFatherEntry.isEmpty() ) &&
363 // ( aFatherEntry == myEntry.c_str() || aFatherEntry == aMainFatherEntry ) )
366 if ( aGeomObj->GetType() == 37 /*GEOM_GROUP*/ ) { // Selected Group that belongs the main object
367 GEOMBase::GetShape(aGeomObj, shape);
368 if ( !shape.IsNull() ) {
369 TopExp_Explorer exp( shape, mySubShType );
370 for ( ; exp.More(); exp.Next() ) {
371 int index = myPreviewActor->GetIndexByShape( exp.Current() );
373 mySelectedIDs.append( index );
374 myPreviewActor->HighlightID( index );
378 } else if ( aGeomObj->GetType() == 28 /*GEOM_SUBSHAPE*/ ||
379 myEntry == IO->getEntry() )
381 GEOMBase::GetShape(aGeomObj, shape);
382 if ( !shape.IsNull() && shape.ShapeType() == mySubShType ) {
383 int index = myPreviewActor->GetIndexByShape( shape );
385 mySelectedIDs.append( index );
386 myPreviewActor->HighlightID( index );
391 } else { // Selected Actor from Actor Collection
392 QString anEntry = IO->getEntry();
394 int index = anEntry.lastIndexOf( str );
395 anEntry.remove(0, index+1);
396 int ind = anEntry.toInt();
398 mySelectedIDs.append( ind );
405 myAddButton->setEnabled(( myListWidget->count() < myMaxSize || myMaxSize == -1 ) &&
406 ( mySelectedIDs.size() > 0 ) &&
407 ( mySelectedIDs.size() <= myMaxSize || myMaxSize == -1 ) );
408 myRemoveButton->setEnabled( mySelectedIDs.size() > 0 );
410 //Connect Selected Ids in viewer and dialog's Ids list
411 bool signalsBlocked = myListWidget->blockSignals( true );
412 myListWidget->clearSelection();
413 if ( mySelectedIDs.size() > 0 ) {
414 for (int i = 0; i < mySelectedIDs.size(); i++) {
415 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
416 QList<QListWidgetItem*> anItems = myListWidget->findItems ( anID, Qt::MatchExactly );
417 QListWidgetItem* item;
418 foreach(item, anItems)
419 item->setSelected(true);
422 myListWidget->blockSignals( signalsBlocked );
425 emit shapeSelected();
428 //=================================================================================
429 // function : onAdd()
430 // purpose : Called when Add Button Clicked
431 //=================================================================================
432 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
434 if ( mySelectedIDs.size() < 1 || !myListWidget )
437 myListWidget->blockSignals( true );
438 for (int i = 0; i < mySelectedIDs.size() && (myMaxSize == -1 || myListOfIDs.size() < myMaxSize); i++) {
439 if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
440 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
442 QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
443 anItem->setSelected(true);
445 myListOfIDs.append( mySelectedIDs.at(i) );
448 onListSelectionChanged();
449 myListWidget->blockSignals( false );
451 mySelectedIDs.clear();
452 myAddButton->setEnabled( false );
455 //=================================================================================
456 // function : onRemove()
457 // purpose : Called when Remove Button Clicked
458 //=================================================================================
459 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
461 if ( myListWidget->count() < 1 || !myListWidget )
464 myListWidget->blockSignals( true );
465 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
466 QListWidgetItem* item;
467 foreach(item, selItems) {
468 QString idStr = item->text();
469 int id = idStr.toInt();
471 int index = myListOfIDs.indexOf( id );
472 myListOfIDs.removeAt( index );
476 onListSelectionChanged();
477 myListWidget->blockSignals( false );
479 myAddButton->setEnabled( !mySelectedIDs.isEmpty() );
482 void StdMeshersGUI_SubShapeSelectorWdg::onPrevious()
484 if ( myPreviewActor ) {
485 myPreviewActor->previous();
487 myListWidget->clearSelection();
489 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
490 aViewWindow->Repaint();
494 void StdMeshersGUI_SubShapeSelectorWdg::onNext()
496 if ( myPreviewActor ) {
497 myPreviewActor->next();
499 myListWidget->clearSelection();
501 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
502 aViewWindow->Repaint();
506 //=================================================================================
507 // function : onListSelectionChanged()
508 // purpose : Called when selection in element list is changed
509 //=================================================================================
510 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
512 if ( !myPreviewActor )
515 myPreviewActor->HighlightAll( false );
516 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
517 QListWidgetItem* anItem;
518 foreach(anItem, selItems)
519 myPreviewActor->HighlightID( anItem->text().toInt() );
521 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
522 aViewWindow->Repaint();
524 // update remove button
525 myRemoveButton->setEnabled( selItems.size() > 0 );
527 emit selectionChanged();
530 //=================================================================================
531 // function : setGeomShape
532 // purpose : Called to set geometry whose sub-shapes are selected
533 //================================================================================
534 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry,
535 const QString& theMainShapeEntry )
537 if ( !theEntry.isEmpty() || theMainShapeEntry.isEmpty() )
539 myParamValue = theEntry;
540 myEntry = theEntry.toStdString();
541 myMainEntry = theMainShapeEntry.toStdString();
543 if ( myMainEntry.empty() ) myMainEntry = myEntry;
544 if ( myEntry.empty() ) myEntry = myMainEntry;
545 if ( myMainEntry.length() > myEntry.length() &&
546 theMainShapeEntry.startsWith( theEntry ))
547 std::swap( myMainEntry, myEntry );
549 myGeomShape = GetTopoDSByEntry( myEntry.c_str() );
550 if ( myEntry == myMainEntry )
551 myMainShape = myGeomShape;
553 myMainShape = GetTopoDSByEntry( myMainEntry.c_str() );
558 //=================================================================================
559 // function : updateState
560 // purpose : update Widget state
561 //=================================================================================
562 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
564 bool state = ( !myGeomShape.IsNull() );
566 myInfoLabel ->setVisible( false );
567 myPrevButton->setVisible( false );
568 myNextButton->setVisible( false );
572 myListWidget->setEnabled( state );
573 myAddButton->setEnabled( mySelectedIDs.size() > 0 );
576 SUIT_OverrideCursor wc;
577 if ( !myPreviewActor )
578 myPreviewActor = new SMESH_PreviewActorsCollection();
579 myPreviewActor->SetSelector( mySelector );
580 myPreviewActor->Init( myGeomShape, myMainShape, mySubShType, myEntry.c_str() );
581 myPreviewActor->SetShown( false );
583 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
584 myRenderer = aViewWindow->getRenderer();
585 myPreviewActor->AddToRender( myRenderer );
586 aViewWindow->Repaint();
592 //=================================================================================
593 // function : GetGeomObjectByEntry
594 // purpose : Called to get GeomObject
595 //=================================================================================
596 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
598 GEOM::GEOM_Object_var aGeomObj;
599 SALOMEDS::SObject_var aSObj = SMESH_Gen_i::getStudyServant()->FindObjectID( theEntry.toUtf8().data() );
600 if (!aSObj->_is_nil() )
602 CORBA::Object_var obj = aSObj->GetObject();
603 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
606 return aGeomObj._retn();
609 //=================================================================================
610 // function : setObjectByEntry
611 // purpose : Called to get GeomObject
612 //=================================================================================
613 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
616 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
617 GEOMBase::GetShape(aGeomObj, shape);
621 //=================================================================================
622 // function : GetListOfIds
623 // purpose : Called to get the list of SubShapes IDs
624 //=================================================================================
625 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
627 SMESH::long_array_var anArray = new SMESH::long_array;
629 int size = myListOfIDs.size();
630 anArray->length( size );
631 for (int i = 0; i < size; i++)
632 anArray[i] = myListOfIDs.at(i);
637 //=================================================================================
638 // function : SetListOfIds
639 // purpose : Called to set the list of SubShapes IDs. Returns false if any ID is invalid
640 //=================================================================================
641 bool StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
643 mySelectedIDs.clear();
645 int size = theIds->length();
646 for ( int i = 0; i < size; i++ )
647 mySelectedIDs.append( theIds[ i ] );
651 myListWidget->blockSignals( true );
652 myListWidget->clear();
653 myListWidget->blockSignals( false );
657 if ( myPreviewActor )
659 for ( int i = 0; i < size && isOk; i++ )
660 isOk = myPreviewActor->IsValidIndex( theIds[ i ] );
662 else if ( !myMainShape.IsNull() )
664 TopTools_IndexedMapOfShape aMainMap;
665 TopExp::MapShapes(myMainShape, aMainMap);
666 for ( int i = 0; i < size && isOk; i++ )
667 isOk = ( theIds[ i ] > 0 && theIds[ i ] <= aMainMap.Extent() );
673 //=================================================================================
674 // function : GetMainShapeEntry
675 // purpose : Called to get the Main Object Entry
676 //=================================================================================
677 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
679 if ( myMainEntry.empty() ) myMainEntry = "";
680 return myMainEntry.c_str();
683 void StdMeshersGUI_SubShapeSelectorWdg::updateButtons()
685 if ( myPreviewActor ) {
686 int total = myPreviewActor->count();
687 int chunk = myPreviewActor->currentChunk();
688 int chunkSize = myPreviewActor->chunkSize();
689 int imin = chunk*chunkSize+1;
690 int imax = std::min((chunk+1)*chunkSize, total);
691 bool vis = imax > 0 && total > chunkSize;
692 myInfoLabel->setVisible( vis );
693 myPrevButton->setVisible( vis );
694 myNextButton->setVisible( vis );
695 myInfoLabel->setText( tr( "X_FROM_Y_ITEMS_SHOWN" ).arg(imin).arg(imax).arg(total) );
696 myPrevButton->setEnabled( myPreviewActor->hasPrevious() );
697 myNextButton->setEnabled( myPreviewActor->hasNext() );