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