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_Algorithm.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_APPLY_AND_CLOSE" ));
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);
314 TextLabel_X = new QLabel(GroupCoordinates, "TextLabel_X");
315 TextLabel_X->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
316 TextLabel_X->setText(tr("SMESH_X" ));
317 GroupCoordinatesLayout->addWidget(TextLabel_X, 0, 0);
319 TextLabel_Y = new QLabel(GroupCoordinates, "TextLabel_Y");
320 //TextLabel_Y->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
321 TextLabel_Y->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
322 TextLabel_Y->setText(tr("SMESH_Y" ));
323 GroupCoordinatesLayout->addWidget(TextLabel_Y, 0, 2);
325 TextLabel_Z = new QLabel(GroupCoordinates, "TextLabel_Z");
326 //TextLabel_Z->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
327 TextLabel_Z->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
328 TextLabel_Z->setText(tr("SMESH_Z" ));
329 GroupCoordinatesLayout->addWidget(TextLabel_Z, 0, 4);
331 SpinBox_X = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_X");
332 GroupCoordinatesLayout->addWidget(SpinBox_X, 0, 1);
334 SpinBox_Y = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_Y");
335 GroupCoordinatesLayout->addWidget(SpinBox_Y, 0, 3);
337 SpinBox_Z = new SMESHGUI_SpinBox(GroupCoordinates, "SpinBox_Z");
338 GroupCoordinatesLayout->addWidget(SpinBox_Z, 0, 5);
340 SMESHGUI_NodesDlgLayout->addWidget(GroupCoordinates, 1, 0);
342 myHelpFileName = "adding_nodes_and_elements_page.html#adding_nodes_anchor";
344 /* Initialisation and display */
348 //=======================================================================
349 // function : ~SMESHGUI_NodesDlg()
350 // purpose : Destructor
351 //=======================================================================
352 SMESHGUI_NodesDlg::~SMESHGUI_NodesDlg()
357 //=================================================================================
360 //=================================================================================
361 void SMESHGUI_NodesDlg::Init ()
363 /* Get setting of step value from file configuration */
365 // QString St = SUIT_CONFIG->getSetting("xxxxxxxxxxxxx"); TODO
366 // step = St.toDouble(); TODO
369 /* min, max, step and decimals for spin boxes */
370 SpinBox_X->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY);
371 SpinBox_Y->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY);
372 SpinBox_Z->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY);
373 SpinBox_X->SetValue(0.0);
374 SpinBox_Y->SetValue(0.0);
375 SpinBox_Z->SetValue(0.0);
377 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
379 /* signals and slots connections */
380 connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
381 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
382 connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
383 connect(buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
385 connect(SpinBox_X, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
386 connect(SpinBox_Y, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
387 connect(SpinBox_Z, SIGNAL (valueChanged(double)), SLOT(ValueChangedInSpinBox(double)));
389 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
390 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
391 /* to close dialog if study frame change */
392 connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
396 // set selection mode
397 SMESH::SetPointRepresentation(true);
398 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
399 aViewWindow->SetSelectionMode(NodeSelection);
401 SelectionIntoArgument();
404 //=================================================================================
405 // function : ValueChangedInSpinBox()
407 //=================================================================================
408 void SMESHGUI_NodesDlg::ValueChangedInSpinBox (double newValue)
410 if (!myMesh->_is_nil()) {
411 double vx = SpinBox_X->GetValue();
412 double vy = SpinBox_Y->GetValue();
413 double vz = SpinBox_Z->GetValue();
415 mySimulation->SetPosition(vx,vy,vz);
419 //=================================================================================
420 // function : ClickOnOk()
422 //=================================================================================
423 void SMESHGUI_NodesDlg::ClickOnOk()
429 //=================================================================================
430 // function : ClickOnApply()
432 //=================================================================================
433 bool SMESHGUI_NodesDlg::ClickOnApply()
435 if (mySMESHGUI->isActiveStudyLocked())
438 if (myMesh->_is_nil()) {
439 SUIT_MessageBox::warn1(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
440 tr("MESH_IS_NOT_SELECTED"), tr("SMESH_BUT_OK"));
444 /* Recup args and call method */
445 double x = SpinBox_X->GetValue();
446 double y = SpinBox_Y->GetValue();
447 double z = SpinBox_Z->GetValue();
448 mySimulation->SetVisibility(false);
449 SMESH::AddNode(myMesh,x,y,z);
450 SMESH::SetPointRepresentation(true);
454 mySelectionMgr->selectedObjects(aList);
455 if (aList.Extent() != 1) {
456 if (SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView()) {
457 VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
458 vtkActorCollection *aCollection = aCopy.GetActors();
459 aCollection->InitTraversal();
460 while (vtkActor *anAct = aCollection->GetNextActor()) {
461 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
462 if (anActor->hasIO()) {
463 if (SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>(anActor->GetObject().get())) {
464 if (myMesh->_is_equivalent(aMeshObj->GetMeshServer())) {
466 aList.Append(anActor->getIO());
467 mySelectionMgr->setSelectedObjects(aList, false);
479 //=================================================================================
480 // function : ClickOnCancel()
482 //=================================================================================
483 void SMESHGUI_NodesDlg::ClickOnCancel()
485 disconnect(mySelectionMgr, 0, this, 0);
486 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
487 aViewWindow->SetSelectionMode(ActorSelection);
489 mySimulation->SetVisibility(false);
490 SMESH::SetPointRepresentation(false);
491 mySMESHGUI->ResetState();
496 //=================================================================================
497 // function : ClickOnHelp()
499 //=================================================================================
500 void SMESHGUI_NodesDlg::ClickOnHelp()
502 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
504 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
508 platform = "winapplication";
510 platform = "application";
512 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
513 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
514 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
515 QObject::tr("BUT_OK"));
519 //=================================================================================
520 // function : SelectionIntoArgument()
521 // purpose : Called when selection as changed or other case
522 //=================================================================================
523 void SMESHGUI_NodesDlg::SelectionIntoArgument()
525 if (!GroupConstructors->isEnabled())
528 mySimulation->SetVisibility(false);
529 SMESH::SetPointRepresentation(true);
531 const SALOME_ListIO& aList = mySelector->StoredIObjects();
532 if (aList.Extent() == 1) {
533 Handle(SALOME_InteractiveObject) anIO = aList.First();
534 if (anIO->hasEntry()) {
535 myMesh = SMESH::GetMeshByIO(anIO);
536 if (myMesh->_is_nil()) return;
538 if (SMESH::GetNameOfSelectedNodes(mySelector,anIO,aText) == 1) {
539 if (SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh.in())) {
540 if (SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh()) {
541 if (const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
542 SpinBox_X->SetValue(aNode->X());
543 SpinBox_Y->SetValue(aNode->Y());
544 SpinBox_Z->SetValue(aNode->Z());
549 mySimulation->SetPosition(SpinBox_X->GetValue(),
550 SpinBox_Y->GetValue(),
551 SpinBox_Z->GetValue());
556 //=================================================================================
557 // function : closeEvent()
559 //=================================================================================
560 void SMESHGUI_NodesDlg::closeEvent (QCloseEvent*)
562 this->ClickOnCancel(); /* same than click on cancel button */
565 //=================================================================================
566 // function : hideEvent()
567 // purpose : caused by ESC key
568 //=================================================================================
569 void SMESHGUI_NodesDlg::hideEvent (QHideEvent*)
575 //=================================================================================
576 // function : enterEvent()
577 // purpose : to reactivate this dialog box when mouse enter onto the window
578 //=================================================================================
579 void SMESHGUI_NodesDlg::enterEvent(QEvent*)
581 if (!GroupConstructors->isEnabled())
582 ActivateThisDialog();
585 //=================================================================================
586 // function : DeactivateActiveDialog()
587 // purpose : public slot to deactivate if active
588 //=================================================================================
589 void SMESHGUI_NodesDlg::DeactivateActiveDialog()
591 if (GroupConstructors->isEnabled()) {
592 GroupConstructors->setEnabled(false);
593 GroupCoordinates->setEnabled(false);
594 GroupButtons->setEnabled(false);
595 mySimulation->SetVisibility(false);
596 mySMESHGUI->ResetState();
597 mySMESHGUI->SetActiveDialogBox(0);
601 //=================================================================================
602 // function : ActivateThisDialog()
604 //=================================================================================
605 void SMESHGUI_NodesDlg::ActivateThisDialog()
607 mySMESHGUI->EmitSignalDeactivateDialog();
608 GroupConstructors->setEnabled(true);
609 GroupCoordinates->setEnabled(true);
610 GroupButtons->setEnabled(true);
612 SMESH::SetPointRepresentation(true);
613 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
614 aViewWindow->SetSelectionMode(NodeSelection);
616 SelectionIntoArgument();
619 //=================================================================================
620 // function : keyPressEvent()
622 //=================================================================================
623 void SMESHGUI_NodesDlg::keyPressEvent( QKeyEvent* e )
625 QDialog::keyPressEvent( e );
626 if ( e->isAccepted() )
629 if ( e->key() == Key_F1 )