Salome HOME
23080: [CEA 1497] Do not merge a middle node in quadratic with the extreme nodes...
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_RemoveElementsDlg.cxx
1 // Copyright (C) 2007-2015  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 // 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   LineEditC1A1->setMaxLength(-1);
127   QPushButton* filterBtn = new QPushButton( tr( "SMESH_BUT_FILTER" ), GroupC1 );
128   connect(filterBtn,   SIGNAL(clicked()), this, SLOT(setFilters()));
129
130   GroupC1Layout->addWidget(TextLabelC1A1);
131   GroupC1Layout->addWidget(SelectButtonC1A1);
132   GroupC1Layout->addWidget(LineEditC1A1);
133   GroupC1Layout->addWidget(filterBtn );
134
135   /***************************************************************/
136   GroupButtons = new QGroupBox(this);
137   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
138   GroupButtonsLayout->setSpacing(SPACING);
139   GroupButtonsLayout->setMargin(MARGIN);
140
141   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
142   buttonOk->setAutoDefault(true);
143   buttonOk->setDefault(true);
144   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
145   buttonApply->setAutoDefault(true);
146   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
147   buttonCancel->setAutoDefault(true);
148   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
149   buttonHelp->setAutoDefault(true);
150
151   GroupButtonsLayout->addWidget(buttonOk);
152   GroupButtonsLayout->addSpacing(10);
153   GroupButtonsLayout->addWidget(buttonApply);
154   GroupButtonsLayout->addSpacing(10);
155   GroupButtonsLayout->addStretch();
156   GroupButtonsLayout->addWidget(buttonCancel);
157   GroupButtonsLayout->addWidget(buttonHelp);
158
159   /***************************************************************/
160   SMESHGUI_RemoveElementsDlgLayout->addWidget(GroupConstructors);
161   SMESHGUI_RemoveElementsDlgLayout->addWidget(GroupC1);
162   SMESHGUI_RemoveElementsDlgLayout->addWidget(GroupButtons);
163
164   myHelpFileName = "removing_nodes_and_elements_page.html#removing_elements_anchor";
165
166   Init(); /* Initialisations */
167 }
168
169 //=================================================================================
170 // function : ~SMESHGUI_RemoveElementsDlg()
171 // purpose  : Destroys the object and frees any allocated resources
172 //=================================================================================
173 SMESHGUI_RemoveElementsDlg::~SMESHGUI_RemoveElementsDlg()
174 {
175   if ( myFilterDlg ) {
176     myFilterDlg->setParent( 0 );
177     delete myFilterDlg;
178     myFilterDlg = 0;
179   }
180 }
181
182 //=================================================================================
183 // function : Init()
184 // purpose  :
185 //=================================================================================
186 void SMESHGUI_RemoveElementsDlg::Init()
187 {
188   myConstructorId = 0;
189   Constructor1->setChecked(true);
190   myEditCurrentArgument = LineEditC1A1;
191
192   myNbOkElements = 0;
193   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
194   myActor = 0;
195   myBusy = false;
196
197   /* signals and slots connections */
198   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
199   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
200   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
201   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
202
203   connect(SelectButtonC1A1, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
204   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
205   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
206   /* to close dialog if study change */
207   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()),      this, SLOT(reject()));
208   connect(mySMESHGUI, SIGNAL (SignalActivatedViewManager()), this, SLOT(onOpenView()));
209   connect(mySMESHGUI, SIGNAL (SignalCloseView()),            this, SLOT(onCloseView()));
210   connect(myEditCurrentArgument, SIGNAL(textChanged(const QString&)),
211           SLOT(onTextChange(const QString&)));
212
213   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
214     aViewWindow->SetSelectionMode(CellSelection);
215
216   SelectionIntoArgument();
217 }
218
219 //=================================================================================
220 // function : ClickOnApply()
221 // purpose  :
222 //=================================================================================
223 void SMESHGUI_RemoveElementsDlg::ClickOnApply()
224 {
225   if (mySMESHGUI->isActiveStudyLocked())
226     return;
227
228   if (myNbOkElements) {
229     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
230     SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
231     anArrayOfIdeces->length(aListId.count());
232     for (int i = 0; i < aListId.count(); i++)
233       anArrayOfIdeces[i] = aListId[ i ].toInt();
234
235     bool aResult = false;
236     try {
237       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
238       aResult = aMeshEditor->RemoveElements(anArrayOfIdeces.in());
239     } catch (const SALOME::SALOME_Exception& S_ex) {
240       SalomeApp_Tools::QtCatchCorbaException(S_ex);
241       myEditCurrentArgument->clear();
242     } catch (...){
243       myEditCurrentArgument->clear();
244     }
245
246     if (aResult) {
247       myEditCurrentArgument->clear();
248       mySelector->ClearIndex();
249       SMESH::UpdateView();
250       SMESHGUI::Modified();
251     }
252   }
253 }
254
255 //=================================================================================
256 // function : ClickOnOk()
257 // purpose  :
258 //=================================================================================
259 void SMESHGUI_RemoveElementsDlg::ClickOnOk()
260 {
261   ClickOnApply();
262   reject();
263 }
264
265 //=================================================================================
266 // function : reject()
267 // purpose  :
268 //=================================================================================
269 void SMESHGUI_RemoveElementsDlg::reject()
270 {
271   if (SMESH::GetCurrentVtkView())
272     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
273   //mySelectionMgr->clearSelected();
274   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
275     aViewWindow->SetSelectionMode(ActorSelection);
276   disconnect(mySelectionMgr, 0, this, 0);
277   mySelectionMgr->clearFilters();
278   mySMESHGUI->ResetState();
279   QDialog::reject();
280 }
281
282 //=================================================================================
283 // function : onOpenView()
284 // purpose  :
285 //=================================================================================
286 void SMESHGUI_RemoveElementsDlg::onOpenView()
287 {
288   if(!mySelector) {
289     mySelector = SMESH::GetViewWindow( mySMESHGUI )->GetSelector();
290     ActivateThisDialog();
291   }
292 }
293
294 //=================================================================================
295 // function : onCloseView()
296 // purpose  :
297 //=================================================================================
298 void SMESHGUI_RemoveElementsDlg::onCloseView()
299 {
300   DeactivateActiveDialog();
301   mySelector = 0;
302 }
303
304 //=================================================================================
305 // function : ClickOnHelp()
306 // purpose  :
307 //=================================================================================
308 void SMESHGUI_RemoveElementsDlg::ClickOnHelp()
309 {
310   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
311   if (app) 
312     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
313   else {
314     QString platform;
315 #ifdef WIN32
316     platform = "winapplication";
317 #else
318     platform = "application";
319 #endif
320     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
321                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
322                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
323                                                                  platform)).
324                              arg(myHelpFileName));
325   }
326 }
327
328 //=======================================================================
329 //function : onTextChange
330 //purpose  :
331 //=======================================================================
332 void SMESHGUI_RemoveElementsDlg::onTextChange(const QString& theNewText)
333 {
334   if (myBusy) return;
335   myBusy = true;
336
337   myNbOkElements = 0;
338
339   // hilight entered elements
340   if(myActor){
341     if(SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh()){
342       Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
343       
344       TColStd_MapOfInteger newIndices;
345       
346       QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
347       for (int i = 0; i < aListId.count(); i++) {
348         if(const SMDS_MeshElement *anElem = aMesh->FindElement(aListId[i].toInt())) {
349           newIndices.Add(anElem->GetID());
350           myNbOkElements++;
351         }
352       }
353       
354       mySelector->AddOrRemoveIndex(anIO,newIndices,false);
355       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
356         aViewWindow->highlight(anIO,true,true);
357     }
358   }
359   else
360   {
361     QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
362     myNbOkElements = aListId.count();
363   }
364   
365   myBusy = false;
366   updateButtons();
367 }
368
369 //=================================================================================
370 // function : SelectionIntoArgument()
371 // purpose  : Called when selection as changed or other case
372 //=================================================================================
373 void SMESHGUI_RemoveElementsDlg::SelectionIntoArgument()
374 {
375   if (myBusy) return;                                  // busy
376   if (myFilterDlg && myFilterDlg->isVisible()) return; // filter digl active
377   if (!GroupButtons->isEnabled()) return;              // inactive
378
379   // clear
380
381   myNbOkElements = 0;
382   myActor = 0;
383
384   myBusy = true;
385   myEditCurrentArgument->setText("");
386   myBusy = false;
387
388   // get selected mesh
389
390   SALOME_ListIO aList;
391   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
392
393   int nbSel = aList.Extent();
394   if (nbSel == 1) {
395
396     Handle(SALOME_InteractiveObject) anIO = aList.First();
397     myMesh = SMESH::GetMeshByIO(anIO);
398
399     if (!myMesh->_is_nil()) {
400
401       myActor = SMESH::FindActorByEntry(anIO->getEntry());
402       if (myActor) {
403         
404         // get selected elements
405         QString aString = "";
406         int nbElems = SMESH::GetNameOfSelectedElements(mySelector,anIO,aString);
407         if (nbElems > 0) {
408           myBusy = true;
409           myEditCurrentArgument->setText(aString);
410           myBusy = false;
411
412           // OK
413
414           myNbOkElements = nbElems;
415         } // if (nbElems > 0)
416       } // if (myActor)
417     } // if (!myMesh->_is_nil())
418   } // if (nbSel == 1) {
419
420   updateButtons();        
421 }
422
423 //=================================================================================
424 // function : SetEditCurrentArgument()
425 // purpose  :
426 //=================================================================================
427 void SMESHGUI_RemoveElementsDlg::SetEditCurrentArgument()
428 {
429   QPushButton* send = (QPushButton*)sender();
430   switch (myConstructorId) {
431   case 0: /* default constructor */
432     {
433       if(send == SelectButtonC1A1) {
434         LineEditC1A1->setFocus();
435         myEditCurrentArgument = LineEditC1A1;
436       }
437       SelectionIntoArgument();
438       break;
439     }
440   }
441 }
442
443 //=================================================================================
444 // function : DeactivateActiveDialog()
445 // purpose  :
446 //=================================================================================
447 void SMESHGUI_RemoveElementsDlg::DeactivateActiveDialog()
448 {
449   if (GroupConstructors->isEnabled()) {
450     GroupConstructors->setEnabled(false);
451     GroupC1->setEnabled(false);
452     GroupButtons->setEnabled(false);
453     mySMESHGUI->ResetState(); // ??
454     mySMESHGUI->SetActiveDialogBox(0); // ??
455   }
456 }
457
458 //=================================================================================
459 // function : ActivateThisDialog()
460 // purpose  :
461 //=================================================================================
462 void SMESHGUI_RemoveElementsDlg::ActivateThisDialog()
463 {
464   /* Emit a signal to deactivate the active dialog */
465   mySMESHGUI->EmitSignalDeactivateDialog();
466
467   GroupConstructors->setEnabled(true);
468   GroupC1->setEnabled(true);
469   GroupButtons->setEnabled(true);
470
471   mySMESHGUI->SetActiveDialogBox((QDialog*)this); // ??
472
473   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
474     aViewWindow->SetSelectionMode(CellSelection);
475
476   SelectionIntoArgument(); // ??
477 }
478
479 //=================================================================================
480 // function : enterEvent()
481 // purpose  :
482 //=================================================================================
483 void SMESHGUI_RemoveElementsDlg::enterEvent(QEvent*)
484 {
485   if (!GroupConstructors->isEnabled()) {
486     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
487     if ( aViewWindow && !mySelector) {
488       mySelector = aViewWindow->GetSelector();
489     }
490     ActivateThisDialog();
491   }
492 }
493
494 //=================================================================================
495 // function : keyPressEvent()
496 // purpose  :
497 //=================================================================================
498 void SMESHGUI_RemoveElementsDlg::keyPressEvent( QKeyEvent* e )
499 {
500   QDialog::keyPressEvent( e );
501   if ( e->isAccepted() )
502     return;
503
504   if ( e->key() == Qt::Key_F1 ) {
505     e->accept();
506     ClickOnHelp();
507   }
508 }
509
510 //=================================================================================
511 // function : setFilters()
512 // purpose  : SLOT. Called when "Filter" button pressed.
513 //=================================================================================
514 void SMESHGUI_RemoveElementsDlg::setFilters()
515 {
516   if(myMesh->_is_nil()) {
517     SUIT_MessageBox::critical(this,
518                               tr("SMESH_ERROR"),
519                               tr("NO_MESH_SELECTED"));
520    return;
521   }
522   if ( !myFilterDlg )
523     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
524
525   myFilterDlg->SetSelection();
526   myFilterDlg->SetMesh( myMesh );
527   myFilterDlg->SetSourceWg( LineEditC1A1 );
528
529   myFilterDlg->show();
530 }
531
532 //=================================================================================
533 // function : updateButtons
534 // purpose  : enable / disable control buttons
535 //=================================================================================
536 void SMESHGUI_RemoveElementsDlg::updateButtons()
537 {
538   buttonOk->setEnabled(myNbOkElements > 0);
539   buttonApply->setEnabled(myNbOkElements > 0);
540 }