Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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 "LightApp_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   this->show();
166
167   // set selection mode
168   mySelectionMgr->installFilter(new SMESH_TypeFilter(GROUP));
169   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
170     aViewWindow->SetSelectionMode(ActorSelection);
171   onSelectionDone();
172
173   return;
174 }
175
176 //=================================================================================
177 // function : isValid()
178 // purpose  : Verify validity of input data
179 //=================================================================================
180 bool SMESHGUI_DeleteGroupDlg::isValid()
181 {
182   if (myListBox->count() == 0) {
183     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
184                              tr("NO_SELECTED_GROUPS"), QMessageBox::Ok);
185     return false;
186   }
187
188   return !mySMESHGUI->isActiveStudyLocked();
189 }
190
191 //=================================================================================
192 // function : onApply()
193 // purpose  : SLOT called when "Apply" button pressed.
194 //=================================================================================
195 bool SMESHGUI_DeleteGroupDlg::onApply()
196 {
197   if (!isValid())
198     return false;
199
200   myBlockSelection = true;
201
202   QValueList<SMESH::SMESH_GroupBase_var>::iterator anIter;
203   for (anIter = myListGrp.begin(); anIter != myListGrp.end(); ++anIter) {
204     SMESH::SMESH_Mesh_ptr aMesh = (*anIter)->GetMesh();
205     if (!aMesh->_is_nil())
206       aMesh->RemoveGroupWithContents(*anIter);
207   }
208
209   myListBox->clear();
210   myListGrp.clear();
211   mySelectionMgr->clearSelected();
212   SMESH::UpdateView();
213   mySMESHGUI->updateObjBrowser(true);
214
215   myBlockSelection = false;
216   return true;
217 }
218
219 //=================================================================================
220 // function : onOk()
221 // purpose  : SLOT called when "Ok" button pressed.
222 //=================================================================================
223 void SMESHGUI_DeleteGroupDlg::onOk()
224 {
225   if (onApply())
226     onClose();
227 }
228
229 //=================================================================================
230 // function : onClose()
231 // purpose  : SLOT called when "Close" button pressed. Close dialog
232 //=================================================================================
233 void SMESHGUI_DeleteGroupDlg::onClose()
234 {
235   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
236     aViewWindow->SetSelectionMode(ActorSelection);
237   disconnect(mySelectionMgr, 0, this, 0);
238   disconnect(mySMESHGUI, 0, this, 0);
239   mySMESHGUI->ResetState();
240   mySelectionMgr->clearFilters();
241   reject();
242 }
243
244 //=================================================================================
245 // function : onSelectionDone()
246 // purpose  : SLOT called when selection changed
247 //=================================================================================
248 void SMESHGUI_DeleteGroupDlg::onSelectionDone()
249 {
250   if (myBlockSelection)
251     return;
252
253   myListGrp.clear();
254   QStringList aNames;
255
256   SALOME_ListIO aListIO;
257   mySelectionMgr->selectedObjects(aListIO);
258   SALOME_ListIteratorOfListIO anIter (aListIO);
259   for (; anIter.More(); anIter.Next()) {
260     SMESH::SMESH_GroupBase_var aGroup =
261       SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIter.Value());
262     if (!aGroup->_is_nil()) {
263       aNames.append(aGroup->GetName());
264       myListGrp.append(aGroup);
265     }
266   }
267
268   myListBox->clear();
269   myListBox->insertStringList(aNames);
270 }
271
272 //=================================================================================
273 // function : onDeactivate()
274 // purpose  : SLOT called when dialog must be deativated
275 //=================================================================================
276 void SMESHGUI_DeleteGroupDlg::onDeactivate()
277 {
278   mySelectionMgr->clearFilters();
279   setEnabled(false);
280 }
281
282 //=================================================================================
283 // function : enterEvent()
284 // purpose  : Event filter
285 //=================================================================================
286 void SMESHGUI_DeleteGroupDlg::enterEvent (QEvent*)
287 {
288   mySMESHGUI->EmitSignalDeactivateDialog();
289   setEnabled(true);
290   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
291     aViewWindow->SetSelectionMode(ActorSelection);
292   mySelectionMgr->installFilter(new SMESH_TypeFilter (GROUP));
293 }
294
295 //=================================================================================
296 // function : closeEvent()
297 // purpose  :
298 //=================================================================================
299 void SMESHGUI_DeleteGroupDlg::closeEvent (QCloseEvent*)
300 {
301   onClose();
302 }