Salome HOME
SMH: Preparation version 3.0.0 - merge (HEAD+POLYWORK)
[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 "SVTK_Selection.h"
42 #include "SALOME_ListIO.hxx"
43 #include "SALOME_ListIteratorOfListIO.hxx"
44
45 // QT Includes
46 #include <qframe.h>
47 #include <qlayout.h>
48 #include <qpushbutton.h>
49 #include <qgroupbox.h>
50 #include <qlabel.h>
51 #include <qlistbox.h>
52 #include <qlist.h>
53 #include <qmessagebox.h>
54
55 // IDL Headers
56 #include "SALOMEconfig.h"
57 #include CORBA_SERVER_HEADER(SMESH_Mesh)
58
59 #define SPACING 5
60 #define MARGIN  10
61
62 /*!
63  *  Class       : SMESHGUI_DeleteGroupDlg
64  *  Description : Delete groups and their contents
65  */
66
67 //=================================================================================
68 // function : SMESHGUI_DeleteGroupDlg()
69 // purpose  : Constructor
70 //=================================================================================
71 SMESHGUI_DeleteGroupDlg::SMESHGUI_DeleteGroupDlg (QWidget*          theParent,
72                                                   SalomeApp_SelectionMgr* theSelection)
73      : QDialog(theParent, "SMESHGUI_DeleteGroupDlg", false,
74                WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
75 {
76   setCaption(tr("CAPTION"));
77
78   QVBoxLayout* aDlgLay = new QVBoxLayout(this, MARGIN, SPACING);
79
80   QFrame* aMainFrame = createMainFrame  (this);
81   QFrame* aBtnFrame  = createButtonFrame(this);
82
83   aDlgLay->addWidget(aMainFrame);
84   aDlgLay->addWidget(aBtnFrame);
85
86   aDlgLay->setStretchFactor(aMainFrame, 1);
87
88   Init(theSelection);
89 }
90
91 //=================================================================================
92 // function : createMainFrame()
93 // purpose  : Create frame containing dialog's input fields
94 //=================================================================================
95 QFrame* SMESHGUI_DeleteGroupDlg::createMainFrame (QWidget* theParent)
96 {
97   QGroupBox* aMainGrp =
98     new QGroupBox(1, Qt::Horizontal, tr("SELECTED_GROUPS"), theParent);
99
100   myListBox = new QListBox(aMainGrp);
101   myListBox->setMinimumHeight(100);
102   myListBox->setSelectionMode(QListBox::NoSelection);
103   myListBox->setRowMode(QListBox::FitToWidth);
104
105   return aMainGrp;
106 }
107
108 //=================================================================================
109 // function : createButtonFrame()
110 // purpose  : Create frame containing buttons
111 //=================================================================================
112 QFrame* SMESHGUI_DeleteGroupDlg::createButtonFrame (QWidget* theParent)
113 {
114   QFrame* aFrame = new QFrame(theParent);
115   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
116
117   myOkBtn     = new QPushButton(tr("SMESH_BUT_OK"   ), aFrame);
118   myApplyBtn  = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
119   myCloseBtn  = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
120
121   QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
122
123   QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
124
125   aLay->addWidget(myOkBtn);
126   aLay->addWidget(myApplyBtn);
127   aLay->addItem(aSpacer);
128   aLay->addWidget(myCloseBtn);
129
130   // connect signals and slots
131   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
132   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
133   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
134
135   return aFrame;
136 }
137
138 //=================================================================================
139 // name    : ~SMESHGUI_DeleteGroupDlg()
140 // Purpose : Destructor
141 //=================================================================================
142 SMESHGUI_DeleteGroupDlg::~SMESHGUI_DeleteGroupDlg()
143 {
144 }
145
146 //=================================================================================
147 // function : Init()
148 // purpose  : Init dialog fields, connect signals and slots, show dialog
149 //=================================================================================
150 void SMESHGUI_DeleteGroupDlg::Init (SalomeApp_SelectionMgr* theSelection)
151 {
152   myBlockSelection = false;
153   mySelectionMgr = theSelection;
154   SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI();
155   aSMESHGUI->SetActiveDialogBox((QDialog*)this);
156
157   // selection and SMESHGUI
158   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
159   connect(aSMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
160   connect(aSMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
161
162   int x, y;
163   aSMESHGUI->DefineDlgPosition(this, x, y);
164   this->move(x, y);
165   this->show();
166
167   // set selection mode
168 #ifdef NEW_GUI
169   mySelectionMgr->setSelectionModes(ActorSelection, true);
170 #else
171   mySelectionMgr->setSelectionModes(ActorSelection);
172 #endif
173   mySelectionMgr->installFilter(new SMESH_TypeFilter(GROUP));
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 !SMESHGUI::GetSMESHGUI()->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   SMESHGUI::GetSMESHGUI()->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   mySelectionMgr->setSelectionModes(ActorSelection);
239   disconnect(mySelectionMgr, 0, this, 0);
240   disconnect(SMESHGUI::GetSMESHGUI(), 0, this, 0);
241   SMESHGUI::GetSMESHGUI()->ResetState();
242   mySelectionMgr->clearFilters();
243   reject();
244 }
245
246 //=================================================================================
247 // function : onSelectionDone()
248 // purpose  : SLOT called when selection changed
249 //=================================================================================
250 void SMESHGUI_DeleteGroupDlg::onSelectionDone()
251 {
252   if (myBlockSelection)
253     return;
254
255   myListGrp.clear();
256   QStringList aNames;
257
258   SALOME_ListIO aListIO;
259   mySelectionMgr->selectedObjects(aListIO);
260   SALOME_ListIteratorOfListIO anIter (aListIO);
261   for (; anIter.More(); anIter.Next()) {
262     SMESH::SMESH_GroupBase_var aGroup =
263       SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIter.Value());
264     if (!aGroup->_is_nil()) {
265       aNames.append(aGroup->GetName());
266       myListGrp.append(aGroup);
267     }
268   }
269
270   myListBox->clear();
271   myListBox->insertStringList(aNames);
272 }
273
274 //=================================================================================
275 // function : onDeactivate()
276 // purpose  : SLOT called when dialog must be deativated
277 //=================================================================================
278 void SMESHGUI_DeleteGroupDlg::onDeactivate()
279 {
280   mySelectionMgr->clearFilters();
281   setEnabled(false);
282 }
283
284 //=================================================================================
285 // function : enterEvent()
286 // purpose  : Event filter
287 //=================================================================================
288 void SMESHGUI_DeleteGroupDlg::enterEvent (QEvent*)
289 {
290   SMESHGUI::GetSMESHGUI()->EmitSignalDeactivateDialog();
291   setEnabled(true);
292 #ifdef NEW_GUI
293   mySelectionMgr->setSelectionModes(ActorSelection, true);
294 #else
295   mySelectionMgr->setSelectionModes(ActorSelection);
296 #endif
297   mySelectionMgr->installFilter(new SMESH_TypeFilter (GROUP));
298 }
299
300 //=================================================================================
301 // function : closeEvent()
302 // purpose  :
303 //=================================================================================
304 void SMESHGUI_DeleteGroupDlg::closeEvent (QCloseEvent*)
305 {
306   onClose();
307 }