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