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