Salome HOME
*** empty log message ***
[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   mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
97   mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
98   myViewWindow(SMESH::GetViewWindow(theModule)),
99   mySMESHGUI(theModule)
100 {
101   myPreviewActor = 0;
102   myBusy = false;
103
104   setCaption(tr("CAPTION"));
105
106   QVBoxLayout* aDlgLay = new QVBoxLayout (this, MARGIN, SPACING);
107
108   QFrame* aMainFrame = createMainFrame  (this);
109   QFrame* aBtnFrame  = createButtonFrame(this);
110
111   aDlgLay->addWidget(aMainFrame);
112   aDlgLay->addWidget(aBtnFrame);
113
114   aDlgLay->setStretchFactor(aMainFrame, 1);
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   myViewWindow->SetSelectionMode(NodeSelection);
234
235   onSelectionDone();
236 }
237
238 //=======================================================================
239 // name    : SMESHGUI_MoveNodesDlg::isValid
240 // Purpose : Verify validity of entry information
241 //=======================================================================
242 bool SMESHGUI_MoveNodesDlg::isValid (const bool theMess) const
243 {
244   if (myId->text().isEmpty()) {
245     if (theMess)
246       QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
247                                tr("NODE_ID_IS_NOT_DEFINED"), QMessageBox::Ok);
248     return false;
249   }
250   return true;
251 }
252
253 //=======================================================================
254 // name    : SMESHGUI_MoveNodesDlg::reset
255 // Purpose : Reset the dialog state
256 //=======================================================================
257 void SMESHGUI_MoveNodesDlg::reset()
258 {
259   myId->clear();
260   myX->SetValue(0);
261   myY->SetValue(0);
262   myZ->SetValue(0);
263   redisplayPreview();
264   updateButtons();
265 }
266
267 //=======================================================================
268 // name    : SMESHGUI_MoveNodesDlg::onApply
269 // Purpose : SLOT called when "Apply" button pressed.
270 //=======================================================================
271 bool SMESHGUI_MoveNodesDlg::onApply()
272 {
273   if (mySMESHGUI->isActiveStudyLocked())
274     return false;
275
276   if (!isValid(true))
277     return false;
278
279   SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
280   if (aMesh->_is_nil()) {
281     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
282                              tr("SMESHG_NO_MESH"), QMessageBox::Ok);
283     return false;
284   }
285
286   SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
287   if (aMeshEditor->_is_nil())
288     return false;
289
290   int anId = myId->text().toInt();
291   bool aResult = false;
292   try {
293     aResult = aMeshEditor->MoveNode(anId, myX->GetValue(), myY->GetValue(), myZ->GetValue());
294   } catch (...) {
295   }
296
297   if (aResult) {
298     SALOME_ListIO aList;
299     aList.Append(myMeshActor->getIO());
300     mySelectionMgr->setSelectedObjects(aList,false);
301     SMESH::UpdateView();
302     reset();
303   }
304
305   return aResult;
306 }
307
308 //=======================================================================
309 // name    : SMESHGUI_MoveNodesDlg::onOk
310 // Purpose : SLOT called when "Ok" button pressed.
311 //=======================================================================
312 void SMESHGUI_MoveNodesDlg::onOk()
313 {
314   if (onApply())
315     onClose();
316 }
317
318 //=======================================================================
319 // name    : SMESHGUI_MoveNodesDlg::onClose
320 // Purpose : SLOT called when "Close" button pressed. Close dialog
321 //=======================================================================
322 void SMESHGUI_MoveNodesDlg::onClose()
323 {
324   mySelectionMgr->clearSelected();
325   SMESH::SetPointRepresentation(false);
326   myViewWindow->SetSelectionMode(ActorSelection);
327   disconnect(mySelectionMgr, 0, this, 0);
328   disconnect(mySMESHGUI, 0, this, 0);
329   mySMESHGUI->ResetState();
330   reject();
331 }
332
333 //=======================================================================
334 // name    : SMESHGUI_MoveNodesDlg::onTextChange
335 // Purpose :
336 //=======================================================================
337 void SMESHGUI_MoveNodesDlg::onTextChange (const QString& theNewText)
338 {
339   if (myBusy) return;
340
341   myOkBtn->setEnabled(false);
342   myApplyBtn->setEnabled(false);
343   erasePreview();
344
345   // select entered node
346   if(myMeshActor){
347     if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()){
348       myBusy = true;
349       Handle(SALOME_InteractiveObject) anIO = myMeshActor->getIO();
350       SALOME_ListIO aList;
351       aList.Append(anIO);
352       mySelectionMgr->setSelectedObjects(aList,false);
353       myBusy = false;
354
355       if(const SMDS_MeshElement *anElem = aMesh->FindElement(theNewText.toInt())) {
356         TColStd_MapOfInteger aListInd;
357         aListInd.Add(anElem->GetID());
358         mySelector->AddOrRemoveIndex(anIO,aListInd, true);
359         myViewWindow->highlight(anIO,true,true);
360       }
361     }
362   }
363 }
364
365 //=======================================================================
366 // name    : SMESHGUI_MoveNodesDlg::onSelectionDone
367 // Purpose : SLOT called when selection changed
368 //=======================================================================
369 void SMESHGUI_MoveNodesDlg::onSelectionDone()
370 {
371   if (myBusy) return;
372   myMeshActor = 0;
373
374   SALOME_ListIO aList;
375   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
376
377   if (aList.Extent() == 1) {
378     Handle(SALOME_InteractiveObject) anIO = aList.First();
379     myMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
380     if(myMeshActor){
381       QString aText;
382       if (SMESH::GetNameOfSelectedNodes(mySelector,anIO,aText) == 1) {
383         if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()) {
384           if(const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
385             myBusy = true;
386             myId->setText(aText);
387             myX->SetValue(aNode->X());
388             myY->SetValue(aNode->Y());
389             myZ->SetValue(aNode->Z());
390             myBusy = false;
391             erasePreview(); // avoid overlapping of a selection and a preview
392             updateButtons();
393             return;
394           }
395         }
396       }
397     }
398   }
399
400   reset();
401 }
402
403 //=======================================================================
404 // name    : SMESHGUI_MoveNodesDlg::onDeactivate
405 // Purpose : SLOT called when dialog must be deativated
406 //=======================================================================
407 void SMESHGUI_MoveNodesDlg::onDeactivate()
408 {
409   setEnabled(false);
410   erasePreview();
411 }
412
413 //=======================================================================
414 // name    : SMESHGUI_MoveNodesDlg::enterEvent
415 // Purpose : Event filter
416 //=======================================================================
417 void SMESHGUI_MoveNodesDlg::enterEvent (QEvent*)
418 {
419   if (!isEnabled()) {
420     mySMESHGUI->EmitSignalDeactivateDialog();
421
422     // set selection mode
423     SMESH::SetPointRepresentation(true);
424     myViewWindow->SetSelectionMode(NodeSelection);
425
426     redisplayPreview();
427
428     setEnabled(true);
429   }
430 }
431
432 //=======================================================================
433 // name    : SMESHGUI_MoveNodesDlg::closeEvent
434 // Purpose :
435 //=======================================================================
436 void SMESHGUI_MoveNodesDlg::closeEvent (QCloseEvent*)
437 {
438   onClose();
439   myViewWindow->Repaint();
440 }
441
442 //=======================================================================
443 // name    : SMESHGUI_MoveNodesDlg::hideEvent
444 // Purpose : may be caused by ESC key
445 //=======================================================================
446 void SMESHGUI_MoveNodesDlg::hideEvent (QHideEvent*)
447 {
448   if (!isMinimized())
449     onClose();
450 }
451
452 //=======================================================================
453 // name    : SMESHGUI_MoveNodesDlg::updateButtons
454 // Purpose : Update buttons state
455 //=======================================================================
456 void SMESHGUI_MoveNodesDlg::updateButtons()
457 {
458   bool isEnabled = isValid(false);
459   myOkBtn->setEnabled(isEnabled);
460   myApplyBtn->setEnabled(isEnabled);
461 }
462
463 //=======================================================================
464 // name    : SMESHGUI_MoveNodesDlg::erasePreview
465 // Purpose : Erase preview
466 //=======================================================================
467 void  SMESHGUI_MoveNodesDlg::erasePreview()
468 {
469   if (myPreviewActor == 0)
470     return;
471
472   myViewWindow->RemoveActor(myPreviewActor);
473   myPreviewActor->Delete();
474   myPreviewActor = 0;
475   myViewWindow->Repaint();
476 }
477
478 //=======================================================================
479 // name    : SMESHGUI_MoveNodesDlg::redisplayPreview
480 // Purpose : Redisplay preview
481 //=======================================================================
482 void SMESHGUI_MoveNodesDlg::redisplayPreview()
483 {
484   if (myBusy)
485     return;
486
487   if (myPreviewActor != 0)
488     erasePreview();
489
490   if (!isValid(false))
491     return;
492
493   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
494
495   vtkPoints* aPoints = vtkPoints::New();
496   aPoints->SetNumberOfPoints(1);
497   aPoints->SetPoint(0, myX->GetValue(), myY->GetValue(), myZ->GetValue());
498
499   // Create cells
500
501   vtkIdList *anIdList = vtkIdList::New();
502   anIdList->SetNumberOfIds(1);
503
504   vtkCellArray *aCells = vtkCellArray::New();
505   aCells->Allocate(2, 0);
506
507   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
508   aCellTypesArray->SetNumberOfComponents(1);
509   aCellTypesArray->Allocate(1);
510
511   anIdList->SetId(0, 0);
512   aCells->InsertNextCell(anIdList);
513   aCellTypesArray->InsertNextValue(VTK_VERTEX);
514   anIdList->Delete();
515
516   vtkIntArray* aCellLocationsArray = vtkIntArray::New();
517   aCellLocationsArray->SetNumberOfComponents(1);
518   aCellLocationsArray->SetNumberOfTuples(1);
519
520   aCells->InitTraversal();
521   vtkIdType npts;
522   aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
523
524   aGrid->SetPoints(aPoints);
525   aPoints->Delete();
526
527   aGrid->SetCells(aCellTypesArray,aCellLocationsArray,aCells);
528   aCellLocationsArray->Delete();
529   aCellTypesArray->Delete();
530   aCells->Delete();
531
532   // Create and display actor
533   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
534   aMapper->SetInput(aGrid);
535   aGrid->Delete();
536
537   myPreviewActor = SALOME_Actor::New();
538   myPreviewActor->PickableOff();
539   myPreviewActor->SetMapper(aMapper);
540   aMapper->Delete();
541
542   vtkProperty* aProp = vtkProperty::New();
543   aProp->SetRepresentationToWireframe();
544   aProp->SetColor(250, 0, 250);
545   aProp->SetPointSize(5);
546   myPreviewActor->SetProperty(aProp);
547   aProp->Delete();
548
549   myViewWindow->AddActor(myPreviewActor);
550   myViewWindow->Repaint();
551 }