1 // Copyright (C) 2007-2016 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 <SVTK_Selector.h>
41 #include <SVTK_ViewModel.h>
42 #include <SVTK_ViewWindow.h>
46 #include <GEOM_TypeFilter.h>
47 #include <GEOM_CompoundFilter.h>
50 #include <QPushButton>
51 #include <QGridLayout>
52 #include <QListWidget>
57 #include <TColStd_MapOfInteger.hxx>
58 #include <TopoDS_Shape.hxx>
60 #include <TopExp_Explorer.hxx>
66 //================================================================================
70 //================================================================================
72 StdMeshersGUI_SubShapeSelectorWdg
73 ::StdMeshersGUI_SubShapeSelectorWdg( QWidget * parent, TopAbs_ShapeEnum aSubShType ):
78 QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
80 QGridLayout* edgesLayout = new QGridLayout( this );
81 edgesLayout->setMargin( MARGIN );
82 edgesLayout->setSpacing( SPACING );
84 myListWidget = new QListWidget( this );
85 myAddButton = new QPushButton( tr( "SMESH_BUT_ADD" ), this );
86 myRemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), this );
87 myInfoLabel = new QLabel( this );
88 myPrevButton = new QPushButton( "<<", this );
89 myNextButton = new QPushButton( ">>", this );
90 myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
92 edgesLayout->addWidget(myListWidget, 0, 0, 3, 3);
93 edgesLayout->addWidget(myAddButton, 0, 3);
94 edgesLayout->addWidget(myRemoveButton, 1, 3);
95 edgesLayout->addWidget(myInfoLabel, 3, 0, 1, 3);
96 edgesLayout->addWidget(myPrevButton, 4, 0);
97 edgesLayout->addWidget(myNextButton, 4, 2);
99 edgesLayout->setRowStretch(2, 5);
100 edgesLayout->setColumnStretch(1, 5);
102 myListWidget->setMinimumWidth(300);
103 myInfoLabel->setMinimumWidth(300);
104 myInfoLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
105 myInfoLabel->setAlignment(Qt::AlignCenter);
107 mySubShType = aSubShType;
112 //================================================================================
116 //================================================================================
118 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
120 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
121 if ( myPreviewActor ) {
122 myPreviewActor->RemoveFromRender( myRenderer );
123 aViewWindow->Repaint();
125 delete myPreviewActor;
131 myMainShape.Nullify();
133 if ( mySelectionMgr && myFilter )
134 mySelectionMgr->removeFilter( myFilter );
135 delete myFilter; myFilter=0;
137 mySelectionMgr->clearSelected();
139 SUIT_SelectionFilter* filter;
140 foreach( filter, myGeomFilters )
144 //================================================================================
146 * Create a layout, initialize fields
148 //================================================================================
150 void StdMeshersGUI_SubShapeSelectorWdg::init()
153 myIsNotCorrected = true; // to dont call the GetCorrectedValue method twice
155 mySelectedIDs.clear();
157 myAddButton->setEnabled( false );
158 myRemoveButton->setEnabled( false );
160 mySMESHGUI = SMESHGUI::GetSMESHGUI();
161 mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
162 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
164 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
165 aViewWindow->SetSelectionMode( ActorSelection );
170 connect( myAddButton, SIGNAL(clicked()), SLOT(onAdd()));
171 connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
172 connect( myPrevButton, SIGNAL(clicked()), SLOT(onPrevious()));
173 connect( myNextButton, SIGNAL(clicked()), SLOT(onNext()));
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 //================================================================================
222 * \brief Clears selected IDs. This is a workaround of a bug that
223 * SUIT_SelectionMgr::clearSelected() does not emit currentSelectionChanged
225 //================================================================================
227 void StdMeshersGUI_SubShapeSelectorWdg::ClearSelected()
229 mySelectedIDs.clear();
230 selectionIntoArgument();
233 //=================================================================================
234 // function : selectionIntoArgument()
235 // purpose : Called when selection as changed or other case
236 //=================================================================================
237 void StdMeshersGUI_SubShapeSelectorWdg::selectionIntoArgument()
239 if ( !myPreviewActor )
242 mySelectedIDs.clear();
246 mySelectionMgr->selectedObjects( aList );
247 int nbSel = aList.Extent();
250 SALOME_ListIteratorOfListIO anIt (aList);
252 for ( ; anIt.More(); anIt.Next()) // Loop on selected objects
254 Handle(SALOME_InteractiveObject) IO = anIt.Value();
256 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( IO->getEntry() );
257 if ( !CORBA::is_nil( aGeomObj ) ) { // Selected Object From Study
258 // commented for IPAL52836
260 // GEOM::GEOM_Object_var aGeomFatherObj = aGeomObj->GetMainShape();
261 // QString aFatherEntry = "";
262 // QString aMainFatherEntry = "";
263 // TopoDS_Shape shape;
264 // if ( !CORBA::is_nil( aGeomFatherObj ) ) {
266 // GEOM::GEOM_Object_var aGeomMain = GetGeomObjectByEntry( myEntry.c_str() );
267 // if ( !CORBA::is_nil( aGeomMain ) && aGeomMain->GetType() == 37 ) { // Main Shape is a Group
268 // GEOM::GEOM_Object_var aMainFatherObj = aGeomMain->GetMainShape();
269 // if ( !CORBA::is_nil( aMainFatherObj ) )
270 // aMainFatherEntry = aMainFatherObj->GetStudyEntry();
272 // aFatherEntry = aGeomFatherObj->GetStudyEntry();
275 // if (( ! aFatherEntry.isEmpty() ) &&
276 // ( aFatherEntry == myEntry.c_str() || aFatherEntry == aMainFatherEntry ) )
279 if ( aGeomObj->GetType() == 37 /*GEOM_GROUP*/ ) { // Selected Group that belongs the main object
280 GEOMBase::GetShape(aGeomObj, shape);
281 if ( !shape.IsNull() ) {
282 TopExp_Explorer exp( shape, mySubShType );
283 for ( ; exp.More(); exp.Next() ) {
284 int index = myPreviewActor->GetIndexByShape( exp.Current() );
286 mySelectedIDs.append( index );
287 myPreviewActor->HighlightID( index );
291 } else if ( aGeomObj->GetType() == 28 /*GEOM_SUBSHAPE*/ ||
292 myEntry == IO->getEntry() )
294 GEOMBase::GetShape(aGeomObj, shape);
295 if ( !shape.IsNull() && shape.ShapeType() == mySubShType ) {
296 int index = myPreviewActor->GetIndexByShape( shape );
298 mySelectedIDs.append( index );
299 myPreviewActor->HighlightID( index );
304 } else { // Selected Actor from Actor Collection
305 QString anEntry = IO->getEntry();
307 int index = anEntry.lastIndexOf( str );
308 anEntry.remove(0, index+1);
309 int ind = anEntry.toInt();
311 mySelectedIDs.append( ind );
316 myAddButton->setEnabled( ( myListWidget->count() < myMaxSize || myMaxSize == -1 ) &&
317 mySelectedIDs.size() > 0 &&
318 ( mySelectedIDs.size() <= myMaxSize || myMaxSize == -1 ) );
320 //Connect Selected Ids in viewer and dialog's Ids list
321 bool signalsBlocked = myListWidget->blockSignals( true );
322 myListWidget->clearSelection();
323 if ( mySelectedIDs.size() > 0 ) {
324 for (int i = 0; i < mySelectedIDs.size(); i++) {
325 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
326 QList<QListWidgetItem*> anItems = myListWidget->findItems ( anID, Qt::MatchExactly );
327 QListWidgetItem* item;
328 foreach(item, anItems)
329 item->setSelected(true);
332 myListWidget->blockSignals( signalsBlocked );
335 //=================================================================================
336 // function : onAdd()
337 // purpose : Called when Add Button Clicked
338 //=================================================================================
339 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
341 if ( mySelectedIDs.size() < 1 )
344 myListWidget->blockSignals( true );
345 for (int i = 0; i < mySelectedIDs.size() && (myMaxSize == -1 || myListOfIDs.size() < myMaxSize); i++) {
346 if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
347 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
349 QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
350 anItem->setSelected(true);
352 myListOfIDs.append( mySelectedIDs.at(i) );
355 onListSelectionChanged();
356 myListWidget->blockSignals( false );
358 mySelectedIDs.clear();
359 myAddButton->setEnabled( false );
362 //=================================================================================
363 // function : onRemove()
364 // purpose : Called when Remove Button Clicked
365 //=================================================================================
366 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
368 if ( myListWidget->count() < 1 )
371 myListWidget->blockSignals( true );
372 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
373 QListWidgetItem* item;
374 foreach(item, selItems) {
375 QString idStr = item->text();
376 int id = idStr.toInt();
378 int index = myListOfIDs.indexOf( id );
379 myListOfIDs.removeAt( index );
383 onListSelectionChanged();
384 myListWidget->blockSignals( false );
386 myAddButton->setEnabled( !mySelectedIDs.isEmpty() );
389 void StdMeshersGUI_SubShapeSelectorWdg::onPrevious()
391 if ( myPreviewActor ) {
392 myPreviewActor->previous();
393 myListWidget->clearSelection();
395 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
396 aViewWindow->Repaint();
400 void StdMeshersGUI_SubShapeSelectorWdg::onNext()
402 if ( myPreviewActor ) {
403 myPreviewActor->next();
404 myListWidget->clearSelection();
406 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
407 aViewWindow->Repaint();
411 //=================================================================================
412 // function : onListSelectionChanged()
413 // purpose : Called when selection in element list is changed
414 //=================================================================================
415 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
417 if ( !myPreviewActor )
420 //mySelectionMgr->clearSelected();
421 myPreviewActor->HighlightAll( false );
422 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
423 QListWidgetItem* anItem;
424 foreach(anItem, selItems)
425 myPreviewActor->HighlightID( anItem->text().toInt() );
427 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
428 aViewWindow->Repaint();
430 // update remove button
431 myRemoveButton->setEnabled( selItems.size() > 0 );
433 emit selectionChanged();
436 //=================================================================================
437 // function : setGeomShape
438 // purpose : Called to set geometry whose sub-shapes are selected
439 //================================================================================
440 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry,
441 const QString& theMainShapeEntry )
443 if ( !theEntry.isEmpty() || theMainShapeEntry.isEmpty() )
445 myParamValue = theEntry;
446 myEntry = theEntry.toStdString();
447 myMainEntry = theMainShapeEntry.toStdString();
449 if ( myMainEntry.empty() ) myMainEntry = myEntry;
450 if ( myEntry.empty() ) myEntry = myMainEntry;
451 if ( myMainEntry.length() > myEntry.length() &&
452 theMainShapeEntry.startsWith( theEntry ))
453 std::swap( myMainEntry, myEntry );
455 myGeomShape = GetTopoDSByEntry( myEntry.c_str() );
456 if ( myEntry == myMainEntry )
457 myMainShape = myGeomShape;
459 myMainShape = GetTopoDSByEntry( myMainEntry.c_str() );
461 myIsNotCorrected = true;
465 //=================================================================================
466 // function : updateState
467 // purpose : update Widget state
468 //=================================================================================
469 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
472 if ( !myGeomShape.IsNull() )
474 myInfoLabel->setVisible( false );
475 myPrevButton->setVisible( false );
476 myNextButton->setVisible( false );
478 myListWidget->setEnabled( state );
479 myAddButton->setEnabled( mySelectedIDs.size() > 0 );
482 SUIT_OverrideCursor wc;
483 myPreviewActor = new SMESH_PreviewActorsCollection();
484 myPreviewActor->SetSelector( mySelector );
485 myPreviewActor->Init( myGeomShape, myMainShape, mySubShType, myEntry.c_str() );
486 myPreviewActor->SetShown( false );
488 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
489 myRenderer = aViewWindow->getRenderer();
490 myPreviewActor->AddToRender( myRenderer );
491 aViewWindow->Repaint();
497 //=================================================================================
498 // function : GetGeomObjectByEntry
499 // purpose : Called to get GeomObject
500 //=================================================================================
501 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
503 GEOM::GEOM_Object_var aGeomObj;
504 SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
505 if ( !aStudy->_is_nil() )
507 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
508 if (!aSObj->_is_nil() )
510 CORBA::Object_var obj = aSObj->GetObject();
511 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
515 return aGeomObj._retn();
518 //=================================================================================
519 // function : setObjectByEntry
520 // purpose : Called to get GeomObject
521 //=================================================================================
522 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
525 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
526 GEOMBase::GetShape(aGeomObj, shape);
530 //=================================================================================
531 // function : GetListOfIds
532 // purpose : Called to get the list of SubShapes IDs
533 //=================================================================================
534 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
536 SMESH::long_array_var anArray = new SMESH::long_array;
538 int size = myListOfIDs.size();
539 anArray->length( size );
540 for (int i = 0; i < size; i++)
541 anArray[i] = myListOfIDs.at(i);
546 //=================================================================================
547 // function : SetListOfIds
548 // purpose : Called to set the list of SubShapes IDs. Returns false if any ID is invalid
549 //=================================================================================
550 bool StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
552 mySelectedIDs.clear();
554 int size = theIds->length();
555 for ( int i = 0; i < size; i++ )
556 mySelectedIDs.append( theIds[ i ] );
558 myListWidget->blockSignals( true );
559 myListWidget->clear();
560 myListWidget->blockSignals( false );
563 if ( myPreviewActor )
565 for ( int i = 0; i < size && isOk; i++ )
566 isOk = myPreviewActor->IsValidIndex( theIds[ i ] );
568 else if ( !myMainShape.IsNull() )
570 TopTools_IndexedMapOfShape aMainMap;
571 TopExp::MapShapes(myMainShape, aMainMap);
572 for ( int i = 0; i < size && isOk; i++ )
573 isOk = ( theIds[ i ] > 0 && theIds[ i ] <= aMainMap.Extent() );
575 // mySelectedIDs = GetCorrectedListOfIDs( false, &isOk );
580 //=================================================================================
581 // function : GetMainShapeEntry
582 // purpose : Called to get the Main Object Entry
583 //=================================================================================
584 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
586 if ( myMainEntry.empty() ) myMainEntry = "";
587 return myMainEntry.c_str();
590 void StdMeshersGUI_SubShapeSelectorWdg::updateButtons()
592 if ( myPreviewActor ) {
593 int total = myPreviewActor->count();
594 int chunk = myPreviewActor->currentChunk();
595 int chunkSize = myPreviewActor->chunkSize();
596 int imin = chunk*chunkSize+1;
597 int imax = std::min((chunk+1)*chunkSize, total);
598 bool vis = imax > 0 && total > chunkSize;
599 myInfoLabel->setVisible( vis );
600 myPrevButton->setVisible( vis );
601 myNextButton->setVisible( vis );
602 myInfoLabel->setText( tr( "X_FROM_Y_ITEMS_SHOWN" ).arg(imin).arg(imax).arg(total) );
603 myPrevButton->setEnabled( myPreviewActor->hasPrevious() );
604 myNextButton->setEnabled( myPreviewActor->hasNext() );