Salome HOME
Merge V9_dev branch into master
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_RenumberingDlg.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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.html#renumbering-nodes-anchor" :
103     "renumbering_nodes_and_elements.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 (SMESH::MESH);
186
187   /* signals and slots connections */
188   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
189   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
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(reject()));
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 (SMESHGUI::isStudyLocked())
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     SMESHGUI::Modified();
247   }
248 }
249
250 //=================================================================================
251 // function : ClickOnOk()
252 // purpose  :
253 //=================================================================================
254 void SMESHGUI_RenumberingDlg::ClickOnOk()
255 {
256   ClickOnApply();
257   reject();
258 }
259
260 //=================================================================================
261 // function : reject()
262 // purpose  :
263 //=================================================================================
264 void SMESHGUI_RenumberingDlg::reject()
265 {
266   //mySelectionMgr->clearSelected();
267   mySelectionMgr->clearFilters();
268   disconnect(mySelectionMgr, 0, this, 0);
269   mySMESHGUI->ResetState();
270   QDialog::reject();
271 }
272
273 //=================================================================================
274 // function : ClickOnHelp()
275 // purpose  :
276 //=================================================================================
277 void SMESHGUI_RenumberingDlg::ClickOnHelp()
278 {
279   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
280   if (app) 
281     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
282   else {
283     QString platform;
284 #ifdef WIN32
285     platform = "winapplication";
286 #else
287     platform = "application";
288 #endif
289     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
290                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
291                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
292                                                                  platform)).
293                              arg(myHelpFileName));
294   }
295 }
296
297 //=================================================================================
298 // function : SelectionIntoArgument()
299 // purpose  : Called when selection as changed or other case
300 //=================================================================================
301 void SMESHGUI_RenumberingDlg::SelectionIntoArgument()
302 {
303   if (!GroupButtons->isEnabled()) // inactive
304     return;
305
306   QString aString = "";
307
308   SALOME_ListIO aList;
309   mySelectionMgr->selectedObjects(aList);
310   int nbSel = SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aString);
311
312   if (myEditCurrentArgument == LineEditMesh) {
313     // mesh
314     if (nbSel != 1) {
315       myMesh = SMESH::SMESH_Mesh::_nil();
316       aString = "";
317     } else {
318       Handle(SALOME_InteractiveObject) IO = aList.First();
319       myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO);
320       if (myMesh->_is_nil())
321         aString = "";
322     }
323   }
324
325   myEditCurrentArgument->setText(aString);
326
327   bool isEnabled = (!myMesh->_is_nil());
328   buttonOk->setEnabled(isEnabled);
329   buttonApply->setEnabled(isEnabled);
330 }
331
332 //=================================================================================
333 // function : SetEditCurrentArgument()
334 // purpose  :
335 //=================================================================================
336 void SMESHGUI_RenumberingDlg::SetEditCurrentArgument()
337 {
338   QPushButton* send = (QPushButton*)sender();
339   switch (myConstructorId)
340     {
341     case 0: /* default constructor */
342       {
343         if(send == SelectButton) {
344           LineEditMesh->setFocus();
345           myEditCurrentArgument = LineEditMesh;
346         }
347         SelectionIntoArgument();
348         break;
349       }
350     }
351 }
352
353 //=================================================================================
354 // function : DeactivateActiveDialog()
355 // purpose  :
356 //=================================================================================
357 void SMESHGUI_RenumberingDlg::DeactivateActiveDialog()
358 {
359   if (GroupConstructors->isEnabled()) {
360     GroupConstructors->setEnabled(false);
361     GroupMesh->setEnabled(false);
362     GroupButtons->setEnabled(false);
363     mySMESHGUI->ResetState();
364     mySMESHGUI->SetActiveDialogBox(0);
365   }
366 }
367
368 //=================================================================================
369 // function : ActivateThisDialog()
370 // purpose  :
371 //=================================================================================
372 void SMESHGUI_RenumberingDlg::ActivateThisDialog()
373 {
374   /* Emit a signal to deactivate the active dialog */
375   mySMESHGUI->EmitSignalDeactivateDialog();
376   GroupConstructors->setEnabled(true);
377   GroupMesh->setEnabled(true);
378   GroupButtons->setEnabled(true);
379
380   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
381   SelectionIntoArgument();
382 }
383
384 //=================================================================================
385 // function : enterEvent()
386 // purpose  :
387 //=================================================================================
388 void SMESHGUI_RenumberingDlg::enterEvent(QEvent* e)
389 {
390   if (GroupConstructors->isEnabled())
391     return;
392   ActivateThisDialog();
393 }
394
395 //=================================================================================
396 // function : keyPressEvent()
397 // purpose  :
398 //=================================================================================
399 void SMESHGUI_RenumberingDlg::keyPressEvent( QKeyEvent* e )
400 {
401   QDialog::keyPressEvent( e );
402   if ( e->isAccepted() )
403     return;
404
405   if ( e->key() == Qt::Key_F1 ) {
406     e->accept();
407     ClickOnHelp();
408   }
409 }