1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : StdMeshersGUI_SubShapeSelectorWdg.cxx
23 // Author : Open CASCADE S.A.S. (dmv)
27 #include "StdMeshersGUI_SubShapeSelectorWdg.h"
30 #include <SMESH_Type.h>
31 #include "SMESHGUI_MeshUtils.h"
32 #include <SMESH_Actor.h>
33 #include <SMESH_PreviewActorsCollection.h>
34 #include <SMESH_ActorUtils.h>
35 #include "SMESHGUI_GroupUtils.h"
36 #include "SMESH_Gen_i.hxx"
37 #include "SMESHGUI_GEOMGenUtils.h"
40 #include <SVTK_ViewWindow.h>
41 #include <SVTK_ViewModel.h>
42 #include <SVTK_ViewWindow.h>
43 #include <SVTK_Selector.h>
45 // SALOME GUI includes
46 #include <SALOME_ListIO.hxx>
47 #include <LightApp_SelectionMgr.h>
48 #include <SALOME_ListIteratorOfListIO.hxx>
51 #include <SUIT_ResourceMgr.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>
70 // SALOME KERNEL includes
71 #include <SALOMEDS_SObject.hxx>
76 //================================================================================
80 //================================================================================
82 StdMeshersGUI_SubShapeSelectorWdg
83 ::StdMeshersGUI_SubShapeSelectorWdg( QWidget * parent ):
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 myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
98 edgesLayout->addWidget(myListWidget, 0, 0, 3, 3);
99 edgesLayout->addWidget(myAddButton, 0, 4);
100 edgesLayout->addWidget(myRemoveButton, 1, 4);
102 edgesLayout->setRowStretch(2, 5);
103 edgesLayout->setColumnStretch(2, 5);
105 setLayout( edgesLayout );
106 setMinimumWidth( 300 );
109 mySubShType = TopAbs_EDGE;
114 //================================================================================
118 //================================================================================
120 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
122 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
123 if ( myPreviewActor ) {
124 myPreviewActor->RemoveFromRender( myRenderer );
125 aViewWindow->Repaint();
127 delete myPreviewActor;
133 myMainShape.Nullify();
136 //================================================================================
138 * Create a layout, initialize fields
140 //================================================================================
142 void StdMeshersGUI_SubShapeSelectorWdg::init()
145 myIsNotCorrected = true; // to dont call the GetCorrectedValue method twice
147 mySelectedIDs.clear();
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();
205 SALOME_ListIteratorOfListIO anIt (aList);
207 for ( ; anIt.More(); anIt.Next()) { // Loop on selected objects
208 Handle(SALOME_InteractiveObject) IO = anIt.Value();
210 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( IO->getEntry() );
211 if ( !CORBA::is_nil( aGeomObj ) ) { // Selected Object From Study
212 GEOM::GEOM_Object_ptr aGeomFatherObj = aGeomObj->GetMainShape();
213 QString aFatherEntry = "";
214 QString aMainFatherEntry = "";
216 if ( !CORBA::is_nil( aGeomFatherObj ) ) {
218 GEOM::GEOM_Object_var aGeomMain = GetGeomObjectByEntry( myEntry );
219 if ( !CORBA::is_nil( aGeomMain ) && aGeomMain->GetType() == 37 ) { // Main Shape is a Group
220 GEOM::GEOM_Object_ptr aMainFatherObj = aGeomMain->GetMainShape();
221 if ( !CORBA::is_nil( aMainFatherObj ) )
222 aMainFatherEntry = aMainFatherObj->GetStudyEntry();
224 aFatherEntry = aGeomFatherObj->GetStudyEntry();
227 if ( aFatherEntry != "" && ( aFatherEntry == myEntry || aFatherEntry == aMainFatherEntry ) ) {
228 if ( aGeomObj->GetType() == 37 /*GEOM_GROUP*/ ) { // Selected Group that belongs the main object
229 GEOMBase::GetShape(aGeomObj, shape);
230 if ( !shape.IsNull() ) {
231 TopExp_Explorer exp( shape, mySubShType );
232 for ( ; exp.More(); exp.Next() ) {
233 int index = myPreviewActor->GetIndexByShape( exp.Current() );
235 mySelectedIDs.append( index );
236 myPreviewActor->HighlightID( index );
240 } else if ( aGeomObj->GetType() == 28 /*GEOM_SUBSHAPE*/ ) {
241 GEOMBase::GetShape(aGeomObj, shape);
242 if ( !shape.IsNull() && shape.ShapeType() == mySubShType ) {
243 int index = myPreviewActor->GetIndexByShape( shape );
245 mySelectedIDs.append( index );
246 myPreviewActor->HighlightID( index );
251 } else { // Selected Actor from Actor Collection
252 QString anEntry = IO->getEntry();
254 int index = anEntry.lastIndexOf( str );
255 anEntry.remove(0, index+1);
256 int ind = anEntry.toInt();
258 mySelectedIDs.append( ind );
263 //=================================================================================
264 // function : onAdd()
265 // purpose : Called when Add Button Clicked
266 //=================================================================================
267 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
269 if ( mySelectedIDs.size() < 1 )
272 myListWidget->blockSignals( true );
273 for (int i = 0; i < mySelectedIDs.size(); i++) {
274 if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
275 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
277 QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
278 anItem->setSelected(true);
280 myListOfIDs.append( mySelectedIDs.at(i) );
283 onListSelectionChanged();
285 myListWidget->blockSignals( false );
287 if( myListOfIDs.size() >= myMaxSize )
288 myAddButton->setEnabled( false );
291 //=================================================================================
292 // function : onRemove()
293 // purpose : Called when Remove Button Clicked
294 //=================================================================================
295 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
297 if ( myListWidget->count() < 1 )
300 myListWidget->blockSignals( true );
301 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
302 QListWidgetItem* item;
303 foreach(item, selItems) {
304 QString idStr = item->text();
305 int id = idStr.toInt();
307 int index = myListOfIDs.indexOf( id );
308 myListOfIDs.removeAt( index );
312 onListSelectionChanged();
313 myListWidget->blockSignals( false );
315 myAddButton->setEnabled( true );
318 //=================================================================================
319 // function : onListSelectionChanged()
320 // purpose : Called when selection in element list is changed
321 //=================================================================================
322 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
324 if ( !myPreviewActor )
327 mySelectionMgr->clearSelected();
328 TColStd_MapOfInteger aIndexes;
329 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
330 QListWidgetItem* anItem;
331 foreach(anItem, selItems)
332 myPreviewActor->HighlightID( anItem->text().toInt() );
335 //=================================================================================
336 // function : setGeomShape
337 // purpose : Called to set geometry
338 //================================================================================
339 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
341 if ( theEntry != "") {
342 myParamValue = theEntry;
344 myGeomShape = GetTopoDSByEntry( theEntry );
346 myIsNotCorrected = true;
350 //=================================================================================
351 // function : updateState
352 // purpose : update Widget state
353 //=================================================================================
354 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
357 if ( !myGeomShape.IsNull() )
360 myListWidget->setEnabled( state );
361 myAddButton->setEnabled( state );
362 myRemoveButton->setEnabled( state );
365 myPreviewActor = new SMESH_PreviewActorsCollection();
366 myPreviewActor->SetSelector( mySelector );
367 myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
368 myPreviewActor->SetShown( false );
370 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
371 myRenderer = aViewWindow->getRenderer();
372 myPreviewActor->AddToRender( myRenderer );
373 aViewWindow->Repaint();
378 //=================================================================================
379 // function : GetGeomObjectByEntry
380 // purpose : Called to get GeomObject
381 //=================================================================================
382 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
384 GEOM::GEOM_Object_var aGeomObj;
385 SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
387 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
388 SALOMEDS::GenericAttribute_var anAttr;
390 if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
391 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
392 CORBA::String_var aVal = anIOR->Value();
393 CORBA::Object_var obj = aStudy->ConvertIORToObject(aVal);
394 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
400 //=================================================================================
401 // function : setObjectByEntry
402 // purpose : Called to get GeomObject
403 //=================================================================================
404 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
407 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
408 GEOMBase::GetShape(aGeomObj, shape);
412 //=================================================================================
413 // function : GetListOfIds
414 // purpose : Called to get the list of SubShapes IDs
415 //=================================================================================
416 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
418 SMESH::long_array_var anArray = new SMESH::long_array;
420 if ( myMainEntry != "" && myIsNotCorrected )
421 myListOfIDs = GetCorrectedListOfIDs( true );
423 int size = myListOfIDs.size();
424 anArray->length( size );
426 for (int i = 0; i < size; i++) {
427 anArray[i] = myListOfIDs.at(i);
433 //=================================================================================
434 // function : SetListOfIds
435 // purpose : Called to set the list of SubShapes IDs
436 //=================================================================================
437 void StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
439 mySelectedIDs.clear();
441 int size = theIds->length();
442 for ( int i = 0; i < size; i++ )
443 mySelectedIDs.append( theIds[ i ] );
445 mySelectedIDs = GetCorrectedListOfIDs( false );
449 //=================================================================================
450 // function : SetMainShapeEntry
451 // purpose : Called to set the Main Object Entry
452 //=================================================================================
453 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
455 myMainEntry = theEntry;
456 myMainShape = GetTopoDSByEntry( theEntry );
457 myIsNotCorrected = true;
460 //=================================================================================
461 // function : GetMainShapeEntry
462 // purpose : Called to get the Main Object Entry
463 //=================================================================================
464 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
466 if ( myMainEntry == "")
467 return myEntry.toLatin1().data();
469 return myMainEntry.toLatin1().data();
472 //=================================================================================
473 // function : GetCorrectedListOfIds
474 // purpose : Called to convert the list of IDs from subshape IDs to main shape IDs
475 //=================================================================================
476 QList<int> StdMeshersGUI_SubShapeSelectorWdg::GetCorrectedListOfIDs( bool fromSubshapeToMainshape )
478 if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && fromSubshapeToMainshape )
480 else if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && !fromSubshapeToMainshape )
481 return mySelectedIDs;
484 TopTools_IndexedMapOfShape aGeomMap;
485 TopTools_IndexedMapOfShape aMainMap;
486 TopExp::MapShapes(myGeomShape, aGeomMap);
487 TopExp::MapShapes(myMainShape, aMainMap);
489 if ( fromSubshapeToMainshape ) { // convert indexes from subshape to mainshape
490 int size = myListOfIDs.size();
491 for (int i = 0; i < size; i++) {
492 TopoDS_Shape aSubShape = aGeomMap.FindKey( myListOfIDs.at(i) );
493 int index = aMainMap.FindIndex( aSubShape );
494 aList.append( index );
496 myIsNotCorrected = false;
497 } else { // convert indexes from main shape to subshape
498 int size = mySelectedIDs.size();
499 for (int i = 0; i < size; i++) {
500 TopoDS_Shape aSubShape = aMainMap.FindKey( mySelectedIDs.at(i) );
501 int index = aGeomMap.FindIndex( aSubShape );
502 aList.append( index );