Salome HOME
Copyright update 2022
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_DisplayEntitiesDlg.cxx
1 // Copyright (C) 2014-2022  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 #include <smIdType.hxx>
37
38 #include <QCheckBox>
39 #include <QGridLayout>
40 #include <QGroupBox>
41 #include <QLabel>
42 #include <QVBoxLayout>
43
44 const int MARGIN  = 9;
45 const int SPACING = 6;
46
47 /*!
48   \class SMESHGUI_DisplayEntitiesDlg
49   \brief Dialog box to select entities to be displayed in viewer
50 */
51
52 /*
53   \brief Constructor
54   \param parent parent widget
55 */
56 SMESHGUI_DisplayEntitiesDlg::SMESHGUI_DisplayEntitiesDlg( QWidget* parent )
57   : SMESHGUI_Dialog( parent, true, false, Standard )
58 {
59   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
60
61   LightApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
62   SALOME_ListIO selected;
63   mgr->selectedObjects( selected );
64   SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_nil();
65   myActor = 0;
66   myNbCheckedButtons = 0;
67
68   SALOME_ListIteratorOfListIO it( selected );
69   myIObject = selected.First();
70   if ( myIObject->hasEntry() ) {
71     myActor = SMESH::FindActorByEntry( myIObject->getEntry() );
72   }
73   myEntityMode = myActor ? myActor->GetEntityMode() : 0;
74
75   aMesh = SMESH::GetMeshByIO( myIObject );
76
77   // set title
78   setWindowTitle( tr( "MEN_DISP_ENT" ) );
79
80   // create widgets
81   QGroupBox* anEntitiesGrp = new QGroupBox( tr( "SMESH_MESHINFO_ENTITIES" ), mainFrame() );
82   QGridLayout* hl = new QGridLayout( anEntitiesGrp );
83   hl->setMargin( MARGIN );
84   hl->setSpacing( SPACING );
85   smIdType nbElements;
86
87   // 0DElements
88   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_0DElement ) : aMesh->Nb0DElements();
89   my0DElemsTB = new QCheckBox( tr("SMESH_ELEMS0D"), anEntitiesGrp );
90   my0DElemsTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_ELEM0D" ) ) ) );
91   bool has0DElems = myEntityMode & SMESH_Actor::e0DElements;
92   my0DElemsTB->setChecked( has0DElems );
93   if ( has0DElems )
94     myNbCheckedButtons++;
95   connect( my0DElemsTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
96   QLabel* nb0DElemsLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
97   hl->addWidget( my0DElemsTB, 0, 0 );
98   hl->addWidget( nb0DElemsLab, 0, 1 );
99   my0DElemsTB->setEnabled( nbElements );
100   nb0DElemsLab->setEnabled( nbElements );
101   myNbTypes = ( nbElements > 0 );
102
103   // Edges
104   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_Edge ) : aMesh->NbEdges();
105   myEdgesTB = new QCheckBox( tr("SMESH_EDGES"), anEntitiesGrp );
106   myEdgesTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_EDGE" ) ) ) );
107   bool hasEdges = myEntityMode & SMESH_Actor::eEdges;
108   myEdgesTB->setChecked( hasEdges );
109   if ( hasEdges )
110     myNbCheckedButtons++;
111   connect( myEdgesTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
112   QLabel* nbEdgesLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
113   hl->addWidget( myEdgesTB, 1, 0 );
114   hl->addWidget( nbEdgesLab, 1, 1 );
115   myEdgesTB->setEnabled( nbElements );
116   nbEdgesLab->setEnabled( nbElements );
117   myNbTypes += ( nbElements > 0 );
118
119   // Faces
120   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_Face ) : aMesh->NbFaces();
121   myFacesTB = new QCheckBox( tr("SMESH_FACES"), anEntitiesGrp );
122   myFacesTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_TRIANGLE" ) ) ) );
123   bool hasFaces = myEntityMode & SMESH_Actor::eFaces;
124   myFacesTB->setChecked( hasFaces );
125   if ( hasFaces )
126     myNbCheckedButtons++;
127   connect( myFacesTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
128   QLabel* nbFacesLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
129   hl->addWidget( myFacesTB, 2, 0 );
130   hl->addWidget( nbFacesLab, 2, 1 );
131   myFacesTB->setEnabled( nbElements );
132   nbFacesLab->setEnabled( nbElements );
133   myNbTypes += ( nbElements > 0 );
134
135   // Volumes
136   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_Volume ) : aMesh->NbVolumes();
137   myVolumesTB = new QCheckBox( tr("SMESH_VOLUMES"), anEntitiesGrp );
138   myVolumesTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_TETRAS" ) ) ) );
139   bool hasVolumes = myEntityMode & SMESH_Actor::eVolumes;
140   myVolumesTB->setChecked( hasVolumes );
141   if ( hasVolumes )
142     myNbCheckedButtons++;
143   connect( myVolumesTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool) ) );
144   QLabel* nbVolumesLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
145   hl->addWidget( myVolumesTB, 3, 0 );
146   hl->addWidget( nbVolumesLab, 3, 1 );
147   myVolumesTB->setEnabled( nbElements );
148   nbVolumesLab->setEnabled( nbElements );
149   myNbTypes += ( nbElements > 0 );
150
151   // Balls
152   nbElements = myActor ? myActor->GetObject()->GetNbEntities( SMDSAbs_Ball ) : aMesh->NbBalls();
153   myBallsTB = new QCheckBox( tr("SMESH_BALLS"), anEntitiesGrp );
154   myBallsTB->setIcon( QIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_DLG_BALL" ) ) ) );
155   bool hasBalls = myEntityMode & SMESH_Actor::eBallElem;
156   myBallsTB->setChecked( hasBalls );
157   if ( hasBalls )
158     myNbCheckedButtons++;
159   connect( myBallsTB, SIGNAL(toggled(bool)), this, SLOT(onChangeEntityMode(bool)) );
160   QLabel* nbBallsLab = new QLabel( QString("%1").arg(nbElements).toLatin1().data(), anEntitiesGrp );
161   hl->addWidget( myBallsTB, 4, 0 );
162   hl->addWidget( nbBallsLab, 4, 1 );
163   myBallsTB->setEnabled( nbElements );
164   nbBallsLab->setEnabled( nbElements );
165   myNbTypes += ( nbElements > 0 );
166
167   QVBoxLayout* aDlgLay = new QVBoxLayout( mainFrame() );
168   aDlgLay->setMargin( 0 );
169   aDlgLay->setSpacing( SPACING );
170   aDlgLay->addWidget( anEntitiesGrp );
171   
172   button( OK )->setText( tr( "SMESH_BUT_OK" ) );
173
174   connect( this, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ));
175   connect( this, SIGNAL( dlgOk() ),   this, SLOT( onOk() ));
176
177   updateButtons();
178 }
179
180 /*
181   \brief Destructor: clean-up resources if necessary
182 */
183 SMESHGUI_DisplayEntitiesDlg::~SMESHGUI_DisplayEntitiesDlg()
184 {
185 }
186
187 void SMESHGUI_DisplayEntitiesDlg::InverseEntityMode(unsigned int& theOutputMode,
188                                                     unsigned int theMode)
189 {
190   bool anIsNotPresent = ~theOutputMode & theMode;
191   if(anIsNotPresent)
192     theOutputMode |= theMode;
193   else
194     theOutputMode &= ~theMode;
195 }
196
197 /*!
198   \brief Slot for changing entities state
199 */
200 void SMESHGUI_DisplayEntitiesDlg::onChangeEntityMode( bool isChecked )
201 {
202   QCheckBox* aSender = (QCheckBox*)sender();
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   updateButtons();
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.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(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 }