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 ):
88 QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
90 QGridLayout* edgesLayout = new QGridLayout( this );
91 edgesLayout->setMargin( MARGIN );
92 edgesLayout->setSpacing( SPACING );
94 myListWidget = new QListWidget( this );
95 myAddButton = new QPushButton( tr( "SMESH_BUT_ADD" ), this );
96 myRemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), this );
97 myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
99 edgesLayout->addWidget(myListWidget, 0, 0, 3, 3);
100 edgesLayout->addWidget(myAddButton, 0, 4);
101 edgesLayout->addWidget(myRemoveButton, 1, 4);
103 edgesLayout->setRowStretch(2, 5);
104 edgesLayout->setColumnStretch(2, 5);
106 setLayout( edgesLayout );
107 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() && (myMaxSize < 1 || myListOfIDs.size() < myMaxSize); 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();
284 myListWidget->blockSignals( false );
285 myAddButton->setEnabled( myListOfIDs.size() < myMaxSize );
288 //=================================================================================
289 // function : onRemove()
290 // purpose : Called when Remove Button Clicked
291 //=================================================================================
292 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
294 if ( myListWidget->count() < 1 )
297 myListWidget->blockSignals( true );
298 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
299 QListWidgetItem* item;
300 foreach(item, selItems) {
301 QString idStr = item->text();
302 int id = idStr.toInt();
304 int index = myListOfIDs.indexOf( id );
305 myListOfIDs.removeAt( index );
309 onListSelectionChanged();
310 myListWidget->blockSignals( false );
312 myAddButton->setEnabled( true );
315 //=================================================================================
316 // function : onListSelectionChanged()
317 // purpose : Called when selection in element list is changed
318 //=================================================================================
319 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
321 if ( !myPreviewActor )
324 mySelectionMgr->clearSelected();
325 TColStd_MapOfInteger aIndexes;
326 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
327 QListWidgetItem* anItem;
328 foreach(anItem, selItems)
329 myPreviewActor->HighlightID( anItem->text().toInt() );
332 //=================================================================================
333 // function : setGeomShape
334 // purpose : Called to set geometry
335 //================================================================================
336 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
338 if ( theEntry != "") {
339 myParamValue = theEntry;
341 myGeomShape = GetTopoDSByEntry( theEntry );
343 myIsNotCorrected = true;
347 //=================================================================================
348 // function : updateState
349 // purpose : update Widget state
350 //=================================================================================
351 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
354 if ( !myGeomShape.IsNull() )
357 myListWidget->setEnabled( state );
358 myAddButton->setEnabled( state );
359 myRemoveButton->setEnabled( state );
362 myPreviewActor = new SMESH_PreviewActorsCollection();
363 myPreviewActor->SetSelector( mySelector );
364 myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
365 myPreviewActor->SetShown( false );
367 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
368 myRenderer = aViewWindow->getRenderer();
369 myPreviewActor->AddToRender( myRenderer );
370 aViewWindow->Repaint();
375 //=================================================================================
376 // function : GetGeomObjectByEntry
377 // purpose : Called to get GeomObject
378 //=================================================================================
379 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
381 GEOM::GEOM_Object_var aGeomObj;
382 SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
384 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
385 SALOMEDS::GenericAttribute_var anAttr;
387 if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
388 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
389 CORBA::String_var aVal = anIOR->Value();
390 CORBA::Object_var obj = aStudy->ConvertIORToObject(aVal);
391 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
397 //=================================================================================
398 // function : setObjectByEntry
399 // purpose : Called to get GeomObject
400 //=================================================================================
401 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
404 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
405 GEOMBase::GetShape(aGeomObj, shape);
409 //=================================================================================
410 // function : GetListOfIds
411 // purpose : Called to get the list of SubShapes IDs
412 //=================================================================================
413 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
415 SMESH::long_array_var anArray = new SMESH::long_array;
417 if ( myMainEntry != "" && myIsNotCorrected )
418 myListOfIDs = GetCorrectedListOfIDs( true );
420 int size = myListOfIDs.size();
421 anArray->length( size );
423 for (int i = 0; i < size; i++) {
424 anArray[i] = myListOfIDs.at(i);
430 //=================================================================================
431 // function : SetListOfIds
432 // purpose : Called to set the list of SubShapes IDs
433 //=================================================================================
434 void StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
436 mySelectedIDs.clear();
438 int size = theIds->length();
439 for ( int i = 0; i < size; i++ )
440 mySelectedIDs.append( theIds[ i ] );
442 mySelectedIDs = GetCorrectedListOfIDs( false );
446 //=================================================================================
447 // function : SetMainShapeEntry
448 // purpose : Called to set the Main Object Entry
449 //=================================================================================
450 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
452 myMainEntry = theEntry;
453 myMainShape = GetTopoDSByEntry( theEntry );
454 myIsNotCorrected = true;
457 //=================================================================================
458 // function : GetMainShapeEntry
459 // purpose : Called to get the Main Object Entry
460 //=================================================================================
461 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
463 if ( myMainEntry == "")
464 return myEntry.toLatin1().data();
466 return myMainEntry.toLatin1().data();
469 //=================================================================================
470 // function : GetCorrectedListOfIds
471 // purpose : Called to convert the list of IDs from subshape IDs to main shape IDs
472 //=================================================================================
473 QList<int> StdMeshersGUI_SubShapeSelectorWdg::GetCorrectedListOfIDs( bool fromSubshapeToMainshape )
475 if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && fromSubshapeToMainshape )
477 else if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) && !fromSubshapeToMainshape )
478 return mySelectedIDs;
481 TopTools_IndexedMapOfShape aGeomMap;
482 TopTools_IndexedMapOfShape aMainMap;
483 TopExp::MapShapes(myGeomShape, aGeomMap);
484 TopExp::MapShapes(myMainShape, aMainMap);
486 if ( fromSubshapeToMainshape ) { // convert indexes from subshape to mainshape
487 int size = myListOfIDs.size();
488 for (int i = 0; i < size; i++) {
489 TopoDS_Shape aSubShape = aGeomMap.FindKey( myListOfIDs.at(i) );
490 int index = aMainMap.FindIndex( aSubShape );
491 aList.append( index );
493 myIsNotCorrected = false;
494 } else { // convert indexes from main shape to subshape
495 int size = mySelectedIDs.size();
496 for (int i = 0; i < size; i++) {
497 TopoDS_Shape aSubShape = aMainMap.FindKey( mySelectedIDs.at(i) );
498 int index = aGeomMap.FindIndex( aSubShape );
499 aList.append( index );