Salome HOME
Update from local sources
[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   myViewWindow(SMESH::GetViewWindow(mySMESHGUI)),
80   mySMESHGUI(theModule)
81 {
82   setCaption(tr("CAPTION"));
83
84   QVBoxLayout* aDlgLay = new QVBoxLayout(this, MARGIN, SPACING);
85
86   QFrame* aMainFrame = createMainFrame  (this);
87   QFrame* aBtnFrame  = createButtonFrame(this);
88
89   aDlgLay->addWidget(aMainFrame);
90   aDlgLay->addWidget(aBtnFrame);
91
92   aDlgLay->setStretchFactor(aMainFrame, 1);
93
94   Init();
95 }
96
97 //=================================================================================
98 // function : createMainFrame()
99 // purpose  : Create frame containing dialog's input fields
100 //=================================================================================
101 QFrame* SMESHGUI_DeleteGroupDlg::createMainFrame (QWidget* theParent)
102 {
103   QGroupBox* aMainGrp =
104     new QGroupBox(1, Qt::Horizontal, tr("SELECTED_GROUPS"), theParent);
105
106   myListBox = new QListBox(aMainGrp);
107   myListBox->setMinimumHeight(100);
108   myListBox->setSelectionMode(QListBox::NoSelection);
109   myListBox->setRowMode(QListBox::FitToWidth);
110
111   return aMainGrp;
112 }
113
114 //=================================================================================
115 // function : createButtonFrame()
116 // purpose  : Create frame containing buttons
117 //=================================================================================
118 QFrame* SMESHGUI_DeleteGroupDlg::createButtonFrame (QWidget* theParent)
119 {
120   QFrame* aFrame = new QFrame(theParent);
121   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
122
123   myOkBtn     = new QPushButton(tr("SMESH_BUT_OK"   ), aFrame);
124   myApplyBtn  = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
125   myCloseBtn  = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
126
127   QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
128
129   QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
130
131   aLay->addWidget(myOkBtn);
132   aLay->addWidget(myApplyBtn);
133   aLay->addItem(aSpacer);
134   aLay->addWidget(myCloseBtn);
135
136   // connect signals and slots
137   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
138   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
139   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
140
141   return aFrame;
142 }
143
144 //=================================================================================
145 // name    : ~SMESHGUI_DeleteGroupDlg()
146 // Purpose : Destructor
147 //=================================================================================
148 SMESHGUI_DeleteGroupDlg::~SMESHGUI_DeleteGroupDlg()
149 {
150 }
151
152 //=================================================================================
153 // function : Init()
154 // purpose  : Init dialog fields, connect signals and slots, show dialog
155 //=================================================================================
156 void SMESHGUI_DeleteGroupDlg::Init ()
157 {
158   myBlockSelection = false;
159   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
160
161   // selection and SMESHGUI
162   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
163   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
164   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
165
166   int x, y;
167   mySMESHGUI->DefineDlgPosition(this, x, y);
168   this->move(x, y);
169   this->show();
170
171   // set selection mode
172   mySelectionMgr->installFilter(new SMESH_TypeFilter(GROUP));
173   myViewWindow->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   myViewWindow->SetSelectionMode(ActorSelection);
239   disconnect(mySelectionMgr, 0, this, 0);
240   disconnect(mySMESHGUI, 0, this, 0);
241   mySMESHGUI->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   mySMESHGUI->EmitSignalDeactivateDialog();
291   setEnabled(true);
292   myViewWindow->SetSelectionMode(ActorSelection);
293   mySelectionMgr->installFilter(new SMESH_TypeFilter (GROUP));
294 }
295
296 //=================================================================================
297 // function : closeEvent()
298 // purpose  :
299 //=================================================================================
300 void SMESHGUI_DeleteGroupDlg::closeEvent (QCloseEvent*)
301 {
302   onClose();
303 }