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)
85 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
91 //=================================================================================
92 // name : SMESHGUI_MoveNodesDlg::SMESHGUI_MoveNodesDlg
94 //=================================================================================
95 SMESHGUI_MoveNodesDlg::SMESHGUI_MoveNodesDlg (SMESHGUI* theModule,
97 QDialog(SMESH::GetDesktop(theModule),
100 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
101 mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
102 mySMESHGUI(theModule)
107 setCaption(tr("CAPTION"));
109 QVBoxLayout* aDlgLay = new QVBoxLayout (this, MARGIN, SPACING);
111 QFrame* aMainFrame = createMainFrame (this);
112 QFrame* aBtnFrame = createButtonFrame(this);
114 aDlgLay->addWidget(aMainFrame);
115 aDlgLay->addWidget(aBtnFrame);
117 aDlgLay->setStretchFactor(aMainFrame, 1);
119 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
121 myHelpFileName = "/files/displacing_nodes.htm";
126 //=======================================================================
127 // name : SMESHGUI_MoveNodesDlg::createButtonFrame
128 // Purpose : Create frame containing buttons
129 //=======================================================================
130 QFrame* SMESHGUI_MoveNodesDlg::createButtonFrame (QWidget* theParent)
132 QFrame* aFrame = new QFrame(theParent);
133 aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
135 myOkBtn = new QPushButton(tr("SMESH_BUT_OK" ), aFrame);
136 myApplyBtn = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
137 myCloseBtn = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
138 myHelpBtn = new QPushButton(tr("SMESH_BUT_HELP"), aFrame);
140 QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
142 QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
144 aLay->addWidget(myOkBtn);
145 aLay->addWidget(myApplyBtn);
146 aLay->addItem(aSpacer);
147 aLay->addWidget(myCloseBtn);
148 aLay->addWidget(myHelpBtn);
150 connect(myOkBtn, SIGNAL(clicked()), SLOT(onOk()));
151 connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
152 connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
153 connect(myHelpBtn, SIGNAL(clicked()), SLOT(onHelp()));
158 //=======================================================================
159 // name : SMESHGUI_MoveNodesDlg::createMainFrame
160 // Purpose : Create frame containing dialog's input fields
161 //=======================================================================
162 QFrame* SMESHGUI_MoveNodesDlg::createMainFrame (QWidget* theParent)
164 QFrame* aFrame = new QFrame(theParent);
166 QPixmap iconMoveNode (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_MOVE_NODE")));
167 QPixmap iconSelect (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
169 QButtonGroup* aPixGrp = new QButtonGroup(1, Qt::Vertical, tr("MESH_NODE"), aFrame);
170 aPixGrp->setExclusive(TRUE);
171 QRadioButton* aRBut = new QRadioButton(aPixGrp);
172 aRBut->setPixmap(iconMoveNode);
173 aRBut->setChecked(TRUE);
175 QGroupBox* anIdGrp = new QGroupBox(1, Qt::Vertical, tr("SMESH_MOVE"), aFrame);
176 new QLabel(tr("NODE_ID"), anIdGrp);
177 (new QPushButton(anIdGrp))->setPixmap(iconSelect);
178 myId = new QLineEdit(anIdGrp);
179 myId->setValidator(new SMESHGUI_IdValidator(this, "validator", 1));
181 QGroupBox* aCoordGrp = new QGroupBox(1, Qt::Vertical, tr("SMESH_COORDINATES"), aFrame);
182 QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
183 aXLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
184 myX = new SMESHGUI_SpinBox(aCoordGrp);
186 QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
187 aYLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
188 myY = new SMESHGUI_SpinBox(aCoordGrp);
190 QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
191 aZLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
192 myZ = new SMESHGUI_SpinBox(aCoordGrp);
194 myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, 3);
195 myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, 3);
196 myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, 3);
198 QVBoxLayout* aLay = new QVBoxLayout(aFrame);
199 aLay->addWidget(aPixGrp);
200 aLay->addWidget(anIdGrp);
201 aLay->addWidget(aCoordGrp);
203 // connect signale and slots
204 connect(myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
205 connect(myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
206 connect(myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
207 connect(myId, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
212 //=======================================================================
213 // name : SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg
215 //=======================================================================
216 SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg()
221 //=======================================================================
222 // name : SMESHGUI_MoveNodesDlg::Init
223 // Purpose : Init dialog fields
224 //=======================================================================
225 void SMESHGUI_MoveNodesDlg::Init()
231 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
233 // selection and SMESHGUI
234 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
235 connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
236 connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
243 // set selection mode
244 SMESH::SetPointRepresentation(true);
245 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
246 aViewWindow->SetSelectionMode(NodeSelection);
251 //=======================================================================
252 // name : SMESHGUI_MoveNodesDlg::isValid
253 // Purpose : Verify validity of entry information
254 //=======================================================================
255 bool SMESHGUI_MoveNodesDlg::isValid (const bool theMess) const
257 if (myId->text().isEmpty()) {
259 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
260 tr("NODE_ID_IS_NOT_DEFINED"), QMessageBox::Ok);
266 //=======================================================================
267 // name : SMESHGUI_MoveNodesDlg::reset
268 // Purpose : Reset the dialog state
269 //=======================================================================
270 void SMESHGUI_MoveNodesDlg::reset()
280 //=======================================================================
281 // name : SMESHGUI_MoveNodesDlg::onApply
282 // Purpose : SLOT called when "Apply" button pressed.
283 //=======================================================================
284 bool SMESHGUI_MoveNodesDlg::onApply()
286 if (mySMESHGUI->isActiveStudyLocked())
292 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
293 if (aMesh->_is_nil()) {
294 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
295 tr("SMESHG_NO_MESH"), QMessageBox::Ok);
299 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
300 if (aMeshEditor->_is_nil())
303 int anId = myId->text().toInt();
304 bool aResult = false;
306 aResult = aMeshEditor->MoveNode(anId, myX->GetValue(), myY->GetValue(), myZ->GetValue());
312 aList.Append(myMeshActor->getIO());
313 mySelectionMgr->setSelectedObjects(aList,false);
321 //=======================================================================
322 // name : SMESHGUI_MoveNodesDlg::onOk
323 // Purpose : SLOT called when "Ok" button pressed.
324 //=======================================================================
325 void SMESHGUI_MoveNodesDlg::onOk()
331 //=======================================================================
332 // name : SMESHGUI_MoveNodesDlg::onClose
333 // Purpose : SLOT called when "Close" button pressed. Close dialog
334 //=======================================================================
335 void SMESHGUI_MoveNodesDlg::onClose()
337 //mySelectionMgr->clearSelected();
338 SMESH::SetPointRepresentation(false);
339 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
340 aViewWindow->SetSelectionMode(ActorSelection);
341 disconnect(mySelectionMgr, 0, this, 0);
342 disconnect(mySMESHGUI, 0, this, 0);
344 mySMESHGUI->ResetState();
348 //=================================================================================
349 // function : onHelp()
351 //=================================================================================
352 void SMESHGUI_MoveNodesDlg::onHelp()
354 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
356 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
358 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
359 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
360 arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
361 QObject::tr("BUT_OK"));
365 //=======================================================================
366 // name : SMESHGUI_MoveNodesDlg::onTextChange
368 //=======================================================================
369 void SMESHGUI_MoveNodesDlg::onTextChange (const QString& theNewText)
373 myOkBtn->setEnabled(false);
374 myApplyBtn->setEnabled(false);
377 // select entered node
379 if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()){
381 Handle(SALOME_InteractiveObject) anIO = myMeshActor->getIO();
384 mySelectionMgr->setSelectedObjects(aList,false);
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);
400 //=======================================================================
401 // name : SMESHGUI_MoveNodesDlg::onSelectionDone
402 // Purpose : SLOT called when selection changed
403 //=======================================================================
404 void SMESHGUI_MoveNodesDlg::onSelectionDone()
410 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
412 if (aList.Extent() == 1) {
413 Handle(SALOME_InteractiveObject) anIO = aList.First();
414 myMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
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())) {
421 myId->setText(aText);
422 myX->SetValue(aNode->X());
423 myY->SetValue(aNode->Y());
424 myZ->SetValue(aNode->Z());
426 erasePreview(); // avoid overlapping of a selection and a preview
438 //=======================================================================
439 // name : SMESHGUI_MoveNodesDlg::onDeactivate
440 // Purpose : SLOT called when dialog must be deativated
441 //=======================================================================
442 void SMESHGUI_MoveNodesDlg::onDeactivate()
448 //=======================================================================
449 // name : SMESHGUI_MoveNodesDlg::enterEvent
450 // Purpose : Event filter
451 //=======================================================================
452 void SMESHGUI_MoveNodesDlg::enterEvent (QEvent*)
455 mySMESHGUI->EmitSignalDeactivateDialog();
457 // set selection mode
458 SMESH::SetPointRepresentation(true);
459 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
460 aViewWindow->SetSelectionMode(NodeSelection);
468 //=======================================================================
469 // name : SMESHGUI_MoveNodesDlg::closeEvent
471 //=======================================================================
472 void SMESHGUI_MoveNodesDlg::closeEvent (QCloseEvent*)
475 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
476 aViewWindow->Repaint();
479 //=======================================================================
480 // name : SMESHGUI_MoveNodesDlg::hideEvent
481 // Purpose : may be caused by ESC key
482 //=======================================================================
483 void SMESHGUI_MoveNodesDlg::hideEvent (QHideEvent*)
489 //=======================================================================
490 // name : SMESHGUI_MoveNodesDlg::updateButtons
491 // Purpose : Update buttons state
492 //=======================================================================
493 void SMESHGUI_MoveNodesDlg::updateButtons()
495 bool isEnabled = isValid(false);
496 myOkBtn->setEnabled(isEnabled);
497 myApplyBtn->setEnabled(isEnabled);
500 //=======================================================================
501 // name : SMESHGUI_MoveNodesDlg::erasePreview
502 // Purpose : Erase preview
503 //=======================================================================
504 void SMESHGUI_MoveNodesDlg::erasePreview()
506 if (myPreviewActor == 0)
509 SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
511 aViewWindow->RemoveActor(myPreviewActor);
512 myPreviewActor->Delete();
515 aViewWindow->Repaint();
518 //=======================================================================
519 // name : SMESHGUI_MoveNodesDlg::redisplayPreview
520 // Purpose : Redisplay preview
521 //=======================================================================
522 void SMESHGUI_MoveNodesDlg::redisplayPreview()
527 if (myPreviewActor != 0)
533 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
535 vtkPoints* aPoints = vtkPoints::New();
536 aPoints->SetNumberOfPoints(1);
537 aPoints->SetPoint(0, myX->GetValue(), myY->GetValue(), myZ->GetValue());
541 vtkIdList *anIdList = vtkIdList::New();
542 anIdList->SetNumberOfIds(1);
544 vtkCellArray *aCells = vtkCellArray::New();
545 aCells->Allocate(2, 0);
547 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
548 aCellTypesArray->SetNumberOfComponents(1);
549 aCellTypesArray->Allocate(1);
551 anIdList->SetId(0, 0);
552 aCells->InsertNextCell(anIdList);
553 aCellTypesArray->InsertNextValue(VTK_VERTEX);
556 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
557 aCellLocationsArray->SetNumberOfComponents(1);
558 aCellLocationsArray->SetNumberOfTuples(1);
560 aCells->InitTraversal();
562 aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
564 aGrid->SetPoints(aPoints);
567 aGrid->SetCells(aCellTypesArray,aCellLocationsArray,aCells);
568 aCellLocationsArray->Delete();
569 aCellTypesArray->Delete();
572 // Create and display actor
573 vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
574 aMapper->SetInput(aGrid);
577 myPreviewActor = SALOME_Actor::New();
578 myPreviewActor->PickableOff();
579 myPreviewActor->SetMapper(aMapper);
582 vtkProperty* aProp = vtkProperty::New();
583 aProp->SetRepresentationToWireframe();
584 aProp->SetColor(250, 0, 250);
585 aProp->SetPointSize(5);
586 myPreviewActor->SetProperty(aProp);
589 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
591 aViewWindow->AddActor(myPreviewActor);
592 aViewWindow->Repaint();
596 //=================================================================================
597 // function : keyPressEvent()
599 //=================================================================================
600 void SMESHGUI_MoveNodesDlg::keyPressEvent( QKeyEvent* e )
602 QDialog::keyPressEvent( e );
603 if ( e->isAccepted() )
606 if ( e->key() == Key_F1 )