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