Salome HOME
Integration of PAL/SALOME V2.1.0c from OCC
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_RenumberingDlg.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_RenumberingDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_RenumberingDlg.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34 #include "SMESH_Actor.h"
35 #include "SMDS_Mesh.hxx"
36
37 #include "QAD_Application.h"
38 #include "QAD_Desktop.h"
39 #include "QAD_MessageBox.h"
40 #include "utilities.h"
41
42 // QT Includes
43 #include <qapplication.h>
44 #include <qbuttongroup.h>
45 #include <qgroupbox.h>
46 #include <qlabel.h>
47 #include <qlineedit.h>
48 #include <qpushbutton.h>
49 #include <qradiobutton.h>
50 #include <qlayout.h>
51 #include <qpixmap.h>
52
53 using namespace std;
54
55 //=================================================================================
56 // class    : SMESHGUI_RenumberingDlg()
57 // purpose  : 
58 //=================================================================================
59 SMESHGUI_RenumberingDlg::SMESHGUI_RenumberingDlg( QWidget* parent, const char* name, SALOME_Selection* Sel,
60                                                   const int unit, bool modal, WFlags fl )
61     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu |
62                Qt::WDestructiveClose)
63 {
64   myUnit = unit;
65
66   if ( !name )
67     setName( "SMESHGUI_RenumberingDlg" );
68   resize( 303, 185 );
69   
70
71   QPixmap image0;
72   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
73
74   if (unit == 0) {
75     image0 = QPixmap(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_DLG_RENUMBERING_NODES")));
76     setCaption( tr( "SMESH_RENUMBERING_NODES_TITLE"  ) );
77   }
78   else if (unit == 1) {
79     image0 = QPixmap(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_DLG_RENUMBERING_ELEMENTS")));
80     setCaption( tr( "SMESH_RENUMBERING_ELEMENTS_TITLE"  ) );
81   }
82   
83   setSizeGripEnabled( TRUE );
84   SMESHGUI_RenumberingDlgLayout = new QGridLayout( this ); 
85   SMESHGUI_RenumberingDlgLayout->setSpacing( 6 );
86   SMESHGUI_RenumberingDlgLayout->setMargin( 11 );
87   
88   /***************************************************************/
89   GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
90   if (unit == 0)
91     GroupConstructors->setTitle( tr( "SMESH_NODES"  ) );
92   else if (unit == 1)
93     GroupConstructors->setTitle( tr( "SMESH_ELEMENTS"  ) );
94   GroupConstructors->setExclusive( TRUE );
95   GroupConstructors->setColumnLayout(0, Qt::Vertical );
96   GroupConstructors->layout()->setSpacing( 0 );
97   GroupConstructors->layout()->setMargin( 0 );
98   GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
99   GroupConstructorsLayout->setAlignment( Qt::AlignTop );
100   GroupConstructorsLayout->setSpacing( 6 );
101   GroupConstructorsLayout->setMargin( 11 );
102   Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
103   Constructor1->setText( tr( ""  ) );
104   Constructor1->setPixmap( image0 );
105   Constructor1->setChecked( TRUE );
106   Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
107   Constructor1->setMinimumSize( QSize( 50, 0 ) );
108   GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
109   QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
110   GroupConstructorsLayout->addItem( spacer, 0, 1 );
111   SMESHGUI_RenumberingDlgLayout->addWidget( GroupConstructors, 0, 0 );
112   
113   /***************************************************************/
114   GroupButtons = new QGroupBox( this, "GroupButtons" );
115   GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
116   GroupButtons->setTitle( tr( ""  ) );
117   GroupButtons->setColumnLayout(0, Qt::Vertical );
118   GroupButtons->layout()->setSpacing( 0 );
119   GroupButtons->layout()->setMargin( 0 );
120   GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
121   GroupButtonsLayout->setAlignment( Qt::AlignTop );
122   GroupButtonsLayout->setSpacing( 6 );
123   GroupButtonsLayout->setMargin( 11 );
124   buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
125   buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
126   buttonCancel->setAutoDefault( TRUE );
127   GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
128   buttonApply = new QPushButton( GroupButtons, "buttonApply" );
129   buttonApply->setText( tr( "SMESH_BUT_APPLY"  ) );
130   buttonApply->setAutoDefault( TRUE );
131   GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
132   QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
133   GroupButtonsLayout->addItem( spacer_9, 0, 2 );
134   buttonOk = new QPushButton( GroupButtons, "buttonOk" );
135   buttonOk->setText( tr( "SMESH_BUT_OK"  ) );
136   buttonOk->setAutoDefault( TRUE );
137   buttonOk->setDefault( TRUE );
138   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
139   SMESHGUI_RenumberingDlgLayout->addWidget( GroupButtons, 2, 0 );
140   
141   /***************************************************************/
142   GroupMesh = new QGroupBox( this, "GroupMesh" );
143   GroupMesh->setTitle( tr( "SMESH_RENUMBERING"  ) );
144   GroupMesh->setMinimumSize( QSize( 0, 0 ) );
145   GroupMesh->setFrameShape( QGroupBox::Box );
146   GroupMesh->setFrameShadow( QGroupBox::Sunken );
147   GroupMesh->setColumnLayout(0, Qt::Vertical );
148   GroupMesh->layout()->setSpacing( 0 );
149   GroupMesh->layout()->setMargin( 0 );
150   GroupMeshLayout = new QGridLayout( GroupMesh->layout() );
151   GroupMeshLayout->setAlignment( Qt::AlignTop );
152   GroupMeshLayout->setSpacing( 6 );
153   GroupMeshLayout->setMargin( 11 );
154   TextLabelMesh = new QLabel( GroupMesh, "TextLabelMesh" );
155   TextLabelMesh->setText( tr( "SMESH_MESH" ) );
156   TextLabelMesh->setMinimumSize( QSize( 50, 0 ) );
157   TextLabelMesh->setFrameShape( QLabel::NoFrame );
158   TextLabelMesh->setFrameShadow( QLabel::Plain );
159   GroupMeshLayout->addWidget( TextLabelMesh, 0, 0 );
160   SelectButton = new QPushButton( GroupMesh, "SelectButton" );
161   SelectButton->setText( tr( ""  ) );
162   SelectButton->setPixmap( image1 );
163   SelectButton->setToggleButton( FALSE );
164   GroupMeshLayout->addWidget( SelectButton, 0, 1 );
165   LineEditMesh = new QLineEdit( GroupMesh, "LineEditMesh" );
166   LineEditMesh->setReadOnly( true );
167   GroupMeshLayout->addWidget( LineEditMesh, 0, 2 );
168   SMESHGUI_RenumberingDlgLayout->addWidget( GroupMesh, 1, 0 );
169   
170   Init(Sel) ; /* Initialisations */
171 }
172
173 //=================================================================================
174 // function : ~SMESHGUI_RenumberingDlg()
175 // purpose  : Destroys the object and frees any allocated resources
176 //=================================================================================
177 SMESHGUI_RenumberingDlg::~SMESHGUI_RenumberingDlg()
178 {
179   // no need to delete child widgets, Qt does it all for us
180 }
181
182
183 //=================================================================================
184 // function : Init()
185 // purpose  :
186 //=================================================================================
187 void SMESHGUI_RenumberingDlg::Init( SALOME_Selection* Sel )
188 {
189
190   GroupMesh->show();
191   myConstructorId = 0 ;
192   Constructor1->setChecked( TRUE );
193   mySelection = Sel;  
194   mySMESHGUI  = SMESHGUI::GetSMESHGUI() ;
195   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
196
197   myMesh = SMESH::SMESH_Mesh::_nil();
198     
199   myMeshFilter = new SMESH_TypeFilter( MESH );
200
201   /* signals and slots connections */
202   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
203   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
204   connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
205   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
206   
207   connect( SelectButton, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
208   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
209   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
210   /* to close dialog if study change */
211   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
212   
213   /* Move widget on the botton right corner of main widget */
214   int x, y ;
215   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
216   this->move( x, y ) ;
217   this->show() ; /* displays Dialog */
218   
219   myEditCurrentArgument = LineEditMesh ;
220   LineEditMesh->setFocus();
221   mySelection->ClearFilters() ;   
222   mySelection->AddFilter( myMeshFilter ) ;
223   
224   SelectionIntoArgument();
225 }
226
227
228 //=================================================================================
229 // function : ConstructorsClicked()
230 // purpose  : Radio button management
231 //=================================================================================
232 void SMESHGUI_RenumberingDlg::ConstructorsClicked(int constructorId)
233 {
234   return ;
235 }
236
237
238 //=================================================================================
239 // function : ClickOnApply()
240 // purpose  :
241 //=================================================================================
242 void SMESHGUI_RenumberingDlg::ClickOnApply()
243 {
244   if (mySMESHGUI->ActiveStudyLocked())
245     return;
246   
247   if ( !myMesh->_is_nil())
248     {
249       bool aResult = false;
250       try
251         {
252           SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
253           SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
254           bool isUnitsLabeled = false;
255           
256           if (myUnit == 0 && anActor) {
257             isUnitsLabeled = anActor->GetPointsLabeled();
258             if (isUnitsLabeled)  anActor->SetPointsLabeled(false);
259           }
260           else if (myUnit == 1 && anActor) {
261             isUnitsLabeled = anActor->GetCellsLabeled();
262             if (isUnitsLabeled)  anActor->SetCellsLabeled(false);
263           }
264           
265           QApplication::setOverrideCursor(Qt::waitCursor);
266           if (myUnit == 0) {
267             aMeshEditor->RenumberNodes();
268             if (isUnitsLabeled && anActor) anActor->SetPointsLabeled(true);
269           }
270           else if (myUnit == 1) {
271             aMeshEditor->RenumberElements();
272             if (isUnitsLabeled && anActor) anActor->SetCellsLabeled(true);
273           }
274           QApplication::restoreOverrideCursor();
275         }
276       catch( ... )
277         {
278         }
279       
280       mySelection->ClearIObjects();
281       SMESH::UpdateView();
282     }
283 }
284
285 //=================================================================================
286 // function : ClickOnOk()
287 // purpose  :
288 //=================================================================================
289 void SMESHGUI_RenumberingDlg::ClickOnOk()
290 {
291   ClickOnApply() ;
292   ClickOnCancel() ;
293 }
294
295         
296 //=================================================================================
297 // function : ClickOnCancel()
298 // purpose  :
299 //=================================================================================
300 void SMESHGUI_RenumberingDlg::ClickOnCancel()
301 {
302   mySelection->ClearIObjects();
303   mySelection->ClearFilters();
304   disconnect( mySelection, 0, this, 0 );
305   mySMESHGUI->ResetState() ;
306   reject() ;
307 }
308
309
310 //=================================================================================
311 // function : SelectionIntoArgument()
312 // purpose  : Called when selection as changed or other case
313 //=================================================================================
314 void SMESHGUI_RenumberingDlg::SelectionIntoArgument()
315 {
316   if ( !GroupButtons->isEnabled() ) // inactive
317     return;
318   
319   QString aString = ""; 
320   
321   int nbSel = SMESH::GetNameOfSelectedIObjects(mySelection, aString) ;
322
323   if ( myEditCurrentArgument == LineEditMesh ) {
324     // mesh
325     if ( nbSel != 1 ) {
326       myMesh = SMESH::SMESH_Mesh::_nil();
327       aString = "";
328     }
329     else {
330       Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
331       myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO) ;
332       if( myMesh->_is_nil() )
333         aString = "";
334     }
335   }
336   
337   myEditCurrentArgument->setText(aString) ;
338   
339   bool isEnabled = (!myMesh->_is_nil());
340   buttonOk->setEnabled( isEnabled );
341   buttonApply->setEnabled( isEnabled );
342 }
343
344
345 //=================================================================================
346 // function : SetEditCurrentArgument()
347 // purpose  :
348 //=================================================================================
349 void SMESHGUI_RenumberingDlg::SetEditCurrentArgument()
350 {
351   QPushButton* send = (QPushButton*)sender();
352   switch (myConstructorId)
353     {
354     case 0: /* default constructor */
355       { 
356         if(send == SelectButton) {
357           LineEditMesh->setFocus() ;
358           myEditCurrentArgument = LineEditMesh;
359         }
360         SelectionIntoArgument() ;
361         break;
362       }
363     }
364 }
365
366 //=================================================================================
367 // function : DeactivateActiveDialog()
368 // purpose  :
369 //=================================================================================
370 void SMESHGUI_RenumberingDlg::DeactivateActiveDialog()
371 {
372   if ( GroupConstructors->isEnabled() ) {
373     GroupConstructors->setEnabled(false) ;
374     GroupMesh->setEnabled(false) ;
375     GroupButtons->setEnabled(false) ;
376     mySMESHGUI->ResetState() ;    
377     mySMESHGUI->SetActiveDialogBox(0) ;
378   }
379 }
380
381
382 //=================================================================================
383 // function : ActivateThisDialog()
384 // purpose  :
385 //=================================================================================
386 void SMESHGUI_RenumberingDlg::ActivateThisDialog()
387 {
388   /* Emit a signal to deactivate the active dialog */
389   mySMESHGUI->EmitSignalDeactivateDialog() ;   
390   GroupConstructors->setEnabled(true) ;
391   GroupMesh->setEnabled(true) ;
392   GroupButtons->setEnabled(true) ;
393   
394   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
395   SelectionIntoArgument();
396 }
397
398
399 //=================================================================================
400 // function : enterEvent()
401 // purpose  :
402 //=================================================================================
403 void SMESHGUI_RenumberingDlg::enterEvent(QEvent* e)
404 {
405   if ( GroupConstructors->isEnabled() )
406     return ;  
407   ActivateThisDialog() ;
408 }
409
410
411 //=================================================================================
412 // function : closeEvent()
413 // purpose  :
414 //=================================================================================
415 void SMESHGUI_RenumberingDlg::closeEvent( QCloseEvent* e )
416 {
417   /* same than click on cancel button */
418   this->ClickOnCancel() ;
419 }
420
421
422 //=======================================================================
423 //function : hideEvent
424 //purpose  : caused by ESC key
425 //=======================================================================
426
427 void SMESHGUI_RenumberingDlg::hideEvent ( QHideEvent * e )
428 {
429   if ( !isMinimized() )
430     ClickOnCancel();
431 }