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