1 // Copyright (C) 2007-2010 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"
37 #include <SVTK_ViewWindow.h>
38 #include <SVTK_ViewModel.h>
39 #include <SVTK_ViewWindow.h>
40 #include <SVTK_Selector.h>
42 // SALOME GUI includes
43 #include <SALOME_ListIO.hxx>
44 #include <LightApp_SelectionMgr.h>
45 #include <SALOME_ListIteratorOfListIO.hxx>
48 #include <SUIT_ResourceMgr.h>
54 #include <QPushButton>
55 #include <QGridLayout>
56 #include <QListWidget>
61 #include <TColStd_MapOfInteger.hxx>
62 #include <TColStd_IndexedMapOfInteger.hxx>
63 #include <TopoDS_Shape.hxx>
65 #include <TopExp_Explorer.hxx>
67 // SALOME KERNEL includes
68 #include <SALOMEDS_SObject.hxx>
73 //================================================================================
77 //================================================================================
79 StdMeshersGUI_SubShapeSelectorWdg
80 ::StdMeshersGUI_SubShapeSelectorWdg( QWidget * parent ):
85 QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
87 QGridLayout* edgesLayout = new QGridLayout( this );
88 edgesLayout->setMargin( MARGIN );
89 edgesLayout->setSpacing( SPACING );
91 myListWidget = new QListWidget( this );
92 myAddButton = new QPushButton( tr( "SMESH_BUT_ADD" ), this );
93 myRemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), this );
94 myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
96 edgesLayout->addWidget(myListWidget, 0, 0, 3, 3);
97 edgesLayout->addWidget(myAddButton, 0, 4);
98 edgesLayout->addWidget(myRemoveButton, 1, 4);
100 edgesLayout->setRowStretch(2, 5);
101 edgesLayout->setColumnStretch(2, 5);
103 setLayout( edgesLayout );
104 setMinimumWidth( 300 );
106 mySubShType = TopAbs_EDGE;
111 //================================================================================
115 //================================================================================
117 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
119 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
120 if ( myPreviewActor ) {
121 myPreviewActor->RemoveFromRender( myRenderer );
122 aViewWindow->Repaint();
124 delete myPreviewActor;
130 myMainShape.Nullify();
133 //================================================================================
135 * Create a layout, initialize fields
137 //================================================================================
139 void StdMeshersGUI_SubShapeSelectorWdg::init()
142 myIsNotCorrected = true; // to dont call the GetCorrectedValue method twice
144 mySelectedIDs.clear();
146 myAddButton->setEnabled( false );
147 myRemoveButton->setEnabled( false );
149 mySMESHGUI = SMESHGUI::GetSMESHGUI();
150 mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
151 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
153 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
154 aViewWindow->SetSelectionMode( ActorSelection );
156 connect( myAddButton, SIGNAL(clicked()), SLOT(onAdd()));
157 connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
159 connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
160 connect( myListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
165 //================================================================================
167 * Create a layout, initialize fields
169 //================================================================================
171 void StdMeshersGUI_SubShapeSelectorWdg::showPreview( bool visible)
173 if ( !myPreviewActor )
176 if ( myIsShown != visible ) {
177 myPreviewActor->SetShown( visible );
179 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
180 aViewWindow->Repaint();
186 //=================================================================================
187 // function : SelectionIntoArgument()
188 // purpose : Called when selection as changed or other case
189 //=================================================================================
190 void StdMeshersGUI_SubShapeSelectorWdg::SelectionIntoArgument()
192 if ( !myPreviewActor )
195 mySelectedIDs.clear();
199 mySelectionMgr->selectedObjects( aList );
200 int nbSel = aList.Extent();
203 SALOME_ListIteratorOfListIO anIt (aList);
205 for ( ; anIt.More(); anIt.Next()) { // Loop on selected objects
206 Handle(SALOME_InteractiveObject) IO = anIt.Value();
208 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( IO->getEntry() );
209 if ( !CORBA::is_nil( aGeomObj ) ) { // Selected Object From Study
210 GEOM::GEOM_Object_ptr aGeomFatherObj = aGeomObj->GetMainShape();
211 QString aFatherEntry = "";
212 QString aMainFatherEntry = "";
214 if ( !CORBA::is_nil( aGeomFatherObj ) ) {
216 GEOM::GEOM_Object_var aGeomMain = GetGeomObjectByEntry( myEntry );
217 if ( !CORBA::is_nil( aGeomMain ) && aGeomMain->GetType() == 37 ) { // Main Shape is a Group
218 GEOM::GEOM_Object_ptr aMainFatherObj = aGeomMain->GetMainShape();
219 if ( !CORBA::is_nil( aMainFatherObj ) )
220 aMainFatherEntry = aMainFatherObj->GetStudyEntry();
222 aFatherEntry = aGeomFatherObj->GetStudyEntry();
225 if ( aFatherEntry != "" && ( aFatherEntry == myEntry || aFatherEntry == aMainFatherEntry ) ) {
226 if ( aGeomObj->GetType() == 37 /*GEOM_GROUP*/ ) { // Selected Group that belongs the main object
227 GEOMBase::GetShape(aGeomObj, shape);
228 if ( !shape.IsNull() ) {
229 TopExp_Explorer exp( shape, mySubShType );
230 for ( ; exp.More(); exp.Next() ) {
231 int index = myPreviewActor->GetIndexByShape( exp.Current() );
233 mySelectedIDs.append( index );
234 myPreviewActor->HighlightID( index );
238 } else if ( aGeomObj->GetType() == 28 /*GEOM_SUBSHAPE*/ ) {
239 GEOMBase::GetShape(aGeomObj, shape);
240 if ( !shape.IsNull() && shape.ShapeType() == mySubShType ) {
241 int index = myPreviewActor->GetIndexByShape( shape );
243 mySelectedIDs.append( index );
244 myPreviewActor->HighlightID( index );
249 } else { // Selected Actor from Actor Collection
250 QString anEntry = IO->getEntry();
252 int index = anEntry.lastIndexOf( str );
253 anEntry.remove(0, index+1);
254 int ind = anEntry.toInt();
256 mySelectedIDs.append( ind );
261 myAddButton->setEnabled( ( myListWidget->count() < myMaxSize || myMaxSize == -1 ) && mySelectedIDs.size() > 0 && ( mySelectedIDs.size() <= myMaxSize || myMaxSize == -1 ) );
263 //Connect Selected Ids in viewer and dialog's Ids list
264 myListWidget->clearSelection();
265 if ( mySelectedIDs.size() > 0 ) {
266 for (int i = 0; i < mySelectedIDs.size(); i++) {
267 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
268 QList<QListWidgetItem*> anItems = myListWidget->findItems ( anID, Qt::MatchExactly );
269 QListWidgetItem* item;
270 foreach(item, anItems)
271 item->setSelected(true);
276 //=================================================================================
277 // function : onAdd()
278 // purpose : Called when Add Button Clicked
279 //=================================================================================
280 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
282 if ( mySelectedIDs.size() < 1 )
285 myListWidget->blockSignals( true );
286 for (int i = 0; i < mySelectedIDs.size() && (myMaxSize == -1 || myListOfIDs.size() < myMaxSize); i++) {
287 if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
288 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
290 QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
291 anItem->setSelected(true);
293 myListOfIDs.append( mySelectedIDs.at(i) );
296 onListSelectionChanged();
297 myListWidget->blockSignals( false );
298 myAddButton->setEnabled( myMaxSize == -1 || myListOfIDs.size() < myMaxSize );
301 //=================================================================================
302 // function : onRemove()
303 // purpose : Called when Remove Button Clicked
304 //=================================================================================
305 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
307 if ( myListWidget->count() < 1 )
310 myListWidget->blockSignals( true );
311 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
312 QListWidgetItem* item;
313 foreach(item, selItems) {
314 QString idStr = item->text();
315 int id = idStr.toInt();
317 int index = myListOfIDs.indexOf( id );
318 myListOfIDs.removeAt( index );
322 onListSelectionChanged();
323 myListWidget->blockSignals( false );
325 myAddButton->setEnabled( true );
328 //=================================================================================
329 // function : onListSelectionChanged()
330 // purpose : Called when selection in element list is changed
331 //=================================================================================
332 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
334 if ( !myPreviewActor )
337 mySelectionMgr->clearSelected();
338 TColStd_MapOfInteger aIndexes;
339 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
340 QListWidgetItem* anItem;
341 foreach(anItem, selItems)
342 myPreviewActor->HighlightID( anItem->text().toInt() );
344 // update remove button
345 myRemoveButton->setEnabled( selItems.size() > 0 );
348 //=================================================================================
349 // function : setGeomShape
350 // purpose : Called to set geometry
351 //================================================================================
352 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
354 if ( theEntry != "") {
355 myParamValue = theEntry;
357 myGeomShape = GetTopoDSByEntry( theEntry );
359 myIsNotCorrected = true;
363 //=================================================================================
364 // function : updateState
365 // purpose : update Widget state
366 //=================================================================================
367 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
370 if ( !myGeomShape.IsNull() )
373 myListWidget->setEnabled( state );
374 myAddButton->setEnabled( mySelectedIDs.size() > 0 );
377 myPreviewActor = new SMESH_PreviewActorsCollection();
378 myPreviewActor->SetSelector( mySelector );
379 myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
380 myPreviewActor->SetShown( false );
382 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
383 myRenderer = aViewWindow->getRenderer();
384 myPreviewActor->AddToRender( myRenderer );
385 aViewWindow->Repaint();
390 //=================================================================================
391 // function : GetGeomObjectByEntry
392 // purpose : Called to get GeomObject
393 //=================================================================================
394 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
396 GEOM::GEOM_Object_var aGeomObj;
397 SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
399 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
400 SALOMEDS::GenericAttribute_var anAttr;
402 if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
403 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
404 CORBA::String_var aVal = anIOR->Value();
405 CORBA::Object_var obj = aStudy->ConvertIORToObject(aVal);
406 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
412 //=================================================================================
413 // function : setObjectByEntry
414 // purpose : Called to get GeomObject
415 //=================================================================================
416 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
419 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
420 GEOMBase::GetShape(aGeomObj, shape);
424 //=================================================================================
425 // function : GetListOfIds
426 // purpose : Called to get the list of SubShapes IDs
427 //=================================================================================
428 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
430 SMESH::long_array_var anArray = new SMESH::long_array;
432 if ( myMainEntry != "" && myIsNotCorrected )
433 myListOfIDs = GetCorrectedListOfIDs( true );
435 int size = myListOfIDs.size();
436 anArray->length( size );
438 for (int i = 0; i < size; i++) {
439 anArray[i] = myListOfIDs.at(i);
445 //=================================================================================
446 // function : SetListOfIds
447 // purpose : Called to set the list of SubShapes IDs
448 //=================================================================================
449 void StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
451 mySelectedIDs.clear();
453 int size = theIds->length();
454 for ( int i = 0; i < size; i++ )
455 mySelectedIDs.append( theIds[ i ] );
457 mySelectedIDs = GetCorrectedListOfIDs( false );
461 //=================================================================================
462 // function : SetMainShapeEntry
463 // purpose : Called to set the Main Object Entry
464 //=================================================================================
465 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
467 myMainEntry = theEntry;
468 myMainShape = GetTopoDSByEntry( theEntry );
469 myIsNotCorrected = true;
472 //=================================================================================
473 // function : GetMainShapeEntry
474 // purpose : Called to get the Main Object Entry
475 //=================================================================================
476 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
478 if ( myMainEntry == "")
479 return myEntry.toLatin1().data();
481 return myMainEntry.toLatin1().data();
484 //=================================================================================
485 // function : GetCorrectedListOfIds
486 // purpose : Called to convert the list of IDs from subshape IDs to main shape IDs
487 //=================================================================================
488 QList<int> StdMeshersGUI_SubShapeSelectorWdg::GetCorrectedListOfIDs( bool fromSubshapeToMainshape )
490 if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && fromSubshapeToMainshape )
492 else if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && !fromSubshapeToMainshape )
493 return mySelectedIDs;
496 TopTools_IndexedMapOfShape aGeomMap;
497 TopTools_IndexedMapOfShape aMainMap;
498 TopExp::MapShapes(myGeomShape, aGeomMap);
499 TopExp::MapShapes(myMainShape, aMainMap);
501 if ( fromSubshapeToMainshape ) { // convert indexes from subshape to mainshape
502 int size = myListOfIDs.size();
503 for (int i = 0; i < size; i++) {
504 TopoDS_Shape aSubShape = aGeomMap.FindKey( myListOfIDs.at(i) );
505 int index = aMainMap.FindIndex( aSubShape );
506 aList.append( index );
508 myIsNotCorrected = false;
509 } else { // convert indexes from main shape to subshape
510 int size = mySelectedIDs.size();
511 for (int i = 0; i < size; i++) {
512 TopoDS_Shape aSubShape = aMainMap.FindKey( mySelectedIDs.at(i) );
513 int index = aGeomMap.FindIndex( aSubShape );
514 aList.append( index );