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