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