Salome HOME
Using files from package LightApp instead of files from package SalomeApp
[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_Desktop.h"
40 #include "SUIT_Session.h"
41
42 #include "SALOME_ListIO.hxx"
43
44 #include "utilities.h"
45
46 // QT Includes
47 #include <qapplication.h>
48 #include <qbuttongroup.h>
49 #include <qgroupbox.h>
50 #include <qlabel.h>
51 #include <qlineedit.h>
52 #include <qpushbutton.h>
53 #include <qradiobutton.h>
54 #include <qlayout.h>
55 #include <qpixmap.h>
56
57 using namespace std;
58
59 //=================================================================================
60 // class    : SMESHGUI_RenumberingDlg()
61 // purpose  :
62 //=================================================================================
63 SMESHGUI_RenumberingDlg::SMESHGUI_RenumberingDlg( SMESHGUI* theModule, const char* name,
64                                                   const int unit, bool modal, WFlags fl)
65      : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder |
66                WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),
67      mySMESHGUI( theModule ),
68      mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
69 {
70   myUnit = unit;
71
72   if (!name)
73     setName("SMESHGUI_RenumberingDlg");
74   resize(303, 185);
75
76   QPixmap image0;
77   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("SMESH",tr("ICON_SELECT")));
78
79   if (unit == 0) {
80     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("SMESH",tr("ICON_DLG_RENUMBERING_NODES")));
81     setCaption(tr("SMESH_RENUMBERING_NODES_TITLE" ));
82   }
83   else if (unit == 1) {
84     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("SMESH",tr("ICON_DLG_RENUMBERING_ELEMENTS")));
85     setCaption(tr("SMESH_RENUMBERING_ELEMENTS_TITLE" ));
86   }
87
88   setSizeGripEnabled(TRUE);
89   SMESHGUI_RenumberingDlgLayout = new QGridLayout(this);
90   SMESHGUI_RenumberingDlgLayout->setSpacing(6);
91   SMESHGUI_RenumberingDlgLayout->setMargin(11);
92
93   /***************************************************************/
94   GroupConstructors = new QButtonGroup(this, "GroupConstructors");
95   if (unit == 0)
96     GroupConstructors->setTitle(tr("SMESH_NODES" ));
97   else if (unit == 1)
98     GroupConstructors->setTitle(tr("SMESH_ELEMENTS" ));
99   GroupConstructors->setExclusive(TRUE);
100   GroupConstructors->setColumnLayout(0, Qt::Vertical);
101   GroupConstructors->layout()->setSpacing(0);
102   GroupConstructors->layout()->setMargin(0);
103   GroupConstructorsLayout = new QGridLayout(GroupConstructors->layout());
104   GroupConstructorsLayout->setAlignment(Qt::AlignTop);
105   GroupConstructorsLayout->setSpacing(6);
106   GroupConstructorsLayout->setMargin(11);
107   Constructor1 = new QRadioButton(GroupConstructors, "Constructor1");
108   Constructor1->setText(tr("" ));
109   Constructor1->setPixmap(image0);
110   Constructor1->setChecked(TRUE);
111   Constructor1->setSizePolicy(QSizePolicy((QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth()));
112   Constructor1->setMinimumSize(QSize(50, 0));
113   GroupConstructorsLayout->addWidget(Constructor1, 0, 0);
114   QSpacerItem* spacer = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
115   GroupConstructorsLayout->addItem(spacer, 0, 1);
116   SMESHGUI_RenumberingDlgLayout->addWidget(GroupConstructors, 0, 0);
117
118   /***************************************************************/
119   GroupButtons = new QGroupBox(this, "GroupButtons");
120   GroupButtons->setGeometry(QRect(10, 10, 281, 48));
121   GroupButtons->setTitle(tr("" ));
122   GroupButtons->setColumnLayout(0, Qt::Vertical);
123   GroupButtons->layout()->setSpacing(0);
124   GroupButtons->layout()->setMargin(0);
125   GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
126   GroupButtonsLayout->setAlignment(Qt::AlignTop);
127   GroupButtonsLayout->setSpacing(6);
128   GroupButtonsLayout->setMargin(11);
129   buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
130   buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
131   buttonCancel->setAutoDefault(TRUE);
132   GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
133   buttonApply = new QPushButton(GroupButtons, "buttonApply");
134   buttonApply->setText(tr("SMESH_BUT_APPLY" ));
135   buttonApply->setAutoDefault(TRUE);
136   GroupButtonsLayout->addWidget(buttonApply, 0, 1);
137   QSpacerItem* spacer_9 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
138   GroupButtonsLayout->addItem(spacer_9, 0, 2);
139   buttonOk = new QPushButton(GroupButtons, "buttonOk");
140   buttonOk->setText(tr("SMESH_BUT_OK" ));
141   buttonOk->setAutoDefault(TRUE);
142   buttonOk->setDefault(TRUE);
143   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
144   SMESHGUI_RenumberingDlgLayout->addWidget(GroupButtons, 2, 0);
145
146   /***************************************************************/
147   GroupMesh = new QGroupBox(this, "GroupMesh");
148   GroupMesh->setTitle(tr("SMESH_RENUMBERING" ));
149   GroupMesh->setMinimumSize(QSize(0, 0));
150   GroupMesh->setFrameShape(QGroupBox::Box);
151   GroupMesh->setFrameShadow(QGroupBox::Sunken);
152   GroupMesh->setColumnLayout(0, Qt::Vertical);
153   GroupMesh->layout()->setSpacing(0);
154   GroupMesh->layout()->setMargin(0);
155   GroupMeshLayout = new QGridLayout(GroupMesh->layout());
156   GroupMeshLayout->setAlignment(Qt::AlignTop);
157   GroupMeshLayout->setSpacing(6);
158   GroupMeshLayout->setMargin(11);
159   TextLabelMesh = new QLabel(GroupMesh, "TextLabelMesh");
160   TextLabelMesh->setText(tr("SMESH_MESH"));
161   TextLabelMesh->setMinimumSize(QSize(50, 0));
162   TextLabelMesh->setFrameShape(QLabel::NoFrame);
163   TextLabelMesh->setFrameShadow(QLabel::Plain);
164   GroupMeshLayout->addWidget(TextLabelMesh, 0, 0);
165   SelectButton = new QPushButton(GroupMesh, "SelectButton");
166   SelectButton->setText(tr("" ));
167   SelectButton->setPixmap(image1);
168   SelectButton->setToggleButton(FALSE);
169   GroupMeshLayout->addWidget(SelectButton, 0, 1);
170   LineEditMesh = new QLineEdit(GroupMesh, "LineEditMesh");
171   LineEditMesh->setReadOnly(true);
172   GroupMeshLayout->addWidget(LineEditMesh, 0, 2);
173   SMESHGUI_RenumberingDlgLayout->addWidget(GroupMesh, 1, 0);
174
175   Init(); /* Initialisations */
176 }
177
178 //=================================================================================
179 // function : ~SMESHGUI_RenumberingDlg()
180 // purpose  : Destroys the object and frees any allocated resources
181 //=================================================================================
182 SMESHGUI_RenumberingDlg::~SMESHGUI_RenumberingDlg()
183 {
184   // no need to delete child widgets, Qt does it all for us
185 }
186
187 //=================================================================================
188 // function : Init()
189 // purpose  :
190 //=================================================================================
191 void SMESHGUI_RenumberingDlg::Init()
192 {
193   GroupMesh->show();
194   myConstructorId = 0;
195   Constructor1->setChecked(TRUE);
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 }