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