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