Salome HOME
Remove obsolete staff; redesign Handle-based and CDL-generated classes
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_DeleteGroupDlg.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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
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(reject()));
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(reject()));
176
177   // set selection mode
178   mySelectionMgr->installFilter(new SMESH_TypeFilter(SMESH::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_var aMesh = (*anIter)->GetMesh();
213     if (!aMesh->_is_nil())
214       aMesh->RemoveGroupWithContents(*anIter);
215   }
216
217   myListBox->clear();
218   myListGrp.clear();
219   mySelectionMgr->clearSelected();
220
221   /** Erase graphical objects **/
222   SALOME_ListIteratorOfListIO anIterIO (myListGrpIO);
223   for ( ; anIterIO.More(); anIterIO.Next())
224     SMESH::RemoveVisualObjectWithActors( anIterIO.Value()->getEntry(), /*fromAllViews=*/true );
225
226   SMESH::UpdateView();
227   SMESHGUI::Modified();
228   mySMESHGUI->updateObjBrowser(true);
229
230   myBlockSelection = false;
231   return true;
232 }
233
234 //=================================================================================
235 // function : onOk()
236 // purpose  : SLOT called when "Ok" button pressed.
237 //=================================================================================
238 void SMESHGUI_DeleteGroupDlg::onOk()
239 {
240   if (onApply())
241     reject();
242 }
243
244 //=================================================================================
245 // function : reject()
246 // purpose  : SLOT called when "Close" button pressed. Close dialog
247 //=================================================================================
248 void SMESHGUI_DeleteGroupDlg::reject()
249 {
250   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
251     aViewWindow->SetSelectionMode(ActorSelection);
252   disconnect(mySelectionMgr, 0, this, 0);
253   disconnect(mySMESHGUI, 0, this, 0);
254   mySMESHGUI->ResetState();
255   mySelectionMgr->clearFilters();
256   QDialog::reject();
257 }
258
259 //=================================================================================
260 // function : onHelp()
261 // purpose  :
262 //=================================================================================
263 void SMESHGUI_DeleteGroupDlg::onHelp()
264 {
265   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
266   if (app) 
267     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
268   else {
269     QString platform;
270 #ifdef WIN32
271     platform = "winapplication";
272 #else
273     platform = "application";
274 #endif
275     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
276                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
277                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
278                                                                  platform)).
279                              arg(myHelpFileName));
280   }
281 }
282
283 //=================================================================================
284 // function : onSelectionDone()
285 // purpose  : SLOT called when selection changed
286 //=================================================================================
287 void SMESHGUI_DeleteGroupDlg::onSelectionDone()
288 {
289   if (myBlockSelection)
290     return;
291
292   myListGrp.clear();
293   myListGrpIO.Clear();
294   QStringList aNames;
295
296   SALOME_ListIO aListIO;
297   mySelectionMgr->selectedObjects(aListIO);
298   SALOME_ListIteratorOfListIO anIter (aListIO);
299   for ( ; anIter.More(); anIter.Next()) {
300     SMESH::SMESH_GroupBase_var aGroup =
301       SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIter.Value());
302     if (!aGroup->_is_nil()) {
303       aNames.append(aGroup->GetName());
304       myListGrp.append(aGroup);
305       myListGrpIO.Append( anIter.Value() );
306     }
307   }
308
309   myListBox->clear();
310   myListBox->addItems(aNames);
311 }
312
313 //=================================================================================
314 // function : onDeactivate()
315 // purpose  : SLOT called when dialog must be deativated
316 //=================================================================================
317 void SMESHGUI_DeleteGroupDlg::onDeactivate()
318 {
319   mySelectionMgr->clearFilters();
320   setEnabled(false);
321 }
322
323 //=================================================================================
324 // function : enterEvent()
325 // purpose  : Event filter
326 //=================================================================================
327 void SMESHGUI_DeleteGroupDlg::enterEvent (QEvent*)
328 {
329   mySMESHGUI->EmitSignalDeactivateDialog();
330   setEnabled(true);
331   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
332     aViewWindow->SetSelectionMode(ActorSelection);
333   mySelectionMgr->installFilter(new SMESH_TypeFilter (SMESH::GROUP));
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 }