Salome HOME
Fix for bug PAL13900 : SMESH : "Move_Node" ergonomy.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MoveNodesDlg.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 //
23 //
24 //  File   : SMESHGUI_MoveNodesDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_MoveNodesDlg.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_SpinBox.h"
33 #include "SMESHGUI_IdValidator.h"
34 #include "SMESHGUI_Utils.h"
35 #include "SMESHGUI_VTKUtils.h"
36 #include "SMESHGUI_MeshUtils.h"
37
38 #include "SMESH_Actor.h"
39 #include "SMDS_Mesh.hxx"
40 #include "SMDS_MeshNode.hxx"
41
42 #include "LightApp_SelectionMgr.h"
43 #include "LightApp_Application.h"
44 #include "SUIT_ResourceMgr.h"
45 #include "SUIT_Desktop.h"
46 #include "SUIT_Session.h"
47 #include "SUIT_MessageBox.h"
48
49 #include "SVTK_Selector.h"
50 #include "SVTK_ViewModel.h"
51 #include "SVTK_ViewWindow.h"
52 #include "SALOME_ListIO.hxx"
53
54 #include "SVTK_ViewWindow.h"
55 #include "VTKViewer_CellLocationsArray.h"
56
57 #include "utilities.h"
58
59 // OCCT includes
60 #include <TColStd_MapOfInteger.hxx>
61
62 // VTK includes
63 #include <vtkCell.h>
64 #include <vtkIdList.h>
65 #include <vtkCellArray.h>
66 #include <vtkUnsignedCharArray.h>
67 #include <vtkUnstructuredGrid.h>
68 #include <vtkDataSetMapper.h>
69 #include <vtkProperty.h>
70
71 // QT includes
72 #include <qgroupbox.h>
73 #include <qlabel.h>
74 #include <qlineedit.h>
75 #include <qpushbutton.h>
76 #include <qradiobutton.h>
77 #include <qlayout.h>
78 #include <qpixmap.h>
79 #include <qmessagebox.h>
80 #include <qbuttongroup.h>
81
82 // IDL Headers
83 #include <SALOMEconfig.h>
84 #include CORBA_SERVER_HEADER(SMESH_Mesh)
85
86 #define MARGIN  10
87 #define SPACING 5
88
89
90 //=================================================================================
91 // name    : SMESHGUI_MoveNodesDlg::SMESHGUI_MoveNodesDlg
92 // Purpose :
93 //=================================================================================
94 SMESHGUI_MoveNodesDlg::SMESHGUI_MoveNodesDlg (SMESHGUI* theModule, 
95                                               const char* theName):
96   QDialog(SMESH::GetDesktop(theModule), 
97           theName, 
98           false,
99           WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
100   mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
101   mySMESHGUI(theModule)
102 {
103   myPreviewActor = 0;
104   myBusy = false;
105
106   setCaption(tr("CAPTION"));
107
108   QVBoxLayout* aDlgLay = new QVBoxLayout (this, MARGIN, SPACING);
109
110   QFrame* aMainFrame = createMainFrame  (this);
111   QFrame* aBtnFrame  = createButtonFrame(this);
112
113   aDlgLay->addWidget(aMainFrame);
114   aDlgLay->addWidget(aBtnFrame);
115
116   aDlgLay->setStretchFactor(aMainFrame, 1);
117
118   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
119
120   myHelpFileName = "/files/displacing_nodes.htm";
121
122   Init();
123 }
124
125 //=======================================================================
126 // name    : SMESHGUI_MoveNodesDlg::createButtonFrame
127 // Purpose : Create frame containing buttons
128 //=======================================================================
129 QFrame* SMESHGUI_MoveNodesDlg::createButtonFrame (QWidget* theParent)
130 {
131   QFrame* aFrame = new QFrame(theParent);
132   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
133
134   myOkBtn     = new QPushButton(tr("SMESH_BUT_OK"   ), aFrame);
135   myApplyBtn  = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
136   myCloseBtn  = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
137   myHelpBtn   = new QPushButton(tr("SMESH_BUT_HELP"), aFrame);
138
139   QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
140
141   QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
142
143   aLay->addWidget(myOkBtn);
144   aLay->addWidget(myApplyBtn);
145   aLay->addItem(aSpacer);
146   aLay->addWidget(myCloseBtn);
147   aLay->addWidget(myHelpBtn);
148
149   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
150   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
151   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
152   connect(myHelpBtn,  SIGNAL(clicked()), SLOT(onHelp()));
153
154   return aFrame;
155 }
156
157 //=======================================================================
158 // name    : SMESHGUI_MoveNodesDlg::createMainFrame
159 // Purpose : Create frame containing dialog's input fields
160 //=======================================================================
161 QFrame* SMESHGUI_MoveNodesDlg::createMainFrame (QWidget* theParent)
162 {
163   QFrame* aFrame = new QFrame(theParent);
164
165   QPixmap iconMoveNode (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_MOVE_NODE")));
166   QPixmap iconSelect   (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
167
168   QButtonGroup* aPixGrp = new QButtonGroup(1, Qt::Vertical, tr("MESH_NODE"), aFrame);
169   aPixGrp->setExclusive(TRUE);
170   QRadioButton* aRBut = new QRadioButton(aPixGrp);
171   aRBut->setPixmap(iconMoveNode);
172   aRBut->setChecked(TRUE);
173
174   QGroupBox* anIdGrp = new QGroupBox(1, Qt::Vertical, tr("SMESH_MOVE"), aFrame);
175   new QLabel(tr("NODE_ID"), anIdGrp);
176   (new QPushButton(anIdGrp))->setPixmap(iconSelect);
177   myId = new QLineEdit(anIdGrp);
178   myId->setValidator(new SMESHGUI_IdValidator(this, "validator", 1));
179
180   QGroupBox* aCoordGrp = new QGroupBox(1, Qt::Vertical, tr("SMESH_COORDINATES"), aFrame);
181   QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
182   aXLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
183   myX = new SMESHGUI_SpinBox(aCoordGrp);
184
185   QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
186   aYLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
187   myY = new SMESHGUI_SpinBox(aCoordGrp);
188
189   QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
190   aZLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
191   myZ = new SMESHGUI_SpinBox(aCoordGrp);
192
193   myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, 3);
194   myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, 3);
195   myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, 3);
196
197   QVBoxLayout* aLay = new QVBoxLayout(aFrame);
198   aLay->addWidget(aPixGrp);
199   aLay->addWidget(anIdGrp);
200   aLay->addWidget(aCoordGrp);
201
202   // connect signale and slots
203   connect(myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
204   connect(myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
205   connect(myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
206   connect(myId, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
207
208   return aFrame;
209 }
210
211 //=======================================================================
212 // name    : SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg
213 // Purpose :
214 //=======================================================================
215 SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg()
216 {
217   erasePreview();
218 }
219
220 //=======================================================================
221 // name    : SMESHGUI_MoveNodesDlg::Init
222 // Purpose : Init dialog fields
223 //=======================================================================
224 void SMESHGUI_MoveNodesDlg::Init()
225 {
226   myPreviewActor = 0;
227   myMeshActor = 0;
228   myBusy = false;
229
230   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
231
232   // selection and SMESHGUI
233   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
234   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
235   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
236
237   reset();
238   setEnabled(true);
239
240   this->show();
241
242   // set selection mode
243   SMESH::SetPointRepresentation(true);
244   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
245     aViewWindow->SetSelectionMode(NodeSelection);
246
247   onSelectionDone();
248 }
249
250 //=======================================================================
251 // name    : SMESHGUI_MoveNodesDlg::isValid
252 // Purpose : Verify validity of entry information
253 //=======================================================================
254 bool SMESHGUI_MoveNodesDlg::isValid (const bool theMess) const
255 {
256   if (myId->text().isEmpty()) {
257     if (theMess)
258       QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
259                                tr("NODE_ID_IS_NOT_DEFINED"), QMessageBox::Ok);
260     return false;
261   }
262   return true;
263 }
264
265 //=======================================================================
266 // name    : SMESHGUI_MoveNodesDlg::reset
267 // Purpose : Reset the dialog state
268 //=======================================================================
269 void SMESHGUI_MoveNodesDlg::reset()
270 {
271   myId->clear();
272   myX->SetValue(0);
273   myY->SetValue(0);
274   myZ->SetValue(0);
275   redisplayPreview();
276   updateButtons();
277 }
278
279 //=======================================================================
280 // name    : SMESHGUI_MoveNodesDlg::onApply
281 // Purpose : SLOT called when "Apply" button pressed.
282 //=======================================================================
283 bool SMESHGUI_MoveNodesDlg::onApply()
284 {
285   if (mySMESHGUI->isActiveStudyLocked())
286     return false;
287
288   if (!isValid(true))
289     return false;
290
291   SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
292   if (aMesh->_is_nil()) {
293     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
294                              tr("SMESHG_NO_MESH"), QMessageBox::Ok);
295     return false;
296   }
297
298   SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
299   if (aMeshEditor->_is_nil())
300     return false;
301
302   int anId = myId->text().toInt();
303   bool aResult = false;
304   try {
305     aResult = aMeshEditor->MoveNode(anId, myX->GetValue(), myY->GetValue(), myZ->GetValue());
306   } catch (...) {
307   }
308
309   if (aResult) {
310     SALOME_ListIO aList;
311     aList.Append(myMeshActor->getIO());
312     mySelectionMgr->setSelectedObjects(aList,false);
313     SMESH::UpdateView();
314     reset();
315   }
316
317   return aResult;
318 }
319
320 //=======================================================================
321 // name    : SMESHGUI_MoveNodesDlg::onOk
322 // Purpose : SLOT called when "Ok" button pressed.
323 //=======================================================================
324 void SMESHGUI_MoveNodesDlg::onOk()
325 {
326   if (onApply())
327     onClose();
328 }
329
330 //=======================================================================
331 // name    : SMESHGUI_MoveNodesDlg::onClose
332 // Purpose : SLOT called when "Close" button pressed. Close dialog
333 //=======================================================================
334 void SMESHGUI_MoveNodesDlg::onClose()
335 {
336   //mySelectionMgr->clearSelected();
337   SMESH::SetPointRepresentation(false);
338   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
339     aViewWindow->SetSelectionMode(ActorSelection);
340   disconnect(mySelectionMgr, 0, this, 0);
341   disconnect(mySMESHGUI, 0, this, 0);
342   erasePreview();
343   mySMESHGUI->ResetState();
344   reject();
345 }
346
347 //=================================================================================
348 // function : onHelp()
349 // purpose  :
350 //=================================================================================
351 void SMESHGUI_MoveNodesDlg::onHelp()
352 {
353   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
354   if (app) 
355     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
356   else {
357     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
358                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
359                            arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
360                            QObject::tr("BUT_OK"));
361   }
362 }
363
364 //=======================================================================
365 // name    : SMESHGUI_MoveNodesDlg::onTextChange
366 // Purpose :
367 //=======================================================================
368 void SMESHGUI_MoveNodesDlg::onTextChange (const QString& theNewText)
369 {
370   if (myBusy) return;
371
372   myOkBtn->setEnabled(false);
373   myApplyBtn->setEnabled(false);
374   erasePreview();
375
376   // select entered node
377   if(myMeshActor){
378     if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()){
379       myBusy = true;
380       Handle(SALOME_InteractiveObject) anIO = myMeshActor->getIO();
381       SALOME_ListIO aList;
382       aList.Append(anIO);
383       mySelectionMgr->setSelectedObjects(aList,false);
384       myBusy = false;
385
386       if(const SMDS_MeshElement *anElem = aMesh->FindElement(theNewText.toInt())) {
387         TColStd_MapOfInteger aListInd;
388         aListInd.Add(anElem->GetID());
389         mySelector->AddOrRemoveIndex(anIO,aListInd, false);
390         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
391           aViewWindow->highlight(anIO,true,true);
392         
393         onSelectionDone();
394       }
395     }
396   }
397 }
398
399 //=======================================================================
400 // name    : SMESHGUI_MoveNodesDlg::onSelectionDone
401 // Purpose : SLOT called when selection changed
402 //=======================================================================
403 void SMESHGUI_MoveNodesDlg::onSelectionDone()
404 {
405   if (myBusy) return;
406   myMeshActor = 0;
407
408   SALOME_ListIO aList;
409   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
410
411   if (aList.Extent() == 1) {
412     Handle(SALOME_InteractiveObject) anIO = aList.First();
413     myMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
414     if(myMeshActor){
415       QString aText;
416       if (SMESH::GetNameOfSelectedNodes(mySelector,anIO,aText) == 1) {
417         if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()) {
418           if(const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
419             myBusy = true;
420             myId->setText(aText);
421             myX->SetValue(aNode->X());
422             myY->SetValue(aNode->Y());
423             myZ->SetValue(aNode->Z());
424             myBusy = false;
425             erasePreview(); // avoid overlapping of a selection and a preview
426             updateButtons();
427             return;
428           }
429         }
430       }
431     }
432   }
433
434   reset();
435 }
436
437 //=======================================================================
438 // name    : SMESHGUI_MoveNodesDlg::onDeactivate
439 // Purpose : SLOT called when dialog must be deativated
440 //=======================================================================
441 void SMESHGUI_MoveNodesDlg::onDeactivate()
442 {
443   setEnabled(false);
444   erasePreview();
445 }
446
447 //=======================================================================
448 // name    : SMESHGUI_MoveNodesDlg::enterEvent
449 // Purpose : Event filter
450 //=======================================================================
451 void SMESHGUI_MoveNodesDlg::enterEvent (QEvent*)
452 {
453   if (!isEnabled()) {
454     mySMESHGUI->EmitSignalDeactivateDialog();
455
456     // set selection mode
457     SMESH::SetPointRepresentation(true);
458     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
459       aViewWindow->SetSelectionMode(NodeSelection);
460
461     redisplayPreview();
462
463     setEnabled(true);
464   }
465 }
466
467 //=======================================================================
468 // name    : SMESHGUI_MoveNodesDlg::closeEvent
469 // Purpose :
470 //=======================================================================
471 void SMESHGUI_MoveNodesDlg::closeEvent (QCloseEvent*)
472 {
473   onClose();
474   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
475     aViewWindow->Repaint();
476 }
477
478 //=======================================================================
479 // name    : SMESHGUI_MoveNodesDlg::hideEvent
480 // Purpose : may be caused by ESC key
481 //=======================================================================
482 void SMESHGUI_MoveNodesDlg::hideEvent (QHideEvent*)
483 {
484   if (!isMinimized())
485     onClose();
486 }
487
488 //=======================================================================
489 // name    : SMESHGUI_MoveNodesDlg::updateButtons
490 // Purpose : Update buttons state
491 //=======================================================================
492 void SMESHGUI_MoveNodesDlg::updateButtons()
493 {
494   bool isEnabled = isValid(false);
495   myOkBtn->setEnabled(isEnabled);
496   myApplyBtn->setEnabled(isEnabled);
497 }
498
499 //=======================================================================
500 // name    : SMESHGUI_MoveNodesDlg::erasePreview
501 // Purpose : Erase preview
502 //=======================================================================
503 void  SMESHGUI_MoveNodesDlg::erasePreview()
504 {
505   if (myPreviewActor == 0)
506     return;
507
508   SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
509   if (aViewWindow)
510     aViewWindow->RemoveActor(myPreviewActor);
511   myPreviewActor->Delete();
512   myPreviewActor = 0;
513   if (aViewWindow)
514     aViewWindow->Repaint();
515 }
516
517 //=======================================================================
518 // name    : SMESHGUI_MoveNodesDlg::redisplayPreview
519 // Purpose : Redisplay preview
520 //=======================================================================
521 void SMESHGUI_MoveNodesDlg::redisplayPreview()
522 {
523   if (myBusy)
524     return;
525
526   if (myPreviewActor != 0)
527     erasePreview();
528
529   if (!isValid(false))
530     return;
531
532   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
533
534   vtkPoints* aPoints = vtkPoints::New();
535   aPoints->SetNumberOfPoints(1);
536   aPoints->SetPoint(0, myX->GetValue(), myY->GetValue(), myZ->GetValue());
537
538   // Create cells
539
540   vtkIdList *anIdList = vtkIdList::New();
541   anIdList->SetNumberOfIds(1);
542
543   vtkCellArray *aCells = vtkCellArray::New();
544   aCells->Allocate(2, 0);
545
546   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
547   aCellTypesArray->SetNumberOfComponents(1);
548   aCellTypesArray->Allocate(1);
549
550   anIdList->SetId(0, 0);
551   aCells->InsertNextCell(anIdList);
552   aCellTypesArray->InsertNextValue(VTK_VERTEX);
553   anIdList->Delete();
554
555   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
556   aCellLocationsArray->SetNumberOfComponents(1);
557   aCellLocationsArray->SetNumberOfTuples(1);
558
559   aCells->InitTraversal();
560   vtkIdType npts;
561   aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
562
563   aGrid->SetPoints(aPoints);
564   aPoints->Delete();
565
566   aGrid->SetCells(aCellTypesArray,aCellLocationsArray,aCells);
567   aCellLocationsArray->Delete();
568   aCellTypesArray->Delete();
569   aCells->Delete();
570
571   // Create and display actor
572   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
573   aMapper->SetInput(aGrid);
574   aGrid->Delete();
575
576   myPreviewActor = SALOME_Actor::New();
577   myPreviewActor->PickableOff();
578   myPreviewActor->SetMapper(aMapper);
579   aMapper->Delete();
580
581   vtkProperty* aProp = vtkProperty::New();
582   aProp->SetRepresentationToWireframe();
583   aProp->SetColor(250, 0, 250);
584   aProp->SetPointSize(5);
585   myPreviewActor->SetProperty(aProp);
586   aProp->Delete();
587
588   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
589     {
590       aViewWindow->AddActor(myPreviewActor);
591       aViewWindow->Repaint();
592     }
593 }
594
595 //=================================================================================
596 // function : keyPressEvent()
597 // purpose  :
598 //=================================================================================
599 void SMESHGUI_MoveNodesDlg::keyPressEvent( QKeyEvent* e )
600 {
601   QDialog::keyPressEvent( e );
602   if ( e->isAccepted() )
603     return;
604
605   if ( e->key() == Key_F1 )
606     {
607       e->accept();
608       onHelp();
609     }
610 }