Salome HOME
Fix for bug PAL19432 (Qt4 porting: Application crash at Add button push (Set Filters...
[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 // File   : SMESHGUI_RenumberingDlg.cxx
23 // Author : Michael ZORIN, Open CASCADE S.A.S.
24 //
25
26 // SMESH includes
27 #include "SMESHGUI_RenumberingDlg.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_VTKUtils.h"
32
33 #include <SMESH_Actor.h>
34 #include <SMESH_TypeFilter.hxx>
35
36 // SALOME GUI includes
37 #include <SUIT_Desktop.h>
38 #include <SUIT_Session.h>
39 #include <SUIT_ResourceMgr.h>
40 #include <SUIT_MessageBox.h>
41
42 #include <LightApp_Application.h>
43 #include <LightApp_SelectionMgr.h>
44
45 #include <SALOME_ListIO.hxx>
46
47 // Qt includes
48 #include <QApplication>
49 #include <QGroupBox>
50 #include <QLabel>
51 #include <QLineEdit>
52 #include <QPushButton>
53 #include <QRadioButton>
54 #include <QVBoxLayout>
55 #include <QHBoxLayout>
56 #include <QKeyEvent>
57 #include <QButtonGroup>
58
59 // IDL includes
60 #include <SALOMEconfig.h>
61 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
62
63 #define SPACING 6
64 #define MARGIN  11
65
66 //=================================================================================
67 // class    : SMESHGUI_RenumberingDlg()
68 // purpose  :
69 //=================================================================================
70 SMESHGUI_RenumberingDlg::SMESHGUI_RenumberingDlg( SMESHGUI* theModule, const int unit)
71   : QDialog( SMESH::GetDesktop( theModule ) ),
72     mySMESHGUI( theModule ),
73     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
74 {
75   myUnit = unit;
76
77   setModal(false);
78   setAttribute(Qt::WA_DeleteOnClose, true);
79   setWindowTitle(unit == 0 ? 
80                  tr("SMESH_RENUMBERING_NODES_TITLE") : 
81                  tr("SMESH_RENUMBERING_ELEMENTS_TITLE"));
82   setSizeGripEnabled(true);
83
84   SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( mySMESHGUI );
85   QPixmap image0(resMgr->loadPixmap("SMESH", unit == 0 ? 
86                                     tr("ICON_DLG_RENUMBERING_NODES") : 
87                                     tr("ICON_DLG_RENUMBERING_ELEMENTS")));
88   QPixmap image1(resMgr->loadPixmap("SMESH",tr("ICON_SELECT")));
89
90   QVBoxLayout* SMESHGUI_RenumberingDlgLayout = new QVBoxLayout(this);
91   SMESHGUI_RenumberingDlgLayout->setSpacing(SPACING);
92   SMESHGUI_RenumberingDlgLayout->setMargin(MARGIN);
93
94   /***************************************************************/
95   GroupConstructors = new QGroupBox(unit == 0 ? 
96                                     tr("SMESH_NODES") :
97                                     tr("SMESH_ELEMENTS"), 
98                                     this);
99   myHelpFileName = unit == 0 ? 
100     "renumbering_nodes_and_elements_page.html#renumbering_nodes_anchor" :
101     "renumbering_nodes_and_elements_page.html#renumbering_elements_anchor";
102
103   QButtonGroup* ButtonGroup = new QButtonGroup(this);
104   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
105   GroupConstructorsLayout->setSpacing(SPACING);
106   GroupConstructorsLayout->setMargin(MARGIN);
107
108   Constructor1 = new QRadioButton(GroupConstructors);
109   Constructor1->setIcon(image0);
110   Constructor1->setChecked(true);
111
112   GroupConstructorsLayout->addWidget(Constructor1);
113   ButtonGroup->addButton(Constructor1, 0);
114
115   /***************************************************************/
116   GroupMesh = new QGroupBox(tr("SMESH_RENUMBERING"), this);
117   QHBoxLayout* GroupMeshLayout = new QHBoxLayout(GroupMesh);
118   GroupMeshLayout->setMargin(MARGIN);
119   GroupMeshLayout->setSpacing(SPACING);
120
121   TextLabelMesh = new QLabel(tr("SMESH_MESH"), GroupMesh);
122   SelectButton = new QPushButton(GroupMesh);
123   SelectButton->setIcon(image1);
124   LineEditMesh = new QLineEdit(GroupMesh);
125   LineEditMesh->setReadOnly(true);
126
127   GroupMeshLayout->addWidget(TextLabelMesh);
128   GroupMeshLayout->addWidget(SelectButton);
129   GroupMeshLayout->addWidget(LineEditMesh);
130
131   /***************************************************************/
132   GroupButtons = new QGroupBox(this);
133   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
134   GroupButtonsLayout->setSpacing(SPACING);
135   GroupButtonsLayout->setMargin(MARGIN);
136
137   buttonOk = new QPushButton(tr("SMESH_BUT_OK"), GroupButtons);
138   buttonOk->setAutoDefault(true);
139   buttonOk->setDefault(true);
140   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
141   buttonApply->setAutoDefault(true);
142   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
143   buttonCancel->setAutoDefault(true);
144   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
145   buttonHelp->setAutoDefault(true);
146
147   GroupButtonsLayout->addWidget(buttonOk);
148   GroupButtonsLayout->addSpacing(10);
149   GroupButtonsLayout->addWidget(buttonApply);
150   GroupButtonsLayout->addSpacing(10);
151   GroupButtonsLayout->addStretch();
152   GroupButtonsLayout->addWidget(buttonCancel);
153   GroupButtonsLayout->addWidget(buttonHelp);
154
155   /***************************************************************/
156   SMESHGUI_RenumberingDlgLayout->addWidget(GroupConstructors);
157   SMESHGUI_RenumberingDlgLayout->addWidget(GroupMesh);
158   SMESHGUI_RenumberingDlgLayout->addWidget(GroupButtons);
159
160   Init(); /* Initialisations */
161 }
162
163 //=================================================================================
164 // function : ~SMESHGUI_RenumberingDlg()
165 // purpose  : Destroys the object and frees any allocated resources
166 //=================================================================================
167 SMESHGUI_RenumberingDlg::~SMESHGUI_RenumberingDlg()
168 {
169 }
170
171 //=================================================================================
172 // function : Init()
173 // purpose  :
174 //=================================================================================
175 void SMESHGUI_RenumberingDlg::Init()
176 {
177   myConstructorId = 0;
178   Constructor1->setChecked(true);
179   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
180
181   myMesh = SMESH::SMESH_Mesh::_nil();
182
183   myMeshFilter = new SMESH_TypeFilter (MESH);
184
185   /* signals and slots connections */
186   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
187   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
188   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
189   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
190
191   connect(SelectButton, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
192   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
193   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
194   /* to close dialog if study change */
195   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
196
197   myEditCurrentArgument = LineEditMesh;
198   LineEditMesh->setFocus();
199   mySelectionMgr->clearFilters();
200   mySelectionMgr->installFilter(myMeshFilter);
201
202   SelectionIntoArgument();
203 }
204
205 //=================================================================================
206 // function : ClickOnApply()
207 // purpose  :
208 //=================================================================================
209 void SMESHGUI_RenumberingDlg::ClickOnApply()
210 {
211   if (mySMESHGUI->isActiveStudyLocked())
212     return;
213
214   if (!myMesh->_is_nil()) {
215     try {
216       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
217       SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
218       bool isUnitsLabeled = false;
219       
220       if (myUnit == 0 && anActor) {
221         isUnitsLabeled = anActor->GetPointsLabeled();
222         if (isUnitsLabeled)  anActor->SetPointsLabeled(false);
223       }
224       else if (myUnit == 1 && anActor) {
225         isUnitsLabeled = anActor->GetCellsLabeled();
226         if (isUnitsLabeled)  anActor->SetCellsLabeled(false);
227       }
228       
229       QApplication::setOverrideCursor(Qt::WaitCursor);
230       if (myUnit == 0) {
231         aMeshEditor->RenumberNodes();
232         if (isUnitsLabeled && anActor) anActor->SetPointsLabeled(true);
233       }
234       else if (myUnit == 1) {
235         aMeshEditor->RenumberElements();
236         if (isUnitsLabeled && anActor) anActor->SetCellsLabeled(true);
237       }
238       QApplication::restoreOverrideCursor();
239     }
240     catch(...) {
241     }
242     
243     //mySelectionMgr->clearSelected();
244     SMESH::UpdateView();
245   }
246 }
247
248 //=================================================================================
249 // function : ClickOnOk()
250 // purpose  :
251 //=================================================================================
252 void SMESHGUI_RenumberingDlg::ClickOnOk()
253 {
254   ClickOnApply();
255   ClickOnCancel();
256 }
257
258 //=================================================================================
259 // function : ClickOnCancel()
260 // purpose  :
261 //=================================================================================
262 void SMESHGUI_RenumberingDlg::ClickOnCancel()
263 {
264   //mySelectionMgr->clearSelected();
265   mySelectionMgr->clearFilters();
266   disconnect(mySelectionMgr, 0, this, 0);
267   mySMESHGUI->ResetState();
268   reject();
269 }
270
271 //=================================================================================
272 // function : ClickOnHelp()
273 // purpose  :
274 //=================================================================================
275 void SMESHGUI_RenumberingDlg::ClickOnHelp()
276 {
277   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
278   if (app) 
279     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
280   else {
281     QString platform;
282 #ifdef WIN32
283     platform = "winapplication";
284 #else
285     platform = "application";
286 #endif
287     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
288                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
289                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
290                                                                  platform)).
291                              arg(myHelpFileName));
292   }
293 }
294
295 //=================================================================================
296 // function : SelectionIntoArgument()
297 // purpose  : Called when selection as changed or other case
298 //=================================================================================
299 void SMESHGUI_RenumberingDlg::SelectionIntoArgument()
300 {
301   if (!GroupButtons->isEnabled()) // inactive
302     return;
303
304   QString aString = "";
305
306   SALOME_ListIO aList;
307   mySelectionMgr->selectedObjects(aList);
308   int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
309
310   if (myEditCurrentArgument == LineEditMesh) {
311     // mesh
312     if (nbSel != 1) {
313       myMesh = SMESH::SMESH_Mesh::_nil();
314       aString = "";
315     } else {
316       Handle(SALOME_InteractiveObject) IO = aList.First();
317       myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
318       if (myMesh->_is_nil())
319         aString = "";
320     }
321   }
322
323   myEditCurrentArgument->setText(aString);
324
325   bool isEnabled = (!myMesh->_is_nil());
326   buttonOk->setEnabled(isEnabled);
327   buttonApply->setEnabled(isEnabled);
328 }
329
330 //=================================================================================
331 // function : SetEditCurrentArgument()
332 // purpose  :
333 //=================================================================================
334 void SMESHGUI_RenumberingDlg::SetEditCurrentArgument()
335 {
336   QPushButton* send = (QPushButton*)sender();
337   switch (myConstructorId)
338     {
339     case 0: /* default constructor */
340       {
341         if(send == SelectButton) {
342           LineEditMesh->setFocus();
343           myEditCurrentArgument = LineEditMesh;
344         }
345         SelectionIntoArgument();
346         break;
347       }
348     }
349 }
350
351 //=================================================================================
352 // function : DeactivateActiveDialog()
353 // purpose  :
354 //=================================================================================
355 void SMESHGUI_RenumberingDlg::DeactivateActiveDialog()
356 {
357   if (GroupConstructors->isEnabled()) {
358     GroupConstructors->setEnabled(false);
359     GroupMesh->setEnabled(false);
360     GroupButtons->setEnabled(false);
361     mySMESHGUI->ResetState();
362     mySMESHGUI->SetActiveDialogBox(0);
363   }
364 }
365
366 //=================================================================================
367 // function : ActivateThisDialog()
368 // purpose  :
369 //=================================================================================
370 void SMESHGUI_RenumberingDlg::ActivateThisDialog()
371 {
372   /* Emit a signal to deactivate the active dialog */
373   mySMESHGUI->EmitSignalDeactivateDialog();
374   GroupConstructors->setEnabled(true);
375   GroupMesh->setEnabled(true);
376   GroupButtons->setEnabled(true);
377
378   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
379   SelectionIntoArgument();
380 }
381
382 //=================================================================================
383 // function : enterEvent()
384 // purpose  :
385 //=================================================================================
386 void SMESHGUI_RenumberingDlg::enterEvent(QEvent* e)
387 {
388   if (GroupConstructors->isEnabled())
389     return;
390   ActivateThisDialog();
391 }
392
393 //=================================================================================
394 // function : closeEvent()
395 // purpose  :
396 //=================================================================================
397 void SMESHGUI_RenumberingDlg::closeEvent(QCloseEvent* e)
398 {
399   /* same than click on cancel button */
400   ClickOnCancel();
401 }
402
403 //=======================================================================
404 //function : hideEvent
405 //purpose  : caused by ESC key
406 //=======================================================================
407 void SMESHGUI_RenumberingDlg::hideEvent (QHideEvent * e)
408 {
409   if (!isMinimized())
410     ClickOnCancel();
411 }
412
413 //=================================================================================
414 // function : keyPressEvent()
415 // purpose  :
416 //=================================================================================
417 void SMESHGUI_RenumberingDlg::keyPressEvent( QKeyEvent* e )
418 {
419   QDialog::keyPressEvent( e );
420   if ( e->isAccepted() )
421     return;
422
423   if ( e->key() == Qt::Key_F1 ) {
424     e->accept();
425     ClickOnHelp();
426   }
427 }