Salome HOME
Join BR-D5-38-2003
[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 "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_RemoveElementsDlg()
72 // purpose  :
73 //=================================================================================
74 SMESHGUI_RemoveElementsDlg
75 ::SMESHGUI_RemoveElementsDlg (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_ELEMENT")));
89     QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
90
91     if (!name)
92       setName("SMESHGUI_RemoveElementsDlg");
93     resize(303, 185);
94     setCaption(tr("SMESH_REMOVE_ELEMENTS_TITLE"));
95     setSizeGripEnabled(TRUE);
96     SMESHGUI_RemoveElementsDlgLayout = new QGridLayout(this);
97     SMESHGUI_RemoveElementsDlgLayout->setSpacing(6);
98     SMESHGUI_RemoveElementsDlgLayout->setMargin(11);
99
100     /***************************************************************/
101     GroupConstructors = new QButtonGroup(this, "GroupConstructors");
102     GroupConstructors->setTitle(tr("SMESH_ELEMENTS" ));
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_RemoveElementsDlgLayout->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_RemoveElementsDlgLayout->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_ELEMENTS" ));
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_RemoveElementsDlgLayout->addWidget(GroupC1, 1, 0);
178
179     Init(); /* Initialisations */
180 }
181
182 //=================================================================================
183 // function : ~SMESHGUI_RemoveElementsDlg()
184 // purpose  : Destroys the object and frees any allocated resources
185 //=================================================================================
186 SMESHGUI_RemoveElementsDlg::~SMESHGUI_RemoveElementsDlg()
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_RemoveElementsDlg::Init()
196 {
197   GroupC1->show();
198   myConstructorId = 0;
199   Constructor1->setChecked(TRUE);
200   myEditCurrentArgument = LineEditC1A1;
201
202   myNbOkElements = false;
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   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
228     aViewWindow->SetSelectionMode(CellSelection);
229
230   SelectionIntoArgument();
231 }
232
233 //=================================================================================
234 // function : ConstructorsClicked()
235 // purpose  : Radio button management
236 //=================================================================================
237 void SMESHGUI_RemoveElementsDlg::ConstructorsClicked (int)
238 {
239 }
240
241 //=================================================================================
242 // function : ClickOnApply()
243 // purpose  :
244 //=================================================================================
245 void SMESHGUI_RemoveElementsDlg::ClickOnApply()
246 {
247   if (mySMESHGUI->isActiveStudyLocked())
248     return;
249   if (myNbOkElements) {
250     QStringList aListId = QStringList::split(" ", myEditCurrentArgument->text(), false);
251     SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
252     anArrayOfIdeces->length(aListId.count());
253     for (int i = 0; i < aListId.count(); i++)
254       anArrayOfIdeces[i] = aListId[ i ].toInt();
255
256     bool aResult = false;
257     try {
258       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
259       aResult = aMeshEditor->RemoveElements(anArrayOfIdeces.inout());
260     } catch (...) {
261     }
262
263     if (aResult) {
264       myEditCurrentArgument->clear();
265       mySelector->ClearIndex();
266       SMESH::UpdateView();
267     }
268   }
269 }
270
271 //=================================================================================
272 // function : ClickOnOk()
273 // purpose  :
274 //=================================================================================
275 void SMESHGUI_RemoveElementsDlg::ClickOnOk()
276 {
277   this->ClickOnApply();
278   this->ClickOnCancel();
279
280   return;
281 }
282
283 //=================================================================================
284 // function : ClickOnCancel()
285 // purpose  :
286 //=================================================================================
287 void SMESHGUI_RemoveElementsDlg::ClickOnCancel()
288 {
289   //mySelectionMgr->clearSelected();
290   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
291     aViewWindow->SetSelectionMode(ActorSelection);
292   disconnect(mySelectionMgr, 0, this, 0);
293   mySMESHGUI->ResetState();
294   reject();
295   return;
296 }
297
298 //=======================================================================
299 //function : onTextChange
300 //purpose  :
301 //=======================================================================
302 void SMESHGUI_RemoveElementsDlg::onTextChange (const QString& theNewText)
303 {
304   if (myBusy) 
305     return;
306   myBusy = true;
307
308   myNbOkElements = 0;
309
310   buttonOk->setEnabled(false);
311   buttonApply->setEnabled(false);
312
313   // hilight entered elements
314   if(myActor){
315     if(SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh()){
316       Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
317       
318       TColStd_MapOfInteger newIndices;
319       
320       QStringList aListId = QStringList::split(" ", theNewText, false);
321       for (int i = 0; i < aListId.count(); i++) {
322         if(const SMDS_MeshElement *anElem = aMesh->FindElement(aListId[i].toInt())) {
323           newIndices.Add(anElem->GetID());
324           myNbOkElements++;
325         }
326       }
327       
328       mySelector->AddOrRemoveIndex(anIO,newIndices,false);
329       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
330         aViewWindow->highlight(anIO,true,true);
331     }
332   }
333   
334   if (myNbOkElements) {
335     buttonOk->setEnabled(true);
336     buttonApply->setEnabled(true);
337   }
338   
339   myBusy = false;
340 }
341
342 //=================================================================================
343 // function : SelectionIntoArgument()
344 // purpose  : Called when selection as changed or other case
345 //=================================================================================
346 void SMESHGUI_RemoveElementsDlg::SelectionIntoArgument()
347 {
348   if (myBusy) return;
349
350   // clear
351
352   myNbOkElements = false;
353   myActor = 0;
354
355   myBusy = true;
356   myEditCurrentArgument->setText("");
357   myBusy = false;
358
359   if (!GroupButtons->isEnabled()) // inactive
360     return;
361
362   buttonOk->setEnabled(false);
363   buttonApply->setEnabled(false);
364
365   // get selected mesh
366
367   SALOME_ListIO aList;
368   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
369
370   int nbSel = aList.Extent();
371   if (nbSel != 1)
372     return;
373
374   Handle(SALOME_InteractiveObject) anIO = aList.First();
375   myMesh = SMESH::GetMeshByIO(anIO);
376   if (myMesh->_is_nil())
377     return;
378
379   myActor = SMESH::FindActorByEntry(anIO->getEntry());
380   if (!myActor)
381     return;
382
383   // get selected nodes
384   QString aString = "";
385   int nbElems = SMESH::GetNameOfSelectedElements(mySelector,anIO,aString);
386   if(nbElems < 1)
387     return;
388   myBusy = true;
389   myEditCurrentArgument->setText(aString);
390   myBusy = false;
391
392   // OK
393
394   myNbOkElements = nbElems;
395
396   buttonOk->setEnabled(true);
397   buttonApply->setEnabled(true);
398 }
399
400 //=================================================================================
401 // function : SetEditCurrentArgument()
402 // purpose  :
403 //=================================================================================
404 void SMESHGUI_RemoveElementsDlg::SetEditCurrentArgument()
405 {
406   QPushButton* send = (QPushButton*)sender();
407   switch (myConstructorId)
408     {
409     case 0: /* default constructor */
410       {
411         if(send == SelectButtonC1A1) {
412           LineEditC1A1->setFocus();
413           myEditCurrentArgument = LineEditC1A1;
414         }
415         SelectionIntoArgument();
416         break;
417       }
418     }
419   return;
420 }
421
422 //=================================================================================
423 // function : DeactivateActiveDialog()
424 // purpose  :
425 //=================================================================================
426 void SMESHGUI_RemoveElementsDlg::DeactivateActiveDialog()
427 {
428   if (GroupConstructors->isEnabled()) {
429     GroupConstructors->setEnabled(false);
430     GroupC1->setEnabled(false);
431     GroupButtons->setEnabled(false);
432     mySMESHGUI->ResetState(); // ??
433     mySMESHGUI->SetActiveDialogBox(0); // ??
434   }
435 }
436
437 //=================================================================================
438 // function : ActivateThisDialog()
439 // purpose  :
440 //=================================================================================
441 void SMESHGUI_RemoveElementsDlg::ActivateThisDialog()
442 {
443   /* Emit a signal to deactivate the active dialog */
444   mySMESHGUI->EmitSignalDeactivateDialog();
445
446   GroupConstructors->setEnabled(true);
447   GroupC1->setEnabled(true);
448   GroupButtons->setEnabled(true);
449
450   mySMESHGUI->SetActiveDialogBox((QDialog*)this); // ??
451
452   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
453     aViewWindow->SetSelectionMode(NodeSelection);
454
455   SelectionIntoArgument(); // ??
456 }
457
458 //=================================================================================
459 // function : enterEvent()
460 // purpose  :
461 //=================================================================================
462 void SMESHGUI_RemoveElementsDlg::enterEvent (QEvent*)
463 {
464   if (!GroupConstructors->isEnabled())
465     ActivateThisDialog();
466 }
467
468 //=================================================================================
469 // function : closeEvent()
470 // purpose  :
471 //=================================================================================
472 void SMESHGUI_RemoveElementsDlg::closeEvent (QCloseEvent*)
473 {
474   /* same than click on cancel button */
475   this->ClickOnCancel();
476   return;
477 }
478
479 //=======================================================================
480 //function : hideEvent
481 //purpose  : caused by ESC key
482 //=======================================================================
483 void SMESHGUI_RemoveElementsDlg::hideEvent (QHideEvent * e)
484 {
485   if (!isMinimized())
486     ClickOnCancel();
487 }