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