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