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