1 // Copyright (C) 2007-2011 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
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File : SMESHGUI_MoveNodesDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
28 #include "SMESHGUI_MoveNodesDlg.h"
31 #include "SMESHGUI_SpinBox.h"
32 #include "SMESHGUI_IdValidator.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_VTKUtils.h"
35 #include "SMESHGUI_MeshUtils.h"
37 #include <SMESH_Actor.h>
38 #include <SMDS_Mesh.hxx>
40 // SALOME GUI includes
41 #include <LightApp_SelectionMgr.h>
42 #include <LightApp_Application.h>
43 #include <SUIT_ResourceMgr.h>
44 #include <SUIT_Desktop.h>
45 #include <SUIT_Session.h>
46 #include <SUIT_MessageBox.h>
48 #include <SVTK_ViewModel.h>
49 #include <SVTK_ViewWindow.h>
50 #include <SALOME_ListIO.hxx>
52 #include <VTKViewer_CellLocationsArray.h>
55 #include <TColStd_MapOfInteger.hxx>
58 #include <vtkIdList.h>
59 #include <vtkCellArray.h>
60 #include <vtkUnsignedCharArray.h>
61 #include <vtkUnstructuredGrid.h>
62 #include <vtkDataSetMapper.h>
63 #include <vtkProperty.h>
69 #include <QPushButton>
70 #include <QRadioButton>
71 #include <QHBoxLayout>
72 #include <QVBoxLayout>
74 #include <QButtonGroup>
77 #include <SALOMEconfig.h>
78 #include CORBA_SERVER_HEADER(SMESH_Mesh)
79 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
84 //=================================================================================
85 // name : SMESHGUI_MoveNodesDlg::SMESHGUI_MoveNodesDlg
87 //=================================================================================
88 SMESHGUI_MoveNodesDlg::SMESHGUI_MoveNodesDlg(SMESHGUI* theModule):
89 QDialog(SMESH::GetDesktop(theModule)),
90 mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
97 setWindowTitle(tr("CAPTION"));
99 QVBoxLayout* aDlgLay = new QVBoxLayout(this);
100 aDlgLay->setSpacing(SPACING);
101 aDlgLay->setMargin(MARGIN);
103 QWidget* aMainFrame = createMainFrame (this);
104 QWidget* aBtnFrame = createButtonFrame(this);
106 aDlgLay->addWidget(aMainFrame);
107 aDlgLay->addWidget(aBtnFrame);
109 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
111 myHelpFileName = "moving_nodes_page.html";
116 //=======================================================================
117 // name : SMESHGUI_MoveNodesDlg::createButtonFrame
118 // Purpose : Create frame containing buttons
119 //=======================================================================
120 QWidget* SMESHGUI_MoveNodesDlg::createButtonFrame (QWidget* theParent)
122 QFrame* aFrame = new QFrame(theParent);
123 aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
125 myOkBtn = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aFrame);
126 myApplyBtn = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
127 myCloseBtn = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
128 myHelpBtn = new QPushButton(tr("SMESH_BUT_HELP"), aFrame);
130 QHBoxLayout* aLay = new QHBoxLayout(aFrame);
131 aLay->setSpacing(SPACING);
132 aLay->setMargin(MARGIN);
134 aLay->addWidget(myOkBtn);
135 aLay->addSpacing(10);
136 aLay->addWidget(myApplyBtn);
137 aLay->addSpacing(10);
139 aLay->addWidget(myCloseBtn);
140 aLay->addWidget(myHelpBtn);
142 connect(myOkBtn, SIGNAL(clicked()), SLOT(onOk()));
143 connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
144 connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
145 connect(myHelpBtn, SIGNAL(clicked()), SLOT(onHelp()));
150 //=======================================================================
151 // name : SMESHGUI_MoveNodesDlg::createMainFrame
152 // Purpose : Create frame containing dialog's input fields
153 //=======================================================================
154 QWidget* SMESHGUI_MoveNodesDlg::createMainFrame (QWidget* theParent)
156 QWidget* aFrame = new QWidget(theParent);
158 QPixmap iconMoveNode (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_MOVE_NODE")));
159 QPixmap iconSelect (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
161 //------------------------------------------------------------
162 QGroupBox* aPixGrp = new QGroupBox(tr("MESH_NODE"), aFrame);
163 QButtonGroup* aBtnGrp = new QButtonGroup(this);
164 QHBoxLayout* aPixGrpLayout = new QHBoxLayout(aPixGrp);
165 aPixGrpLayout->setSpacing(SPACING);
166 aPixGrpLayout->setMargin(MARGIN);
168 QRadioButton* aRBut = new QRadioButton(aPixGrp);
169 aRBut->setIcon(iconMoveNode);
170 aRBut->setChecked(true);
172 aPixGrpLayout->addWidget(aRBut);
173 aBtnGrp->addButton(aRBut, 0);
175 //------------------------------------------------------------
176 QGroupBox* anIdGrp = new QGroupBox(tr("SMESH_MOVE"), aFrame);
177 QHBoxLayout* anIdGrpLayout = new QHBoxLayout(anIdGrp);
178 anIdGrpLayout->setSpacing(SPACING);
179 anIdGrpLayout->setMargin(MARGIN);
181 QLabel* idLabl = new QLabel(tr("NODE_ID"), anIdGrp);
182 QPushButton* idBtn = new QPushButton(anIdGrp);
183 idBtn->setIcon(iconSelect);
184 myId = new QLineEdit(anIdGrp);
185 myId->setValidator(new SMESHGUI_IdValidator(this, 1));
187 anIdGrpLayout->addWidget(idLabl);
188 anIdGrpLayout->addWidget(idBtn);
189 anIdGrpLayout->addWidget(myId);
191 //------------------------------------------------------------
192 QGroupBox* aCoordGrp = new QGroupBox(tr("SMESH_COORDINATES"), aFrame);
193 QHBoxLayout* aCoordGrpLayout = new QHBoxLayout(aCoordGrp);
194 aCoordGrpLayout->setSpacing(SPACING);
195 aCoordGrpLayout->setMargin(MARGIN);
197 QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
198 myX = new SMESHGUI_SpinBox(aCoordGrp);
200 QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
201 myY = new SMESHGUI_SpinBox(aCoordGrp);
203 QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
204 myZ = new SMESHGUI_SpinBox(aCoordGrp);
206 aCoordGrpLayout->addWidget(aXLabel);
207 aCoordGrpLayout->addWidget(myX);
208 aCoordGrpLayout->addWidget(aYLabel);
209 aCoordGrpLayout->addWidget(myY);
210 aCoordGrpLayout->addWidget(aZLabel);
211 aCoordGrpLayout->addWidget(myZ);
213 //------------------------------------------------------------
214 myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, "length_precision");
215 myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, "length_precision");
216 myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, "length_precision");
218 //------------------------------------------------------------
219 QVBoxLayout* aLay = new QVBoxLayout(aFrame);
221 aLay->setMargin(SPACING);
222 aLay->addWidget(aPixGrp);
223 aLay->addWidget(anIdGrp);
224 aLay->addWidget(aCoordGrp);
226 //------------------------------------------------------------
227 // connect signale and slots
228 connect(myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
229 connect(myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
230 connect(myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
231 connect(myId, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
236 //=======================================================================
237 // name : SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg
239 //=======================================================================
240 SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg()
245 //=======================================================================
246 // name : SMESHGUI_MoveNodesDlg::Init
247 // Purpose : Init dialog fields
248 //=======================================================================
249 void SMESHGUI_MoveNodesDlg::Init()
255 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
257 // selection and SMESHGUI
258 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
259 connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
260 connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
265 // set selection mode
266 SMESH::SetPointRepresentation(true);
267 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
268 aViewWindow->SetSelectionMode(NodeSelection);
273 //=======================================================================
274 // name : SMESHGUI_MoveNodesDlg::isValid
275 // Purpose : Verify validity of entry information
276 //=======================================================================
277 bool SMESHGUI_MoveNodesDlg::isValid (const bool theMess)
279 if (myId->text().isEmpty()) {
281 SUIT_MessageBox::information(this, tr("SMESH_WARNING"),
282 tr("NODE_ID_IS_NOT_DEFINED"));
288 ok = myX->isValid( msg, theMess ) && ok;
289 ok = myY->isValid( msg, theMess ) && ok;
290 ok = myZ->isValid( msg, theMess ) && ok;
293 QString str( tr( "SMESH_INCORRECT_INPUT" ) );
294 if ( !msg.isEmpty() )
296 SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
304 //=======================================================================
305 // name : SMESHGUI_MoveNodesDlg::reset
306 // Purpose : Reset the dialog state
307 //=======================================================================
308 void SMESHGUI_MoveNodesDlg::reset()
318 //=======================================================================
319 // name : SMESHGUI_MoveNodesDlg::onApply
320 // Purpose : SLOT called when "Apply" button pressed.
321 //=======================================================================
322 bool SMESHGUI_MoveNodesDlg::onApply()
324 if (mySMESHGUI->isActiveStudyLocked())
330 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
331 if (aMesh->_is_nil()) {
332 SUIT_MessageBox::information(this, tr("SMESH_ERROR"),
333 tr("SMESHG_NO_MESH"));
337 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
338 if (aMeshEditor->_is_nil())
341 int anId = myId->text().toInt();
342 bool aResult = false;
344 aResult = aMeshEditor->MoveNode(anId, myX->GetValue(), myY->GetValue(), myZ->GetValue());
346 QStringList aParameters;
347 aParameters << myX->text();
348 aParameters << myY->text();
349 aParameters << myZ->text();
350 aMesh->SetParameters( aParameters.join(":").toLatin1().constData() );
356 aList.Append(myMeshActor->getIO());
357 mySelectionMgr->setSelectedObjects(aList,false);
359 SMESHGUI::Modified();
366 //=======================================================================
367 // name : SMESHGUI_MoveNodesDlg::onOk
368 // Purpose : SLOT called when "Ok" button pressed.
369 //=======================================================================
370 void SMESHGUI_MoveNodesDlg::onOk()
376 //=======================================================================
377 // name : SMESHGUI_MoveNodesDlg::onClose
378 // Purpose : SLOT called when "Close" button pressed. Close dialog
379 //=======================================================================
380 void SMESHGUI_MoveNodesDlg::onClose()
382 //mySelectionMgr->clearSelected();
383 SMESH::SetPointRepresentation(false);
384 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
385 aViewWindow->SetSelectionMode(ActorSelection);
386 disconnect(mySelectionMgr, 0, this, 0);
387 disconnect(mySMESHGUI, 0, this, 0);
389 mySMESHGUI->ResetState();
393 //=================================================================================
394 // function : onHelp()
396 //=================================================================================
397 void SMESHGUI_MoveNodesDlg::onHelp()
399 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
401 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
405 platform = "winapplication";
407 platform = "application";
409 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
410 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
411 arg(app->resourceMgr()->stringValue("ExternalBrowser",
413 arg(myHelpFileName));
417 //=======================================================================
418 // name : SMESHGUI_MoveNodesDlg::onTextChange
420 //=======================================================================
421 void SMESHGUI_MoveNodesDlg::onTextChange (const QString& theNewText)
425 myOkBtn->setEnabled(false);
426 myApplyBtn->setEnabled(false);
429 // select entered node
431 if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()){
433 Handle(SALOME_InteractiveObject) anIO = myMeshActor->getIO();
436 mySelectionMgr->setSelectedObjects(aList,false);
439 if(const SMDS_MeshElement *anElem = aMesh->FindElement(theNewText.toInt())) {
440 TColStd_MapOfInteger aListInd;
441 aListInd.Add(anElem->GetID());
442 mySelector->AddOrRemoveIndex(anIO,aListInd, false);
443 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
444 aViewWindow->highlight(anIO,true,true);
452 //=======================================================================
453 // name : SMESHGUI_MoveNodesDlg::onSelectionDone
454 // Purpose : SLOT called when selection changed
455 //=======================================================================
456 void SMESHGUI_MoveNodesDlg::onSelectionDone()
462 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
464 if (aList.Extent() == 1) {
465 Handle(SALOME_InteractiveObject) anIO = aList.First();
466 myMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
469 if (SMESH::GetNameOfSelectedNodes(mySelector,anIO,aText) == 1) {
470 if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()) {
471 if(const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
473 myId->setText(aText);
474 myX->SetValue(aNode->X());
475 myY->SetValue(aNode->Y());
476 myZ->SetValue(aNode->Z());
478 erasePreview(); // avoid overlapping of a selection and a preview
490 //=======================================================================
491 // name : SMESHGUI_MoveNodesDlg::onDeactivate
492 // Purpose : SLOT called when dialog must be deativated
493 //=======================================================================
494 void SMESHGUI_MoveNodesDlg::onDeactivate()
500 //=======================================================================
501 // name : SMESHGUI_MoveNodesDlg::enterEvent
502 // Purpose : Event filter
503 //=======================================================================
504 void SMESHGUI_MoveNodesDlg::enterEvent (QEvent*)
507 mySMESHGUI->EmitSignalDeactivateDialog();
509 // set selection mode
510 SMESH::SetPointRepresentation(true);
511 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
512 aViewWindow->SetSelectionMode(NodeSelection);
520 //=======================================================================
521 // name : SMESHGUI_MoveNodesDlg::closeEvent
523 //=======================================================================
524 void SMESHGUI_MoveNodesDlg::closeEvent (QCloseEvent*)
527 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
528 aViewWindow->Repaint();
531 //=======================================================================
532 // name : SMESHGUI_MoveNodesDlg::hideEvent
533 // Purpose : may be caused by ESC key
534 //=======================================================================
535 void SMESHGUI_MoveNodesDlg::hideEvent (QHideEvent*)
541 //=======================================================================
542 // name : SMESHGUI_MoveNodesDlg::updateButtons
543 // Purpose : Update buttons state
544 //=======================================================================
545 void SMESHGUI_MoveNodesDlg::updateButtons()
547 bool isEnabled = isValid(false);
548 myOkBtn->setEnabled(isEnabled);
549 myApplyBtn->setEnabled(isEnabled);
552 //=======================================================================
553 // name : SMESHGUI_MoveNodesDlg::erasePreview
554 // Purpose : Erase preview
555 //=======================================================================
556 void SMESHGUI_MoveNodesDlg::erasePreview()
558 if (myPreviewActor == 0)
561 SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
563 aViewWindow->RemoveActor(myPreviewActor);
564 myPreviewActor->Delete();
567 aViewWindow->Repaint();
570 //=======================================================================
571 // name : SMESHGUI_MoveNodesDlg::redisplayPreview
572 // Purpose : Redisplay preview
573 //=======================================================================
574 void SMESHGUI_MoveNodesDlg::redisplayPreview()
579 if (myPreviewActor != 0)
585 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
587 vtkPoints* aPoints = vtkPoints::New();
588 aPoints->SetNumberOfPoints(1);
589 aPoints->SetPoint(0, myX->GetValue(), myY->GetValue(), myZ->GetValue());
593 vtkIdList *anIdList = vtkIdList::New();
594 anIdList->SetNumberOfIds(1);
596 vtkCellArray *aCells = vtkCellArray::New();
597 aCells->Allocate(2, 0);
599 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
600 aCellTypesArray->SetNumberOfComponents(1);
601 aCellTypesArray->Allocate(1);
603 anIdList->SetId(0, 0);
604 aCells->InsertNextCell(anIdList);
605 aCellTypesArray->InsertNextValue(VTK_VERTEX);
608 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
609 aCellLocationsArray->SetNumberOfComponents(1);
610 aCellLocationsArray->SetNumberOfTuples(1);
612 aCells->InitTraversal();
614 aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
616 aGrid->SetPoints(aPoints);
619 aGrid->SetCells(aCellTypesArray,aCellLocationsArray,aCells);
620 aCellLocationsArray->Delete();
621 aCellTypesArray->Delete();
624 // Create and display actor
625 vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
626 aMapper->SetInput(aGrid);
629 myPreviewActor = SALOME_Actor::New();
630 myPreviewActor->PickableOff();
631 myPreviewActor->SetMapper(aMapper);
634 vtkProperty* aProp = vtkProperty::New();
635 aProp->SetRepresentationToWireframe();
636 aProp->SetColor(250, 0, 250);
637 aProp->SetPointSize(5);
638 myPreviewActor->SetProperty(aProp);
641 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
643 aViewWindow->AddActor(myPreviewActor);
644 aViewWindow->Repaint();
648 //=================================================================================
649 // function : keyPressEvent()
651 //=================================================================================
652 void SMESHGUI_MoveNodesDlg::keyPressEvent( QKeyEvent* e )
654 QDialog::keyPressEvent( e );
655 if ( e->isAccepted() )
658 if ( e->key() == Qt::Key_F1 ) {