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