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