Salome HOME
Porting SMESH module to Qt 4
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_RemoveNodesDlg.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_RemoveNodesDlg.cxx
23 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
24 //
25
26 // SMESH includes
27 #include "SMESHGUI_RemoveNodesDlg.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_VTKUtils.h"
32 #include "SMESHGUI_MeshUtils.h"
33 #include "SMESHGUI_IdValidator.h"
34
35 #include <SMESH_Actor.h>
36 #include <SMDS_Mesh.hxx>
37
38 // SALOME GUI includes
39 #include <SUIT_ResourceMgr.h>
40 #include <SUIT_Desktop.h>
41 #include <SUIT_Session.h>
42 #include <SUIT_MessageBox.h>
43
44 #include <LightApp_Application.h>
45 #include <LightApp_SelectionMgr.h>
46 #include <SalomeApp_Tools.h>
47
48 #include <SVTK_Selector.h>
49 #include <SVTK_ViewModel.h>
50 #include <SVTK_ViewWindow.h>
51 #include <SALOME_ListIO.hxx>
52
53 // OCCT includes
54 #include <TColStd_MapOfInteger.hxx>
55
56 // Qt includes
57 #include <QGroupBox>
58 #include <QLabel>
59 #include <QLineEdit>
60 #include <QPushButton>
61 #include <QRadioButton>
62 #include <QVBoxLayout>
63 #include <QHBoxLayout>
64 #include <QKeyEvent>
65
66 // IDL includes
67 #include <SALOMEconfig.h>
68 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
69
70 #define SPACING 6
71 #define MARGIN  11
72
73 //=================================================================================
74 // class    : SMESHGUI_RemoveNodesDlg()
75 // purpose  :
76 //=================================================================================
77 SMESHGUI_RemoveNodesDlg
78 ::SMESHGUI_RemoveNodesDlg(SMESHGUI* theModule)
79   : QDialog(SMESH::GetDesktop(theModule)),
80     mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
81     mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
82     mySMESHGUI(theModule),
83     myBusy(false)
84 {
85   setModal( false );
86   setAttribute( Qt::WA_DeleteOnClose, true );
87   setWindowTitle(tr("SMESH_REMOVE_NODES_TITLE"));
88   setSizeGripEnabled(true);
89   
90   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_REM_NODE")));
91   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
92
93   QVBoxLayout* SMESHGUI_RemoveNodesDlgLayout = new QVBoxLayout(this);
94   SMESHGUI_RemoveNodesDlgLayout->setSpacing(SPACING);
95   SMESHGUI_RemoveNodesDlgLayout->setMargin(MARGIN);
96
97   /***************************************************************/
98   GroupConstructors = new QGroupBox(tr("SMESH_NODES"), this);
99   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
100   GroupConstructorsLayout->setSpacing(SPACING);
101   GroupConstructorsLayout->setMargin(MARGIN);
102
103   Constructor1 = new QRadioButton(GroupConstructors);
104   Constructor1->setIcon(image0);
105   Constructor1->setChecked(true);
106
107   GroupConstructorsLayout->addWidget(Constructor1);
108   GroupConstructorsLayout->addStretch();
109
110   /***************************************************************/
111   GroupC1 = new QGroupBox(tr("SMESH_REMOVE"), this);
112   QHBoxLayout* GroupC1Layout = new QHBoxLayout(GroupC1);
113   GroupC1Layout->setSpacing(SPACING);
114   GroupC1Layout->setMargin(MARGIN);
115
116   TextLabelC1A1 = new QLabel(tr("SMESH_ID_NODES"), GroupC1);
117   SelectButtonC1A1 = new QPushButton(GroupC1);
118   SelectButtonC1A1->setIcon(image1);
119   LineEditC1A1 = new QLineEdit(GroupC1);
120   LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this));
121
122   GroupC1Layout->addWidget(TextLabelC1A1);
123   GroupC1Layout->addWidget(SelectButtonC1A1);
124   GroupC1Layout->addWidget(LineEditC1A1);
125
126   /***************************************************************/
127   GroupButtons = new QGroupBox(this);
128   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
129   GroupButtonsLayout->setSpacing(SPACING);
130   GroupButtonsLayout->setMargin(MARGIN);
131
132   buttonOk = new QPushButton(tr("SMESH_BUT_OK"), GroupButtons);
133   buttonOk->setAutoDefault(true);
134   buttonOk->setDefault(true);
135   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
136   buttonApply->setAutoDefault(true);
137   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
138   buttonCancel->setAutoDefault(true);
139   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
140   buttonHelp->setAutoDefault(true);
141
142   GroupButtonsLayout->addWidget(buttonOk);
143   GroupButtonsLayout->addSpacing(10);
144   GroupButtonsLayout->addWidget(buttonApply);
145   GroupButtonsLayout->addSpacing(10);
146   GroupButtonsLayout->addStretch();
147   GroupButtonsLayout->addWidget(buttonCancel);
148   GroupButtonsLayout->addWidget(buttonHelp);
149
150   /***************************************************************/
151   SMESHGUI_RemoveNodesDlgLayout->addWidget(GroupConstructors);
152   SMESHGUI_RemoveNodesDlgLayout->addWidget(GroupC1);
153   SMESHGUI_RemoveNodesDlgLayout->addWidget(GroupButtons);
154
155   myHelpFileName = "removing_nodes_and_elements_page.html#removing_nodes_anchor";
156
157   Init(); /* Initialisations */
158 }
159
160 //=================================================================================
161 // function : ~SMESHGUI_RemoveNodesDlg()
162 // purpose  : Destroys the object and frees any allocated resources
163 //=================================================================================
164 SMESHGUI_RemoveNodesDlg::~SMESHGUI_RemoveNodesDlg()
165 {
166 }
167
168 //=================================================================================
169 // function : Init()
170 // purpose  :
171 //=================================================================================
172 void SMESHGUI_RemoveNodesDlg::Init()
173 {
174   myConstructorId = 0;
175   Constructor1->setChecked(true);
176   myEditCurrentArgument = LineEditC1A1;
177
178   myNbOkNodes = 0;
179   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
180   myActor = 0;
181   myBusy = false;
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(SelectButtonC1A1, 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   connect(myEditCurrentArgument, SIGNAL(textChanged(const QString&)),
195           SLOT(onTextChange(const QString&)));
196   
197   SMESH::SetPointRepresentation(true);
198   
199   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
200     aViewWindow->SetSelectionMode(NodeSelection);
201
202   SelectionIntoArgument();
203 }
204
205 //=================================================================================
206 // function : ClickOnApply()
207 // purpose  :
208 //=================================================================================
209 void SMESHGUI_RemoveNodesDlg::ClickOnApply()
210 {
211   if (mySMESHGUI->isActiveStudyLocked())
212     return;
213
214   if (myNbOkNodes) {
215     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
216     SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
217     anArrayOfIdeces->length(aListId.count());
218     for (int i = 0; i < aListId.count(); i++)
219       anArrayOfIdeces[i] = aListId[ i ].toInt();
220
221     bool aResult = false;
222     try {
223       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
224       aResult = aMeshEditor->RemoveNodes(anArrayOfIdeces.inout());
225     } catch (const SALOME::SALOME_Exception& S_ex) {
226       SalomeApp_Tools::QtCatchCorbaException(S_ex);
227       myEditCurrentArgument->clear();
228     } catch (...){
229       myEditCurrentArgument->clear();
230     }
231
232     if (aResult) {
233       myEditCurrentArgument->clear();
234       mySelector->ClearIndex();
235       SMESH::UpdateView();
236     }
237
238     SMESH::SetPointRepresentation(true);
239   }
240 }
241
242 //=================================================================================
243 // function : ClickOnOk()
244 // purpose  :
245 //=================================================================================
246 void SMESHGUI_RemoveNodesDlg::ClickOnOk()
247 {
248   ClickOnApply();
249   ClickOnCancel();
250 }
251
252 //=================================================================================
253 // function : ClickOnCancel()
254 // purpose  :
255 //=================================================================================
256 void SMESHGUI_RemoveNodesDlg::ClickOnCancel()
257 {
258   //mySelectionMgr->clearSelected();
259   SMESH::SetPointRepresentation(false);
260   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
261     aViewWindow->SetSelectionMode(ActorSelection);
262   disconnect(mySelectionMgr, 0, this, 0);
263   mySMESHGUI->ResetState();
264   reject();
265 }
266
267 //=================================================================================
268 // function : ClickOnHelp()
269 // purpose  :
270 //=================================================================================
271 void SMESHGUI_RemoveNodesDlg::ClickOnHelp()
272 {
273   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
274   if (app) 
275     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
276   else {
277     QString platform;
278 #ifdef WIN32
279     platform = "winapplication";
280 #else
281     platform = "application";
282 #endif
283     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
284                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
285                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
286                                                                  platform)).
287                              arg(myHelpFileName));
288   }
289 }
290
291 //=======================================================================
292 //function : onTextChange
293 //purpose  :
294 //=======================================================================
295 void SMESHGUI_RemoveNodesDlg::onTextChange(const QString& theNewText)
296 {
297   if (myBusy) return;
298   myBusy = true;
299
300   myNbOkNodes = 0;
301
302   buttonOk->setEnabled(false);
303   buttonApply->setEnabled(false);
304
305   // hilight entered nodes
306   if(myActor){
307     if(SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh()){
308       Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
309            
310       TColStd_MapOfInteger newIndices;
311       
312       QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
313       for (int i = 0; i < aListId.count(); i++) {
314         if (const SMDS_MeshNode *aNode = aMesh->FindNode(aListId[i].toInt())) {
315           newIndices.Add(aNode->GetID());
316           myNbOkNodes++;
317         }
318       }
319
320       mySelector->AddOrRemoveIndex(anIO,newIndices,false);
321       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
322         aViewWindow->highlight(anIO,true,true);
323     }
324   }
325
326   if (myNbOkNodes) {
327     buttonOk->setEnabled(true);
328     buttonApply->setEnabled(true);
329   }
330
331   myBusy = false;
332 }
333
334 //=================================================================================
335 // function : SelectionIntoArgument()
336 // purpose  : Called when selection as changed or other case
337 //=================================================================================
338 void SMESHGUI_RemoveNodesDlg::SelectionIntoArgument()
339 {
340   if (myBusy) return;
341
342   // clear
343
344   myNbOkNodes = false;
345   myActor = 0;
346
347   myBusy = true;
348   myEditCurrentArgument->setText("");
349   myBusy = false;
350
351   if (!GroupButtons->isEnabled()) // inactive
352     return;
353
354   buttonOk->setEnabled(false);
355   buttonApply->setEnabled(false);
356
357   // get selected mesh
358   SALOME_ListIO aList;
359   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
360
361   int nbSel = aList.Extent();
362   if (nbSel != 1)
363     return;
364
365   Handle(SALOME_InteractiveObject) anIO = aList.First();
366   myMesh = SMESH::GetMeshByIO(anIO);
367   if (myMesh->_is_nil())
368     return;
369
370   myActor = SMESH::FindActorByEntry(anIO->getEntry());
371   if (!myActor)
372     return;
373
374   // get selected nodes
375
376   QString aString = "";
377   int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,anIO,aString);
378   if(nbNodes < 1)
379     return;
380   myBusy = true;
381   myEditCurrentArgument->setText(aString);
382   myBusy = false;
383
384   // OK
385
386   myNbOkNodes = true;
387
388   buttonOk->setEnabled(true);
389   buttonApply->setEnabled(true);
390 }
391
392 //=================================================================================
393 // function : SetEditCurrentArgument()
394 // purpose  :
395 //=================================================================================
396 void SMESHGUI_RemoveNodesDlg::SetEditCurrentArgument()
397 {
398   QPushButton* send = (QPushButton*)sender();
399   switch (myConstructorId) {
400   case 0: /* default constructor */
401     {
402       if(send == SelectButtonC1A1) {
403         LineEditC1A1->setFocus();
404         myEditCurrentArgument = LineEditC1A1;
405       }
406       SelectionIntoArgument();
407       break;
408     }
409   }
410 }
411
412 //=================================================================================
413 // function : DeactivateActiveDialog()
414 // purpose  :
415 //=================================================================================
416 void SMESHGUI_RemoveNodesDlg::DeactivateActiveDialog()
417 {
418   if (GroupConstructors->isEnabled()) {
419     GroupConstructors->setEnabled(false);
420     GroupC1->setEnabled(false);
421     GroupButtons->setEnabled(false);
422     mySMESHGUI->ResetState(); // ??
423     mySMESHGUI->SetActiveDialogBox(0); // ??
424   }
425 }
426
427 //=================================================================================
428 // function : ActivateThisDialog()
429 // purpose  :
430 //=================================================================================
431 void SMESHGUI_RemoveNodesDlg::ActivateThisDialog()
432 {
433   /* Emit a signal to deactivate the active dialog */
434   mySMESHGUI->EmitSignalDeactivateDialog();
435
436   GroupConstructors->setEnabled(true);
437   GroupC1->setEnabled(true);
438   GroupButtons->setEnabled(true);
439
440   mySMESHGUI->SetActiveDialogBox((QDialog*)this); // ??
441
442   SMESH::SetPointRepresentation(true);
443   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
444     aViewWindow->SetSelectionMode(NodeSelection);
445
446   SelectionIntoArgument(); // ??
447 }
448
449 //=================================================================================
450 // function : enterEvent()
451 // purpose  :
452 //=================================================================================
453 void SMESHGUI_RemoveNodesDlg::enterEvent(QEvent*)
454 {
455   if (!GroupConstructors->isEnabled())
456     ActivateThisDialog();
457 }
458
459 //=================================================================================
460 // function : closeEvent()
461 // purpose  :
462 //=================================================================================
463 void SMESHGUI_RemoveNodesDlg::closeEvent(QCloseEvent*)
464 {
465   /* same than click on cancel button */
466   ClickOnCancel();
467 }
468
469 //=======================================================================
470 //function : hideEvent
471 //purpose  : caused by ESC key
472 //=======================================================================
473 void SMESHGUI_RemoveNodesDlg::hideEvent( QHideEvent* )
474 {
475   if (!isMinimized())
476     ClickOnCancel();
477 }
478
479 //=================================================================================
480 // function : keyPressEvent()
481 // purpose  :
482 //=================================================================================
483 void SMESHGUI_RemoveNodesDlg::keyPressEvent( QKeyEvent* e )
484 {
485   QDialog::keyPressEvent( e );
486   if ( e->isAccepted() )
487     return;
488
489   if ( e->key() == Qt::Key_F1 ) {
490     e->accept();
491     ClickOnHelp();
492   }
493 }