Salome HOME
GPUSPHGUI: add Offset transformation
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_RemoveElementsDlg.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_RemoveElementsDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_RemoveElementsDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_VTKUtils.h"
33 #include "SMESHGUI_MeshUtils.h"
34 #include "SMESHGUI_IdValidator.h"
35 #include "SMESHGUI_FilterDlg.h"
36
37 #include <SMESH_Actor.h>
38 #include <SMDS_Mesh.hxx>
39
40 // SALOME GUI includes
41 #include <SUIT_ResourceMgr.h>
42 #include <SUIT_Desktop.h>
43 #include <SUIT_Session.h>
44 #include <SUIT_MessageBox.h>
45 #include <SUIT_OverrideCursor.h>
46
47 #include <LightApp_Application.h>
48 #include <LightApp_SelectionMgr.h>
49 #include <SalomeApp_Tools.h>
50
51 #include <SVTK_Selector.h>
52 #include <SVTK_ViewModel.h>
53 #include <SVTK_ViewWindow.h>
54 #include <SALOME_ListIO.hxx>
55
56 // OCCT includes
57 #include <TColStd_MapOfInteger.hxx>
58
59 // Qt includes
60 #include <QGroupBox>
61 #include <QLabel>
62 #include <QLineEdit>
63 #include <QPushButton>
64 #include <QRadioButton>
65 #include <QVBoxLayout>
66 #include <QHBoxLayout>
67 #include <QKeyEvent>
68 #include <QButtonGroup>
69
70 // IDL includes
71 #include <SALOMEconfig.h>
72 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
73
74 #define SPACING 6
75 #define MARGIN  11
76
77 //=================================================================================
78 // class    : SMESHGUI_RemoveElementsDlg()
79 // purpose  :
80 //=================================================================================
81 SMESHGUI_RemoveElementsDlg
82 ::SMESHGUI_RemoveElementsDlg(SMESHGUI* theModule)
83   : QDialog(SMESH::GetDesktop(theModule)),
84     mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
85     mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
86     mySMESHGUI(theModule),
87     myBusy(false),
88     myFilterDlg(0)
89 {
90   setModal( false );
91   setAttribute( Qt::WA_DeleteOnClose, true );
92   setWindowTitle(tr("SMESH_REMOVE_ELEMENTS_TITLE"));
93   setSizeGripEnabled(true);
94   
95   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_REM_ELEMENT")));
96   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
97
98   QVBoxLayout* SMESHGUI_RemoveElementsDlgLayout = new QVBoxLayout(this);
99   SMESHGUI_RemoveElementsDlgLayout->setSpacing(SPACING);
100   SMESHGUI_RemoveElementsDlgLayout->setMargin(MARGIN);
101
102   /***************************************************************/
103   GroupConstructors = new QGroupBox(tr("SMESH_ELEMENTS"), this);
104   QButtonGroup* ButtonGroup = new QButtonGroup(this);
105   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
106   GroupConstructorsLayout->setSpacing(SPACING);
107   GroupConstructorsLayout->setMargin(MARGIN);
108
109   Constructor1 = new QRadioButton(GroupConstructors);
110   Constructor1->setIcon(image0);
111   Constructor1->setChecked(true);
112
113   GroupConstructorsLayout->addWidget(Constructor1);
114   ButtonGroup->addButton(Constructor1, 0);
115
116   /***************************************************************/
117   GroupC1 = new QGroupBox(tr("SMESH_REMOVE"), this);
118   QHBoxLayout* GroupC1Layout = new QHBoxLayout(GroupC1);
119   GroupC1Layout->setSpacing(SPACING);
120   GroupC1Layout->setMargin(MARGIN);
121
122   TextLabelC1A1 = new QLabel(tr("SMESH_ID_ELEMENTS"), GroupC1);
123   SelectButtonC1A1 = new QPushButton(GroupC1);
124   SelectButtonC1A1->setIcon(image1);
125   LineEditC1A1 = new QLineEdit(GroupC1);
126   LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this));
127   LineEditC1A1->setMaxLength(-1);
128   QPushButton* filterBtn = new QPushButton( tr( "SMESH_BUT_FILTER" ), GroupC1 );
129   connect(filterBtn,   SIGNAL(clicked()), this, SLOT(setFilters()));
130
131   GroupC1Layout->addWidget(TextLabelC1A1);
132   GroupC1Layout->addWidget(SelectButtonC1A1);
133   GroupC1Layout->addWidget(LineEditC1A1);
134   GroupC1Layout->addWidget(filterBtn );
135
136   /***************************************************************/
137   GroupButtons = new QGroupBox(this);
138   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
139   GroupButtonsLayout->setSpacing(SPACING);
140   GroupButtonsLayout->setMargin(MARGIN);
141
142   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
143   buttonOk->setAutoDefault(true);
144   buttonOk->setDefault(true);
145   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
146   buttonApply->setAutoDefault(true);
147   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
148   buttonCancel->setAutoDefault(true);
149   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
150   buttonHelp->setAutoDefault(true);
151
152   GroupButtonsLayout->addWidget(buttonOk);
153   GroupButtonsLayout->addSpacing(10);
154   GroupButtonsLayout->addWidget(buttonApply);
155   GroupButtonsLayout->addSpacing(10);
156   GroupButtonsLayout->addStretch();
157   GroupButtonsLayout->addWidget(buttonCancel);
158   GroupButtonsLayout->addWidget(buttonHelp);
159
160   /***************************************************************/
161   SMESHGUI_RemoveElementsDlgLayout->addWidget(GroupConstructors);
162   SMESHGUI_RemoveElementsDlgLayout->addWidget(GroupC1);
163   SMESHGUI_RemoveElementsDlgLayout->addWidget(GroupButtons);
164
165   myHelpFileName = "removing_nodes_and_elements_page.html#removing_elements_anchor";
166
167   Init(); /* Initialisations */
168 }
169
170 //=================================================================================
171 // function : ~SMESHGUI_RemoveElementsDlg()
172 // purpose  : Destroys the object and frees any allocated resources
173 //=================================================================================
174 SMESHGUI_RemoveElementsDlg::~SMESHGUI_RemoveElementsDlg()
175 {
176   if ( myFilterDlg ) {
177     myFilterDlg->setParent( 0 );
178     delete myFilterDlg;
179     myFilterDlg = 0;
180   }
181 }
182
183 //=================================================================================
184 // function : Init()
185 // purpose  :
186 //=================================================================================
187 void SMESHGUI_RemoveElementsDlg::Init()
188 {
189   myConstructorId = 0;
190   Constructor1->setChecked(true);
191   myEditCurrentArgument = LineEditC1A1;
192
193   myNbOkElements = 0;
194   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
195   myActor = 0;
196   myBusy = false;
197
198   /* signals and slots connections */
199   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
200   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
201   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
202   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
203
204   connect(SelectButtonC1A1, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
205   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
206   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
207   /* to close dialog if study change */
208   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()),      this, SLOT(reject()));
209   connect(mySMESHGUI, SIGNAL (SignalActivatedViewManager()), this, SLOT(onOpenView()));
210   connect(mySMESHGUI, SIGNAL (SignalCloseView()),            this, SLOT(onCloseView()));
211   connect(myEditCurrentArgument, SIGNAL(textChanged(const QString&)),
212           SLOT(onTextChange(const QString&)));
213
214   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
215     aViewWindow->SetSelectionMode(CellSelection);
216
217   SelectionIntoArgument();
218 }
219
220 //=================================================================================
221 // function : ClickOnApply()
222 // purpose  :
223 //=================================================================================
224 void SMESHGUI_RemoveElementsDlg::ClickOnApply()
225 {
226   if (mySMESHGUI->isActiveStudyLocked())
227     return;
228
229   if (myNbOkElements)
230   {
231     SUIT_OverrideCursor wc;
232
233     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
234     SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
235     anArrayOfIdeces->length(aListId.count());
236     for (int i = 0; i < aListId.count(); i++)
237       anArrayOfIdeces[i] = aListId[ i ].toInt();
238
239     bool aResult = false;
240     try
241     {
242       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
243       aResult = aMeshEditor->RemoveElements(anArrayOfIdeces.in());
244
245       if ( myActor && myMesh->NbElements() == 0 )
246         myActor->SetRepresentation(SMESH_Actor::ePoint);
247
248     } catch (const SALOME::SALOME_Exception& S_ex) {
249       SalomeApp_Tools::QtCatchCorbaException(S_ex);
250       myEditCurrentArgument->clear();
251     } catch (...){
252       myEditCurrentArgument->clear();
253     }
254
255     if (aResult) {
256       myEditCurrentArgument->clear();
257       mySelector->ClearIndex();
258       SMESH::UpdateView();
259       SMESHGUI::Modified();
260     }
261   }
262 }
263
264 //=================================================================================
265 // function : ClickOnOk()
266 // purpose  :
267 //=================================================================================
268 void SMESHGUI_RemoveElementsDlg::ClickOnOk()
269 {
270   ClickOnApply();
271   reject();
272 }
273
274 //=================================================================================
275 // function : reject()
276 // purpose  :
277 //=================================================================================
278 void SMESHGUI_RemoveElementsDlg::reject()
279 {
280   if (SMESH::GetCurrentVtkView())
281     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
282   //mySelectionMgr->clearSelected();
283   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
284     aViewWindow->SetSelectionMode(ActorSelection);
285   disconnect(mySelectionMgr, 0, this, 0);
286   mySelectionMgr->clearFilters();
287   mySMESHGUI->ResetState();
288   QDialog::reject();
289 }
290
291 //=================================================================================
292 // function : onOpenView()
293 // purpose  :
294 //=================================================================================
295 void SMESHGUI_RemoveElementsDlg::onOpenView()
296 {
297   if(!mySelector) {
298     mySelector = SMESH::GetViewWindow( mySMESHGUI )->GetSelector();
299     ActivateThisDialog();
300   }
301 }
302
303 //=================================================================================
304 // function : onCloseView()
305 // purpose  :
306 //=================================================================================
307 void SMESHGUI_RemoveElementsDlg::onCloseView()
308 {
309   DeactivateActiveDialog();
310   mySelector = 0;
311 }
312
313 //=================================================================================
314 // function : ClickOnHelp()
315 // purpose  :
316 //=================================================================================
317 void SMESHGUI_RemoveElementsDlg::ClickOnHelp()
318 {
319   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
320   if (app) 
321     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
322   else {
323     QString platform;
324 #ifdef WIN32
325     platform = "winapplication";
326 #else
327     platform = "application";
328 #endif
329     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
330                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
331                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
332                                                                  platform)).
333                              arg(myHelpFileName));
334   }
335 }
336
337 //=======================================================================
338 //function : onTextChange
339 //purpose  :
340 //=======================================================================
341 void SMESHGUI_RemoveElementsDlg::onTextChange(const QString& theNewText)
342 {
343   if (myBusy) return;
344   myBusy = true;
345
346   myNbOkElements = 0;
347
348   // highlight entered elements
349   if(myActor){
350     if(SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh()){
351       Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
352       
353       TColStd_MapOfInteger newIndices;
354       
355       QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
356       for (int i = 0; i < aListId.count(); i++) {
357         if(const SMDS_MeshElement *anElem = aMesh->FindElement(aListId[i].toInt())) {
358           newIndices.Add(anElem->GetID());
359           myNbOkElements++;
360         }
361       }
362       
363       mySelector->AddOrRemoveIndex(anIO,newIndices,false);
364       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
365         aViewWindow->highlight(anIO,true,true);
366     }
367   }
368   else
369   {
370     QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
371     myNbOkElements = aListId.count();
372   }
373   
374   myBusy = false;
375   updateButtons();
376 }
377
378 //=================================================================================
379 // function : SelectionIntoArgument()
380 // purpose  : Called when selection as changed or other case
381 //=================================================================================
382 void SMESHGUI_RemoveElementsDlg::SelectionIntoArgument()
383 {
384   if (myBusy) return;                                  // busy
385   if (myFilterDlg && myFilterDlg->isVisible()) return; // filter dlg active
386   if (!GroupButtons->isEnabled()) return;              // inactive
387
388   // clear
389
390   myNbOkElements = 0;
391   myActor = 0;
392
393   myBusy = true;
394   myEditCurrentArgument->setText("");
395   myBusy = false;
396
397   // get selected mesh
398
399   SALOME_ListIO aList;
400   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
401
402   int nbSel = aList.Extent();
403   if (nbSel == 1) {
404
405     Handle(SALOME_InteractiveObject) anIO = aList.First();
406     myMesh = SMESH::GetMeshByIO(anIO);
407
408     if (!myMesh->_is_nil()) {
409
410       myActor = SMESH::FindActorByEntry(anIO->getEntry());
411       if (myActor) {
412         
413         // get selected elements
414         QString aString = "";
415         int nbElems = SMESH::GetNameOfSelectedElements(mySelector,anIO,aString);
416         if (nbElems > 0) {
417           myBusy = true;
418           myEditCurrentArgument->setText(aString);
419           myBusy = false;
420
421           // OK
422
423           myNbOkElements = nbElems;
424         } // if (nbElems > 0)
425       } // if (myActor)
426     } // if (!myMesh->_is_nil())
427   } // if (nbSel == 1) {
428
429   updateButtons();        
430 }
431
432 //=================================================================================
433 // function : SetEditCurrentArgument()
434 // purpose  :
435 //=================================================================================
436 void SMESHGUI_RemoveElementsDlg::SetEditCurrentArgument()
437 {
438   QPushButton* send = (QPushButton*)sender();
439   switch (myConstructorId) {
440   case 0: /* default constructor */
441     {
442       if(send == SelectButtonC1A1) {
443         LineEditC1A1->setFocus();
444         myEditCurrentArgument = LineEditC1A1;
445       }
446       SelectionIntoArgument();
447       break;
448     }
449   }
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(CellSelection);
484
485   SelectionIntoArgument(); // ??
486 }
487
488 //=================================================================================
489 // function : enterEvent()
490 // purpose  :
491 //=================================================================================
492 void SMESHGUI_RemoveElementsDlg::enterEvent(QEvent*)
493 {
494   if (!GroupConstructors->isEnabled()) {
495     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
496     if ( aViewWindow && !mySelector) {
497       mySelector = aViewWindow->GetSelector();
498     }
499     ActivateThisDialog();
500   }
501 }
502
503 //=================================================================================
504 // function : keyPressEvent()
505 // purpose  :
506 //=================================================================================
507 void SMESHGUI_RemoveElementsDlg::keyPressEvent( QKeyEvent* e )
508 {
509   QDialog::keyPressEvent( e );
510   if ( e->isAccepted() )
511     return;
512
513   if ( e->key() == Qt::Key_F1 ) {
514     e->accept();
515     ClickOnHelp();
516   }
517 }
518
519 //=================================================================================
520 // function : setFilters()
521 // purpose  : SLOT. Called when "Filter" button pressed.
522 //=================================================================================
523 void SMESHGUI_RemoveElementsDlg::setFilters()
524 {
525   if(myMesh->_is_nil()) {
526     SUIT_MessageBox::critical(this,
527                               tr("SMESH_ERROR"),
528                               tr("NO_MESH_SELECTED"));
529     return;
530   }
531   if ( !myFilterDlg )
532     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
533
534   QList<int> types;
535   if ( myMesh->NbEdges()     ) types << SMESH::EDGE;
536   if ( myMesh->NbFaces()     ) types << SMESH::FACE;
537   if ( myMesh->NbVolumes()   ) types << SMESH::VOLUME;
538   if ( myMesh->NbBalls()     ) types << SMESH::BALL;
539   if ( myMesh->Nb0DElements()) types << SMESH::ELEM0D;
540   if ( types.count() > 1 )     types << SMESH::ALL;
541
542   myFilterDlg->Init( types );
543   myFilterDlg->SetSelection();
544   myFilterDlg->SetMesh( myMesh );
545   myFilterDlg->SetSourceWg( LineEditC1A1 );
546
547   myFilterDlg->show();
548 }
549
550 //=================================================================================
551 // function : updateButtons
552 // purpose  : enable / disable control buttons
553 //=================================================================================
554 void SMESHGUI_RemoveElementsDlg::updateButtons()
555 {
556   buttonOk->setEnabled(myNbOkElements > 0);
557   buttonApply->setEnabled(myNbOkElements > 0);
558 }