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