1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : SMESHGUI_MoveNodesDlg.cxx
25 // Author : Nicolas REJNERI
29 #include "SMESHGUI_MoveNodesDlg.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"
38 #include "SMESH_Actor.h"
39 #include "SMDS_Mesh.hxx"
40 #include "SMDS_MeshNode.hxx"
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"
49 #include "SVTK_Selector.h"
50 #include "SVTK_ViewModel.h"
51 #include "SVTK_ViewWindow.h"
52 #include "SALOME_ListIO.hxx"
54 #include "SVTK_ViewWindow.h"
55 #include "VTKViewer_CellLocationsArray.h"
57 #include "utilities.h"
60 #include <TColStd_MapOfInteger.hxx>
64 #include <vtkIdList.h>
65 #include <vtkCellArray.h>
66 #include <vtkUnsignedCharArray.h>
67 #include <vtkUnstructuredGrid.h>
68 #include <vtkDataSetMapper.h>
69 #include <vtkProperty.h>
72 #include <qgroupbox.h>
74 #include <qlineedit.h>
75 #include <qpushbutton.h>
76 #include <qradiobutton.h>
79 #include <qmessagebox.h>
80 #include <qbuttongroup.h>
83 #include <SALOMEconfig.h>
84 #include CORBA_SERVER_HEADER(SMESH_Mesh)
90 //=================================================================================
91 // name : SMESHGUI_MoveNodesDlg::SMESHGUI_MoveNodesDlg
93 //=================================================================================
94 SMESHGUI_MoveNodesDlg::SMESHGUI_MoveNodesDlg (SMESHGUI* theModule,
96 QDialog(SMESH::GetDesktop(theModule),
99 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
100 mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
101 mySMESHGUI(theModule)
106 setCaption(tr("CAPTION"));
108 QVBoxLayout* aDlgLay = new QVBoxLayout (this, MARGIN, SPACING);
110 QFrame* aMainFrame = createMainFrame (this);
111 QFrame* aBtnFrame = createButtonFrame(this);
113 aDlgLay->addWidget(aMainFrame);
114 aDlgLay->addWidget(aBtnFrame);
116 aDlgLay->setStretchFactor(aMainFrame, 1);
118 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
120 myHelpFileName = "/files/displacing_nodes.htm";
125 //=======================================================================
126 // name : SMESHGUI_MoveNodesDlg::createButtonFrame
127 // Purpose : Create frame containing buttons
128 //=======================================================================
129 QFrame* SMESHGUI_MoveNodesDlg::createButtonFrame (QWidget* theParent)
131 QFrame* aFrame = new QFrame(theParent);
132 aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
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);
139 QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
141 QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
143 aLay->addWidget(myOkBtn);
144 aLay->addWidget(myApplyBtn);
145 aLay->addItem(aSpacer);
146 aLay->addWidget(myCloseBtn);
147 aLay->addWidget(myHelpBtn);
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()));
157 //=======================================================================
158 // name : SMESHGUI_MoveNodesDlg::createMainFrame
159 // Purpose : Create frame containing dialog's input fields
160 //=======================================================================
161 QFrame* SMESHGUI_MoveNodesDlg::createMainFrame (QWidget* theParent)
163 QFrame* aFrame = new QFrame(theParent);
165 QPixmap iconMoveNode (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_MOVE_NODE")));
166 QPixmap iconSelect (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
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);
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));
180 QGroupBox* aCoordGrp = new QGroupBox(1, Qt::Vertical, tr("SMESH_COORDINATES"), aFrame);
181 QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
182 aXLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
183 myX = new SMESHGUI_SpinBox(aCoordGrp);
185 QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
186 aYLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
187 myY = new SMESHGUI_SpinBox(aCoordGrp);
189 QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
190 aZLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
191 myZ = new SMESHGUI_SpinBox(aCoordGrp);
193 myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, 3);
194 myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, 3);
195 myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, 3);
197 QVBoxLayout* aLay = new QVBoxLayout(aFrame);
198 aLay->addWidget(aPixGrp);
199 aLay->addWidget(anIdGrp);
200 aLay->addWidget(aCoordGrp);
202 // connect signale and slots
203 connect(myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
204 connect(myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
205 connect(myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
206 connect(myId, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
211 //=======================================================================
212 // name : SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg
214 //=======================================================================
215 SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg()
220 //=======================================================================
221 // name : SMESHGUI_MoveNodesDlg::Init
222 // Purpose : Init dialog fields
223 //=======================================================================
224 void SMESHGUI_MoveNodesDlg::Init()
230 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
232 // selection and SMESHGUI
233 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
234 connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
235 connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
242 // set selection mode
243 SMESH::SetPointRepresentation(true);
244 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
245 aViewWindow->SetSelectionMode(NodeSelection);
250 //=======================================================================
251 // name : SMESHGUI_MoveNodesDlg::isValid
252 // Purpose : Verify validity of entry information
253 //=======================================================================
254 bool SMESHGUI_MoveNodesDlg::isValid (const bool theMess) const
256 if (myId->text().isEmpty()) {
258 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
259 tr("NODE_ID_IS_NOT_DEFINED"), QMessageBox::Ok);
265 //=======================================================================
266 // name : SMESHGUI_MoveNodesDlg::reset
267 // Purpose : Reset the dialog state
268 //=======================================================================
269 void SMESHGUI_MoveNodesDlg::reset()
279 //=======================================================================
280 // name : SMESHGUI_MoveNodesDlg::onApply
281 // Purpose : SLOT called when "Apply" button pressed.
282 //=======================================================================
283 bool SMESHGUI_MoveNodesDlg::onApply()
285 if (mySMESHGUI->isActiveStudyLocked())
291 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
292 if (aMesh->_is_nil()) {
293 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
294 tr("SMESHG_NO_MESH"), QMessageBox::Ok);
298 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
299 if (aMeshEditor->_is_nil())
302 int anId = myId->text().toInt();
303 bool aResult = false;
305 aResult = aMeshEditor->MoveNode(anId, myX->GetValue(), myY->GetValue(), myZ->GetValue());
311 aList.Append(myMeshActor->getIO());
312 mySelectionMgr->setSelectedObjects(aList,false);
320 //=======================================================================
321 // name : SMESHGUI_MoveNodesDlg::onOk
322 // Purpose : SLOT called when "Ok" button pressed.
323 //=======================================================================
324 void SMESHGUI_MoveNodesDlg::onOk()
330 //=======================================================================
331 // name : SMESHGUI_MoveNodesDlg::onClose
332 // Purpose : SLOT called when "Close" button pressed. Close dialog
333 //=======================================================================
334 void SMESHGUI_MoveNodesDlg::onClose()
336 //mySelectionMgr->clearSelected();
337 SMESH::SetPointRepresentation(false);
338 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
339 aViewWindow->SetSelectionMode(ActorSelection);
340 disconnect(mySelectionMgr, 0, this, 0);
341 disconnect(mySMESHGUI, 0, this, 0);
343 mySMESHGUI->ResetState();
347 //=================================================================================
348 // function : onHelp()
350 //=================================================================================
351 void SMESHGUI_MoveNodesDlg::onHelp()
353 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
355 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
357 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
358 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
359 arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
360 QObject::tr("BUT_OK"));
364 //=======================================================================
365 // name : SMESHGUI_MoveNodesDlg::onTextChange
367 //=======================================================================
368 void SMESHGUI_MoveNodesDlg::onTextChange (const QString& theNewText)
372 myOkBtn->setEnabled(false);
373 myApplyBtn->setEnabled(false);
376 // select entered node
378 if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()){
380 Handle(SALOME_InteractiveObject) anIO = myMeshActor->getIO();
383 mySelectionMgr->setSelectedObjects(aList,false);
386 if(const SMDS_MeshElement *anElem = aMesh->FindElement(theNewText.toInt())) {
387 TColStd_MapOfInteger aListInd;
388 aListInd.Add(anElem->GetID());
389 mySelector->AddOrRemoveIndex(anIO,aListInd, false);
390 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
391 aViewWindow->highlight(anIO,true,true);
399 //=======================================================================
400 // name : SMESHGUI_MoveNodesDlg::onSelectionDone
401 // Purpose : SLOT called when selection changed
402 //=======================================================================
403 void SMESHGUI_MoveNodesDlg::onSelectionDone()
409 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
411 if (aList.Extent() == 1) {
412 Handle(SALOME_InteractiveObject) anIO = aList.First();
413 myMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
416 if (SMESH::GetNameOfSelectedNodes(mySelector,anIO,aText) == 1) {
417 if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()) {
418 if(const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
420 myId->setText(aText);
421 myX->SetValue(aNode->X());
422 myY->SetValue(aNode->Y());
423 myZ->SetValue(aNode->Z());
425 erasePreview(); // avoid overlapping of a selection and a preview
437 //=======================================================================
438 // name : SMESHGUI_MoveNodesDlg::onDeactivate
439 // Purpose : SLOT called when dialog must be deativated
440 //=======================================================================
441 void SMESHGUI_MoveNodesDlg::onDeactivate()
447 //=======================================================================
448 // name : SMESHGUI_MoveNodesDlg::enterEvent
449 // Purpose : Event filter
450 //=======================================================================
451 void SMESHGUI_MoveNodesDlg::enterEvent (QEvent*)
454 mySMESHGUI->EmitSignalDeactivateDialog();
456 // set selection mode
457 SMESH::SetPointRepresentation(true);
458 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
459 aViewWindow->SetSelectionMode(NodeSelection);
467 //=======================================================================
468 // name : SMESHGUI_MoveNodesDlg::closeEvent
470 //=======================================================================
471 void SMESHGUI_MoveNodesDlg::closeEvent (QCloseEvent*)
474 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
475 aViewWindow->Repaint();
478 //=======================================================================
479 // name : SMESHGUI_MoveNodesDlg::hideEvent
480 // Purpose : may be caused by ESC key
481 //=======================================================================
482 void SMESHGUI_MoveNodesDlg::hideEvent (QHideEvent*)
488 //=======================================================================
489 // name : SMESHGUI_MoveNodesDlg::updateButtons
490 // Purpose : Update buttons state
491 //=======================================================================
492 void SMESHGUI_MoveNodesDlg::updateButtons()
494 bool isEnabled = isValid(false);
495 myOkBtn->setEnabled(isEnabled);
496 myApplyBtn->setEnabled(isEnabled);
499 //=======================================================================
500 // name : SMESHGUI_MoveNodesDlg::erasePreview
501 // Purpose : Erase preview
502 //=======================================================================
503 void SMESHGUI_MoveNodesDlg::erasePreview()
505 if (myPreviewActor == 0)
508 SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
510 aViewWindow->RemoveActor(myPreviewActor);
511 myPreviewActor->Delete();
514 aViewWindow->Repaint();
517 //=======================================================================
518 // name : SMESHGUI_MoveNodesDlg::redisplayPreview
519 // Purpose : Redisplay preview
520 //=======================================================================
521 void SMESHGUI_MoveNodesDlg::redisplayPreview()
526 if (myPreviewActor != 0)
532 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
534 vtkPoints* aPoints = vtkPoints::New();
535 aPoints->SetNumberOfPoints(1);
536 aPoints->SetPoint(0, myX->GetValue(), myY->GetValue(), myZ->GetValue());
540 vtkIdList *anIdList = vtkIdList::New();
541 anIdList->SetNumberOfIds(1);
543 vtkCellArray *aCells = vtkCellArray::New();
544 aCells->Allocate(2, 0);
546 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
547 aCellTypesArray->SetNumberOfComponents(1);
548 aCellTypesArray->Allocate(1);
550 anIdList->SetId(0, 0);
551 aCells->InsertNextCell(anIdList);
552 aCellTypesArray->InsertNextValue(VTK_VERTEX);
555 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
556 aCellLocationsArray->SetNumberOfComponents(1);
557 aCellLocationsArray->SetNumberOfTuples(1);
559 aCells->InitTraversal();
561 aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
563 aGrid->SetPoints(aPoints);
566 aGrid->SetCells(aCellTypesArray,aCellLocationsArray,aCells);
567 aCellLocationsArray->Delete();
568 aCellTypesArray->Delete();
571 // Create and display actor
572 vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
573 aMapper->SetInput(aGrid);
576 myPreviewActor = SALOME_Actor::New();
577 myPreviewActor->PickableOff();
578 myPreviewActor->SetMapper(aMapper);
581 vtkProperty* aProp = vtkProperty::New();
582 aProp->SetRepresentationToWireframe();
583 aProp->SetColor(250, 0, 250);
584 aProp->SetPointSize(5);
585 myPreviewActor->SetProperty(aProp);
588 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
590 aViewWindow->AddActor(myPreviewActor);
591 aViewWindow->Repaint();
595 //=================================================================================
596 // function : keyPressEvent()
598 //=================================================================================
599 void SMESHGUI_MoveNodesDlg::keyPressEvent( QKeyEvent* e )
601 QDialog::keyPressEvent( e );
602 if ( e->isAccepted() )
605 if ( e->key() == Key_F1 )