1 // Copyright (C) 2007-2013 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>
75 //================================================================================
79 //================================================================================
81 StdMeshersGUI_SubShapeSelectorWdg
82 ::StdMeshersGUI_SubShapeSelectorWdg( QWidget * parent, TopAbs_ShapeEnum aSubShType ):
87 QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
89 QGridLayout* edgesLayout = new QGridLayout( this );
90 edgesLayout->setMargin( MARGIN );
91 edgesLayout->setSpacing( SPACING );
93 myListWidget = new QListWidget( this );
94 myAddButton = new QPushButton( tr( "SMESH_BUT_ADD" ), this );
95 myRemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), this );
96 myInfoLabel = new QLabel( this );
97 myPrevButton = new QPushButton( "<<", this );
98 myNextButton = new QPushButton( ">>", this );
99 myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
101 edgesLayout->addWidget(myListWidget, 0, 0, 3, 3);
102 edgesLayout->addWidget(myAddButton, 0, 3);
103 edgesLayout->addWidget(myRemoveButton, 1, 3);
104 edgesLayout->addWidget(myInfoLabel, 3, 0, 1, 3);
105 edgesLayout->addWidget(myPrevButton, 4, 0);
106 edgesLayout->addWidget(myNextButton, 4, 2);
108 edgesLayout->setRowStretch(2, 5);
109 edgesLayout->setColumnStretch(1, 5);
111 myListWidget->setMinimumWidth(300);
112 myInfoLabel->setMinimumWidth(300);
113 myInfoLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
114 myInfoLabel->setAlignment(Qt::AlignCenter);
116 mySubShType = aSubShType;
121 //================================================================================
125 //================================================================================
127 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
129 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
130 if ( myPreviewActor ) {
131 myPreviewActor->RemoveFromRender( myRenderer );
132 aViewWindow->Repaint();
134 delete myPreviewActor;
140 myMainShape.Nullify();
142 if ( mySelectionMgr && myFilter )
143 mySelectionMgr->removeFilter( myFilter );
144 delete myFilter; myFilter=0;
146 SUIT_SelectionFilter* filter;
147 foreach( filter, myGeomFilters )
151 //================================================================================
153 * Create a layout, initialize fields
155 //================================================================================
157 void StdMeshersGUI_SubShapeSelectorWdg::init()
160 myIsNotCorrected = true; // to dont call the GetCorrectedValue method twice
162 mySelectedIDs.clear();
164 myAddButton->setEnabled( false );
165 myRemoveButton->setEnabled( false );
167 mySMESHGUI = SMESHGUI::GetSMESHGUI();
168 mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
169 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
171 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
172 aViewWindow->SetSelectionMode( ActorSelection );
177 connect( myAddButton, SIGNAL(clicked()), SLOT(onAdd()));
178 connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
179 connect( myPrevButton, SIGNAL(clicked()), SLOT(onPrevious()));
180 connect( myNextButton, SIGNAL(clicked()), SLOT(onNext()));
182 connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
183 connect( myListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
188 //================================================================================
190 * \brief Install filters to select sub-shapes of mySubShType or their groups
192 //================================================================================
194 void StdMeshersGUI_SubShapeSelectorWdg::setFilter()
196 SalomeApp_Study* study = mySMESHGUI->activeStudy();
197 GEOM_TypeFilter* typeFilter = new GEOM_TypeFilter(study, mySubShType, /*isShapeType=*/true );
198 GEOM_CompoundFilter* gpoupFilter = new GEOM_CompoundFilter(study);
199 gpoupFilter->addSubType( mySubShType );
200 myGeomFilters.append( typeFilter );
201 myGeomFilters.append( gpoupFilter );
202 myFilter = new SMESH_LogicalFilter( myGeomFilters, SMESH_LogicalFilter::LO_OR );
203 mySelectionMgr->installFilter( myFilter );
206 //================================================================================
208 * Create a layout, initialize fields
210 //================================================================================
212 void StdMeshersGUI_SubShapeSelectorWdg::showPreview( bool visible)
214 if ( !myPreviewActor )
217 if ( myIsShown != visible ) {
218 myPreviewActor->SetShown( visible );
220 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
221 aViewWindow->Repaint();
227 //=================================================================================
228 // function : SelectionIntoArgument()
229 // purpose : Called when selection as changed or other case
230 //=================================================================================
231 void StdMeshersGUI_SubShapeSelectorWdg::SelectionIntoArgument()
233 if ( !myPreviewActor )
236 mySelectedIDs.clear();
240 mySelectionMgr->selectedObjects( aList );
241 int nbSel = aList.Extent();
244 SALOME_ListIteratorOfListIO anIt (aList);
246 for ( ; anIt.More(); anIt.Next()) { // Loop on selected objects
247 Handle(SALOME_InteractiveObject) IO = anIt.Value();
249 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( IO->getEntry() );
250 if ( !CORBA::is_nil( aGeomObj ) ) { // Selected Object From Study
251 GEOM::GEOM_Object_var aGeomFatherObj = aGeomObj->GetMainShape();
252 QString aFatherEntry = "";
253 QString aMainFatherEntry = "";
255 if ( !CORBA::is_nil( aGeomFatherObj ) ) {
257 GEOM::GEOM_Object_var aGeomMain = GetGeomObjectByEntry( myEntry );
258 if ( !CORBA::is_nil( aGeomMain ) && aGeomMain->GetType() == 37 ) { // Main Shape is a Group
259 GEOM::GEOM_Object_var aMainFatherObj = aGeomMain->GetMainShape();
260 if ( !CORBA::is_nil( aMainFatherObj ) )
261 aMainFatherEntry = aMainFatherObj->GetStudyEntry();
263 aFatherEntry = aGeomFatherObj->GetStudyEntry();
266 if ( aFatherEntry != "" && ( aFatherEntry == myEntry || aFatherEntry == aMainFatherEntry ) )
268 if ( aGeomObj->GetType() == 37 /*GEOM_GROUP*/ ) { // Selected Group that belongs the main object
269 GEOMBase::GetShape(aGeomObj, shape);
270 if ( !shape.IsNull() ) {
271 TopExp_Explorer exp( shape, mySubShType );
272 for ( ; exp.More(); exp.Next() ) {
273 int index = myPreviewActor->GetIndexByShape( exp.Current() );
275 mySelectedIDs.append( index );
276 myPreviewActor->HighlightID( index );
280 } else if ( aGeomObj->GetType() == 28 /*GEOM_SUBSHAPE*/ ) {
281 GEOMBase::GetShape(aGeomObj, shape);
282 if ( !shape.IsNull() && shape.ShapeType() == mySubShType ) {
283 int index = myPreviewActor->GetIndexByShape( shape );
285 mySelectedIDs.append( index );
286 myPreviewActor->HighlightID( index );
291 } else { // Selected Actor from Actor Collection
292 QString anEntry = IO->getEntry();
294 int index = anEntry.lastIndexOf( str );
295 anEntry.remove(0, index+1);
296 int ind = anEntry.toInt();
298 mySelectedIDs.append( ind );
303 myAddButton->setEnabled( ( myListWidget->count() < myMaxSize || myMaxSize == -1 ) &&
304 mySelectedIDs.size() > 0 &&
305 ( mySelectedIDs.size() <= myMaxSize || myMaxSize == -1 ) );
307 //Connect Selected Ids in viewer and dialog's Ids list
308 bool signalsBlocked = myListWidget->blockSignals( true );
309 myListWidget->clearSelection();
310 if ( mySelectedIDs.size() > 0 ) {
311 for (int i = 0; i < mySelectedIDs.size(); i++) {
312 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
313 QList<QListWidgetItem*> anItems = myListWidget->findItems ( anID, Qt::MatchExactly );
314 QListWidgetItem* item;
315 foreach(item, anItems)
316 item->setSelected(true);
319 myListWidget->blockSignals( signalsBlocked );
322 //=================================================================================
323 // function : onAdd()
324 // purpose : Called when Add Button Clicked
325 //=================================================================================
326 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
328 if ( mySelectedIDs.size() < 1 )
331 myListWidget->blockSignals( true );
332 for (int i = 0; i < mySelectedIDs.size() && (myMaxSize == -1 || myListOfIDs.size() < myMaxSize); i++) {
333 if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
334 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
336 QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
337 anItem->setSelected(true);
339 myListOfIDs.append( mySelectedIDs.at(i) );
342 onListSelectionChanged();
343 myListWidget->blockSignals( false );
344 myAddButton->setEnabled( myMaxSize == -1 || myListOfIDs.size() < myMaxSize );
347 //=================================================================================
348 // function : onRemove()
349 // purpose : Called when Remove Button Clicked
350 //=================================================================================
351 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
353 if ( myListWidget->count() < 1 )
356 myListWidget->blockSignals( true );
357 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
358 QListWidgetItem* item;
359 foreach(item, selItems) {
360 QString idStr = item->text();
361 int id = idStr.toInt();
363 int index = myListOfIDs.indexOf( id );
364 myListOfIDs.removeAt( index );
368 onListSelectionChanged();
369 myListWidget->blockSignals( false );
371 myAddButton->setEnabled( true );
374 void StdMeshersGUI_SubShapeSelectorWdg::onPrevious()
376 if ( myPreviewActor ) {
377 myPreviewActor->previous();
378 myListWidget->clearSelection();
380 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
381 aViewWindow->Repaint();
385 void StdMeshersGUI_SubShapeSelectorWdg::onNext()
387 if ( myPreviewActor ) {
388 myPreviewActor->next();
389 myListWidget->clearSelection();
391 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
392 aViewWindow->Repaint();
396 //=================================================================================
397 // function : onListSelectionChanged()
398 // purpose : Called when selection in element list is changed
399 //=================================================================================
400 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
402 if ( !myPreviewActor )
405 mySelectionMgr->clearSelected();
406 TColStd_MapOfInteger aIndexes;
407 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
408 QListWidgetItem* anItem;
409 foreach(anItem, selItems)
410 myPreviewActor->HighlightID( anItem->text().toInt() );
412 // update remove button
413 myRemoveButton->setEnabled( selItems.size() > 0 );
416 //=================================================================================
417 // function : setGeomShape
418 // purpose : Called to set geometry whose sub-shapes are selected
419 //================================================================================
420 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
422 if ( theEntry != "") {
423 myParamValue = theEntry;
425 myGeomShape = GetTopoDSByEntry( theEntry );
427 myIsNotCorrected = true;
431 //=================================================================================
432 // function : updateState
433 // purpose : update Widget state
434 //=================================================================================
435 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
438 if ( !myGeomShape.IsNull() )
440 myInfoLabel->setVisible( false );
441 myPrevButton->setVisible( false );
442 myNextButton->setVisible( false );
444 myListWidget->setEnabled( state );
445 myAddButton->setEnabled( mySelectedIDs.size() > 0 );
448 myPreviewActor = new SMESH_PreviewActorsCollection();
449 myPreviewActor->SetSelector( mySelector );
450 myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
451 myPreviewActor->SetShown( false );
453 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
454 myRenderer = aViewWindow->getRenderer();
455 myPreviewActor->AddToRender( myRenderer );
456 aViewWindow->Repaint();
462 //=================================================================================
463 // function : GetGeomObjectByEntry
464 // purpose : Called to get GeomObject
465 //=================================================================================
466 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
468 GEOM::GEOM_Object_var aGeomObj;
469 SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
470 if ( !aStudy->_is_nil() )
472 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
473 if (!aSObj->_is_nil() )
475 CORBA::Object_var obj = aSObj->GetObject();
476 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
480 return aGeomObj._retn();
483 //=================================================================================
484 // function : setObjectByEntry
485 // purpose : Called to get GeomObject
486 //=================================================================================
487 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
490 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
491 GEOMBase::GetShape(aGeomObj, shape);
495 //=================================================================================
496 // function : GetListOfIds
497 // purpose : Called to get the list of SubShapes IDs
498 //=================================================================================
499 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
501 SMESH::long_array_var anArray = new SMESH::long_array;
503 if ( myMainEntry != "" && myIsNotCorrected )
504 myListOfIDs = GetCorrectedListOfIDs( true );
506 int size = myListOfIDs.size();
507 anArray->length( size );
509 for (int i = 0; i < size; i++) {
510 anArray[i] = myListOfIDs.at(i);
516 //=================================================================================
517 // function : SetListOfIds
518 // purpose : Called to set the list of SubShapes IDs
519 //=================================================================================
520 void StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
522 mySelectedIDs.clear();
524 int size = theIds->length();
525 for ( int i = 0; i < size; i++ )
526 mySelectedIDs.append( theIds[ i ] );
528 mySelectedIDs = GetCorrectedListOfIDs( false );
532 //=================================================================================
533 // function : SetMainShapeEntry
534 // purpose : Called to set the Entry of main shape of the mesh
535 //=================================================================================
536 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
538 myMainEntry = theEntry;
539 myMainShape = GetTopoDSByEntry( theEntry );
540 myIsNotCorrected = true;
543 //=================================================================================
544 // function : GetMainShapeEntry
545 // purpose : Called to get the Main Object Entry
546 //=================================================================================
547 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
549 if ( myMainEntry == "")
550 return myEntry.toLatin1().data();
552 return myMainEntry.toLatin1().data();
555 //=================================================================================
556 // function : GetCorrectedListOfIds
557 // purpose : Called to convert the list of IDs from sub-shape IDs to main shape IDs
558 //=================================================================================
559 QList<int> StdMeshersGUI_SubShapeSelectorWdg::GetCorrectedListOfIDs( bool fromSubshapeToMainshape )
561 if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && fromSubshapeToMainshape )
563 else if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && !fromSubshapeToMainshape )
564 return mySelectedIDs;
567 TopTools_IndexedMapOfShape aGeomMap;
568 TopTools_IndexedMapOfShape aMainMap;
569 TopExp::MapShapes(myGeomShape, aGeomMap);
570 TopExp::MapShapes(myMainShape, aMainMap);
572 if ( fromSubshapeToMainshape ) { // convert indexes from sub-shape to mainshape
573 int size = myListOfIDs.size();
574 for (int i = 0; i < size; i++) {
575 TopoDS_Shape aSubShape = aGeomMap.FindKey( myListOfIDs.at(i) );
576 int index = aMainMap.FindIndex( aSubShape );
577 aList.append( index );
579 myIsNotCorrected = false;
580 } else { // convert indexes from main shape to sub-shape
581 int size = mySelectedIDs.size();
582 for (int i = 0; i < size; i++) {
583 TopoDS_Shape aSubShape = aMainMap.FindKey( mySelectedIDs.at(i) );
584 int index = aGeomMap.FindIndex( aSubShape );
585 aList.append( index );
592 void StdMeshersGUI_SubShapeSelectorWdg::updateButtons()
594 if ( myPreviewActor ) {
595 int total = myPreviewActor->count();
596 int chunk = myPreviewActor->currentChunk();
597 int chunkSize = myPreviewActor->chunkSize();
598 int imin = chunk*chunkSize+1;
599 int imax = std::min((chunk+1)*chunkSize, total);
600 bool vis = imax > 0 && total > chunkSize;
601 myInfoLabel->setVisible( vis );
602 myPrevButton->setVisible( vis );
603 myNextButton->setVisible( vis );
604 myInfoLabel->setText( tr( "X_FROM_Y_ITEMS_SHOWN" ).arg(imin).arg(imax).arg(total) );
605 myPrevButton->setEnabled( myPreviewActor->hasPrevious() );
606 myNextButton->setEnabled( myPreviewActor->hasNext() );