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