Salome HOME
PAL6938
[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       try
250         {
251           SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
252           SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
253           bool isUnitsLabeled = false;
254           
255           if (myUnit == 0 && anActor) {
256             isUnitsLabeled = anActor->GetPointsLabeled();
257             if (isUnitsLabeled)  anActor->SetPointsLabeled(false);
258           }
259           else if (myUnit == 1 && anActor) {
260             isUnitsLabeled = anActor->GetCellsLabeled();
261             if (isUnitsLabeled)  anActor->SetCellsLabeled(false);
262           }
263           
264           QApplication::setOverrideCursor(Qt::waitCursor);
265           if (myUnit == 0) {
266             aMeshEditor->RenumberNodes();
267             if (isUnitsLabeled && anActor) anActor->SetPointsLabeled(true);
268           }
269           else if (myUnit == 1) {
270             aMeshEditor->RenumberElements();
271             if (isUnitsLabeled && anActor) anActor->SetCellsLabeled(true);
272           }
273           QApplication::restoreOverrideCursor();
274         }
275       catch( ... )
276         {
277         }
278       
279       mySelection->ClearIObjects();
280       SMESH::UpdateView();
281     }
282 }
283
284 //=================================================================================
285 // function : ClickOnOk()
286 // purpose  :
287 //=================================================================================
288 void SMESHGUI_RenumberingDlg::ClickOnOk()
289 {
290   ClickOnApply() ;
291   ClickOnCancel() ;
292 }
293
294         
295 //=================================================================================
296 // function : ClickOnCancel()
297 // purpose  :
298 //=================================================================================
299 void SMESHGUI_RenumberingDlg::ClickOnCancel()
300 {
301   mySelection->ClearIObjects();
302   mySelection->ClearFilters();
303   disconnect( mySelection, 0, this, 0 );
304   mySMESHGUI->ResetState() ;
305   reject() ;
306 }
307
308
309 //=================================================================================
310 // function : SelectionIntoArgument()
311 // purpose  : Called when selection as changed or other case
312 //=================================================================================
313 void SMESHGUI_RenumberingDlg::SelectionIntoArgument()
314 {
315   if ( !GroupButtons->isEnabled() ) // inactive
316     return;
317   
318   QString aString = ""; 
319   
320   int nbSel = SMESH::GetNameOfSelectedIObjects(mySelection, aString) ;
321
322   if ( myEditCurrentArgument == LineEditMesh ) {
323     // mesh
324     if ( nbSel != 1 ) {
325       myMesh = SMESH::SMESH_Mesh::_nil();
326       aString = "";
327     }
328     else {
329       Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
330       myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO) ;
331       if( myMesh->_is_nil() )
332         aString = "";
333     }
334   }
335   
336   myEditCurrentArgument->setText(aString) ;
337   
338   bool isEnabled = (!myMesh->_is_nil());
339   buttonOk->setEnabled( isEnabled );
340   buttonApply->setEnabled( isEnabled );
341 }
342
343
344 //=================================================================================
345 // function : SetEditCurrentArgument()
346 // purpose  :
347 //=================================================================================
348 void SMESHGUI_RenumberingDlg::SetEditCurrentArgument()
349 {
350   QPushButton* send = (QPushButton*)sender();
351   switch (myConstructorId)
352     {
353     case 0: /* default constructor */
354       { 
355         if(send == SelectButton) {
356           LineEditMesh->setFocus() ;
357           myEditCurrentArgument = LineEditMesh;
358         }
359         SelectionIntoArgument() ;
360         break;
361       }
362     }
363 }
364
365 //=================================================================================
366 // function : DeactivateActiveDialog()
367 // purpose  :
368 //=================================================================================
369 void SMESHGUI_RenumberingDlg::DeactivateActiveDialog()
370 {
371   if ( GroupConstructors->isEnabled() ) {
372     GroupConstructors->setEnabled(false) ;
373     GroupMesh->setEnabled(false) ;
374     GroupButtons->setEnabled(false) ;
375     mySMESHGUI->ResetState() ;    
376     mySMESHGUI->SetActiveDialogBox(0) ;
377   }
378 }
379
380
381 //=================================================================================
382 // function : ActivateThisDialog()
383 // purpose  :
384 //=================================================================================
385 void SMESHGUI_RenumberingDlg::ActivateThisDialog()
386 {
387   /* Emit a signal to deactivate the active dialog */
388   mySMESHGUI->EmitSignalDeactivateDialog() ;   
389   GroupConstructors->setEnabled(true) ;
390   GroupMesh->setEnabled(true) ;
391   GroupButtons->setEnabled(true) ;
392   
393   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
394   SelectionIntoArgument();
395 }
396
397
398 //=================================================================================
399 // function : enterEvent()
400 // purpose  :
401 //=================================================================================
402 void SMESHGUI_RenumberingDlg::enterEvent(QEvent* e)
403 {
404   if ( GroupConstructors->isEnabled() )
405     return ;  
406   ActivateThisDialog() ;
407 }
408
409
410 //=================================================================================
411 // function : closeEvent()
412 // purpose  :
413 //=================================================================================
414 void SMESHGUI_RenumberingDlg::closeEvent( QCloseEvent* e )
415 {
416   /* same than click on cancel button */
417   this->ClickOnCancel() ;
418 }
419
420
421 //=======================================================================
422 //function : hideEvent
423 //purpose  : caused by ESC key
424 //=======================================================================
425
426 void SMESHGUI_RenumberingDlg::hideEvent ( QHideEvent * e )
427 {
428   if ( !isMinimized() )
429     ClickOnCancel();
430 }