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