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