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 mySMESHGUI = SMESHGUI::GetSMESHGUI();
147 mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
148 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
150 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
151 aViewWindow->SetSelectionMode( ActorSelection );
153 connect( myAddButton, SIGNAL(clicked()), SLOT(onAdd()));
154 connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
156 connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
157 connect( myListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
162 //================================================================================
164 * Create a layout, initialize fields
166 //================================================================================
168 void StdMeshersGUI_SubShapeSelectorWdg::showPreview( bool visible)
170 if ( !myPreviewActor )
173 if ( myIsShown != visible ) {
174 myPreviewActor->SetShown( visible );
176 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
177 aViewWindow->Repaint();
183 //=================================================================================
184 // function : SelectionIntoArgument()
185 // purpose : Called when selection as changed or other case
186 //=================================================================================
187 void StdMeshersGUI_SubShapeSelectorWdg::SelectionIntoArgument()
189 if ( !myPreviewActor )
192 mySelectedIDs.clear();
196 mySelectionMgr->selectedObjects( aList );
197 int nbSel = aList.Extent();
202 SALOME_ListIteratorOfListIO anIt (aList);
204 for ( ; anIt.More(); anIt.Next()) { // Loop on selected objects
205 Handle(SALOME_InteractiveObject) IO = anIt.Value();
207 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( IO->getEntry() );
208 if ( !CORBA::is_nil( aGeomObj ) ) { // Selected Object From Study
209 GEOM::GEOM_Object_ptr aGeomFatherObj = aGeomObj->GetMainShape();
210 QString aFatherEntry = "";
211 QString aMainFatherEntry = "";
213 if ( !CORBA::is_nil( aGeomFatherObj ) ) {
215 GEOM::GEOM_Object_var aGeomMain = GetGeomObjectByEntry( myEntry );
216 if ( !CORBA::is_nil( aGeomMain ) && aGeomMain->GetType() == 37 ) { // Main Shape is a Group
217 GEOM::GEOM_Object_ptr aMainFatherObj = aGeomMain->GetMainShape();
218 if ( !CORBA::is_nil( aMainFatherObj ) )
219 aMainFatherEntry = aMainFatherObj->GetStudyEntry();
221 aFatherEntry = aGeomFatherObj->GetStudyEntry();
224 if ( aFatherEntry != "" && ( aFatherEntry == myEntry || aFatherEntry == aMainFatherEntry ) ) {
225 if ( aGeomObj->GetType() == 37 /*GEOM_GROUP*/ ) { // Selected Group that belongs the main object
226 GEOMBase::GetShape(aGeomObj, shape);
227 if ( !shape.IsNull() ) {
228 TopExp_Explorer exp( shape, mySubShType );
229 for ( ; exp.More(); exp.Next() ) {
230 int index = myPreviewActor->GetIndexByShape( exp.Current() );
232 mySelectedIDs.append( index );
233 myPreviewActor->HighlightID( index );
237 } else if ( aGeomObj->GetType() == 28 /*GEOM_SUBSHAPE*/ ) {
238 GEOMBase::GetShape(aGeomObj, shape);
239 if ( !shape.IsNull() && shape.ShapeType() == mySubShType ) {
240 int index = myPreviewActor->GetIndexByShape( shape );
242 mySelectedIDs.append( index );
243 myPreviewActor->HighlightID( index );
248 } else { // Selected Actor from Actor Collection
249 QString anEntry = IO->getEntry();
251 int index = anEntry.lastIndexOf( str );
252 anEntry.remove(0, index+1);
253 int ind = anEntry.toInt();
255 mySelectedIDs.append( ind );
260 //=================================================================================
261 // function : onAdd()
262 // purpose : Called when Add Button Clicked
263 //=================================================================================
264 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
266 if ( mySelectedIDs.size() < 1 )
269 myListWidget->blockSignals( true );
270 for (int i = 0; i < mySelectedIDs.size() && (myMaxSize == -1 || myListOfIDs.size() < myMaxSize); i++) {
271 if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
272 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
274 QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
275 anItem->setSelected(true);
277 myListOfIDs.append( mySelectedIDs.at(i) );
280 onListSelectionChanged();
281 myListWidget->blockSignals( false );
282 myAddButton->setEnabled( myMaxSize == -1 || myListOfIDs.size() < myMaxSize );
285 //=================================================================================
286 // function : onRemove()
287 // purpose : Called when Remove Button Clicked
288 //=================================================================================
289 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
291 if ( myListWidget->count() < 1 )
294 myListWidget->blockSignals( true );
295 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
296 QListWidgetItem* item;
297 foreach(item, selItems) {
298 QString idStr = item->text();
299 int id = idStr.toInt();
301 int index = myListOfIDs.indexOf( id );
302 myListOfIDs.removeAt( index );
306 onListSelectionChanged();
307 myListWidget->blockSignals( false );
309 myAddButton->setEnabled( true );
312 //=================================================================================
313 // function : onListSelectionChanged()
314 // purpose : Called when selection in element list is changed
315 //=================================================================================
316 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
318 if ( !myPreviewActor )
321 mySelectionMgr->clearSelected();
322 TColStd_MapOfInteger aIndexes;
323 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
324 QListWidgetItem* anItem;
325 foreach(anItem, selItems)
326 myPreviewActor->HighlightID( anItem->text().toInt() );
329 //=================================================================================
330 // function : setGeomShape
331 // purpose : Called to set geometry
332 //================================================================================
333 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
335 if ( theEntry != "") {
336 myParamValue = theEntry;
338 myGeomShape = GetTopoDSByEntry( theEntry );
340 myIsNotCorrected = true;
344 //=================================================================================
345 // function : updateState
346 // purpose : update Widget state
347 //=================================================================================
348 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
351 if ( !myGeomShape.IsNull() )
354 myListWidget->setEnabled( state );
355 myAddButton->setEnabled( state );
356 myRemoveButton->setEnabled( state );
359 myPreviewActor = new SMESH_PreviewActorsCollection();
360 myPreviewActor->SetSelector( mySelector );
361 myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
362 myPreviewActor->SetShown( false );
364 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
365 myRenderer = aViewWindow->getRenderer();
366 myPreviewActor->AddToRender( myRenderer );
367 aViewWindow->Repaint();
372 //=================================================================================
373 // function : GetGeomObjectByEntry
374 // purpose : Called to get GeomObject
375 //=================================================================================
376 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
378 GEOM::GEOM_Object_var aGeomObj;
379 SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
381 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
382 SALOMEDS::GenericAttribute_var anAttr;
384 if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
385 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
386 CORBA::String_var aVal = anIOR->Value();
387 CORBA::Object_var obj = aStudy->ConvertIORToObject(aVal);
388 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
394 //=================================================================================
395 // function : setObjectByEntry
396 // purpose : Called to get GeomObject
397 //=================================================================================
398 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
401 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
402 GEOMBase::GetShape(aGeomObj, shape);
406 //=================================================================================
407 // function : GetListOfIds
408 // purpose : Called to get the list of SubShapes IDs
409 //=================================================================================
410 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
412 SMESH::long_array_var anArray = new SMESH::long_array;
414 if ( myMainEntry != "" && myIsNotCorrected )
415 myListOfIDs = GetCorrectedListOfIDs( true );
417 int size = myListOfIDs.size();
418 anArray->length( size );
420 for (int i = 0; i < size; i++) {
421 anArray[i] = myListOfIDs.at(i);
427 //=================================================================================
428 // function : SetListOfIds
429 // purpose : Called to set the list of SubShapes IDs
430 //=================================================================================
431 void StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
433 mySelectedIDs.clear();
435 int size = theIds->length();
436 for ( int i = 0; i < size; i++ )
437 mySelectedIDs.append( theIds[ i ] );
439 mySelectedIDs = GetCorrectedListOfIDs( false );
443 //=================================================================================
444 // function : SetMainShapeEntry
445 // purpose : Called to set the Main Object Entry
446 //=================================================================================
447 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
449 myMainEntry = theEntry;
450 myMainShape = GetTopoDSByEntry( theEntry );
451 myIsNotCorrected = true;
454 //=================================================================================
455 // function : GetMainShapeEntry
456 // purpose : Called to get the Main Object Entry
457 //=================================================================================
458 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
460 if ( myMainEntry == "")
461 return myEntry.toLatin1().data();
463 return myMainEntry.toLatin1().data();
466 //=================================================================================
467 // function : GetCorrectedListOfIds
468 // purpose : Called to convert the list of IDs from subshape IDs to main shape IDs
469 //=================================================================================
470 QList<int> StdMeshersGUI_SubShapeSelectorWdg::GetCorrectedListOfIDs( bool fromSubshapeToMainshape )
472 if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && fromSubshapeToMainshape )
474 else if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && !fromSubshapeToMainshape )
475 return mySelectedIDs;
478 TopTools_IndexedMapOfShape aGeomMap;
479 TopTools_IndexedMapOfShape aMainMap;
480 TopExp::MapShapes(myGeomShape, aGeomMap);
481 TopExp::MapShapes(myMainShape, aMainMap);
483 if ( fromSubshapeToMainshape ) { // convert indexes from subshape to mainshape
484 int size = myListOfIDs.size();
485 for (int i = 0; i < size; i++) {
486 TopoDS_Shape aSubShape = aGeomMap.FindKey( myListOfIDs.at(i) );
487 int index = aMainMap.FindIndex( aSubShape );
488 aList.append( index );
490 myIsNotCorrected = false;
491 } else { // convert indexes from main shape to subshape
492 int size = mySelectedIDs.size();
493 for (int i = 0; i < size; i++) {
494 TopoDS_Shape aSubShape = aMainMap.FindKey( mySelectedIDs.at(i) );
495 int index = aGeomMap.FindIndex( aSubShape );
496 aList.append( index );