Salome HOME
Merging with WPdev
[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   new QLabel(tr("SMESH_X"), aCoordGrp);
182   myX = new SMESHGUI_SpinBox(aCoordGrp);
183   new QLabel(tr("SMESH_Y"), aCoordGrp);
184   myY = new SMESHGUI_SpinBox(aCoordGrp);
185   new QLabel(tr("SMESH_Z"), aCoordGrp);
186   myZ = new SMESHGUI_SpinBox(aCoordGrp);
187
188   myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, 3);
189   myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, 3);
190   myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, 3);
191
192   QVBoxLayout* aLay = new QVBoxLayout(aFrame);
193   aLay->addWidget(aPixGrp);
194   aLay->addWidget(anIdGrp);
195   aLay->addWidget(aCoordGrp);
196
197   // connect signale and slots
198   connect(myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
199   connect(myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
200   connect(myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
201   connect(myId, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
202
203   return aFrame;
204 }
205
206 //=======================================================================
207 // name    : SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg
208 // Purpose :
209 //=======================================================================
210 SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg()
211 {
212   erasePreview();
213 }
214
215 //=======================================================================
216 // name    : SMESHGUI_MoveNodesDlg::Init
217 // Purpose : Init dialog fields
218 //=======================================================================
219 void SMESHGUI_MoveNodesDlg::Init()
220 {
221   myPreviewActor = 0;
222   myMeshActor = 0;
223   myBusy = false;
224
225   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
226
227   // selection and SMESHGUI
228   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
229   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
230   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
231
232   reset();
233   setEnabled(true);
234
235   this->show();
236
237   // set selection mode
238   SMESH::SetPointRepresentation(true);
239   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
240     aViewWindow->SetSelectionMode(NodeSelection);
241
242   onSelectionDone();
243 }
244
245 //=======================================================================
246 // name    : SMESHGUI_MoveNodesDlg::isValid
247 // Purpose : Verify validity of entry information
248 //=======================================================================
249 bool SMESHGUI_MoveNodesDlg::isValid (const bool theMess) const
250 {
251   if (myId->text().isEmpty()) {
252     if (theMess)
253       QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
254                                tr("NODE_ID_IS_NOT_DEFINED"), QMessageBox::Ok);
255     return false;
256   }
257   return true;
258 }
259
260 //=======================================================================
261 // name    : SMESHGUI_MoveNodesDlg::reset
262 // Purpose : Reset the dialog state
263 //=======================================================================
264 void SMESHGUI_MoveNodesDlg::reset()
265 {
266   myId->clear();
267   myX->SetValue(0);
268   myY->SetValue(0);
269   myZ->SetValue(0);
270   redisplayPreview();
271   updateButtons();
272 }
273
274 //=======================================================================
275 // name    : SMESHGUI_MoveNodesDlg::onApply
276 // Purpose : SLOT called when "Apply" button pressed.
277 //=======================================================================
278 bool SMESHGUI_MoveNodesDlg::onApply()
279 {
280   if (mySMESHGUI->isActiveStudyLocked())
281     return false;
282
283   if (!isValid(true))
284     return false;
285
286   SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
287   if (aMesh->_is_nil()) {
288     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
289                              tr("SMESHG_NO_MESH"), QMessageBox::Ok);
290     return false;
291   }
292
293   SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
294   if (aMeshEditor->_is_nil())
295     return false;
296
297   int anId = myId->text().toInt();
298   bool aResult = false;
299   try {
300     aResult = aMeshEditor->MoveNode(anId, myX->GetValue(), myY->GetValue(), myZ->GetValue());
301   } catch (...) {
302   }
303
304   if (aResult) {
305     SALOME_ListIO aList;
306     aList.Append(myMeshActor->getIO());
307     mySelectionMgr->setSelectedObjects(aList,false);
308     SMESH::UpdateView();
309     reset();
310   }
311
312   return aResult;
313 }
314
315 //=======================================================================
316 // name    : SMESHGUI_MoveNodesDlg::onOk
317 // Purpose : SLOT called when "Ok" button pressed.
318 //=======================================================================
319 void SMESHGUI_MoveNodesDlg::onOk()
320 {
321   if (onApply())
322     onClose();
323 }
324
325 //=======================================================================
326 // name    : SMESHGUI_MoveNodesDlg::onClose
327 // Purpose : SLOT called when "Close" button pressed. Close dialog
328 //=======================================================================
329 void SMESHGUI_MoveNodesDlg::onClose()
330 {
331   //mySelectionMgr->clearSelected();
332   SMESH::SetPointRepresentation(false);
333   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
334     aViewWindow->SetSelectionMode(ActorSelection);
335   disconnect(mySelectionMgr, 0, this, 0);
336   disconnect(mySMESHGUI, 0, this, 0);
337   erasePreview();
338   mySMESHGUI->ResetState();
339   reject();
340 }
341
342 //=================================================================================
343 // function : onHelp()
344 // purpose  :
345 //=================================================================================
346 void SMESHGUI_MoveNodesDlg::onHelp()
347 {
348   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
349   if (app) 
350     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
351   else {
352                 QString platform;
353 #ifdef WIN32
354                 platform = "winapplication";
355 #else
356                 platform = "application";
357 #endif
358     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
359                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
360                            arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
361                            QObject::tr("BUT_OK"));
362   }
363 }
364
365 //=======================================================================
366 // name    : SMESHGUI_MoveNodesDlg::onTextChange
367 // Purpose :
368 //=======================================================================
369 void SMESHGUI_MoveNodesDlg::onTextChange (const QString& theNewText)
370 {
371   if (myBusy) return;
372
373   myOkBtn->setEnabled(false);
374   myApplyBtn->setEnabled(false);
375   erasePreview();
376
377   // select entered node
378   if(myMeshActor){
379     if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()){
380       myBusy = true;
381       Handle(SALOME_InteractiveObject) anIO = myMeshActor->getIO();
382       SALOME_ListIO aList;
383       aList.Append(anIO);
384       mySelectionMgr->setSelectedObjects(aList,false);
385       myBusy = false;
386
387       if(const SMDS_MeshElement *anElem = aMesh->FindElement(theNewText.toInt())) {
388         TColStd_MapOfInteger aListInd;
389         aListInd.Add(anElem->GetID());
390         mySelector->AddOrRemoveIndex(anIO,aListInd, false);
391         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
392           aViewWindow->highlight(anIO,true,true);
393         
394         onSelectionDone();
395       }
396     }
397   }
398 }
399
400 //=======================================================================
401 // name    : SMESHGUI_MoveNodesDlg::onSelectionDone
402 // Purpose : SLOT called when selection changed
403 //=======================================================================
404 void SMESHGUI_MoveNodesDlg::onSelectionDone()
405 {
406   if (myBusy) return;
407   myMeshActor = 0;
408
409   SALOME_ListIO aList;
410   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
411
412   if (aList.Extent() == 1) {
413     Handle(SALOME_InteractiveObject) anIO = aList.First();
414     myMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
415     if(myMeshActor){
416       QString aText;
417       if (SMESH::GetNameOfSelectedNodes(mySelector,anIO,aText) == 1) {
418         if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()) {
419           if(const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
420             myBusy = true;
421             myId->setText(aText);
422             myX->SetValue(aNode->X());
423             myY->SetValue(aNode->Y());
424             myZ->SetValue(aNode->Z());
425             myBusy = false;
426             erasePreview(); // avoid overlapping of a selection and a preview
427             updateButtons();
428             return;
429           }
430         }
431       }
432     }
433   }
434
435   reset();
436 }
437
438 //=======================================================================
439 // name    : SMESHGUI_MoveNodesDlg::onDeactivate
440 // Purpose : SLOT called when dialog must be deativated
441 //=======================================================================
442 void SMESHGUI_MoveNodesDlg::onDeactivate()
443 {
444   setEnabled(false);
445   erasePreview();
446 }
447
448 //=======================================================================
449 // name    : SMESHGUI_MoveNodesDlg::enterEvent
450 // Purpose : Event filter
451 //=======================================================================
452 void SMESHGUI_MoveNodesDlg::enterEvent (QEvent*)
453 {
454   if (!isEnabled()) {
455     mySMESHGUI->EmitSignalDeactivateDialog();
456
457     // set selection mode
458     SMESH::SetPointRepresentation(true);
459     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
460       aViewWindow->SetSelectionMode(NodeSelection);
461
462     redisplayPreview();
463
464     setEnabled(true);
465   }
466 }
467
468 //=======================================================================
469 // name    : SMESHGUI_MoveNodesDlg::closeEvent
470 // Purpose :
471 //=======================================================================
472 void SMESHGUI_MoveNodesDlg::closeEvent (QCloseEvent*)
473 {
474   onClose();
475   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
476     aViewWindow->Repaint();
477 }
478
479 //=======================================================================
480 // name    : SMESHGUI_MoveNodesDlg::hideEvent
481 // Purpose : may be caused by ESC key
482 //=======================================================================
483 void SMESHGUI_MoveNodesDlg::hideEvent (QHideEvent*)
484 {
485   if (!isMinimized())
486     onClose();
487 }
488
489 //=======================================================================
490 // name    : SMESHGUI_MoveNodesDlg::updateButtons
491 // Purpose : Update buttons state
492 //=======================================================================
493 void SMESHGUI_MoveNodesDlg::updateButtons()
494 {
495   bool isEnabled = isValid(false);
496   myOkBtn->setEnabled(isEnabled);
497   myApplyBtn->setEnabled(isEnabled);
498 }
499
500 //=======================================================================
501 // name    : SMESHGUI_MoveNodesDlg::erasePreview
502 // Purpose : Erase preview
503 //=======================================================================
504 void  SMESHGUI_MoveNodesDlg::erasePreview()
505 {
506   if (myPreviewActor == 0)
507     return;
508
509   SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
510   if (aViewWindow)
511     aViewWindow->RemoveActor(myPreviewActor);
512   myPreviewActor->Delete();
513   myPreviewActor = 0;
514   if (aViewWindow)
515     aViewWindow->Repaint();
516 }
517
518 //=======================================================================
519 // name    : SMESHGUI_MoveNodesDlg::redisplayPreview
520 // Purpose : Redisplay preview
521 //=======================================================================
522 void SMESHGUI_MoveNodesDlg::redisplayPreview()
523 {
524   if (myBusy)
525     return;
526
527   if (myPreviewActor != 0)
528     erasePreview();
529
530   if (!isValid(false))
531     return;
532
533   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
534
535   vtkPoints* aPoints = vtkPoints::New();
536   aPoints->SetNumberOfPoints(1);
537   aPoints->SetPoint(0, myX->GetValue(), myY->GetValue(), myZ->GetValue());
538
539   // Create cells
540
541   vtkIdList *anIdList = vtkIdList::New();
542   anIdList->SetNumberOfIds(1);
543
544   vtkCellArray *aCells = vtkCellArray::New();
545   aCells->Allocate(2, 0);
546
547   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
548   aCellTypesArray->SetNumberOfComponents(1);
549   aCellTypesArray->Allocate(1);
550
551   anIdList->SetId(0, 0);
552   aCells->InsertNextCell(anIdList);
553   aCellTypesArray->InsertNextValue(VTK_VERTEX);
554   anIdList->Delete();
555
556   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
557   aCellLocationsArray->SetNumberOfComponents(1);
558   aCellLocationsArray->SetNumberOfTuples(1);
559
560   aCells->InitTraversal();
561   vtkIdType npts;
562   aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
563
564   aGrid->SetPoints(aPoints);
565   aPoints->Delete();
566
567   aGrid->SetCells(aCellTypesArray,aCellLocationsArray,aCells);
568   aCellLocationsArray->Delete();
569   aCellTypesArray->Delete();
570   aCells->Delete();
571
572   // Create and display actor
573   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
574   aMapper->SetInput(aGrid);
575   aGrid->Delete();
576
577   myPreviewActor = SALOME_Actor::New();
578   myPreviewActor->PickableOff();
579   myPreviewActor->SetMapper(aMapper);
580   aMapper->Delete();
581
582   vtkProperty* aProp = vtkProperty::New();
583   aProp->SetRepresentationToWireframe();
584   aProp->SetColor(250, 0, 250);
585   aProp->SetPointSize(5);
586   myPreviewActor->SetProperty(aProp);
587   aProp->Delete();
588
589   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
590     {
591       aViewWindow->AddActor(myPreviewActor);
592       aViewWindow->Repaint();
593     }
594 }
595
596 //=================================================================================
597 // function : keyPressEvent()
598 // purpose  :
599 //=================================================================================
600 void SMESHGUI_MoveNodesDlg::keyPressEvent( QKeyEvent* e )
601 {
602   QDialog::keyPressEvent( e );
603   if ( e->isAccepted() )
604     return;
605
606   if ( e->key() == Key_F1 )
607     {
608       e->accept();
609       onHelp();
610     }
611 }