Salome HOME
e1d2b30828e56b1c0cfb96d4b066d429ec984103
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_DeleteGroupDlg.cxx
1 // Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 //
4 // This library is free software; you can redistribute it and/or 
5 // modify it under the terms of the GNU Lesser General Public 
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License. 
8 //
9 // This library is distributed in the hope that it will be useful, 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details. 
13 //
14 // You should have received a copy of the GNU Lesser General Public 
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File   : SMESHGUI_DeleteGroupDlg.cxx
21 // Author : Sergey LITONIN, Open CASCADE S.A.S.
22 //
23
24 // SMESH includes
25 #include "SMESHGUI_DeleteGroupDlg.h"
26
27 #include "SMESHGUI.h"
28 #include "SMESHGUI_Utils.h"
29 #include "SMESHGUI_VTKUtils.h"
30
31 #include <SMESH_TypeFilter.hxx>
32
33 // SALOME GUI includes
34 #include <SUIT_Desktop.h>
35 #include <SUIT_Session.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_ResourceMgr.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 <QHBoxLayout>
51 #include <QVBoxLayout>
52 #include <QPushButton>
53 #include <QGroupBox>
54 #include <QListWidget>
55 #include <QKeyEvent>
56
57 // IDL includes
58 #include <SALOMEconfig.h>
59 #include CORBA_SERVER_HEADER(SMESH_Mesh)
60
61 #define SPACING 6
62 #define MARGIN  11
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   mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
76   mySMESHGUI(theModule)
77 {
78   setModal(false);
79   setWindowTitle(tr("CAPTION"));
80
81   QVBoxLayout* aDlgLay = new QVBoxLayout(this);
82   aDlgLay->setMargin(MARGIN);
83   aDlgLay->setSpacing(SPACING);
84
85   QWidget* aMainFrame = createMainFrame  (this);
86   QWidget* aBtnFrame  = createButtonFrame(this);
87
88   aDlgLay->addWidget(aMainFrame);
89   aDlgLay->addWidget(aBtnFrame);
90
91   myHelpFileName = "deleting_groups_page.html";
92
93   Init();
94 }
95
96 //=================================================================================
97 // function : createMainFrame()
98 // purpose  : Create frame containing dialog's input fields
99 //=================================================================================
100 QWidget* SMESHGUI_DeleteGroupDlg::createMainFrame (QWidget* theParent)
101 {
102   QGroupBox* aMainGrp =
103     new QGroupBox(tr("SELECTED_GROUPS"), theParent);
104   QVBoxLayout* aLay = new QVBoxLayout(aMainGrp);
105   aLay->setMargin(MARGIN);
106   aLay->setSpacing(SPACING);
107
108   myListBox = new QListWidget(aMainGrp);
109   myListBox->setMinimumSize(150, 100);
110   myListBox->setSelectionMode(QListWidget::NoSelection);
111   //myListBox->setRowMode(QListBox::FitToWidth);
112   myListBox->setFlow(QListWidget::LeftToRight);
113   myListBox->setWrapping(true);
114
115   aLay->addWidget(myListBox);
116   
117   return aMainGrp;
118 }
119
120 //=================================================================================
121 // function : createButtonFrame()
122 // purpose  : Create frame containing buttons
123 //=================================================================================
124 QWidget* SMESHGUI_DeleteGroupDlg::createButtonFrame (QWidget* theParent)
125 {
126   QGroupBox* aFrame = new QGroupBox(theParent);
127
128   myOkBtn     = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aFrame);
129   myApplyBtn  = new QPushButton(tr("SMESH_BUT_APPLY"),           aFrame);
130   myCloseBtn  = new QPushButton(tr("SMESH_BUT_CLOSE"),           aFrame);
131   myHelpBtn   = new QPushButton(tr("SMESH_BUT_HELP"),            aFrame);
132
133   QHBoxLayout* aLay = new QHBoxLayout(aFrame);
134   aLay->setMargin(MARGIN);
135   aLay->setSpacing(SPACING);
136
137   aLay->addWidget(myOkBtn);
138   aLay->addSpacing(10);
139   aLay->addWidget(myApplyBtn);
140   aLay->addSpacing(10);
141   aLay->addStretch();
142   aLay->addWidget(myCloseBtn);
143   aLay->addWidget(myHelpBtn);
144
145   // connect signals and slots
146   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
147   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
148   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
149   connect(myHelpBtn,  SIGNAL(clicked()), SLOT(onHelp()));
150
151   return aFrame;
152 }
153
154 //=================================================================================
155 // name    : ~SMESHGUI_DeleteGroupDlg()
156 // Purpose : Destructor
157 //=================================================================================
158 SMESHGUI_DeleteGroupDlg::~SMESHGUI_DeleteGroupDlg()
159 {
160 }
161
162 //=================================================================================
163 // function : Init()
164 // purpose  : Init dialog fields, connect signals and slots, show dialog
165 //=================================================================================
166 void SMESHGUI_DeleteGroupDlg::Init ()
167 {
168   myBlockSelection = false;
169   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
170
171   // selection and SMESHGUI
172   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
173   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
174   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
175
176   // set selection mode
177   mySelectionMgr->installFilter(new SMESH_TypeFilter(GROUP));
178   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
179     aViewWindow->SetSelectionMode(ActorSelection);
180   onSelectionDone();
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     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
191                                  tr("NO_SELECTED_GROUPS"));
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   QList<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::warning(this, tr("WRN_WARNING"),
268                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
269                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
270                                                                  platform)).
271                              arg(myHelpFileName));
272   }
273 }
274
275 //=================================================================================
276 // function : onSelectionDone()
277 // purpose  : SLOT called when selection changed
278 //=================================================================================
279 void SMESHGUI_DeleteGroupDlg::onSelectionDone()
280 {
281   if (myBlockSelection)
282     return;
283
284   myListGrp.clear();
285   QStringList aNames;
286
287   SALOME_ListIO aListIO;
288   mySelectionMgr->selectedObjects(aListIO);
289   SALOME_ListIteratorOfListIO anIter (aListIO);
290   for ( ; anIter.More(); anIter.Next()) {
291     SMESH::SMESH_GroupBase_var aGroup =
292       SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIter.Value());
293     if (!aGroup->_is_nil()) {
294       aNames.append(aGroup->GetName());
295       myListGrp.append(aGroup);
296     }
297   }
298
299   myListBox->clear();
300   myListBox->addItems(aNames);
301 }
302
303 //=================================================================================
304 // function : onDeactivate()
305 // purpose  : SLOT called when dialog must be deativated
306 //=================================================================================
307 void SMESHGUI_DeleteGroupDlg::onDeactivate()
308 {
309   mySelectionMgr->clearFilters();
310   setEnabled(false);
311 }
312
313 //=================================================================================
314 // function : enterEvent()
315 // purpose  : Event filter
316 //=================================================================================
317 void SMESHGUI_DeleteGroupDlg::enterEvent (QEvent*)
318 {
319   mySMESHGUI->EmitSignalDeactivateDialog();
320   setEnabled(true);
321   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
322     aViewWindow->SetSelectionMode(ActorSelection);
323   mySelectionMgr->installFilter(new SMESH_TypeFilter (GROUP));
324 }
325
326 //=================================================================================
327 // function : closeEvent()
328 // purpose  :
329 //=================================================================================
330 void SMESHGUI_DeleteGroupDlg::closeEvent (QCloseEvent*)
331 {
332   onClose();
333 }
334
335 //=================================================================================
336 // function : keyPressEvent()
337 // purpose  :
338 //=================================================================================
339 void SMESHGUI_DeleteGroupDlg::keyPressEvent( QKeyEvent* e )
340 {
341   QDialog::keyPressEvent( e );
342   if ( e->isAccepted() )
343     return;
344
345   if ( e->key() == Qt::Key_F1 ) {
346     e->accept();
347     onHelp();
348   }
349 }