Salome HOME
23080: [CEA 1497] Do not merge a middle node in quadratic with the extreme nodes...
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_RemoveNodesDlg.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_RemoveNodesDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_RemoveNodesDlg.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_RemoveNodesDlg()
78 // purpose  :
79 //=================================================================================
80 SMESHGUI_RemoveNodesDlg
81 ::SMESHGUI_RemoveNodesDlg(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_NODES_TITLE"));
92   setSizeGripEnabled(true);
93   
94   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_REM_NODE")));
95   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
96
97   QVBoxLayout* SMESHGUI_RemoveNodesDlgLayout = new QVBoxLayout(this);
98   SMESHGUI_RemoveNodesDlgLayout->setSpacing(SPACING);
99   SMESHGUI_RemoveNodesDlgLayout->setMargin(MARGIN);
100
101   /***************************************************************/
102   GroupConstructors = new QGroupBox(tr("SMESH_NODES"), 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_NODES"), 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_RemoveNodesDlgLayout->addWidget(GroupConstructors);
161   SMESHGUI_RemoveNodesDlgLayout->addWidget(GroupC1);
162   SMESHGUI_RemoveNodesDlgLayout->addWidget(GroupButtons);
163
164   myHelpFileName = "removing_nodes_and_elements_page.html#removing_nodes_anchor";
165
166   Init(); /* Initialisations */
167 }
168
169 //=================================================================================
170 // function : ~SMESHGUI_RemoveNodesDlg()
171 // purpose  : Destroys the object and frees any allocated resources
172 //=================================================================================
173 SMESHGUI_RemoveNodesDlg::~SMESHGUI_RemoveNodesDlg()
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_RemoveNodesDlg::Init()
187 {
188   myConstructorId = 0;
189   Constructor1->setChecked(true);
190   myEditCurrentArgument = LineEditC1A1;
191
192   myNbOkNodes = 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   SMESH::SetPointRepresentation(true);
214   
215   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
216     aViewWindow->SetSelectionMode(NodeSelection);
217
218   SelectionIntoArgument();
219 }
220
221 //=================================================================================
222 // function : ClickOnApply()
223 // purpose  :
224 //=================================================================================
225 void SMESHGUI_RemoveNodesDlg::ClickOnApply()
226 {
227   if (mySMESHGUI->isActiveStudyLocked())
228     return;
229
230   if (myNbOkNodes) {
231     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
232     SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
233     anArrayOfIdeces->length(aListId.count());
234     for (int i = 0; i < aListId.count(); i++)
235       anArrayOfIdeces[i] = aListId[ i ].toInt();
236
237     bool aResult = false;
238     try {
239       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
240       aResult = aMeshEditor->RemoveNodes(anArrayOfIdeces.in());
241     } catch (const SALOME::SALOME_Exception& S_ex) {
242       SalomeApp_Tools::QtCatchCorbaException(S_ex);
243       myEditCurrentArgument->clear();
244     } catch (...){
245       myEditCurrentArgument->clear();
246     }
247
248     if (aResult) {
249       myEditCurrentArgument->clear();
250       mySelector->ClearIndex();
251       SMESH::UpdateView();
252       SMESHGUI::Modified();
253     }
254
255     SMESH::SetPointRepresentation(true);
256   }
257 }
258
259 //=================================================================================
260 // function : ClickOnOk()
261 // purpose  :
262 //=================================================================================
263 void SMESHGUI_RemoveNodesDlg::ClickOnOk()
264 {
265   ClickOnApply();
266   reject();
267 }
268
269 //=================================================================================
270 // function : reject()
271 // purpose  :
272 //=================================================================================
273 void SMESHGUI_RemoveNodesDlg::reject()
274 {
275   //mySelectionMgr->clearSelected();
276   if (SMESH::GetCurrentVtkView()) {
277     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
278     SMESH::SetPointRepresentation(false);
279   }
280   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
281     aViewWindow->SetSelectionMode(ActorSelection);
282   disconnect(mySelectionMgr, 0, this, 0);
283   mySelectionMgr->clearFilters();
284   mySMESHGUI->ResetState();
285   QDialog::reject();
286 }
287
288
289 //=================================================================================
290 // function : onOpenView()
291 // purpose  :
292 //=================================================================================
293 void SMESHGUI_RemoveNodesDlg::onOpenView()
294 {
295   if ( mySelector) {
296     SMESH::SetPointRepresentation(false);
297   }
298   else {
299     mySelector = SMESH::GetViewWindow( mySMESHGUI )->GetSelector();
300     ActivateThisDialog();
301   }
302 }
303
304 //=================================================================================
305 // function : onCloseView()
306 // purpose  :
307 //=================================================================================
308 void SMESHGUI_RemoveNodesDlg::onCloseView()
309 {
310   DeactivateActiveDialog();
311   mySelector = 0;
312 }
313
314 //=================================================================================
315 // function : ClickOnHelp()
316 // purpose  :
317 //=================================================================================
318 void SMESHGUI_RemoveNodesDlg::ClickOnHelp()
319 {
320   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
321   if (app) 
322     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
323   else {
324     QString platform;
325 #ifdef WIN32
326     platform = "winapplication";
327 #else
328     platform = "application";
329 #endif
330     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
331                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
332                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
333                                                                  platform)).
334                              arg(myHelpFileName));
335   }
336 }
337
338 //=======================================================================
339 //function : onTextChange
340 //purpose  :
341 //=======================================================================
342 void SMESHGUI_RemoveNodesDlg::onTextChange(const QString& theNewText)
343 {
344   if (myBusy) return;
345   myBusy = true;
346
347   myNbOkNodes = 0;
348
349   // hilight entered nodes
350   if(myActor){
351     if(SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh()){
352       Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
353            
354       TColStd_MapOfInteger newIndices;
355       
356       QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
357       for (int i = 0; i < aListId.count(); i++) {
358         if (const SMDS_MeshNode *aNode = aMesh->FindNode(aListId[i].toInt())) {
359           newIndices.Add(aNode->GetID());
360           myNbOkNodes++;
361         }
362       }
363
364       mySelector->AddOrRemoveIndex(anIO,newIndices,false);
365       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
366         aViewWindow->highlight(anIO,true,true);
367     }
368   }
369
370   myBusy = false;
371   updateButtons();
372 }
373
374 //=================================================================================
375 // function : SelectionIntoArgument()
376 // purpose  : Called when selection as changed or other case
377 //=================================================================================
378 void SMESHGUI_RemoveNodesDlg::SelectionIntoArgument()
379 {
380   if (myBusy) return;                                  // busy
381   if (myFilterDlg && myFilterDlg->isVisible()) return; // filter dlg active
382   if (!GroupButtons->isEnabled()) return;              // inactive
383     
384   // clear
385
386   myNbOkNodes = 0;
387   myActor = 0;
388
389   myBusy = true;
390   myEditCurrentArgument->setText("");
391   myBusy = false;
392
393   // get selected mesh
394   SALOME_ListIO aList;
395   mySelectionMgr->selectedObjects(aList);
396
397   int nbSel = aList.Extent();
398   if (nbSel == 1) {
399
400     Handle(SALOME_InteractiveObject) anIO = aList.First();
401     myMesh = SMESH::GetMeshByIO(anIO);
402
403     if (!myMesh->_is_nil()) {
404
405       myActor = SMESH::FindActorByEntry(anIO->getEntry());
406       if (myActor) {
407
408         // get selected nodes
409         QString aString = "";
410         int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,anIO,aString);
411         if (nbNodes > 0) {
412           myBusy = true;
413           myEditCurrentArgument->setText(aString);
414           myBusy = false;
415           
416           // OK
417           
418           myNbOkNodes = nbNodes;
419         } // if (nbNodes > 0)
420       } // if (myActor)
421     } // if (!myMesh->_is_nil())
422   } // if (nbSel == 1)
423
424   updateButtons();        
425 }
426
427 //=================================================================================
428 // function : SetEditCurrentArgument()
429 // purpose  :
430 //=================================================================================
431 void SMESHGUI_RemoveNodesDlg::SetEditCurrentArgument()
432 {
433   QPushButton* send = (QPushButton*)sender();
434   switch (myConstructorId) {
435   case 0: /* default constructor */
436     {
437       if(send == SelectButtonC1A1) {
438         LineEditC1A1->setFocus();
439         myEditCurrentArgument = LineEditC1A1;
440       }
441       SelectionIntoArgument();
442       break;
443     }
444   }
445 }
446
447 //=================================================================================
448 // function : DeactivateActiveDialog()
449 // purpose  :
450 //=================================================================================
451 void SMESHGUI_RemoveNodesDlg::DeactivateActiveDialog()
452 {
453   if (GroupConstructors->isEnabled()) {
454     GroupConstructors->setEnabled(false);
455     GroupC1->setEnabled(false);
456     GroupButtons->setEnabled(false);
457     mySMESHGUI->ResetState(); // ??
458     mySMESHGUI->SetActiveDialogBox(0); // ??
459   }
460 }
461
462 //=================================================================================
463 // function : ActivateThisDialog()
464 // purpose  :
465 //=================================================================================
466 void SMESHGUI_RemoveNodesDlg::ActivateThisDialog()
467 {
468   /* Emit a signal to deactivate the active dialog */
469   mySMESHGUI->EmitSignalDeactivateDialog();
470
471   GroupConstructors->setEnabled(true);
472   GroupC1->setEnabled(true);
473   GroupButtons->setEnabled(true);
474
475   mySMESHGUI->SetActiveDialogBox((QDialog*)this); // ??
476
477   SMESH::SetPointRepresentation(true);
478   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
479     aViewWindow->SetSelectionMode(NodeSelection);
480
481   SelectionIntoArgument(); // ??
482 }
483
484 //=================================================================================
485 // function : enterEvent()
486 // purpose  :
487 //=================================================================================
488 void SMESHGUI_RemoveNodesDlg::enterEvent(QEvent*)
489 {
490   if (!GroupConstructors->isEnabled()) {
491     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
492     if ( aViewWindow && !mySelector) {
493       mySelector = aViewWindow->GetSelector();
494     }
495     ActivateThisDialog();
496   }
497 }
498
499 //=================================================================================
500 // function : keyPressEvent()
501 // purpose  :
502 //=================================================================================
503 void SMESHGUI_RemoveNodesDlg::keyPressEvent( QKeyEvent* e )
504 {
505   QDialog::keyPressEvent( e );
506   if ( e->isAccepted() )
507     return;
508
509   if ( e->key() == Qt::Key_F1 ) {
510     e->accept();
511     ClickOnHelp();
512   }
513 }
514
515 //=================================================================================
516 // function : setFilters()
517 // purpose  : SLOT. Called when "Filter" button pressed.
518 //=================================================================================
519 void SMESHGUI_RemoveNodesDlg::setFilters()
520 {
521   if(myMesh->_is_nil()) {
522     SUIT_MessageBox::critical(this,
523                               tr("SMESH_ERROR"),
524                               tr("NO_MESH_SELECTED"));
525    return;
526   }
527   if ( !myFilterDlg )
528     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::NODE );
529
530   myFilterDlg->SetSelection();
531   myFilterDlg->SetMesh( myMesh );
532   myFilterDlg->SetSourceWg( LineEditC1A1 );
533
534   myFilterDlg->show();
535 }
536
537 //=================================================================================
538 // function : updateButtons
539 // purpose  : enable / disable control buttons
540 //=================================================================================
541 void SMESHGUI_RemoveNodesDlg::updateButtons()
542 {
543   buttonOk->setEnabled(myNbOkNodes > 0);
544   buttonApply->setEnabled(myNbOkNodes > 0);
545 }