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