Salome HOME
The selections of elements corrected.
[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, false);
359         myViewWindow->highlight(anIO,true,true);
360         
361         onSelectionDone();
362       }
363     }
364   }
365 }
366
367 //=======================================================================
368 // name    : SMESHGUI_MoveNodesDlg::onSelectionDone
369 // Purpose : SLOT called when selection changed
370 //=======================================================================
371 void SMESHGUI_MoveNodesDlg::onSelectionDone()
372 {
373   if (myBusy) return;
374   myMeshActor = 0;
375
376   SALOME_ListIO aList;
377   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
378
379   if (aList.Extent() == 1) {
380     Handle(SALOME_InteractiveObject) anIO = aList.First();
381     myMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
382     if(myMeshActor){
383       QString aText;
384       if (SMESH::GetNameOfSelectedNodes(mySelector,anIO,aText) == 1) {
385         if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()) {
386           if(const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
387             myBusy = true;
388             myId->setText(aText);
389             myX->SetValue(aNode->X());
390             myY->SetValue(aNode->Y());
391             myZ->SetValue(aNode->Z());
392             myBusy = false;
393             erasePreview(); // avoid overlapping of a selection and a preview
394             updateButtons();
395             return;
396           }
397         }
398       }
399     }
400   }
401
402   reset();
403 }
404
405 //=======================================================================
406 // name    : SMESHGUI_MoveNodesDlg::onDeactivate
407 // Purpose : SLOT called when dialog must be deativated
408 //=======================================================================
409 void SMESHGUI_MoveNodesDlg::onDeactivate()
410 {
411   setEnabled(false);
412   erasePreview();
413 }
414
415 //=======================================================================
416 // name    : SMESHGUI_MoveNodesDlg::enterEvent
417 // Purpose : Event filter
418 //=======================================================================
419 void SMESHGUI_MoveNodesDlg::enterEvent (QEvent*)
420 {
421   if (!isEnabled()) {
422     mySMESHGUI->EmitSignalDeactivateDialog();
423
424     // set selection mode
425     SMESH::SetPointRepresentation(true);
426     myViewWindow->SetSelectionMode(NodeSelection);
427
428     redisplayPreview();
429
430     setEnabled(true);
431   }
432 }
433
434 //=======================================================================
435 // name    : SMESHGUI_MoveNodesDlg::closeEvent
436 // Purpose :
437 //=======================================================================
438 void SMESHGUI_MoveNodesDlg::closeEvent (QCloseEvent*)
439 {
440   onClose();
441   myViewWindow->Repaint();
442 }
443
444 //=======================================================================
445 // name    : SMESHGUI_MoveNodesDlg::hideEvent
446 // Purpose : may be caused by ESC key
447 //=======================================================================
448 void SMESHGUI_MoveNodesDlg::hideEvent (QHideEvent*)
449 {
450   if (!isMinimized())
451     onClose();
452 }
453
454 //=======================================================================
455 // name    : SMESHGUI_MoveNodesDlg::updateButtons
456 // Purpose : Update buttons state
457 //=======================================================================
458 void SMESHGUI_MoveNodesDlg::updateButtons()
459 {
460   bool isEnabled = isValid(false);
461   myOkBtn->setEnabled(isEnabled);
462   myApplyBtn->setEnabled(isEnabled);
463 }
464
465 //=======================================================================
466 // name    : SMESHGUI_MoveNodesDlg::erasePreview
467 // Purpose : Erase preview
468 //=======================================================================
469 void  SMESHGUI_MoveNodesDlg::erasePreview()
470 {
471   if (myPreviewActor == 0)
472     return;
473
474   myViewWindow->RemoveActor(myPreviewActor);
475   myPreviewActor->Delete();
476   myPreviewActor = 0;
477   myViewWindow->Repaint();
478 }
479
480 //=======================================================================
481 // name    : SMESHGUI_MoveNodesDlg::redisplayPreview
482 // Purpose : Redisplay preview
483 //=======================================================================
484 void SMESHGUI_MoveNodesDlg::redisplayPreview()
485 {
486   if (myBusy)
487     return;
488
489   if (myPreviewActor != 0)
490     erasePreview();
491
492   if (!isValid(false))
493     return;
494
495   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
496
497   vtkPoints* aPoints = vtkPoints::New();
498   aPoints->SetNumberOfPoints(1);
499   aPoints->SetPoint(0, myX->GetValue(), myY->GetValue(), myZ->GetValue());
500
501   // Create cells
502
503   vtkIdList *anIdList = vtkIdList::New();
504   anIdList->SetNumberOfIds(1);
505
506   vtkCellArray *aCells = vtkCellArray::New();
507   aCells->Allocate(2, 0);
508
509   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
510   aCellTypesArray->SetNumberOfComponents(1);
511   aCellTypesArray->Allocate(1);
512
513   anIdList->SetId(0, 0);
514   aCells->InsertNextCell(anIdList);
515   aCellTypesArray->InsertNextValue(VTK_VERTEX);
516   anIdList->Delete();
517
518   vtkIntArray* aCellLocationsArray = vtkIntArray::New();
519   aCellLocationsArray->SetNumberOfComponents(1);
520   aCellLocationsArray->SetNumberOfTuples(1);
521
522   aCells->InitTraversal();
523   vtkIdType npts;
524   aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
525
526   aGrid->SetPoints(aPoints);
527   aPoints->Delete();
528
529   aGrid->SetCells(aCellTypesArray,aCellLocationsArray,aCells);
530   aCellLocationsArray->Delete();
531   aCellTypesArray->Delete();
532   aCells->Delete();
533
534   // Create and display actor
535   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
536   aMapper->SetInput(aGrid);
537   aGrid->Delete();
538
539   myPreviewActor = SALOME_Actor::New();
540   myPreviewActor->PickableOff();
541   myPreviewActor->SetMapper(aMapper);
542   aMapper->Delete();
543
544   vtkProperty* aProp = vtkProperty::New();
545   aProp->SetRepresentationToWireframe();
546   aProp->SetColor(250, 0, 250);
547   aProp->SetPointSize(5);
548   myPreviewActor->SetProperty(aProp);
549   aProp->Delete();
550
551   myViewWindow->AddActor(myPreviewActor);
552   myViewWindow->Repaint();
553 }