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
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"
39 #include "SMDS_MeshNode.hxx"
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_Selector.h"
49 #include "SVTK_ViewModel.h"
50 #include "SVTK_ViewWindow.h"
51 #include "SALOME_ListIO.hxx"
53 #include "SVTK_ViewWindow.h"
54 #include "VTKViewer_CellLocationsArray.h"
56 #include "utilities.h"
59 #include <TColStd_MapOfInteger.hxx>
63 #include <vtkIdList.h>
64 #include <vtkCellArray.h>
65 #include <vtkUnsignedCharArray.h>
66 #include <vtkUnstructuredGrid.h>
67 #include <vtkDataSetMapper.h>
68 #include <vtkProperty.h>
71 #include <qgroupbox.h>
73 #include <qlineedit.h>
74 #include <qpushbutton.h>
75 #include <qradiobutton.h>
78 #include <qmessagebox.h>
79 #include <qbuttongroup.h>
82 #include <SALOMEconfig.h>
83 #include CORBA_SERVER_HEADER(SMESH_Mesh)
84 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
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 = "moving_nodes_page.html";
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 myApplyBtn = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
135 myCloseBtn = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
136 myOkBtn = new QPushButton(tr("SMESH_BUT_APPLY_AND_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 aYLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
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 aZLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
193 myZ = new SMESHGUI_SpinBox(aCoordGrp);
195 myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, DBL_DIGITS_DISPLAY);
196 myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, DBL_DIGITS_DISPLAY);
197 myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 25.0, DBL_DIGITS_DISPLAY);
199 QVBoxLayout* aLay = new QVBoxLayout(aFrame);
200 aLay->addWidget(aPixGrp);
201 aLay->addWidget(anIdGrp);
202 aLay->addWidget(aCoordGrp);
204 // connect signale and slots
205 connect(myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
206 connect(myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
207 connect(myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
208 connect(myId, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
213 //=======================================================================
214 // name : SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg
216 //=======================================================================
217 SMESHGUI_MoveNodesDlg::~SMESHGUI_MoveNodesDlg()
222 //=======================================================================
223 // name : SMESHGUI_MoveNodesDlg::Init
224 // Purpose : Init dialog fields
225 //=======================================================================
226 void SMESHGUI_MoveNodesDlg::Init()
232 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
234 // selection and SMESHGUI
235 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
236 connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
237 connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
244 // set selection mode
245 SMESH::SetPointRepresentation(true);
246 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
247 aViewWindow->SetSelectionMode(NodeSelection);
252 //=======================================================================
253 // name : SMESHGUI_MoveNodesDlg::isValid
254 // Purpose : Verify validity of entry information
255 //=======================================================================
256 bool SMESHGUI_MoveNodesDlg::isValid (const bool theMess) const
258 if (myId->text().isEmpty()) {
260 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
261 tr("NODE_ID_IS_NOT_DEFINED"), QMessageBox::Ok);
267 //=======================================================================
268 // name : SMESHGUI_MoveNodesDlg::reset
269 // Purpose : Reset the dialog state
270 //=======================================================================
271 void SMESHGUI_MoveNodesDlg::reset()
281 //=======================================================================
282 // name : SMESHGUI_MoveNodesDlg::onApply
283 // Purpose : SLOT called when "Apply" button pressed.
284 //=======================================================================
285 bool SMESHGUI_MoveNodesDlg::onApply()
287 if (mySMESHGUI->isActiveStudyLocked())
293 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
294 if (aMesh->_is_nil()) {
295 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
296 tr("SMESHG_NO_MESH"), QMessageBox::Ok);
300 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
301 if (aMeshEditor->_is_nil())
304 int anId = myId->text().toInt();
305 bool aResult = false;
307 aResult = aMeshEditor->MoveNode(anId, myX->GetValue(), myY->GetValue(), myZ->GetValue());
313 aList.Append(myMeshActor->getIO());
314 mySelectionMgr->setSelectedObjects(aList,false);
322 //=======================================================================
323 // name : SMESHGUI_MoveNodesDlg::onOk
324 // Purpose : SLOT called when "Ok" button pressed.
325 //=======================================================================
326 void SMESHGUI_MoveNodesDlg::onOk()
332 //=======================================================================
333 // name : SMESHGUI_MoveNodesDlg::onClose
334 // Purpose : SLOT called when "Close" button pressed. Close dialog
335 //=======================================================================
336 void SMESHGUI_MoveNodesDlg::onClose()
338 //mySelectionMgr->clearSelected();
339 SMESH::SetPointRepresentation(false);
340 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
341 aViewWindow->SetSelectionMode(ActorSelection);
342 disconnect(mySelectionMgr, 0, this, 0);
343 disconnect(mySMESHGUI, 0, this, 0);
345 mySMESHGUI->ResetState();
349 //=================================================================================
350 // function : onHelp()
352 //=================================================================================
353 void SMESHGUI_MoveNodesDlg::onHelp()
355 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
357 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
361 platform = "winapplication";
363 platform = "application";
365 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
366 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
367 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
368 QObject::tr("BUT_OK"));
372 //=======================================================================
373 // name : SMESHGUI_MoveNodesDlg::onTextChange
375 //=======================================================================
376 void SMESHGUI_MoveNodesDlg::onTextChange (const QString& theNewText)
380 myOkBtn->setEnabled(false);
381 myApplyBtn->setEnabled(false);
384 // select entered node
386 if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()){
388 Handle(SALOME_InteractiveObject) anIO = myMeshActor->getIO();
391 mySelectionMgr->setSelectedObjects(aList,false);
394 if(const SMDS_MeshElement *anElem = aMesh->FindElement(theNewText.toInt())) {
395 TColStd_MapOfInteger aListInd;
396 aListInd.Add(anElem->GetID());
397 mySelector->AddOrRemoveIndex(anIO,aListInd, false);
398 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
399 aViewWindow->highlight(anIO,true,true);
407 //=======================================================================
408 // name : SMESHGUI_MoveNodesDlg::onSelectionDone
409 // Purpose : SLOT called when selection changed
410 //=======================================================================
411 void SMESHGUI_MoveNodesDlg::onSelectionDone()
417 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
419 if (aList.Extent() == 1) {
420 Handle(SALOME_InteractiveObject) anIO = aList.First();
421 myMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
424 if (SMESH::GetNameOfSelectedNodes(mySelector,anIO,aText) == 1) {
425 if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()) {
426 if(const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
428 myId->setText(aText);
429 myX->SetValue(aNode->X());
430 myY->SetValue(aNode->Y());
431 myZ->SetValue(aNode->Z());
433 erasePreview(); // avoid overlapping of a selection and a preview
445 //=======================================================================
446 // name : SMESHGUI_MoveNodesDlg::onDeactivate
447 // Purpose : SLOT called when dialog must be deativated
448 //=======================================================================
449 void SMESHGUI_MoveNodesDlg::onDeactivate()
455 //=======================================================================
456 // name : SMESHGUI_MoveNodesDlg::enterEvent
457 // Purpose : Event filter
458 //=======================================================================
459 void SMESHGUI_MoveNodesDlg::enterEvent (QEvent*)
462 mySMESHGUI->EmitSignalDeactivateDialog();
464 // set selection mode
465 SMESH::SetPointRepresentation(true);
466 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
467 aViewWindow->SetSelectionMode(NodeSelection);
475 //=======================================================================
476 // name : SMESHGUI_MoveNodesDlg::closeEvent
478 //=======================================================================
479 void SMESHGUI_MoveNodesDlg::closeEvent (QCloseEvent*)
482 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
483 aViewWindow->Repaint();
486 //=======================================================================
487 // name : SMESHGUI_MoveNodesDlg::hideEvent
488 // Purpose : may be caused by ESC key
489 //=======================================================================
490 void SMESHGUI_MoveNodesDlg::hideEvent (QHideEvent*)
496 //=======================================================================
497 // name : SMESHGUI_MoveNodesDlg::updateButtons
498 // Purpose : Update buttons state
499 //=======================================================================
500 void SMESHGUI_MoveNodesDlg::updateButtons()
502 bool isEnabled = isValid(false);
503 myOkBtn->setEnabled(isEnabled);
504 myApplyBtn->setEnabled(isEnabled);
507 //=======================================================================
508 // name : SMESHGUI_MoveNodesDlg::erasePreview
509 // Purpose : Erase preview
510 //=======================================================================
511 void SMESHGUI_MoveNodesDlg::erasePreview()
513 if (myPreviewActor == 0)
516 SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
518 aViewWindow->RemoveActor(myPreviewActor);
519 myPreviewActor->Delete();
522 aViewWindow->Repaint();
525 //=======================================================================
526 // name : SMESHGUI_MoveNodesDlg::redisplayPreview
527 // Purpose : Redisplay preview
528 //=======================================================================
529 void SMESHGUI_MoveNodesDlg::redisplayPreview()
534 if (myPreviewActor != 0)
540 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
542 vtkPoints* aPoints = vtkPoints::New();
543 aPoints->SetNumberOfPoints(1);
544 aPoints->SetPoint(0, myX->GetValue(), myY->GetValue(), myZ->GetValue());
548 vtkIdList *anIdList = vtkIdList::New();
549 anIdList->SetNumberOfIds(1);
551 vtkCellArray *aCells = vtkCellArray::New();
552 aCells->Allocate(2, 0);
554 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
555 aCellTypesArray->SetNumberOfComponents(1);
556 aCellTypesArray->Allocate(1);
558 anIdList->SetId(0, 0);
559 aCells->InsertNextCell(anIdList);
560 aCellTypesArray->InsertNextValue(VTK_VERTEX);
563 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
564 aCellLocationsArray->SetNumberOfComponents(1);
565 aCellLocationsArray->SetNumberOfTuples(1);
567 aCells->InitTraversal();
569 aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
571 aGrid->SetPoints(aPoints);
574 aGrid->SetCells(aCellTypesArray,aCellLocationsArray,aCells);
575 aCellLocationsArray->Delete();
576 aCellTypesArray->Delete();
579 // Create and display actor
580 vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
581 aMapper->SetInput(aGrid);
584 myPreviewActor = SALOME_Actor::New();
585 myPreviewActor->PickableOff();
586 myPreviewActor->SetMapper(aMapper);
589 vtkProperty* aProp = vtkProperty::New();
590 aProp->SetRepresentationToWireframe();
591 aProp->SetColor(250, 0, 250);
592 aProp->SetPointSize(5);
593 myPreviewActor->SetProperty(aProp);
596 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
598 aViewWindow->AddActor(myPreviewActor);
599 aViewWindow->Repaint();
603 //=================================================================================
604 // function : keyPressEvent()
606 //=================================================================================
607 void SMESHGUI_MoveNodesDlg::keyPressEvent( QKeyEvent* e )
609 QDialog::keyPressEvent( e );
610 if ( e->isAccepted() )
613 if ( e->key() == Key_F1 )