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