Salome HOME
IMP 22792: EDF 8159 SMESH: Multi-dimensional extrusion/extrusion along a path/revolution
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_DisplayEntitiesDlg.cxx
1 // Copyright (C) 2014-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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, or (at your option) any later version.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //  File   : SMESHGUI_DisplayEntitiesDlg.cxx
20 //  Author : Alexander KOVALEV, Open CASCADE S.A.S. (alexander.kovalev@opencascade.com)
21
22 #include "SMESHGUI_DisplayEntitiesDlg.h"
23
24 #include "SMESHGUI.h"
25 #include "SMESHGUI_MeshUtils.h"
26 #include "SMESHGUI_Utils.h"
27 #include "SMESHGUI_VTKUtils.h"
28
29 #include <LightApp_Application.h>
30 #include <LightApp_SelectionMgr.h>
31 #include <SALOME_ListIO.hxx>
32 #include <SUIT_MessageBox.h>
33 #include <SUIT_OverrideCursor.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_Session.h>
36
37 #include <QCheckBox>
38 #include <QGridLayout>
39 #include <QGroupBox>
40 #include <QLabel>
41 #include <QVBoxLayout>
42
43 const int MARGIN  = 9;
44 const int SPACING = 6;
45
46 /*!
47   \class SMESHGUI_DisplayEntitiesDlg
48   \brief Dialog box to select entities to be displayed in viewer
49 */
50
51 /*
52   \brief Constructor
53   \param parent parent widget
54 */
55 SMESHGUI_DisplayEntitiesDlg::SMESHGUI_DisplayEntitiesDlg( QWidget* parent )
56   : SMESHGUI_Dialog( parent, true, false, Standard )
57 {
58   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
59
60   LightApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
61   SALOME_ListIO selected;
62   mgr->selectedObjects( selected );
63   SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_nil();
64   myActor = 0;
65   myNbCheckedButtons = 0;
66
67   SALOME_ListIteratorOfListIO it( selected );
68   myIObject = selected.First();
69   if ( myIObject->hasEntry() ) {
70     myActor = SMESH::FindActorByEntry( myIObject->getEntry() );
71   }
72   myEntityMode = myActor ? myActor->GetEntityMode() : 0;
73
74   aMesh = SMESH::GetMeshByIO( myIObject );
75
76   // set title
77   setWindowTitle( tr( "MEN_DISP_ENT" ) );
78
79   // create widgets
80   QGroupBox* anEntitiesGrp = new QGroupBox( tr( "SMESH_MESHINFO_ENTITIES" ), mainFrame() );
81   QGridLayout* hl = new QGridLayout( anEntitiesGrp );
82   hl->setMargin( MARGIN );
83   hl->setSpacing( SPACING );
84   int nbElements;
85
86   // 0DElements
87   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_0DElement ) : aMesh->Nb0DElements();
88   my0DElemsTB = new QCheckBox( tr("SMESH_ELEMS0D"), anEntitiesGrp );
89   my0DElemsTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_ELEM0D" ) ) ) );
90   bool has0DElems = myEntityMode & SMESH_Actor::e0DElements;
91   my0DElemsTB->setChecked( has0DElems );
92   if ( has0DElems )
93     myNbCheckedButtons++;
94   connect( my0DElemsTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
95   QLabel* nb0DElemsLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
96   hl->addWidget( my0DElemsTB, 0, 0 );
97   hl->addWidget( nb0DElemsLab, 0, 1 );
98   my0DElemsTB->setEnabled( nbElements );
99   nb0DElemsLab->setEnabled( nbElements );
100
101   // Edges
102   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_Edge ) : aMesh->NbEdges();
103   myEdgesTB = new QCheckBox( tr("SMESH_EDGES"), anEntitiesGrp );
104   myEdgesTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_EDGE" ) ) ) );
105   bool hasEdges = myEntityMode & SMESH_Actor::eEdges;
106   myEdgesTB->setChecked( hasEdges );
107   if ( hasEdges )
108     myNbCheckedButtons++;
109   connect( myEdgesTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
110   QLabel* nbEdgesLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
111   hl->addWidget( myEdgesTB, 1, 0 );
112   hl->addWidget( nbEdgesLab, 1, 1 );
113   myEdgesTB->setEnabled( nbElements );
114   nbEdgesLab->setEnabled( nbElements );
115
116   // Faces
117   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_Face ) : aMesh->NbFaces();
118   myFacesTB = new QCheckBox( tr("SMESH_FACES"), anEntitiesGrp );
119   myFacesTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_TRIANGLE" ) ) ) );
120   bool hasFaces = myEntityMode & SMESH_Actor::eFaces;
121   myFacesTB->setChecked( hasFaces );
122   if ( hasFaces )
123     myNbCheckedButtons++;
124   connect( myFacesTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
125   QLabel* nbFacesLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
126   hl->addWidget( myFacesTB, 2, 0 );
127   hl->addWidget( nbFacesLab, 2, 1 );
128   myFacesTB->setEnabled( nbElements );
129   nbFacesLab->setEnabled( nbElements );
130
131   // Volumes
132   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_Volume ) : aMesh->NbVolumes();
133   myVolumesTB = new QCheckBox( tr("SMESH_VOLUMES"), anEntitiesGrp );
134   myVolumesTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_TETRAS" ) ) ) );
135   bool hasVolumes = myEntityMode & SMESH_Actor::eVolumes;
136   myVolumesTB->setChecked( hasVolumes );
137   if ( hasVolumes )
138     myNbCheckedButtons++;
139   connect( myVolumesTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool) ) );
140   QLabel* nbVolumesLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
141   hl->addWidget( myVolumesTB, 3, 0 );
142   hl->addWidget( nbVolumesLab, 3, 1 );
143   myVolumesTB->setEnabled( nbElements );
144   nbVolumesLab->setEnabled( nbElements );
145
146   // Balls
147   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_Ball ) : aMesh->NbBalls();
148   myBallsTB = new QCheckBox( tr("SMESH_BALLS"), anEntitiesGrp );
149   myBallsTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_BALL" ) ) ) );
150   bool hasBalls = myEntityMode & SMESH_Actor::eBallElem;
151   myBallsTB->setChecked( hasBalls );
152   if ( hasBalls )
153     myNbCheckedButtons++;
154   connect( myBallsTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
155   QLabel* nbBallsLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
156   hl->addWidget( myBallsTB, 4, 0 );
157   hl->addWidget( nbBallsLab, 4, 1 );
158   myBallsTB->setEnabled( nbElements );
159   nbBallsLab->setEnabled( nbElements );
160
161   QVBoxLayout* aDlgLay = new QVBoxLayout( mainFrame() );
162   aDlgLay->setMargin( 0 );
163   aDlgLay->setSpacing( SPACING );
164   aDlgLay->addWidget( anEntitiesGrp );
165   
166   button( OK )->setText( tr( "SMESH_BUT_OK" ) );
167
168   connect( this, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
169   connect( this, SIGNAL( dlgOk() ),   this, SLOT( onOk() ) );
170 }
171
172 /*
173   \brief Destructor: clean-up resources if necessary
174 */
175 SMESHGUI_DisplayEntitiesDlg::~SMESHGUI_DisplayEntitiesDlg()
176 {
177 }
178
179 void SMESHGUI_DisplayEntitiesDlg::InverseEntityMode(unsigned int& theOutputMode,
180                                                     unsigned int theMode)
181 {
182   bool anIsNotPresent = ~theOutputMode & theMode;
183   if(anIsNotPresent)
184     theOutputMode |= theMode;
185   else
186     theOutputMode &= ~theMode;
187 }
188
189 /*!
190   \brief Slot for changing entities state
191 */
192 void SMESHGUI_DisplayEntitiesDlg::onChangeEntityMode( bool isChecked )
193 {
194   QCheckBox* aSender = (QCheckBox*)sender();
195   if ( myNbCheckedButtons == 1 && !isChecked ) {
196     SUIT_MessageBox::warning(this, tr("SMESH_WRN_WARNING"),
197                              tr("WRN_AT_LEAST_ONE"));
198     disconnect( aSender, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
199     aSender->setChecked( true );
200     connect( aSender, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
201     return;
202   }
203   if ( my0DElemsTB == aSender )
204     InverseEntityMode( myEntityMode, SMESH_Actor::e0DElements );
205   else if ( myEdgesTB == aSender )
206     InverseEntityMode( myEntityMode, SMESH_Actor::eEdges );
207   else if ( myFacesTB == aSender )
208     InverseEntityMode( myEntityMode, SMESH_Actor::eFaces );
209   else if ( myVolumesTB == aSender )
210     InverseEntityMode( myEntityMode, SMESH_Actor::eVolumes );
211   else if ( myBallsTB == aSender )
212     InverseEntityMode( myEntityMode, SMESH_Actor::eBallElem );
213   
214   isChecked ? myNbCheckedButtons++ : myNbCheckedButtons--;
215   
216 }
217
218 /*!
219   \brief Show online help on dialog box
220 */
221 void SMESHGUI_DisplayEntitiesDlg::onHelp()
222 {
223   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
224   app->onHelpContextModule( "SMESH", "display_entity_page.html" );
225 }
226
227 /*!
228   \brief Display or update the mesh in the 3D view with selected entity mode
229 */
230 void SMESHGUI_DisplayEntitiesDlg::onOk()
231 {
232   SUIT_OverrideCursor wc;
233
234   const char* entry = myIObject->getEntry();
235   
236   if ( !myActor ) {
237     myActor = SMESH::CreateActor(SMESH::GetActiveStudyDocument(), 
238                                  entry, true);
239   }
240
241   if( myEntityMode != myActor->GetEntityMode() ) {
242     myActor->SetEntityMode(myEntityMode);
243     SUIT_ViewWindow* wnd = SMESH::GetActiveWindow();
244     SMESH::DisplayActor( wnd, myActor );
245     SUIT_DataOwnerPtrList aList;
246     aList.append( new LightApp_DataOwner( entry ) );
247     SMESHGUI::selectionMgr()->setSelected( aList, false );
248     SMESH::UpdateView( wnd, SMESH::eDisplay, entry );
249   }
250 }