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)
26 #include "StdMeshersGUI_SubShapeSelectorWdg.h"
29 #include <SMESH_Type.h>
30 #include "SMESHGUI_MeshUtils.h"
31 #include <SMESH_Actor.h>
32 #include <SMESH_PreviewActorsCollection.h>
33 #include <SMESH_ActorUtils.h>
34 #include "SMESHGUI_GroupUtils.h"
35 #include "SMESH_Gen_i.hxx"
36 #include "SMESHGUI_GEOMGenUtils.h"
39 #include <SVTK_ViewWindow.h>
40 #include <SVTK_ViewModel.h>
41 #include <SVTK_ViewWindow.h>
42 #include <SVTK_Selector.h>
44 // SALOME GUI includes
45 #include <SALOME_ListIO.hxx>
46 #include <LightApp_SelectionMgr.h>
47 #include <SALOME_ListIteratorOfListIO.hxx>
50 #include <SUIT_ResourceMgr.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>
69 // SALOME KERNEL includes
70 #include <SALOMEDS_SObject.hxx>
75 //================================================================================
79 //================================================================================
81 StdMeshersGUI_SubShapeSelectorWdg
82 ::StdMeshersGUI_SubShapeSelectorWdg( QWidget * parent ):
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 myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
97 edgesLayout->addWidget(myListWidget, 0, 0, 3, 3);
98 edgesLayout->addWidget(myAddButton, 0, 4);
99 edgesLayout->addWidget(myRemoveButton, 1, 4);
101 edgesLayout->setRowStretch(2, 5);
102 edgesLayout->setColumnStretch(2, 5);
104 setLayout( edgesLayout );
105 setMinimumWidth( 300 );
108 mySubShType = TopAbs_EDGE;
113 //================================================================================
117 //================================================================================
119 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
121 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
122 if ( myPreviewActor ) {
123 myPreviewActor->RemoveFromRender( myRenderer );
124 aViewWindow->Repaint();
126 delete myPreviewActor;
132 myMainShape.Nullify();
135 //================================================================================
137 * Create a layout, initialize fields
139 //================================================================================
141 void StdMeshersGUI_SubShapeSelectorWdg::init()
145 mySelectedIDs.clear();
147 mySMESHGUI = SMESHGUI::GetSMESHGUI();
148 mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
149 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
151 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
152 aViewWindow->SetSelectionMode( ActorSelection );
154 connect( myAddButton, SIGNAL(clicked()), SLOT(onAdd()));
155 connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
157 connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
158 connect( myListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
163 //================================================================================
165 * Create a layout, initialize fields
167 //================================================================================
169 void StdMeshersGUI_SubShapeSelectorWdg::showPreview( bool visible)
171 if ( !myPreviewActor )
174 if ( myIsShown != visible ) {
175 myPreviewActor->SetShown( visible );
177 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
178 aViewWindow->Repaint();
184 //=================================================================================
185 // function : SelectionIntoArgument()
186 // purpose : Called when selection as changed or other case
187 //=================================================================================
188 void StdMeshersGUI_SubShapeSelectorWdg::SelectionIntoArgument()
190 if ( !myPreviewActor )
193 mySelectedIDs.clear();
197 mySelectionMgr->selectedObjects( aList );
198 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 //=================================================================================
262 // function : onAdd()
263 // purpose : Called when Add Button Clicked
264 //=================================================================================
265 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
267 if ( mySelectedIDs.size() < 1 )
270 myListWidget->blockSignals( true );
271 for (int i = 0; i < mySelectedIDs.size(); i++) {
272 if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
273 QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
275 QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
276 anItem->setSelected(true);
278 myListOfIDs.append( mySelectedIDs.at(i) );
281 onListSelectionChanged();
283 myListWidget->blockSignals( false );
285 if( myListOfIDs.size() >= myMaxSize )
286 myAddButton->setEnabled( false );
289 //=================================================================================
290 // function : onRemove()
291 // purpose : Called when Remove Button Clicked
292 //=================================================================================
293 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
295 if ( myListWidget->count() < 1 )
298 myListWidget->blockSignals( true );
299 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
300 QListWidgetItem* item;
301 foreach(item, selItems) {
302 QString idStr = item->text();
303 int id = idStr.toInt();
305 int index = myListOfIDs.indexOf( id );
306 myListOfIDs.removeAt( index );
310 onListSelectionChanged();
311 myListWidget->blockSignals( false );
313 myAddButton->setEnabled( true );
316 //=================================================================================
317 // function : onListSelectionChanged()
318 // purpose : Called when selection in element list is changed
319 //=================================================================================
320 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
322 if ( !myPreviewActor )
325 mySelectionMgr->clearSelected();
326 TColStd_MapOfInteger aIndexes;
327 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
328 QListWidgetItem* anItem;
329 foreach(anItem, selItems)
330 myPreviewActor->HighlightID( anItem->text().toInt() );
333 //=================================================================================
334 // function : setGeomShape
335 // purpose : Called to set geometry
336 //================================================================================
337 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
339 if ( theEntry != "") {
340 myParamValue = theEntry;
342 myMainShape = GetTopoDSByEntry( theEntry );
347 //=================================================================================
348 // function : updateState
349 // purpose : update Widget state
350 //=================================================================================
351 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
354 if ( !myMainShape.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( myMainShape, TopAbs_EDGE, myEntry );
365 myPreviewActor->Init( myMainShape, mySubShType, myEntry );
366 myPreviewActor->SetShown( false );
368 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
369 myRenderer = aViewWindow->getRenderer();
370 myPreviewActor->AddToRender( myRenderer );
371 aViewWindow->Repaint();
376 //=================================================================================
377 // function : GetGeomObjectByEntry
378 // purpose : Called to get GeomObject
379 //=================================================================================
380 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
382 GEOM::GEOM_Object_var aGeomObj;
383 SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
385 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
386 SALOMEDS::GenericAttribute_var anAttr;
388 if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
389 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
390 CORBA::String_var aVal = anIOR->Value();
391 CORBA::Object_var obj = aStudy->ConvertIORToObject(aVal);
392 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
398 //=================================================================================
399 // function : setObjectByEntry
400 // purpose : Called to get GeomObject
401 //=================================================================================
402 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
405 GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
406 GEOMBase::GetShape(aGeomObj, shape);
410 //=================================================================================
411 // function : GetListOfIds
412 // purpose : Called to get the list of SubShapes IDs
413 //=================================================================================
414 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
416 SMESH::long_array_var anArray = new SMESH::long_array;
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 ] );