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_NodesDlg.cxx
24 // Author : Nicolas REJNERI
28 #include "SMESHGUI_NodesDlg.h"
31 #include "SMESHGUI_SpinBox.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34 #include "SMESHGUI_MeshUtils.h"
36 #include "SMESH_Actor.h"
37 #include "SMESH_ActorUtils.h"
38 #include "SMESH_ObjectDef.h"
40 #include "SMDS_Mesh.hxx"
41 #include "SMDS_MeshNode.hxx"
43 #include "SUIT_Session.h"
44 #include "SUIT_OverrideCursor.h"
45 #include "SUIT_ViewWindow.h"
46 #include "SUIT_ViewManager.h"
47 #include "SUIT_MessageBox.h"
48 #include "SUIT_Desktop.h"
50 #include "SalomeApp_Study.h"
51 #include "LightApp_Application.h"
52 #include "LightApp_SelectionMgr.h"
54 #include "SVTK_Selector.h"
55 #include "SVTK_ViewWindow.h"
56 #include "VTKViewer_CellLocationsArray.h"
58 #include "SALOME_Actor.h"
59 #include "SALOME_ListIO.hxx"
61 #include "utilities.h"
65 #include <vtkIdList.h>
66 #include <vtkCellArray.h>
67 #include <vtkUnsignedCharArray.h>
68 #include <vtkUnstructuredGrid.h>
69 #include <vtkDataSetMapper.h>
70 #include <vtkActorCollection.h>
71 #include <vtkRenderer.h>
72 #include <vtkProperty.h>
75 #include <qbuttongroup.h>
77 #include <qgroupbox.h>
79 #include <qlineedit.h>
80 #include <qpushbutton.h>
81 #include <qradiobutton.h>
85 #include <qwhatsthis.h>
88 #include <qvalidator.h>
91 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
98 void AddNode (SMESH::SMESH_Mesh_ptr theMesh, float x, float y, float z)
100 SUIT_OverrideCursor wc;
102 _PTR(SObject) aSobj = SMESH::FindSObject(theMesh);
103 SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
104 aMeshEditor->AddNode(x,y,z);
105 _PTR(Study) aStudy = GetActiveStudyDocument();
106 CORBA::Long anId = aStudy->StudyId();
107 if (TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId, aSobj->GetID().c_str())) {
108 aVisualObj->Update(true);
110 } catch (SALOME::SALOME_Exception& exc) {
111 INFOS("Follow exception was cought:\n\t" << exc.details.text);
112 } catch (const std::exception& exc) {
113 INFOS("Follow exception was cought:\n\t" << exc.what());
115 INFOS("Unknown exception was cought !!!");
119 class TNodeSimulation {
120 SVTK_ViewWindow* myViewWindow;
122 SALOME_Actor *myPreviewActor;
123 vtkDataSetMapper* myMapper;
127 TNodeSimulation(SVTK_ViewWindow* theViewWindow):
128 myViewWindow(theViewWindow)
130 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
133 myPoints = vtkPoints::New();
134 myPoints->SetNumberOfPoints(1);
135 myPoints->SetPoint(0,0.0,0.0,0.0);
138 vtkIdList *anIdList = vtkIdList::New();
139 anIdList->SetNumberOfIds(1);
141 vtkCellArray *aCells = vtkCellArray::New();
142 aCells->Allocate(2, 0);
144 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
145 aCellTypesArray->SetNumberOfComponents(1);
146 aCellTypesArray->Allocate(1);
148 anIdList->SetId(0, 0);
149 aCells->InsertNextCell(anIdList);
150 aCellTypesArray->InsertNextValue(VTK_VERTEX);
152 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
153 aCellLocationsArray->SetNumberOfComponents(1);
154 aCellLocationsArray->SetNumberOfTuples(1);
156 aCells->InitTraversal();
158 aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
160 aGrid->SetCells(aCellTypesArray, aCellLocationsArray, aCells);
162 aGrid->SetPoints(myPoints);
163 aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aCells);
164 aCellLocationsArray->Delete();
165 aCellTypesArray->Delete();
169 // Create and display actor
170 myMapper = vtkDataSetMapper::New();
171 myMapper->SetInput(aGrid);
174 myPreviewActor = SALOME_Actor::New();
175 myPreviewActor->SetInfinitive(true);
176 myPreviewActor->VisibilityOff();
177 myPreviewActor->PickableOff();
178 myPreviewActor->SetMapper(myMapper);
180 vtkProperty* aProp = vtkProperty::New();
181 aProp->SetRepresentationToPoints();
183 vtkFloatingPointType anRGB[3];
184 GetColor( "SMESH", "node_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 255, 0 ) );
185 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
187 vtkFloatingPointType aPointSize = GetFloat( "SMESH:node_size", 3 );
188 aProp->SetPointSize( aPointSize );
190 myPreviewActor->SetProperty( aProp );
193 myViewWindow->AddActor(myPreviewActor);
196 void SetPosition (float x, float y, float z)
198 myPoints->SetPoint(0,x,y,z);
199 myPoints->Modified();
203 void SetVisibility (bool theVisibility)
205 myPreviewActor->SetVisibility(theVisibility);
206 RepaintCurrentView();
211 myViewWindow->RemoveActor(myPreviewActor);
212 myPreviewActor->Delete();
214 myMapper->RemoveAllInputs();
222 //=================================================================================
223 // class : SMESHGUI_NodesDlg()
225 //=================================================================================
226 SMESHGUI_NodesDlg::SMESHGUI_NodesDlg (SMESHGUI* theModule,
230 QDialog(SMESH::GetDesktop(theModule),
233 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),
234 mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
235 mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
236 mySMESHGUI(theModule)
238 mySimulation = new SMESH::TNodeSimulation(SMESH::GetViewWindow( mySMESHGUI ));
240 QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_DLG_NODE")));
242 setName("SMESHGUI_NodesDlg");
244 setCaption(tr("MESH_NODE_TITLE"));
245 setSizeGripEnabled(TRUE);
246 SMESHGUI_NodesDlgLayout = new QGridLayout(this);
247 SMESHGUI_NodesDlgLayout->setSpacing(6);
248 SMESHGUI_NodesDlgLayout->setMargin(11);
250 /***************************************************************/
251 GroupButtons = new QGroupBox(this, "GroupButtons");
252 GroupButtons->setGeometry(QRect(10, 10, 281, 48));
253 GroupButtons->setTitle(tr("" ));
254 GroupButtons->setColumnLayout(0, Qt::Vertical);
255 GroupButtons->layout()->setSpacing(0);
256 GroupButtons->layout()->setMargin(0);
257 GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
258 GroupButtonsLayout->setAlignment(Qt::AlignTop);
259 GroupButtonsLayout->setSpacing(6);
260 GroupButtonsLayout->setMargin(11);
261 buttonHelp = new QPushButton(GroupButtons, "buttonHelp");
262 buttonHelp->setText(tr("SMESH_BUT_HELP" ));
263 buttonHelp->setAutoDefault(TRUE);
264 GroupButtonsLayout->addWidget(buttonHelp, 0, 4);
265 buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
266 buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
267 buttonCancel->setAutoDefault(TRUE);
268 GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
269 buttonApply = new QPushButton(GroupButtons, "buttonApply");
270 buttonApply->setText(tr("SMESH_BUT_APPLY" ));
271 buttonApply->setAutoDefault(TRUE);
272 GroupButtonsLayout->addWidget(buttonApply, 0, 1);
273 QSpacerItem* spacer_9 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
274 GroupButtonsLayout->addItem(spacer_9, 0, 2);
275 buttonOk = new QPushButton(GroupButtons, "buttonOk");
276 buttonOk->setText(tr("SMESH_BUT_APPLY_AND_CLOSE" ));
277 buttonOk->setAutoDefault(TRUE);
278 buttonOk->setDefault(TRUE);
279 GroupButtonsLayout->addWidget(buttonOk, 0, 0);
280 SMESHGUI_NodesDlgLayout->addWidget(GroupButtons, 2, 0);
282 /***************************************************************/
283 GroupConstructors = new QButtonGroup(this, "GroupConstructors");
284 GroupConstructors->setTitle(tr("MESH_NODE" ));
285 GroupConstructors->setExclusive(TRUE);
286 GroupConstructors->setColumnLayout(0, Qt::Vertical);
287 GroupConstructors->layout()->setSpacing(0);
288 GroupConstructors->layout()->setMargin(0);
289 GroupConstructorsLayout = new QGridLayout(GroupConstructors->layout());
290 GroupConstructorsLayout->setAlignment(Qt::AlignTop);
291 GroupConstructorsLayout->setSpacing(6);
292 GroupConstructorsLayout->setMargin(11);
293 Constructor1 = new QRadioButton(GroupConstructors, "Constructor1");
294 Constructor1->setText(tr("" ));
295 Constructor1->setPixmap(image0);
296 Constructor1->setChecked(TRUE);
297 GroupConstructorsLayout->addWidget(Constructor1, 0, 0);
298 QSpacerItem* spacer_2 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
299 GroupConstructorsLayout->addItem(spacer_2, 0, 1);
300 SMESHGUI_NodesDlgLayout->addWidget(GroupConstructors, 0, 0);
302 /***************************************************************/
303 GroupCoordinates = new QGroupBox(this, "GroupCoordinates");
304 GroupCoordinates->setTitle(tr("SMESH_COORDINATES" ));
305 GroupCoordinates->setColumnLayout(0, Qt::Vertical);
306 GroupCoordinates->layout()->setSpacing(0);
307 GroupCoordinates->layout()->setMargin(0);
308 GroupCoordinatesLayout = new QGridLayout(GroupCoordinates->layout());
309 GroupCoordinatesLayout->setAlignment(Qt::AlignTop);
310 GroupCoordinatesLayout->setSpacing(6);
311 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);
318 TextLabel_Y = new QLabel(GroupCoordinates, "TextLabel_Y");
319 //TextLabel_Y->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
320 TextLabel_Y->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
321 TextLabel_Y->setText(tr("SMESH_Y" ));
322 GroupCoordinatesLayout->addWidget(TextLabel_Y, 0, 2);
324 TextLabel_Z = new QLabel(GroupCoordinates, "TextLabel_Z");
325 //TextLabel_Z->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
326 TextLabel_Z->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
327 TextLabel_Z->setText(tr("SMESH_Z" ));
328 GroupCoordinatesLayout->addWidget(TextLabel_Z, 0, 4);
330 SpinBox_X = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_X");
331 GroupCoordinatesLayout->addWidget(SpinBox_X, 0, 1);
333 SpinBox_Y = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_Y");
334 GroupCoordinatesLayout->addWidget(SpinBox_Y, 0, 3);
336 SpinBox_Z = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_Z");
337 GroupCoordinatesLayout->addWidget(SpinBox_Z, 0, 5);
339 SMESHGUI_NodesDlgLayout->addWidget(GroupCoordinates, 1, 0);
341 myHelpFileName = "adding_nodes_and_elements_page.html#adding_nodes_anchor";
343 /* Initialisation and display */
347 //=======================================================================
348 // function : ~SMESHGUI_NodesDlg()
349 // purpose : Destructor
350 //=======================================================================
351 SMESHGUI_NodesDlg::~SMESHGUI_NodesDlg()
356 //=================================================================================
359 //=================================================================================
360 void SMESHGUI_NodesDlg::Init ()
362 /* Get setting of step value from file configuration */
364 // QString St = SUIT_CONFIG->getSetting("xxxxxxxxxxxxx"); TODO
365 // step = St.toDouble(); TODO
368 /* min, max, step and decimals for spin boxes */
369 SpinBox_X->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY);
370 SpinBox_Y->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY);
371 SpinBox_Z->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY);
372 SpinBox_X->SetValue(0.0);
373 SpinBox_Y->SetValue(0.0);
374 SpinBox_Z->SetValue(0.0);
376 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
378 /* signals and slots connections */
379 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
380 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
381 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
382 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
384 connect(SpinBox_X, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
385 connect(SpinBox_Y, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
386 connect(SpinBox_Z, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
388 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
389 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
390 /* to close dialog if study frame change */
391 connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
395 // set selection mode
396 SMESH::SetPointRepresentation(true);
397 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
398 aViewWindow->SetSelectionMode(NodeSelection);
400 SelectionIntoArgument();
403 //=================================================================================
404 // function : ValueChangedInSpinBox()
406 //=================================================================================
407 void SMESHGUI_NodesDlg::ValueChangedInSpinBox (double newValue)
409 if (!myMesh->_is_nil()) {
410 double vx = SpinBox_X->GetValue();
411 double vy = SpinBox_Y->GetValue();
412 double vz = SpinBox_Z->GetValue();
414 mySimulation->SetPosition(vx,vy,vz);
418 //=================================================================================
419 // function : ClickOnOk()
421 //=================================================================================
422 void SMESHGUI_NodesDlg::ClickOnOk()
428 //=================================================================================
429 // function : ClickOnApply()
431 //=================================================================================
432 bool SMESHGUI_NodesDlg::ClickOnApply()
434 if (mySMESHGUI->isActiveStudyLocked())
437 if (myMesh->_is_nil()) {
438 SUIT_MessageBox::warn1(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
439 tr("MESH_IS_NOT_SELECTED"), tr("SMESH_BUT_OK"));
443 /* Recup args and call method */
444 double x = SpinBox_X->GetValue();
445 double y = SpinBox_Y->GetValue();
446 double z = SpinBox_Z->GetValue();
447 mySimulation->SetVisibility(false);
448 SMESH::AddNode(myMesh,x,y,z);
449 SMESH::SetPointRepresentation(true);
453 mySelectionMgr->selectedObjects(aList);
454 if (aList.Extent() != 1) {
455 if (SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView()) {
456 vtkActorCollection *aCollection = aViewWindow->getRenderer()->GetActors();
457 aCollection->InitTraversal();
458 while (vtkActor *anAct = aCollection->GetNextActor()) {
459 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
460 if (anActor->hasIO()) {
461 if (SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>(anActor->GetObject().get())) {
462 if (myMesh->_is_equivalent(aMeshObj->GetMeshServer())) {
464 aList.Append(anActor->getIO());
465 mySelectionMgr->setSelectedObjects(aList, false);
477 //=================================================================================
478 // function : ClickOnCancel()
480 //=================================================================================
481 void SMESHGUI_NodesDlg::ClickOnCancel()
483 disconnect(mySelectionMgr, 0, this, 0);
484 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
485 aViewWindow->SetSelectionMode(ActorSelection);
487 mySimulation->SetVisibility(false);
488 SMESH::SetPointRepresentation(false);
489 mySMESHGUI->ResetState();
494 //=================================================================================
495 // function : ClickOnHelp()
497 //=================================================================================
498 void SMESHGUI_NodesDlg::ClickOnHelp()
500 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
502 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
506 platform = "winapplication";
508 platform = "application";
510 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
511 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
512 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
513 QObject::tr("BUT_OK"));
517 //=================================================================================
518 // function : SelectionIntoArgument()
519 // purpose : Called when selection as changed or other case
520 //=================================================================================
521 void SMESHGUI_NodesDlg::SelectionIntoArgument()
523 if (!GroupConstructors->isEnabled())
526 mySimulation->SetVisibility(false);
527 SMESH::SetPointRepresentation(true);
529 const SALOME_ListIO& aList = mySelector->StoredIObjects();
530 if (aList.Extent() == 1) {
531 Handle(SALOME_InteractiveObject) anIO = aList.First();
532 if (anIO->hasEntry()) {
533 myMesh = SMESH::GetMeshByIO(anIO);
534 if (myMesh->_is_nil()) return;
536 if (SMESH::GetNameOfSelectedNodes(mySelector,anIO,aText) == 1) {
537 if (SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh.in())) {
538 if (SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh()) {
539 if (const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
540 SpinBox_X->SetValue(aNode->X());
541 SpinBox_Y->SetValue(aNode->Y());
542 SpinBox_Z->SetValue(aNode->Z());
547 mySimulation->SetPosition(SpinBox_X->GetValue(),
548 SpinBox_Y->GetValue(),
549 SpinBox_Z->GetValue());
554 //=================================================================================
555 // function : closeEvent()
557 //=================================================================================
558 void SMESHGUI_NodesDlg::closeEvent (QCloseEvent*)
560 this->ClickOnCancel(); /* same than click on cancel button */
563 //=================================================================================
564 // function : hideEvent()
565 // purpose : caused by ESC key
566 //=================================================================================
567 void SMESHGUI_NodesDlg::hideEvent (QHideEvent*)
573 //=================================================================================
574 // function : enterEvent()
575 // purpose : to reactivate this dialog box when mouse enter onto the window
576 //=================================================================================
577 void SMESHGUI_NodesDlg::enterEvent(QEvent*)
579 if (!GroupConstructors->isEnabled())
580 ActivateThisDialog();
583 //=================================================================================
584 // function : DeactivateActiveDialog()
585 // purpose : public slot to deactivate if active
586 //=================================================================================
587 void SMESHGUI_NodesDlg::DeactivateActiveDialog()
589 if (GroupConstructors->isEnabled()) {
590 GroupConstructors->setEnabled(false);
591 GroupCoordinates->setEnabled(false);
592 GroupButtons->setEnabled(false);
593 mySimulation->SetVisibility(false);
594 mySMESHGUI->ResetState();
595 mySMESHGUI->SetActiveDialogBox(0);
599 //=================================================================================
600 // function : ActivateThisDialog()
602 //=================================================================================
603 void SMESHGUI_NodesDlg::ActivateThisDialog()
605 mySMESHGUI->EmitSignalDeactivateDialog();
606 GroupConstructors->setEnabled(true);
607 GroupCoordinates->setEnabled(true);
608 GroupButtons->setEnabled(true);
610 SMESH::SetPointRepresentation(true);
611 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
612 aViewWindow->SetSelectionMode(NodeSelection);
614 SelectionIntoArgument();
617 //=================================================================================
618 // function : keyPressEvent()
620 //=================================================================================
621 void SMESHGUI_NodesDlg::keyPressEvent( QKeyEvent* e )
623 QDialog::keyPressEvent( e );
624 if ( e->isAccepted() )
627 if ( e->key() == Key_F1 )