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