Back to index

salome-smesh  6.5.0
SMESHGUI_RemoveElementsDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // SMESH SMESHGUI : GUI for SMESH component
00024 // File   : SMESHGUI_RemoveElementsDlg.cxx
00025 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
00026 // SMESH includes
00027 //
00028 #include "SMESHGUI_RemoveElementsDlg.h"
00029 
00030 #include "SMESHGUI.h"
00031 #include "SMESHGUI_Utils.h"
00032 #include "SMESHGUI_VTKUtils.h"
00033 #include "SMESHGUI_MeshUtils.h"
00034 #include "SMESHGUI_IdValidator.h"
00035 #include "SMESHGUI_FilterDlg.h"
00036 
00037 #include <SMESH_Actor.h>
00038 #include <SMDS_Mesh.hxx>
00039 
00040 // SALOME GUI includes
00041 #include <SUIT_ResourceMgr.h>
00042 #include <SUIT_Desktop.h>
00043 #include <SUIT_Session.h>
00044 #include <SUIT_MessageBox.h>
00045 
00046 #include <LightApp_Application.h>
00047 #include <LightApp_SelectionMgr.h>
00048 #include <SalomeApp_Tools.h>
00049 
00050 #include <SVTK_Selector.h>
00051 #include <SVTK_ViewModel.h>
00052 #include <SVTK_ViewWindow.h>
00053 #include <SALOME_ListIO.hxx>
00054 
00055 // OCCT includes
00056 #include <TColStd_MapOfInteger.hxx>
00057 
00058 // Qt includes
00059 #include <QGroupBox>
00060 #include <QLabel>
00061 #include <QLineEdit>
00062 #include <QPushButton>
00063 #include <QRadioButton>
00064 #include <QVBoxLayout>
00065 #include <QHBoxLayout>
00066 #include <QKeyEvent>
00067 #include <QButtonGroup>
00068 
00069 // IDL includes
00070 #include <SALOMEconfig.h>
00071 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
00072 
00073 #define SPACING 6
00074 #define MARGIN  11
00075 
00076 //=================================================================================
00077 // class    : SMESHGUI_RemoveElementsDlg()
00078 // purpose  :
00079 //=================================================================================
00080 SMESHGUI_RemoveElementsDlg
00081 ::SMESHGUI_RemoveElementsDlg(SMESHGUI* theModule)
00082   : QDialog(SMESH::GetDesktop(theModule)),
00083     mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
00084     mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
00085     mySMESHGUI(theModule),
00086     myBusy(false),
00087     myFilterDlg(0)
00088 {
00089   setModal( false );
00090   setAttribute( Qt::WA_DeleteOnClose, true );
00091   setWindowTitle(tr("SMESH_REMOVE_ELEMENTS_TITLE"));
00092   setSizeGripEnabled(true);
00093   
00094   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_REM_ELEMENT")));
00095   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
00096 
00097   QVBoxLayout* SMESHGUI_RemoveElementsDlgLayout = new QVBoxLayout(this);
00098   SMESHGUI_RemoveElementsDlgLayout->setSpacing(SPACING);
00099   SMESHGUI_RemoveElementsDlgLayout->setMargin(MARGIN);
00100 
00101   /***************************************************************/
00102   GroupConstructors = new QGroupBox(tr("SMESH_ELEMENTS"), this);
00103   QButtonGroup* ButtonGroup = new QButtonGroup(this);
00104   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
00105   GroupConstructorsLayout->setSpacing(SPACING);
00106   GroupConstructorsLayout->setMargin(MARGIN);
00107 
00108   Constructor1 = new QRadioButton(GroupConstructors);
00109   Constructor1->setIcon(image0);
00110   Constructor1->setChecked(true);
00111 
00112   GroupConstructorsLayout->addWidget(Constructor1);
00113   ButtonGroup->addButton(Constructor1, 0);
00114 
00115   /***************************************************************/
00116   GroupC1 = new QGroupBox(tr("SMESH_REMOVE"), this);
00117   QHBoxLayout* GroupC1Layout = new QHBoxLayout(GroupC1);
00118   GroupC1Layout->setSpacing(SPACING);
00119   GroupC1Layout->setMargin(MARGIN);
00120 
00121   TextLabelC1A1 = new QLabel(tr("SMESH_ID_ELEMENTS"), GroupC1);
00122   SelectButtonC1A1 = new QPushButton(GroupC1);
00123   SelectButtonC1A1->setIcon(image1);
00124   LineEditC1A1 = new QLineEdit(GroupC1);
00125   LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this));
00126   LineEditC1A1->setMaxLength(-1);
00127   QPushButton* filterBtn = new QPushButton( tr( "SMESH_BUT_FILTER" ), GroupC1 );
00128   connect(filterBtn,   SIGNAL(clicked()), this, SLOT(setFilters()));
00129 
00130   GroupC1Layout->addWidget(TextLabelC1A1);
00131   GroupC1Layout->addWidget(SelectButtonC1A1);
00132   GroupC1Layout->addWidget(LineEditC1A1);
00133   GroupC1Layout->addWidget(filterBtn );
00134 
00135   /***************************************************************/
00136   GroupButtons = new QGroupBox(this);
00137   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
00138   GroupButtonsLayout->setSpacing(SPACING);
00139   GroupButtonsLayout->setMargin(MARGIN);
00140 
00141   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
00142   buttonOk->setAutoDefault(true);
00143   buttonOk->setDefault(true);
00144   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
00145   buttonApply->setAutoDefault(true);
00146   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
00147   buttonCancel->setAutoDefault(true);
00148   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
00149   buttonHelp->setAutoDefault(true);
00150 
00151   GroupButtonsLayout->addWidget(buttonOk);
00152   GroupButtonsLayout->addSpacing(10);
00153   GroupButtonsLayout->addWidget(buttonApply);
00154   GroupButtonsLayout->addSpacing(10);
00155   GroupButtonsLayout->addStretch();
00156   GroupButtonsLayout->addWidget(buttonCancel);
00157   GroupButtonsLayout->addWidget(buttonHelp);
00158 
00159   /***************************************************************/
00160   SMESHGUI_RemoveElementsDlgLayout->addWidget(GroupConstructors);
00161   SMESHGUI_RemoveElementsDlgLayout->addWidget(GroupC1);
00162   SMESHGUI_RemoveElementsDlgLayout->addWidget(GroupButtons);
00163 
00164   myHelpFileName = "removing_nodes_and_elements_page.html#removing_elements_anchor";
00165 
00166   Init(); /* Initialisations */
00167 }
00168 
00169 //=================================================================================
00170 // function : ~SMESHGUI_RemoveElementsDlg()
00171 // purpose  : Destroys the object and frees any allocated resources
00172 //=================================================================================
00173 SMESHGUI_RemoveElementsDlg::~SMESHGUI_RemoveElementsDlg()
00174 {
00175   if ( myFilterDlg ) {
00176     myFilterDlg->setParent( 0 );
00177     delete myFilterDlg;
00178     myFilterDlg = 0;
00179   }
00180 }
00181 
00182 //=================================================================================
00183 // function : Init()
00184 // purpose  :
00185 //=================================================================================
00186 void SMESHGUI_RemoveElementsDlg::Init()
00187 {
00188   myConstructorId = 0;
00189   Constructor1->setChecked(true);
00190   myEditCurrentArgument = LineEditC1A1;
00191 
00192   myNbOkElements = 0;
00193   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
00194   myActor = 0;
00195   myBusy = false;
00196 
00197   /* signals and slots connections */
00198   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
00199   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
00200   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
00201   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
00202 
00203   connect(SelectButtonC1A1, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
00204   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
00205   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
00206   /* to close dialog if study change */
00207   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
00208   connect(myEditCurrentArgument, SIGNAL(textChanged(const QString&)),
00209           SLOT(onTextChange(const QString&)));
00210 
00211   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
00212     aViewWindow->SetSelectionMode(CellSelection);
00213 
00214   SelectionIntoArgument();
00215 }
00216 
00217 //=================================================================================
00218 // function : ClickOnApply()
00219 // purpose  :
00220 //=================================================================================
00221 void SMESHGUI_RemoveElementsDlg::ClickOnApply()
00222 {
00223   if (mySMESHGUI->isActiveStudyLocked())
00224     return;
00225 
00226   if (myNbOkElements) {
00227     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
00228     SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
00229     anArrayOfIdeces->length(aListId.count());
00230     for (int i = 0; i < aListId.count(); i++)
00231       anArrayOfIdeces[i] = aListId[ i ].toInt();
00232 
00233     bool aResult = false;
00234     try {
00235       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
00236       aResult = aMeshEditor->RemoveElements(anArrayOfIdeces.in());
00237     } catch (const SALOME::SALOME_Exception& S_ex) {
00238       SalomeApp_Tools::QtCatchCorbaException(S_ex);
00239       myEditCurrentArgument->clear();
00240     } catch (...){
00241       myEditCurrentArgument->clear();
00242     }
00243 
00244     if (aResult) {
00245       myEditCurrentArgument->clear();
00246       mySelector->ClearIndex();
00247       SMESH::UpdateView();
00248       SMESHGUI::Modified();
00249     }
00250   }
00251 }
00252 
00253 //=================================================================================
00254 // function : ClickOnOk()
00255 // purpose  :
00256 //=================================================================================
00257 void SMESHGUI_RemoveElementsDlg::ClickOnOk()
00258 {
00259   ClickOnApply();
00260   ClickOnCancel();
00261 }
00262 
00263 //=================================================================================
00264 // function : ClickOnCancel()
00265 // purpose  :
00266 //=================================================================================
00267 void SMESHGUI_RemoveElementsDlg::ClickOnCancel()
00268 {
00269   if (SMESH::GetCurrentVtkView())
00270     SMESH::RemoveFilters(); // PAL6938 -- clean all mesh entity filters
00271   //mySelectionMgr->clearSelected();
00272   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
00273     aViewWindow->SetSelectionMode(ActorSelection);
00274   disconnect(mySelectionMgr, 0, this, 0);
00275   mySelectionMgr->clearFilters();
00276   mySMESHGUI->ResetState();
00277   reject();
00278 }
00279 
00280 //=================================================================================
00281 // function : ClickOnHelp()
00282 // purpose  :
00283 //=================================================================================
00284 void SMESHGUI_RemoveElementsDlg::ClickOnHelp()
00285 {
00286   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
00287   if (app) 
00288     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
00289   else {
00290     QString platform;
00291 #ifdef WIN32
00292     platform = "winapplication";
00293 #else
00294     platform = "application";
00295 #endif
00296     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
00297                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
00298                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
00299                                                                  platform)).
00300                              arg(myHelpFileName));
00301   }
00302 }
00303 
00304 //=======================================================================
00305 //function : onTextChange
00306 //purpose  :
00307 //=======================================================================
00308 void SMESHGUI_RemoveElementsDlg::onTextChange(const QString& theNewText)
00309 {
00310   if (myBusy) return;
00311   myBusy = true;
00312 
00313   myNbOkElements = 0;
00314 
00315   // hilight entered elements
00316   if(myActor){
00317     if(SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh()){
00318       Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
00319       
00320       TColStd_MapOfInteger newIndices;
00321       
00322       QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
00323       for (int i = 0; i < aListId.count(); i++) {
00324         if(const SMDS_MeshElement *anElem = aMesh->FindElement(aListId[i].toInt())) {
00325           newIndices.Add(anElem->GetID());
00326           myNbOkElements++;
00327         }
00328       }
00329       
00330       mySelector->AddOrRemoveIndex(anIO,newIndices,false);
00331       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
00332         aViewWindow->highlight(anIO,true,true);
00333     }
00334   }
00335   
00336   myBusy = false;
00337   updateButtons();
00338 }
00339 
00340 //=================================================================================
00341 // function : SelectionIntoArgument()
00342 // purpose  : Called when selection as changed or other case
00343 //=================================================================================
00344 void SMESHGUI_RemoveElementsDlg::SelectionIntoArgument()
00345 {
00346   if (myBusy) return;                                  // busy
00347   if (myFilterDlg && myFilterDlg->isVisible()) return; // filter digl active
00348   if (!GroupButtons->isEnabled()) return;              // inactive
00349 
00350   // clear
00351 
00352   myNbOkElements = 0;
00353   myActor = 0;
00354 
00355   myBusy = true;
00356   myEditCurrentArgument->setText("");
00357   myBusy = false;
00358 
00359   // get selected mesh
00360 
00361   SALOME_ListIO aList;
00362   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
00363 
00364   int nbSel = aList.Extent();
00365   if (nbSel == 1) {
00366 
00367     Handle(SALOME_InteractiveObject) anIO = aList.First();
00368     myMesh = SMESH::GetMeshByIO(anIO);
00369 
00370     if (!myMesh->_is_nil()) {
00371 
00372       myActor = SMESH::FindActorByEntry(anIO->getEntry());
00373       if (myActor) {
00374         
00375         // get selected nodes
00376         QString aString = "";
00377         int nbElems = SMESH::GetNameOfSelectedElements(mySelector,anIO,aString);
00378         if (nbElems > 0) {
00379           myBusy = true;
00380           myEditCurrentArgument->setText(aString);
00381           myBusy = false;
00382           
00383           // OK
00384           
00385           myNbOkElements = nbElems;
00386         } // if (nbElems > 0)
00387       } // if (myActor)
00388     } // if (!myMesh->_is_nil())
00389   } // if (nbSel == 1) {
00390 
00391   updateButtons();        
00392 }
00393 
00394 //=================================================================================
00395 // function : SetEditCurrentArgument()
00396 // purpose  :
00397 //=================================================================================
00398 void SMESHGUI_RemoveElementsDlg::SetEditCurrentArgument()
00399 {
00400   QPushButton* send = (QPushButton*)sender();
00401   switch (myConstructorId) {
00402   case 0: /* default constructor */
00403     {
00404       if(send == SelectButtonC1A1) {
00405         LineEditC1A1->setFocus();
00406         myEditCurrentArgument = LineEditC1A1;
00407       }
00408       SelectionIntoArgument();
00409       break;
00410     }
00411   }
00412 }
00413 
00414 //=================================================================================
00415 // function : DeactivateActiveDialog()
00416 // purpose  :
00417 //=================================================================================
00418 void SMESHGUI_RemoveElementsDlg::DeactivateActiveDialog()
00419 {
00420   if (GroupConstructors->isEnabled()) {
00421     GroupConstructors->setEnabled(false);
00422     GroupC1->setEnabled(false);
00423     GroupButtons->setEnabled(false);
00424     mySMESHGUI->ResetState(); // ??
00425     mySMESHGUI->SetActiveDialogBox(0); // ??
00426   }
00427 }
00428 
00429 //=================================================================================
00430 // function : ActivateThisDialog()
00431 // purpose  :
00432 //=================================================================================
00433 void SMESHGUI_RemoveElementsDlg::ActivateThisDialog()
00434 {
00435   /* Emit a signal to deactivate the active dialog */
00436   mySMESHGUI->EmitSignalDeactivateDialog();
00437 
00438   GroupConstructors->setEnabled(true);
00439   GroupC1->setEnabled(true);
00440   GroupButtons->setEnabled(true);
00441 
00442   mySMESHGUI->SetActiveDialogBox((QDialog*)this); // ??
00443 
00444   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
00445     aViewWindow->SetSelectionMode(CellSelection);
00446 
00447   SelectionIntoArgument(); // ??
00448 }
00449 
00450 //=================================================================================
00451 // function : enterEvent()
00452 // purpose  :
00453 //=================================================================================
00454 void SMESHGUI_RemoveElementsDlg::enterEvent(QEvent*)
00455 {
00456   if (!GroupConstructors->isEnabled())
00457     ActivateThisDialog();
00458 }
00459 
00460 //=================================================================================
00461 // function : closeEvent()
00462 // purpose  :
00463 //=================================================================================
00464 void SMESHGUI_RemoveElementsDlg::closeEvent(QCloseEvent*)
00465 {
00466   /* same than click on cancel button */
00467   ClickOnCancel();
00468 }
00469 
00470 //=======================================================================
00471 //function : hideEvent
00472 //purpose  : caused by ESC key
00473 //=======================================================================
00474 void SMESHGUI_RemoveElementsDlg::hideEvent( QHideEvent* )
00475 {
00476   if (!isMinimized())
00477     ClickOnCancel();
00478 }
00479 
00480 //=================================================================================
00481 // function : keyPressEvent()
00482 // purpose  :
00483 //=================================================================================
00484 void SMESHGUI_RemoveElementsDlg::keyPressEvent( QKeyEvent* e )
00485 {
00486   QDialog::keyPressEvent( e );
00487   if ( e->isAccepted() )
00488     return;
00489 
00490   if ( e->key() == Qt::Key_F1 ) {
00491     e->accept();
00492     ClickOnHelp();
00493   }
00494 }
00495 
00496 //=================================================================================
00497 // function : setFilters()
00498 // purpose  : SLOT. Called when "Filter" button pressed.
00499 //=================================================================================
00500 void SMESHGUI_RemoveElementsDlg::setFilters()
00501 {
00502   if(myMesh->_is_nil()) {
00503     SUIT_MessageBox::critical(this,
00504                               tr("SMESH_ERROR"),
00505                               tr("NO_MESH_SELECTED"));
00506    return;
00507   }
00508   if ( !myFilterDlg )
00509     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
00510 
00511   myFilterDlg->SetSelection();
00512   myFilterDlg->SetMesh( myMesh );
00513   myFilterDlg->SetSourceWg( LineEditC1A1 );
00514 
00515   myFilterDlg->show();
00516 }
00517 
00518 //=================================================================================
00519 // function : updateButtons
00520 // purpose  : enable / disable control buttons
00521 //=================================================================================
00522 void SMESHGUI_RemoveElementsDlg::updateButtons()
00523 {
00524   buttonOk->setEnabled(myNbOkElements > 0);
00525   buttonApply->setEnabled(myNbOkElements > 0);
00526 }