Salome HOME
Issue 0020860: EDF 1404 SMESH: Save operation unavailable after a group creation...
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_RemoveElementsDlg.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 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_RemoveElementsDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_RemoveElementsDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_VTKUtils.h"
33 #include "SMESHGUI_MeshUtils.h"
34 #include "SMESHGUI_IdValidator.h"
35 #include "SMESHGUI_FilterDlg.h"
36
37 #include <SMESH_Actor.h>
38 #include <SMDS_Mesh.hxx>
39
40 // SALOME GUI includes
41 #include <SUIT_ResourceMgr.h>
42 #include <SUIT_Desktop.h>
43 #include <SUIT_Session.h>
44 #include <SUIT_MessageBox.h>
45
46 #include <LightApp_Application.h>
47 #include <LightApp_SelectionMgr.h>
48 #include <SalomeApp_Tools.h>
49
50 #include <SVTK_Selector.h>
51 #include <SVTK_ViewModel.h>
52 #include <SVTK_ViewWindow.h>
53 #include <SALOME_ListIO.hxx>
54
55 // OCCT includes
56 #include <TColStd_MapOfInteger.hxx>
57
58 // Qt includes
59 #include <QGroupBox>
60 #include <QLabel>
61 #include <QLineEdit>
62 #include <QPushButton>
63 #include <QRadioButton>
64 #include <QVBoxLayout>
65 #include <QHBoxLayout>
66 #include <QKeyEvent>
67 #include <QButtonGroup>
68
69 // IDL includes
70 #include <SALOMEconfig.h>
71 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
72
73 #define SPACING 6
74 #define MARGIN  11
75
76 //=================================================================================
77 // class    : SMESHGUI_RemoveElementsDlg()
78 // purpose  :
79 //=================================================================================
80 SMESHGUI_RemoveElementsDlg
81 ::SMESHGUI_RemoveElementsDlg(SMESHGUI* theModule)
82   : QDialog(SMESH::GetDesktop(theModule)),
83     mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
84     mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
85     mySMESHGUI(theModule),
86     myBusy(false),
87     myFilterDlg(0)
88 {
89   setModal( false );
90   setAttribute( Qt::WA_DeleteOnClose, true );
91   setWindowTitle(tr("SMESH_REMOVE_ELEMENTS_TITLE"));
92   setSizeGripEnabled(true);
93   
94   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_REM_ELEMENT")));
95   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
96
97   QVBoxLayout* SMESHGUI_RemoveElementsDlgLayout = new QVBoxLayout(this);
98   SMESHGUI_RemoveElementsDlgLayout->setSpacing(SPACING);
99   SMESHGUI_RemoveElementsDlgLayout->setMargin(MARGIN);
100
101   /***************************************************************/
102   GroupConstructors = new QGroupBox(tr("SMESH_ELEMENTS"), this);
103   QButtonGroup* ButtonGroup = new QButtonGroup(this);
104   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
105   GroupConstructorsLayout->setSpacing(SPACING);
106   GroupConstructorsLayout->setMargin(MARGIN);
107
108   Constructor1 = new QRadioButton(GroupConstructors);
109   Constructor1->setIcon(image0);
110   Constructor1->setChecked(true);
111
112   GroupConstructorsLayout->addWidget(Constructor1);
113   ButtonGroup->addButton(Constructor1, 0);
114
115   /***************************************************************/
116   GroupC1 = new QGroupBox(tr("SMESH_REMOVE"), this);
117   QHBoxLayout* GroupC1Layout = new QHBoxLayout(GroupC1);
118   GroupC1Layout->setSpacing(SPACING);
119   GroupC1Layout->setMargin(MARGIN);
120
121   TextLabelC1A1 = new QLabel(tr("SMESH_ID_ELEMENTS"), GroupC1);
122   SelectButtonC1A1 = new QPushButton(GroupC1);
123   SelectButtonC1A1->setIcon(image1);
124   LineEditC1A1 = new QLineEdit(GroupC1);
125   LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this));
126   QPushButton* filterBtn = new QPushButton( tr( "SMESH_BUT_FILTER" ), GroupC1 );
127   connect(filterBtn,   SIGNAL(clicked()), this, SLOT(setFilters()));
128
129   GroupC1Layout->addWidget(TextLabelC1A1);
130   GroupC1Layout->addWidget(SelectButtonC1A1);
131   GroupC1Layout->addWidget(LineEditC1A1);
132   GroupC1Layout->addWidget(filterBtn );
133
134   /***************************************************************/
135   GroupButtons = new QGroupBox(this);
136   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
137   GroupButtonsLayout->setSpacing(SPACING);
138   GroupButtonsLayout->setMargin(MARGIN);
139
140   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
141   buttonOk->setAutoDefault(true);
142   buttonOk->setDefault(true);
143   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
144   buttonApply->setAutoDefault(true);
145   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
146   buttonCancel->setAutoDefault(true);
147   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
148   buttonHelp->setAutoDefault(true);
149
150   GroupButtonsLayout->addWidget(buttonOk);
151   GroupButtonsLayout->addSpacing(10);
152   GroupButtonsLayout->addWidget(buttonApply);
153   GroupButtonsLayout->addSpacing(10);
154   GroupButtonsLayout->addStretch();
155   GroupButtonsLayout->addWidget(buttonCancel);
156   GroupButtonsLayout->addWidget(buttonHelp);
157
158   /***************************************************************/
159   SMESHGUI_RemoveElementsDlgLayout->addWidget(GroupConstructors);
160   SMESHGUI_RemoveElementsDlgLayout->addWidget(GroupC1);
161   SMESHGUI_RemoveElementsDlgLayout->addWidget(GroupButtons);
162
163   myHelpFileName = "removing_nodes_and_elements_page.html#removing_elements_anchor";
164
165   Init(); /* Initialisations */
166 }
167
168 //=================================================================================
169 // function : ~SMESHGUI_RemoveElementsDlg()
170 // purpose  : Destroys the object and frees any allocated resources
171 //=================================================================================
172 SMESHGUI_RemoveElementsDlg::~SMESHGUI_RemoveElementsDlg()
173 {
174   if ( myFilterDlg ) {
175     myFilterDlg->setParent( 0 );
176     delete myFilterDlg;
177     myFilterDlg = 0;
178   }
179 }
180
181 //=================================================================================
182 // function : Init()
183 // purpose  :
184 //=================================================================================
185 void SMESHGUI_RemoveElementsDlg::Init()
186 {
187   myConstructorId = 0;
188   Constructor1->setChecked(true);
189   myEditCurrentArgument = LineEditC1A1;
190
191   myNbOkElements = 0;
192   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
193   myActor = 0;
194   myBusy = false;
195
196   /* signals and slots connections */
197   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
198   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
199   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
200   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
201
202   connect(SelectButtonC1A1, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
203   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
204   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
205   /* to close dialog if study change */
206   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
207   connect(myEditCurrentArgument, SIGNAL(textChanged(const QString&)),
208           SLOT(onTextChange(const QString&)));
209
210   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
211     aViewWindow->SetSelectionMode(CellSelection);
212
213   SelectionIntoArgument();
214 }
215
216 //=================================================================================
217 // function : ClickOnApply()
218 // purpose  :
219 //=================================================================================
220 void SMESHGUI_RemoveElementsDlg::ClickOnApply()
221 {
222   if (mySMESHGUI->isActiveStudyLocked())
223     return;
224
225   if (myNbOkElements) {
226     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
227     SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
228     anArrayOfIdeces->length(aListId.count());
229     for (int i = 0; i < aListId.count(); i++)
230       anArrayOfIdeces[i] = aListId[ i ].toInt();
231
232     bool aResult = false;
233     try {
234       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
235       aResult = aMeshEditor->RemoveElements(anArrayOfIdeces.inout());
236     } catch (const SALOME::SALOME_Exception& S_ex) {
237       SalomeApp_Tools::QtCatchCorbaException(S_ex);
238       myEditCurrentArgument->clear();
239     } catch (...){
240       myEditCurrentArgument->clear();
241     }
242
243     if (aResult) {
244       myEditCurrentArgument->clear();
245       mySelector->ClearIndex();
246       SMESH::UpdateView();
247       SMESHGUI::Modified();
248     }
249   }
250 }
251
252 //=================================================================================
253 // function : ClickOnOk()
254 // purpose  :
255 //=================================================================================
256 void SMESHGUI_RemoveElementsDlg::ClickOnOk()
257 {
258   ClickOnApply();
259   ClickOnCancel();
260 }
261
262 //=================================================================================
263 // function : ClickOnCancel()
264 // purpose  :
265 //=================================================================================
266 void SMESHGUI_RemoveElementsDlg::ClickOnCancel()
267 {
268   if (SMESH::GetCurrentVtkView())
269     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
270   //mySelectionMgr->clearSelected();
271   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
272     aViewWindow->SetSelectionMode(ActorSelection);
273   disconnect(mySelectionMgr, 0, this, 0);
274   mySelectionMgr->clearFilters();
275   mySMESHGUI->ResetState();
276   reject();
277 }
278
279 //=================================================================================
280 // function : ClickOnHelp()
281 // purpose  :
282 //=================================================================================
283 void SMESHGUI_RemoveElementsDlg::ClickOnHelp()
284 {
285   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
286   if (app) 
287     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
288   else {
289     QString platform;
290 #ifdef WIN32
291     platform = "winapplication";
292 #else
293     platform = "application";
294 #endif
295     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
296                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
297                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
298                                                                  platform)).
299                              arg(myHelpFileName));
300   }
301 }
302
303 //=======================================================================
304 //function : onTextChange
305 //purpose  :
306 //=======================================================================
307 void SMESHGUI_RemoveElementsDlg::onTextChange(const QString& theNewText)
308 {
309   if (myBusy) return;
310   myBusy = true;
311
312   myNbOkElements = 0;
313
314   buttonOk->setEnabled(false);
315   buttonApply->setEnabled(false);
316
317   // hilight entered elements
318   if(myActor){
319     if(SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh()){
320       Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
321       
322       TColStd_MapOfInteger newIndices;
323       
324       QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
325       for (int i = 0; i < aListId.count(); i++) {
326         if(const SMDS_MeshElement *anElem = aMesh->FindElement(aListId[i].toInt())) {
327           newIndices.Add(anElem->GetID());
328           myNbOkElements++;
329         }
330       }
331       
332       mySelector->AddOrRemoveIndex(anIO,newIndices,false);
333       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
334         aViewWindow->highlight(anIO,true,true);
335     }
336   }
337   
338   if (myNbOkElements) {
339     buttonOk->setEnabled(true);
340     buttonApply->setEnabled(true);
341   }
342   
343   myBusy = false;
344 }
345
346 //=================================================================================
347 // function : SelectionIntoArgument()
348 // purpose  : Called when selection as changed or other case
349 //=================================================================================
350 void SMESHGUI_RemoveElementsDlg::SelectionIntoArgument()
351 {
352   if (myBusy) return;
353
354   // clear
355
356   myNbOkElements = false;
357   myActor = 0;
358
359   myBusy = true;
360   myEditCurrentArgument->setText("");
361   myBusy = false;
362
363   if (!GroupButtons->isEnabled()) // inactive
364     return;
365
366   buttonOk->setEnabled(false);
367   buttonApply->setEnabled(false);
368
369   // get selected mesh
370
371   SALOME_ListIO aList;
372   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
373
374   int nbSel = aList.Extent();
375   if (nbSel != 1)
376     return;
377
378   Handle(SALOME_InteractiveObject) anIO = aList.First();
379   myMesh = SMESH::GetMeshByIO(anIO);
380   if (myMesh->_is_nil())
381     return;
382
383   myActor = SMESH::FindActorByEntry(anIO->getEntry());
384   if (!myActor)
385     return;
386
387   // get selected nodes
388   QString aString = "";
389   int nbElems = SMESH::GetNameOfSelectedElements(mySelector,anIO,aString);
390   if(nbElems < 1)
391     return;
392   myBusy = true;
393   myEditCurrentArgument->setText(aString);
394   myBusy = false;
395
396   // OK
397
398   myNbOkElements = nbElems;
399
400   buttonOk->setEnabled(true);
401   buttonApply->setEnabled(true);
402 }
403
404 //=================================================================================
405 // function : SetEditCurrentArgument()
406 // purpose  :
407 //=================================================================================
408 void SMESHGUI_RemoveElementsDlg::SetEditCurrentArgument()
409 {
410   QPushButton* send = (QPushButton*)sender();
411   switch (myConstructorId) {
412   case 0: /* default constructor */
413     {
414       if(send == SelectButtonC1A1) {
415         LineEditC1A1->setFocus();
416         myEditCurrentArgument = LineEditC1A1;
417       }
418       SelectionIntoArgument();
419       break;
420     }
421   }
422 }
423
424 //=================================================================================
425 // function : DeactivateActiveDialog()
426 // purpose  :
427 //=================================================================================
428 void SMESHGUI_RemoveElementsDlg::DeactivateActiveDialog()
429 {
430   if (GroupConstructors->isEnabled()) {
431     GroupConstructors->setEnabled(false);
432     GroupC1->setEnabled(false);
433     GroupButtons->setEnabled(false);
434     mySMESHGUI->ResetState(); // ??
435     mySMESHGUI->SetActiveDialogBox(0); // ??
436   }
437 }
438
439 //=================================================================================
440 // function : ActivateThisDialog()
441 // purpose  :
442 //=================================================================================
443 void SMESHGUI_RemoveElementsDlg::ActivateThisDialog()
444 {
445   /* Emit a signal to deactivate the active dialog */
446   mySMESHGUI->EmitSignalDeactivateDialog();
447
448   GroupConstructors->setEnabled(true);
449   GroupC1->setEnabled(true);
450   GroupButtons->setEnabled(true);
451
452   mySMESHGUI->SetActiveDialogBox((QDialog*)this); // ??
453
454   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
455     aViewWindow->SetSelectionMode(CellSelection);
456
457   SelectionIntoArgument(); // ??
458 }
459
460 //=================================================================================
461 // function : enterEvent()
462 // purpose  :
463 //=================================================================================
464 void SMESHGUI_RemoveElementsDlg::enterEvent(QEvent*)
465 {
466   if (!GroupConstructors->isEnabled())
467     ActivateThisDialog();
468 }
469
470 //=================================================================================
471 // function : closeEvent()
472 // purpose  :
473 //=================================================================================
474 void SMESHGUI_RemoveElementsDlg::closeEvent(QCloseEvent*)
475 {
476   /* same than click on cancel button */
477   ClickOnCancel();
478 }
479
480 //=======================================================================
481 //function : hideEvent
482 //purpose  : caused by ESC key
483 //=======================================================================
484 void SMESHGUI_RemoveElementsDlg::hideEvent( QHideEvent* )
485 {
486   if (!isMinimized())
487     ClickOnCancel();
488 }
489
490 //=================================================================================
491 // function : keyPressEvent()
492 // purpose  :
493 //=================================================================================
494 void SMESHGUI_RemoveElementsDlg::keyPressEvent( QKeyEvent* e )
495 {
496   QDialog::keyPressEvent( e );
497   if ( e->isAccepted() )
498     return;
499
500   if ( e->key() == Qt::Key_F1 ) {
501     e->accept();
502     ClickOnHelp();
503   }
504 }
505
506 //=================================================================================
507 // function : setFilters()
508 // purpose  : SLOT. Called when "Filter" button pressed.
509 //=================================================================================
510 void SMESHGUI_RemoveElementsDlg::setFilters()
511 {
512   if(myMesh->_is_nil()) {
513     SUIT_MessageBox::critical(this,
514                               tr("SMESH_ERROR"),
515                               tr("NO_MESH_SELECTED"));
516    return;
517   }
518   if ( !myFilterDlg )
519     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
520
521   myFilterDlg->SetSelection();
522   myFilterDlg->SetMesh( myMesh );
523   myFilterDlg->SetSourceWg( LineEditC1A1 );
524
525   myFilterDlg->show();
526 }