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