1 // Copyright (C) 2007-2012 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 ) &&
307 mySelectedIDs.size() > 0 &&
308 ( mySelectedIDs.size() <= myMaxSize || myMaxSize == -1 ) );
310 //Connect Selected Ids in viewer and dialog's Ids list
311 bool signalsBlocked = myListWidget->blockSignals( true );
312 myListWidget->clearSelection();
313 if ( mySelectedIDs.size() > 0 ) {
314 for (int i = 0; i < mySelectedIDs.size(); i++) {
315 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
316 QList<QListWidgetItem*> anItems = myListWidget->findItems ( anID, Qt::MatchExactly );
317 QListWidgetItem* item;
318 foreach(item, anItems)
319 item->setSelected(true);
322 myListWidget->blockSignals( signalsBlocked );
325 //=================================================================================
326 // function : onAdd()
327 // purpose : Called when Add Button Clicked
328 //=================================================================================
329 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
331 if ( mySelectedIDs.size() < 1 )
334 myListWidget->blockSignals( true );
335 for (int i = 0; i < mySelectedIDs.size() && (myMaxSize == -1 || myListOfIDs.size() < myMaxSize); i++) {
336 if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
337 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
339 QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
340 anItem->setSelected(true);
342 myListOfIDs.append( mySelectedIDs.at(i) );
345 onListSelectionChanged();
346 myListWidget->blockSignals( false );
347 myAddButton->setEnabled( myMaxSize == -1 || myListOfIDs.size() < myMaxSize );
350 //=================================================================================
351 // function : onRemove()
352 // purpose : Called when Remove Button Clicked
353 //=================================================================================
354 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
356 if ( myListWidget->count() < 1 )
359 myListWidget->blockSignals( true );
360 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
361 QListWidgetItem* item;
362 foreach(item, selItems) {
363 QString idStr = item->text();
364 int id = idStr.toInt();
366 int index = myListOfIDs.indexOf( id );
367 myListOfIDs.removeAt( index );
371 onListSelectionChanged();
372 myListWidget->blockSignals( false );
374 myAddButton->setEnabled( true );
377 void StdMeshersGUI_SubShapeSelectorWdg::onPrevious()
379 if ( myPreviewActor ) {
380 myPreviewActor->previous();
381 myListWidget->clearSelection();
383 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
384 aViewWindow->Repaint();
388 void StdMeshersGUI_SubShapeSelectorWdg::onNext()
390 if ( myPreviewActor ) {
391 myPreviewActor->next();
392 myListWidget->clearSelection();
394 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
395 aViewWindow->Repaint();
399 //=================================================================================
400 // function : onListSelectionChanged()
401 // purpose : Called when selection in element list is changed
402 //=================================================================================
403 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
405 if ( !myPreviewActor )
408 mySelectionMgr->clearSelected();
409 TColStd_MapOfInteger aIndexes;
410 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
411 QListWidgetItem* anItem;
412 foreach(anItem, selItems)
413 myPreviewActor->HighlightID( anItem->text().toInt() );
415 // update remove button
416 myRemoveButton->setEnabled( selItems.size() > 0 );
419 //=================================================================================
420 // function : setGeomShape
421 // purpose : Called to set geometry whose sub-shapes are selected
422 //================================================================================
423 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
425 if ( theEntry != "") {
426 myParamValue = theEntry;
428 myGeomShape = GetTopoDSByEntry( theEntry );
430 myIsNotCorrected = true;
434 //=================================================================================
435 // function : updateState
436 // purpose : update Widget state
437 //=================================================================================
438 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
441 if ( !myGeomShape.IsNull() )
443 myInfoLabel->setVisible( false );
444 myPrevButton->setVisible( false );
445 myNextButton->setVisible( false );
447 myListWidget->setEnabled( state );
448 myAddButton->setEnabled( mySelectedIDs.size() > 0 );
451 myPreviewActor = new SMESH_PreviewActorsCollection();
452 myPreviewActor->SetSelector( mySelector );
453 myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
454 myPreviewActor->SetShown( false );
456 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
457 myRenderer = aViewWindow->getRenderer();
458 myPreviewActor->AddToRender( myRenderer );
459 aViewWindow->Repaint();
465 //=================================================================================
466 // function : GetGeomObjectByEntry
467 // purpose : Called to get GeomObject
468 //=================================================================================
469 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
471 GEOM::GEOM_Object_var aGeomObj;
472 SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
474 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
475 SALOMEDS::GenericAttribute_var anAttr;
477 if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
478 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
479 CORBA::String_var aVal = anIOR->Value();
480 CORBA::Object_var obj = aStudy->ConvertIORToObject(aVal);
481 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
487 //=================================================================================
488 // function : setObjectByEntry
489 // purpose : Called to get GeomObject
490 //=================================================================================
491 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
494 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
495 GEOMBase::GetShape(aGeomObj, shape);
499 //=================================================================================
500 // function : GetListOfIds
501 // purpose : Called to get the list of SubShapes IDs
502 //=================================================================================
503 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
505 SMESH::long_array_var anArray = new SMESH::long_array;
507 if ( myMainEntry != "" && myIsNotCorrected )
508 myListOfIDs = GetCorrectedListOfIDs( true );
510 int size = myListOfIDs.size();
511 anArray->length( size );
513 for (int i = 0; i < size; i++) {
514 anArray[i] = myListOfIDs.at(i);
520 //=================================================================================
521 // function : SetListOfIds
522 // purpose : Called to set the list of SubShapes IDs
523 //=================================================================================
524 void StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
526 mySelectedIDs.clear();
528 int size = theIds->length();
529 for ( int i = 0; i < size; i++ )
530 mySelectedIDs.append( theIds[ i ] );
532 mySelectedIDs = GetCorrectedListOfIDs( false );
536 //=================================================================================
537 // function : SetMainShapeEntry
538 // purpose : Called to set the Entry of main shape of the mesh
539 //=================================================================================
540 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
542 myMainEntry = theEntry;
543 myMainShape = GetTopoDSByEntry( theEntry );
544 myIsNotCorrected = true;
547 //=================================================================================
548 // function : GetMainShapeEntry
549 // purpose : Called to get the Main Object Entry
550 //=================================================================================
551 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
553 if ( myMainEntry == "")
554 return myEntry.toLatin1().data();
556 return myMainEntry.toLatin1().data();
559 //=================================================================================
560 // function : GetCorrectedListOfIds
561 // purpose : Called to convert the list of IDs from sub-shape IDs to main shape IDs
562 //=================================================================================
563 QList<int> StdMeshersGUI_SubShapeSelectorWdg::GetCorrectedListOfIDs( bool fromSubshapeToMainshape )
565 if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && fromSubshapeToMainshape )
567 else if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && !fromSubshapeToMainshape )
568 return mySelectedIDs;
571 TopTools_IndexedMapOfShape aGeomMap;
572 TopTools_IndexedMapOfShape aMainMap;
573 TopExp::MapShapes(myGeomShape, aGeomMap);
574 TopExp::MapShapes(myMainShape, aMainMap);
576 if ( fromSubshapeToMainshape ) { // convert indexes from sub-shape to mainshape
577 int size = myListOfIDs.size();
578 for (int i = 0; i < size; i++) {
579 TopoDS_Shape aSubShape = aGeomMap.FindKey( myListOfIDs.at(i) );
580 int index = aMainMap.FindIndex( aSubShape );
581 aList.append( index );
583 myIsNotCorrected = false;
584 } else { // convert indexes from main shape to sub-shape
585 int size = mySelectedIDs.size();
586 for (int i = 0; i < size; i++) {
587 TopoDS_Shape aSubShape = aMainMap.FindKey( mySelectedIDs.at(i) );
588 int index = aGeomMap.FindIndex( aSubShape );
589 aList.append( index );
596 void StdMeshersGUI_SubShapeSelectorWdg::updateButtons()
598 if ( myPreviewActor ) {
599 int total = myPreviewActor->count();
600 int chunk = myPreviewActor->currentChunk();
601 int chunkSize = myPreviewActor->chunkSize();
602 int imin = chunk*chunkSize+1;
603 int imax = std::min((chunk+1)*chunkSize, total);
604 bool vis = imax > 0 && total > chunkSize;
605 myInfoLabel->setVisible( vis );
606 myPrevButton->setVisible( vis );
607 myNextButton->setVisible( vis );
608 myInfoLabel->setText( tr( "X_FROM_Y_ITEMS_SHOWN" ).arg(imin).arg(imax).arg(total) );
609 myPrevButton->setEnabled( myPreviewActor->hasPrevious() );
610 myNextButton->setEnabled( myPreviewActor->hasNext() );