Salome HOME
Redesign SALOME documentation
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_DisplayEntitiesDlg.cxx
1 // Copyright (C) 2014-2016  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   myNbTypes = ( nbElements > 0 );
101
102   // Edges
103   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_Edge ) : aMesh->NbEdges();
104   myEdgesTB = new QCheckBox( tr("SMESH_EDGES"), anEntitiesGrp );
105   myEdgesTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_EDGE" ) ) ) );
106   bool hasEdges = myEntityMode & SMESH_Actor::eEdges;
107   myEdgesTB->setChecked( hasEdges );
108   if ( hasEdges )
109     myNbCheckedButtons++;
110   connect( myEdgesTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
111   QLabel* nbEdgesLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
112   hl->addWidget( myEdgesTB, 1, 0 );
113   hl->addWidget( nbEdgesLab, 1, 1 );
114   myEdgesTB->setEnabled( nbElements );
115   nbEdgesLab->setEnabled( nbElements );
116   myNbTypes += ( nbElements > 0 );
117
118   // Faces
119   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_Face ) : aMesh->NbFaces();
120   myFacesTB = new QCheckBox( tr("SMESH_FACES"), anEntitiesGrp );
121   myFacesTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_TRIANGLE" ) ) ) );
122   bool hasFaces = myEntityMode & SMESH_Actor::eFaces;
123   myFacesTB->setChecked( hasFaces );
124   if ( hasFaces )
125     myNbCheckedButtons++;
126   connect( myFacesTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
127   QLabel* nbFacesLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
128   hl->addWidget( myFacesTB, 2, 0 );
129   hl->addWidget( nbFacesLab, 2, 1 );
130   myFacesTB->setEnabled( nbElements );
131   nbFacesLab->setEnabled( nbElements );
132   myNbTypes += ( nbElements > 0 );
133
134   // Volumes
135   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_Volume ) : aMesh->NbVolumes();
136   myVolumesTB = new QCheckBox( tr("SMESH_VOLUMES"), anEntitiesGrp );
137   myVolumesTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_TETRAS" ) ) ) );
138   bool hasVolumes = myEntityMode & SMESH_Actor::eVolumes;
139   myVolumesTB->setChecked( hasVolumes );
140   if ( hasVolumes )
141     myNbCheckedButtons++;
142   connect( myVolumesTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool) ) );
143   QLabel* nbVolumesLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
144   hl->addWidget( myVolumesTB, 3, 0 );
145   hl->addWidget( nbVolumesLab, 3, 1 );
146   myVolumesTB->setEnabled( nbElements );
147   nbVolumesLab->setEnabled( nbElements );
148   myNbTypes += ( nbElements > 0 );
149
150   // Balls
151   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_Ball ) : aMesh->NbBalls();
152   myBallsTB = new QCheckBox( tr("SMESH_BALLS"), anEntitiesGrp );
153   myBallsTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_BALL" ) ) ) );
154   bool hasBalls = myEntityMode & SMESH_Actor::eBallElem;
155   myBallsTB->setChecked( hasBalls );
156   if ( hasBalls )
157     myNbCheckedButtons++;
158   connect( myBallsTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
159   QLabel* nbBallsLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
160   hl->addWidget( myBallsTB, 4, 0 );
161   hl->addWidget( nbBallsLab, 4, 1 );
162   myBallsTB->setEnabled( nbElements );
163   nbBallsLab->setEnabled( nbElements );
164   myNbTypes += ( nbElements > 0 );
165
166   QVBoxLayout* aDlgLay = new QVBoxLayout( mainFrame() );
167   aDlgLay->setMargin( 0 );
168   aDlgLay->setSpacing( SPACING );
169   aDlgLay->addWidget( anEntitiesGrp );
170   
171   button( OK )->setText( tr( "SMESH_BUT_OK" ) );
172
173   connect( this, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ));
174   connect( this, SIGNAL( dlgOk() ),   this, SLOT( onOk() ));
175
176   updateButtons();
177 }
178
179 /*
180   \brief Destructor: clean-up resources if necessary
181 */
182 SMESHGUI_DisplayEntitiesDlg::~SMESHGUI_DisplayEntitiesDlg()
183 {
184 }
185
186 void SMESHGUI_DisplayEntitiesDlg::InverseEntityMode(unsigned int& theOutputMode,
187                                                     unsigned int theMode)
188 {
189   bool anIsNotPresent = ~theOutputMode & theMode;
190   if(anIsNotPresent)
191     theOutputMode |= theMode;
192   else
193     theOutputMode &= ~theMode;
194 }
195
196 /*!
197   \brief Slot for changing entities state
198 */
199 void SMESHGUI_DisplayEntitiesDlg::onChangeEntityMode( bool isChecked )
200 {
201   QCheckBox* aSender = (QCheckBox*)sender();
202   if ( my0DElemsTB == aSender )
203     InverseEntityMode( myEntityMode, SMESH_Actor::e0DElements );
204   else if ( myEdgesTB == aSender )
205     InverseEntityMode( myEntityMode, SMESH_Actor::eEdges );
206   else if ( myFacesTB == aSender )
207     InverseEntityMode( myEntityMode, SMESH_Actor::eFaces );
208   else if ( myVolumesTB == aSender )
209     InverseEntityMode( myEntityMode, SMESH_Actor::eVolumes );
210   else if ( myBallsTB == aSender )
211     InverseEntityMode( myEntityMode, SMESH_Actor::eBallElem );
212
213   isChecked ? myNbCheckedButtons++ : myNbCheckedButtons--;
214   updateButtons();
215 }
216
217 /*!
218   \brief Show online help on dialog box
219 */
220 void SMESHGUI_DisplayEntitiesDlg::onHelp()
221 {
222   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
223   app->onHelpContextModule( "SMESH", "display_entity.html" );
224 }
225
226 /*!
227   \brief Display or update the mesh in the 3D view with selected entity mode
228 */
229 void SMESHGUI_DisplayEntitiesDlg::onOk()
230 {
231   SUIT_OverrideCursor wc;
232
233   const char* entry = myIObject->getEntry();
234   
235   if ( !myActor ) {
236     myActor = SMESH::CreateActor(SMESH::GetActiveStudyDocument(), 
237                                  entry, true);
238   }
239
240   if( myEntityMode != myActor->GetEntityMode() ) {
241     myActor->SetEntityMode(myEntityMode);
242     SUIT_ViewWindow* wnd = SMESH::GetActiveWindow();
243     SMESH::DisplayActor( wnd, myActor );
244     SUIT_DataOwnerPtrList aList;
245     aList.append( new LightApp_DataOwner( entry ) );
246     SMESHGUI::selectionMgr()->setSelected( aList, false );
247     SMESH::UpdateView( wnd, SMESH::eDisplay, entry );
248   }
249 }
250
251 /*!
252  * \brief Enable/disable OK button depending on nb of selected entities
253  */
254 void SMESHGUI_DisplayEntitiesDlg::updateButtons()
255 {
256   setButtonEnabled( myNbCheckedButtons > 0 || myNbTypes == 0, OK );
257 }