1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESHGUI_ShapeByMeshDlg.cxx
25 // Author : Edward AGAPOV
28 #include "SMESHGUI_ShapeByMeshDlg.h"
31 #include "SMESHGUI_GEOMGenUtils.h"
32 #include "SMESHGUI_IdValidator.h"
33 #include "SMESHGUI_MeshUtils.h"
34 #include "SMESHGUI_Utils.h"
35 #include "SMESHGUI_VTKUtils.h"
37 #include "SMDS_Mesh.hxx"
38 #include "SMDS_MeshNode.hxx"
39 #include "SMESH_Actor.h"
42 #include "GeometryGUI.h"
44 #include "LightApp_DataOwner.h"
45 #include "LightApp_SelectionMgr.h"
46 #include "SALOMEDSClient_SObject.hxx"
47 #include "SALOME_ListIO.hxx"
48 #include "SUIT_Desktop.h"
49 #include "SVTK_Selector.h"
50 #include "SVTK_ViewWindow.h"
51 #include "SVTK_ViewModel.h"
52 #include "SalomeApp_Tools.h"
55 #include <TColStd_MapOfInteger.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <TopExp_Explorer.hxx>
62 #include <qlineedit.h>
63 #include <qpushbutton.h>
65 #include <qradiobutton.h>
66 #include <qbuttongroup.h>
67 #include <qapplication.h>
68 #include <qstringlist.h>
73 enum { EDGE = 0, FACE, VOLUME };
76 * \brief Dialog to publish a sub-shape of the mesh main shape
77 * by selecting mesh elements
79 SMESHGUI_ShapeByMeshDlg::SMESHGUI_ShapeByMeshDlg( SMESHGUI* theModule,
81 : QDialog( SMESH::GetDesktop( theModule ), theName, false,
82 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
83 mySMESHGUI( theModule ),
84 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
86 setCaption(tr("CAPTION"));
88 QVBoxLayout* aDlgLay = new QVBoxLayout (this, MARGIN, SPACING);
90 QFrame* aMainFrame = createMainFrame (this);
91 QFrame* aBtnFrame = createButtonFrame(this);
93 aDlgLay->addWidget(aMainFrame);
94 aDlgLay->addWidget(aBtnFrame);
96 aDlgLay->setStretchFactor(aMainFrame, 1);
98 myViewWindow = SMESH::GetViewWindow( mySMESHGUI );
103 //=======================================================================
104 // function : createMainFrame()
105 // purpose : Create frame containing dialog's input fields
106 //=======================================================================
107 QFrame* SMESHGUI_ShapeByMeshDlg::createMainFrame (QWidget* theParent)
109 QFrame* aMainGrp = new QFrame(theParent, "main frame");
110 QGridLayout* aLayout = new QGridLayout(aMainGrp, 3, 2);
111 aLayout->setSpacing(6);
112 aLayout->setAutoAdd(false);
115 myElemTypeGroup = new QButtonGroup(1, Qt::Vertical, aMainGrp, "Group types");
116 myElemTypeGroup->setTitle(tr("SMESH_ELEMENT_TYPE"));
117 myElemTypeGroup->setExclusive(true);
119 (new QRadioButton( tr("SMESH_EDGE") , myElemTypeGroup))->setChecked(true);
120 new QRadioButton( tr("SMESH_FACE") , myElemTypeGroup);
121 new QRadioButton( tr("SMESH_VOLUME"), myElemTypeGroup);
124 QLabel* anIdLabel = new QLabel( aMainGrp, "element id label");
125 anIdLabel->setText( tr("ELEMENT_ID") );
126 myElementId = new QLineEdit( aMainGrp, "element id");
127 myElementId->setValidator( new SMESHGUI_IdValidator( theParent, "id validator", 1 ));
130 QLabel* aNameLabel = new QLabel( aMainGrp, "geom name label");
131 aNameLabel->setText( tr("GEOMETRY_NAME") );
132 myGeomName = new QLineEdit( aMainGrp, "geom name");
134 aLayout->addMultiCellWidget(myElemTypeGroup, 0, 0, 0, 1);
135 aLayout->addWidget(anIdLabel, 1, 0);
136 aLayout->addWidget(myElementId, 1, 1);
137 aLayout->addWidget(aNameLabel, 2, 0);
138 aLayout->addWidget(myGeomName, 2, 1);
140 connect(myElemTypeGroup, SIGNAL(clicked(int)), SLOT(onTypeChanged(int)));
141 connect(myElementId, SIGNAL(textChanged(const QString&)), SLOT(onElemIdChanged(const QString&)));
146 //=======================================================================
147 // function : createButtonFrame()
148 // purpose : Create frame containing buttons
149 //=======================================================================
150 QFrame* SMESHGUI_ShapeByMeshDlg::createButtonFrame (QWidget* theParent)
152 QFrame* aFrame = new QFrame(theParent);
153 aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
155 myOkBtn = new QPushButton(tr("SMESH_BUT_OK" ), aFrame);
156 myCloseBtn = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
158 QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
160 QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
162 aLay->addWidget(myOkBtn);
163 aLay->addItem(aSpacer);
164 aLay->addWidget(myCloseBtn);
166 connect(myOkBtn, SIGNAL(clicked()), SLOT(onOk()));
167 connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
172 //=======================================================================
173 // function : ~SMESHGUI_ShapeByMeshDlg()
174 // purpose : Destructor
175 //=======================================================================
176 SMESHGUI_ShapeByMeshDlg::~SMESHGUI_ShapeByMeshDlg()
178 // no need to delete child widgets, Qt does it all for us
181 //=======================================================================
183 // purpose : Init dialog fields, connect signals and slots, show dialog
184 //=======================================================================
185 void SMESHGUI_ShapeByMeshDlg::Init()
187 SetMesh( SMESH::SMESH_Mesh::_nil() );
188 myIsManualIdEnter = false;
192 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
194 // selection and SMESHGUI
195 connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
196 connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
198 setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
199 qApp->processEvents();
202 resize(minimumSize());
208 mySMESHGUI->DefineDlgPosition(this, x, y);
213 //=======================================================================
214 // function : SetMesh()
215 // purpose : Set mesh to dialog
216 //=======================================================================
218 void SMESHGUI_ShapeByMeshDlg::SetMesh (SMESH::SMESH_Mesh_ptr thePtr)
220 myMesh = SMESH::SMESH_Mesh::_duplicate(thePtr);
221 myGeomObj = GEOM::GEOM_Object::_nil();
224 vector< bool > hasElement (myElemTypeGroup->count(), false);
225 if (!myMesh->_is_nil() && myViewWindow )
227 _PTR(SObject) aSobj = SMESH::FindSObject(myMesh.in());
228 SUIT_DataOwnerPtr anIObj (new LightApp_DataOwner(aSobj->GetID().c_str()));
230 vector< int > nbShapes( TopAbs_SHAPE, 0 );
231 int shapeDim = 0; // max dim with several shapes
232 if ( mySelectionMgr->isOk(anIObj) ) // check that the mesh has a valid shape
234 _PTR(SObject) aSO = SMESH::FindSObject(myMesh.in());
235 GEOM::GEOM_Object_var mainShape = SMESH::GetGeom(aSO);
236 if ( !mainShape->_is_nil() )
238 if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
239 GeometryGUI::InitGeomGen();
241 if ( GEOMBase::GetShape(mainShape, aShape))
243 TopAbs_ShapeEnum types[4] = { TopAbs_EDGE, TopAbs_FACE, TopAbs_SHELL, TopAbs_SOLID };
244 for ( int dim = 4; dim > 0; --dim ) {
245 TopAbs_ShapeEnum type = types[ dim - 1 ];
246 TopAbs_ShapeEnum avoid = ( type == TopAbs_SHELL ) ? TopAbs_SOLID : TopAbs_SHAPE;
247 TopExp_Explorer exp( aShape, type, avoid );
248 for ( ; nbShapes[ type ] < 2 && exp.More(); exp.Next() )
250 if ( nbShapes[ type ] > 1 ) {
260 if ( nbShapes[ TopAbs_SHELL ] + nbShapes[ TopAbs_SOLID ] > 1 )
262 hasElement[ EDGE ] = shapeDim > 0 && myMesh->NbEdges() ;
263 hasElement[ FACE ] = shapeDim > 1 && myMesh->NbFaces() ;
264 hasElement[ VOLUME ] = shapeDim > 2 && myMesh->NbVolumes();
266 if ( hasElement[ EDGE ] && myViewWindow->GetSelector() )
268 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
271 myHasSolids = nbShapes[ TopAbs_SOLID ];
274 // disable inexistant elem types
275 for ( int i = 0; i < myElemTypeGroup->count(); ++i ) {
276 if ( QButton* button = myElemTypeGroup->find( i ) )
277 button->setEnabled( hasElement[ i ] );
279 myElementId->setEnabled( hasElement[ EDGE ] );
280 myGeomName-> setEnabled( hasElement[ EDGE ] );
285 //=======================================================================
286 // function : GetShape()
287 // purpose : Get published sub-shape
288 //=======================================================================
289 GEOM::GEOM_Object_ptr SMESHGUI_ShapeByMeshDlg::GetShape()
291 return myGeomObj.in();
294 //=======================================================================
296 // purpose : SLOT called when "Ok" button pressed.
297 //=======================================================================
298 void SMESHGUI_ShapeByMeshDlg::onOk()
301 int elemID = myElementId->text().toInt();
302 myGeomObj = SMESHGUI::GetSMESHGen()->GetGeometryByMeshElement
303 ( myMesh.in(), elemID, myGeomName->text().latin1());
308 catch (const SALOME::SALOME_Exception& S_ex) {
309 SalomeApp_Tools::QtCatchCorbaException(S_ex);
313 myViewWindow->SetSelectionMode( ActorSelection );
314 disconnect(mySelectionMgr, 0, this, 0);
315 disconnect(mySMESHGUI, 0, this, 0);
316 mySMESHGUI->ResetState();
319 //=======================================================================
320 // function : onClose()
321 // purpose : SLOT called when "Close" button pressed. Close dialog
322 //=======================================================================
323 void SMESHGUI_ShapeByMeshDlg::onClose()
325 myViewWindow->SetSelectionMode( ActorSelection );
326 disconnect(mySelectionMgr, 0, this, 0);
327 disconnect(mySMESHGUI, 0, this, 0);
328 mySMESHGUI->ResetState();
333 //=======================================================================
334 // function : onSelectionDone()
335 // purpose : SLOT called when selection changed
336 //=======================================================================
337 void SMESHGUI_ShapeByMeshDlg::onSelectionDone()
339 myOkBtn->setEnabled( false );
344 mySelectionMgr->selectedObjects(aList, SVTK_Viewer::Type());
345 if (aList.Extent() != 1)
348 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(aList.First());
349 if (aMesh->_is_nil() || myMesh->_is_nil() || aMesh->GetId() != myMesh->GetId() )
353 int nbElems = SMESH::GetNameOfSelectedElements(myViewWindow->GetSelector(),
354 aList.First(), aString);
355 if ( nbElems == 1 ) {
356 setElementID( aString );
357 myOkBtn->setEnabled( true );
363 //=======================================================================
364 // function : onDeactivate()
365 // purpose : SLOT called when dialog must be deativated
366 //=======================================================================
367 void SMESHGUI_ShapeByMeshDlg::onDeactivate()
370 //disconnect(mySelectionMgr, 0, this, 0);
371 myViewWindow->SetSelectionMode( ActorSelection );
376 //=======================================================================
377 // function : enterEvent()
378 // purpose : Event filter
379 //=======================================================================
380 void SMESHGUI_ShapeByMeshDlg::enterEvent (QEvent*)
382 // there is a stange problem that enterEvent() comes after onSave()
383 if ( isVisible () && !isEnabled() ) {
384 mySMESHGUI->EmitSignalDeactivateDialog();
387 //connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
391 //=================================================================================
392 // function : closeEvent()
393 // purpose : Close dialog box
394 //=================================================================================
395 void SMESHGUI_ShapeByMeshDlg::closeEvent (QCloseEvent*)
400 //=======================================================================
401 // function : activateSelection()
402 // purpose : Activate selection in accordance with current pattern type
403 //=======================================================================
404 void SMESHGUI_ShapeByMeshDlg::activateSelection()
406 mySelectionMgr->clearFilters();
407 SMESH::SetPointRepresentation(false);
409 myGeomName->setText("");
414 Selection_Mode mode = EdgeSelection;
415 switch ( myElemTypeGroup->id( myElemTypeGroup->selected() )) {
417 mode = EdgeSelection; geomName = tr("GEOM_EDGE"); break;
419 mode = FaceSelection; geomName = tr("GEOM_FACE"); break;
421 mode = VolumeSelection; geomName = tr(myHasSolids ? "GEOM_SOLID" : "GEOM_SHELL"); break;
424 if ( myViewWindow->SelectionMode() != mode )
425 myViewWindow->SetSelectionMode( mode );
427 myGeomName->setText( GEOMBase::GetDefaultName( geomName ));
431 //=======================================================================
432 //function : onTypeChanged
433 //purpose : SLOT. Called when element type changed.
434 //=======================================================================
436 void SMESHGUI_ShapeByMeshDlg::onTypeChanged (int theType)
442 //=======================================================================
443 //function : onTypeChanged
444 //purpose : SLOT. Called when element id is entered
445 // Highlight the element whose Ids the user entered manually
446 //=======================================================================
448 void SMESHGUI_ShapeByMeshDlg::onElemIdChanged(const QString& theNewText)
450 myOkBtn->setEnabled( false );
452 if ( myIsManualIdEnter && !myMesh->_is_nil() && myViewWindow )
453 if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) )
454 if ( SMDS_Mesh* aMesh = actor->GetObject()->GetMesh() )
456 SMDSAbs_ElementType type = SMDSAbs_Edge;
457 switch ( myElemTypeGroup->id( myElemTypeGroup->selected() )) {
458 case EDGE : type = SMDSAbs_Edge; break;
459 case FACE : type = SMDSAbs_Face; break;
460 case VOLUME: type = SMDSAbs_Volume; break;
463 TColStd_MapOfInteger newIndices;
464 QStringList aListId = QStringList::split( " ", theNewText, false);
465 for ( int i = 0; i < aListId.count(); i++ ) {
466 if ( const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() ))
467 if ( e->GetType() == type )
468 newIndices.Add( e->GetID() );
471 if ( !newIndices.IsEmpty() && newIndices.Extent() == 1 )
472 if ( SVTK_Selector* s = myViewWindow->GetSelector() ) {
473 s->AddOrRemoveIndex( actor->getIO(), newIndices, false );
474 myViewWindow->highlight( actor->getIO(), true, true );
475 myOkBtn->setEnabled( true );
480 //=======================================================================
481 //function : setElementID
482 //purpose : programmatically set element id
483 //=======================================================================
485 void SMESHGUI_ShapeByMeshDlg::setElementID(const QString& theText)
487 myIsManualIdEnter = false;
488 myElementId->setText(theText);
489 myIsManualIdEnter = true;