1 // Copyright (C) 2007-2015 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 "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>
48 #include <SUIT_ResourceMgr.h>
52 #include <GEOM_TypeFilter.h>
53 #include <GEOM_CompoundFilter.h>
56 #include <QPushButton>
57 #include <QGridLayout>
58 #include <QListWidget>
63 #include <TColStd_MapOfInteger.hxx>
64 #include <TColStd_IndexedMapOfInteger.hxx>
65 #include <TopoDS_Shape.hxx>
67 #include <TopExp_Explorer.hxx>
68 #include <StdSelect_TypeOfEdge.hxx>
74 //================================================================================
78 //================================================================================
80 StdMeshersGUI_SubShapeSelectorWdg
81 ::StdMeshersGUI_SubShapeSelectorWdg( QWidget * parent, TopAbs_ShapeEnum aSubShType ):
86 QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
88 QGridLayout* edgesLayout = new QGridLayout( this );
89 edgesLayout->setMargin( MARGIN );
90 edgesLayout->setSpacing( SPACING );
92 myListWidget = new QListWidget( this );
93 myAddButton = new QPushButton( tr( "SMESH_BUT_ADD" ), this );
94 myRemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), this );
95 myInfoLabel = new QLabel( this );
96 myPrevButton = new QPushButton( "<<", this );
97 myNextButton = new QPushButton( ">>", this );
98 myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
100 edgesLayout->addWidget(myListWidget, 0, 0, 3, 3);
101 edgesLayout->addWidget(myAddButton, 0, 3);
102 edgesLayout->addWidget(myRemoveButton, 1, 3);
103 edgesLayout->addWidget(myInfoLabel, 3, 0, 1, 3);
104 edgesLayout->addWidget(myPrevButton, 4, 0);
105 edgesLayout->addWidget(myNextButton, 4, 2);
107 edgesLayout->setRowStretch(2, 5);
108 edgesLayout->setColumnStretch(1, 5);
110 myListWidget->setMinimumWidth(300);
111 myInfoLabel->setMinimumWidth(300);
112 myInfoLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
113 myInfoLabel->setAlignment(Qt::AlignCenter);
115 mySubShType = aSubShType;
120 //================================================================================
124 //================================================================================
126 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
128 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
129 if ( myPreviewActor ) {
130 myPreviewActor->RemoveFromRender( myRenderer );
131 aViewWindow->Repaint();
133 delete myPreviewActor;
139 myMainShape.Nullify();
141 if ( mySelectionMgr && myFilter )
142 mySelectionMgr->removeFilter( myFilter );
143 delete myFilter; myFilter=0;
145 SUIT_SelectionFilter* filter;
146 foreach( filter, myGeomFilters )
150 //================================================================================
152 * Create a layout, initialize fields
154 //================================================================================
156 void StdMeshersGUI_SubShapeSelectorWdg::init()
159 myIsNotCorrected = true; // to dont call the GetCorrectedValue method twice
161 mySelectedIDs.clear();
163 myAddButton->setEnabled( false );
164 myRemoveButton->setEnabled( false );
166 mySMESHGUI = SMESHGUI::GetSMESHGUI();
167 mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
168 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
170 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
171 aViewWindow->SetSelectionMode( ActorSelection );
176 connect( myAddButton, SIGNAL(clicked()), SLOT(onAdd()));
177 connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
178 connect( myPrevButton, SIGNAL(clicked()), SLOT(onPrevious()));
179 connect( myNextButton, SIGNAL(clicked()), SLOT(onNext()));
181 connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
182 connect( myListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
187 //================================================================================
189 * \brief Install filters to select sub-shapes of mySubShType or their groups
191 //================================================================================
193 void StdMeshersGUI_SubShapeSelectorWdg::setFilter()
195 SalomeApp_Study* study = mySMESHGUI->activeStudy();
196 GEOM_TypeFilter* typeFilter = new GEOM_TypeFilter(study, mySubShType, /*isShapeType=*/true );
197 GEOM_CompoundFilter* gpoupFilter = new GEOM_CompoundFilter(study);
198 gpoupFilter->addSubType( mySubShType );
199 myGeomFilters.append( typeFilter );
200 myGeomFilters.append( gpoupFilter );
201 myFilter = new SMESH_LogicalFilter( myGeomFilters, SMESH_LogicalFilter::LO_OR );
202 mySelectionMgr->installFilter( myFilter );
205 //================================================================================
207 * Create a layout, initialize fields
209 //================================================================================
211 void StdMeshersGUI_SubShapeSelectorWdg::showPreview( bool visible)
213 if ( !myPreviewActor )
216 if ( myIsShown != visible ) {
217 myPreviewActor->SetShown( visible );
219 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
220 aViewWindow->Repaint();
226 //=================================================================================
227 // function : SelectionIntoArgument()
228 // purpose : Called when selection as changed or other case
229 //=================================================================================
230 void StdMeshersGUI_SubShapeSelectorWdg::SelectionIntoArgument()
232 if ( !myPreviewActor )
235 mySelectedIDs.clear();
239 mySelectionMgr->selectedObjects( aList );
240 int nbSel = aList.Extent();
243 SALOME_ListIteratorOfListIO anIt (aList);
245 for ( ; anIt.More(); anIt.Next()) { // Loop on selected objects
246 Handle(SALOME_InteractiveObject) IO = anIt.Value();
248 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( IO->getEntry() );
249 if ( !CORBA::is_nil( aGeomObj ) ) { // Selected Object From Study
250 GEOM::GEOM_Object_var aGeomFatherObj = aGeomObj->GetMainShape();
251 QString aFatherEntry = "";
252 QString aMainFatherEntry = "";
254 if ( !CORBA::is_nil( aGeomFatherObj ) ) {
256 GEOM::GEOM_Object_var aGeomMain = GetGeomObjectByEntry( myEntry );
257 if ( !CORBA::is_nil( aGeomMain ) && aGeomMain->GetType() == 37 ) { // Main Shape is a Group
258 GEOM::GEOM_Object_var aMainFatherObj = aGeomMain->GetMainShape();
259 if ( !CORBA::is_nil( aMainFatherObj ) )
260 aMainFatherEntry = aMainFatherObj->GetStudyEntry();
262 aFatherEntry = aGeomFatherObj->GetStudyEntry();
265 if ( aFatherEntry != "" && ( aFatherEntry == myEntry || aFatherEntry == aMainFatherEntry ) )
267 if ( aGeomObj->GetType() == 37 /*GEOM_GROUP*/ ) { // Selected Group that belongs the main object
268 GEOMBase::GetShape(aGeomObj, shape);
269 if ( !shape.IsNull() ) {
270 TopExp_Explorer exp( shape, mySubShType );
271 for ( ; exp.More(); exp.Next() ) {
272 int index = myPreviewActor->GetIndexByShape( exp.Current() );
274 mySelectedIDs.append( index );
275 myPreviewActor->HighlightID( index );
279 } else if ( aGeomObj->GetType() == 28 /*GEOM_SUBSHAPE*/ ) {
280 GEOMBase::GetShape(aGeomObj, shape);
281 if ( !shape.IsNull() && shape.ShapeType() == mySubShType ) {
282 int index = myPreviewActor->GetIndexByShape( shape );
284 mySelectedIDs.append( index );
285 myPreviewActor->HighlightID( index );
290 } else { // Selected Actor from Actor Collection
291 QString anEntry = IO->getEntry();
293 int index = anEntry.lastIndexOf( str );
294 anEntry.remove(0, index+1);
295 int ind = anEntry.toInt();
297 mySelectedIDs.append( ind );
302 myAddButton->setEnabled( ( myListWidget->count() < myMaxSize || myMaxSize == -1 ) &&
303 mySelectedIDs.size() > 0 &&
304 ( mySelectedIDs.size() <= myMaxSize || myMaxSize == -1 ) );
306 //Connect Selected Ids in viewer and dialog's Ids list
307 bool signalsBlocked = myListWidget->blockSignals( true );
308 myListWidget->clearSelection();
309 if ( mySelectedIDs.size() > 0 ) {
310 for (int i = 0; i < mySelectedIDs.size(); i++) {
311 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
312 QList<QListWidgetItem*> anItems = myListWidget->findItems ( anID, Qt::MatchExactly );
313 QListWidgetItem* item;
314 foreach(item, anItems)
315 item->setSelected(true);
318 myListWidget->blockSignals( signalsBlocked );
321 //=================================================================================
322 // function : onAdd()
323 // purpose : Called when Add Button Clicked
324 //=================================================================================
325 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
327 if ( mySelectedIDs.size() < 1 )
330 myListWidget->blockSignals( true );
331 for (int i = 0; i < mySelectedIDs.size() && (myMaxSize == -1 || myListOfIDs.size() < myMaxSize); i++) {
332 if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
333 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
335 QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
336 anItem->setSelected(true);
338 myListOfIDs.append( mySelectedIDs.at(i) );
341 onListSelectionChanged();
342 myListWidget->blockSignals( false );
343 myAddButton->setEnabled( myMaxSize == -1 || myListOfIDs.size() < myMaxSize );
346 //=================================================================================
347 // function : onRemove()
348 // purpose : Called when Remove Button Clicked
349 //=================================================================================
350 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
352 if ( myListWidget->count() < 1 )
355 myListWidget->blockSignals( true );
356 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
357 QListWidgetItem* item;
358 foreach(item, selItems) {
359 QString idStr = item->text();
360 int id = idStr.toInt();
362 int index = myListOfIDs.indexOf( id );
363 myListOfIDs.removeAt( index );
367 onListSelectionChanged();
368 myListWidget->blockSignals( false );
370 myAddButton->setEnabled( true );
373 void StdMeshersGUI_SubShapeSelectorWdg::onPrevious()
375 if ( myPreviewActor ) {
376 myPreviewActor->previous();
377 myListWidget->clearSelection();
379 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
380 aViewWindow->Repaint();
384 void StdMeshersGUI_SubShapeSelectorWdg::onNext()
386 if ( myPreviewActor ) {
387 myPreviewActor->next();
388 myListWidget->clearSelection();
390 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
391 aViewWindow->Repaint();
395 //=================================================================================
396 // function : onListSelectionChanged()
397 // purpose : Called when selection in element list is changed
398 //=================================================================================
399 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
401 if ( !myPreviewActor )
404 mySelectionMgr->clearSelected();
405 TColStd_MapOfInteger aIndexes;
406 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
407 QListWidgetItem* anItem;
408 foreach(anItem, selItems)
409 myPreviewActor->HighlightID( anItem->text().toInt() );
411 // update remove button
412 myRemoveButton->setEnabled( selItems.size() > 0 );
415 //=================================================================================
416 // function : setGeomShape
417 // purpose : Called to set geometry whose sub-shapes are selected
418 //================================================================================
419 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
421 if ( theEntry != "") {
422 myParamValue = theEntry;
424 myGeomShape = GetTopoDSByEntry( theEntry );
426 myIsNotCorrected = true;
430 //=================================================================================
431 // function : updateState
432 // purpose : update Widget state
433 //=================================================================================
434 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
437 if ( !myGeomShape.IsNull() )
439 myInfoLabel->setVisible( false );
440 myPrevButton->setVisible( false );
441 myNextButton->setVisible( false );
443 myListWidget->setEnabled( state );
444 myAddButton->setEnabled( mySelectedIDs.size() > 0 );
447 myPreviewActor = new SMESH_PreviewActorsCollection();
448 myPreviewActor->SetSelector( mySelector );
449 myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
450 myPreviewActor->SetShown( false );
452 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
453 myRenderer = aViewWindow->getRenderer();
454 myPreviewActor->AddToRender( myRenderer );
455 aViewWindow->Repaint();
461 //=================================================================================
462 // function : GetGeomObjectByEntry
463 // purpose : Called to get GeomObject
464 //=================================================================================
465 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
467 GEOM::GEOM_Object_var aGeomObj;
468 SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
469 if ( !aStudy->_is_nil() )
471 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
472 if (!aSObj->_is_nil() )
474 CORBA::Object_var obj = aSObj->GetObject();
475 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
479 return aGeomObj._retn();
482 //=================================================================================
483 // function : setObjectByEntry
484 // purpose : Called to get GeomObject
485 //=================================================================================
486 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
489 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
490 GEOMBase::GetShape(aGeomObj, shape);
494 //=================================================================================
495 // function : GetListOfIds
496 // purpose : Called to get the list of SubShapes IDs
497 //=================================================================================
498 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
500 SMESH::long_array_var anArray = new SMESH::long_array;
502 if ( myMainEntry != "" && myIsNotCorrected )
503 myListOfIDs = GetCorrectedListOfIDs( true );
505 int size = myListOfIDs.size();
506 anArray->length( size );
508 for (int i = 0; i < size; i++) {
509 anArray[i] = myListOfIDs.at(i);
515 //=================================================================================
516 // function : SetListOfIds
517 // purpose : Called to set the list of SubShapes IDs. Returns false if any ID is invalid
518 //=================================================================================
519 bool StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
521 mySelectedIDs.clear();
523 int size = theIds->length();
524 for ( int i = 0; i < size; i++ )
525 mySelectedIDs.append( theIds[ i ] );
528 mySelectedIDs = GetCorrectedListOfIDs( false, &isOk );
533 //=================================================================================
534 // function : SetMainShapeEntry
535 // purpose : Called to set the Entry of main shape of the mesh
536 //=================================================================================
537 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
539 myMainEntry = theEntry;
540 myMainShape = GetTopoDSByEntry( theEntry );
541 myIsNotCorrected = true;
544 //=================================================================================
545 // function : GetMainShapeEntry
546 // purpose : Called to get the Main Object Entry
547 //=================================================================================
548 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
550 if ( myMainEntry == "")
551 return myEntry.toLatin1().data();
553 return myMainEntry.toLatin1().data();
556 //=================================================================================
557 // function : GetCorrectedListOfIds
558 // purpose : Called to convert the list of IDs from sub-shape IDs to main shape IDs
559 //=================================================================================
561 StdMeshersGUI_SubShapeSelectorWdg::GetCorrectedListOfIDs( bool fromSubshapeToMainshape,
564 if (( myMainShape.IsNull() || myGeomShape.IsNull() ) && fromSubshapeToMainshape )
566 else if (( myMainShape.IsNull() /*||*/&& myGeomShape.IsNull() ) && !fromSubshapeToMainshape )
567 return mySelectedIDs;
569 if ( !fromSubshapeToMainshape ) // called from SetListOfIDs
571 if ( myMainShape.IsNull() )
572 std::swap( myMainShape, myGeomShape );
576 TopTools_IndexedMapOfShape aGeomMap, aMainMap;
577 TopExp::MapShapes(myMainShape, aMainMap);
578 if ( !myGeomShape.IsNull() )
579 TopExp::MapShapes(myGeomShape, aGeomMap);
582 if ( fromSubshapeToMainshape ) // convert indexes from sub-shape to mainshape
584 int size = myListOfIDs.size();
585 for (int i = 0; i < size; i++) {
586 int index = myListOfIDs.at(i);
587 if ( aGeomMap.Extent() < index )
593 TopoDS_Shape aSubShape = aGeomMap.FindKey( index );
594 if ( mySubShType != aSubShape.ShapeType() )
596 if ( !aMainMap.Contains( aSubShape ))
599 index = aMainMap.FindIndex( aSubShape );
601 aList.append( index );
603 myIsNotCorrected = false;
605 else // convert indexes from main shape to sub-shape, or just check indices
607 int size = mySelectedIDs.size();
608 for (int i = 0; i < size; i++) {
609 int index = mySelectedIDs.at(i);
610 if ( aMainMap.Extent() < index )
616 TopoDS_Shape aSubShape = aMainMap.FindKey( index );
617 if ( mySubShType != aSubShape.ShapeType() )
619 if ( !aGeomMap.Contains( aSubShape ) && !aGeomMap.IsEmpty() )
622 index = aGeomMap.FindIndex( aSubShape );
624 aList.append( index );
627 if ( isOK ) *isOK = ok;
632 void StdMeshersGUI_SubShapeSelectorWdg::updateButtons()
634 if ( myPreviewActor ) {
635 int total = myPreviewActor->count();
636 int chunk = myPreviewActor->currentChunk();
637 int chunkSize = myPreviewActor->chunkSize();
638 int imin = chunk*chunkSize+1;
639 int imax = std::min((chunk+1)*chunkSize, total);
640 bool vis = imax > 0 && total > chunkSize;
641 myInfoLabel->setVisible( vis );
642 myPrevButton->setVisible( vis );
643 myNextButton->setVisible( vis );
644 myInfoLabel->setText( tr( "X_FROM_Y_ITEMS_SHOWN" ).arg(imin).arg(imax).arg(total) );
645 myPrevButton->setEnabled( myPreviewActor->hasPrevious() );
646 myNextButton->setEnabled( myPreviewActor->hasNext() );