Salome HOME
PAL10953. Add SMESHGUI_ShapeByMeshDlg class files
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ShapeByMeshDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SMESHGUI_ShapeByMeshDlg.cxx
25 //  Author : Edward AGAPOV
26 //  Module : SMESH
27
28 #include "SMESHGUI_ShapeByMeshDlg.h"
29
30 #include "SMESHGUI.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"
36
37 #include "SMDS_Mesh.hxx"
38 #include "SMDS_MeshNode.hxx"
39 #include "SMESH_Actor.h"
40
41 #include "GEOMBase.h"
42 #include "GeometryGUI.h"
43
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"
53
54 // OCCT Includes
55 #include <TColStd_MapOfInteger.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <TopExp_Explorer.hxx>
58
59 // QT Includes
60 #include <qframe.h>
61 #include <qlayout.h>
62 #include <qlineedit.h>
63 #include <qpushbutton.h>
64 #include <qlabel.h>
65 #include <qradiobutton.h>
66 #include <qbuttongroup.h>
67 #include <qapplication.h>
68 #include <qstringlist.h>
69
70 #define SPACING 5
71 #define MARGIN  10
72
73 enum { EDGE = 0, FACE, VOLUME };
74
75 /*!
76  * \brief Dialog to publish a sub-shape of the mesh main shape
77  *        by selecting mesh elements
78  */
79 SMESHGUI_ShapeByMeshDlg::SMESHGUI_ShapeByMeshDlg( SMESHGUI*   theModule,
80                                                   const char* theName)
81      : QDialog( SMESH::GetDesktop( theModule ), theName, false,
82                 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
83      mySMESHGUI( theModule ),
84      mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
85 {
86   setCaption(tr("CAPTION"));
87
88   QVBoxLayout* aDlgLay = new QVBoxLayout (this, MARGIN, SPACING);
89
90   QFrame* aMainFrame = createMainFrame  (this);
91   QFrame* aBtnFrame  = createButtonFrame(this);
92
93   aDlgLay->addWidget(aMainFrame);
94   aDlgLay->addWidget(aBtnFrame);
95
96   aDlgLay->setStretchFactor(aMainFrame, 1);
97
98   myViewWindow = SMESH::GetViewWindow( mySMESHGUI );
99
100   Init();
101 }
102
103 //=======================================================================
104 // function : createMainFrame()
105 // purpose  : Create frame containing dialog's input fields
106 //=======================================================================
107 QFrame* SMESHGUI_ShapeByMeshDlg::createMainFrame (QWidget* theParent)
108 {
109   QFrame* aMainGrp = new QFrame(theParent, "main frame");
110   QGridLayout* aLayout = new QGridLayout(aMainGrp, 3, 2);
111   aLayout->setSpacing(6);
112   aLayout->setAutoAdd(false);
113
114   // elem type
115   myElemTypeGroup = new QButtonGroup(1, Qt::Vertical, aMainGrp, "Group types");
116   myElemTypeGroup->setTitle(tr("SMESH_ELEMENT_TYPE"));
117   myElemTypeGroup->setExclusive(true);
118
119   (new QRadioButton( tr("SMESH_EDGE")  , myElemTypeGroup))->setChecked(true);
120   new QRadioButton( tr("SMESH_FACE")  , myElemTypeGroup);
121   new QRadioButton( tr("SMESH_VOLUME"), myElemTypeGroup);
122
123   // element id
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 ));
128
129   // shape name
130   QLabel* aNameLabel = new QLabel( aMainGrp, "geom name label");
131   aNameLabel->setText( tr("GEOMETRY_NAME") );
132   myGeomName = new QLineEdit( aMainGrp, "geom name");
133
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);
139
140   connect(myElemTypeGroup, SIGNAL(clicked(int)), SLOT(onTypeChanged(int)));
141   connect(myElementId, SIGNAL(textChanged(const QString&)), SLOT(onElemIdChanged(const QString&)));
142
143   return aMainGrp;
144 }
145
146 //=======================================================================
147 // function : createButtonFrame()
148 // purpose  : Create frame containing buttons
149 //=======================================================================
150 QFrame* SMESHGUI_ShapeByMeshDlg::createButtonFrame (QWidget* theParent)
151 {
152   QFrame* aFrame = new QFrame(theParent);
153   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
154
155   myOkBtn    = new QPushButton(tr("SMESH_BUT_OK"   ), aFrame);
156   myCloseBtn = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
157
158   QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
159
160   QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
161
162   aLay->addWidget(myOkBtn);
163   aLay->addItem(aSpacer);
164   aLay->addWidget(myCloseBtn);
165
166   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
167   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
168
169   return aFrame;
170 }
171
172 //=======================================================================
173 // function : ~SMESHGUI_ShapeByMeshDlg()
174 // purpose  : Destructor
175 //=======================================================================
176 SMESHGUI_ShapeByMeshDlg::~SMESHGUI_ShapeByMeshDlg()
177 {
178   // no need to delete child widgets, Qt does it all for us
179 }
180
181 //=======================================================================
182 // function : Init()
183 // purpose  : Init dialog fields, connect signals and slots, show dialog
184 //=======================================================================
185 void SMESHGUI_ShapeByMeshDlg::Init()
186 {
187   SetMesh( SMESH::SMESH_Mesh::_nil() );
188   myIsManualIdEnter = false;
189
190   //erasePreview();
191
192   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
193
194   // selection and SMESHGUI
195   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
196   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
197
198   setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
199   qApp->processEvents();
200   updateGeometry();
201   adjustSize();
202   resize(minimumSize());
203
204   activateSelection();
205   onSelectionDone();
206
207   int x, y;
208   mySMESHGUI->DefineDlgPosition(this, x, y);
209   this->move(x, y);
210   this->show();
211 }
212
213 //=======================================================================
214 // function : SetMesh()
215 // purpose  : Set mesh to dialog
216 //=======================================================================
217
218 void SMESHGUI_ShapeByMeshDlg::SetMesh (SMESH::SMESH_Mesh_ptr thePtr)
219 {
220   myMesh    = SMESH::SMESH_Mesh::_duplicate(thePtr);
221   myGeomObj = GEOM::GEOM_Object::_nil();
222   myHasSolids = false;
223
224   bool isValidMesh = false;
225   vector< bool > hasElement (myElemTypeGroup->count(), false);
226   if (!myMesh->_is_nil() && myViewWindow )
227   {
228     _PTR(SObject) aSobj = SMESH::FindSObject(myMesh.in());
229     SUIT_DataOwnerPtr anIObj (new LightApp_DataOwner(aSobj->GetID().c_str()));
230     isValidMesh = mySelectionMgr->isOk(anIObj);
231
232     int nb3dShapes = 0;
233     if (isValidMesh) // check that the mesh has a shape
234     {
235       isValidMesh = false;
236       _PTR(SObject) aSO = SMESH::FindSObject(myMesh.in());
237       GEOM::GEOM_Object_var mainShape = SMESH::GetGeom(aSO);
238       if ( !mainShape->_is_nil() ) 
239       {
240         if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
241           GeometryGUI::InitGeomGen();
242         TopoDS_Shape aShape;
243         if ( GEOMBase::GetShape(mainShape, aShape)) {
244           isValidMesh = true;
245           TopExp_Explorer exp( aShape, TopAbs_SOLID );
246           myHasSolids = exp.More();
247           for ( ; exp.More(); exp.Next())
248             nb3dShapes++;
249           for ( exp.Init( aShape, TopAbs_SHELL, TopAbs_SOLID ); exp.More(); exp.Next())
250             nb3dShapes++;
251         }
252       }
253     }
254     if (isValidMesh)
255     {
256       hasElement[ EDGE ]   = myMesh->NbEdges();
257       hasElement[ FACE ]   = myMesh->NbFaces();
258       hasElement[ VOLUME ] = myMesh->NbVolumes() && nb3dShapes > 1;
259
260       if ( hasElement[ EDGE ] && myViewWindow->GetSelector() )
261       {
262         connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
263       }
264     }
265   }
266
267   // disable inexistant elem types
268   for ( int i = 0; i < myElemTypeGroup->count(); ++i ) {
269     if ( QButton* button = myElemTypeGroup->find( i ) )
270       button->setEnabled( hasElement[ i ] );
271   }
272   setElementID("");
273 }
274
275 //=======================================================================
276 // function : GetShape()
277 // purpose  : Get published sub-shape
278 //=======================================================================
279 GEOM::GEOM_Object_ptr SMESHGUI_ShapeByMeshDlg::GetShape()
280 {
281   return myGeomObj.in();
282 }
283
284 //=======================================================================
285 // function : onOk()
286 // purpose  : SLOT called when "Ok" button pressed.
287 //=======================================================================
288 void SMESHGUI_ShapeByMeshDlg::onOk()
289 {
290   try {
291     int elemID = myElementId->text().toInt();
292     myGeomObj = SMESHGUI::GetSMESHGen()->GetGeometryByMeshElement
293       ( myMesh.in(), elemID, myGeomName->text().latin1());
294
295     accept();
296     emit PublishShape();
297   }
298   catch (const SALOME::SALOME_Exception& S_ex) {
299     SalomeApp_Tools::QtCatchCorbaException(S_ex);
300   }
301   catch (...) {
302   }
303   myViewWindow->SetSelectionMode( ActorSelection );
304   disconnect(mySelectionMgr, 0, this, 0);
305   disconnect(mySMESHGUI, 0, this, 0);
306   mySMESHGUI->ResetState();
307 }
308
309 //=======================================================================
310 // function : onClose()
311 // purpose  : SLOT called when "Close" button pressed. Close dialog
312 //=======================================================================
313 void SMESHGUI_ShapeByMeshDlg::onClose()
314 {
315   myViewWindow->SetSelectionMode( ActorSelection );
316   disconnect(mySelectionMgr, 0, this, 0);
317   disconnect(mySMESHGUI, 0, this, 0);
318   mySMESHGUI->ResetState();
319   reject();
320   emit Close();
321 }
322
323 //=======================================================================
324 // function : onSelectionDone()
325 // purpose  : SLOT called when selection changed
326 //=======================================================================
327 void SMESHGUI_ShapeByMeshDlg::onSelectionDone()
328 {
329   myOkBtn->setEnabled( false );
330   setElementID("");
331
332   try {
333     SALOME_ListIO aList;
334     mySelectionMgr->selectedObjects(aList, SVTK_Viewer::Type());
335     if (aList.Extent() != 1)
336       return;
337
338     SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(aList.First());
339     if (aMesh->_is_nil() || myMesh->_is_nil() || aMesh->GetId() != myMesh->GetId() )
340       return;
341
342     QString aString;
343     int nbElems = SMESH::GetNameOfSelectedElements(myViewWindow->GetSelector(),
344                                                    aList.First(), aString);
345     if ( nbElems == 1 ) {
346       setElementID( aString );
347       myOkBtn->setEnabled( true );
348     }
349   } catch (...) {
350   }
351 }
352
353 //=======================================================================
354 // function : onDeactivate()
355 // purpose  : SLOT called when dialog must be deativated
356 //=======================================================================
357 void SMESHGUI_ShapeByMeshDlg::onDeactivate()
358 {
359   if ( isEnabled() ) {
360     //disconnect(mySelectionMgr, 0, this, 0);
361     myViewWindow->SetSelectionMode( ActorSelection );
362     setEnabled(false);
363   }
364 }
365
366 //=======================================================================
367 // function : enterEvent()
368 // purpose  : Event filter
369 //=======================================================================
370 void SMESHGUI_ShapeByMeshDlg::enterEvent (QEvent*)
371 {
372   // there is a stange problem that enterEvent() comes after onSave()
373   if ( isVisible () && !isEnabled() ) {
374     mySMESHGUI->EmitSignalDeactivateDialog();
375     setEnabled(true);
376     activateSelection();
377     //connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
378   }
379 }
380
381 //=================================================================================
382 // function : closeEvent()
383 // purpose  : Close dialog box
384 //=================================================================================
385 void SMESHGUI_ShapeByMeshDlg::closeEvent (QCloseEvent*)
386 {
387   onClose();
388 }
389
390 //=======================================================================
391 // function : activateSelection()
392 // purpose  : Activate selection in accordance with current pattern type
393 //=======================================================================
394 void SMESHGUI_ShapeByMeshDlg::activateSelection()
395 {
396   mySelectionMgr->clearFilters();
397   SMESH::SetPointRepresentation(false);
398
399   myGeomName->setText("");
400
401   if ( myViewWindow )
402   {
403     QString geomName;
404     Selection_Mode mode = EdgeSelection;
405     switch ( myElemTypeGroup->id( myElemTypeGroup->selected() )) {
406     case EDGE  :
407       mode = EdgeSelection;   geomName = tr("GEOM_EDGE"); break;
408     case FACE  :
409       mode = FaceSelection;   geomName = tr("GEOM_FACE"); break;
410     case VOLUME:
411       mode = VolumeSelection; geomName = tr(myHasSolids ? "GEOM_SOLID" : "GEOM_SHELL"); break;
412     default: return;
413     }
414     if ( myViewWindow->SelectionMode() != mode )
415       myViewWindow->SetSelectionMode( mode );
416
417     myGeomName->setText( GEOMBase::GetDefaultName( geomName ));
418   }
419 }
420
421 //=======================================================================
422 //function : onTypeChanged
423 //purpose  : SLOT. Called when element type changed.
424 //=======================================================================
425
426 void SMESHGUI_ShapeByMeshDlg::onTypeChanged (int theType)
427 {
428   setElementID("");
429   activateSelection();
430 }
431
432 //=======================================================================
433 //function : onTypeChanged
434 //purpose  : SLOT. Called when element id is entered
435 //           Highlight the element whose Ids the user entered manually
436 //=======================================================================
437
438 void SMESHGUI_ShapeByMeshDlg::onElemIdChanged(const QString& theNewText)
439 {
440   if ( myIsManualIdEnter && !myMesh->_is_nil() && myViewWindow )
441     if ( SMESH_Actor* actor = SMESH::FindActorByObject(myMesh) )
442       if ( SMDS_Mesh* aMesh = actor->GetObject()->GetMesh() )
443       {
444         TColStd_MapOfInteger newIndices;
445         QStringList aListId = QStringList::split( " ", theNewText, false);
446         for ( int i = 0; i < aListId.count(); i++ ) {
447           if ( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ))
448             newIndices.Add(n->GetID());
449         }
450
451         if ( !newIndices.IsEmpty() )
452           if ( SVTK_Selector* s = myViewWindow->GetSelector() ) {
453             s->AddOrRemoveIndex( actor->getIO(), newIndices, false );
454             myViewWindow->highlight( actor->getIO(), true, true );
455           }
456       }
457 }
458
459 //=======================================================================
460 //function : setElementID
461 //purpose  : programmatically set element id
462 //=======================================================================
463
464 void SMESHGUI_ShapeByMeshDlg::setElementID(const QString& theText)
465 {
466   myIsManualIdEnter = false;
467   myElementId->setText(theText);
468   myIsManualIdEnter = true;
469 }