1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // SMESH SMESHGUI : GUI for SMESH component
23 // File : SMESHGUI_MoveNodesDlg.cxx
24 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
27 #include "SMESHGUI_MoveNodesDlg.h"
30 #include "SMESHGUI_SpinBox.h"
31 #include "SMESHGUI_IdValidator.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34 #include "SMESHGUI_MeshUtils.h"
36 #include <SMESH_Actor.h>
37 #include <SMDS_Mesh.hxx>
39 // SALOME GUI includes
40 #include <LightApp_SelectionMgr.h>
41 #include <LightApp_Application.h>
42 #include <SUIT_ResourceMgr.h>
43 #include <SUIT_Desktop.h>
44 #include <SUIT_Session.h>
45 #include <SUIT_MessageBox.h>
47 #include <SVTK_ViewModel.h>
48 #include <SVTK_ViewWindow.h>
49 #include <SALOME_ListIO.hxx>
51 #include <VTKViewer_CellLocationsArray.h>
54 #include <TColStd_MapOfInteger.hxx>
57 #include <vtkIdList.h>
58 #include <vtkCellArray.h>
59 #include <vtkUnsignedCharArray.h>
60 #include <vtkUnstructuredGrid.h>
61 #include <vtkDataSetMapper.h>
62 #include <vtkProperty.h>
68 #include <QPushButton>
69 #include <QRadioButton>
70 #include <QHBoxLayout>
71 #include <QVBoxLayout>
73 #include <QButtonGroup>
76 #include <SALOMEconfig.h>
77 #include CORBA_SERVER_HEADER(SMESH_Mesh)
78 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
83 //=================================================================================
84 // name : SMESHGUI_MoveNodesDlg::SMESHGUI_MoveNodesDlg
86 //=================================================================================
87 SMESHGUI_MoveNodesDlg::SMESHGUI_MoveNodesDlg(SMESHGUI* theModule):
88 QDialog(SMESH::GetDesktop(theModule)),
89 mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
96 setWindowTitle(tr("CAPTION"));
98 QVBoxLayout* aDlgLay = new QVBoxLayout(this);
99 aDlgLay->setSpacing(SPACING);
100 aDlgLay->setMargin(MARGIN);
102 QWidget* aMainFrame = createMainFrame (this);
103 QWidget* aBtnFrame = createButtonFrame(this);
105 aDlgLay->addWidget(aMainFrame);
106 aDlgLay->addWidget(aBtnFrame);
108 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
110 myHelpFileName = "moving_nodes_page.html";
115 //=======================================================================
116 // name : SMESHGUI_MoveNodesDlg::createButtonFrame
117 // Purpose : Create frame containing buttons
118 //=======================================================================
119 QWidget* SMESHGUI_MoveNodesDlg::createButtonFrame (QWidget* theParent)
121 QFrame* aFrame = new QFrame(theParent);
122 aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
124 myOkBtn = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aFrame);
125 myApplyBtn = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
126 myCloseBtn = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
127 myHelpBtn = new QPushButton(tr("SMESH_BUT_HELP"), aFrame);
129 QHBoxLayout* aLay = new QHBoxLayout(aFrame);
130 aLay->setSpacing(SPACING);
131 aLay->setMargin(MARGIN);
133 aLay->addWidget(myOkBtn);
134 aLay->addSpacing(10);
135 aLay->addWidget(myApplyBtn);
136 aLay->addSpacing(10);
138 aLay->addWidget(myCloseBtn);
139 aLay->addWidget(myHelpBtn);
141 connect(myOkBtn, SIGNAL(clicked()), SLOT(onOk()));
142 connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
143 connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
144 connect(myHelpBtn, SIGNAL(clicked()), SLOT(onHelp()));
149 //=======================================================================
150 // name : SMESHGUI_MoveNodesDlg::createMainFrame
151 // Purpose : Create frame containing dialog's input fields
152 //=======================================================================
153 QWidget* SMESHGUI_MoveNodesDlg::createMainFrame (QWidget* theParent)
155 QWidget* aFrame = new QWidget(theParent);
157 QPixmap iconMoveNode (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_MOVE_NODE")));
158 QPixmap iconSelect (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
160 //------------------------------------------------------------
161 QGroupBox* aPixGrp = new QGroupBox(tr("MESH_NODE"), aFrame);
162 QButtonGroup* aBtnGrp = new QButtonGroup(this);
163 QHBoxLayout* aPixGrpLayout = new QHBoxLayout(aPixGrp);
164 aPixGrpLayout->setSpacing(SPACING);
165 aPixGrpLayout->setMargin(MARGIN);
167 QRadioButton* aRBut = new QRadioButton(aPixGrp);
168 aRBut->setIcon(iconMoveNode);
169 aRBut->setChecked(true);
171 aPixGrpLayout->addWidget(aRBut);
172 aBtnGrp->addButton(aRBut, 0);
174 //------------------------------------------------------------
175 QGroupBox* anIdGrp = new QGroupBox(tr("SMESH_MOVE"), aFrame);
176 QHBoxLayout* anIdGrpLayout = new QHBoxLayout(anIdGrp);
177 anIdGrpLayout->setSpacing(SPACING);
178 anIdGrpLayout->setMargin(MARGIN);
180 QLabel* idLabl = new QLabel(tr("NODE_ID"), anIdGrp);
181 QPushButton* idBtn = new QPushButton(anIdGrp);
182 idBtn->setIcon(iconSelect);
183 myId = new QLineEdit(anIdGrp);
184 myId->setValidator(new SMESHGUI_IdValidator(this, 1));
186 anIdGrpLayout->addWidget(idLabl);
187 anIdGrpLayout->addWidget(idBtn);
188 anIdGrpLayout->addWidget(myId);
190 //------------------------------------------------------------
191 QGroupBox* aCoordGrp = new QGroupBox(tr("SMESH_COORDINATES"), aFrame);
192 QHBoxLayout* aCoordGrpLayout = new QHBoxLayout(aCoordGrp);
193 aCoordGrpLayout->setSpacing(SPACING);
194 aCoordGrpLayout->setMargin(MARGIN);
196 QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
197 myX = new SMESHGUI_SpinBox(aCoordGrp);
199 QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
200 myY = new SMESHGUI_SpinBox(aCoordGrp);
202 QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
203 myZ = new SMESHGUI_SpinBox(aCoordGrp);
205 aCoordGrpLayout->addWidget(aXLabel);
206 aCoordGrpLayout->addWidget(myX);
207 aCoordGrpLayout->addWidget(aYLabel);
208 aCoordGrpLayout->addWidget(myY);
209 aCoordGrpLayout->addWidget(aZLabel);
210 aCoordGrpLayout->addWidget(myZ);
212 //------------------------------------------------------------
213 myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, DBL_DIGITS_DISPLAY);
214 myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, DBL_DIGITS_DISPLAY);
215 myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, DBL_DIGITS_DISPLAY);
217 //------------------------------------------------------------
218 QVBoxLayout* aLay = new QVBoxLayout(aFrame);
220 aLay->setMargin(SPACING);
221 aLay->addWidget(aPixGrp);
222 aLay->addWidget(anIdGrp);
223 aLay->addWidget(aCoordGrp);
225 //------------------------------------------------------------
226 // connect signale and slots
227 connect(myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
228 connect(myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
229 connect(myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
230 connect(myId, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
235 //=======================================================================
236 // name : SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg
238 //=======================================================================
239 SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg()
244 //=======================================================================
245 // name : SMESHGUI_MoveNodesDlg::Init
246 // Purpose : Init dialog fields
247 //=======================================================================
248 void SMESHGUI_MoveNodesDlg::Init()
254 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
256 // selection and SMESHGUI
257 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
258 connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
259 connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
264 // set selection mode
265 SMESH::SetPointRepresentation(true);
266 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
267 aViewWindow->SetSelectionMode(NodeSelection);
272 //=======================================================================
273 // name : SMESHGUI_MoveNodesDlg::isValid
274 // Purpose : Verify validity of entry information
275 //=======================================================================
276 bool SMESHGUI_MoveNodesDlg::isValid (const bool theMess)
278 if (myId->text().isEmpty()) {
280 SUIT_MessageBox::information(this, tr("SMESH_WARNING"),
281 tr("NODE_ID_IS_NOT_DEFINED"));
287 ok = myX->isValid( msg, theMess ) && ok;
288 ok = myY->isValid( msg, theMess ) && ok;
289 ok = myZ->isValid( msg, theMess ) && ok;
292 QString str( tr( "SMESH_INCORRECT_INPUT" ) );
293 if ( !msg.isEmpty() )
295 SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
303 //=======================================================================
304 // name : SMESHGUI_MoveNodesDlg::reset
305 // Purpose : Reset the dialog state
306 //=======================================================================
307 void SMESHGUI_MoveNodesDlg::reset()
317 //=======================================================================
318 // name : SMESHGUI_MoveNodesDlg::onApply
319 // Purpose : SLOT called when "Apply" button pressed.
320 //=======================================================================
321 bool SMESHGUI_MoveNodesDlg::onApply()
323 if (mySMESHGUI->isActiveStudyLocked())
329 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
330 if (aMesh->_is_nil()) {
331 SUIT_MessageBox::information(this, tr("SMESH_ERROR"),
332 tr("SMESHG_NO_MESH"));
336 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
337 if (aMeshEditor->_is_nil())
340 int anId = myId->text().toInt();
341 bool aResult = false;
343 aResult = aMeshEditor->MoveNode(anId, myX->GetValue(), myY->GetValue(), myZ->GetValue());
345 QStringList aParameters;
346 aParameters << myX->text();
347 aParameters << myY->text();
348 aParameters << myZ->text();
349 aMesh->SetParameters( SMESHGUI::JoinObjectParameters(aParameters) );
355 aList.Append(myMeshActor->getIO());
356 mySelectionMgr->setSelectedObjects(aList,false);
364 //=======================================================================
365 // name : SMESHGUI_MoveNodesDlg::onOk
366 // Purpose : SLOT called when "Ok" button pressed.
367 //=======================================================================
368 void SMESHGUI_MoveNodesDlg::onOk()
374 //=======================================================================
375 // name : SMESHGUI_MoveNodesDlg::onClose
376 // Purpose : SLOT called when "Close" button pressed. Close dialog
377 //=======================================================================
378 void SMESHGUI_MoveNodesDlg::onClose()
380 //mySelectionMgr->clearSelected();
381 SMESH::SetPointRepresentation(false);
382 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
383 aViewWindow->SetSelectionMode(ActorSelection);
384 disconnect(mySelectionMgr, 0, this, 0);
385 disconnect(mySMESHGUI, 0, this, 0);
387 mySMESHGUI->ResetState();
391 //=================================================================================
392 // function : onHelp()
394 //=================================================================================
395 void SMESHGUI_MoveNodesDlg::onHelp()
397 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
399 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
403 platform = "winapplication";
405 platform = "application";
407 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
408 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
409 arg(app->resourceMgr()->stringValue("ExternalBrowser",
411 arg(myHelpFileName));
415 //=======================================================================
416 // name : SMESHGUI_MoveNodesDlg::onTextChange
418 //=======================================================================
419 void SMESHGUI_MoveNodesDlg::onTextChange (const QString& theNewText)
423 myOkBtn->setEnabled(false);
424 myApplyBtn->setEnabled(false);
427 // select entered node
429 if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()){
431 Handle(SALOME_InteractiveObject) anIO = myMeshActor->getIO();
434 mySelectionMgr->setSelectedObjects(aList,false);
437 if(const SMDS_MeshElement *anElem = aMesh->FindElement(theNewText.toInt())) {
438 TColStd_MapOfInteger aListInd;
439 aListInd.Add(anElem->GetID());
440 mySelector->AddOrRemoveIndex(anIO,aListInd, false);
441 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
442 aViewWindow->highlight(anIO,true,true);
450 //=======================================================================
451 // name : SMESHGUI_MoveNodesDlg::onSelectionDone
452 // Purpose : SLOT called when selection changed
453 //=======================================================================
454 void SMESHGUI_MoveNodesDlg::onSelectionDone()
460 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
462 if (aList.Extent() == 1) {
463 Handle(SALOME_InteractiveObject) anIO = aList.First();
464 myMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
467 if (SMESH::GetNameOfSelectedNodes(mySelector,anIO,aText) == 1) {
468 if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()) {
469 if(const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
471 myId->setText(aText);
472 myX->SetValue(aNode->X());
473 myY->SetValue(aNode->Y());
474 myZ->SetValue(aNode->Z());
476 erasePreview(); // avoid overlapping of a selection and a preview
488 //=======================================================================
489 // name : SMESHGUI_MoveNodesDlg::onDeactivate
490 // Purpose : SLOT called when dialog must be deativated
491 //=======================================================================
492 void SMESHGUI_MoveNodesDlg::onDeactivate()
498 //=======================================================================
499 // name : SMESHGUI_MoveNodesDlg::enterEvent
500 // Purpose : Event filter
501 //=======================================================================
502 void SMESHGUI_MoveNodesDlg::enterEvent (QEvent*)
505 mySMESHGUI->EmitSignalDeactivateDialog();
507 // set selection mode
508 SMESH::SetPointRepresentation(true);
509 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
510 aViewWindow->SetSelectionMode(NodeSelection);
518 //=======================================================================
519 // name : SMESHGUI_MoveNodesDlg::closeEvent
521 //=======================================================================
522 void SMESHGUI_MoveNodesDlg::closeEvent (QCloseEvent*)
525 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
526 aViewWindow->Repaint();
529 //=======================================================================
530 // name : SMESHGUI_MoveNodesDlg::hideEvent
531 // Purpose : may be caused by ESC key
532 //=======================================================================
533 void SMESHGUI_MoveNodesDlg::hideEvent (QHideEvent*)
539 //=======================================================================
540 // name : SMESHGUI_MoveNodesDlg::updateButtons
541 // Purpose : Update buttons state
542 //=======================================================================
543 void SMESHGUI_MoveNodesDlg::updateButtons()
545 bool isEnabled = isValid(false);
546 myOkBtn->setEnabled(isEnabled);
547 myApplyBtn->setEnabled(isEnabled);
550 //=======================================================================
551 // name : SMESHGUI_MoveNodesDlg::erasePreview
552 // Purpose : Erase preview
553 //=======================================================================
554 void SMESHGUI_MoveNodesDlg::erasePreview()
556 if (myPreviewActor == 0)
559 SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
561 aViewWindow->RemoveActor(myPreviewActor);
562 myPreviewActor->Delete();
565 aViewWindow->Repaint();
568 //=======================================================================
569 // name : SMESHGUI_MoveNodesDlg::redisplayPreview
570 // Purpose : Redisplay preview
571 //=======================================================================
572 void SMESHGUI_MoveNodesDlg::redisplayPreview()
577 if (myPreviewActor != 0)
583 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
585 vtkPoints* aPoints = vtkPoints::New();
586 aPoints->SetNumberOfPoints(1);
587 aPoints->SetPoint(0, myX->GetValue(), myY->GetValue(), myZ->GetValue());
591 vtkIdList *anIdList = vtkIdList::New();
592 anIdList->SetNumberOfIds(1);
594 vtkCellArray *aCells = vtkCellArray::New();
595 aCells->Allocate(2, 0);
597 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
598 aCellTypesArray->SetNumberOfComponents(1);
599 aCellTypesArray->Allocate(1);
601 anIdList->SetId(0, 0);
602 aCells->InsertNextCell(anIdList);
603 aCellTypesArray->InsertNextValue(VTK_VERTEX);
606 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
607 aCellLocationsArray->SetNumberOfComponents(1);
608 aCellLocationsArray->SetNumberOfTuples(1);
610 aCells->InitTraversal();
612 aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
614 aGrid->SetPoints(aPoints);
617 aGrid->SetCells(aCellTypesArray,aCellLocationsArray,aCells);
618 aCellLocationsArray->Delete();
619 aCellTypesArray->Delete();
622 // Create and display actor
623 vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
624 aMapper->SetInput(aGrid);
627 myPreviewActor = SALOME_Actor::New();
628 myPreviewActor->PickableOff();
629 myPreviewActor->SetMapper(aMapper);
632 vtkProperty* aProp = vtkProperty::New();
633 aProp->SetRepresentationToWireframe();
634 aProp->SetColor(250, 0, 250);
635 aProp->SetPointSize(5);
636 myPreviewActor->SetProperty(aProp);
639 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
641 aViewWindow->AddActor(myPreviewActor);
642 aViewWindow->Repaint();
646 //=================================================================================
647 // function : keyPressEvent()
649 //=================================================================================
650 void SMESHGUI_MoveNodesDlg::keyPressEvent( QKeyEvent* e )
652 QDialog::keyPressEvent( e );
653 if ( e->isAccepted() )
656 if ( e->key() == Qt::Key_F1 ) {