Salome HOME
Fix bug 12796: Warning missed for the bad file 'test18.med'
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 "SMESHGUI_Utils.h"
32 #include "SMESHGUI_VTKUtils.h"
33
34 #include "SMESH_TypeFilter.hxx"
35
36 #include "SUIT_Desktop.h"
37 #include "SUIT_Session.h"
38 #include "SUIT_MessageBox.h"
39
40 #include "SalomeApp_Study.h"
41 #include "LightApp_Application.h"
42 #include "LightApp_SelectionMgr.h"
43
44 #include "SALOME_ListIO.hxx"
45 #include "SALOME_ListIteratorOfListIO.hxx"
46
47 #include "SVTK_Selection.h"
48 #include "SVTK_ViewWindow.h"
49
50 // QT Includes
51 #include <qframe.h>
52 #include <qlayout.h>
53 #include <qpushbutton.h>
54 #include <qgroupbox.h>
55 #include <qlabel.h>
56 #include <qlistbox.h>
57 #include <qlist.h>
58 #include <qmessagebox.h>
59
60 // IDL Headers
61 #include "SALOMEconfig.h"
62 #include CORBA_SERVER_HEADER(SMESH_Mesh)
63
64 #define SPACING 5
65 #define MARGIN  10
66
67 /*!
68  *  Class       : SMESHGUI_DeleteGroupDlg
69  *  Description : Delete groups and their contents
70  */
71
72 //=================================================================================
73 // function : SMESHGUI_DeleteGroupDlg()
74 // purpose  : Constructor
75 //=================================================================================
76 SMESHGUI_DeleteGroupDlg::SMESHGUI_DeleteGroupDlg (SMESHGUI* theModule):
77   QDialog(SMESH::GetDesktop(theModule), 
78           "SMESHGUI_DeleteGroupDlg", 
79           false,
80           WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
81   mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
82   mySMESHGUI(theModule)
83 {
84   setCaption(tr("CAPTION"));
85
86   QVBoxLayout* aDlgLay = new QVBoxLayout(this, MARGIN, SPACING);
87
88   QFrame* aMainFrame = createMainFrame  (this);
89   QFrame* aBtnFrame  = createButtonFrame(this);
90
91   aDlgLay->addWidget(aMainFrame);
92   aDlgLay->addWidget(aBtnFrame);
93
94   aDlgLay->setStretchFactor(aMainFrame, 1);
95
96   myHelpFileName = "deleting_groups.htm";
97
98   Init();
99 }
100
101 //=================================================================================
102 // function : createMainFrame()
103 // purpose  : Create frame containing dialog's input fields
104 //=================================================================================
105 QFrame* SMESHGUI_DeleteGroupDlg::createMainFrame (QWidget* theParent)
106 {
107   QGroupBox* aMainGrp =
108     new QGroupBox(1, Qt::Horizontal, tr("SELECTED_GROUPS"), theParent);
109
110   myListBox = new QListBox(aMainGrp);
111   myListBox->setMinimumHeight(100);
112   myListBox->setSelectionMode(QListBox::NoSelection);
113   myListBox->setRowMode(QListBox::FitToWidth);
114
115   return aMainGrp;
116 }
117
118 //=================================================================================
119 // function : createButtonFrame()
120 // purpose  : Create frame containing buttons
121 //=================================================================================
122 QFrame* SMESHGUI_DeleteGroupDlg::createButtonFrame (QWidget* theParent)
123 {
124   QFrame* aFrame = new QFrame(theParent);
125   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
126
127   myOkBtn     = new QPushButton(tr("SMESH_BUT_OK"   ), aFrame);
128   myApplyBtn  = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
129   myCloseBtn  = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
130   myHelpBtn  = new QPushButton(tr("SMESH_BUT_HELP"), aFrame);
131
132   QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
133
134   QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
135
136   aLay->addWidget(myOkBtn);
137   aLay->addWidget(myApplyBtn);
138   aLay->addItem(aSpacer);
139   aLay->addWidget(myCloseBtn);
140   aLay->addWidget(myHelpBtn);
141
142   // connect signals and slots
143   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
144   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
145   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
146   connect(myHelpBtn,  SIGNAL(clicked()), SLOT(onHelp()));
147
148   return aFrame;
149 }
150
151 //=================================================================================
152 // name    : ~SMESHGUI_DeleteGroupDlg()
153 // Purpose : Destructor
154 //=================================================================================
155 SMESHGUI_DeleteGroupDlg::~SMESHGUI_DeleteGroupDlg()
156 {
157 }
158
159 //=================================================================================
160 // function : Init()
161 // purpose  : Init dialog fields, connect signals and slots, show dialog
162 //=================================================================================
163 void SMESHGUI_DeleteGroupDlg::Init ()
164 {
165   myBlockSelection = false;
166   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
167
168   // selection and SMESHGUI
169   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
170   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
171   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
172
173   this->show();
174
175   // set selection mode
176   mySelectionMgr->installFilter(new SMESH_TypeFilter(GROUP));
177   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
178     aViewWindow->SetSelectionMode(ActorSelection);
179   onSelectionDone();
180
181   return;
182 }
183
184 //=================================================================================
185 // function : isValid()
186 // purpose  : Verify validity of input data
187 //=================================================================================
188 bool SMESHGUI_DeleteGroupDlg::isValid()
189 {
190   if (myListBox->count() == 0) {
191     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
192                              tr("NO_SELECTED_GROUPS"), QMessageBox::Ok);
193     return false;
194   }
195
196   return !mySMESHGUI->isActiveStudyLocked();
197 }
198
199 //=================================================================================
200 // function : onApply()
201 // purpose  : SLOT called when "Apply" button pressed.
202 //=================================================================================
203 bool SMESHGUI_DeleteGroupDlg::onApply()
204 {
205   if (!isValid())
206     return false;
207
208   myBlockSelection = true;
209
210   QValueList<SMESH::SMESH_GroupBase_var>::iterator anIter;
211   for (anIter = myListGrp.begin(); anIter != myListGrp.end(); ++anIter) {
212     SMESH::SMESH_Mesh_ptr aMesh = (*anIter)->GetMesh();
213     if (!aMesh->_is_nil())
214       aMesh->RemoveGroupWithContents(*anIter);
215   }
216
217   myListBox->clear();
218   myListGrp.clear();
219   mySelectionMgr->clearSelected();
220   SMESH::UpdateView();
221   mySMESHGUI->updateObjBrowser(true);
222
223   myBlockSelection = false;
224   return true;
225 }
226
227 //=================================================================================
228 // function : onOk()
229 // purpose  : SLOT called when "Ok" button pressed.
230 //=================================================================================
231 void SMESHGUI_DeleteGroupDlg::onOk()
232 {
233   if (onApply())
234     onClose();
235 }
236
237 //=================================================================================
238 // function : onClose()
239 // purpose  : SLOT called when "Close" button pressed. Close dialog
240 //=================================================================================
241 void SMESHGUI_DeleteGroupDlg::onClose()
242 {
243   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
244     aViewWindow->SetSelectionMode(ActorSelection);
245   disconnect(mySelectionMgr, 0, this, 0);
246   disconnect(mySMESHGUI, 0, this, 0);
247   mySMESHGUI->ResetState();
248   mySelectionMgr->clearFilters();
249   reject();
250 }
251
252 //=================================================================================
253 // function : onHelp()
254 // purpose  :
255 //=================================================================================
256 void SMESHGUI_DeleteGroupDlg::onHelp()
257 {
258   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
259   if (app) 
260     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
261   else {
262     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
263                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
264                            arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
265                            QObject::tr("BUT_OK"));
266   }
267 }
268
269 //=================================================================================
270 // function : onSelectionDone()
271 // purpose  : SLOT called when selection changed
272 //=================================================================================
273 void SMESHGUI_DeleteGroupDlg::onSelectionDone()
274 {
275   if (myBlockSelection)
276     return;
277
278   myListGrp.clear();
279   QStringList aNames;
280
281   SALOME_ListIO aListIO;
282   mySelectionMgr->selectedObjects(aListIO);
283   SALOME_ListIteratorOfListIO anIter (aListIO);
284   for (; anIter.More(); anIter.Next()) {
285     SMESH::SMESH_GroupBase_var aGroup =
286       SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIter.Value());
287     if (!aGroup->_is_nil()) {
288       aNames.append(aGroup->GetName());
289       myListGrp.append(aGroup);
290     }
291   }
292
293   myListBox->clear();
294   myListBox->insertStringList(aNames);
295 }
296
297 //=================================================================================
298 // function : onDeactivate()
299 // purpose  : SLOT called when dialog must be deativated
300 //=================================================================================
301 void SMESHGUI_DeleteGroupDlg::onDeactivate()
302 {
303   mySelectionMgr->clearFilters();
304   setEnabled(false);
305 }
306
307 //=================================================================================
308 // function : enterEvent()
309 // purpose  : Event filter
310 //=================================================================================
311 void SMESHGUI_DeleteGroupDlg::enterEvent (QEvent*)
312 {
313   mySMESHGUI->EmitSignalDeactivateDialog();
314   setEnabled(true);
315   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
316     aViewWindow->SetSelectionMode(ActorSelection);
317   mySelectionMgr->installFilter(new SMESH_TypeFilter (GROUP));
318 }
319
320 //=================================================================================
321 // function : closeEvent()
322 // purpose  :
323 //=================================================================================
324 void SMESHGUI_DeleteGroupDlg::closeEvent (QCloseEvent*)
325 {
326   onClose();
327 }
328
329 //=================================================================================
330 // function : keyPressEvent()
331 // purpose  :
332 //=================================================================================
333 void SMESHGUI_DeleteGroupDlg::keyPressEvent( QKeyEvent* e )
334 {
335   QDialog::keyPressEvent( e );
336   if ( e->isAccepted() )
337     return;
338
339   if ( e->key() == Key_F1 )
340     {
341       e->accept();
342       onHelp();
343     }
344 }