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
22 // File : SMESHGUI_AddMeshElementDlg.cxx
23 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
27 #include "SMESHGUI_AddMeshElementDlg.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_VTKUtils.h"
32 #include "SMESHGUI_MeshUtils.h"
33 #include "SMESHGUI_IdValidator.h"
35 #include <SMESH_Actor.h>
36 #include <SMESH_ActorUtils.h>
37 #include <SMDS_Mesh.hxx>
40 #include <SUIT_Desktop.h>
41 #include <SUIT_Session.h>
42 #include <SUIT_ResourceMgr.h>
43 #include <SUIT_MessageBox.h>
44 #include <SUIT_ViewManager.h>
45 #include <LightApp_SelectionMgr.h>
46 #include <SALOME_ListIO.hxx>
47 #include <SalomeApp_Application.h>
48 #include <SVTK_ViewModel.h>
49 #include <SVTK_ViewWindow.h>
52 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
55 #include <TColStd_MapOfInteger.hxx>
59 #include <vtkIdList.h>
60 #include <vtkUnstructuredGrid.h>
61 #include <vtkDataSetMapper.h>
62 #include <vtkProperty.h>
68 #include <QPushButton>
69 #include <QRadioButton>
70 #include <QHBoxLayout>
71 #include <QVBoxLayout>
72 #include <QGridLayout>
76 #include <QButtonGroup>
83 class TElementSimulation
85 SalomeApp_Application* myApplication;
86 SUIT_ViewWindow* myViewWindow;
87 SVTK_ViewWindow* myVTKViewWindow;
89 SALOME_Actor* myPreviewActor;
90 vtkDataSetMapper* myMapper;
91 vtkUnstructuredGrid* myGrid;
94 TElementSimulation (SalomeApp_Application* theApplication)
96 myApplication = theApplication;
97 SUIT_ViewManager* mgr = theApplication->activeViewManager();
99 myViewWindow = mgr->getActiveView();
100 myVTKViewWindow = GetVtkViewWindow(myViewWindow);
102 myGrid = vtkUnstructuredGrid::New();
104 // Create and display actor
105 myMapper = vtkDataSetMapper::New();
106 myMapper->SetInput(myGrid);
108 myPreviewActor = SALOME_Actor::New();
109 myPreviewActor->PickableOff();
110 myPreviewActor->VisibilityOff();
111 myPreviewActor->SetMapper(myMapper);
113 vtkFloatingPointType anRGB[3];
114 vtkProperty* aProp = vtkProperty::New();
115 GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
116 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
117 myPreviewActor->SetProperty( aProp );
120 vtkProperty* aBackProp = vtkProperty::New();
121 GetColor( "SMESH", "backface_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 0, 255 ) );
122 aBackProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
123 myPreviewActor->SetBackfaceProperty( aBackProp );
126 myVTKViewWindow->AddActor(myPreviewActor);
129 typedef std::vector<vtkIdType> TVTKIds;
130 void SetPosition (SMESH_Actor* theActor,
132 const TVTKIds& theIds)
134 vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
135 myGrid->SetPoints(aGrid->GetPoints());
137 const int* aConn = NULL;
141 static int anIds[] = {0,2,1,3};
147 static int anIds[] = {0,3,2,1,4};
153 static int anIds[] = {0,3,2,1,4,7,6,5};
160 vtkIdList *anIds = vtkIdList::New();
163 for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
164 anIds->InsertId(i,theIds[aConn[i]]);
166 for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
167 anIds->InsertId(i,theIds[i]);
169 myGrid->InsertNextCell(theType,anIds);
178 void SetVisibility (bool theVisibility)
180 myPreviewActor->SetVisibility(theVisibility);
181 RepaintCurrentView();
185 ~TElementSimulation()
187 if (FindVtkViewWindow(myApplication->activeViewManager(), myViewWindow)) {
188 myVTKViewWindow->RemoveActor(myPreviewActor);
190 myPreviewActor->Delete();
192 myMapper->RemoveAllInputs();
200 //=================================================================================
201 // function : SMESHGUI_AddMeshElementDlg()
202 // purpose : constructor
203 //=================================================================================
204 SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI* theModule,
205 SMDSAbs_ElementType ElementType,
207 : QDialog( SMESH::GetDesktop( theModule ) ),
208 mySMESHGUI( theModule ),
209 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
212 setAttribute( Qt::WA_DeleteOnClose, true );
214 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
215 (SUIT_Session::session()->activeApplication());
217 mySimulation = new SMESH::TElementSimulation (anApp);
218 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
220 // verify nb nodes and type
222 myElementType = ElementType;
223 switch (ElementType) {
225 // if (myNbNodes != 3 && myNbNodes != 4)
229 // if (myNbNodes != 4 && myNbNodes != 8) //(nbNodes < 4 || nbNodes > 8 || nbNodes == 7)
233 myElementType = SMDSAbs_Edge;
238 if (myNbNodes == 2) {
240 myHelpFileName = "adding_nodes_and_elements_page.html#adding_edges_anchor";
242 else if (myNbNodes == 3) {
243 elemName = "TRIANGLE";
244 myHelpFileName = "adding_nodes_and_elements_page.html#adding_triangles_anchor";
246 else if (myNbNodes == 4)
247 if (myElementType == SMDSAbs_Face) {
248 elemName = "QUADRANGLE";
249 myHelpFileName = "adding_nodes_and_elements_page.html#adding_quadrangles_anchor";
253 myHelpFileName = "adding_nodes_and_elements_page.html#adding_tetrahedrons_anchor";
255 else if (myNbNodes == 8) {
257 myHelpFileName = "adding_nodes_and_elements_page.html#adding_hexahedrons_anchor";
259 else if (myElementType == SMDSAbs_Face) {
260 elemName = "POLYGON";
262 myHelpFileName = "adding_nodes_and_elements_page.html#adding_polygons_anchor";
264 else if (myElementType == SMDSAbs_Volume) {
265 myHelpFileName = "adding_nodes_and_elements_page.html#adding_polyhedrons_anchor";
268 QString iconName = tr(QString("ICON_DLG_%1").arg(elemName).toLatin1().data());
269 QString buttonGrTitle = tr(QString("SMESH_%1").arg(elemName).toLatin1().data());
270 QString caption = tr(QString("SMESH_ADD_%1_TITLE").arg(elemName).toLatin1().data());
271 QString grBoxTitle = tr(QString("SMESH_ADD_%1").arg(elemName).toLatin1().data());
273 QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", iconName));
274 QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
276 setWindowTitle(caption);
277 setSizeGripEnabled(true);
279 QVBoxLayout* aTopLayout = new QVBoxLayout(this);
280 aTopLayout->setSpacing(SPACING);
281 aTopLayout->setMargin(MARGIN);
283 /***************************************************************/
284 GroupConstructors = new QGroupBox(buttonGrTitle, this);
285 QButtonGroup* ButtonGroup = new QButtonGroup(this);
286 QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
287 GroupConstructorsLayout->setSpacing(SPACING);
288 GroupConstructorsLayout->setMargin(MARGIN);
290 Constructor1 = new QRadioButton(GroupConstructors);
291 Constructor1->setIcon(image0);
292 Constructor1->setChecked(true);
294 GroupConstructorsLayout->addWidget(Constructor1);
295 ButtonGroup->addButton( Constructor1, 0 );
297 /***************************************************************/
298 GroupC1 = new QGroupBox(grBoxTitle, this);
299 QGridLayout* GroupC1Layout = new QGridLayout(GroupC1);
300 GroupC1Layout->setSpacing(SPACING);
301 GroupC1Layout->setMargin(MARGIN);
303 TextLabelC1A1 = new QLabel(tr("SMESH_ID_NODES"), GroupC1);
304 SelectButtonC1A1 = new QPushButton(GroupC1);
305 SelectButtonC1A1->setIcon(image1);
306 LineEditC1A1 = new QLineEdit(GroupC1);
307 // LineEditC1A1->setReadOnly(true);
309 LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this, myNbNodes));
311 Reverse = myElementType == SMDSAbs_Face ? new QCheckBox(tr("SMESH_REVERSE"), GroupC1) : 0;
313 GroupC1Layout->addWidget(TextLabelC1A1, 0, 0);
314 GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
315 GroupC1Layout->addWidget(LineEditC1A1, 0, 2);
316 if ( Reverse ) GroupC1Layout->addWidget(Reverse, 1, 0, 1, 3);
318 /***************************************************************/
319 GroupButtons = new QGroupBox(this);
320 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
321 GroupButtonsLayout->setSpacing(SPACING);
322 GroupButtonsLayout->setMargin(MARGIN);
324 buttonOk = new QPushButton(tr("SMESH_BUT_OK"), GroupButtons);
325 buttonOk->setAutoDefault(true);
326 buttonOk->setDefault(true);
327 buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
328 buttonApply->setAutoDefault(true);
329 buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
330 buttonCancel->setAutoDefault(true);
331 buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
332 buttonHelp->setAutoDefault(true);
334 GroupButtonsLayout->addWidget(buttonOk);
335 GroupButtonsLayout->addSpacing(10);
336 GroupButtonsLayout->addWidget(buttonApply);
337 GroupButtonsLayout->addSpacing(10);
338 GroupButtonsLayout->addStretch();
339 GroupButtonsLayout->addWidget(buttonCancel);
340 GroupButtonsLayout->addWidget(buttonHelp);
342 /***************************************************************/
343 aTopLayout->addWidget(GroupConstructors);
344 aTopLayout->addWidget(GroupC1);
345 aTopLayout->addWidget(GroupButtons);
347 Init(); /* Initialisations */
350 //=================================================================================
351 // function : ~SMESHGUI_AddMeshElementDlg()
352 // purpose : Destroys the object and frees any allocated resources
353 //=================================================================================
354 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
359 //=================================================================================
362 //=================================================================================
363 void SMESHGUI_AddMeshElementDlg::Init()
366 Constructor1->setChecked(true);
367 myEditCurrentArgument = LineEditC1A1;
368 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
373 /* signals and slots connections */
374 connect(buttonOk, SIGNAL(clicked()), SLOT(ClickOnOk()));
375 connect(buttonCancel, SIGNAL(clicked()), SLOT(ClickOnCancel()));
376 connect(buttonApply, SIGNAL(clicked()), SLOT(ClickOnApply()));
377 connect(buttonHelp, SIGNAL(clicked()), SLOT(ClickOnHelp()));
379 connect(SelectButtonC1A1, SIGNAL(clicked()), SLOT(SetEditCurrentArgument()));
380 connect(LineEditC1A1, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
381 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
382 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
383 /* to close dialog if study frame change */
384 connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
387 connect(Reverse, SIGNAL(stateChanged(int)), SLOT(CheckBox(int)));
389 // set selection mode
390 SMESH::SetPointRepresentation(true);
392 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
393 aViewWindow->SetSelectionMode( NodeSelection );
397 SelectionIntoArgument();
400 //=================================================================================
401 // function : ClickOnApply()
403 //=================================================================================
404 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
406 if (myNbOkNodes && !mySMESHGUI->isActiveStudyLocked()) {
408 SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
409 anArrayOfIdeces->length(myNbNodes);
410 bool reverse = (Reverse && Reverse->isChecked());
411 QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
412 for (int i = 0; i < aListId.count(); i++)
414 anArrayOfIdeces[i] = aListId[ myNbNodes - i - 1 ].toInt();
416 anArrayOfIdeces[i] = aListId[ i ].toInt();
418 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
419 switch (myElementType) {
421 aMeshEditor->AddEdge(anArrayOfIdeces.inout()); break;
424 aMeshEditor->AddPolygonalFace(anArrayOfIdeces.inout());
426 aMeshEditor->AddFace(anArrayOfIdeces.inout());
431 aMeshEditor->AddVolume(anArrayOfIdeces.inout()); break;
435 SALOME_ListIO aList; aList.Append( myActor->getIO() );
436 mySelector->ClearIndex();
437 mySelectionMgr->setSelectedObjects( aList, false );
440 mySimulation->SetVisibility(false);
442 buttonOk->setEnabled(false);
443 buttonApply->setEnabled(false);
445 myEditCurrentArgument->setText("");
451 //=================================================================================
452 // function : ClickOnOk()
454 //=================================================================================
455 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
461 //=================================================================================
462 // function : ClickOnCancel()
464 //=================================================================================
465 void SMESHGUI_AddMeshElementDlg::ClickOnCancel()
467 //mySelectionMgr->clearSelected();
468 mySimulation->SetVisibility(false);
469 SMESH::SetPointRepresentation(false);
470 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
471 aViewWindow->SetSelectionMode( ActorSelection );
472 disconnect(mySelectionMgr, 0, this, 0);
473 mySMESHGUI->ResetState();
477 //=================================================================================
478 // function : ClickOnHelp()
480 //=================================================================================
481 void SMESHGUI_AddMeshElementDlg::ClickOnHelp()
483 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
485 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
489 platform = "winapplication";
491 platform = "application";
493 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
494 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
495 arg(app->resourceMgr()->stringValue("ExternalBrowser",
497 arg(myHelpFileName));
501 //=================================================================================
502 // function : onTextChange()
504 //=================================================================================
505 void SMESHGUI_AddMeshElementDlg::onTextChange (const QString& theNewText)
512 buttonOk->setEnabled(false);
513 buttonApply->setEnabled(false);
515 mySimulation->SetVisibility(false);
517 // hilight entered nodes
518 SMDS_Mesh* aMesh = 0;
520 aMesh = myActor->GetObject()->GetMesh();
523 TColStd_MapOfInteger newIndices;
525 QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
527 for (int i = 0; i < aListId.count(); i++) {
528 if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
530 newIndices.Add( n->GetID() );
537 mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
538 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
539 aViewWindow->highlight( myActor->getIO(), true, true );
541 myNbOkNodes = ( allOk && myNbNodes == aListId.count() );
545 if ( !allOk || myElementType != SMDSAbs_Face || aListId.count() < 3 )
548 myNbOkNodes = aListId.count();
553 buttonOk->setEnabled(true);
554 buttonApply->setEnabled(true);
561 //=================================================================================
562 // function : SelectionIntoArgument()
563 // purpose : Called when selection has changed
564 //=================================================================================
565 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
574 myEditCurrentArgument->setText("");
577 if (!GroupButtons->isEnabled()) // inactive
580 buttonOk->setEnabled(false);
581 buttonApply->setEnabled(false);
583 mySimulation->SetVisibility(false);
584 // SMESH::SetPointRepresentation(true);
588 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
590 if (aList.Extent() != 1)
593 Handle(SALOME_InteractiveObject) anIO = aList.First();
594 myMesh = SMESH::GetMeshByIO(anIO);
595 if (myMesh->_is_nil())
598 myActor = SMESH::FindActorByEntry(anIO->getEntry());
602 // get selected nodes
603 QString aString = "";
604 int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,myActor->getIO(),aString);
606 myEditCurrentArgument->setText(aString);
608 if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 ) {
610 } else if (myNbNodes != nbNodes) {
615 myNbOkNodes = nbNodes;
617 buttonOk->setEnabled(true);
618 buttonApply->setEnabled(true);
623 //=================================================================================
624 // function : displaySimulation()
626 //=================================================================================
627 void SMESHGUI_AddMeshElementDlg::displaySimulation()
629 if (myNbOkNodes && GroupButtons->isEnabled()) {
630 SMESH::TElementSimulation::TVTKIds anIds;
631 QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
632 for (int i = 0; i < aListId.count(); i++)
633 anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
635 if (Reverse && Reverse->isChecked())
636 reverse(anIds.begin(),anIds.end());
640 switch ( myElementType ) {
641 case SMDSAbs_Face : aType = VTK_POLYGON; break;
646 case 2: aType = VTK_LINE; break;
647 case 3: aType = VTK_TRIANGLE; break;
648 case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
649 case 8: aType = VTK_HEXAHEDRON; break;
654 mySimulation->SetPosition(myActor,aType,anIds);
659 //=================================================================================
660 // function : SetEditCurrentArgument()
662 //=================================================================================
663 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
665 QPushButton* send = (QPushButton*)sender();
666 if (send == SelectButtonC1A1) {
667 LineEditC1A1->setFocus();
668 myEditCurrentArgument = LineEditC1A1;
670 SelectionIntoArgument();
673 //=================================================================================
674 // function : DeactivateActiveDialog()
676 //=================================================================================
677 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
679 if (GroupConstructors->isEnabled()) {
680 GroupConstructors->setEnabled(false);
681 GroupC1->setEnabled(false);
682 GroupButtons->setEnabled(false);
683 mySimulation->SetVisibility(false);
684 mySMESHGUI->ResetState();
685 mySMESHGUI->SetActiveDialogBox(0);
689 //=================================================================================
690 // function : ActivateThisDialog()
692 //=================================================================================
693 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
695 /* Emit a signal to deactivate the active dialog */
696 mySMESHGUI->EmitSignalDeactivateDialog();
698 GroupConstructors->setEnabled(true);
699 GroupC1->setEnabled(true);
700 GroupButtons->setEnabled(true);
702 SMESH::SetPointRepresentation(true);
704 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
705 aViewWindow->SetSelectionMode( NodeSelection );
706 SelectionIntoArgument();
709 //=================================================================================
710 // function : enterEvent()
712 //=================================================================================
713 void SMESHGUI_AddMeshElementDlg::enterEvent (QEvent*)
715 if (GroupConstructors->isEnabled())
717 ActivateThisDialog();
720 //=================================================================================
721 // function : closeEvent()
723 //=================================================================================
724 void SMESHGUI_AddMeshElementDlg::closeEvent (QCloseEvent*)
726 /* same than click on cancel button */
730 //=================================================================================
731 // function : hideEvent()
732 // purpose : caused by ESC key
733 //=================================================================================
734 void SMESHGUI_AddMeshElementDlg::hideEvent (QHideEvent*)
740 //=================================================================================
741 // function : CheckBox()
743 //=================================================================================
744 void SMESHGUI_AddMeshElementDlg::CheckBox (int state)
750 mySimulation->SetVisibility(false);
755 //=================================================================================
756 // function : keyPressEvent()
758 //=================================================================================
759 void SMESHGUI_AddMeshElementDlg::keyPressEvent( QKeyEvent* e )
761 QDialog::keyPressEvent( e );
762 if ( e->isAccepted() )
765 if ( e->key() == Qt::Key_F1 ) {