Salome HOME
Nerge with PAL/SALOME 2.1.0d
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_DeleteGroupDlg.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_DeleteGroupDlg.cxx
25 //  Author : Sergey LITONIN
26 //  Module : SMESH
27
28 #include "SMESHGUI_DeleteGroupDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESH_TypeFilter.hxx"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34
35 #include "QAD_Desktop.h"
36 #include "SALOME_Selection.h"
37 #include "SALOME_ListIteratorOfListIO.hxx"
38
39 #include <qframe.h>
40 #include <qlayout.h>
41 #include <qpushbutton.h>
42 #include <qgroupbox.h>
43 #include <qlabel.h>
44 #include <qlistbox.h>
45 #include <qlist.h>
46 #include <qmessagebox.h>
47
48 #include "SALOMEconfig.h"
49 #include CORBA_SERVER_HEADER(SMESH_Mesh)
50
51 #define SPACING 5
52 #define MARGIN  10
53
54 /*
55   Class       : SMESHGUI_DeleteGroupDlg
56   Description : Delete groups and their contents
57 */
58
59 //=======================================================================
60 // name    : SMESHGUI_DeleteGroupDlg::SMESHGUI_DeleteGroupDlg
61 // Purpose : Constructor
62 //=======================================================================
63 SMESHGUI_DeleteGroupDlg::SMESHGUI_DeleteGroupDlg( QWidget*          theParent, 
64                                                   SALOME_Selection* theSelection )
65 : QDialog( theParent, "SMESHGUI_DeleteGroupDlg", false, 
66            WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
67 {
68   setCaption( tr( "CAPTION" ) );
69
70   QVBoxLayout* aDlgLay = new QVBoxLayout( this, MARGIN, SPACING );
71
72   QFrame* aMainFrame = createMainFrame  ( this );
73   QFrame* aBtnFrame  = createButtonFrame( this );
74
75   aDlgLay->addWidget( aMainFrame );
76   aDlgLay->addWidget( aBtnFrame );
77
78   aDlgLay->setStretchFactor( aMainFrame, 1 );
79
80   Init( theSelection ) ; 
81 }
82
83 //=======================================================================
84 // name    : SMESHGUI_DeleteGroupDlg::createMainFrame
85 // Purpose : Create frame containing dialog's input fields
86 //=======================================================================
87 QFrame* SMESHGUI_DeleteGroupDlg::createMainFrame( QWidget* theParent )
88 {
89   QGroupBox* aMainGrp = new QGroupBox( 1, Qt::Horizontal, tr( "SELECTED_GROUPS" ), theParent );
90   
91   myListBox = new QListBox( aMainGrp );
92   myListBox->setMinimumHeight( 100 );
93   myListBox->setSelectionMode( QListBox::NoSelection );
94   myListBox->setRowMode( QListBox::FitToWidth );
95   
96   return aMainGrp;
97 }
98
99 //=======================================================================
100 // name    : SMESHGUI_DeleteGroupDlg::createButtonFrame
101 // Purpose : Create frame containing buttons
102 //=======================================================================
103 QFrame* SMESHGUI_DeleteGroupDlg::createButtonFrame( QWidget* theParent )
104 {
105   QFrame* aFrame = new QFrame( theParent );
106   aFrame->setFrameStyle( QFrame::Box | QFrame::Sunken );
107
108   myOkBtn     = new QPushButton( tr( "SMESH_BUT_OK"    ), aFrame );
109   myApplyBtn  = new QPushButton( tr( "SMESH_BUT_APPLY" ), aFrame );
110   myCloseBtn  = new QPushButton( tr( "SMESH_BUT_CLOSE" ), aFrame );
111
112   QSpacerItem* aSpacer = new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum );
113
114   QHBoxLayout* aLay = new QHBoxLayout( aFrame, MARGIN, SPACING );
115
116   aLay->addWidget( myOkBtn );
117   aLay->addWidget( myApplyBtn );
118   aLay->addItem( aSpacer);
119   aLay->addWidget( myCloseBtn );
120   
121   // connect signals and slots
122   connect( myOkBtn,    SIGNAL( clicked() ), SLOT( onOk() ) );
123   connect( myCloseBtn, SIGNAL( clicked() ), SLOT( onClose() ) ) ;
124   connect( myApplyBtn, SIGNAL( clicked() ), SLOT( onApply() ) );
125   
126   return aFrame;
127 }
128
129 //=======================================================================
130 // name    : SMESHGUI_DeleteGroupDlg::~SMESHGUI_DeleteGroupDlg
131 // Purpose : Destructor
132 //=======================================================================
133 SMESHGUI_DeleteGroupDlg::~SMESHGUI_DeleteGroupDlg()
134 {
135 }
136
137 //=======================================================================
138 // name    : SMESHGUI_DeleteGroupDlg::Init
139 // Purpose : Init dialog fields, connect signals and slots, show dialog
140 //=======================================================================
141 void SMESHGUI_DeleteGroupDlg::Init( SALOME_Selection* theSelection )
142 {
143   myBlockSelection = false;
144   mySelection = theSelection;  
145   SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI();
146   aSMESHGUI->SetActiveDialogBox( ( QDialog* )this ) ;
147   
148   // selection and SMESHGUI
149   connect( mySelection, SIGNAL( currentSelectionChanged() ), SLOT( onSelectionDone() ) );
150   connect( aSMESHGUI, SIGNAL( SignalDeactivateActiveDialog() ), SLOT( onDeactivate() ) );
151   connect( aSMESHGUI, SIGNAL( SignalCloseAllDialogs() ), SLOT( ClickOnClose() ) );
152   
153   int x, y ;
154   aSMESHGUI->DefineDlgPosition( this, x, y );
155   this->move( x, y );
156   this->show(); 
157
158   // set selection mode
159   QAD_Application::getDesktop()->SetSelectionMode( ActorSelection, true ); 
160   mySelection->AddFilter( new SMESH_TypeFilter( GROUP ) );
161   onSelectionDone();
162
163   return;
164 }
165
166 //=======================================================================
167 // name    : SMESHGUI_DeleteGroupDlg::isValid
168 // Purpose : Verify validity of input data
169 //=======================================================================
170 bool SMESHGUI_DeleteGroupDlg::isValid()
171 {
172   if ( myListBox->count() == 0 )
173   {
174     QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),
175       tr( "SMESH_INSUFFICIENT_DATA" ), tr( "NO_SELECTED_GROUPS" ), QMessageBox::Ok ); 
176     return false;
177   }
178   
179   return !SMESHGUI::GetSMESHGUI()->ActiveStudyLocked();
180 }
181
182 //=======================================================================
183 // name    : SMESHGUI_DeleteGroupDlg::onApply
184 // Purpose : SLOT called when "Apply" button pressed. 
185 //=======================================================================
186 bool SMESHGUI_DeleteGroupDlg::onApply()
187 {
188   if ( !isValid() )
189     return false;
190
191   myBlockSelection = true;
192   
193   QValueList<SMESH::SMESH_GroupBase_var>::iterator anIter;
194   for ( anIter = myListGrp.begin(); anIter != myListGrp.end(); ++anIter )
195   {
196     SMESH::SMESH_Mesh_ptr aMesh = (*anIter)->GetMesh();
197     if ( !aMesh->_is_nil() )
198       aMesh->RemoveGroupWithContents( *anIter );
199   }
200
201   myListBox->clear();
202   myListGrp.clear();
203   mySelection->ClearIObjects();
204   SMESH::UpdateView();
205   SMESHGUI::GetSMESHGUI()->GetActiveStudy()->updateObjBrowser( true );
206    
207   myBlockSelection = false;
208   return false;
209 }
210
211 //=======================================================================
212 // name    : SMESHGUI_DeleteGroupDlg::onOk
213 // Purpose : SLOT called when "Ok" button pressed. 
214 //=======================================================================
215 void SMESHGUI_DeleteGroupDlg::onOk()
216 {
217   if ( onApply() )
218     onClose();
219 }
220
221 //=======================================================================
222 // name    : SMESHGUI_DeleteGroupDlg::onClose
223 // Purpose : SLOT called when "Close" button pressed. Close dialog
224 //=======================================================================
225 void SMESHGUI_DeleteGroupDlg::onClose()
226 {
227   QAD_Application::getDesktop()->SetSelectionMode( ActorSelection );
228   disconnect( mySelection, 0, this, 0 );
229   disconnect( SMESHGUI::GetSMESHGUI(), 0, this, 0 );
230   SMESHGUI::GetSMESHGUI()->ResetState() ;
231   mySelection->ClearFilters();
232   reject();
233 }
234
235 //=======================================================================
236 // name    : SMESHGUI_DeleteGroupDlg::onSelectionDone
237 // Purpose : SLOT called when selection changed
238 //=======================================================================
239 void SMESHGUI_DeleteGroupDlg::onSelectionDone()
240 {
241   if ( myBlockSelection )
242     return;
243   
244   myListGrp.clear();
245   QStringList aNames;
246   
247   const SALOME_ListIO& aListIO = mySelection->StoredIObjects();
248   SALOME_ListIteratorOfListIO anIter( aListIO );
249   for( ; anIter.More(); anIter.Next() )
250   {
251     SMESH::SMESH_GroupBase_var aGroup = 
252       SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>( anIter.Value() );
253     if ( !aGroup->_is_nil() )
254     {
255       aNames.append( aGroup->GetName() );
256       myListGrp.append( aGroup );
257     }
258   }
259     
260   myListBox->clear();
261   myListBox->insertStringList( aNames );
262 }
263
264 //=======================================================================
265 // name    : SMESHGUI_DeleteGroupDlg::onDeactivate
266 // Purpose : SLOT called when dialog must be deativated
267 //=======================================================================
268 void SMESHGUI_DeleteGroupDlg::onDeactivate()
269 {
270   mySelection->ClearFilters();
271   setEnabled( false );
272 }
273
274 //=======================================================================
275 // name    : SMESHGUI_DeleteGroupDlg::enterEvent
276 // Purpose : Event filter
277 //=======================================================================
278 void SMESHGUI_DeleteGroupDlg::enterEvent( QEvent* )
279 {
280   SMESHGUI::GetSMESHGUI()->EmitSignalDeactivateDialog() ;   
281   setEnabled( true );
282   QAD_Application::getDesktop()->SetSelectionMode( ActorSelection, true ); 
283   mySelection->AddFilter( new SMESH_TypeFilter( GROUP ) );
284 }
285
286
287 //=================================================================================
288 // function : closeEvent()
289 // purpose  :
290 //=================================================================================
291 void SMESHGUI_DeleteGroupDlg::closeEvent( QCloseEvent* )
292 {
293   onClose() ;
294 }
295