Salome HOME
untabify
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_RenumberingDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 // SMESH SMESHGUI : GUI for SMESH component
23 // File   : SMESHGUI_RenumberingDlg.cxx
24 // Author : Michael ZORIN, Open CASCADE S.A.S.
25 // SMESH includes
26 //
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 #include <SUIT_OverrideCursor.h>
42
43 #include <LightApp_Application.h>
44 #include <LightApp_SelectionMgr.h>
45
46 #include <SALOME_ListIO.hxx>
47
48 // Qt includes
49 #include <QApplication>
50 #include <QGroupBox>
51 #include <QLabel>
52 #include <QLineEdit>
53 #include <QPushButton>
54 #include <QRadioButton>
55 #include <QVBoxLayout>
56 #include <QHBoxLayout>
57 #include <QKeyEvent>
58 #include <QButtonGroup>
59
60 // IDL includes
61 #include <SALOMEconfig.h>
62 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
63
64 #define SPACING 6
65 #define MARGIN  11
66
67 //=================================================================================
68 // class    : SMESHGUI_RenumberingDlg()
69 // purpose  :
70 //=================================================================================
71 SMESHGUI_RenumberingDlg::SMESHGUI_RenumberingDlg( SMESHGUI* theModule, const int unit)
72   : QDialog( SMESH::GetDesktop( theModule ) ),
73     mySMESHGUI( theModule ),
74     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
75 {
76   myUnit = unit;
77
78   setModal(false);
79   setAttribute(Qt::WA_DeleteOnClose, true);
80   setWindowTitle(unit == 0 ? 
81                  tr("SMESH_RENUMBERING_NODES_TITLE") : 
82                  tr("SMESH_RENUMBERING_ELEMENTS_TITLE"));
83   setSizeGripEnabled(true);
84
85   SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( mySMESHGUI );
86   QPixmap image0(resMgr->loadPixmap("SMESH", unit == 0 ? 
87                                     tr("ICON_DLG_RENUMBERING_NODES") : 
88                                     tr("ICON_DLG_RENUMBERING_ELEMENTS")));
89   QPixmap image1(resMgr->loadPixmap("SMESH",tr("ICON_SELECT")));
90
91   QVBoxLayout* SMESHGUI_RenumberingDlgLayout = new QVBoxLayout(this);
92   SMESHGUI_RenumberingDlgLayout->setSpacing(SPACING);
93   SMESHGUI_RenumberingDlgLayout->setMargin(MARGIN);
94
95   /***************************************************************/
96   GroupConstructors = new QGroupBox(unit == 0 ? 
97                                     tr("SMESH_NODES") :
98                                     tr("SMESH_ELEMENTS"), 
99                                     this);
100   myHelpFileName = unit == 0 ? 
101     "renumbering_nodes_and_elements_page.html#renumbering_nodes_anchor" :
102     "renumbering_nodes_and_elements_page.html#renumbering_elements_anchor";
103
104   QButtonGroup* ButtonGroup = new QButtonGroup(this);
105   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
106   GroupConstructorsLayout->setSpacing(SPACING);
107   GroupConstructorsLayout->setMargin(MARGIN);
108
109   Constructor1 = new QRadioButton(GroupConstructors);
110   Constructor1->setIcon(image0);
111   Constructor1->setChecked(true);
112
113   GroupConstructorsLayout->addWidget(Constructor1);
114   ButtonGroup->addButton(Constructor1, 0);
115
116   /***************************************************************/
117   GroupMesh = new QGroupBox(tr("SMESH_RENUMBERING"), this);
118   QHBoxLayout* GroupMeshLayout = new QHBoxLayout(GroupMesh);
119   GroupMeshLayout->setMargin(MARGIN);
120   GroupMeshLayout->setSpacing(SPACING);
121
122   TextLabelMesh = new QLabel(tr("SMESH_MESH"), GroupMesh);
123   SelectButton = new QPushButton(GroupMesh);
124   SelectButton->setIcon(image1);
125   LineEditMesh = new QLineEdit(GroupMesh);
126   LineEditMesh->setReadOnly(true);
127
128   GroupMeshLayout->addWidget(TextLabelMesh);
129   GroupMeshLayout->addWidget(SelectButton);
130   GroupMeshLayout->addWidget(LineEditMesh);
131
132   /***************************************************************/
133   GroupButtons = new QGroupBox(this);
134   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
135   GroupButtonsLayout->setSpacing(SPACING);
136   GroupButtonsLayout->setMargin(MARGIN);
137
138   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
139   buttonOk->setAutoDefault(true);
140   buttonOk->setDefault(true);
141   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
142   buttonApply->setAutoDefault(true);
143   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
144   buttonCancel->setAutoDefault(true);
145   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
146   buttonHelp->setAutoDefault(true);
147
148   GroupButtonsLayout->addWidget(buttonOk);
149   GroupButtonsLayout->addSpacing(10);
150   GroupButtonsLayout->addWidget(buttonApply);
151   GroupButtonsLayout->addSpacing(10);
152   GroupButtonsLayout->addStretch();
153   GroupButtonsLayout->addWidget(buttonCancel);
154   GroupButtonsLayout->addWidget(buttonHelp);
155
156   /***************************************************************/
157   SMESHGUI_RenumberingDlgLayout->addWidget(GroupConstructors);
158   SMESHGUI_RenumberingDlgLayout->addWidget(GroupMesh);
159   SMESHGUI_RenumberingDlgLayout->addWidget(GroupButtons);
160
161   Init(); /* Initialisations */
162 }
163
164 //=================================================================================
165 // function : ~SMESHGUI_RenumberingDlg()
166 // purpose  : Destroys the object and frees any allocated resources
167 //=================================================================================
168 SMESHGUI_RenumberingDlg::~SMESHGUI_RenumberingDlg()
169 {
170 }
171
172 //=================================================================================
173 // function : Init()
174 // purpose  :
175 //=================================================================================
176 void SMESHGUI_RenumberingDlg::Init()
177 {
178   myConstructorId = 0;
179   Constructor1->setChecked(true);
180   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
181
182   myMesh = SMESH::SMESH_Mesh::_nil();
183
184   myMeshFilter = new SMESH_TypeFilter (MESH);
185
186   /* signals and slots connections */
187   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
188   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
189   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
190   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
191
192   connect(SelectButton, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
193   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
194   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
195   /* to close dialog if study change */
196   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
197
198   myEditCurrentArgument = LineEditMesh;
199   LineEditMesh->setFocus();
200   mySelectionMgr->clearFilters();
201   mySelectionMgr->installFilter(myMeshFilter);
202
203   SelectionIntoArgument();
204 }
205
206 //=================================================================================
207 // function : ClickOnApply()
208 // purpose  :
209 //=================================================================================
210 void SMESHGUI_RenumberingDlg::ClickOnApply()
211 {
212   if (mySMESHGUI->isActiveStudyLocked())
213     return;
214
215   if (!myMesh->_is_nil()) {
216     try {
217       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
218       SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
219       bool isUnitsLabeled = false;
220       
221       if (myUnit == 0 && anActor) {
222         isUnitsLabeled = anActor->GetPointsLabeled();
223         if (isUnitsLabeled)  anActor->SetPointsLabeled(false);
224       }
225       else if (myUnit == 1 && anActor) {
226         isUnitsLabeled = anActor->GetCellsLabeled();
227         if (isUnitsLabeled)  anActor->SetCellsLabeled(false);
228       }
229       
230       SUIT_OverrideCursor aWaitCursor;
231       if (myUnit == 0) {
232         aMeshEditor->RenumberNodes();
233         if (isUnitsLabeled && anActor) anActor->SetPointsLabeled(true);
234       }
235       else if (myUnit == 1) {
236         aMeshEditor->RenumberElements();
237         if (isUnitsLabeled && anActor) anActor->SetCellsLabeled(true);
238       }
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 }