Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_RemoveElementsDlg.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_RemoveElementsDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_RemoveElementsDlg.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 "SalomeApp_Tools.h"
49 #include "utilities.h"
50
51 // OCCT Includes
52 #include <TColStd_MapOfInteger.hxx>
53 #include <TColStd_IndexedMapOfInteger.hxx>
54
55 // QT Includes
56 #include <qbuttongroup.h>
57 #include <qgroupbox.h>
58 #include <qlabel.h>
59 #include <qlineedit.h>
60 #include <qpushbutton.h>
61 #include <qradiobutton.h>
62 #include <qlayout.h>
63 #include <qvariant.h>
64 #include <qtooltip.h>
65 #include <qwhatsthis.h>
66 #include <qimage.h>
67 #include <qpixmap.h>
68
69 using namespace std;
70
71 //=================================================================================
72 // class    : SMESHGUI_RemoveElementsDlg()
73 // purpose  :
74 //=================================================================================
75 SMESHGUI_RemoveElementsDlg
76 ::SMESHGUI_RemoveElementsDlg (SMESHGUI* theModule, 
77                               const char* name,
78                               bool modal, 
79                               WFlags fl)
80   : QDialog(SMESH::GetDesktop(theModule), 
81             name, 
82             modal, 
83             WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),
84     mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
85     mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
86     mySMESHGUI(theModule),
87     myBusy(false)
88 {
89     QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_REM_ELEMENT")));
90     QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
91
92     if (!name)
93       setName("SMESHGUI_RemoveElementsDlg");
94     resize(303, 185);
95     setCaption(tr("SMESH_REMOVE_ELEMENTS_TITLE"));
96     setSizeGripEnabled(TRUE);
97     SMESHGUI_RemoveElementsDlgLayout = new QGridLayout(this);
98     SMESHGUI_RemoveElementsDlgLayout->setSpacing(6);
99     SMESHGUI_RemoveElementsDlgLayout->setMargin(11);
100
101     /***************************************************************/
102     GroupConstructors = new QButtonGroup(this, "GroupConstructors");
103     GroupConstructors->setTitle(tr("SMESH_ELEMENTS" ));
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_RemoveElementsDlgLayout->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_RemoveElementsDlgLayout->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_ELEMENTS" ));
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_RemoveElementsDlgLayout->addWidget(GroupC1, 1, 0);
179
180     Init(); /* Initialisations */
181 }
182
183 //=================================================================================
184 // function : ~SMESHGUI_RemoveElementsDlg()
185 // purpose  : Destroys the object and frees any allocated resources
186 //=================================================================================
187 SMESHGUI_RemoveElementsDlg::~SMESHGUI_RemoveElementsDlg()
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_RemoveElementsDlg::Init()
197 {
198   GroupC1->show();
199   myConstructorId = 0;
200   Constructor1->setChecked(TRUE);
201   myEditCurrentArgument = LineEditC1A1;
202
203   myNbOkElements = false;
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   /* to close dialog if study change */
218   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
219   connect(myEditCurrentArgument, SIGNAL(textChanged(const QString&)),
220            SLOT(onTextChange(const QString&)));
221
222   this->show(); /* displays Dialog */
223
224   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
225     aViewWindow->SetSelectionMode(CellSelection);
226
227   SelectionIntoArgument();
228 }
229
230 //=================================================================================
231 // function : ConstructorsClicked()
232 // purpose  : Radio button management
233 //=================================================================================
234 void SMESHGUI_RemoveElementsDlg::ConstructorsClicked (int)
235 {
236 }
237
238 //=================================================================================
239 // function : ClickOnApply()
240 // purpose  :
241 //=================================================================================
242 void SMESHGUI_RemoveElementsDlg::ClickOnApply()
243 {
244   if (mySMESHGUI->isActiveStudyLocked())
245     return;
246   if (myNbOkElements) {
247     QStringList aListId = QStringList::split(" ", myEditCurrentArgument->text(), false);
248     SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
249     anArrayOfIdeces->length(aListId.count());
250     for (int i = 0; i < aListId.count(); i++)
251       anArrayOfIdeces[i] = aListId[ i ].toInt();
252
253     bool aResult = false;
254     try {
255       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
256       aResult = aMeshEditor->RemoveElements(anArrayOfIdeces.inout());
257     } catch (const SALOME::SALOME_Exception& S_ex) {
258       SalomeApp_Tools::QtCatchCorbaException(S_ex);
259       myEditCurrentArgument->clear();
260     } catch (...){
261       myEditCurrentArgument->clear();
262     }
263
264     if (aResult) {
265       myEditCurrentArgument->clear();
266       mySelector->ClearIndex();
267       SMESH::UpdateView();
268     }
269   }
270 }
271
272 //=================================================================================
273 // function : ClickOnOk()
274 // purpose  :
275 //=================================================================================
276 void SMESHGUI_RemoveElementsDlg::ClickOnOk()
277 {
278   this->ClickOnApply();
279   this->ClickOnCancel();
280
281   return;
282 }
283
284 //=================================================================================
285 // function : ClickOnCancel()
286 // purpose  :
287 //=================================================================================
288 void SMESHGUI_RemoveElementsDlg::ClickOnCancel()
289 {
290   //mySelectionMgr->clearSelected();
291   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
292     aViewWindow->SetSelectionMode(ActorSelection);
293   disconnect(mySelectionMgr, 0, this, 0);
294   mySMESHGUI->ResetState();
295   reject();
296   return;
297 }
298
299 //=======================================================================
300 //function : onTextChange
301 //purpose  :
302 //=======================================================================
303 void SMESHGUI_RemoveElementsDlg::onTextChange (const QString& theNewText)
304 {
305   if (myBusy) 
306     return;
307   myBusy = true;
308
309   myNbOkElements = 0;
310
311   buttonOk->setEnabled(false);
312   buttonApply->setEnabled(false);
313
314   // hilight entered elements
315   if(myActor){
316     if(SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh()){
317       Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
318       
319       TColStd_MapOfInteger newIndices;
320       
321       QStringList aListId = QStringList::split(" ", theNewText, false);
322       for (int i = 0; i < aListId.count(); i++) {
323         if(const SMDS_MeshElement *anElem = aMesh->FindElement(aListId[i].toInt())) {
324           newIndices.Add(anElem->GetID());
325           myNbOkElements++;
326         }
327       }
328       
329       mySelector->AddOrRemoveIndex(anIO,newIndices,false);
330       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
331         aViewWindow->highlight(anIO,true,true);
332     }
333   }
334   
335   if (myNbOkElements) {
336     buttonOk->setEnabled(true);
337     buttonApply->setEnabled(true);
338   }
339   
340   myBusy = false;
341 }
342
343 //=================================================================================
344 // function : SelectionIntoArgument()
345 // purpose  : Called when selection as changed or other case
346 //=================================================================================
347 void SMESHGUI_RemoveElementsDlg::SelectionIntoArgument()
348 {
349   if (myBusy) return;
350
351   // clear
352
353   myNbOkElements = false;
354   myActor = 0;
355
356   myBusy = true;
357   myEditCurrentArgument->setText("");
358   myBusy = false;
359
360   if (!GroupButtons->isEnabled()) // inactive
361     return;
362
363   buttonOk->setEnabled(false);
364   buttonApply->setEnabled(false);
365
366   // get selected mesh
367
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   QString aString = "";
386   int nbElems = SMESH::GetNameOfSelectedElements(mySelector,anIO,aString);
387   if(nbElems < 1)
388     return;
389   myBusy = true;
390   myEditCurrentArgument->setText(aString);
391   myBusy = false;
392
393   // OK
394
395   myNbOkElements = nbElems;
396
397   buttonOk->setEnabled(true);
398   buttonApply->setEnabled(true);
399 }
400
401 //=================================================================================
402 // function : SetEditCurrentArgument()
403 // purpose  :
404 //=================================================================================
405 void SMESHGUI_RemoveElementsDlg::SetEditCurrentArgument()
406 {
407   QPushButton* send = (QPushButton*)sender();
408   switch (myConstructorId)
409     {
410     case 0: /* default constructor */
411       {
412         if(send == SelectButtonC1A1) {
413           LineEditC1A1->setFocus();
414           myEditCurrentArgument = LineEditC1A1;
415         }
416         SelectionIntoArgument();
417         break;
418       }
419     }
420   return;
421 }
422
423 //=================================================================================
424 // function : DeactivateActiveDialog()
425 // purpose  :
426 //=================================================================================
427 void SMESHGUI_RemoveElementsDlg::DeactivateActiveDialog()
428 {
429   if (GroupConstructors->isEnabled()) {
430     GroupConstructors->setEnabled(false);
431     GroupC1->setEnabled(false);
432     GroupButtons->setEnabled(false);
433     mySMESHGUI->ResetState(); // ??
434     mySMESHGUI->SetActiveDialogBox(0); // ??
435   }
436 }
437
438 //=================================================================================
439 // function : ActivateThisDialog()
440 // purpose  :
441 //=================================================================================
442 void SMESHGUI_RemoveElementsDlg::ActivateThisDialog()
443 {
444   /* Emit a signal to deactivate the active dialog */
445   mySMESHGUI->EmitSignalDeactivateDialog();
446
447   GroupConstructors->setEnabled(true);
448   GroupC1->setEnabled(true);
449   GroupButtons->setEnabled(true);
450
451   mySMESHGUI->SetActiveDialogBox((QDialog*)this); // ??
452
453   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
454     aViewWindow->SetSelectionMode(NodeSelection);
455
456   SelectionIntoArgument(); // ??
457 }
458
459 //=================================================================================
460 // function : enterEvent()
461 // purpose  :
462 //=================================================================================
463 void SMESHGUI_RemoveElementsDlg::enterEvent (QEvent*)
464 {
465   if (!GroupConstructors->isEnabled())
466     ActivateThisDialog();
467 }
468
469 //=================================================================================
470 // function : closeEvent()
471 // purpose  :
472 //=================================================================================
473 void SMESHGUI_RemoveElementsDlg::closeEvent (QCloseEvent*)
474 {
475   /* same than click on cancel button */
476   this->ClickOnCancel();
477   return;
478 }
479
480 //=======================================================================
481 //function : hideEvent
482 //purpose  : caused by ESC key
483 //=======================================================================
484 void SMESHGUI_RemoveElementsDlg::hideEvent (QHideEvent * e)
485 {
486   if (!isMinimized())
487     ClickOnCancel();
488 }