Salome HOME
fix PAL8469. Updating the global mesh icon when local hypotheses are edited
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_RemoveNodesDlg.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_RemoveNodesDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_RemoveNodesDlg.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 #include "SMESH_Actor.h"
37 #include "SMDS_Mesh.hxx"
38
39 #include "QAD_Application.h"
40 #include "QAD_Desktop.h"
41 #include "QAD_MessageBox.h"
42 #include "utilities.h"
43
44 // QT Includes
45 #include <qbuttongroup.h>
46 #include <qgroupbox.h>
47 #include <qlabel.h>
48 #include <qlineedit.h>
49 #include <qpushbutton.h>
50 #include <qradiobutton.h>
51 #include <qlayout.h>
52 #include <qvariant.h>
53 #include <qtooltip.h>
54 #include <qwhatsthis.h>
55 #include <qimage.h>
56 #include <qpixmap.h>
57
58 using namespace std;
59
60 //=================================================================================
61 // class    : SMESHGUI_RemoveNodesDlg()
62 // purpose  : 
63 //=================================================================================
64 SMESHGUI_RemoveNodesDlg::SMESHGUI_RemoveNodesDlg( QWidget* parent, const char* name, SALOME_Selection* Sel,
65                                                   bool modal, WFlags fl )
66      : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu |
67                Qt::WDestructiveClose),
68        myBusy( false )
69 {
70     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_DLG_REM_NODE")));
71     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
72
73     if ( !name )
74         setName( "SMESHGUI_RemoveNodesDlg" );
75     resize( 303, 185 ); 
76     setCaption( tr( "SMESH_REMOVE_NODES_TITLE"  ) );
77     setSizeGripEnabled( TRUE );
78     SMESHGUI_RemoveNodesDlgLayout = new QGridLayout( this ); 
79     SMESHGUI_RemoveNodesDlgLayout->setSpacing( 6 );
80     SMESHGUI_RemoveNodesDlgLayout->setMargin( 11 );
81
82     /***************************************************************/
83     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
84     GroupConstructors->setTitle( tr( "SMESH_NODES"  ) );
85     GroupConstructors->setExclusive( TRUE );
86     GroupConstructors->setColumnLayout(0, Qt::Vertical );
87     GroupConstructors->layout()->setSpacing( 0 );
88     GroupConstructors->layout()->setMargin( 0 );
89     GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
90     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
91     GroupConstructorsLayout->setSpacing( 6 );
92     GroupConstructorsLayout->setMargin( 11 );
93     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
94     Constructor1->setText( tr( ""  ) );
95     Constructor1->setPixmap( image0 );
96     Constructor1->setChecked( TRUE );
97     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
98     Constructor1->setMinimumSize( QSize( 50, 0 ) );
99     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
100     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
101     GroupConstructorsLayout->addItem( spacer, 0, 1 );
102     SMESHGUI_RemoveNodesDlgLayout->addWidget( GroupConstructors, 0, 0 );
103     
104     /***************************************************************/
105     GroupButtons = new QGroupBox( this, "GroupButtons" );
106     GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
107     GroupButtons->setTitle( tr( ""  ) );
108     GroupButtons->setColumnLayout(0, Qt::Vertical );
109     GroupButtons->layout()->setSpacing( 0 );
110     GroupButtons->layout()->setMargin( 0 );
111     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
112     GroupButtonsLayout->setAlignment( Qt::AlignTop );
113     GroupButtonsLayout->setSpacing( 6 );
114     GroupButtonsLayout->setMargin( 11 );
115     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
116     buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
117     buttonCancel->setAutoDefault( TRUE );
118     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
119     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
120     buttonApply->setText( tr( "SMESH_BUT_APPLY"  ) );
121     buttonApply->setAutoDefault( TRUE );
122     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
123     QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
124     GroupButtonsLayout->addItem( spacer_9, 0, 2 );
125     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
126     buttonOk->setText( tr( "SMESH_BUT_OK"  ) );
127     buttonOk->setAutoDefault( TRUE );
128     buttonOk->setDefault( TRUE );
129     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
130     SMESHGUI_RemoveNodesDlgLayout->addWidget( GroupButtons, 2, 0 );
131
132     /***************************************************************/
133     GroupC1 = new QGroupBox( this, "GroupC1" );
134     GroupC1->setTitle( tr( "SMESH_REMOVE"  ) );
135     GroupC1->setMinimumSize( QSize( 0, 0 ) );
136     GroupC1->setFrameShape( QGroupBox::Box );
137     GroupC1->setFrameShadow( QGroupBox::Sunken );
138     GroupC1->setColumnLayout(0, Qt::Vertical );
139     GroupC1->layout()->setSpacing( 0 );
140     GroupC1->layout()->setMargin( 0 );
141     GroupC1Layout = new QGridLayout( GroupC1->layout() );
142     GroupC1Layout->setAlignment( Qt::AlignTop );
143     GroupC1Layout->setSpacing( 6 );
144     GroupC1Layout->setMargin( 11 );
145     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
146     TextLabelC1A1->setText( tr( "SMESH_ID_NODES"  ) );
147     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
148     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
149     TextLabelC1A1->setFrameShadow( QLabel::Plain );
150     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
151     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
152     SelectButtonC1A1->setText( tr( ""  ) );
153     SelectButtonC1A1->setPixmap( image1 );
154     SelectButtonC1A1->setToggleButton( FALSE );
155     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
156     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
157     LineEditC1A1->setValidator( new SMESHGUI_IdValidator( this, "validator" ));
158     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
159     SMESHGUI_RemoveNodesDlgLayout->addWidget( GroupC1, 1, 0 );
160
161     Init(Sel) ; /* Initialisations */
162 }
163
164 //=================================================================================
165 // function : ~SMESHGUI_RemoveNodesDlg()
166 // purpose  : Destroys the object and frees any allocated resources
167 //=================================================================================
168 SMESHGUI_RemoveNodesDlg::~SMESHGUI_RemoveNodesDlg()
169 {
170     // no need to delete child widgets, Qt does it all for us
171 }
172
173
174 //=================================================================================
175 // function : Init()
176 // purpose  :
177 //=================================================================================
178 void SMESHGUI_RemoveNodesDlg::Init( SALOME_Selection* Sel )
179 {
180   myBusy = false;    
181
182   GroupC1->show();
183   myConstructorId = 0 ;
184   Constructor1->setChecked( TRUE );
185   myEditCurrentArgument = LineEditC1A1 ;        
186   mySelection = Sel;  
187   myNbOkNodes = 0 ;
188   myActor     = 0;
189   mySMESHGUI  = SMESHGUI::GetSMESHGUI() ;
190   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
191
192   /* signals and slots connections */
193   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
194   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
195   connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
196   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
197
198   connect( SelectButtonC1A1, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
199   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
200   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
201   /* to close dialog if study change */
202   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
203   connect( myEditCurrentArgument, SIGNAL( textChanged( const QString& )),
204            SLOT( onTextChange( const QString& )));
205
206   /* Move widget on the botton right corner of main widget */
207   int x, y ;
208   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
209   this->move( x, y ) ;
210   this->show() ; /* displays Dialog */
211
212   SMESH::SetPointRepresentation(true);
213   QAD_Application::getDesktop()->SetSelectionMode( NodeSelection, true );
214
215   SelectionIntoArgument();
216   return ;
217 }
218
219
220 //=================================================================================
221 // function : ConstructorsClicked()
222 // purpose  : Radio button management
223 //=================================================================================
224 void SMESHGUI_RemoveNodesDlg::ConstructorsClicked(int constructorId)
225 {
226   return ;
227 }
228
229
230 //=================================================================================
231 // function : ClickOnApply()
232 // purpose  :
233 //=================================================================================
234 void SMESHGUI_RemoveNodesDlg::ClickOnApply()
235 {
236   if (mySMESHGUI->ActiveStudyLocked())
237     return;
238   
239   if ( myNbOkNodes )
240   {
241     QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text(), false);
242     SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
243     anArrayOfIdeces->length( aListId.count() );
244     for ( int i = 0; i < aListId.count(); i++ )
245       anArrayOfIdeces[i] = aListId[ i ].toInt();
246
247     bool aResult = false;
248     try
249     {
250       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
251       aResult = aMeshEditor->RemoveNodes(anArrayOfIdeces.inout()) ;
252     }
253     catch( ... )
254     {
255     }
256
257     if ( aResult )
258     {
259       Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
260       mySelection->ClearIObjects();
261       SMESH::UpdateView();
262       mySelection->AddIObject( anIO, false );
263     }
264
265     SMESH::SetPointRepresentation(true);
266   }
267 }
268
269 //=================================================================================
270 // function : ClickOnOk()
271 // purpose  :
272 //=================================================================================
273 void SMESHGUI_RemoveNodesDlg::ClickOnOk()
274 {
275   this->ClickOnApply() ;
276   this->ClickOnCancel() ;
277
278   return ;
279 }
280
281         
282 //=================================================================================
283 // function : ClickOnCancel()
284 // purpose  :
285 //=================================================================================
286 void SMESHGUI_RemoveNodesDlg::ClickOnCancel()
287 {
288   mySelection->ClearIObjects();
289   SMESH::SetPointRepresentation(false);
290   QAD_Application::getDesktop()->SetSelectionMode( ActorSelection );
291   disconnect( mySelection, 0, this, 0 );
292   mySMESHGUI->ResetState() ;
293   reject() ;
294   return ;
295 }
296
297 //=======================================================================
298 //function : onTextChange
299 //purpose  : 
300 //=======================================================================
301
302 void SMESHGUI_RemoveNodesDlg::onTextChange(const QString& theNewText)
303 {
304   if ( myBusy ) return;
305   myBusy = true;
306
307   myNbOkNodes = 0;
308
309   buttonOk->setEnabled( false );
310   buttonApply->setEnabled( false );
311
312   // hilight entered nodes
313   SMDS_Mesh* aMesh = 0;
314   if ( myActor )
315     aMesh = myActor->GetObject()->GetMesh();
316   if ( aMesh ) {
317
318     mySelection->ClearIObjects();
319     mySelection->AddIObject( myActor->getIO() );
320
321     QStringList aListId = QStringList::split( " ", theNewText, false);
322     for ( int i = 0; i < aListId.count(); i++ ) {
323       const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() );
324       if ( n ) {
325         if ( !mySelection->IsIndexSelected( myActor->getIO(), n->GetID() ))
326           mySelection->AddOrRemoveIndex (myActor->getIO(), n->GetID(), true);
327         myNbOkNodes++;
328       }
329     }
330
331     if ( myNbOkNodes ) {
332       buttonOk->setEnabled( true );
333       buttonApply->setEnabled( true );
334     }
335   }
336
337   myBusy = false;
338 }
339
340 //=================================================================================
341 // function : SelectionIntoArgument()
342 // purpose  : Called when selection as changed or other case
343 //=================================================================================
344 void SMESHGUI_RemoveNodesDlg::SelectionIntoArgument()
345 {
346   if ( myBusy ) return;
347
348   // clear
349
350   myNbOkNodes = false;
351   myActor = 0;
352
353   myBusy = true;
354   myEditCurrentArgument->setText( "" );
355   myBusy = false;
356
357   if ( !GroupButtons->isEnabled() ) // inactive
358     return;
359
360   buttonOk->setEnabled( false );
361   buttonApply->setEnabled( false );
362
363   // get selected mesh
364
365   int nbSel = mySelection->IObjectCount();
366   if(nbSel != 1)
367     return;
368
369   myMesh = SMESH::GetMeshByIO( mySelection->firstIObject() );
370   if ( myMesh->_is_nil() )
371     return;
372
373   myActor = SMESH::FindActorByEntry( mySelection->firstIObject()->getEntry() );
374   if ( !myActor )
375     return;
376
377   // get selected nodes
378
379   QString aString = "";
380   int nbNodes = SMESH::GetNameOfSelectedNodes(mySelection, aString) ;
381   if(nbNodes < 1)
382     return ;
383   myBusy = true;
384   myEditCurrentArgument->setText( aString );
385   myBusy = false;
386
387   // OK
388
389   myNbOkNodes = true;
390
391   buttonOk->setEnabled( true );
392   buttonApply->setEnabled( true );
393 }
394
395
396 //=================================================================================
397 // function : SetEditCurrentArgument()
398 // purpose  :
399 //=================================================================================
400 void SMESHGUI_RemoveNodesDlg::SetEditCurrentArgument()
401 {
402   QPushButton* send = (QPushButton*)sender();
403   switch (myConstructorId)
404     {
405     case 0: /* default constructor */
406       { 
407         if(send == SelectButtonC1A1) {
408           LineEditC1A1->setFocus() ;
409           myEditCurrentArgument = LineEditC1A1;
410         }
411         SelectionIntoArgument() ;
412         break;
413       }
414     }
415   return ;
416 }
417
418 //=================================================================================
419 // function : DeactivateActiveDialog()
420 // purpose  :
421 //=================================================================================
422 void SMESHGUI_RemoveNodesDlg::DeactivateActiveDialog()
423 {
424   if ( GroupConstructors->isEnabled() ) {
425     GroupConstructors->setEnabled(false) ;
426     GroupC1->setEnabled(false) ;
427     GroupButtons->setEnabled(false) ;
428     mySMESHGUI->ResetState() ;    
429     mySMESHGUI->SetActiveDialogBox(0) ;
430   }
431   return ;
432 }
433
434
435 //=================================================================================
436 // function : ActivateThisDialog()
437 // purpose  :
438 //=================================================================================
439 void SMESHGUI_RemoveNodesDlg::ActivateThisDialog()
440 {
441   /* Emit a signal to deactivate the active dialog */
442   mySMESHGUI->EmitSignalDeactivateDialog() ;   
443   GroupConstructors->setEnabled(true) ;
444   GroupC1->setEnabled(true) ;
445   GroupButtons->setEnabled(true) ;
446   
447   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
448
449   SMESH::SetPointRepresentation(true);
450   QAD_Application::getDesktop()->SetSelectionMode( NodeSelection, true );
451
452   SelectionIntoArgument();
453 }
454
455
456 //=================================================================================
457 // function : enterEvent()
458 // purpose  :
459 //=================================================================================
460 void SMESHGUI_RemoveNodesDlg::enterEvent(QEvent* e)
461 {
462   if ( GroupConstructors->isEnabled() )
463     return ;  
464   ActivateThisDialog() ;
465   return ;
466 }
467
468
469 //=================================================================================
470 // function : closeEvent()
471 // purpose  :
472 //=================================================================================
473 void SMESHGUI_RemoveNodesDlg::closeEvent( QCloseEvent* e )
474 {
475   /* same than click on cancel button */
476   this->ClickOnCancel() ;
477   return ;
478 }
479
480
481 //=======================================================================
482 //function : hideEvent
483 //purpose  : caused by ESC key
484 //=======================================================================
485
486 void SMESHGUI_RemoveNodesDlg::hideEvent ( QHideEvent * e )
487 {
488   if ( !isMinimized() )
489     ClickOnCancel();
490 }