Salome HOME
IPAL21120 SIGSEGV on Meshing attached Compound with Automatic Hexadralization
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_RemoveNodesDlg.cxx
1 //  Copyright (C) 2007-2008  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 //  SMESH SMESHGUI : GUI for SMESH component
23 //  File   : SMESHGUI_RemoveNodesDlg.cxx
24 //  Author : Nicolas REJNERI
25 //  Module : SMESH
26 //  $Header$
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
36 #include "SMESH_Actor.h"
37 #include "SMDS_Mesh.hxx"
38
39 #include "SUIT_ResourceMgr.h"
40 #include "SUIT_Desktop.h"
41 #include "SUIT_Session.h"
42 #include "SUIT_MessageBox.h"
43
44 #include "LightApp_Application.h"
45
46 #include "SVTK_Selector.h"
47 #include "SVTK_ViewModel.h"
48 #include "SVTK_ViewWindow.h"
49 #include "SALOME_ListIO.hxx"
50
51 #include "utilities.h"
52
53 // OCCT Includes
54 #include <TColStd_MapOfInteger.hxx>
55 #include <TColStd_IndexedMapOfInteger.hxx>
56
57 // QT Includes
58 #include <qbuttongroup.h>
59 #include <qgroupbox.h>
60 #include <qlabel.h>
61 #include <qlineedit.h>
62 #include <qpushbutton.h>
63 #include <qradiobutton.h>
64 #include <qlayout.h>
65 #include <qvariant.h>
66 #include <qtooltip.h>
67 #include <qwhatsthis.h>
68 #include <qimage.h>
69 #include <qpixmap.h>
70
71 using namespace std;
72
73 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
74
75 //=================================================================================
76 // class    : SMESHGUI_RemoveNodesDlg()
77 // purpose  :
78 //=================================================================================
79 SMESHGUI_RemoveNodesDlg
80 ::SMESHGUI_RemoveNodesDlg(SMESHGUI* theModule, 
81                           const char* name,
82                           bool modal, 
83                           WFlags fl)
84   : QDialog(SMESH::GetDesktop(theModule), 
85             name, 
86             modal, 
87             WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),
88     mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
89     mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
90     mySMESHGUI(theModule),
91     myBusy(false)
92 {
93     QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_REM_NODE")));
94     QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
95
96     if (!name)
97         setName("SMESHGUI_RemoveNodesDlg");
98     resize(303, 185);
99     setCaption(tr("SMESH_REMOVE_NODES_TITLE" ));
100     setSizeGripEnabled(TRUE);
101     SMESHGUI_RemoveNodesDlgLayout = new QGridLayout(this);
102     SMESHGUI_RemoveNodesDlgLayout->setSpacing(6);
103     SMESHGUI_RemoveNodesDlgLayout->setMargin(11);
104
105     /***************************************************************/
106     GroupConstructors = new QButtonGroup(this, "GroupConstructors");
107     GroupConstructors->setTitle(tr("SMESH_NODES" ));
108     GroupConstructors->setExclusive(TRUE);
109     GroupConstructors->setColumnLayout(0, Qt::Vertical);
110     GroupConstructors->layout()->setSpacing(0);
111     GroupConstructors->layout()->setMargin(0);
112     GroupConstructorsLayout = new QGridLayout(GroupConstructors->layout());
113     GroupConstructorsLayout->setAlignment(Qt::AlignTop);
114     GroupConstructorsLayout->setSpacing(6);
115     GroupConstructorsLayout->setMargin(11);
116     Constructor1 = new QRadioButton(GroupConstructors, "Constructor1");
117     Constructor1->setText(tr("" ));
118     Constructor1->setPixmap(image0);
119     Constructor1->setChecked(TRUE);
120     Constructor1->setSizePolicy(QSizePolicy((QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth()));
121     Constructor1->setMinimumSize(QSize(50, 0));
122     GroupConstructorsLayout->addWidget(Constructor1, 0, 0);
123     QSpacerItem* spacer = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
124     GroupConstructorsLayout->addItem(spacer, 0, 1);
125     SMESHGUI_RemoveNodesDlgLayout->addWidget(GroupConstructors, 0, 0);
126
127     /***************************************************************/
128     GroupButtons = new QGroupBox(this, "GroupButtons");
129     GroupButtons->setGeometry(QRect(10, 10, 281, 48));
130     GroupButtons->setTitle(tr("" ));
131     GroupButtons->setColumnLayout(0, Qt::Vertical);
132     GroupButtons->layout()->setSpacing(0);
133     GroupButtons->layout()->setMargin(0);
134     GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
135     GroupButtonsLayout->setAlignment(Qt::AlignTop);
136     GroupButtonsLayout->setSpacing(6);
137     GroupButtonsLayout->setMargin(11);
138     buttonHelp = new QPushButton(GroupButtons, "buttonHelp");
139     buttonHelp->setText(tr("SMESH_BUT_HELP" ));
140     buttonHelp->setAutoDefault(TRUE);
141     GroupButtonsLayout->addWidget(buttonHelp, 0, 4);
142     buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
143     buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
144     buttonCancel->setAutoDefault(TRUE);
145     GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
146     buttonApply = new QPushButton(GroupButtons, "buttonApply");
147     buttonApply->setText(tr("SMESH_BUT_APPLY" ));
148     buttonApply->setAutoDefault(TRUE);
149     GroupButtonsLayout->addWidget(buttonApply, 0, 1);
150     QSpacerItem* spacer_9 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
151     GroupButtonsLayout->addItem(spacer_9, 0, 2);
152     buttonOk = new QPushButton(GroupButtons, "buttonOk");
153     buttonOk->setText(tr("SMESH_BUT_APPLY_AND_CLOSE" ));
154     buttonOk->setAutoDefault(TRUE);
155     buttonOk->setDefault(TRUE);
156     GroupButtonsLayout->addWidget(buttonOk, 0, 0);
157     SMESHGUI_RemoveNodesDlgLayout->addWidget(GroupButtons, 2, 0);
158
159     /***************************************************************/
160     GroupC1 = new QGroupBox(this, "GroupC1");
161     GroupC1->setTitle(tr("SMESH_REMOVE" ));
162     GroupC1->setMinimumSize(QSize(0, 0));
163     GroupC1->setFrameShape(QGroupBox::Box);
164     GroupC1->setFrameShadow(QGroupBox::Sunken);
165     GroupC1->setColumnLayout(0, Qt::Vertical);
166     GroupC1->layout()->setSpacing(0);
167     GroupC1->layout()->setMargin(0);
168     GroupC1Layout = new QGridLayout(GroupC1->layout());
169     GroupC1Layout->setAlignment(Qt::AlignTop);
170     GroupC1Layout->setSpacing(6);
171     GroupC1Layout->setMargin(11);
172     TextLabelC1A1 = new QLabel(GroupC1, "TextLabelC1A1");
173     TextLabelC1A1->setText(tr("SMESH_ID_NODES" ));
174     TextLabelC1A1->setMinimumSize(QSize(50, 0));
175     TextLabelC1A1->setFrameShape(QLabel::NoFrame);
176     TextLabelC1A1->setFrameShadow(QLabel::Plain);
177     GroupC1Layout->addWidget(TextLabelC1A1, 0, 0);
178     SelectButtonC1A1 = new QPushButton(GroupC1, "SelectButtonC1A1");
179     SelectButtonC1A1->setText(tr("" ));
180     SelectButtonC1A1->setPixmap(image1);
181     SelectButtonC1A1->setToggleButton(FALSE);
182     GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
183     LineEditC1A1 = new QLineEdit(GroupC1, "LineEditC1A1");
184     LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this, "validator"));
185     GroupC1Layout->addWidget(LineEditC1A1, 0, 2);
186     SMESHGUI_RemoveNodesDlgLayout->addWidget(GroupC1, 1, 0);
187
188     myHelpFileName = "removing_nodes_and_elements_page.html#removing_nodes_anchor";
189
190     Init(); /* Initialisations */
191 }
192
193 //=================================================================================
194 // function : ~SMESHGUI_RemoveNodesDlg()
195 // purpose  : Destroys the object and frees any allocated resources
196 //=================================================================================
197 SMESHGUI_RemoveNodesDlg::~SMESHGUI_RemoveNodesDlg()
198 {
199   // no need to delete child widgets, Qt does it all for us
200 }
201
202 //=================================================================================
203 // function : Init()
204 // purpose  :
205 //=================================================================================
206 void SMESHGUI_RemoveNodesDlg::Init()
207 {
208   GroupC1->show();
209   myConstructorId = 0;
210   Constructor1->setChecked(TRUE);
211   myEditCurrentArgument = LineEditC1A1;
212
213   myNbOkNodes = 0;
214   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
215   myActor = 0;
216   myBusy = false;
217
218   /* signals and slots connections */
219   connect(buttonOk, SIGNAL(clicked()),     this, SLOT(ClickOnOk()));
220   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
221   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
222   connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
223   connect(GroupConstructors, SIGNAL(clicked(int)), SLOT(ConstructorsClicked(int)));
224
225   connect(SelectButtonC1A1, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
226   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
227   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
228   /* to close dialog if study change */
229   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
230   connect(myEditCurrentArgument, SIGNAL(textChanged(const QString&)),
231           SLOT(onTextChange(const QString&)));
232   
233   this->show(); /* displays Dialog */
234
235   SMESH::SetPointRepresentation(true);
236   
237   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
238     aViewWindow->SetSelectionMode(NodeSelection);
239
240   SelectionIntoArgument();
241 }
242
243 //=================================================================================
244 // function : ConstructorsClicked()
245 // purpose  : Radio button management
246 //=================================================================================
247 void SMESHGUI_RemoveNodesDlg::ConstructorsClicked (int)
248 {
249 }
250
251 //=================================================================================
252 // function : ClickOnApply()
253 // purpose  :
254 //=================================================================================
255 void SMESHGUI_RemoveNodesDlg::ClickOnApply()
256 {
257   if (mySMESHGUI->isActiveStudyLocked())
258     return;
259
260   if (myNbOkNodes) {
261     QStringList aListId = QStringList::split(" ", myEditCurrentArgument->text(), false);
262     SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
263     anArrayOfIdeces->length(aListId.count());
264     for (int i = 0; i < aListId.count(); i++)
265       anArrayOfIdeces[i] = aListId[ i ].toInt();
266
267     bool aResult = false;
268     try {
269       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
270       aResult = aMeshEditor->RemoveNodes(anArrayOfIdeces.inout());
271     } catch(...) {
272     }
273
274     if (aResult) {
275       myEditCurrentArgument->clear();
276       mySelector->ClearIndex();
277       SMESH::UpdateView();
278     }
279
280     SMESH::SetPointRepresentation(true);
281   }
282 }
283
284 //=================================================================================
285 // function : ClickOnOk()
286 // purpose  :
287 //=================================================================================
288 void SMESHGUI_RemoveNodesDlg::ClickOnOk()
289 {
290   ClickOnApply();
291   ClickOnCancel();
292 }
293
294 //=================================================================================
295 // function : ClickOnCancel()
296 // purpose  :
297 //=================================================================================
298 void SMESHGUI_RemoveNodesDlg::ClickOnCancel()
299 {
300   //mySelectionMgr->clearSelected();
301   SMESH::SetPointRepresentation(false);
302   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
303     aViewWindow->SetSelectionMode(ActorSelection);
304   disconnect(mySelectionMgr, 0, this, 0);
305   mySMESHGUI->ResetState();
306   reject();
307 }
308
309 //=================================================================================
310 // function : ClickOnHelp()
311 // purpose  :
312 //=================================================================================
313 void SMESHGUI_RemoveNodesDlg::ClickOnHelp()
314 {
315   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
316   if (app) 
317     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
318   else {
319                 QString platform;
320 #ifdef WIN32
321                 platform = "winapplication";
322 #else
323                 platform = "application";
324 #endif
325     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
326                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
327                            arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
328                            QObject::tr("BUT_OK"));
329   }
330 }
331
332 //=======================================================================
333 //function : onTextChange
334 //purpose  :
335 //=======================================================================
336 void SMESHGUI_RemoveNodesDlg::onTextChange (const QString& theNewText)
337 {
338   if (myBusy) return;
339   myBusy = true;
340
341   myNbOkNodes = 0;
342
343   buttonOk->setEnabled(false);
344   buttonApply->setEnabled(false);
345
346   // hilight entered nodes
347   if(myActor){
348     if(SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh()){
349       Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
350            
351       TColStd_MapOfInteger newIndices;
352       
353       QStringList aListId = QStringList::split(" ", theNewText, false);
354       for (int i = 0; i < aListId.count(); i++) {
355         if (const SMDS_MeshNode *aNode = aMesh->FindNode(aListId[i].toInt())) {
356           newIndices.Add(aNode->GetID());
357           myNbOkNodes++;
358         }
359       }
360
361       mySelector->AddOrRemoveIndex(anIO,newIndices,false);
362       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
363         aViewWindow->highlight(anIO,true,true);
364     }
365   }
366
367   if (myNbOkNodes) {
368     buttonOk->setEnabled(true);
369     buttonApply->setEnabled(true);
370   }
371
372   myBusy = false;
373 }
374
375 //=================================================================================
376 // function : SelectionIntoArgument()
377 // purpose  : Called when selection as changed or other case
378 //=================================================================================
379 void SMESHGUI_RemoveNodesDlg::SelectionIntoArgument()
380 {
381   if (myBusy) return;
382
383   // clear
384
385   myNbOkNodes = false;
386   myActor = 0;
387
388   myBusy = true;
389   myEditCurrentArgument->setText("");
390   myBusy = false;
391
392   if (!GroupButtons->isEnabled()) // inactive
393     return;
394
395   buttonOk->setEnabled(false);
396   buttonApply->setEnabled(false);
397
398   // get selected mesh
399   SALOME_ListIO aList;
400   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
401
402   int nbSel = aList.Extent();
403   if (nbSel != 1)
404     return;
405
406   Handle(SALOME_InteractiveObject) anIO = aList.First();
407   myMesh = SMESH::GetMeshByIO(anIO);
408   if (myMesh->_is_nil())
409     return;
410
411   myActor = SMESH::FindActorByEntry(anIO->getEntry());
412   if (!myActor)
413     return;
414
415   // get selected nodes
416
417   QString aString = "";
418   int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,anIO,aString);
419   if(nbNodes < 1)
420     return;
421   myBusy = true;
422   myEditCurrentArgument->setText(aString);
423   myBusy = false;
424
425   // OK
426
427   myNbOkNodes = true;
428
429   buttonOk->setEnabled(true);
430   buttonApply->setEnabled(true);
431 }
432
433 //=================================================================================
434 // function : SetEditCurrentArgument()
435 // purpose  :
436 //=================================================================================
437 void SMESHGUI_RemoveNodesDlg::SetEditCurrentArgument()
438 {
439   QPushButton* send = (QPushButton*)sender();
440   switch (myConstructorId)
441     {
442     case 0: /* default constructor */
443       {
444         if(send == SelectButtonC1A1) {
445           LineEditC1A1->setFocus();
446           myEditCurrentArgument = LineEditC1A1;
447         }
448         SelectionIntoArgument();
449         break;
450       }
451     }
452   return;
453 }
454
455 //=================================================================================
456 // function : DeactivateActiveDialog()
457 // purpose  :
458 //=================================================================================
459 void SMESHGUI_RemoveNodesDlg::DeactivateActiveDialog()
460 {
461   if (GroupConstructors->isEnabled()) {
462     GroupConstructors->setEnabled(false);
463     GroupC1->setEnabled(false);
464     GroupButtons->setEnabled(false);
465     mySMESHGUI->ResetState(); // ??
466     mySMESHGUI->SetActiveDialogBox(0); // ??
467   }
468 }
469
470 //=================================================================================
471 // function : ActivateThisDialog()
472 // purpose  :
473 //=================================================================================
474 void SMESHGUI_RemoveNodesDlg::ActivateThisDialog()
475 {
476   /* Emit a signal to deactivate the active dialog */
477   mySMESHGUI->EmitSignalDeactivateDialog();
478
479   GroupConstructors->setEnabled(true);
480   GroupC1->setEnabled(true);
481   GroupButtons->setEnabled(true);
482
483   mySMESHGUI->SetActiveDialogBox((QDialog*)this); // ??
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     ActivateThisDialog();
500 }
501
502 //=================================================================================
503 // function : closeEvent()
504 // purpose  :
505 //=================================================================================
506 void SMESHGUI_RemoveNodesDlg::closeEvent (QCloseEvent*)
507 {
508   /* same than click on cancel button */
509   this->ClickOnCancel();
510   return;
511 }
512
513 //=======================================================================
514 //function : hideEvent
515 //purpose  : caused by ESC key
516 //=======================================================================
517 void SMESHGUI_RemoveNodesDlg::hideEvent (QHideEvent * e)
518 {
519   if (!isMinimized())
520     ClickOnCancel();
521 }
522
523 //=================================================================================
524 // function : keyPressEvent()
525 // purpose  :
526 //=================================================================================
527 void SMESHGUI_RemoveNodesDlg::keyPressEvent( QKeyEvent* e )
528 {
529   QDialog::keyPressEvent( e );
530   if ( e->isAccepted() )
531     return;
532
533   if ( e->key() == Key_F1 )
534     {
535       e->accept();
536       ClickOnHelp();
537     }
538 }