Salome HOME
Merge from V6_4_BR 05/12/2011
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddMeshElementDlg.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_AddMeshElementDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 //  SMESH includes
27 //
28 #include "SMESHGUI_AddMeshElementDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_VTKUtils.h"
33 #include "SMESHGUI_MeshUtils.h"
34 #include "SMESHGUI_GroupUtils.h"
35 #include "SMESHGUI_IdValidator.h"
36
37 #include <SMESH_Actor.h>
38 #include <SMESH_ActorUtils.h>
39 #include <SMESH_FaceOrientationFilter.h>
40 #include <SMDS_Mesh.hxx>
41
42 // SALOME GUI inclues
43 #include <SUIT_Desktop.h>
44 #include <SUIT_Session.h>
45 #include <SUIT_ResourceMgr.h>
46 #include <SUIT_MessageBox.h>
47 #include <SUIT_ViewManager.h>
48 #include <LightApp_SelectionMgr.h>
49 #include <SALOME_ListIO.hxx>
50 #include <SalomeApp_Application.h>
51 #include <SVTK_ViewModel.h>
52 #include <SVTK_ViewWindow.h>
53
54 // IDL incldues
55 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
56
57 // OCCT includes
58 #include <TColStd_MapOfInteger.hxx>
59
60 // VTK includes
61 #include <vtkCell.h>
62 #include <vtkIdList.h>
63 #include <vtkUnstructuredGrid.h>
64 #include <vtkDataSetMapper.h>
65 #include <vtkPolyDataMapper.h>
66 #include <vtkProperty.h>
67
68 // Qt includes
69 #include <QComboBox>
70 #include <QGroupBox>
71 #include <QLabel>
72 #include <QLineEdit>
73 #include <QPushButton>
74 #include <QRadioButton>
75 #include <QHBoxLayout>
76 #include <QVBoxLayout>
77 #include <QGridLayout>
78 #include <QVariant>
79 #include <QCheckBox>
80 #include <QKeyEvent>
81 #include <QButtonGroup>
82
83 #define SPACING 6
84 #define MARGIN  11
85
86 namespace SMESH
87 {
88   class TElementSimulation
89   {
90     SalomeApp_Application* myApplication;
91     SUIT_ViewWindow* myViewWindow;
92     SVTK_ViewWindow* myVTKViewWindow;
93
94     SALOME_Actor* myPreviewActor;
95     vtkDataSetMapper* myMapper;
96     vtkUnstructuredGrid* myGrid;
97
98     SALOME_Actor* myFaceOrientation;
99     vtkPolyDataMapper* myFaceOrientationDataMapper;
100     SMESH_FaceOrientationFilter* myFaceOrientationFilter;
101
102   public:
103     TElementSimulation (SalomeApp_Application* theApplication)
104     {
105       myApplication = theApplication;
106       SUIT_ViewManager* mgr = theApplication->activeViewManager();
107       if (!mgr) return;
108       myViewWindow = mgr->getActiveView();
109       myVTKViewWindow = GetVtkViewWindow(myViewWindow);
110
111       myGrid = vtkUnstructuredGrid::New();
112
113       // Create and display actor
114       myMapper = vtkDataSetMapper::New();
115       myMapper->SetInput(myGrid);
116
117       myPreviewActor = SALOME_Actor::New();
118       myPreviewActor->PickableOff();
119       myPreviewActor->VisibilityOff();
120       myPreviewActor->SetMapper(myMapper);
121
122       vtkFloatingPointType anRGB[3];
123       vtkProperty* aProp = vtkProperty::New();
124       GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
125       aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
126       myPreviewActor->SetProperty( aProp );
127       aProp->Delete();
128
129       vtkProperty* aBackProp = vtkProperty::New();
130       GetColor( "SMESH", "backface_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 0, 255 ) );
131       aBackProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
132       myPreviewActor->SetBackfaceProperty( aBackProp );
133       aBackProp->Delete();
134
135       myVTKViewWindow->AddActor(myPreviewActor);
136
137       // Orientation of faces
138       myFaceOrientationFilter = SMESH_FaceOrientationFilter::New();
139       myFaceOrientationFilter->SetInput(myGrid);
140
141       myFaceOrientationDataMapper = vtkPolyDataMapper::New();
142       myFaceOrientationDataMapper->SetInput(myFaceOrientationFilter->GetOutput());
143
144       myFaceOrientation = SALOME_Actor::New();
145       myFaceOrientation->PickableOff();
146       myFaceOrientation->VisibilityOff();
147       myFaceOrientation->SetMapper(myFaceOrientationDataMapper);
148
149       vtkProperty* anOrientationProp = vtkProperty::New();
150       GetColor( "SMESH", "orientation_color", anRGB[0], anRGB[1], anRGB[2], QColor( 255, 255, 255 ) );
151       anOrientationProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
152       myFaceOrientation->SetProperty( anOrientationProp );
153       anOrientationProp->Delete();
154
155       myVTKViewWindow->AddActor(myFaceOrientation);
156     }
157
158     typedef std::vector<vtkIdType> TVTKIds;
159     void SetPosition (SMESH_Actor* theActor,
160                       vtkIdType theType,
161                       const TVTKIds& theIds)
162     {
163       vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
164       myGrid->SetPoints(aGrid->GetPoints());
165
166       const int* aConn = NULL;
167       switch (theType) {
168       case VTK_TETRA:
169         {
170           static int anIds[] = {0,2,1,3};
171           aConn = anIds;
172           break;
173         }
174       case VTK_PYRAMID:
175         {
176           static int anIds[] = {0,3,2,1,4};
177           aConn = anIds;
178           break;
179         }
180       case VTK_HEXAHEDRON:
181         {
182           static int anIds[] = {0,3,2,1,4,7,6,5};
183           aConn = anIds;
184           break;
185         }
186       }
187
188       myGrid->Reset();
189       vtkIdList *anIds = vtkIdList::New();
190
191       if(aConn)
192         for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
193           anIds->InsertId(i,theIds[aConn[i]]);
194       else
195         for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
196           anIds->InsertId(i,theIds[i]);
197
198       myGrid->InsertNextCell(theType,anIds);
199       anIds->Delete();
200
201       myGrid->Modified();
202
203       SetVisibility(true, theActor->GetFacesOriented());
204     }
205
206
207     void SetVisibility (bool theVisibility, bool theShowOrientation = false)
208     {
209       myPreviewActor->SetVisibility(theVisibility);
210       myFaceOrientation->SetVisibility(theShowOrientation);
211       RepaintCurrentView();
212     }
213
214
215     ~TElementSimulation()
216     {
217       if (FindVtkViewWindow(myApplication->activeViewManager(), myViewWindow)) {
218         myVTKViewWindow->RemoveActor(myPreviewActor);
219         myVTKViewWindow->RemoveActor(myFaceOrientation);
220       }
221       myPreviewActor->Delete();
222       myFaceOrientation->Delete();
223
224       myMapper->RemoveAllInputs();
225       myMapper->Delete();
226
227       myFaceOrientationFilter->Delete();
228
229       myFaceOrientationDataMapper->RemoveAllInputs();
230       myFaceOrientationDataMapper->Delete();
231
232       myGrid->Delete();
233     }
234   };
235 }
236
237 //=================================================================================
238 // function : SMESHGUI_AddMeshElementDlg()
239 // purpose  : constructor
240 //=================================================================================
241 SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI* theModule,
242                                                         SMDSAbs_ElementType ElementType,
243                                                         int nbNodes )
244   : QDialog( SMESH::GetDesktop( theModule ) ),
245     mySMESHGUI( theModule ),
246     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
247     myBusy ( false )
248 {
249   setModal( false );
250   setAttribute( Qt::WA_DeleteOnClose, true );
251
252   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
253     (SUIT_Session::session()->activeApplication());
254   myIsPoly = false;
255   mySimulation = new SMESH::TElementSimulation (anApp);
256   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
257
258   // verify nb nodes and type
259   myNbNodes = nbNodes;
260   myElementType = ElementType;
261   switch (ElementType) {
262   case SMDSAbs_0DElement:
263     if (myNbNodes != 1)
264       myNbNodes = 1;
265     break;
266   case SMDSAbs_Face:
267     //     if (myNbNodes != 3 && myNbNodes != 4)
268     //       myNbNodes = 3;
269     //     break;
270   case SMDSAbs_Volume:
271     //     if (myNbNodes != 4 && myNbNodes != 8) //(nbNodes < 4 || nbNodes > 8 || nbNodes == 7)
272     //       myNbNodes = 4;
273     break;
274   default:
275     myElementType = SMDSAbs_Edge;
276     myNbNodes = 2;
277   }
278
279   QString elemName;
280   if (myNbNodes == 1) {
281     elemName = "ELEM0D";
282     myHelpFileName = "adding_nodes_and_elements_page.html#adding_0delems_anchor";
283   }
284   else if (myNbNodes == 2) {
285     elemName = "EDGE";
286     myHelpFileName = "adding_nodes_and_elements_page.html#adding_edges_anchor";
287   }
288   else if (myNbNodes == 3) {
289     elemName = "TRIANGLE";
290     myHelpFileName = "adding_nodes_and_elements_page.html#adding_triangles_anchor";
291   }
292   else if (myNbNodes == 4) {
293     if (myElementType == SMDSAbs_Face) {
294       elemName = "QUADRANGLE";
295       myHelpFileName = "adding_nodes_and_elements_page.html#adding_quadrangles_anchor";
296     }
297     else {
298       elemName = "TETRAS";
299       myHelpFileName = "adding_nodes_and_elements_page.html#adding_tetrahedrons_anchor";
300     }
301   }
302   else if (myNbNodes == 8) {
303     elemName = "HEXAS";
304     myHelpFileName = "adding_nodes_and_elements_page.html#adding_hexahedrons_anchor";
305   }
306   else if (myElementType == SMDSAbs_Face) {
307     elemName = "POLYGON";
308     myIsPoly = true;
309     myHelpFileName = "adding_nodes_and_elements_page.html#adding_polygons_anchor";
310   }
311   else if (myElementType == SMDSAbs_Volume) {
312     myHelpFileName = "adding_nodes_and_elements_page.html#adding_polyhedrons_anchor";
313   }
314
315   QString iconName      = tr(QString("ICON_DLG_%1").arg(elemName).toLatin1().data());
316   QString buttonGrTitle = tr(QString("SMESH_%1").arg(elemName).toLatin1().data());
317   QString caption       = tr(QString("SMESH_ADD_%1_TITLE").arg(elemName).toLatin1().data());
318   QString grBoxTitle    = tr(QString("SMESH_ADD_%1").arg(elemName).toLatin1().data());
319
320   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", iconName));
321   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
322
323   setWindowTitle(caption);
324   setSizeGripEnabled(true);
325
326   QVBoxLayout* aTopLayout = new QVBoxLayout(this);
327   aTopLayout->setSpacing(SPACING);
328   aTopLayout->setMargin(MARGIN);
329
330   /***************************************************************/
331   GroupConstructors = new QGroupBox(buttonGrTitle, this);
332   QButtonGroup* ButtonGroup = new QButtonGroup(this);
333   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
334   GroupConstructorsLayout->setSpacing(SPACING);
335   GroupConstructorsLayout->setMargin(MARGIN);
336
337   Constructor1 = new QRadioButton(GroupConstructors);
338   Constructor1->setIcon(image0);
339   Constructor1->setChecked(true);
340
341   GroupConstructorsLayout->addWidget(Constructor1);
342   ButtonGroup->addButton( Constructor1, 0 );
343
344   /***************************************************************/
345   GroupC1 = new QGroupBox(grBoxTitle, this);
346   QGridLayout* GroupC1Layout = new QGridLayout(GroupC1);
347   GroupC1Layout->setSpacing(SPACING);
348   GroupC1Layout->setMargin(MARGIN);
349
350   TextLabelC1A1 = new QLabel(tr("SMESH_ID_NODES"), GroupC1);
351   SelectButtonC1A1 = new QPushButton(GroupC1);
352   SelectButtonC1A1->setIcon(image1);
353   LineEditC1A1 = new QLineEdit(GroupC1);
354   //  LineEditC1A1->setReadOnly(true);
355   if (!myIsPoly)
356     LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this, myNbNodes));
357
358   Reverse = myElementType == SMDSAbs_Face ? new QCheckBox(tr("SMESH_REVERSE"), GroupC1) : 0;
359
360   GroupC1Layout->addWidget(TextLabelC1A1,    0, 0);
361   GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
362   GroupC1Layout->addWidget(LineEditC1A1,     0, 2);
363   if ( Reverse ) GroupC1Layout->addWidget(Reverse, 1, 0, 1, 3);
364
365   /***************************************************************/
366   GroupGroups = new QGroupBox( tr( "SMESH_ADD_TO_GROUP" ), this );
367   GroupGroups->setCheckable( true );
368   QHBoxLayout* GroupGroupsLayout = new QHBoxLayout(GroupGroups);
369   GroupGroupsLayout->setSpacing(SPACING);
370   GroupGroupsLayout->setMargin(MARGIN);
371
372   TextLabel_GroupName = new QLabel( tr( "SMESH_GROUP" ), GroupGroups );
373   ComboBox_GroupName = new QComboBox( GroupGroups );
374   ComboBox_GroupName->setEditable( true );
375   ComboBox_GroupName->setInsertPolicy( QComboBox::NoInsert );
376
377   GroupGroupsLayout->addWidget( TextLabel_GroupName );
378   GroupGroupsLayout->addWidget( ComboBox_GroupName, 1 );
379
380   /***************************************************************/
381   GroupButtons = new QGroupBox(this);
382   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
383   GroupButtonsLayout->setSpacing(SPACING);
384   GroupButtonsLayout->setMargin(MARGIN);
385
386   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
387   buttonOk->setAutoDefault(true);
388   buttonOk->setDefault(true);
389   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
390   buttonApply->setAutoDefault(true);
391   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
392   buttonCancel->setAutoDefault(true);
393   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
394   buttonHelp->setAutoDefault(true);
395
396   GroupButtonsLayout->addWidget(buttonOk);
397   GroupButtonsLayout->addSpacing(10);
398   GroupButtonsLayout->addWidget(buttonApply);
399   GroupButtonsLayout->addSpacing(10);
400   GroupButtonsLayout->addStretch();
401   GroupButtonsLayout->addWidget(buttonCancel);
402   GroupButtonsLayout->addWidget(buttonHelp);
403
404   /***************************************************************/
405   aTopLayout->addWidget(GroupConstructors);
406   aTopLayout->addWidget(GroupC1);
407   aTopLayout->addWidget(GroupGroups);
408   aTopLayout->addWidget(GroupButtons);
409
410   Init(); /* Initialisations */
411 }
412
413 //=================================================================================
414 // function : ~SMESHGUI_AddMeshElementDlg()
415 // purpose  : Destroys the object and frees any allocated resources
416 //=================================================================================
417 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
418 {
419   delete mySimulation;
420 }
421
422 //=================================================================================
423 // function : Init()
424 // purpose  :
425 //=================================================================================
426 void SMESHGUI_AddMeshElementDlg::Init()
427 {
428   GroupC1->show();
429   Constructor1->setChecked(true);
430   myEditCurrentArgument = LineEditC1A1;
431   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
432
433   /* reset "Add to group" control */
434   GroupGroups->setChecked( false );
435   GroupGroups->setVisible( myElementType != SMDSAbs_0DElement );
436
437   myNbOkNodes = 0;
438   myActor = 0;
439
440   /* signals and slots connections */
441   connect(buttonOk, SIGNAL(clicked()),     SLOT(ClickOnOk()));
442   connect(buttonCancel, SIGNAL(clicked()), SLOT(ClickOnCancel()));
443   connect(buttonApply, SIGNAL(clicked()),  SLOT(ClickOnApply()));
444   connect(buttonHelp, SIGNAL(clicked()),   SLOT(ClickOnHelp()));
445
446   connect(SelectButtonC1A1, SIGNAL(clicked()), SLOT(SetEditCurrentArgument()));
447   connect(LineEditC1A1, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
448   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
449   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
450   /* to close dialog if study frame change */
451   connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
452   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), SLOT(ClickOnCancel()));    
453
454   if (Reverse)
455     connect(Reverse, SIGNAL(stateChanged(int)), SLOT(CheckBox(int)));
456
457   // set selection mode
458   SMESH::SetPointRepresentation(true);
459
460   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
461     aViewWindow->SetSelectionMode( NodeSelection );
462
463   myBusy = false;
464
465   SelectionIntoArgument();
466 }
467
468 //=================================================================================
469 // function : ClickOnApply()
470 // purpose  :
471 //=================================================================================
472 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
473 {
474   if( !isValid() )
475     return;
476
477   if (myNbOkNodes && !mySMESHGUI->isActiveStudyLocked()) {
478     myBusy = true;
479     SMESH::long_array_var anArrayOfIndices = new SMESH::long_array;
480     anArrayOfIndices->length(myNbNodes);
481     bool reverse = (Reverse && Reverse->isChecked());
482     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
483     for (int i = 0; i < aListId.count(); i++)
484       if (reverse)
485         anArrayOfIndices[i] = aListId[ myNbNodes - i - 1 ].toInt();
486       else
487         anArrayOfIndices[i] = aListId[ i ].toInt();
488
489     bool addToGroup = GroupGroups->isChecked();
490     QString aGroupName;
491
492     SMESH::SMESH_GroupBase_var aGroup;
493     int idx = 0;
494     if( addToGroup ) {
495       aGroupName = ComboBox_GroupName->currentText();
496       for ( int i = 1; i < ComboBox_GroupName->count(); i++ ) {
497         QString aName = ComboBox_GroupName->itemText( i );
498         if ( aGroupName == aName && ( i == ComboBox_GroupName->currentIndex() || idx == 0 ) )
499           idx = i;
500       }
501       if ( idx > 0 && idx < myGroups.count() ) {
502         SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( myGroups[idx-1] );
503         if ( !aGeomGroup->_is_nil() ) {
504           int res = SUIT_MessageBox::question( this, tr( "SMESH_WRN_WARNING" ),
505                                                tr( "MESH_STANDALONE_GRP_CHOSEN" ).arg( aGroupName ),
506                                                tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 );
507           if ( res == 1 ) return;
508         }
509         aGroup = myGroups[idx-1];
510       }
511     }
512
513     long anElemId = -1;
514     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
515     switch (myElementType) {
516     case SMDSAbs_0DElement:
517       anElemId = aMeshEditor->Add0DElement(anArrayOfIndices[0]); break;
518     case SMDSAbs_Edge:
519       anElemId = aMeshEditor->AddEdge(anArrayOfIndices.inout()); break;
520     case SMDSAbs_Face: {
521       if(myIsPoly)
522         anElemId = aMeshEditor->AddPolygonalFace(anArrayOfIndices.inout());
523       else
524         anElemId = aMeshEditor->AddFace(anArrayOfIndices.inout());
525       break;
526     }
527     case SMDSAbs_Volume:
528       anElemId = aMeshEditor->AddVolume(anArrayOfIndices.inout()); break;
529     default: break;
530     }
531
532     if ( anElemId != -1 && addToGroup && !aGroupName.isEmpty() ) {
533       SMESH::SMESH_Group_var aGroupUsed;
534       if ( aGroup->_is_nil() ) {
535         // create new group 
536         aGroupUsed = SMESH::AddGroup( myMesh, (SMESH::ElementType)myElementType, aGroupName );
537         if ( !aGroupUsed->_is_nil() ) {
538           myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroupUsed));
539           ComboBox_GroupName->addItem( aGroupName );
540         }
541       }
542       else {
543         SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
544         if ( !aGeomGroup->_is_nil() ) {
545           aGroupUsed = myMesh->ConvertToStandalone( aGeomGroup );
546           if ( !aGroupUsed->_is_nil() && idx > 0 ) {
547             myGroups[idx-1] = SMESH::SMESH_GroupBase::_duplicate(aGroupUsed);
548             SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser();
549           }
550         }
551         else
552           aGroupUsed = SMESH::SMESH_Group::_narrow( aGroup );
553       }
554
555       if ( !aGroupUsed->_is_nil() ) {
556         SMESH::long_array_var anIdList = new SMESH::long_array;
557         anIdList->length( 1 );
558         anIdList[0] = anElemId;
559         aGroupUsed->Add( anIdList.inout() );
560       }
561     }
562
563     SALOME_ListIO aList; aList.Append( myActor->getIO() );
564     mySelector->ClearIndex();
565     mySelectionMgr->setSelectedObjects( aList, false );
566
567     SMESH::UpdateView();
568     mySimulation->SetVisibility(false);
569
570     buttonOk->setEnabled(false);
571     buttonApply->setEnabled(false);
572
573     myEditCurrentArgument->setText("");
574
575     myBusy = false;
576
577     SMESHGUI::Modified();
578   }
579 }
580
581 //=================================================================================
582 // function : ClickOnOk()
583 // purpose  :
584 //=================================================================================
585 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
586 {
587   ClickOnApply();
588   ClickOnCancel();
589 }
590
591 //=================================================================================
592 // function : ClickOnCancel()
593 // purpose  :
594 //=================================================================================
595 void SMESHGUI_AddMeshElementDlg::ClickOnCancel()
596 {
597   //mySelectionMgr->clearSelected();
598   mySimulation->SetVisibility(false);
599   SMESH::SetPointRepresentation(false);
600   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
601     aViewWindow->SetSelectionMode( ActorSelection );
602   disconnect(mySelectionMgr, 0, this, 0);
603   mySMESHGUI->ResetState();
604   reject();
605 }
606
607 //=================================================================================
608 // function : ClickOnHelp()
609 // purpose  :
610 //=================================================================================
611 void SMESHGUI_AddMeshElementDlg::ClickOnHelp()
612 {
613   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
614   if (app)
615     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""),
616                              myHelpFileName);
617   else {
618     QString platform;
619 #ifdef WIN32
620     platform = "winapplication";
621 #else
622     platform = "application";
623 #endif
624     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
625                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
626                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
627                                                                  platform)).
628                              arg(myHelpFileName));
629   }
630 }
631
632 //=================================================================================
633 // function : onTextChange()
634 // purpose  :
635 //=================================================================================
636 void SMESHGUI_AddMeshElementDlg::onTextChange (const QString& theNewText)
637 {
638   if (myBusy) return;
639   myBusy = true;
640
641   myNbOkNodes = 0;
642
643   buttonOk->setEnabled(false);
644   buttonApply->setEnabled(false);
645
646   mySimulation->SetVisibility(false);
647
648   // hilight entered nodes
649   SMDS_Mesh* aMesh = 0;
650   if (myActor)
651     aMesh = myActor->GetObject()->GetMesh();
652
653   if (aMesh) {
654     TColStd_MapOfInteger newIndices;
655
656     QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
657     bool allOk = true;
658     for (int i = 0; i < aListId.count(); i++) {
659       if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
660         {
661           newIndices.Add( n->GetID() );
662           myNbOkNodes++;
663         }
664       else
665         allOk = false;  
666     }
667
668     mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
669     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
670       aViewWindow->highlight( myActor->getIO(), true, true );
671
672     myNbOkNodes = ( allOk && myNbNodes == aListId.count() );
673
674     if (myIsPoly)
675       {
676         if ( !allOk || myElementType != SMDSAbs_Face || aListId.count() < 3 )
677           myNbOkNodes = 0;
678         else
679           myNbOkNodes = aListId.count();
680       }
681   }
682
683   if(myNbOkNodes) {
684     buttonOk->setEnabled(true);
685     buttonApply->setEnabled(true);
686     displaySimulation();
687   }
688
689   myBusy = false;
690 }
691
692 //=================================================================================
693 // function : SelectionIntoArgument()
694 // purpose  : Called when selection has changed
695 //=================================================================================
696 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
697 {
698   if (myBusy) return;
699
700   // clear
701   myNbOkNodes = 0;
702   myActor = 0;
703
704   myBusy = true;
705   myEditCurrentArgument->setText("");
706   myBusy = false;
707
708   if (!GroupButtons->isEnabled()) // inactive
709     return;
710
711   buttonOk->setEnabled(false);
712   buttonApply->setEnabled(false);
713
714   mySimulation->SetVisibility(false);
715   //  SMESH::SetPointRepresentation(true);
716
717   QString aCurrentEntry = myEntry;
718
719   // get selected mesh
720   SALOME_ListIO aList;
721   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
722
723   if (aList.Extent() != 1)
724     return;
725
726   Handle(SALOME_InteractiveObject) anIO = aList.First();
727   myEntry = anIO->getEntry();
728   myMesh = SMESH::GetMeshByIO(anIO);
729   if (myMesh->_is_nil())
730     return;
731
732   // process groups
733   if ( !myMesh->_is_nil() && myEntry != aCurrentEntry ) {
734     myGroups.clear();
735     ComboBox_GroupName->clear();
736     ComboBox_GroupName->addItem( QString() );
737     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
738     for ( int i = 0, n = aListOfGroups.length(); i < n; i++ ) {
739       SMESH::SMESH_GroupBase_var aGroup = aListOfGroups[i];
740       if ( !aGroup->_is_nil() && aGroup->GetType() == (SMESH::ElementType)myElementType ) {
741         QString aGroupName( aGroup->GetName() );
742         if ( !aGroupName.isEmpty() ) {
743           myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroup));
744           ComboBox_GroupName->addItem( aGroupName );
745         }
746       }
747     }
748   }
749
750   myActor = SMESH::FindActorByEntry(anIO->getEntry());
751   if (!myActor)
752     return;
753
754   // get selected nodes
755   QString aString = "";
756   int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,myActor->getIO(),aString);
757   myBusy = true;
758   myEditCurrentArgument->setText(aString);
759   myBusy = false;
760   if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 ) {
761     myNbNodes = nbNodes;
762   } else if (myNbNodes != nbNodes) {
763     return;
764   }
765
766   // OK
767   myNbOkNodes = nbNodes;
768
769   buttonOk->setEnabled(true);
770   buttonApply->setEnabled(true);
771
772   displaySimulation();
773 }
774
775 //=================================================================================
776 // function : displaySimulation()
777 // purpose  :
778 //=================================================================================
779 void SMESHGUI_AddMeshElementDlg::displaySimulation()
780 {
781   if (myNbOkNodes && GroupButtons->isEnabled()) {
782     SMESH::TElementSimulation::TVTKIds anIds;
783     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
784     for (int i = 0; i < aListId.count(); i++)
785       anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
786
787     if (Reverse && Reverse->isChecked())
788       reverse(anIds.begin(),anIds.end());
789
790     vtkIdType aType = 0;
791     if (myIsPoly)
792       switch ( myElementType ) {
793       case SMDSAbs_Face  : aType = VTK_POLYGON; break;
794       default: return;
795       }
796     else {
797       switch (myNbNodes) {
798       case 2: aType = VTK_LINE; break;
799       case 3: aType = VTK_TRIANGLE; break;
800       case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
801       case 8: aType = VTK_HEXAHEDRON; break;
802       default: return;
803       }
804     }
805
806     mySimulation->SetPosition(myActor,aType,anIds);
807     SMESH::UpdateView();
808   }
809 }
810
811 //=================================================================================
812 // function : SetEditCurrentArgument()
813 // purpose  :
814 //=================================================================================
815 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
816 {
817   QPushButton* send = (QPushButton*)sender();
818   if (send == SelectButtonC1A1) {
819     LineEditC1A1->setFocus();
820     myEditCurrentArgument = LineEditC1A1;
821   }
822   SelectionIntoArgument();
823 }
824
825 //=================================================================================
826 // function : DeactivateActiveDialog()
827 // purpose  :
828 //=================================================================================
829 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
830 {
831   if (GroupConstructors->isEnabled()) {
832     GroupConstructors->setEnabled(false);
833     GroupC1->setEnabled(false);
834     GroupButtons->setEnabled(false);
835     mySimulation->SetVisibility(false);
836     mySMESHGUI->ResetState();
837     mySMESHGUI->SetActiveDialogBox(0);
838   }
839 }
840
841 //=================================================================================
842 // function : ActivateThisDialog()
843 // purpose  :
844 //=================================================================================
845 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
846 {
847   /* Emit a signal to deactivate the active dialog */
848   mySMESHGUI->EmitSignalDeactivateDialog();
849
850   GroupConstructors->setEnabled(true);
851   GroupC1->setEnabled(true);
852   GroupButtons->setEnabled(true);
853
854   SMESH::SetPointRepresentation(true);
855
856   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
857     aViewWindow->SetSelectionMode( NodeSelection );
858   SelectionIntoArgument();
859 }
860
861 //=================================================================================
862 // function : enterEvent()
863 // purpose  :
864 //=================================================================================
865 void SMESHGUI_AddMeshElementDlg::enterEvent (QEvent*)
866 {
867   if (GroupConstructors->isEnabled())
868     return;
869   ActivateThisDialog();
870 }
871
872 //=================================================================================
873 // function : closeEvent()
874 // purpose  :
875 //=================================================================================
876 void SMESHGUI_AddMeshElementDlg::closeEvent (QCloseEvent*)
877 {
878   /* same than click on cancel button */
879   ClickOnCancel();
880 }
881
882 //=================================================================================
883 // function : hideEvent()
884 // purpose  : caused by ESC key
885 //=================================================================================
886 void SMESHGUI_AddMeshElementDlg::hideEvent (QHideEvent*)
887 {
888   if (!isMinimized())
889     ClickOnCancel();
890 }
891
892 //=================================================================================
893 // function : CheckBox()
894 // purpose  :
895 //=================================================================================
896 void SMESHGUI_AddMeshElementDlg::CheckBox (int state)
897 {
898   if (!myNbOkNodes)
899     return;
900
901   if (state >= 0) {
902     mySimulation->SetVisibility(false);
903     displaySimulation();
904   }
905 }
906
907 //=================================================================================
908 // function : keyPressEvent()
909 // purpose  :
910 //=================================================================================
911 void SMESHGUI_AddMeshElementDlg::keyPressEvent( QKeyEvent* e )
912 {
913   QDialog::keyPressEvent( e );
914   if ( e->isAccepted() )
915     return;
916
917   if ( e->key() == Qt::Key_F1 ) {
918     e->accept();
919     ClickOnHelp();
920   }
921 }
922
923 //=================================================================================
924 // function : isValid
925 // purpose  :
926 //=================================================================================
927 bool SMESHGUI_AddMeshElementDlg::isValid()
928 {
929   if( GroupGroups->isChecked() && ComboBox_GroupName->currentText().isEmpty() ) {
930     SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ), tr( "GROUP_NAME_IS_EMPTY" ) );
931     return false;
932   }
933   return true;
934 }