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_NodesDlg.cxx
25 // Author : Nicolas REJNERI
29 #include "SMESHGUI_NodesDlg.h"
32 #include "SMESHGUI_SpinBox.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_VTKUtils.h"
35 #include "SMESHGUI_MeshUtils.h"
37 #include "SMESH_Actor.h"
38 #include "SMESH_ActorUtils.h"
39 #include "SMESH_ObjectDef.h"
41 #include "SMDS_Mesh.hxx"
42 #include "SMDS_MeshNode.hxx"
44 #include "SUIT_Session.h"
45 #include "SUIT_OverrideCursor.h"
46 #include "SUIT_ViewWindow.h"
47 #include "SUIT_ViewManager.h"
48 #include "SUIT_MessageBox.h"
49 #include "SUIT_Desktop.h"
51 #include "SalomeApp_Study.h"
52 #include "LightApp_Application.h"
53 #include "LightApp_SelectionMgr.h"
55 #include "SVTK_Selector.h"
56 #include "SVTK_ViewWindow.h"
57 #include "VTKViewer_CellLocationsArray.h"
59 #include "SALOME_Actor.h"
60 #include "SALOME_ListIO.hxx"
62 #include "utilities.h"
66 #include <vtkIdList.h>
67 #include <vtkCellArray.h>
68 #include <vtkUnsignedCharArray.h>
69 #include <vtkUnstructuredGrid.h>
70 #include <vtkDataSetMapper.h>
71 #include <vtkActorCollection.h>
72 #include <vtkRenderer.h>
73 #include <vtkProperty.h>
76 #include <qbuttongroup.h>
78 #include <qgroupbox.h>
80 #include <qlineedit.h>
81 #include <qpushbutton.h>
82 #include <qradiobutton.h>
86 #include <qwhatsthis.h>
89 #include <qvalidator.h>
92 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
99 void AddNode (SMESH::SMESH_Mesh_ptr theMesh, float x, float y, float z)
101 SUIT_OverrideCursor wc;
103 _PTR(SObject) aSobj = SMESH::FindSObject(theMesh);
104 SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
105 aMeshEditor->AddNode(x,y,z);
106 _PTR(Study) aStudy = GetActiveStudyDocument();
107 CORBA::Long anId = aStudy->StudyId();
108 if (TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId, aSobj->GetID().c_str())) {
109 aVisualObj->Update(true);
111 } catch (SALOME::SALOME_Exception& exc) {
112 INFOS("Follow exception was cought:\n\t" << exc.details.text);
113 } catch (const std::exception& exc) {
114 INFOS("Follow exception was cought:\n\t" << exc.what());
116 INFOS("Unknown exception was cought !!!");
120 class TNodeSimulation {
121 SVTK_ViewWindow* myViewWindow;
123 SALOME_Actor *myPreviewActor;
124 vtkDataSetMapper* myMapper;
128 TNodeSimulation(SVTK_ViewWindow* theViewWindow):
129 myViewWindow(theViewWindow)
131 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
134 myPoints = vtkPoints::New();
135 myPoints->SetNumberOfPoints(1);
136 myPoints->SetPoint(0,0.0,0.0,0.0);
139 vtkIdList *anIdList = vtkIdList::New();
140 anIdList->SetNumberOfIds(1);
142 vtkCellArray *aCells = vtkCellArray::New();
143 aCells->Allocate(2, 0);
145 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
146 aCellTypesArray->SetNumberOfComponents(1);
147 aCellTypesArray->Allocate(1);
149 anIdList->SetId(0, 0);
150 aCells->InsertNextCell(anIdList);
151 aCellTypesArray->InsertNextValue(VTK_VERTEX);
153 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
154 aCellLocationsArray->SetNumberOfComponents(1);
155 aCellLocationsArray->SetNumberOfTuples(1);
157 aCells->InitTraversal();
159 aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
161 aGrid->SetCells(aCellTypesArray, aCellLocationsArray, aCells);
163 aGrid->SetPoints(myPoints);
164 aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aCells);
165 aCellLocationsArray->Delete();
166 aCellTypesArray->Delete();
170 // Create and display actor
171 myMapper = vtkDataSetMapper::New();
172 myMapper->SetInput(aGrid);
175 myPreviewActor = SALOME_Actor::New();
176 myPreviewActor->SetInfinitive(true);
177 myPreviewActor->VisibilityOff();
178 myPreviewActor->PickableOff();
179 myPreviewActor->SetMapper(myMapper);
181 vtkProperty* aProp = vtkProperty::New();
182 aProp->SetRepresentationToPoints();
184 vtkFloatingPointType anRGB[3];
185 GetColor( "SMESH", "node_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 255, 0 ) );
186 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
188 vtkFloatingPointType aPointSize = GetFloat( "SMESH:node_size", 3 );
189 aProp->SetPointSize( aPointSize );
191 myPreviewActor->SetProperty( aProp );
194 myViewWindow->AddActor(myPreviewActor);
197 void SetPosition (float x, float y, float z)
199 myPoints->SetPoint(0,x,y,z);
200 myPoints->Modified();
204 void SetVisibility (bool theVisibility)
206 myPreviewActor->SetVisibility(theVisibility);
207 RepaintCurrentView();
212 myViewWindow->RemoveActor(myPreviewActor);
213 myPreviewActor->Delete();
215 myMapper->RemoveAllInputs();
223 //=================================================================================
224 // class : SMESHGUI_NodesDlg()
226 //=================================================================================
227 SMESHGUI_NodesDlg::SMESHGUI_NodesDlg (SMESHGUI* theModule,
231 QDialog(SMESH::GetDesktop(theModule),
234 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),
235 mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
236 mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
237 mySMESHGUI(theModule)
239 mySimulation = new SMESH::TNodeSimulation(SMESH::GetViewWindow( mySMESHGUI ));
241 QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_NODE")));
243 setName("SMESHGUI_NodesDlg");
245 setCaption(tr("MESH_NODE_TITLE"));
246 setSizeGripEnabled(TRUE);
247 SMESHGUI_NodesDlgLayout = new QGridLayout(this);
248 SMESHGUI_NodesDlgLayout->setSpacing(6);
249 SMESHGUI_NodesDlgLayout->setMargin(11);
251 /***************************************************************/
252 GroupButtons = new QGroupBox(this, "GroupButtons");
253 GroupButtons->setGeometry(QRect(10, 10, 281, 48));
254 GroupButtons->setTitle(tr("" ));
255 GroupButtons->setColumnLayout(0, Qt::Vertical);
256 GroupButtons->layout()->setSpacing(0);
257 GroupButtons->layout()->setMargin(0);
258 GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
259 GroupButtonsLayout->setAlignment(Qt::AlignTop);
260 GroupButtonsLayout->setSpacing(6);
261 GroupButtonsLayout->setMargin(11);
262 buttonHelp = new QPushButton(GroupButtons, "buttonHelp");
263 buttonHelp->setText(tr("SMESH_BUT_HELP" ));
264 buttonHelp->setAutoDefault(TRUE);
265 GroupButtonsLayout->addWidget(buttonHelp, 0, 4);
266 buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
267 buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
268 buttonCancel->setAutoDefault(TRUE);
269 GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
270 buttonApply = new QPushButton(GroupButtons, "buttonApply");
271 buttonApply->setText(tr("SMESH_BUT_APPLY" ));
272 buttonApply->setAutoDefault(TRUE);
273 GroupButtonsLayout->addWidget(buttonApply, 0, 1);
274 QSpacerItem* spacer_9 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
275 GroupButtonsLayout->addItem(spacer_9, 0, 2);
276 buttonOk = new QPushButton(GroupButtons, "buttonOk");
277 buttonOk->setText(tr("SMESH_BUT_OK" ));
278 buttonOk->setAutoDefault(TRUE);
279 buttonOk->setDefault(TRUE);
280 GroupButtonsLayout->addWidget(buttonOk, 0, 0);
281 SMESHGUI_NodesDlgLayout->addWidget(GroupButtons, 2, 0);
283 /***************************************************************/
284 GroupConstructors = new QButtonGroup(this, "GroupConstructors");
285 GroupConstructors->setTitle(tr("MESH_NODE" ));
286 GroupConstructors->setExclusive(TRUE);
287 GroupConstructors->setColumnLayout(0, Qt::Vertical);
288 GroupConstructors->layout()->setSpacing(0);
289 GroupConstructors->layout()->setMargin(0);
290 GroupConstructorsLayout = new QGridLayout(GroupConstructors->layout());
291 GroupConstructorsLayout->setAlignment(Qt::AlignTop);
292 GroupConstructorsLayout->setSpacing(6);
293 GroupConstructorsLayout->setMargin(11);
294 Constructor1 = new QRadioButton(GroupConstructors, "Constructor1");
295 Constructor1->setText(tr("" ));
296 Constructor1->setPixmap(image0);
297 Constructor1->setChecked(TRUE);
298 GroupConstructorsLayout->addWidget(Constructor1, 0, 0);
299 QSpacerItem* spacer_2 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
300 GroupConstructorsLayout->addItem(spacer_2, 0, 1);
301 SMESHGUI_NodesDlgLayout->addWidget(GroupConstructors, 0, 0);
303 /***************************************************************/
304 GroupCoordinates = new QGroupBox(this, "GroupCoordinates");
305 GroupCoordinates->setTitle(tr("SMESH_COORDINATES" ));
306 GroupCoordinates->setColumnLayout(0, Qt::Vertical);
307 GroupCoordinates->layout()->setSpacing(0);
308 GroupCoordinates->layout()->setMargin(0);
309 GroupCoordinatesLayout = new QGridLayout(GroupCoordinates->layout());
310 GroupCoordinatesLayout->setAlignment(Qt::AlignTop);
311 GroupCoordinatesLayout->setSpacing(6);
312 GroupCoordinatesLayout->setMargin(11);
313 TextLabel_X = new QLabel(GroupCoordinates, "TextLabel_X");
314 TextLabel_X->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
315 TextLabel_X->setText(tr("SMESH_X" ));
316 GroupCoordinatesLayout->addWidget(TextLabel_X, 0, 0);
317 TextLabel_Y = new QLabel(GroupCoordinates, "TextLabel_Y");
318 TextLabel_Y->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
319 TextLabel_Y->setText(tr("SMESH_Y" ));
320 GroupCoordinatesLayout->addWidget(TextLabel_Y, 0, 2);
322 TextLabel_Z = new QLabel(GroupCoordinates, "TextLabel_Z");
323 TextLabel_Z->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
324 TextLabel_Z->setText(tr("SMESH_Z" ));
325 GroupCoordinatesLayout->addWidget(TextLabel_Z, 0, 4);
327 SpinBox_X = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_X");
328 GroupCoordinatesLayout->addWidget(SpinBox_X, 0, 1);
330 SpinBox_Y = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_Y");
331 GroupCoordinatesLayout->addWidget(SpinBox_Y, 0, 3);
333 SpinBox_Z = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_Z");
334 GroupCoordinatesLayout->addWidget(SpinBox_Z, 0, 5);
336 SMESHGUI_NodesDlgLayout->addWidget(GroupCoordinates, 1, 0);
338 myHelpFileName = "/files/adding_nodes_and_elements.htm#Adding_nodes";
340 /* Initialisation and display */
344 //=======================================================================
345 // function : ~SMESHGUI_NodesDlg()
346 // purpose : Destructor
347 //=======================================================================
348 SMESHGUI_NodesDlg::~SMESHGUI_NodesDlg()
353 //=================================================================================
356 //=================================================================================
357 void SMESHGUI_NodesDlg::Init ()
359 /* Get setting of step value from file configuration */
361 // QString St = SUIT_CONFIG->getSetting("xxxxxxxxxxxxx"); TODO
362 // step = St.toDouble(); TODO
365 /* min, max, step and decimals for spin boxes */
366 SpinBox_X->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, 3);
367 SpinBox_Y->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, 3);
368 SpinBox_Z->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, 3);
369 SpinBox_X->SetValue(0.0);
370 SpinBox_Y->SetValue(0.0);
371 SpinBox_Z->SetValue(0.0);
373 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
375 /* signals and slots connections */
376 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
377 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
378 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
379 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
381 connect(SpinBox_X, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
382 connect(SpinBox_Y, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
383 connect(SpinBox_Z, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
385 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
386 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
387 /* to close dialog if study frame change */
388 connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
392 // set selection mode
393 SMESH::SetPointRepresentation(true);
394 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
395 aViewWindow->SetSelectionMode(NodeSelection);
397 SelectionIntoArgument();
400 //=================================================================================
401 // function : ValueChangedInSpinBox()
403 //=================================================================================
404 void SMESHGUI_NodesDlg::ValueChangedInSpinBox (double newValue)
406 if (!myMesh->_is_nil()) {
407 double vx = SpinBox_X->GetValue();
408 double vy = SpinBox_Y->GetValue();
409 double vz = SpinBox_Z->GetValue();
411 mySimulation->SetPosition(vx,vy,vz);
415 //=================================================================================
416 // function : ClickOnOk()
418 //=================================================================================
419 void SMESHGUI_NodesDlg::ClickOnOk()
425 //=================================================================================
426 // function : ClickOnApply()
428 //=================================================================================
429 bool SMESHGUI_NodesDlg::ClickOnApply()
431 if (mySMESHGUI->isActiveStudyLocked())
434 if (myMesh->_is_nil()) {
435 SUIT_MessageBox::warn1(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
436 tr("MESH_IS_NOT_SELECTED"), tr("SMESH_BUT_OK"));
440 /* Recup args and call method */
441 double x = SpinBox_X->GetValue();
442 double y = SpinBox_Y->GetValue();
443 double z = SpinBox_Z->GetValue();
444 mySimulation->SetVisibility(false);
445 SMESH::AddNode(myMesh,x,y,z);
446 SMESH::SetPointRepresentation(true);
450 mySelectionMgr->selectedObjects(aList);
451 if (aList.Extent() != 1) {
452 if (SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView()) {
453 vtkActorCollection *aCollection = aViewWindow->getRenderer()->GetActors();
454 aCollection->InitTraversal();
455 while (vtkActor *anAct = aCollection->GetNextActor()) {
456 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
457 if (anActor->hasIO()) {
458 if (SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>(anActor->GetObject().get())) {
459 if (myMesh->_is_equivalent(aMeshObj->GetMeshServer())) {
461 aList.Append(anActor->getIO());
462 mySelectionMgr->setSelectedObjects(aList, false);
474 //=================================================================================
475 // function : ClickOnCancel()
477 //=================================================================================
478 void SMESHGUI_NodesDlg::ClickOnCancel()
480 disconnect(mySelectionMgr, 0, this, 0);
481 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
482 aViewWindow->SetSelectionMode(ActorSelection);
484 mySimulation->SetVisibility(false);
485 SMESH::SetPointRepresentation(false);
486 mySMESHGUI->ResetState();
491 //=================================================================================
492 // function : ClickOnHelp()
494 //=================================================================================
495 void SMESHGUI_NodesDlg::ClickOnHelp()
497 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
499 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
501 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
502 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
503 arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
504 QObject::tr("BUT_OK"));
508 //=================================================================================
509 // function : SelectionIntoArgument()
510 // purpose : Called when selection as changed or other case
511 //=================================================================================
512 void SMESHGUI_NodesDlg::SelectionIntoArgument()
514 if (!GroupConstructors->isEnabled())
517 mySimulation->SetVisibility(false);
518 SMESH::SetPointRepresentation(true);
520 const SALOME_ListIO& aList = mySelector->StoredIObjects();
521 if (aList.Extent() == 1) {
522 Handle(SALOME_InteractiveObject) anIO = aList.First();
523 if (anIO->hasEntry()) {
524 myMesh = SMESH::GetMeshByIO(anIO);
525 if (myMesh->_is_nil()) return;
527 if (SMESH::GetNameOfSelectedNodes(mySelector,anIO,aText) == 1) {
528 if (SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh.in())) {
529 if (SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh()) {
530 if (const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
531 SpinBox_X->SetValue(aNode->X());
532 SpinBox_Y->SetValue(aNode->Y());
533 SpinBox_Z->SetValue(aNode->Z());
538 mySimulation->SetPosition(SpinBox_X->GetValue(),
539 SpinBox_Y->GetValue(),
540 SpinBox_Z->GetValue());
545 //=================================================================================
546 // function : closeEvent()
548 //=================================================================================
549 void SMESHGUI_NodesDlg::closeEvent (QCloseEvent*)
551 this->ClickOnCancel(); /* same than click on cancel button */
554 //=================================================================================
555 // function : hideEvent()
556 // purpose : caused by ESC key
557 //=================================================================================
558 void SMESHGUI_NodesDlg::hideEvent (QHideEvent*)
564 //=================================================================================
565 // function : enterEvent()
566 // purpose : to reactivate this dialog box when mouse enter onto the window
567 //=================================================================================
568 void SMESHGUI_NodesDlg::enterEvent(QEvent*)
570 if (!GroupConstructors->isEnabled())
571 ActivateThisDialog();
574 //=================================================================================
575 // function : DeactivateActiveDialog()
576 // purpose : public slot to deactivate if active
577 //=================================================================================
578 void SMESHGUI_NodesDlg::DeactivateActiveDialog()
580 if (GroupConstructors->isEnabled()) {
581 GroupConstructors->setEnabled(false);
582 GroupCoordinates->setEnabled(false);
583 GroupButtons->setEnabled(false);
584 mySimulation->SetVisibility(false);
585 mySMESHGUI->ResetState();
586 mySMESHGUI->SetActiveDialogBox(0);
590 //=================================================================================
591 // function : ActivateThisDialog()
593 //=================================================================================
594 void SMESHGUI_NodesDlg::ActivateThisDialog()
596 mySMESHGUI->EmitSignalDeactivateDialog();
597 GroupConstructors->setEnabled(true);
598 GroupCoordinates->setEnabled(true);
599 GroupButtons->setEnabled(true);
601 SMESH::SetPointRepresentation(true);
602 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
603 aViewWindow->SetSelectionMode(NodeSelection);
605 SelectionIntoArgument();
608 //=================================================================================
609 // function : keyPressEvent()
611 //=================================================================================
612 void SMESHGUI_NodesDlg::keyPressEvent( QKeyEvent* e )
614 QDialog::keyPressEvent( e );
615 if ( e->isAccepted() )
618 if ( e->key() == Key_F1 )